
// literals *******************************************************************

// used as param unit in Date.add()
Date.MILLI = 1;
Date.SECOND = Date.MILLI * 1000;
Date.MINUTE = Date.SECOND * 60;
Date.HOUR = Date.MINUTE * 60;
Date.DAY = Date.HOUR * 24;
Date.WEEK = Date.Day * 7;
Date.MONTH = -1;
Date.YEAR = -2;

Date.DAYS_IN_MONTH = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

// methods ********************************************************************

function _Date_toCanonString()
{
	return this.getFullYear() +
			 _pad(this.getMonth() + 1) + 
			 _pad(this.getDate());
}

function _Date_getFullYear()
{
	var y = this.getYear();
	if(y < 100 && y > 0)
		y += 1900;
	return y;
}

function _Date_setFullYear(val)
{
	this.setYear(val);
}

function _Date_compareTo(other)
{
	return Date.compare(this, other);
}

function _Date_isLeapYear()
{
	return Date.leapYear(this.getFullYear());
}

function _Date_add(date, unit, amount)
{
	return Date.addDate(this, date, unit, amount);
}

function _Date_getDaysInMonth()
{
	return Date.daysInMonth(this.getFullYear(), this.getMonth());
}

// utility functions **********************************************************

function _isLeapYear(year)
{
	return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}

function _compareDate(d1, d2)
{
	return (new Date(d1)).getTime() - (new Date(d2)).getTime();
}
//Ticket:104396, To fix the daily recurrence events issue,
//Added parseInt() to amount when unit == Date.DAY
function _addDate(date, unit, amount)
{ 
	if(unit == Date.MONTH)
		date.setMonth(date.getMonth() + amount);
	else if(unit == Date.YEAR)
		date.setFullYear(date.getFullYear() + amount);
	else if(unit == Date.DAY)
		date.setDate(date.getDate() + parseInt(amount));
	else
		date.setTime(date.getTime() + unit * amount);
	return date;
}

function _getDaysInMonth(year, month)
{
	return month == 1 && Date.leapYear(year) ? 29 : Date.DAYS_IN_MONTH[month];
}

function _pad(n)
{
	return (n < 10 ? "0" : "") + n;
}

function buildDateAmPm(date, hour, minute, ampm)
{
	if (ampm == 'PM') {	if (hour != '12') {hour = '' + (parseInt(hour) + 12);} }		
	else { if (hour == '12') {hour = '0';} }
	
	var objDate = new Date(date);
	objDate.setHours(hour, minute);
	
	return (objDate);	
}

// initialization *************************************************************

Date.prototype.toCanonString = _Date_toCanonString;
if(!Date.prototype.getFullYear)
{
	Date.prototype.getFullYear = _Date_getFullYear;
	Date.prototype.setFullYear = _Date_setFullYear;
}
Date.prototype.isLeapYear = _Date_isLeapYear;
Date.prototype.compareTo = _Date_compareTo;
Date.prototype.add = _Date_add;
Date.prototype.getDaysInMonth = _Date_getDaysInMonth;

Date.leapYear = _isLeapYear;
Date.compare = _compareDate;
Date.addDate = _addDate;
Date.daysInMonth = _getDaysInMonth;

