/* PopUp Calendar v2.1
© PCI, Inc.,2000 • Freeware
webmaster@personal-connections.com
+1 (925) 955 1624
Permission granted  for unlimited use so far
as the copyright notice above remains intact. */

/* Settings. Please read readme.html file for instructions*/
var ppcMinAdvPurchase = 0;
var ppcPopupWidth = "150";
var ppc_return_allow_same_day = false;
var ppcOffsetTop=0;
var ppcOffsetLeft=0;

var ppcDF = "m/d/Y";
var ppcMN = new Array("January", "February", "March", "April", "May", "June",
											"July", "August", "September", "October", "November",
											"December");
var ppcWN = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
											"Friday", "Saturday");

var ppcDOW = "";
var ppcaDOW = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"); //LANGUAGE-SPECIFIC

var ppcER = new Array(4);
ppcER[0] = "Required DHTML functions are not supported in this browser.";
ppcER[1] = "Target form field is not assigned or not accessible.";
ppcER[2] =
	"Sorry, the chosen date is not acceptable. Please read instructions on the page.";
ppcER[3] = "Unknown error occured while executing this script.";
var ppcUC = false;
var ppcUX = 4;
var ppcUY = 4;

var ppcToday = new Date();
var ppcMaxDate = new Date();
if ((ppcMaxDate.getMonth() == 1) && (ppcMaxDate.getDate() == 29)) {
	ppcMaxDate.setDate(28); 
}
ppcMaxDate.setFullYear(ppcMaxDate.getFullYear()+1);
ppcMaxDate.setTime(ppcMaxDate.getTime() - 24*60*60*1000);

var ppcMinDate = new Date(ppcToday.getFullYear(), ppcToday.getMonth(), ppcToday.getDate());
ppcMinDate.setTime(ppcMinDate.getTime() + 3 * 24*60*60*1000);

/* Do not edit below this line unless you are sure what are you doing! */

var ppcIE = (navigator.appName == "Microsoft Internet Explorer" || true);
var ppcNN = ((navigator.appName == "Netscape") && (document.layers) && false);

//if (document.layers)
//	alert("document.layers");

//if (navigator.appName)
//	alert("ppcIE:" + ppcIE + " ppcNN:" + ppcNN);

var ppcTT =
	"<table width=\"" + ppcPopupWidth + "\" cellspacing=\"0\" cellpadding=\"1\" border=\"1\" bordercolor=\"#00309C\">\n";
var ppcCD = ppcTT;
var ppcFT =
	"<font face=\"MS Sans Serif, sans-serif\" size=\"1\" color=\"#000000\">";
var ppcFC = true;
var ppcTI = false;
var ppcSV = null;
var ppcRL = null;
var ppcXC = null;
var ppcYC = null;
var ppcML = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var ppcWE = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
											"Friday", "Saturday");
var ppcNow = new Date();
var ppcPtr = new Date();

if (ppcNN)
{
	window.captureEvents(Event.RESIZE);
	window.onresize = restoreLayers;
	document.captureEvents(Event.MOUSEDOWN | Event.MOUSEUP);
	document.onmousedown = recordXY;
	document.onmouseup = confirmXY;
}

function ppcSetppcTT(thisWidth)
{
	ppcPopupWidth = thisWidth;
	ppcTT = "<table width=\"" + ppcPopupWidth + "\" cellspacing=\"0\" cellpadding=\"1\" border=\"1\" bordercolor=\"#00309C\">\n";
}

function restoreLayers(e){
	if (ppcNN){
		with(window.document){
			open("text/html");
			write("<html><head><title>Restoring the layer structure...</title></head>");
			write("<body bgcolor=\"#FFFFFF\" onLoad=\"history.go(-1)\">");
			write("</body></html>");
			close();
		}
	}
}


function recordXY(e){
	if (ppcNN){
		ppcXC = e.x;
		ppcYC = e.y;
		document.routeEvent(e);
	}
}

function confirmXY(e){
	if (ppcNN){
		ppcXC = (ppcXC == e.x) ? e.x : null;
		ppcYC = (ppcYC == e.y) ? e.y : null;
		document.routeEvent(e);
	}
}

