//	written	by Tan Ling Wee	on 2 Dec 2001
//	last updated 23 June 2002
//	email :	fuushikaden@yahoo.com
//	Modified by Paul Morel on 6 May 2003 -- Condensed and rearranged code
//	Modified by Tom Summerford (tjsummerford@yahoo.com) on 17 June 2004 
//				expanded date formatting
//				removed all images
//				prefaced numerous functions, id's and variables with 'pc'
//				transferred most style information into a stylsheet

//You can edit these variables
var fixedX = -1 // x position (-1 if to appear below control)
var fixedY = -1 // y position (-1 if to appear below control)
var showWeekNumber = 0 // 0 - don't show; 1 - show
var showToday = 1 // 0 - don't show; 1 - show
var gotoString = "Go To Current Month"
var todayString = "Today is"
var weekString = "Wk"
var scrollLeftMessage = "Click to scroll to previous month. Hold mouse button to scroll automatically."
var scrollRightMessage = "Click to scroll to next month. Hold mouse button to scroll automatically."
var pcSelectMonthMessage = "Click to select a month."
var pcSelectYearMessage = "Click to select a year."
var selectDateMessage = "Select [date] as date." // do not replace [date], it will be replaced by date.
var styleAnchor="text-decoration:none;color:black;"
var styleLightBorder="border:1px solid #a0a0a0;"
var monthName =	new Array("January","February","March","April","May","June","July","August","September","October","November","December")
var dayFullName = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");

//Don't Edit these variables
var crossobj, crossMonthObj, crossYearObj, monthSelected, yearSelected, dateSelected, omonthSelected, oyearSelected, odateSelected, monthConstructed, yearConstructed, intervalID1, intervalID2, timeoutID1, timeoutID2, ctlToPlaceValue, ctlNow, dateFormat, nStartingYear
var bPageLoaded=false
var ie=document.all
var dom=document.getElementById
var ns4=document.layers
var today = new Date()
var dateNow=today.getDate()
var monthNow=today.getMonth()
var yearNow=today.getFullYear()
var img	= new Array() 
var bShow = false;
var HolidaysCounter = 0
var Holidays = new Array()

if (dom){
	document.write ("<style type='text/css'>")
	document.write ("#pcCalendar, #pcSelectMonth, #pcSelectYear {z-index:+999; position:absolute; visibility:hidden;} #pcCalendar td, #pcCalendar th, #pcCalendar div, #pcCalendar span, #pcSelectMonth td, #pcSelectYear td{font:11px arial;} #pcCalendar a {"+ styleAnchor +"}")
	document.write ("#pcHeader {background:#00A;} #pcHeader span {border:1px solid #36F; cursor:pointer} #pcHeader td {padding:2px; color:#FFF;}")
	document.write ("#pcCalendar th {width:14%; font-weight:bold;} #pcCalendar td.pcDay {text-align:center;} #pcCalendar td.pcSelected {background:#FF9;} #pcCalendar td.pcWeekend a {color:#666;} #pcCalendar td#pcToday {border:1px solid #f99}")
	document.write ("</style><div onclick='bShow=true' id='pcCalendar'>")
	document.write ("<table border='0' cellspacing='2' cellpadding='2' bgcolor='#FFFFFF' width='"+((showWeekNumber==1)?250:200)+"' style='border:1px solid #a0a0a0; background:#FFF;'>")
	document.write ("<tr><td id='pcHeader'><table border='0' cellspacing='0' cellspadding='0' width='100%'><tr><td id='pcCaption'></td><td align='right'><a href='javascript:pcHideCalendar()' style='color:#FFF; text-decoration:none; border:1px solid #36F; cursor:pointer' onmouseover='this.style.borderColor=\"#88AAFF\";window.status=\""+scrollRightMessage+"\"' onmouseout='clearInterval(intervalID1); this.style.borderColor=\"#3366FF\";window.status=\"\"'>&nbsp;x&nbsp;</a></td></tr></table></td></tr>")
	document.write ("<tr><td style='padding:5px' bgcolor='#ffffff'><span id='pcContent'></span></td></tr>")
	if (showToday==1) document.write ("<tr bgcolor='#f0f0f0'><td style='padding:4px' align=center><span id='lblToday'></span></td></tr>")
	document.write ("</table></div><div id='pcSelectMonth'></div><div id='pcSelectYear'></div>");
}
document.onkeypress = function hidecal1 () { 
	if (document.onkeypress.keyCode==27) pcHideCalendar()
}
document.onclick = function hidecal2 () { 		
	if (!bShow) pcHideCalendar()
	bShow = false
}
if(ie){
	pcInit()
} else {
	window.onload=pcInit
}

