//
// Modification to the Date object
//

/*extern Date */

Date._MD = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
Date._MS = [
		["J", "F", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D"],
		["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
		["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
	   ];

Date._DS = [
		["S", "M", "T", "W", "T", "F", "S"],
		["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
		["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
	   ];

Date.SUN = 0;
Date.MON = 1;
Date.TUE = 2;
Date.WED = 3;
Date.THU = 4;
Date.FRI = 5;
Date.SAT = 6;

Date.JAN = 0;
Date.FEB = 1;
Date.MAR = 2;
Date.APR = 3;
Date.MAY = 4;
Date.JUN = 5;
Date.JUL = 6;
Date.AUG = 7;
Date.SEP = 8;
Date.OCT = 9;
Date.NOV = 10;
Date.DEC = 11;

Date.getDaysInMonth = function(month, year)
{
	if ((((year % 4) === 0) &&	// Leap year every 4 years
	     (((year % 100) !== 0) ||	// Except on the turn of the century
	      ((year % 400) === 0))) &&	// But every 4th century
	    month == Date.FEB)		// February
	{
		return 29;
	}
	else
	{
		return Date._MD[month];
	}	
};

Date.prototype.getDaysInMonth = function(month, year)
{
	if (typeof year == "undefined")
	{
		year = this.getFullYear();
	}

	if (typeof month == "undefined")
	{
		month = this.getMonth();
	}
	return Date.getDaysInMonth(month, year);
};

Date.getWeeksInMonth = function(month, year)
{
	var tempDate = new Date(year, month, 1);

	var daysLeft = Date.getDaysInMonth(month, year);

	daysLeft -= (7 - tempDate.getDay());    // number of days in first week

	var numWeeks = 1;

	numWeeks += Math.ceil(daysLeft / 7);
	return numWeeks;
};

Date.prototype.getWeeksInMonth = function(month, year)
{
	if (typeof year == "undefined")
	{
		year = this.getFullYear();
	}

	if (typeof month == "undefined")
	{
		month = this.getMonth();
	}
	return Date.getWeeksInMonth(month, year);
};

Date.prototype.getNumDays = function()
{
	return Math.floor(this.getTime() / 86400000);
};

Date.prototype.getNumWeeks = function()
{
	return Math.floor(this.getTime() / 604800000);
};

function LZ(x) {return(x<0||x>9?"":"0")+x;}

// A partial implementation of PHP's date formating
Date.prototype.phpFormat = function(fmt)
{
	fmt=fmt+"";
	var result="";
	var c="";
	var Y=this.getFullYear();
	var n=this.getMonth()+1;
	var j=this.getDate();
	var w=this.getDay();
	var G=this.getHours();
	var I=this.getMinutes();
	var S=this.getSeconds();
	// Convert real date parts into formatted versions
	var value=[];
	value.d=LZ(j);
	value.D=Date._DS[1][w];
	value.j=j+"";
	value.l=Date._DS[2][w];
	value.N=(w===0?7:w)+"";
	value.w=w+"";
	value.F=Date._MS[2][n-1];
	value.m=LZ(n);
	value.M=Date._MS[1][n-1];
	value.n=n+"";
	value.t=this.getDaysInMonth();
	value.Y=Y+"";
	value.y=LZ(Y%100);
	value.a=(G>11?"pm":"am");
	value.A=(G>11?"PM":"AM");
	value.g=(G%12)+1+"";
	value.G=G+"";
	value.h=LZ((G%12)+1);
	value.H=LZ(G);
	value.i=LZ(I);
	value.s=LZ(S);
	for (var i = 0; i < fmt.length; i++)
	{
		c=fmt.charAt(i);
		if (value[c])
		{
			result = result + value[c];
		}
		else
		{
			result = result + c;
		}
	}
	return result;
};

Date.prototype.getMonthString = function()
{
	return Date._MS[2][this.getMonth()];
};

Date.prototype.eventString = function()
{
        return this.phpFormat("Ymd");
};

Date.prototype.parse = function(eventString)
{
	this.setFullYear(parseInt(eventString.substring(11, 15), 10));
	this.setMonth(parseInt(eventString.substring(15, 17), 10) - 1);
	this.setDate(parseInt(eventString.substring(17, 19), 10));
};

Date.parse = function(eventString)
{
	var d = new Date();
	d.setFullYear(parseInt(eventString.substring(11, 15), 10));
	d.setMonth(parseInt(eventString.substring(15, 17), 10) - 1);
	d.setDate(parseInt(eventString.substring(17, 19), 10));
	return d;
};

