/************************************************************************

Date.format()
	parameters:
		format - accpets any variation of the following list
		yyyy  is a 4-digit year - i.e., 2002   
		yy    is a 2-digit year - i.e., 02
		month is the full month - i.e., September
		mon   is the first three letters of the month - i.e., Sep
		mmm   is the number of the month - i.e., 9
		hh    is hours - i.e., 3
		hh24  is hours in military time - i.e., 3 
		mm    is minutes (always 2-digit) - i.e., 05
		ss    is seconds (always 2-digit) - i.e., 08
		ddd   is the first three letters of the day - i.e., Wed
		dd    is the numerical day of the month - i.e, 25
		day   is the full day of the week - i.e., Wednesday
		timezone is the the timezone in hours from GMT - i.e., GMT+5
		time24   is the time based on a 24 hour clock - i.e., 18:24   
		time     is the time based on am/pm - i.e., 6:24PM  
		meridian AM or PM
		
	examples:
		myDate = newDate();
		myDate.format("day, month dd, yyyy hh:mm:ss timezone");
		would return "Wednesday, September 25, 2002 12:14:11 GMT-5"
		
		myDate = new Date('5/21/2002 11:22:08 PM');
		myDate.format('ddd, mmm/dd/yy hh:mm:ss meridian');
******************************************************************************/ 
 
//Store the date info in the Date object
Date.prototype.Months = ["January", "February", "March", 
                         "April", "May", "June", "July", 
                         "August", "September", "October", 
                         "November", "December"];

Date.prototype.Days = ["Sunday", "Monday", "Tuesday", 
                       "Wednesday", "Thursday", 
                       "Friday", "Saturday"];

Date.prototype.format = DateFormat;

function DateFormat(sFormat) {
   var sDate = sFormat;

   //yyyy  is a 4-digit year - i.e., 2002  
   sDate = sDate.replace( new RegExp("yyyy", "gi"), this.getFullYear() );
   //yy    is a 2-digit year - i.e., 02
   sDate = sDate.replace( new RegExp("yy", "gi"), new String( this.getFullYear() ).substring(2,4) );
   //month is the full month - i.e., September
   sDate = sDate.replace( new RegExp("month", "gi"), this.Months[this.getMonth()] );
   //mon   is the first three letters of the month - i.e., Sep
   sDate = sDate.replace( new RegExp("mon", "gi"), new String( this.Months[this.getMonth()] ).substring(0,3) );
   //mmm   is the number of the month - i.e., 9
   sDate = sDate.replace( new RegExp("mmm", "gi"), (this.getMonth() + 1) );   
   //hh    is hours - i.e., 3
   sDate = sDate.replace( new RegExp("hh", "gi"), (this.getHours() > 12) ? (this.getHours() - 12) : (this.getHours()) );
   //hh24    is hours - i.e., 3
   sDate = sDate.replace( new RegExp("hh", "gi"), this.getHours() );
   //mm    is minutes (always 2-digit) - i.e., 05
   var mm = new String( this.getMinutes() );
   if (mm.length == 1) mm = "0" + mm; //pad if single digit
   sDate = sDate.replace( new RegExp("mm", "gi"), mm );
   //ss    is seconds (always 2-digit) - i.e., 08
   var ss = new String( this.getSeconds() );
   if (ss.length == 1) ss = "0" + ss; //pad if single digit
   sDate = sDate.replace( new RegExp("ss", "gi"), ss ); 
   //ddd   is the first three letters of the day - i.e., Wed
   sDate = sDate.replace( new RegExp("ddd", "gi"), new String( this.Days[this.getDay()] ).substring(0,3) );
   //dd    is the numerical day of the month - i.e, 25
   sDate = sDate.replace( new RegExp("dd", "gi"), this.getDate() );
   //day   is the full day of the week - i.e., Wednesday
   sDate = sDate.replace( new RegExp("day", "gi"), this.Days[this.getDay()] );
   //meridian  AM/PM
   sDate = sDate.replace( new RegExp("meridian", "gi"), ((this.getHours() < 12) ? 'AM' : 'PM') );

   //timezone is the the timezone in hours from GMT - i.e., GMT+5
   var d = new Date();
   tz = d.getTimezoneOffset();
   timezone = "";
   if (tz < 0)
      timezone = "GMT-" +  tz / 60;
   else if (tz == 0)
      timezone = "GMT";
   else
      timezone = "GMT+" + tz / 60;
   sDate = sDate.replace( new RegExp("timezone", "gi"), timezone );
   
   //time24   is the time based on a 24 hour clock - i.e., 18:24   
   var minutes = new String( this.getMinutes() );
   if (minutes.length == 1) minutes = "0" + minutes; //pad if single digit
   var time24 = new String( this.getHours() + ":" + minutes );
   sDate = sDate.replace( new RegExp("time24", "gi"), time24 );
   
   //time     is the time based on am/pm - i.e., 6:24PM
   var time;
   var ampm;
   var hour = this.getHours();
   if ( hour < 12) {
      if (hour == 0) hour = 12;
         ampm = "AM"
   } else {
      if (hour !=12)
         hour = hour - 12;
      ampm = "PM";   
   }
   time = new String(hour + ":" + minutes + ampm);     
   sDate = sDate.replace( new RegExp("time", "gi"), time );

   return sDate;   
}