// hides <select> and <applet> objects (for IE only)
function pcHideElement( elmID, overDiv ){
	if( ie ){
		for( i = 0; i < document.all.tags( elmID ).length; i++ ){
  			obj = document.all.tags( elmID )[i];
  			if( !obj || !obj.offsetParent ){
    			continue;
  			}
			// Find the element's offsetTop and offsetLeft relative to the BODY tag.
			objLeft   = obj.offsetLeft;
			objTop    = obj.offsetTop;
			objParent = obj.offsetParent;
			while( objParent.tagName.toUpperCase() != "BODY" ) {
				objLeft  += objParent.offsetLeft;
				objTop   += objParent.offsetTop;
				objParent = objParent.offsetParent;
			}
			objHeight = obj.offsetHeight;
			objWidth = obj.offsetWidth;
			if(( overDiv.offsetLeft + overDiv.offsetWidth ) <= objLeft );
			else if(( overDiv.offsetTop + overDiv.offsetHeight ) <= objTop );
			else if( overDiv.offsetTop >= ( objTop + objHeight + obj.height ));
			else if( overDiv.offsetLeft >= ( objLeft + objWidth ));
			else obj.style.visibility = "hidden";
		}
	}
}
     
//unhides <select> and <applet> objects (for IE only)
function pcShowElement( elmID ){
	if( ie ){
		for( i = 0; i < document.all.tags( elmID ).length; i++ ){
  			obj = document.all.tags( elmID )[i];
			if( !obj || !obj.offsetParent ){
    				continue;
			}
			obj.style.visibility = "";
		}
	}
}
function pcHolidayRec (d, m, y, desc){
	this.d = d
	this.m = m
	this.y = y
	this.desc = desc
}
function pcAddHoliday (d, m, y, desc) {Holidays[HolidaysCounter++] = new pcHolidayRec ( d, m, y, desc )}
function pcInit()	{
	if (!ns4) {
		crossobj=(dom)?document.getElementById("pcCalendar").style : ie? document.all.pcCalendar : document.pcCalendar
		pcHideCalendar()
		crossMonthObj=(dom)?document.getElementById("pcSelectMonth").style : ie? document.all.pcSelectMonth	: document.pcSelectMonth
		crossYearObj=(dom)?document.getElementById("pcSelectYear").style : ie? document.all.pcSelectYear : document.pcSelectYear
		monthConstructed=false;
		yearConstructed=false;
		if (showToday==1) {
			document.getElementById("lblToday").innerHTML =	todayString + " <a onmousemove='window.status=\""+gotoString+"\"' onmouseout='window.status=\"\"' title='"+gotoString+"' href='javascript:monthSelected=monthNow;yearSelected=yearNow;pcConstructCalendar();'>"+ dayFullName[today.getDay()].substring(0,3)+", " + monthName[monthNow].substring(0,3)+ " " + dateNow + ", " +	yearNow	+ "</a>"
		}
		sHTML1="<span onmouseover='this.style.borderColor=\"#88AAFF\";window.status=\""+scrollLeftMessage+"\"' onclick='javascript:pcDecMonth()' onmouseout='clearInterval(intervalID1); this.style.borderColor=\"#3366FF\";window.status=\"\"' onmousedown='clearTimeout(timeoutID1);timeoutID1=setTimeout(\"pcStartpcDecMonth()\",500)' onmouseup='clearTimeout(timeoutID1);clearInterval(intervalID1)'>&nbsp;&lt;&nbsp;</span>&nbsp;"
		sHTML1+="<span onmouseover='this.style.borderColor=\"#88AAFF\";window.status=\""+scrollRightMessage+"\"' onmouseout='clearInterval(intervalID1); this.style.borderColor=\"#3366FF\";window.status=\"\"' onclick='pcIncMonth()' onmousedown='clearTimeout(timeoutID1);timeoutID1=setTimeout(\"pcStartpcDecMonth()\",500)'	onmouseup='clearTimeout(timeoutID1);clearInterval(intervalID1)'>&nbsp;&gt;&nbsp;</span>&nbsp"
		sHTML1+="<span id='pcSpanMonth' onmouseover='this.style.borderColor=\"#88AAFF\";window.status=\""+pcSelectMonthMessage+"\"' onmouseout='this.style.borderColor=\"#3366FF\";window.status=\"\"' onclick='pcPopUpMonth()'></span>&nbsp;"
		sHTML1+="<span id='pcSpanYear' onmouseover='this.style.borderColor=\"#88AAFF\";window.status=\""+pcSelectYearMessage+"\"'	onmouseout='this.style.borderColor=\"#3366FF\";window.status=\"\"'	onclick='pcPopUpYear()'></span>&nbsp;"
		document.getElementById("pcCaption").innerHTML  =	sHTML1
		bPageLoaded=true
	}
}