function setCalendarFor(target, daydrop, monthdrop, rules, fields, minDayDrop, minMonthDrop, thisDate, dowHTML)
{
	//alert("DEBUG:setCalendarFor 1");
	hideOverrideFields(fields);
	pdaydrop = daydrop;
	pmonthdrop = monthdrop;
	ppcSV = target;
	ppcRL = rules;

	//alert("DEBUG:setCalendarFor 2");
	
	if (dowHTML==null)
		{ppcDOW = "";}
	else
		{ppcDOW = dowHTML;}

	if (ppcFC)
	{
		//alert("DEBUG ppcFC:" + ppcNow);
		setCalendar();
		ppcFC = false;
	}

	//alert("DEBUG:setCalendarFor 3");
	if (minMonthDrop== null)
	{
		ppcMinDate = new Date(ppcToday.getFullYear(), ppcToday.getMonth(), ppcToday.getDate());
		ppcMinDate.setTime(ppcMinDate.getTime() + ppcMinAdvPurchase * 24*60*60*1000);		//Was 3
	}
	else
	{
		ppcMinDate.setDate(minDayDrop.options[minDayDrop.selectedIndex].value);
		ppcMinDate.setMonth(minMonthDrop.options[minMonthDrop.selectedIndex].value.split('-')[0]-1);
		ppcMinDate.setYear(minMonthDrop.options[minMonthDrop.selectedIndex].value.split('-')[1]);
		if (ppc_return_allow_same_day)
			ppcMinDate.setTime(ppcMinDate.getTime() + 0 * 24*60*60*1000);						//Was 1
		else
			ppcMinDate.setTime(ppcMinDate.getTime() + 1 * 24*60*60*1000);						//Was 1
	}
	
	var lMonth = thisDate.getMonth();
	selectDate(thisDate.getFullYear()+"|"+lMonth+"|"+thisDate.getDate());
}