//some simple date formatting functions based on Date.format()
Date.prototype.toShortDateString = function() {
	return this.format('mmm/dd/yyyy');
}

Date.prototype.toShortTimeString = function() {
	return this.format('hh:mm meridian');
}

//DateInterval "enum"
var DateInterval = new Object();
DateInterval.Day = 'd';
DateInterval.DayOfYear = 'y';
DateInterval.Hour = 'h';
DateInterval.Minute = 'n';
DateInterval.Month = 'm';
DateInterval.Quarter = 'q';
DateInterval.Second = 's';
DateInterval.Weekday = 'w';
DateInterval.WeekOfYear = 'ww';
DateInterval.Year = 'yyyy';
DateInterval.Millisecond = 'ss';

//adds the specified interval to a date
function Date_AddInterval(dDate, nValue, sInterval) {
	var nYears = 0, nMonths = 0, nDays = 0, nHours = 0, nMinutes = 0, nSeconds = 0, nMilliseconds = 0;
	
	if (sInterval == DateInterval.Year) {
		nYears = nValue;
	} else if (sInterval == DateInterval.Month) {
		nMonths = nValue;
	} else if (sInterval == DateInterval.Day) {
		nDays = nValue;
	} else if (sInterval == DateInterval.Hour) {
		nHours = nValue;
	} else if (sInterval == DateInterval.Minute) {
		nMinutes = nValue;
	} else if (sInterval == DateInterval.Second) {
		nSeconds = nValue;
	} else if (sInterval == DateInterval.Millisecond) {
		nMilliseconds = nValue;
	}
	
	return new Date(dDate.getFullYear() + nYears, dDate.getMonth() + nMonths, dDate.getDate() + nDays, dDate.getHours() + nHours, dDate.getMinutes() + nMinutes, dDate.getSeconds() + nSeconds, dDate.getMilliseconds() + nMilliseconds)
}

//add native methods to the Date object to support date addition/subtraction
Date.prototype.addInterval = function(nValue, sInterval) {
	return Date_AddInterval(this, nValue, sInterval);
}

Date.prototype.addMilliseconds = function(nValue) {
	return Date_AddInterval(this, nValue, DateInterval.Millisecond);
}

Date.prototype.addSeconds = function(nValue) {
	return Date_AddInterval(this, nValue, DateInterval.Second);
}

Date.prototype.addMinutes = function(nValue) {
	return Date_AddInterval(this, nValue, DateInterval.Minute);
}

Date.prototype.addHours = function(nValue) {
	return Date_AddInterval(this, nValue, DateInterval.Hour);
}

Date.prototype.addDays = function(nValue) {
	return Date_AddInterval(this, nValue, DateInterval.Day);
}

Date.prototype.addMonths = function(nValue) {
	return Date_AddInterval(this, nValue, DateInterval.Month);
}

Date.prototype.addYears = function(nValue) {
	return Date_AddInterval(this, nValue, DateInterval.Year);
}