function pcHideCalendar()	{
	crossobj.visibility="hidden"
	if (crossMonthObj != null){crossMonthObj.visibility="hidden"}
	if (crossYearObj !=	null){crossYearObj.visibility="hidden"}
    	pcShowElement( 'SELECT' );
	pcShowElement( 'APPLET' );
}
function pcConstructDate(d,m,y) {
	sTmp = new Date(y, m, d)
	sTmp = pcFormatDate(sTmp, dateFormat)
	return sTmp
}
function pcCloseCalendar() {
	var sTmp
	pcHideCalendar();
	ctlToPlaceValue.value =	pcConstructDate(dateSelected, monthSelected, yearSelected)
}
// Month Pulldown
function pcStartpcDecMonth() {intervalID1=setInterval("pcDecMonth()",80)}
function pcStartpcDecMonth() {intervalID1=setInterval("pcIncMonth()",80)}
function pcIncMonth () {
	monthSelected++
	if (monthSelected>11) {
		monthSelected=0
		yearSelected++
	}
	pcConstructCalendar()
}
function pcDecMonth () {
	monthSelected--
	if (monthSelected<0) {
		monthSelected=11
		yearSelected--
	}
	pcConstructCalendar()
}
function pcConstructMonth() {
	popDownYear()
	if (!monthConstructed) {
		sHTML =	""
		for	(i=0; i<12;	i++) {
			sName =	monthName[i];
			if (i==monthSelected) sName = "<b>"+ sName +"</b>"
			sHTML += "<tr><td id='m" + i + "' onmouseover='this.style.backgroundColor=\"#FFCC99\"' onmouseout='this.style.backgroundColor=\"\"' style='cursor:pointer' onclick='monthConstructed=false;monthSelected=" + i + ";pcConstructCalendar();popDownMonth();event.cancelBubble=true'>&nbsp;" + sName + "&nbsp;</td></tr>"
		}
		document.getElementById("pcSelectMonth").innerHTML = "<table width='70' style='border:1px solid #a0a0a0;' bgcolor='#FFFFDD' cellspacing='0' onmouseover='clearTimeout(timeoutID1)'	onmouseout='clearTimeout(timeoutID1);timeoutID1=setTimeout(\"popDownMonth()\",100);event.cancelBubble=true'>" +	sHTML +	"</table>"
		monthConstructed=true
	}
}
function pcPopUpMonth() {
	pcConstructMonth()
	crossMonthObj.visibility = (dom||ie)? "visible"	: "show"
	leftOffset = parseInt(crossobj.left) + document.getElementById("pcSpanMonth").offsetLeft
	if (ie) {leftOffset += 6}
	crossMonthObj.left = leftOffset +"px"
	crossMonthObj.top = parseInt(crossobj.top) + 26 +"px"
}
function popDownMonth()	{crossMonthObj.visibility= "hidden"}

