Date.prototype.getDayNum   = function(){return this.getDate()}
Date.prototype.setDayNum   = function(day){this.setDate(day)}
Date.prototype.getMonthNum = function(){return this.getMonth()+1 }
Date.prototype.setMonthNum = function(month){ this.setMonth(month-1)}
Date.prototype.getYearNum  = function(){return this.getFullYear()}
Date.prototype.setYearNum  = function(year){this.setYear(year)}
var calTitle = 'Calender';

Date.prototype.getDayOfWeekNum = function()
{
var day = this.getDay();
if (day == 6) 
	return 1;
if (day == 5)
	 return 0;
else return day+2;
}

Date.prototype.setDayMonthYear = function(day,month,year)
{
this.setDate(day);
month = parseInt(month,10)-1 ;
this.setMonth(month);
this.setFullYear(year);
}

Date.prototype.addDay = function()
{
	this.setDate(this.getDate()+1);
}

Date.prototype.addMonth = function()
{
	this.setMonth(this.getMonth()+1);
}

Date.prototype.addYear = function(){
	this.setYear(this.getFullYear()+1);
}

Date.prototype.subtractYear = function()
{
	this.setYear(this.getFullYear()-1);
}

Date.prototype.copyTo = function(date2)
{
	date2.setTime(this.getTime());
}

Date.prototype.monthIsLessThan = function(date2)
{
	var yr1 = this.getYearNum();
	var yr2 = date2.getYearNum();
	if (yr1<yr2) {
		return true;
	} else if (yr1==yr2){
		return this.getMonthNum()<date2.getMonthNum();
	} else return false;
}

Date.prototype.monthIsGreaterThan = function(date2)
{
var yr1 = this.getYearNum();
var yr2 = date2.getYearNum();
if (yr1>yr2) {
	return true;
} else if (yr1==yr2){
	return this.getMonthNum()>date2.getMonthNum();
} else return false;
}

Date.prototype.monthIsSameAs = function(date2)
{
if (this.getYearNum()==date2.getYearNum()){
	return this.getMonthNum()==date2.getMonthNum();
}
return false;
}

Date.prototype.dayIsSameAs = function(date2)
{
return this.getDayNum()==date2.getDayNum() && this.getMonthNum()==date2.getMonthNum() && this.getYearNum()==date2.getYearNum();
}

Date.isDate = function(day,month,year)
{
//Checks if a date is valid
if (year>999 && year<10000)
{
	var dt = new Date(year,month-1,day);
	return dt.getDate()==day && dt.getMonth()==month-1 && dt.getFullYear()==year;
}
return false;
}

//Calendar initialisation functions

function Calendar()
{
	
this.brand = "";
this.lang = "";	
this.width = 225; 
this.height = 230; 
this.x = 50;
this.y = 50;
this.todaysDate = new Date();
this.selectedDate = new Date();
this.earliestDate = new Date();
this.earliestDate.setDayMonthYear(1,1,1000);
this.latestDate = new Date();
this.latestDate.setDayMonthYear(1,1,3000);
this.viewDate = new Date();
this.counterDate = new Date();
this.outputWindow = new Object();
this.getFormDay = new Function();
this.getFormMonth = new Function();
this.getFormYear = new Function();
this.setFormDate = new Function();
this.id = Math.ceil(Math.random()*99999);
this.outputHtml = new Array();
this.styleSheetPath = "";
this.showTodayControl = true;
this.monthsText = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
this.dayText = new Array("M","T","W","T","F","S","S");
this.nextText = "<img src='./languages/eng/calnext.gif' alt='next' height='19' width='67' border='0'>";
this.prevText = "<img src='./languages/eng/calprevious.gif' alt='previous' height='19' width='67' border='0'>";
this.todayText = "Today";
this.closeText = "Close";
}
Calendar.prototype.setDisplay = function(brand ,lang )
{
this.brand = brand;
this.lang = lang;
this.stylesheetPath = "../pages/css/availability.css";
this.stylesheetPathColours = "../pages/css/calendar.css";
}
Calendar.prototype.setTodaysDate = function(day,month,year)
{
this.todaysDate.setDayMonthYear(day,month,year);
}
Calendar.prototype.setSelectedDate = function(day,month,year)
{
this.selectedDate.setDayMonthYear(day,month,year);
this.setViewDate(day,month,year);
}
Calendar.prototype.setEarliestDate = function(day,month,year)
{
this.earliestDate.setDayMonthYear(day,month,year);
}
Calendar.prototype.setLatestDate = function(day,month,year)
{
this.latestDate.setDayMonthYear(day,month,year);
}
Calendar.prototype.setViewDate = function(day,month,year)
{
	this.viewDate.setDayMonthYear(day,month,year);
}
Calendar.prototype.writeLn = function(text)
{
this.outputHtml.push( text );
}

