//document.onclick = closeCStart;
document.onclick = closeBothCals;


function GetWeekday(strYear,strMonth,strDay) {
	var datDate, aryDayNames;
	aryDayNames = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday','Thursday', 'Friday', 'Saturday');
	datDate = new Date();
	datDate.setDate(strDay);
	datDate.setMonth(strMonth-1);// Month starts to 0
	datDate.setFullYear(strYear);
	return(aryDayNames[datDate.getDay()] += ',');
}
function updDepartDay() {
	var datDate,strDay,strMonth,strYear;
	strDay=document.forms['frmSearch'].selDepartDay.options[document.forms['frmSearch'].selDepartDay.selectedIndex].text;
	strMonth=document.forms['frmSearch'].selDepartMonth.value;
	strYear=document.forms['frmSearch'].selDepartYear.options[document.forms['frmSearch'].selDepartYear.selectedIndex].text;

	document.forms['frmSearch'].txtDepartDay.value=GetWeekday(strYear,strMonth,strDay);
	}
function updArriveDay() {
	var datDate,strDay,strMonth,strYear;
	strDay=document.forms['frmSearch'].selDayStart.options[document.forms['frmSearch'].selDayStart.selectedIndex].text;
	strMonth=document.forms['frmSearch'].selMonthStart.value;
	strYear=document.forms['frmSearch'].selYearStart.options[document.forms['frmSearch'].selYearStart.selectedIndex].text;

	document.forms['frmSearch'].txtArriveDay.value=GetWeekday(strYear,strMonth,strDay);
}

<!-- Begin Calendar Script -->
function getMonthNumber(input){
// ****************************************************************
// *	Description: converts month string from form into number for use later				
// ****************************************************************
if (input == "Jan")
	{return "1"}
if (input == "Feb")
	{return "2"}
if (input  == "Mar")
	{return "3"}
if (input  == "Apr")
	{return "4"}
if (input  == "May")
	{return "5"}
if (input  == "Jun")
	{return "6"}
if (input == "Jul")
	{return "7"}
if (input  == "Aug")
	{return "8"}
if (input  == "Sep")
	{return "9"}
if (input  == "Oct")
	{return "10"}
if (input == "Nov")
	{return "11"}
if (input  == "Dec")
	{return "12"}
}


function isBrowserSupp() {
// ****************************************************************
// *	Description: Checks if browser is Netscape 2.0 since the options 
// *	array properties don't work with Netscape 2.0x
// ****************************************************************

    // Get the version of the browser
    version =  parseFloat( navigator.appVersion );

    if ( ( version >= 2.0 ) && ( version < 2.1 ) && ( navigator.appName.indexOf( "Netscape" ) != -1 ) ) {
        return false;
    }
    else {
        return true;
    }                  
}


function isLeapYear(yrStr)
{
	// ****************************************************************
	// *	Description:	Checks if Year selected is a leap year
	// ****************************************************************
	var leapYear=false;
	// every fourth year is a leap year
	if ((parseInt(yrStr, 10)%4) == 0)
	{
		leapYear=true;
	}
	return leapYear;
}

function getDaysInMonth(mthIdx, YrStr)
// ****************************************************************
// *	Description:	Retrieves the number of days in a given month
// ****************************************************************
{
	//Default number of days in a month is 31
	var maxDays=31
	// expect Feb. 
	if (mthIdx==2) 
	{
		if (isLeapYear(YrStr))
		{
			maxDays=29;
		}
		else 
		{
			maxDays=28;
		}
	}
	// All the rest of the months have 30 days
	if (mthIdx==4 || mthIdx==6 || mthIdx==9 || mthIdx==11)
	{
		maxDays=30;
	}
	return maxDays;
}


