//********************************************************************//
//********************************************************************//
//**																**//
//**	dateFormat.js         			 							**//
//**	JavaScript based date formating methods (based on PHP)		**//					//does not support e or T
//**    	      					  	  							**//
//** 	Doucmentation at software.ben-robinson.com					**//
//**    	      					  	  							**//
//**    	      					  	  							**//
//**	Copywrite 2007 ben-robinson.com								**//
//**	Your are free you use and modify this as you like, 			**//
//**	please do not distribute modified versions with out			**//
//**	express permission (contact via ben-robinson.com)			**//
//**																**//
//**	This code is provided AS IS with no warranties. 			**//
//**	I am in no way responsible for any loss caused 				**//
//**	by failure of this code.									**//
//**																**//
//**																**//
//********************************************************************//
//********************************************************************//


 


//****************************************************//
//**	International Users							**//
//**	Change text values here for your language	**//
//****************************************************//

	var monthNames = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
	var dayNames = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');

//****************************************************//



Date.prototype.format = function(format)
	{
		var formatedDate = '';
		var skipNext = false;
		for (var i=0; i<format.length; i++) {
			if (skipNext) { formatedDate += format.charAt(i); skipNext = false;
			} else if (format.charAt(i)=='$') { skipNext = true;
			} else if (format.charAt(i)=='d') { formatedDate += fix2Digits(this.getDate());
			} else if (format.charAt(i)=='D') { formatedDate += dayNames[this.getDay()].slice(0,3);
			} else if (format.charAt(i)=='j') { formatedDate += this.getDate();
			} else if (format.charAt(i)=='l') { formatedDate += dayNames[this.getDay()];
			} else if (format.charAt(i)=='N') { formatedDate += this.getISODay();
			} else if (format.charAt(i)=='S') { formatedDate += getEnglishOrdinal(this.getDate());
			} else if (format.charAt(i)=='w') { formatedDate += this.getDay();
			} else if (format.charAt(i)=='z') { formatedDate += this.getDayOfYear();
			} else if (format.charAt(i)=='W') { formatedDate += this.getISODate().week;
			} else if (format.charAt(i)=='F') { formatedDate += monthNames[this.getMonth()];
			} else if (format.charAt(i)=='m') { formatedDate += fix2Digits(this.getMonth()+1);
			} else if (format.charAt(i)=='M') { formatedDate += monthNames[this.getMonth()].slice(0,3);
			} else if (format.charAt(i)=='n') { formatedDate += this.getMonth()+1;
			} else if (format.charAt(i)=='t') { formatedDate += (this.getMonth()==1 && this.isLeapYear()) ? 29 : monthDays[this.getMonth()];
			} else if (format.charAt(i)=='L') { formatedDate += (this.isLeapYear()) ? 1 : 0;
			} else if (format.charAt(i)=='o') { formatedDate += this.getISODate().year;
			} else if (format.charAt(i)=='Y') { formatedDate += this.getFullYear();
			} else if (format.charAt(i)=='y') { formatedDate += this.getFullYear().toString().slice(2);
			} else if (format.charAt(i)=='a') { formatedDate += (this.getHours()>12) ? 'pm' : 'am';
			} else if (format.charAt(i)=='A') { formatedDate += (this.getHours()>12) ? 'PM' : 'AM';
			} else if (format.charAt(i)=='B') { formatedDate += this.getBeats();
			} else if (format.charAt(i)=='g') { formatedDate += (this.getHours()>12) ? this.getHours()-12 : this.getHours();
			} else if (format.charAt(i)=='G') { formatedDate += this.getHours();
			} else if (format.charAt(i)=='h') { formatedDate += fix2Digits((this.getHours()>12) ? this.getHours()-12 : this.getHours());
			} else if (format.charAt(i)=='H') { formatedDate += fix2Digits(this.getHours());
			} else if (format.charAt(i)=='i') { formatedDate += fix2Digits(this.getMinutes());
			} else if (format.charAt(i)=='s') { formatedDate += fix2Digits(this.getSeconds());
			//} else if (format.charAt(i)=='e') { formatedDate += '';	//???
			} else if (format.charAt(i)=='I') { formatedDate += (this.inDaylightSaving()==1) ? 1 : 0;
			} else if (format.charAt(i)=='O') { formatedDate += this.getTimezoneOffsetFormatted();
			} else if (format.charAt(i)=='P') { formatedDate += this.getTimezoneOffsetFormatted(true);
			//} else if { (format.charAt(i)=='T') { formatedDate += '';	//???
			} else if (format.charAt(i)=='Z') { formatedDate += this.getTimezoneOffset()*60;
			} else if (format.charAt(i)=='U') { formatedDate += parseInt(this.valueOf()/1000);
			} else if (format.charAt(i)=='c') { formatedDate += this.getFullYear() + '-' + fix2Digits(this.getMonth()+1) + '-' + fix2Digits(this.getDate()) + 'T' + fix2Digits(this.getHours()) + ':' + fix2Digits(this.getMinutes()) + ':' + fix2Digits(this.getSeconds()) + this.getTimezoneOffsetFormatted(true);
			} else if (format.charAt(i)=='r') { formatedDate += dayNames[this.getDay()].slice(0,3) + ', ' + this.getDate() + ' ' + monthNames[this.getMonth()].slice(0,3) + ' ' + this.getFullYear() + ' ' + fix2Digits(this.getHours()) + ':' + fix2Digits(this.getMinutes()) + ':' + fix2Digits(this.getSeconds()) + ' ' + this.getTimezoneOffsetFormatted();
			} else { formatedDate += format.charAt(i);
			}
		}
		
		return formatedDate		
	}
	