Calendar.prototype.writeTableStart = function()
{
this.writeLn('<table width="200" border="0" cellspacing="0" cellpadding="0"><tr><td class="tableborder">');
if(this.brand =='c4y')
{
	this.writeLn('<table width="200" border="0" cellspacing="2" cellpadding="1">');
}
else
{
	this.writeLn('<table width="200" border="0" cellspacing="1" cellpadding="2">');
}
}
Calendar.prototype.writeYearTableStart = function()
{
	this.writeLn('<table width="200" border="0" cellspacing="0" cellpadding="1"><tr><td>');
	this.writeLn('<table width="200" border="0" cellspacing="0" cellpadding="0" style="margin-top:4px;">');
}
Calendar.prototype.writeTableDivider = function()
{
	this.writeLn('</td></tr><tr><td>');
}
Calendar.prototype.writeTableEnd = function()
{
	this.writeLn('</table>');
	this.writeLn('</td></tr></table>');
}
Calendar.prototype.writeCalendar = function()
{
this.outputHtml = new Array();
this.outputWindow.document.open();
this.writeLn('<html><head>');
if (!isIEPC()) this.writeLn('<base href="' + window.location.href + '">');
this.writeLn('<title>');
this.writeLn(calTitle);
this.writeLn('</title>');
this.writeLn('<link rel="StyleSheet" href="'+this.stylesheetPath+'" type="text/css">');
if (this.stylesheetPathColours) this.writeLn('<link rel="StyleSheet" href="'+this.stylesheetPathColours+'" type="text/css">');
this.writeLn('<style>td {text-align:center;}</style>');
this.writeLn('<script language="javascript" src="../../../js/Common.js" type="text/javascript"></script>');
this.writeLn('<script language="javascript" src="../../../../../js/Common.js" type="text/javascript"></script>');
this.writeLn('</head>');
this.writeLn('<body marginheight="0" marginwidth="0" topmargin="0" leftmargin="0" onload="AssociateDisableRefreshFunc();"><center>');
this.writeLn('<table border="0" width="100%" cellspacing="0" cellpadding="2"><tr><td class="background" align="center">');
this.writeYearControls();
this.writeTableDivider();
this.writeMonthControls();
this.writeTableDivider();
this.writeMonth();
if (this.showTodayControl)
{
	this.writeTableDivider();
	this.writeTodayControl();
}
this.writeTableDivider();
this.writeCloseControl();
this.writeLn('</td></tr></table>');
this.writeLn('</center></body></html>');
this.outputWindow.calendar = this;
this.outputWindow.document.write(this.outputHtml.join(""));
this.outputWindow.document.close();
this.outputHtml = "";
this.outputWindow.calendar = this;
}

Calendar.prototype.writeYearControls = function()
{
this.writeYearTableStart();
var earliestYear = this.earliestDate.getYearNum();
var viewYear = this.viewDate.getYearNum();
var latestYear = this.latestDate.getYearNum();
this.writeLn('<tr>');
if (viewYear>earliestYear)
{
	this.writeLn('<td class="yearbutton" width="75"><a href="javascript:calendar.viewPreviousYear()">' + this.prevText + '</a></td>');
}
else
{
	this.writeLn('<td width="75" class="yearbutton">&nbsp;</td>');
}
this.writeLn('<td width="50" class="year">' + this.viewDate.getYearNum() + '</td>');
if (viewYear<latestYear)
{
	this.writeLn('<td class="yearbutton" width="75"><a href="javascript:calendar.viewNextYear()">' + this.nextText + '</a></td>');
}
else
{
	this.writeLn('<td width="75" class="yearbutton">&nbsp;</td>');
}
this.writeLn('</tr>');
this.writeTableEnd();
}

Calendar.prototype.writeMonthControls = function()
{
this.viewDate.copyTo(this.counterDate);
this.counterDate.setMonthNum(1);
this.writeTableStart();
this.writeLn('<tr>');
for (var i=1;i<=12;i++)
{
	if (this.counterDate.monthIsLessThan(this.earliestDate) || this.counterDate.monthIsGreaterThan(this.latestDate)){
		this.writeLn('<td class="disabledmonth">'+this.monthsText[i-1]+'</td>');
	} else if (this.counterDate.monthIsSameAs(this.viewDate)){
		this.writeLn('<td class="viewmonth">'+this.monthsText[i-1]+'</td>');
	} else {
		this.writeLn('<td class="month"><a href="javascript:calendar.viewMonth('+i+')">'+this.monthsText[i-1]+'</a></td>');
	}

	if (i==6)
	{
		this.writeLn('</tr><tr>');
	}

	

	this.counterDate.addMonth();
}
this.writeLn('</tr>');
this.writeTableEnd();
}