function getCalendarFor(target, daydrop, monthdrop, rules, fields, minDayDrop, minMonthDrop, thisDate, dowHTML){
	hideOverrideFields(fields);
	pdaydrop = daydrop;
	pmonthdrop = monthdrop;
	ppcSV = target;
	ppcRL = rules;

	if (dowHTML==null)
		{ppcDOW = "";}
	else
		{ppcDOW = dowHTML;}

	//alert(pmonthdrop.options[pmonthdrop.selectedIndex].value + '|' + pdaydrop.options[pdaydrop.selectedIndex].value)

	if (minMonthDrop== null){
		ppcMinDate = new Date(ppcToday.getFullYear(), ppcToday.getMonth(), ppcToday.getDate());
		ppcMinDate.setTime(ppcMinDate.getTime() + ppcMinAdvPurchase  * 24*60*60*1000);		//Was 3
	}
	else
	{
		ppcMinDate.setDate(minDayDrop.options[minDayDrop.selectedIndex].value);
		ppcMinDate.setMonth(minMonthDrop.options[minMonthDrop.selectedIndex].value.split('-')[0]-1);
		ppcMinDate.setYear(minMonthDrop.options[minMonthDrop.selectedIndex].value.split('-')[1]);
		if (ppc_return_allow_same_day)
			ppcMinDate.setTime(ppcMinDate.getTime() + 0 * 24*60*60*1000);						//Was 1
		else
			ppcMinDate.setTime(ppcMinDate.getTime() + 1 * 24*60*60*1000);						//Was 1
	}


	if (ppcFC){
		//alert(ppcNow);
		setCalendar();
		ppcFC = false;
	}

		var curSel = pmonthdrop.options[pmonthdrop.selectedIndex].value.split('-')[1] + '|' 
		+ (parseFloat(pmonthdrop.options[pmonthdrop.selectedIndex].value.split('-')[0])-1) + '|' 
		+ parseFloat(pdaydrop.options[pdaydrop.selectedIndex].value);

		//alert(curSel);

	switchMonth(curSel);

	if ((ppcSV != null) && (ppcSV)){
		if (ppcIE){
			var obj = document.getElementById("PopUpCalendar");
			//   if (typeof(document.documentElement.scrollTop) != 'undefined' && document.documentElement.scrollTop != 0) {
			//    zlvbzz_wtop = document.documentElement.scrollTop;
			//  } else if (typeof(document.body.scrollTop) != 'undefined' && document.body.scrollTop != 0) {
			//    zlvbzz_wtop = document.body.scrollTop;
			//  } else if (typeof(window.pageYOffset) != 'undefined' && window.pageYOffset != 0) {
			//    zlvbzz_wtop = window.pageYOffset;
			//  } else {
			zlvbzz_wtop = 0;
			//  }

			var lCoordsFound = false;
			if (window.event)
			{
				if (com.travelpower.AnchorPosition && event.srcElement)
				{
					if (event.srcElement.id)
					{
						var coordinates = com.travelpower.AnchorPosition.getAnchorPosition(event.srcElement.id);
						obj.style.left = coordinates.x;
						obj.style.top = coordinates.y;
						lCoordsFound = true;
					}
				}
			}

			if (lCoordsFound == false)
			{
				if (com.travelpower.AnchorPosition)
				{
					if (target.id)
					{
						var coordinates = com.travelpower.AnchorPosition.getAnchorPosition(target.id);
						obj.style.left = coordinates.x;
						obj.style.top = coordinates.y;
						lCoordsFound = true;
					}
				}
			}

//			if (lCoordsFound == false)
//			{
//				alert("DEBUG:" + target.clientX + " " + target.clientY);
//				obj.style.left = target.clientX - ppcOffsetLeft;
//				obj.style.top = target.clientY - ppcOffsetTop;
//			}

			if (lCoordsFound == false)
			{
				try
				{
					obj.style.left = event.clientX - ppcOffsetLeft;
					obj.style.top = event.clientY - ppcOffsetTop;
					//alert("event.clientX:"+event.clientX+"\nppcOffsetLeft:"+ppcOffsetLeft+"\nobj.style.left:"+obj.style.left);
				}
				catch (er) 
				{
					
					//obj.style.left = document.body.scrollLeft + event.clientX;
					//obj.style.top = zlvbzz_wtop + event.clientY;
					obj.style.left = document.body.scrollLeft + target.clientX;
					obj.style.top = zlvbzz_wtop + target.clientY;
				}
			}
			obj.style.visibility = "visible";
		}
		else if (ppcNN){
			var obj = document.layers['PopUpCalendar'];
			obj.left = ppcXC;
			obj.top = ppcYC;
			obj.visibility = "show";
		}
		else{
			showError(ppcER[0]);
		}
	}
	else{
		showError(ppcER[1]);
	}
}

function switchMonth(param){
	var tmp = param.split("|");
	setCalendar(tmp[0], tmp[1]);
}


function moveMonth(dir){
	var obj = null;
	var limit = false;
	var tmp, dptrYear, dptrMonth;
	var todaysDate = new Date();
	var maxDate = todaysDate.setDate(todaysDate.getDate() + 365);

	if (ppcIE){
		obj = document.getElementById("ppcMonthList").sItem;
	}
	else if (ppcNN){
		obj = document.layers['PopUpCalendar'].document.layers['monthSelector'].document.ppcMonthList.sItem;
	}
	else{
		showError(ppcER[0]);
	}
	if (obj != null){
		if ((dir.toLowerCase() == "back") && (obj.selectedIndex > 0)){
			obj.selectedIndex--;
		}
		else if ((dir.toLowerCase() == "forward") && (obj.selectedIndex < 14)){
			var tempMonth = obj.selectedIndex + 1;
			if (tempMonth <= 12){
				obj.selectedIndex++;
			}
		}
		else{
			limit = true;
		}
	}
	if (!limit){
		tmp = obj.options[obj.selectedIndex].value.split("|");
		dptrYear = tmp[0];
		dptrMonth = tmp[1];
		setCalendar(dptrYear, dptrMonth);
	}
	else{
		if (ppcIE){
			obj.style.backgroundColor = "#FF0000";
			window.setTimeout("document.getElementById('ppcMonthList').sItem.style.backgroundColor = '#FFFFFF'", 50);
		}
	}
}

function mPUPDATEToDayOfWeek(inDate)
{
	return(ppcaDOW[inDate.getDay()]);
}

