//Written: 11/03/2009 01:20 PM
var undefined;

// ********************************************
// * Calendar Functions ***********************
var objDateField = null;
var objDateDisplayField = null;
var w = null;
function getGECalendarDate(evt, iconClicked, monthsToShow) {
    var iLeft = 0;
    var iTop = 0;
    if (window.event)
    {
	    iLeft = window.event.screenX;
	    iTop = window.event.screenY;
	}
	else
	{
	    iLeft = evt.screenX;
	    iTop = evt.screenY;
	}
	
	// recalculate top/left so calendar shows up in its entirety, with a little room to spare - in case button is close to edge of screen
	if ((iLeft + 150) > window.screen.availWidth){
		iLeft = window.screen.availWidth - 160;
	}
	
	if ((iTop + 155) > window.screen.availHeight){
		iTop = window.screen.availHeight - 190;
	}


	//var htmlDateRow = iconClicked.parentElement.parentElement;
	var htmlDateRow = iconClicked.parentNode.parentNode;
	
	var cells = htmlDateRow.childNodes.length;
	for (var c = 0; c < cells; c++) 
	{
		// IE is 0 based and Firefox is 1 based
		var htmlInputCell = htmlDateRow.childNodes[c];
		if (htmlInputCell != undefined) {
			var elementCount = htmlInputCell.childNodes.length;
			var objField = null;

			for (var n=0; n < elementCount; n++) {
				objField = htmlInputCell.childNodes[n];
				if (objField.tagName == "INPUT") {
					n = elementCount;
					c = cells;
				}
			}
		}
	}

	objDateField = objField;

	var im = '';
	var iy = '';

    var valDate = null;
    try
    {
	    valDate = new Date(Date.parse(objDateField.value));
	}
	catch (e) {}
	
	if (valDate == null)
	{
	    valDate = undefined;
	}
	
	if (!isNaN(valDate)) {
		im = new String(valDate.getMonth()+1);
		
		//var nYears = valDate.getYear();
		var nYears = valDate.getFullYear();
		
		// any 19xx year is only return 2 digit, everything else is 4 digit
		if (nYears < 100) { nYears += 1900; }

		var iy = new String(nYears);
	}
	
	var strQueryString = 'M=' + im + '&Y=' + iy + '&SEL=' + objDateField.value + "&SHOW=" + monthsToShow;
	

	var su = objDateField.getAttribute("su");
	var mo = objDateField.getAttribute("mo");
	var tu = objDateField.getAttribute("tu");
	var we = objDateField.getAttribute("we");
	var th = objDateField.getAttribute("th");
	var fr = objDateField.getAttribute("fr");
	var sa = objDateField.getAttribute("sa");
	
	if (su != null) {
		strQueryString += "&SU=" + su;
	}
	if (mo != null) {
		strQueryString += "&MO=" + mo;
	}
	if (tu != null) {
		strQueryString += "&TU=" + tu;
	}
	if (we != null) {
		strQueryString += "&WE=" + we;
	}
	if (th != null) {
		strQueryString += "&TH=" + th;
	}
	if (fr != null) {
		strQueryString += "&FR=" + fr;
	}
	if (sa != null) {
		strQueryString += "&SA=" + sa;
	}
	
	var height = 155;
	var width = 186;
	switch (monthsToShow) {
		case 2:
			width = 382;
			break;
		case 3:
			width = 575;
			break;
		case 4:
			width = 382;
			height = 310;
			break;
		case 5:
			width = 575;
			height = 310;
			break;
		case 6:
			width = 575;
			height = 310;
			break;
		default:
			break;
	}
	
	// inc_strAspCalPath is registered in the page in another script block to shrink the number of times we pass around the path
	var strPath = inc_strAspCalPath + '?resource=inlinecalendar.html&' + strQueryString;
	//w = window.open(strPath,'selectdate','top=' + iTop + ',left=' + iLeft + ',height=155,width=150,status=no,toolbar=no,menubar=no,scrollbars=no,location=no,titlebar=no');
	w = window.open(strPath,'selectdate','top=' + iTop + ',left=' + iLeft + ',height=' + height + ',width=' + width + ',status=no,toolbar=no,menubar=no,scrollbars=no,location=no,titlebar=no');
}

function return_date(str) {
	objDateField.value = str;
	// note that only IE will fire the event
	try
	{
	// fireevent is IE specific, onchange method works in both IE and FireFox
	objDateField.onchange();
	//objDateField.fireEvent("onchange");
	}
	catch(e) {}
}
var blnShowMonthPick = false;

function checkMonth() {
	if (!blnShowMonthPick) {
		MONTHPICK.style.visibility = 'hidden';
	}
}

function document_onmouseout(e) {
	var obj = getsourceElement(e); //window.event.srcElement;
	var strClass = obj.className;
	if (strClass == 'd' || strClass == 'today') {
		obj.style.background = '#FFFFFF';
	} else {
		if (strClass == 'mpick') {
			obj.style.background = 'white';
			obj.style.color = 'black';
			blnShowMonthPick = false;
			setTimeout("checkMonth()", 250);
		}
		if (strClass == 'mpickcurrent') {
			//obj.style.background = 'white';
			//obj.style.color = 'black';
			blnShowMonthPick = false;
			setTimeout("checkMonth()", 250);
		}
	}
	
}

function document_onmouseover(e) {
	var obj = getsourceElement(e); //window.event.srcElement;
	var strClass = obj.className;
	if (strClass == 'd' || strClass == 'today') {
		obj.style.background = '#c0c0c0';
	} else {
		if (strClass == 'mpick') {
			obj.style.background = 'navy';
			obj.style.color = 'white';
			blnShowMonthPick = true;
		}
		if (strClass == 'mpickcurrent') {
			blnShowMonthPick = true;
		}
	}
}

function window_onload() {
	window.focus();
}

function document_onclick(e) {
	var srcElement = getsourceElement(e);
	if (srcElement.className == 'd' || srcElement.className == 'today' || srcElement.className == 'daysel') {
		var iDay = srcElement.innerHTML;

		var monthSelected = _calendarMonth; //srcElement.m;
		var yearSelected = _calendarYear; //srcElement.y;
		
		if (iDay.length == 1) { iDay = "0" + iDay; } 
		var strDate = monthSelected + '/' + iDay + '/' + yearSelected;

		window.opener.return_date(strDate);		
		
		window.close();
	}
}

function getsourceElement(e)
{
	// window.event exists due to script, so check for srcElement
	if (window.event && window.event.srcElement)
	{
		return window.event.srcElement;
	}
	else
	{
		return e.target;
	}
}