Calendar.prototype.writeMonth = function()
{
this.viewDate.copyTo(this.counterDate);
this.writeTableStart();
this.writeLn('<tr align="center">');
for (var i=0;i<7;i++){
	this.writeLn('<td width="25" class="dayofweek">'+this.dayText[i]+'</td>');
}
this.writeLn('</tr>');
while (this.counterDate.getMonthNum()==this.viewDate.getMonthNum()){
	this.writeWeek();
}
this.writeTableEnd();
}

Calendar.prototype.writeWeek = function()
{
this.writeLn('<tr>');
var dayOfWeek = this.counterDate.getDayOfWeekNum();
this.writeBlankDays(dayOfWeek);
var i;
for (i=dayOfWeek;i<7;i++)
{
	if (this.counterDate.getMonthNum()==this.viewDate.getMonthNum()){
		this.writeDay();
	} else break;
}
this.writeBlankDays(7-i);
this.writeLn('</tr>');
}

Calendar.prototype.writeDay = function()
{
var day = this.counterDate.getDayNum();
var month = this.counterDate.getMonthNum();
var year = this.counterDate.getYearNum();
if (this.counterDate.dayIsSameAs(this.selectedDate)&& !this.counterDate.dayIsSameAs(this.earliestDate))
{
	this.writeLn('<td class="selectedday">'+this.counterDate.getDayNum()+'</td>');
}
else if (this.counterDate>=this.earliestDate && this.counterDate<=this.latestDate)
{
	this.writeLn('<td class="day"><a href="javascript:calendar.selectDay('+day+','+month+','+year+')">'+this.counterDate.getDayNum()+'</a></td>');
}
else
{
	this.writeLn('<td class="disabledday">'+this.counterDate.getDayNum()+'</td>');
}
this.counterDate.addDay();
}

Calendar.prototype.writeBlankDays = function(colspan)
{
if (colspan>0) this.writeLn('<td colspan="'+colspan+'" class="blankday">&nbsp;</td>');
}

Calendar.prototype.writeCloseControl = function()
{
this.writeTableStart();
this.writeLn('<tr><td class="control"><a href="javascript:calendar.close()">&nbsp; &nbsp; &nbsp; &nbsp; '+this.closeText+' &nbsp; &nbsp; &nbsp; &nbsp;</a></td></tr>');
this.writeTableEnd();
}

Calendar.prototype.writeTodayControl = function()
{
this.writeTableStart();
this.writeLn('<tr><td class="control"><a href="javascript:calendar.selectToday()">&nbsp; &nbsp; &nbsp; '+this.todayText+' &nbsp; &nbsp; &nbsp;</a></td></tr>');
this.writeTableEnd();
}

//User actions

function isIEPC()
{
return navigator.platform.indexOf("Win")!=-1 && navigator.appVersion.indexOf("MSIE")!=-1;
}

Calendar.prototype.open = function()
{
var formDay = this.getFormDay();
var formMonth = this.getFormMonth();
var formYear = this.getFormYear();
if (Date.isDate(formDay,formMonth,formYear))
{
	this.setSelectedDate(formDay,formMonth,formYear);
	if (this.selectedDate.monthIsLessThan(this.earliestDate))
	{
		this.earliestDate.copyTo(this.viewDate);
	} else if (this.selectedDate.monthIsGreaterThan(this.latestDate))
	{
		this.latestDate.copyTo(this.viewDate);
	}
}
if (isIEPC())
{
	this.close();		
}
this.outputWindow = window.open("#","calendar" + this.id,"width="+this.width+",height="+this.height+",top="+this.x+",left="+this.y+",noresize,location=no");
this.viewDate.setDayNum(1);
this.writeCalendar();
this.outputWindow.focus();
}
Calendar.prototype.close = function()
{
if (this.outputWindow && this.outputWindow.close) this.outputWindow.close();
}
Calendar.prototype.selectDay = function(day,month,year)
{
this.setSelectedDate(day,month,year);
this.setFormDate(day,month,year);
this.close();
}
Calendar.prototype.viewMonth = function(month)
{
this.viewDate.setMonthNum(month);
this.writeCalendar();
}
Calendar.prototype.selectToday = function()
{
this.todaysDate.copyTo(this.viewDate);
this.todaysDate.copyTo(this.selectedDate);
this.setFormDate(this.selectedDate.getDayNum(),this.selectedDate.getMonthNum(),this.selectedDate.getYearNum());
this.viewDate.setDayNum(1);
this.writeCalendar();
}
Calendar.prototype.viewPreviousYear = function()
{
this.viewDate.subtractYear();
this.viewDate.setMonthNum(12);
this.writeCalendar();
}
Calendar.prototype.viewNextYear = function()
{
this.viewDate.addYear();
this.viewDate.setMonthNum(1);
this.writeCalendar();
}