function selectDate(param)
{
	var arr = param.split("|");
	var year = arr[0];
	var month = arr[1];
	var date = arr[2];
	var ptr = parseInt(date);
	ppcPtr.setDate(ptr);

	//alert("DEBUG selectDate");
	
	if ((ppcSV != null) && (ppcSV)){
		if (validDate(date))
		{
			gTPFORMSDaysDropDown(pdaydrop,parseInt(month)+1,year);
			
			pdaydrop.selectedIndex = (date - 1);
			//alert(param + " - " + month + " - " + ppcNow.getMonth() + " - " + year + " - " + getFullYear(ppcNow));
															
			pmonthdrop.selectedIndex = (month - ppcNow.getMonth() + 12 * (year -
															getFullYear(ppcNow)));

			ppcSV.value = dateFormat(year, month, date);
			if (ppcDOW!="")
			{
				try {mfPopupCalendarUsed(ppcDOW);}
				catch (er) {}
			}
			hideCalendar();
			//document.getElementById('BlankFill').style.display='none'
			//document.getElementById("infants").style.display='inline';
		}
		else{
			showError(ppcER[2]);
			if (ppcTI){
				clearTimeout(ppcTI);
				ppcTI = false;
			}
		}
	}
	else{
		showError(ppcER[1]);
		hideCalendar();
		//document.getElementById('BlankFill').style.display='none'
		//document.getElementById("infants").style.display='inline';
	}
}

function setCalendar(year, month)
{
	//alert("DEBUG:setCalendar 1:"+year+" - "+month);

	if (year == null)
	{
		year = getFullYear(ppcNow);
	}
	if (month == null){
		month = ppcNow.getMonth();
	}

	//alert("DEBUG:setCalendar 2:"+year+" - "+month);

	setSelectList(year, month);

	//alert("DEBUG:setCalendar 3:"+year+" - "+month);

	if (month == 1)
	{
		ppcML[1] = (isLeap(year)) ? 29 : 28;
	}
	
	//alert("DEBUG:" + year + " - " + month);
	
	ppcPtr.setYear(year);
	ppcPtr.setMonth(month);
	ppcPtr.setDate(1);
//  window.status=ppcPtr.toString();

	//alert("DEBUG:setCalendar 4:"+year+" - "+month);

	updateContent();
}

function updateContent()
{

	//alert("DEBUG:updateContent 1:");

	generateContent();

	//alert("DEBUG:updateContent 2:");

	if (ppcIE)
	{

		//alert("DEBUG:updateContent 3:");
		//alert("DEBUG:updateContent 3:"+ppcCD);

		if (document.getElementById("monthDays"))
			document.getElementById("monthDays").innerHTML = ppcCD;

		//alert("DEBUG:updateContent 4:");
	}
	else if (ppcNN)
	{
		with(document.layers['PopUpCalendar'].document.layers['monthDays'].document){
			open("text/html");
			write(
				"<html>\n<head>\n<title>DynDoc</title>\n</head>\n<body bgcolor=\"#FFFFFF\">\n");
			write(ppcCD);
			write("</body>\n</html>");
			close();
		}
	}
	else
	{
		showError(ppcER[0]);
	}
	ppcCD = ppcTT;
}

function generateContent() {
	//alert("DEBUG ppcPtr:" + ppcPtr);
	
	var year = getFullYear(ppcPtr);
	var month = ppcPtr.getMonth();
	var date = 1;
	var day = ppcPtr.getDay();
	var len = ppcML[month];
	var bgr, cnt, tmp = "";
	var j, i = 0;
	for (j = 0; j < 7; ++j){
		if (date > len){
			break;
		}
		for (i = 0; i < 7; ++i){
			bgr = ((i == 0) || (i == 6)) ? "#639ACE" : "#FFFFFF";
			if (((j == 0) && (i < day)) || (date > len)){
				tmp += makeCell(bgr, year, month, 0);
			}
			else{
				tmp += makeCell(bgr, year, month, date);
				++date;
			}
		}
		ppcCD += "<tr align=\"center\">\n" + tmp + "</tr>\n";
		tmp = "";
	}
	ppcCD += "</table>\n";
}