function adjustDate(mthIdx, Dt, Yr) 
// ****************************************************************
// *	Description:	Adjusts the format of the Date
// ****************************************************************
{
var value=0; 		
var numDays=getDaysInMonth(mthIdx, Yr.options[Yr.options.selectedIndex].text);

	if (mthIdx==2) 
	{
		if (Dt.options.selectedIndex < numDays)
		{
				return 0;
		}
		else 
		{
			//check for leap year
			Dt.options.selectedIndex=numDays;
			if (numDays==29)
			{
				return 99;
			}
			else 
			{
				return 1;
			}
		}
	}
	if (Dt.options.selectedIndex < numDays)
	{
		value=0;
	}
	else 
	{
		if (Dt.options.selectedIndex > numDays)
		{
			Dt.options.selectedIndex;
			value=3;
		}
		else 
		{
			//index is 31 or 30
			value=2;
		}
	}
	return value;
}


function parseMonth(mth, inM)
// ****************************************************************
// *	Description:	Parses a string and returns a month value
// ****************************************************************
{
	var i=1;
	var retval =1;
	for (i=1;i<=12;i++)
	{
		if (mth == inM.options[i].text)
		{
			retval=i;	
			break;
		}	
	}
	return retval;
}

function parseDay(day, inD)
// ****************************************************************
// *	Description:	Parses a string and returns a day value
// ****************************************************************
{
	var i=1;
	var retval =1;
	for (i=1;i<=31;i++)
	{
		if (day == inD.options[i].text)
		{
			retval=i;	
			break;
		}	
	}
	return retval;
}

function parseYear(year, inY)
// ****************************************************************
// *	Description:	Parses a string and returns a year value
// ****************************************************************
{
	var retval=0;
	var i=0;
	for (i=0; i<=5; i++)
	{
		if (year == inY.options[i].text)
		{
			retval=i;	
			break;
		}	
	}
	return retval;
}

//Calendar Section

//calculation functions
function nextMonth(month)
// ****************************************************************
// *	Description:	Retrieves the next Month's value
// **************************************************************** 
{
	if (month==12)
	{
		return 1;
	}
	else
	{
		return (month+1);
	}
}


function prevMonth(month) 
// ****************************************************************
// *	Description:	Retrieves the previous Month's value
// ****************************************************************
{
	var prevMonth = (month-1)
	if (month==1)
	{
		prevMonth = 12;
	}
	return prevMonth
}

function changeYear(direction,month,year)
// ****************************************************************
// *	Description:	Increments or decrements month when it goes
// *	past Jan or Dec
// ****************************************************************
{
	var theYear = year
	if (direction=="next")
	{
		if (month == 12)
		{
			theYear = (year+1)
		}
	}
	if (direction=="prev")
	{
		if (month == 1)
		{
			theYear = (year-1)
		}
	}
	return theYear
}


function createCalendar(month,year,io) 
// ****************************************************************
// *	Description:	//opens a new window for the calendar
// ****************************************************************
{
	if (!isBrowserSupp())
	{
		alert("Your browser is outdated and does not support this feature")
		return;
	}
	if (navigator.appVersion.indexOf("Mac",0) != -1) 
	{
		calendarWindow = window.open("","Calendar","width=230,height=365,resizable=yes,scrollbars=no");
	} 
	else 
	{
		calendarWindow = window.open("","Calendar","width=230,height=345,resizable=yes,scrollbars=no");
	}
	var mthIdx = month.options.selectedIndex
	var mthVal = getMonthNumber(month.options[mthIdx].text)
	var yearVal = year.options[year.options.selectedIndex].text
	//call the function to populate the window
	generateCalendar(calendarWindow,mthVal,yearVal,io)
}