function fix2Digits(n)
	{
		var fixed=n;
		if (n<10) fixed='0'+n;
		return fixed
	}
	
Date.prototype.getDayOfYear = function()
	{
		return parseInt((this-new Date(this.getFullYear() + '/1/1'))/86400000)
	}
Date.prototype.isLeapYear = function()
	{
		return new Date(this.getFullYear(), 1, 29).getMonth() == 1;
	}
function getEnglishOrdinal(n)
	{
		var eo;
		var d=n.toString();
		if (d.charAt(d.length-1)==1 && (d.charAt(0)!=1||d.length==1)) eo = 'st';
		else if (d.charAt(d.length-1)==2 && (d.charAt(0)!=1||d.length==1)) eo = 'nd';
		else if (d.charAt(d.length-1)==3 && (d.charAt(0)!=1||d.length==1)) eo = 'rd';
		else eo = 'th';
		return eo;
	}
Date.prototype.getTimezoneOffsetFormatted = function(colon)
	{
		var s='-';
		var m=this.getTimezoneOffset().toString();
		if (m.slice(0,1) == '-') {
			s='+';
			m=m.slice(1,99)
		}
		var h = Math.floor(parseInt(m)/60);
		m=m%60;
		
		if (h==0&&m==0) s='+';
		
		var returnStr;
		returnStr = s + fix2Digits(h);
		if(colon) returnStr += ':'
		returnStr += fix2Digits(m);
		return returnStr
	}
Date.prototype.getISODay = function()
	{
		var ISODay = this.getDay();
		if (ISODay==0) ISODay=7;
		return ISODay;
	}
Date.prototype.getISODate = function()
	{
		var yr = this.getFullYear();
		var dyNo = this.getDayOfYear()+1;
		var jan1Dy = new Date(this.getFullYear() + '/01/01').getISODay();
		var ISOdy = this.getISODay();
		var ISOyr, ISOwk;
		
		if (dyNo <= 8-jan1Dy && jan1Dy > 4) {
			ISOyr = yr-1;
			ISOwk = (jan1Dy==5 || (jan1Dy==6 && new Date((yr-1) + '/1/1').isLeapYear())) ? 53 : 52
		} else {
			ISOyr = yr;
		}
		if (ISOyr == yr) {
			var dys = (this.isLeapYear()) ? 366 : 365;
			if (dys-dyNo < 4-ISOdy) {
				ISOyr = yr+1
				ISOwk = 1
			}
		}
		if (ISOyr == yr) {
			ISOwk = parseInt((dyNo+(7-ISOdy)+(jan1Dy-1))/7);
			if (jan1Dy > 4) ISOwk -= 1;
		}
		
		return { year: ISOyr, week: fix2Digits(ISOwk), day: ISOdy }
	}
Date.prototype.getBeats = function()
	{
		var b = parseInt((this.getSeconds() + (this.getMinutes()*60) + (this.getHours()*3600))/86.4);
		b=fix2Digits(b);
		if (b<100) b='0'+b
		return b
	}
Date.prototype.inDaylightSaving = function()
	{
		var ds = this.getDaylightSavingChangeDate();
		var s=ds.start;
		var e=ds.end;
		
		if (s===null || e===null) return -1;
		
		if (s.valueOf()<e.valueOf()) {
			return (this.valueOf() > s.valueOf() && this.valueOf() < e.valueOf()) ? 1 : 0;
		} else {
			return (this.valueOf() > e.valueOf() && this.valueOf() < s.valueOf()) ? 0 : 1;
		}

	}
Date.prototype.getDaylightSavingChangeDate = function()
	{
		var testDate = new Date(this.getFullYear() + '/02/01 00:00:00');
		var changeDate = null;
		
		var d1 = null;
		var d2 = null;
		var chgDir1 = 1;
		var chgDir2 = 1;
		
		var changeDir = 1;
		var lastTZO;
		
		while (testDate.getMonth() < 11)
			{
				var preDate = new Date(testDate);
				lastTZO = testDate.getTimezoneOffset();
				testDate.setHours(testDate.getHours()+1,0,0,0);
				if(lastTZO!=testDate.getTimezoneOffset() || preDate.valueOf()==testDate.valueOf()) {
					if(d1===null) {
						d1=new Date(testDate);
						if(preDate.getTimezoneOffset()<testDate.getTimezoneOffset()) chgDir1=0;
						testDate.setMonth(8);
					} else {
						d2=new Date(testDate);
						if(preDate.getTimezoneOffset()<testDate.getTimezoneOffset()) chgDir2=0;
					}
					
				}
				if(preDate.valueOf()==testDate.valueOf()) testDate.setHours(testDate.getHours()+2,0,0,0);
				
				if (testDate.getMonth() == 4) testDate.setMonth(8)
			}
		
		var s, e
		if (chgDir1==1)	{ s=d1; e=d2; } else { s=d2; e=d1; }

		return { start:s, end:e };
	}

var monthDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
