//	written	by Tan Ling	Wee	on 2 Dec 2001
//	last updated 25 Sept 2006
//	email :	fuushikaden@yahoo.com
//  Modified (very little) by Michael on 09/25/06
//  email :	songweihua@gmail.com    
//	Modified (quite a bit) by Mark Rae (http://www.markrae.com) 25 Jul - 04 Aug 2006 
//		support for ASP.NET 2, specifically MasterPages & ContentPlaceHolders
//		upgraded to XHTML compliance (mainly the CSS)
//		support for usage as a stand-alone element i.e. not within a UserControl
//		support for the TextBox to be read-only
//		support for triggering a postback if the contents of the destination control change
//		support for correct positioning on FireFox & Netscape
//		fixed default year bug in Opera (showed as 106 instead of 2006 when the TextBox was blank)
//		fixed Esc key for hiding calendar in FireFox & Netscape
//		corrected the background transparency of the left, right, up & down arrows
//		removed redundant code (e.g. OtherFunctions, etc)
//		combined incMonth and decMonth into shiftMonth and incYear and decYear into shiftYear
//		generally tidied up the code - formatting, semi-colons at the ends of lines, etc...
//		tested on the following OS-browser combinations:
//			WinXP Pro + SP2 & IE6, IE7 Beta, FireFox 1.5.0.6, Netscape 8.1, Opera 9.0, Mozilla 1.7.13
//			MacOSX Tiger & Safari 2.0.4, FireFox 1.5.0.6, Netscape 7.2, Opera 9.0, Mozilla 1.7.13, Camino 1.0.2
//			Linux SuSE 10 & Konqueror 3.5.3, Epiphany 1.8.0, FireFox 1.5.0.6, Netscape 7.2, Opera 9.0, Mozilla 1.7.11
//		remaining bugs:
//			mouseover on arrows doesn't work correctly on some Mac browsers (aesthetic)
//			holding the mouse down on some Mac browsers causes the context menu to appear (annoying)

var intFixedX = -1;					// x position (-1 to align with the left of the calling control)
var intFixedY = -1;					// y position (-1 to align with the top of the calling control)
var bytFirstDay = 1;				// 0 = Sunday, 1 = Monday - starting at other days not currently supported
var blnShowWeekNumber = false;		// show / don't show the week number
var blnShowToday = false;			// show / don't show today's date at the bottom of the calendar
var strImageFolder = "../images/cal/";	// directory for images e.g. var strImageFolder="/img/"

var strGoto = "Go to current month"
var strToday = "Today:"
var strWeek = "Wk"
var strScrollLeft = "Click to scroll to previous month. Hold mouse button to scroll automatically."
var strScrollRight = "Click to scroll to next month. Hold mouse button to scroll automatically."
var strSelectMonth = "Click to select a month."
var strSelectYear = "Click to select a year."
var strSelectDate = "Select [date] as date." // do not replace [date], it will be replaced by date.

var	crossobj;
var crossMonthObj;
var crossYearObj;
var monthSelected;
var yearSelected;
var dateSelected;
var omonthSelected;
var oyearSelected;
var odateSelected;
var blnMonthConstructed;
var blnYearConstructed;
var intervalID1;
var intervalID2;
var timeoutID1;
var timeoutID2;
var objDestination;
var blnPostBack;
var ctlNow;
var strDateFormat;
var intStartingYear;

var	blnPageLoaded = false;
var	blnIE = document.all;	// unfinished - this is not the correct way to test for IE - e.g. Opera passes this test
var	blnDOM = document.getElementById;
var	blnNS4 = document.layers;

var	dtmNow = new Date();
var	bytCurrentDay = dtmNow.getDate();
var	bytCurrentMonth = dtmNow.getMonth();
var	intCurrentYear = dtmNow.getYear();	// not all browsers support getFullYear()
if(intCurrentYear < 2000)
{
	intCurrentYear += 1900;
}
var	astrImageSource = new Array("drop1.gif","drop2.gif","left1.gif","left2.gif","right1.gif","right2.gif");
var	aobjImage	= new Array();