//generates the look of the calendar
function generateCalendar(target,showMonth,showYear,io)
// ****************************************************************
// *	Description:	generates the contents of the calender window
// **************************************************************** 
{
	if (!isBrowserSupp())
	{
		return;
	}	
	var monthName = new Array ("January","February","March","April","May","June","July","August","September","October","November","December")

	//begin table for calendar
	target.document.open()
	calendar = "<html><head>"
	calendar +="<link href='styles.css' rel='stylesheet' type='text/css'>"
	calendar +="<style>"
	calendar +="td{font-family:verdana;font-size:11px;color:#333333;}"
	calendar +="a:link{font-family:verdana;font-size:11px;color:#333333;}"
	calendar +="a:visited{font-family:verdana;font-size:11px;color:#333333; text-decoration:none;}"
	calendar +="a:hover{font-family:verdana;font-size:11px;color:#666666; text-decoration:underline;}"
	calendar +="</style>"
	calendar +="</head><body bgcolor=ffffff>"
	calendar +="<table border=0 cellspacing=0 cellpadding=2 style='width:150px;height:185px;'>"                                                             
	calendar +="<tr valign=top align=center>"

	//The parseInt function parses the string argument as a signed decimal integer. 
	var mthIdx = parseInt(showMonth);
	var endday = getDaysInMonth(mthIdx, showYear)

	//month header
	calendar +="<td colspan=3 style='background-color:#dddddd;border-bottom:1px solid #999999;'>"
	var index = (mthIdx-1)
	calendar +="<b>" + monthName[index] + " " + showYear + "</b></td>"
	calendar +="</tr>"

	//writes in the day of the week labels
	calendar +="<tr align=center><td colspan=3>"
	calendar +="<table border=0 cellspacing=0 cellpadding=3><tr>"
	calendar +="<td width=10 align=center><b>S</b></td>"
	calendar +="<td width=10 align=center><b>M</b></td>"
	calendar +="<td width=10 align=center><b>T</b></td>"
	calendar +="<td width=10 align=center><b>W</b></td>"
	calendar +="<td width=10 align=center><b>T</b></td>"
	calendar +="<td width=10 align=center><b>F</b></td>"
	calendar +="<td width=10 align=center><b>S</b></td>"
	calendar +="</tr>"

	wholeDate = showMonth + "/01/" + showYear
	thedate = new Date(wholeDate)
	firstDay = thedate.getDay()

	selectedmonth = mthIdx;
	var today = new Date();
	var thisyear = today.getYear();
	var thismonth = today.getMonth() + 1;
	var thisdate = today.getDate();
	selectedyear = showYear

	//var lastDay = (endday + firstDay+1)
	var lastDay = 43
	calendar +="<tr>"
	
	for (var i = 1; i < lastDay; i++)
	{
		strStyle = ""
		if (i <= firstDay || i>endday+firstDay)
		{
			// 'empty' boxes prior to first day & after last day - to make a full blank extra row if necessary
			calendar +="<td>&nbsp;</td>"
		}
		else 
		{
			strLink = "<a href='#' onClick='parent.closeC"+io+"();parent.closeCalendar"+io+"("+(i-firstDay) + ")'> "+(i-firstDay)+"</a>"
	
			var selDay = document.getElementById('selDay'+io);
			var selMonth = document.getElementById('selMonth'+io);
			var selYear = document.getElementById('selYear'+io);
	
			if(i-firstDay==selDay.value && selectedmonth==selMonth.value && showYear==selYear.value)
			{strStyle = "background-color:#eeeeee; border:solid 1px #777777;"}
					
			if(((selectedyear==thisyear)&&(selectedmonth==thismonth)&&(i-firstDay<thisdate)) || ((selectedyear==thisyear)&&(selectedmonth<thismonth)) || (selectedyear<thisyear))
			{
				strStyle += "color:#bbbbbb;"
				strLink = (i-firstDay)
			}
	
			calendar +="<td style='"+strStyle+"' align=center>"+strLink+"</td>"
		}
		
		//must start new row after each week
		if (i % 7 == 0 &&  i != lastDay)
		{
			calendar +="</tr><tr>"
		}
	
	}
	calendar +="</tr></table></td></tr>"


	//next month and previous month buttons
	var goPrevMonth = prevMonth(mthIdx)
	var goNextMonth = nextMonth(mthIdx)
	var nextYear = changeYear("next",parseInt(showMonth),parseInt(showYear))
	var prevYear = changeYear("prev",parseInt(showMonth),parseInt(showYear))
	
	if(navigator.userAgent.indexOf('MSIE',0) != -1)
	{
		calendar +="<tr><td align=left style='background-color:#dddddd;border-top:1px solid #999999;'>"
		if(((selectedyear==thisyear)&&(selectedmonth<=thismonth)) || (selectedyear<thisyear))
		{
			calendar +="<span style='color:#999999;'>&laquo;</span>"
		}
		else
		{
			calendar +="<a href='#' onclick='parent.generateCalendar(parent.cal"+io+","+goPrevMonth+","+prevYear+",\""+io+"\")'>&laquo;</a>"
		}
		calendar +="&nbsp;</td>"
		calendar +="<td align=center style='background-color:#dddddd;border-top:1px solid #999999;'><a href='#' onclick='parent.closeC"+io+"();'>Cancel</a></td>"
		calendar +="<td align=right style='background-color:#dddddd;border-top:1px solid #999999;'>"
		
		sumMonth=thismonth+18;
		remMonth=sumMonth%12;
		addYear=(sumMonth-remMonth)/12;
		
		if((selectedyear>(thisyear+addYear)) || ((selectedyear==(thisyear+addYear)) && (selectedmonth>=remMonth)))
		{
			calendar +="<span style='color:#999999;'>&raquo;</span>"
		}
		else
		{
			calendar +="<a href='#' onclick='parent.generateCalendar(parent.cal"+io+","+goNextMonth+","+nextYear+",\""+io+"\")'>&raquo;</a>"
		}
		calendar +="&nbsp;</td></tr>"
		calendar +="</table></body></html>"
	}
	else
	{
		calendar +="<form><tr><td align=left bgcolor=#dddddd><input type=button value=' < '"+"onClick='document.clear();opener.generateCalendar(opener.calendarWindow,"+goPrevMonth+","+prevYear+",\""+io+"\")'></td>"
		calendar +="<td align=center style='background-color:#cccccc;border-top:1px solid #999999;'><a href='#' onclick='parent.closeC"+io+"();'>Cancel</a></td>"
		calendar +="<td align=right bgcolor=#dddddd><input type=button value=' > '"+"onClick='document.clear();opener.generateCalendar(opener.calendarWindow,"+goNextMonth+","+nextYear+",\""+io+"\")'></td></tr></form>"
		calendar +="</table></body></html>"
	}
	target.document.write(calendar);
}


