// Native Extensions

Date.prototype.MSJsonDateFormat = function(){
  
  this.day = this.getDate();
  this.month = this.getMonth() + 1;
  this.year = this.getFullYear();
  this.hour = this.getHours();
  this.minute = this.getMinutes();
  this.second = this.getSeconds();
  this.ms = this.getMilliseconds();

  this.monthToDays = function(year, month) {
    var add = 0;
    var result = 0;
    if ((year % 4 == 0) && ((year % 100 != 0) || ((year % 100 == 0) && (year % 400 == 0)))) add++;

    switch (month) {
      case 0: return 0;
      case 1: result = 31; break;
      case 2: result = 59; break;
      case 3: result = 90; break;
      case 4: result = 120; break;
      case 5: result = 151; break;
      case 6: result = 181; break;
      case 7: result = 212; break;
      case 8: result = 243; break;
      case 9: result = 273; break;
      case 10: result = 304; break;
      case 11: result = 334; break;
      case 12: result = 365; break;
    }
    if (month > 1) result += add;
    return result;
  }

  this.dateToTicks = function(year, month, day) {
    var a = parseInt((year - 1) * 365);
    var b = parseInt((year - 1) / 4);
    var c = parseInt((year - 1) / 100);
    var d = parseInt((a + b) - c);
    var e = parseInt((year - 1) / 400);
    var f = parseInt(d + e);
    var monthDays = this.monthToDays(year, month - 1);
    var g = parseInt((f + monthDays) + day);
    var h = parseInt(g - 1);
    return h * 86400000; //0000;
  }

  this.timeToTicks = function(hour, minute, second) {
    return (((hour * 3600) + minute * 60) + second) * 10000000;
  }

  var ticks = this.dateToTicks(this.year, this.month, this.day) + this.timeToTicks(this.hour, this.minute, this.second) + (this.ms * 10000);
  ticks -= 62135596800000; //0000;
  
  return '/Date(' + ticks.toString() + '+0000)/';

}

//
// jQuery Extensions
//

//var msJsonDateFormatRegExp = new RegExp();
////msJsonDateFormatRegExp.compile('^\\\\/Date\\((\\d{1,})(\\+|-)(\\d{4,4})\\)\\\\/$', '');
//msJsonDateFormatRegExp.compile('"/Date\\((\\d{1,})(\\+|-)(\\d{4,4})\\)/"', '');

//$.extend({

//  CreateDotNetRemoteMethodCall: function(endPoint, methodName){

//    endPoint += '/' + methodName + '?';

//    for (var argumentIndexer = 2; argumentIndexer < arguments.length; argumentIndexer++){

//      endPoint += (argumentIndexer - 2).toString() + '=';

//      var jsonEncoded = JSON.stringify(arguments[argumentIndexer]);

//      alert(arguments[argumentIndexer]);
//      alert(jsonEncoded);

//      if (msJsonDateFormatRegExp.test(jsonEncoded)){
//        var msJsonDateOriginal = jsonEncoded.match(msJsonDateFormatRegExp)[0];
//        var msJsonDatePatched = msJsonDateOriginal.replace(/\//g, '\\/');
//        jsonEncoded = jsonEncoded.replace(
//          msJsonDateOriginal, msJsonDatePatched
//        );alert(jsonEncoded);
//      }      
//      endPoint += encodeURIComponent(jsonEncoded);

//      if (argumentIndexer < (arguments.length - 1)){
//        endPoint += '&';
//      }

//    }
//alert(endPoint);
//    return endPoint;

//  }

//});

$.extend({
	getUrlParam : function (param) {
		var regex = '[?&]' + param + '=([^&#]*)';
		var results = (new RegExp(regex)).exec(window.location.href);
		if(results) return results[1];
		return '';
	}
});

$.extend({
     formatCurrency : function (num) {
        num = num.toString().replace(/\£|\,/g,'');

        if(isNaN(num))
            num = "0";

        sign = (num == (num = Math.abs(num)));
        num = Math.floor(num * 100 + 0.50000000001);
        pence = num % 100;
        pounds = Math.floor(num / 100).toString();

        if(pence < 10)
            pence = "0" + pence;

        for (var i = 0; i < Math.floor((pounds.length - (1 + i)) / 3); i++)
            pounds = pounds.substring(0, pounds.length - (4 * i + 3)) + ',' + pounds.substring(pounds.length - (4 * i + 3));

        // Note the use of the unicode expansion for a pound sign...
        return (((sign)?'':'-') + '\u00A3' + pounds + '.' + pence);
    }

});

$.fn.extend({
	setHrefNoAction : function( ){
         return this.each(function() {
				$(this).attr("href","#"); 
				$(this).bind("click",function(event){
					event.preventDefault();
				})  
          }); 
    }
});