var intHolidays = 0;
var aHolidays = new Array();

var blnShow = false;
var strHTML;

var	astrMonthName =	new	Array("January","February","March","April","May","June","July","August","September","October","November","December");
if (bytFirstDay == 0)
{
	astrDayName = new Array	("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
}
else
{
	astrDayName = new Array	("Mon","Tue","Wed","Thu","Fri","Sat","Sun")
}

if (blnDOM)
{
	for	(i = 0; i < astrImageSource.length; i++)
	{
		aobjImage[i] = new Image;
		aobjImage[i].src= aobjImage + astrImageSource[i];
	}
	document.write("<div onclick='blnShow=true;' id='calendar' class='div-style'><table width='" + ((blnShowWeekNumber) ? 250 : 220) + "px' class='table-style'><tr class='title-background-style'><td><table width='" + ((blnShowWeekNumber) ? 248 : 218) + "px'><tr><td class='title-style'><b><span id='caption'></span></b></td><td align='right'><a href='javascript:hideCalendar()'><img src='" + strImageFolder + "close.gif' width='15px' height='13px' border='0px' alt='Close the calendar'></a></td></tr></table></td></tr><tr><td class='body-style'><span id='content'></span></td></tr>");
		
	if (blnShowToday)
	{
		document.write ("<tr class='today-style'><td><span id='lblToday'></span></td></tr>");
	}
		
	document.write ("</table></div><div id='selectMonth' class='div-style'></div><div id='selectYear' class='div-style'></div>");
}

addHoliday(2006, 12, 25, 'Christmas Day');

function rewriteDIV()
{
    if (blnDOM)
    {
	    document.write("<div onclick='blnShow=true;' id='calendar' class='div-style'><table width='" + ((blnShowWeekNumber) ? 250 : 220) + "px' class='table-style'><tr class='title-background-style'><td><table width='" + ((blnShowWeekNumber) ? 248 : 218) + "px'><tr><td class='title-style'><b><span id='caption'></span></b></td><td align='right'><a  href='javascript:hideCalendar()'><img runat='server' src='" + strImageFolder + "close.gif' width='15px' height='13px' border='0px' alt='Close the calendar'></a></td></tr></table></td></tr><tr><td class='body-style'><span id='content'></span></td></tr>");
    		
	    if (blnShowToday)
	    {
		    document.write ("<tr class='today-style'><td><span id='lblToday'></span></td></tr>");
	    }
    		
	    document.write ("</table></div><div id='selectMonth' class='div-style'></div><div id='selectYear' class='div-style'></div>");
    }

}

/* hides <select> and <applet> objects (for IE only) */
function hideElement( elmID, overDiv )
{
	if(blnIE)
	{
		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;

			// unfinished - not too sure about this...
			if((overDiv.offsetLeft + overDiv.offsetWidth) <= objLeft);
			else if(( overDiv.offsetTop + overDiv.offsetHeight ) <= objTop );
			else if( overDiv.offsetTop >= ( objTop + objHeight ));
			else if( overDiv.offsetLeft >= ( objLeft + objWidth ));
			else
			{
				obj.style.visibility = "hidden";
			}
		}
	}
}
 