function makeCell(bgr, year, month, date){
	var param = "\'" + year + "|" + month + "|" + date + "\'";
	var cellDate = new Date(year, month, date);
	var td1 = "<td width=\"20\" bgcolor=\"" + bgr + "\" ";  
	var td2 = (ppcIE) ? "</font></span></td>\n" : "</font></a></td>\n";
	var evt = "onmouseover=\"this.style.backgroundColor=\'#FF0000\';window.status=" + param + "\" onmouseout=\"this.style.backgroundColor=\'" + bgr + "\'\" onclick=\"selectDate(" + param + ")\" ";
	var ext = "<span Style=\"cursor: pointer\">";
	var lck = "<span Style=\"cursor: default\">";
	var lnk = "<a href=\"javascript:selectDate(" + param + ")\" onmouseover=\"window.status=" + param + "\' \';return true;\">";
	var cellValue = (date != 0) ? date + "": "&nbsp;";
	if ((ppcNow.getDate() == date) && (ppcNow.getMonth() == month) && (getFullYear(ppcNow) == year)){
		cellValue = "<b>" + cellValue + "</b>";
	}

	if ((cellDate.getTime() < ppcMinDate.getTime()) || cellDate.getTime() > ppcMaxDate.getTime()) 
	{
//		alert("cellDate:" + cellDate + " ppcMinDate:" + ppcMinDate + " ppcMaxDate:" + ppcMaxDate);
		evt = " ";
		cellValue = "<font color=\"gray\">" + cellValue + "</font>";
	}
	
	var cellCode = "";
	if (date == 0){
		if (ppcIE){
			cellCode = td1 + "Style=\"cursor: default\">" + lck + ppcFT + cellValue + td2;
		} else{
			cellCode = td1 + ">" + ppcFT + cellValue + td2;
		}
	}
	else{
		if (ppcIE){
			cellCode = td1 + evt + "Style=\"cursor: pointer\">" + ext + ppcFT + cellValue + td2;
		}    else{
			if (date < 10){
				cellValue = "&nbsp;" + cellValue + "&nbsp;";
			}
			cellCode = td1 + ">" + lnk + ppcFT + cellValue + td2;
		}
	}
	//alert("cellCode:" + cellCode);
	
	return cellCode;
}

function setSelectList(selyear, selmonth){
	var i = 0;
	var obj = null;
	if (ppcIE){
		obj = document.getElementById("ppcMonthList").sItem;
	}
	else if (ppcNN){
		obj = document.layers['PopUpCalendar'].document.layers['monthSelector'].document.ppcMonthList.sItem;
	}
	else{
		 /* NOP */
	}
	
	var year = ppcMinDate.getFullYear();
	var month = ppcMinDate.getMonth();
	
	while ((year < ppcMaxDate.getFullYear()) || ((year == ppcMaxDate.getFullYear()) && (month <= ppcMaxDate.getMonth()))) {
		if (i >= obj.options.length ) obj.options[i] = new Option("","");
		obj.options[i].value = year + "|" + month;
		obj.options[i].text = year + " " + ppcMN[month];
		if ((year == selyear) && (month == selmonth)) obj.options[i].selected = true;
		else obj.options[i].selected = false;
		i++;
		month++;
		if (month == 12){
			year++;
			month = 0;
		}
	}
	while (i < obj.options.length){
		obj.options[i] = null;
	}

}

function hideCalendar(){
	//document.getElementById('BlankFill').style.display='none';
	//document.getElementById("infants").style.display='inline';
	if (ppcIE){
		document.getElementById("PopUpCalendar").style.visibility = "hidden";
	}
	else if (ppcNN){
		document.layers['PopUpCalendar'].visibility = "hide";
		window.status = " ";
	}
	else{
		 /* NOP */
	}
	ppcTI = false;
	setCalendar();
	ppcSV = null;
	if (ppcIE){
		var obj = document.getElementById("ppcMonthList").sItem;
	}
	else if (ppcNN){
		var obj = document.layers['PopUpCalendar'].document.layers['monthSelector'].document.ppcMonthList.sItem;
	}
	else{
		 /* NOP */
	}
	obj.selectedIndex = 0;
	showOverrideFields();
}