// Year Pulldown
function incYear() {
	for	(i=0; i<7; i++){
		newYear	= (i+nStartingYear)+1
		if (newYear==yearSelected)
		{ txtYear =	"&nbsp;<B>"	+ newYear +	"</B>&nbsp;" }
		else
		{ txtYear =	"&nbsp;" + newYear + "&nbsp;" }
		document.getElementById("y"+i).innerHTML = txtYear
	}
	nStartingYear ++;
	bShow=true
}
function decYear() {
	for	(i=0; i<7; i++){
		newYear	= (i+nStartingYear)-1
		if (newYear==yearSelected)
		{ txtYear =	"&nbsp;<B>"	+ newYear +	"</B>&nbsp;" }
		else
		{ txtYear =	"&nbsp;" + newYear + "&nbsp;" }
		document.getElementById("y"+i).innerHTML = txtYear
	}
	nStartingYear --;
	bShow=true
}
function pcSelectYear(nYear) {
	yearSelected=parseInt(nYear+nStartingYear);
	yearConstructed=false;
	pcConstructCalendar();
	popDownYear();
}
function constructYear() {
	popDownMonth()
	sHTML =	""
	if (!yearConstructed) {
		sHTML =	"<tr><td align='center'	onmouseover='this.style.backgroundColor=\"#FC9\"' onmouseout='clearInterval(intervalID1);this.style.backgroundColor=\"\"' style='cursor:pointer'	onmousedown='clearInterval(intervalID1);intervalID1=setInterval(\"decYear()\",30)' onmouseup='clearInterval(intervalID1)'>-</td></tr>"
		j = 0
		nStartingYear =	yearSelected-3
		for	(i=(yearSelected-3); i<=(yearSelected+3); i++) {
			sName =	i;
			if (i==yearSelected){
				sName =	"<B>" +	sName +	"</B>"
			}
			sHTML += "<tr><td id='y" + j + "' onmouseover='this.style.backgroundColor=\"#FC9\"' onmouseout='this.style.backgroundColor=\"\"' style='cursor:pointer' onclick='pcSelectYear("+j+");event.cancelBubble=true'>&nbsp;" + sName + "&nbsp;</td></tr>"
			j ++;
		}
		sHTML += "<tr><td align='center' onmouseover='this.style.backgroundColor=\"#FC9\"' onmouseout='clearInterval(intervalID2);this.style.backgroundColor=\"\"' style='cursor:pointer' onmousedown='clearInterval(intervalID2);intervalID2=setInterval(\"incYear()\",30)'	onmouseup='clearInterval(intervalID2)'>+</td></tr>"
		document.getElementById("pcSelectYear").innerHTML	= "<table width='44' style='border:1px solid #a0a0a0;'	bgcolor='#FFFFDD' onmouseover='clearTimeout(timeoutID2)' onmouseout='clearTimeout(timeoutID2);timeoutID2=setTimeout(\"popDownYear()\",100)' cellspacing=0>" + sHTML + "</table>"
		yearConstructed	= true
	}
}
function popDownYear() {
	clearInterval(intervalID1)
	clearTimeout(timeoutID1)
	clearInterval(intervalID2)
	clearTimeout(timeoutID2)
	crossYearObj.visibility= "hidden"
}
function pcPopUpYear() {
	var leftOffset
	constructYear()
	crossYearObj.visibility	= (dom||ie)? "visible" : "show"
	leftOffset = parseInt(crossobj.left) + document.getElementById("pcSpanYear").offsetLeft
	if (ie) {leftOffset += 6}
	crossYearObj.left =	leftOffset +"px"
	crossYearObj.top = parseInt(crossobj.top) +	26 +"px"
}
function pcWeekNbr(n) {
	// Algorithm used:
	// From Klaus Tondering's Calendar document (The Authority/Guru)
	// hhtp://www.tondering.dk/claus/calendar.html
	// a = (14-month) / 12
	// y = year + 4800 - a
	// m = month + 12a - 3
	// J = day + (153m + 2) / 5 + 365y + y / 4 - y / 100 + y / 400 - 32045
	// d4 = (J + 31741 - (J mod 7)) mod 146097 mod 36524 mod 1461
	// L = d4 / 1460
	// d1 = ((d4 - L) mod 365) + L
	// WeekNumber = d1 / 7 + 1

	year = n.getFullYear();
	month = n.getMonth() + 1;
	day = n.getDate();
	a = Math.floor((14-month) / 12);
	y = year + 4800 - a;
	m = month + 12 * a - 3;
	b = Math.floor(y/4) - Math.floor(y/100) + Math.floor(y/400);
	J = day + Math.floor((153 * m + 2) / 5) + 365 * y + b - 32045;
	d4 = (((J + 31741 - (J % 7)) % 146097) % 36524) % 1461;
	L = Math.floor(d4 / 1460);
	d1 = ((d4 - L) % 365) + L;
	week = Math.floor(d1/7) + 1;
	return week;
}