function closeCalendarStart(day) {
	var yrIdx = parseYear(selectedyear,document.frmSearch.selYearStart );
	// Decrement index for day and month, because code assumes 
	// that we have an extra defaultvalue at the start.
	document.frmSearch.selMonthStart.options.selectedIndex=selectedmonth-1;
	document.frmSearch.selYearStart.options.selectedIndex= yrIdx;
	document.frmSearch.selDayStart.options.selectedIndex=parseInt(day)-1;
}
function closeCalendarEnd(day) {
	var yrIdx = parseYear(selectedyear,document.frmSearch.selYearEnd );
	// Decrement index for day and month, because code assumes 
	// that we have an extra defaultvalue at the start.
	document.frmSearch.selMonthEnd.options.selectedIndex=selectedmonth-1;
	document.frmSearch.selYearEnd.options.selectedIndex= yrIdx;
	document.frmSearch.selDayEnd.options.selectedIndex=parseInt(day)-1;
}
function closeCStart()
{
	if(document.getElementById('calStart') != null)
	{
		var cS = document.getElementById('calStart');
		cS.style.display="none";
	}
}

function closeCEnd()
{
	if(document.getElementById('calEnd') != null)
	{
		var cE = document.getElementById('calEnd');
		cE.style.display="none";
	}
}

function closeBothCals()
{
	closeCStart();
	closeCEnd();
}