/* unhides <select> and <applet> objects (for IE only) */
function showElement(elmID)
{
	if(blnIE)
	{
		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 HolidayRec (pintYear, pbytMonth, pbytDay, pstrDescription)
{
	this.intYear = pintYear;
	this.bytMonth = pbytMonth;
	this.bytDay = pbytDay;
	this.strDescription = pstrDescription;
}

function addHoliday (pintYear, pbytMonth, pbytDay, pstrDescription)
{
	aHolidays[intHolidays++] = new HolidayRec(pintYear, pbytMonth, pbytDay, pstrDescription);
}

function swapImage(srcImg, destImg)
{
	document.getElementById(srcImg).setAttribute("src", strImageFolder + destImg);
}

function init()
{
	if (!blnNS4)
	{
	    //if (document.getElementById("calendar")==null) rewriteDIV();
		crossobj = (blnDOM) ? document.getElementById("calendar").style : blnIE? document.all.calendar : document.calendar;
		hideCalendar();
		crossMonthObj = (blnDOM) ? document.getElementById("selectMonth").style : blnIE? document.all.selectMonth : document.selectMonth;
		crossYearObj = (blnDOM) ? document.getElementById("selectYear").style : blnIE? document.all.selectYear : document.selectYear;

		blnMonthConstructed = false;
		blnYearConstructed = false;

		if (blnShowToday)
		{
			document.getElementById("lblToday").innerHTML =	strToday + " <a class='today-style' onmousemove='window.status=\"" + strGoto + "\"' onmouseout='window.status=\"\"' title='" + strGoto + "' href='javascript:monthSelected=bytCurrentMonth;yearSelected=intCurrentYear;constructCalendar();'>" + astrDayName[(dtmNow.getDay() - bytFirstDay == -1) ? 6 : (dtmNow.getDay() - bytFirstDay)] + ", " + bytCurrentDay + " " + astrMonthName[bytCurrentMonth].substring(0,3) + " " +	intCurrentYear	+ "</a>";
		}

		strHTML =  "<span id='spanLeft'  class='title-control-normal-style' onmouseover='swapImage(\"changeLeft\",\"left2.gif\");this.className=\"title-control-select-style\";window.status=\"" + strScrollLeft + "\"' onclick='javascript:shiftMonth(-1)' onmouseout='clearInterval(intervalID1);swapImage(\"changeLeft\",\"left1.gif\");this.className=\"title-control-normal-style\";window.status=\"\"' onmousedown='clearTimeout(timeoutID1);timeoutID1=setTimeout(\"StartDecMonth()\",500)' onmouseup='clearTimeout(timeoutID1);clearInterval(intervalID1)'>&nbsp<img id='changeLeft' src='" + strImageFolder + "left1.gif' width='10px' height='11px' border='0px'>&nbsp;</span>&nbsp;";
		strHTML += "<span id='spanRight' class='title-control-normal-style' onmouseover='swapImage(\"changeRight\",\"right2.gif\");this.className=\"title-control-select-style\";window.status=\"" + strScrollRight + "\"' onmouseout='clearInterval(intervalID1);swapImage(\"changeRight\",\"right1.gif\");this.className=\"title-control-normal-style\";window.status=\"\"' onclick='shiftMonth(1)' onmousedown='clearTimeout(timeoutID1);timeoutID1=setTimeout(\"StartIncMonth()\",500)'	onmouseup='clearTimeout(timeoutID1);clearInterval(intervalID1)'>&nbsp<img id='changeRight' src='" + strImageFolder + "right1.gif' width='10px' height='11px' border='0px'>&nbsp;</span>&nbsp;";
		strHTML += "<span id='spanMonth' class='title-control-normal-style' onmouseover='swapImage(\"changeMonth\",\"drop2.gif\");this.className=\"title-control-select-style\";window.status=\"" + strSelectMonth + "\"' onmouseout='swapImage(\"changeMonth\",\"drop1.gif\");this.className=\"title-control-normal-style\";window.status=\"\"' onclick='popUpMonth()'></span>&nbsp;";
		strHTML += "<span id='spanYear'  class='title-control-normal-style' onmouseover='swapImage(\"changeYear\",\"drop2.gif\");this.className=\"title-control-select-style\";window.status=\"" + strSelectYear + "\"'	onmouseout='swapImage(\"changeYear\",\"drop1.gif\");this.className=\"title-control-normal-style\";window.status=\"\"'	onclick='popUpYear()'></span>&nbsp;";
		document.getElementById("caption").innerHTML  =	strHTML;

		blnPageLoaded = true;
	}
}

function hideCalendar()	
{
	if(crossobj)
	{
		crossobj.visibility="hidden";
		if(crossMonthObj != null)
		{
			crossMonthObj.visibility = "hidden";
		}
		if(crossYearObj != null)
		{
			crossYearObj.visibility = "hidden";
		}

		showElement('SELECT');
		showElement('APPLET');
	}
}

function padZero(num) 
{
	return (num	< 10) ? '0' + num : num ;
}

function constructDate(d,m,y)
{
	strDate = strDateFormat;
	strDate = strDate.replace("dd", "<e>");
	strDate = strDate.replace("DD", "<e>");
	strDate = strDate.replace("d", "<d>");
	strDate = strDate.replace("<e>", padZero(d));
	strDate = strDate.replace("<d>", d);
	strDate = strDate.replace("MMMM", "<p>");
	strDate = strDate.replace("MMM", "<o>");
	strDate = strDate.replace("MM", "<n>");
	strDate = strDate.replace("M", "<m>");
	strDate = strDate.replace("<m>", m + 1);
	strDate = strDate.replace("<n>", padZero(m + 1));
	strDate = strDate.replace("<o>", astrMonthName[m].substring(0, 3));
	strDate = strDate.replace("<p>", astrMonthName[m]);
	strDate = strDate.replace("YYYY", y);
	return strDate.replace("yyyy", y);
}

function closeCalendar() 
{
	hideCalendar();

	strOldValue = objDestination.value;
	strNewValue = constructDate(dateSelected, monthSelected, yearSelected)
	if (strNewValue != strOldValue)
	{
		if (objDestination.readOnly = true)
		{
			objDestination.readOnly = false;
			objDestination.value = strNewValue;
			objDestination.readOnly = true;
		}
		else
		{
			objDestination.value = strNewValue;
		}
		if (blnPostBack)
		{
			__doPostBack(objDestination.id,'');
		}
	}
	document.body.style.cursor = 'default';
}

/*** Month Pulldown	***/

function StartDecMonth()
{
	intervalID1 = setInterval("shiftMonth(-1)", 80);
}

function StartIncMonth()
{
	intervalID1 = setInterval("shiftMonth(1)", 80);
}

function shiftMonth(pbytShift) 
{
	monthSelected += pbytShift;
	if (monthSelected > 11) 
	{
		monthSelected = 0;
		yearSelected++;
	}
	else if(monthSelected < 0) 
	{
		monthSelected = 11;
		yearSelected--;
	}
	constructCalendar();
}

function constructMonth() 
{
	popDownYear();
	if (!blnMonthConstructed) 
	{
		strHTML = ""
		for(i = 0; i < 12; i++) 
		{
			strName = astrMonthName[i];
			if(i == monthSelected)
			{
				strName = "<b>" + strName +	"</b>";
			}
			strHTML += "<tr><td id='m" + i + "' onmouseover='this.className=\"dropdown-select-style\"' onmouseout='this.className=\"dropdown-normal-style\"' onclick='blnMonthConstructed=false;monthSelected=" + i + ";constructCalendar();popDownMonth();event.cancelBubble=true'>&nbsp;" + strName + "&nbsp;</td></tr>";
		}
		document.getElementById("selectMonth").innerHTML = "<table width='70px' class='dropdown-style' cellspacing='0px' onmouseover='clearTimeout(timeoutID1)'	onmouseout='clearTimeout(timeoutID1);timeoutID1=setTimeout(\"popDownMonth()\",100);event.cancelBubble=true'>" +	strHTML + "</table>";
		blnMonthConstructed = true;
	}
}

function popUpMonth() 
{
	constructMonth();
	crossMonthObj.visibility = (blnDOM || blnIE)? "visible"	: "show";
	crossMonthObj.left = (parseInt(crossobj.left) + 50) + "px";
	crossMonthObj.top =	(parseInt(crossobj.top) + 26) + "px";
}

function popDownMonth()	
{
	crossMonthObj.visibility= "hidden";
}

/*** Year Pulldown ***/
function shiftYear(pbytShift) 
{
	for	(i = 0; i < 7; i++)
	{
		newYear	= (i + intStartingYear) + pbytShift;
		if(newYear == yearSelected)
		{
			txtYear = "&nbsp;<b>" + newYear + "</b>&nbsp;";
		}
		else
		{
			txtYear = "&nbsp;" + newYear + "&nbsp;";
		}
		document.getElementById("y" + i).innerHTML = txtYear;
	}
	intStartingYear += pbytShift;
	blnShow = true;
}

function selectYear(pintYear)
{
	yearSelected = parseInt(pintYear + intStartingYear);
	blnYearConstructed = false;
	constructCalendar();
	popDownYear();
}

function constructYear()
{
	popDownMonth();
	strHTML = "";
	if (!blnYearConstructed)
	{
		strHTML = "<tr><td align='center' onmouseover='this.className=\"dropdown-select-style\"' onmouseout='clearInterval(intervalID1);this.className=\"dropdown-normal-style\"' onmousedown='clearInterval(intervalID1);intervalID1=setInterval(\"shiftYear(-1)\",30)' onmouseup='clearInterval(intervalID1)'>-</td></tr>";
		j =	0;
		intStartingYear = yearSelected - 3;
		for(i = (yearSelected - 3); i <= (yearSelected + 3); i++)
		{
			strName = i;
			if (i == yearSelected)
			{
				strName = "<b>" + strName +	"</b>";
			}
			strHTML += "<tr><td id='y" + j + "' onmouseover='this.className=\"dropdown-select-style\"' onmouseout='this.className=\"dropdown-normal-style\"' onclick='selectYear(" + j + ");event.cancelBubble=true'>&nbsp;" + strName + "&nbsp;</td></tr>";
			j++;
		}

		strHTML += "<tr><td align='center' onmouseover='this.className=\"dropdown-select-style\"' onmouseout='clearInterval(intervalID2);this.className=\"dropdown-normal-style\"' onmousedown='clearInterval(intervalID2);intervalID2=setInterval(\"shiftYear(1)\",30)' onmouseup='clearInterval(intervalID2)'>+</td></tr>";
		document.getElementById("selectYear").innerHTML	= "<table width='44px' class='dropdown-style' onmouseover='clearTimeout(timeoutID2)' onmouseout='clearTimeout(timeoutID2);timeoutID2=setTimeout(\"popDownYear()\",100)' cellspacing='0px'>"	+ strHTML + "</table>";
		blnYearConstructed	= true;
	}
}

function popDownYear() 
{
	clearInterval(intervalID1);
	clearTimeout(timeoutID1);
	clearInterval(intervalID2);
	clearTimeout(timeoutID2);
	crossYearObj.visibility = "hidden";
}

function popUpYear() 
{
	var	leftOffset;

	constructYear();
	crossYearObj.visibility	= (blnDOM || blnIE) ? "visible" : "show";
	leftOffset = parseInt(crossobj.left) + document.getElementById("spanYear").offsetLeft;
	if (blnIE)
	{
		leftOffset += 6;
	}
	crossYearObj.left =	leftOffset + "px";
	crossYearObj.top = (parseInt(crossobj.top) + 26) + "px";
}

/*** calendar ***/

function WeekNbr(dtmNow)
{
	Year = takeYear(dtmNow);
	Month = dtmNow.getMonth();
	Day = dtmNow.getDate();
	now = Date.UTC(Year, Month, Day + 1, 0, 0, 0);
	var Firstday = new Date();
	Firstday.setYear(Year);
	Firstday.setMonth(0);
	Firstday.setDate(1);
	then = Date.UTC(Year, 0, 1, 0, 0, 0);
	var Compensation = Firstday.getDay();
	if(Compensation > 3)
	{
		Compensation -= 4;
	}
	else 
	{
		Compensation += 3;
	}
	NumberOfWeek = Math.round((((now-then)/86400000) + Compensation) / 7);
	return NumberOfWeek;
}

function takeYear(pdtmDate)
{
	intYear = pdtmDate.getYear();
	if(intYear < 2000)
	{
		intYear += 1900;
	}
	var y = intYear % 100;
	y += (y < 38) ? 2000 : 1900;
	return y;
}

function constructCalendar() 
{
	var dateMessage;
	var	startDate =	new	Date(yearSelected, monthSelected, 1);
	var	endDate	= new Date(yearSelected, monthSelected + 1, 1);
	endDate	= new Date(endDate - (24*60*60*1000));
	numDaysInMonth = endDate.getDate();

	datePointer	= 0;
	dayPointer = startDate.getDay() - bytFirstDay;
	
	if (dayPointer < 0)
	{
		dayPointer = 6;
	}

	strHTML = "<table border='0px' class='body-style'><tr>";

	if (blnShowWeekNumber)
	{
		strHTML += "<td width='27px'><b>" + strWeek + "</b></td><td width='1px' rowspan='7' class='weeknumber-div-style'><img src='" + strImageFolder + "divider.gif' width='1px'></td>";
	}

	for	(i = 0; i < 7; i++)	
	{
		strHTML += "<td width='27px' align='right'><b>" + astrDayName[i] + "</b></td>";
	}
	strHTML += "</tr><tr>";
	
	if (blnShowWeekNumber == 1)
	{
		strHTML += "<td align='right'>" + WeekNbr(startDate) + "&nbsp;</td>";
	}

	for	(i = 1; i <= dayPointer; i++)
	{
		strHTML += "<td>&nbsp;</td>";
	}

	for	(datePointer = 1; datePointer <= numDaysInMonth; datePointer++)
	{
		dayPointer++;
		strHTML += "<td align='right'>";

		var strStyle="normal-day-style"; //regular day

		if ((datePointer == bytCurrentDay) && (monthSelected == bytCurrentMonth) && (yearSelected == intCurrentYear)) //today
		{
			strStyle = "current-day-style";
		}
		var dtmDate = new Date(yearSelected, monthSelected, datePointer);
		if (dtmDate.getDay() == 0 || dtmDate.getDay() == 6)
		{
			strStyle = "end-of-weekday-style";
		}

		//selected day
		if((datePointer == odateSelected) && (monthSelected == omonthSelected) && (yearSelected == oyearSelected))
		{
			strStyle += " selected-day-style"; 
		}

		strHint = "";
		for(intHoliday = 0; intHoliday < intHolidays; intHoliday++)
		{
			if ((parseInt(aHolidays[intHoliday].bytDay) == datePointer) && (parseInt(aHolidays[intHoliday].bytMonth) == (monthSelected + 1)))
			{
				if ((parseInt(aHolidays[intHoliday].intYear) == 0) || ((parseInt(aHolidays[intHoliday].intYear) == yearSelected) && (parseInt(aHolidays[intHoliday].intYear) != 0)))
				{
					strStyle += " holiday-style";
					strHint += strHint == "" ? aHolidays[intHoliday].strDescription : "\n" + aHolidays[intHoliday].strDescription;
				}
			}
		}

		var regexp = /\"/g;
		strHint = strHint.replace(regexp, "&quot;");

		dateMessage = "onmousemove='window.status=\"" + strSelectDate.replace("[date]", constructDate(datePointer, monthSelected, yearSelected)) + "\"' onmouseout='window.status=\"\"' ";
		strHTML += "<a class='" + strStyle + "' " + dateMessage + " title=\"" + strHint + "\" href='javascript:dateSelected=" + datePointer + ";closeCalendar();'>&nbsp;" + datePointer + "&nbsp;</a>";
		strHTML += "";
		if ((dayPointer + bytFirstDay) % 7 == bytFirstDay) 
		{ 
			strHTML += "</tr><tr>";
			if ((blnShowWeekNumber == 1) && (datePointer < numDaysInMonth))
			{
				strHTML += "<td align='right'>" + (WeekNbr(new Date(yearSelected,monthSelected,datePointer+1))) + "&nbsp;</td>";
			}
		}
	}

	document.getElementById("content").innerHTML = strHTML;
	document.getElementById("spanMonth").innerHTML = "&nbsp;" +	astrMonthName[monthSelected] + "&nbsp;<img id='changeMonth' src = '" + strImageFolder + "drop1.gif' width='12px' height='10px' border='0px'>";
	document.getElementById("spanYear").innerHTML =	"&nbsp;" + yearSelected	+ "&nbsp;<img id='changeYear' src = '" + strImageFolder + "drop1.gif' width='12px' height='10px' border='0px'>";
}

function popUpCalendar(pobjCaller, pobjDestination, pstrDateFormat, pblnPostBack) 
{
	var	intLeftPos = 0;
	var	intTopPos = 0;
	
	if (blnPageLoaded)
	{
		if (crossobj.visibility == "hidden") 
		{
			blnPostBack = pblnPostBack;
			objDestination = pobjDestination;
			strDateFormat = pstrDateFormat;

			formatChar = "";
			aFormat	= strDateFormat.split(formatChar);
			if(aFormat.length < 3)
			{
				formatChar = "/";
				aFormat	= strDateFormat.split(formatChar);
				if(aFormat.length < 3)
				{
					formatChar = ".";
					aFormat	= strDateFormat.split(formatChar);
					if(aFormat.length < 3)
					{
						formatChar = "-";
						aFormat	= strDateFormat.split(formatChar);
						if(aFormat.length < 3)
						{
							// invalid date	format
							formatChar = "";
						}
					}
				}
			}

			tokensChanged =	0;
			if(formatChar != "")
			{
				// use user's date
				aData =	pobjDestination.value.split(formatChar);

				for	(i = 0; i < 3; i++)
				{
					if((aFormat[i] == "d") || (aFormat[i] == "dd") ||(aFormat[i] == "DD") )
					{
						dateSelected = parseInt(aData[i], 10);
						tokensChanged++;
					}
					else if((aFormat[i] == "M") || (aFormat[i] == "MM"))
					{
						monthSelected =	parseInt(aData[i], 10) - 1;
						tokensChanged++;
					}
					else if((aFormat[i] == "yyyy") || (aFormat[i] == "YYYY"))
					{
						yearSelected = parseInt(aData[i], 10);
						tokensChanged++;
					}
					else if(aFormat[i] == "MMM")
					{
						for(j = 0; j < 12; j++)
						{
							if (aData[i] == astrMonthName[j].substring(0,3))
							{
								monthSelected = j;
								tokensChanged++;
							}
						}
					}
					else if(aFormat[i] == "MMMM")
					{
						for(j = 0; j < 12; j++)
						{
							if (aData[i] == astrMonthName[j])
							{
								monthSelected = j;
								tokensChanged++;
							}
						}
					}
				}
			}

			if ((tokensChanged != 3) || isNaN(dateSelected) || isNaN(monthSelected) || isNaN(yearSelected))
			{
				dateSelected = bytCurrentDay;
				monthSelected =	bytCurrentMonth;
				yearSelected = intCurrentYear;
			}

			odateSelected = dateSelected;
			omonthSelected = monthSelected;
			oyearSelected = yearSelected;

			aTag = pobjCaller;
			do
			{
				aTag = aTag.offsetParent;
				intLeftPos	+= aTag.offsetLeft;
				intTopPos += aTag.offsetTop;
			}
			while(aTag.tagName != "BODY");

			crossobj.left =	(intFixedX == -1 ? pobjCaller.offsetLeft + intLeftPos : intFixedX) + "px";
			crossobj.top = (intFixedY == -1 ? pobjCaller.offsetTop + intTopPos : intFixedY) + "px";
			constructCalendar (1, monthSelected, yearSelected);
			crossobj.visibility = (blnDOM || blnIE) ? "visible" : "show"
			
			hideElement( 'SELECT', document.getElementById("calendar") );
			hideElement( 'APPLET', document.getElementById("calendar") );			

			blnShow = true;
		}
	}
	else
	{
		init();
		popUpCalendar(pobjCaller, pobjDestination, pstrDateFormat, pblnPostBack);
	}
}

document.onkeyup = function keyPress(evt)
{
	if(blnIE)
	{
		var whichKey = window.event.keyCode;
	}
	else
	{
		var whichKey = evt.which;
	}
	if(whichKey == 27)
	{
		hideCalendar();
	}
}

document.onclick = function keyClick() 
{ 		
	if (!blnShow)
	{
		hideCalendar();
	}
	blnShow = false
}