function pcConstructCalendar () {
	var aNumDays = Array (31,0,31,30,31,30,31,31,30,31,30,31)
	var dateMessage
	var startDate =	new Date (yearSelected,monthSelected,1)
	var endDate
	var dayClass
	var dayID
	if (monthSelected==1){
		endDate	= new Date (yearSelected,monthSelected+1,1);
		endDate	= new Date (endDate	- (24*60*60*1000));
		numDaysInMonth = endDate.getDate()
	}
	else{
		numDaysInMonth = aNumDays[monthSelected];
	}
	datePointer	= 0
	dayPointer = startDate.getDay()
	if (dayPointer<0){
		dayPointer = 6
	}
	sHTML =	"<table width='100%' cellspadding='0' cellspacing='0' border='0'><tr>"
	for	(i=0; i<7; i++)	{
		sHTML += "<th>"+ dayFullName[i].substring(0,1) +"</th>"
	}
	sHTML +="</tr><tr>"
	for	( var i=1; i<=dayPointer;i++ ){
		sHTML += "<td>&nbsp;</td>"
	}
	for	( datePointer=1; datePointer<=numDaysInMonth; datePointer++ ){
		dayPointer++;
		sHint = ""
		dayClass = "pcDay "
		dayID = ""
		
		for (k=0;k<HolidaysCounter;k++){
			if ((parseInt(Holidays[k].d)==datePointer)&&(parseInt(Holidays[k].m)==(monthSelected+1))){
				if ((parseInt(Holidays[k].y)==0)||((parseInt(Holidays[k].y)==yearSelected)&&(parseInt(Holidays[k].y)!=0))){
					sHint+=sHint==""?Holidays[k].desc:"\n"+Holidays[k].desc
				}
			}
		}
		var regexp= /\"/g
		sHint = sHint.replace(regexp,"&quot;")
		
	  //selected date
		if ((datePointer==odateSelected) &&	(monthSelected==omonthSelected)	&& (yearSelected==oyearSelected)) dayClass += "pcSelected "
	  //today
		if ((datePointer==dateNow)&&(monthSelected==monthNow)&&(yearSelected==yearNow)) dayID += "pcToday"
	  //weekend
		if	(dayPointer % 7 == 1 || dayPointer % 7 == 0) dayClass += "pcWeekend "
		
		sHTML += "<td class='"+ dayClass +"'"
		if (dayID.toString().length > 0 ) sHTML += " id='"+ dayID +"' "
		sHTML += "><a onmousemove='window.status=\"\"' onmouseout='window.status=\"\"' title=\"" + sHint + "\" href='javascript:dateSelected="+datePointer+";pcCloseCalendar();'>" + datePointer + "</a></td>"
		
		if (dayPointer % 7 == 0) sHTML += "</tr><tr>" 
	}
	document.getElementById("pcContent").innerHTML   = sHTML
	document.getElementById("pcSpanMonth").innerHTML = "&nbsp;" + monthName[monthSelected] + "&nbsp;"
	document.getElementById("pcSpanYear").innerHTML =	"&nbsp;" + yearSelected	+ "&nbsp;"
}
function pcFormatDate(dt, format) {
	/*
	%m Month as a decimal (2)
	%mm 2 digit Month (02)
	%B Full month name (February)
	%b Abbreviated month name (Feb )
	%d Day of the month (23)
	%dd 2 digit day of the month (23)
	%Y Year with century (1998)
	%y Year without century (98)
	%w Weekday as integer (0 is Sunday)
	%a Abbreviated day name (Fri)
	%A Weekday Name (Friday)
	*/
	var re;
	var dtReturn = format;
	var dt = new Date(dt);
	if (isNaN(dt)) dtReturn = new Date()
	else {
		
	  //Load variables
		var intMonth = dt.getMonth()+1;
		var intMonth2 = intMonth.toString();
		var strMonthName = monthName[dt.getMonth()];
		var strMonthNameShort = strMonthName;
		var intDate = dt.getDate();
		var intDate2 = intDate.toString();
		var intWeekDay = dt.getDay();
		var DayName = dayFullName[dt.getDay()];
		var FullYear = dt.getFullYear().toString();
		
		if (intMonth2.length < 2) intMonth2 = "0"+ intMonth2
		if (intDate2.length < 2) intDate2 = "0"+ intDate2
		if (strMonthNameShort.length > 5) strMonthNameShort = strMonthNameShort.substring(0,3);
		
	  //Build return string
		//Month
		dtReturn = dtReturn.replace(/%mm/, intMonth2);
		dtReturn = dtReturn.replace(/%m/, intMonth);
		dtReturn = dtReturn.replace(/%B/, strMonthName);
		dtReturn = dtReturn.replace(/%b/, strMonthNameShort);
		//Date & Day
		dtReturn = dtReturn.replace(/%dd/, intDate2);
		dtReturn = dtReturn.replace(/%d/, intDate);
		dtReturn = dtReturn.replace(/%a/, DayName.substring(0,3));
		dtReturn = dtReturn.replace(/%A/, DayName);
		//Year
		dtReturn = dtReturn.replace(/%Y/, FullYear);
		dtReturn = dtReturn.replace(/%y/, FullYear.substring(2,4));
	}
	return dtReturn;
}
function pcLaunchCalendar(ctl, ctl2, format) {
	var leftpos=0
	var toppos=0
	var ctl2Date = pcFormatDate(ctl2.value, format);
	var offestX = arguments[3]
	var offestY = arguments[4]
	if (bPageLoaded){
		if ( crossobj.visibility ==	"hidden" ) {
			dateFormat = format;
			ctlToPlaceValue	= ctl2
			ctl2Date = new Date(ctl2Date);
			
			dateSelected	= ctl2Date.getDate();
			monthSelected	= ctl2Date.getMonth();
			yearSelected	= ctl2Date.getFullYear();
			
			odateSelected	= dateSelected
			omonthSelected	= monthSelected
			oyearSelected	= yearSelected
			
		  //Position the calendar
			aTag = ctl
			do {
				aTag = aTag.offsetParent;
				leftpos	+= aTag.offsetLeft;
				toppos += aTag.offsetTop;
			} while(aTag.tagName!="BODY");
			if (offestX == null) offestX = 175
			if (offestY == null) offestY = 2
			leftpos = fixedX == -1 ? ctl.offsetLeft + leftpos - offestX : fixedX
			toppos = fixedY == -1 ?	ctl.offsetTop +	toppos + ctl.offsetHeight +	offestY :	fixedY
			crossobj.left =	leftpos.toString() +"px"
			crossobj.top = toppos.toString() +"px"
			
			pcConstructCalendar (1, monthSelected, yearSelected);
			crossobj.visibility=(dom||ie)? "visible" : "show"
			pcHideElement('SELECT', document.getElementById("pcCalendar"));
			pcHideElement('APPLET', document.getElementById("pcCalendar"));
			bShow = true;
		}
		else{
			pcHideCalendar()
			if (ctlNow!=ctl) {pcLaunchCalendar(ctl, ctl2, format)}
		}
		ctlNow = ctl
	}
}