function showError(message){
	window.alert("[ PopUp Calendar ]\n\n" + message);
}

function isLeap(year){
	if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))){
		return true;
	}
	else{
		return false;
	}
}

function getFullYear(obj){
	if (ppcNN) {
		//alert("DEBUG getFullYear ppcNN");
		return obj.getYear() + 1900;
	}
	else
	{
		return obj.getFullYear();
	}
}

function validDate(date){
	var reply = true;
	if (ppcRL == null){
		 /* NOP */
	}
	else{
		var arr = ppcRL.split(":");
		var mode = arr[0];
		var arg = arr[1];
		var key = arr[2].charAt(0).toLowerCase();
		if (key != "d"){
			var day = ppcPtr.getDay();
			var orn = isEvenOrOdd(date);
			reply = (mode == "[^]") ? !((day == arg) && ((orn == key) || (key == "a"))):
						((day == arg) && ((orn == key) || (key == "a")));
		}
		else{
			reply = (mode == "[^]") ? (date != arg): (date == arg);
		}
	}
	return reply;
}

function isEvenOrOdd(date){
	if (date - 21 > 0){
		return "e";
	}
	else if (date - 14 > 0){
		return "o";
	}
	else if (date - 7 > 0){
		return "e";
	}
	else{
		return "o";
	}
}

function dateFormat(year, month, date){
	if (ppcDF == null){
		ppcDF = "m/d/Y";
	}
	var day = ppcPtr.getDay();
	var crt = "";
	var str = "";
	var chars = ppcDF.length;
	for (var i = 0; i < chars; ++i){
		crt = ppcDF.charAt(i);
		switch (crt){
			case "M":
				str += ppcMN[month];
				break;
			case "m":
				str += (month < 9) ? ("0" + (++month)): ++month;
				break;
			case "Y":
				str += year;
				break;
			case "y":
				str += year.substring(2);
				break;
			case "d":
				str += ((ppcDF.indexOf("m") !=  - 1) && (date < 10)) ? ("0" + date): date;
				break;
			case "W":
				str += ppcWN[day];
				break;
			default:
				str += crt;
		}
	}
	return unescape(str);
}

// TN 03/12/2003 Hide / Show fields functionality
function hideOverrideFields(asGroups)
{
	//alert("hideOverrideFields:"+asGroups);
	
	var i;
	var fieldName = "";
	if (typeof(asGroups) == "undefined"){
		asGroups = "";
	}
	
	if (asGroups=="")
		return(true);
		
	tempArr = asGroups.split(",");
	for (var feForm=0; feForm < document.forms.length; feForm++)
	{
		for (var feElement=0; feElement < document.forms[feForm].elements.length; feElement++)
		{
			var each = document.forms[feForm].elements[feElement];
			try
			{
				if (each.getAttribute("hideField"+asGroups))
				{
					//alert(each.name+": hidefield"+asGroups+"="+each.getAttribute("hideField"+asGroups)+" Display="+each.style.display)
					if (each.getAttribute("hideField"+asGroups) == "0")
					{
						each.setAttribute("TPIsHidden","2");
						each.style.display = "inline";
					}
					if (each.getAttribute("hideField"+asGroups) == "1" && (each.style.display == "inline" || each.style.display == ""))
					{
						each.setAttribute("TPIsHidden","1");
						each.style.display = "none";
					}
				}
			}
			catch(er) {}
		}
	}
	//alert("hideOverrideFields:"+asGroups+" end");
}

function showOverrideFields()
{
	for (var feForm=0; feForm < document.forms.length; feForm++)
	{
		for (var feElement=0; feElement < document.forms[feForm].elements.length; feElement++)
		{
			var each = document.forms[feForm].elements[feElement];
			try
			{
				if (each.getAttribute("TPIsHidden") == "1")
				{
					each.style.display = "inline";
					each.setAttribute("TPIsHidden","0");
				}
				if (each.getAttribute("TPIsHidden") == "2")
				{
					each.style.display = "none";
					each.setAttribute("TPIsHidden","0");
				}
			}
			catch(er) {}
		}
	}
}
