var currentYear = 2000;

function makeArray(n) {
	this.length = n;
	return this;
}

// ------ global declarations and initialization
function init ($myDate) {
	theDate         = new Date();
	var tryAgain = true;
	var numErrs = 0;
	
	if ($myDate != null) {
		today = $myDate;
	}
	else {
		today = new Date();
	}
	// Put this in a loop so if we get an error with a passed in date we can try todays date.
	while (tryAgain) {
		try {			
			currMonth       = today.getMonth();
			if (today.getYear() < 1000) {
				currYear = today.getYear() + 1900;
			}
			else {
				currYear = today.getYear();
			}
			tryAgain = false;	// Everything is ok.  Just continue.
		} catch(e) {
			today = new Date();	// If there was a problem with the date then use todays date.
			if (numErrs != 0) {
				tryAgain = false;	// If we get another error then don't try again.
			}
			numErrs++;
		}
	}
	msPerDay        = 24*60*60*1000;
	cookieString    = "dispMonth="+currMonth+";";
	document.cookie = cookieString;
	startOfString   = document.cookie.indexOf("dispMonth");
	countbegin      = document.cookie.indexOf("=",startOfString) + 1;
	countend        = document.cookie.indexOf(";",countbegin);
	if (countend == -1) {
		countend = document.cookie.length;
	}

	dispMonth     = eval ("document.cookie.substring(countbegin,countend)");
	firstOfMonth  = new Date(currYear,dispMonth,1);
	monthName     = new makeArray(12);
	monthName[1]  = "January";
	monthName[2]  = "February";
	monthName[3]  = "March";
	monthName[4]  = "April";
	monthName[5]  = "May";
	monthName[6]  = "June";
	monthName[7]  = "July";
	monthName[8]  = "August";
	monthName[9]  = "September";
	monthName[10] = "October";
	monthName[11] = "November";
	monthName[12] = "December";
}

// ------ entry point
function openCalendar (dateFldName, currentDate, formNumber) {
	if (currentDate != 'null') {
		$currentDate = 1;
		firstOfMonth = makeDate(currentDate, 0);
		init(firstOfMonth);
	}
	else {
		$currentDate = 0;
		init();
	}
	formNum  = formNumber;
	dateFld = dateFldName;
	windowOptions  = "toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,copyhistory=yes,width=275,height=290";
	calendarWindow = this.open("","calendarWindow",windowOptions);
	calendarWindow.callingForm = this;
	redraw();
	$currentDate = 0;
}

// ------ repaint the calendar
function redraw() {
	
	calendarWindow.callingForm = this;
	if ($currentDate != 1) {
		firstOfMonth = new Date(currYear,dispMonth,1);
	}
	calendarWindow.document.open();
	calTitle = "<TITLE>Web Calendar</TITLE>";
	calendarWindow.document.write(calTitle);
	drawCalendar(firstOfMonth);
	calendarWindow.document.write(htmlBuffer);
	calendarWindow.document.close();
	calendarWindow.callingForm = this;
	calendarWindow.focus();
}

// ------ fill the calling forms date and month
function fillDate(filler) {

	var m = new String(monthNum);
	var d = new String (filler);

	if (yearNum >= 99 && yearNum <= currentYear) { yearNum += 1900; }

	var y = new String(yearNum);
	//alert("\"document.\"" + formNum + "\".m_\"" + dateFld + "\".value\"")
	eval(formNum + ".m_" + dateFld + ".value= '" + m + "';");
	eval(formNum + ".d_" + dateFld + ".value= '" + d + "';");
	eval(formNum + ".y_" + dateFld + ".value= '" + y + "';");
	
	//var mydate = m + "/" + d + "/" + y.charAt(2) + y.charAt(3);
	
	//eval("document." + formNum + "." + dateFld + ".value= '" + mydate + "';");
	
	calendarWindow.close();
}

// ------ set the month
function changeMonth (increment) {
	nextMonth = dispMonth;
	if (increment == 1) {
		nextMonth++ ;
	} 
	else {
		nextMonth-- ;
	}
	dispMonth = nextMonth;
	document.cookie="dispMonth="+nextMonth;
	redraw();
}

// ------ generate the calendar document
function drawCalendar (theDate) {
	monthNum = theDate.getMonth() + 1;
	yearNum = theDate.getYear();
	htmlBuffer  = "<HTML>";
	htmlBuffer += "<BODY BGCOLOR=#FFFFCC>";
	htmlBuffer += "<FORM>";
	htmlBuffer += "<CENTER><TABLE BORDER=1 WIDTH=50% CELLSPACING=0 CELLPADDING=1>";
	htmlBuffer += "<TH COLSPAN=2><INPUT TYPE=BUTTON NAME=monthDn VALUE=\"<<\" onClick=callingForm.changeMonth(-1)>";
	htmlBuffer += "<TH COLSPAN=3><FONT face='Verdana, Helvetica, Arial' size=2> ";
	htmlBuffer += monthName[monthNum];
	if (theDate.getYear() < 1000)
		var dispYear = theDate.getYear() + 1900
	else 
		var dispYear = theDate.getYear()
	htmlBuffer += " " + dispYear;
	htmlBuffer += "<TH COLSPAN=2><INPUT TYPE=BUTTON NAME=monthUp VALUE=\">>\" onClick=callingForm.changeMonth(1)>";
	htmlBuffer += "<TR>";
	htmlBuffer += "<TH><FONT face='Verdana, Helvetica, Arial' size=2>S";
	htmlBuffer += "<TH><FONT face='Verdana, Helvetica, Arial' size=2>M";
	htmlBuffer += "<TH><FONT face='Verdana, Helvetica, Arial' size=2>T";
	htmlBuffer += "<TH><FONT face='Verdana, Helvetica, Arial' size=2>W";
	htmlBuffer += "<TH><FONT face='Verdana, Helvetica, Arial' size=2>T";
	htmlBuffer += "<TH><FONT face='Verdana, Helvetica, Arial' size=2>F";
	htmlBuffer += "<TH><FONT face='Verdana, Helvetica, Arial' size=2>S<TR>";

	drawBody(theDate);
	htmlBuffer += "</TABLE>";
	htmlBuffer += "</BODY></HTML>";
}

// ------ generate the calendar body
function drawBody (theDate) {
	thisMonth = theDate.getMonth();
	thisDate  = theDate.getDate();
	firstSunday(theDate);
	for (w=0; w<6; w++) {
		for (d=0; d<7; d++) {
			date = theDate.getDate();
			htmlBuffer   += "<TD ALIGN=CENTER>";
			// skip previous month
			if (theDate.getMonth() != thisMonth) {
				htmlBuffer += "<BR>";
			}
			else {
				htmlBuffer += "<INPUT TYPE=BUTTON NAME=";
				htmlBuffer += date;
				htmlBuffer += " VALUE=";
				if ( date < 10 ) {
					htmlBuffer += "0";
				}
				htmlBuffer += date;
				htmlBuffer += " onClick=callingForm.fillDate(" + date + ");>";
			}
			// increment the date
			timezoneoffset = theDate.getTimezoneOffset();
			if ( timezoneoffset == 0) {
				theDate.setTime(newTime);
			} 
			newTime = theDate.getTime() + msPerDay;
			theDate.setTime(newTime);
			// check for DST 
			if (theDate.getHours() != 23) {
				theDate.setTime(newTime + 3600000);
			} 
		}
		htmlBuffer += "<TR>";
	}
	htmlBuffer += "</FORM>";
}


// ------ reset the startdate to get the
// ------ previous sunday before the current date
// ------ so that the drawing can begin from a sunday
function firstSunday (fromDate) {
	while (fromDate.getDay() != 0) {
		newTime = fromDate.getTime() - msPerDay;
		fromDate.setTime(newTime);
		if ( fromDate.getDay() == 6 ) {
			return;
		}
	}
}

function makeDate (data, check) {
	
	error = 0;
	$month_length = data.indexOf("/");
	$month_index = 0;
	$day_index = $month_length + 1;
	
	$day_length = data.indexOf("/", $day_index) - $day_index;
	$year_index = data.indexOf("/", $day_index) + 1;
	$year_length = data.length - $year_index
	if((data.charAt(1) != "/" && (data.charAt(2) != "/") || (data.charAt(3) != "/" && data.charAt(4) != "/" && data.charAt(5)!="/")) || data.length > 10)
		error = 1;
	else
	{
		if ($day_length == 2)
			$day = eval(data.charAt($day_index)+data.charAt($day_index+1));
		else
			$day = eval(data.charAt($day_index));

		$day = parseInt($day);


		if ($month_length == 2)
			$mnth = eval(data.charAt($month_index)+data.charAt($month_index+1));
		else
			$mnth = eval(data.charAt($month_index));

		$mnth = parseInt($mnth);
		

		if ($year_length == 2) {
			$check_yr = eval(data.charAt($year_index)+data.charAt($year_index+1));
			if (parseInt($check_yr) >= 90) {
				$yr = eval(19+data.charAt($year_index)+data.charAt($year_index+1));
			}
			else {
				$yr = eval(20+data.charAt($year_index)+data.charAt($year_index+1));
			}
		}
		else if ($year_length == 4)
			$yr = eval(data.charAt($year_index)+data.charAt($year_index+1)+data.charAt($year_index+2)+data.charAt($year_index+3));
		else
			error = 5;

		if (error != 5) {
			$yr = parseInt($yr);
		

			if($mnth>12 || $mnth<1)
				error = 2;
			if (error != 2) {
					
		                    // mnthArray[0] is january, mnthArray[11] is december
				$mnthArray = new Array(31,29,31,30,31,30,31,31,30,31,30,31);
		
				// netscape/IE number months starting with january = 0
				$mnth = $mnth-1;
				
				//alert("The year here is: " + $yr);
				$mydate = new Date($yr,$mnth,$day);
		
				if($day > parseInt($mnthArray[parseInt($mnth)]))
					{
						// Don't diplay the message.  We will just go to todays month.
						//alert("day is " + $day);
						//alert("month array: " + parseInt($mnthArray[parseInt($mnth)]));
					error = 3;
					}
			}
		}
	}
	switch (check) {
		case 0:
		if (error == 0) {
			return $mydate;
		}
		else {
			return false;
		}
		break;
		
		case 1:
		if (error == 0) {
			return true;
		}
		else {
			alert ("Calendar cannot open because date is invalid.\n");
			return false;
		}
		break;
	}	
}


////////////////////////////////////////////////////////////
//   Calendar Validation Tools
////////////////////////////////////////////////////////////

var isNav, isIE;
var coll = "";
var styleObj = "";
if (parseInt(navigator.appVersion) >= 4) {
	if (navigator.appName == "Netscape") {
		isNav = true;
		isNav4 = true;
		isIE = false;
		coll = "";
	} else {
		isIE = true;
		coll = "all.";
		styleObj = ".style";
		isNav4 = false;
	}
}

$chars = new Array();
function validate(data, $obj)
{
	error = 0;
	for ($i = 0; $i < data.length; $i++) {
		$chars[$i] = data.charAt($i);
	}
	$month_length = data.indexOf("/");
	$month_index = 0;
	$day_index = $month_length + 1;
	
	$day_length = data.indexOf("/", $day_index) - $day_index;
	$year_index = data.indexOf("/", $day_index) + 1;
	$year_length = data.length - $year_index
	if((data.charAt(1) != "/" && (data.charAt(2) != "/") || (data.charAt(3) != "/" && data.charAt(4) != "/" && data.charAt(5)!="/")) || data.length > 10)
		error = 1;
	else
	{
		if ($day_length == 2)
			$day = eval(data.charAt($day_index)+data.charAt($day_index+1));
		else
			$day = eval(data.charAt($day_index));

		$day = parseInt($day);


		if ($month_length == 2)
			$mnth = eval(data.charAt($month_index)+data.charAt($month_index+1));
		else
			$mnth = eval(data.charAt($month_index));

		$mnth = parseInt($mnth);
		

		if ($year_length == 2) {
			$check_yr = eval(data.charAt($year_index)+data.charAt($year_index+1));
			$yr = eval(19+data.charAt($year_index)+data.charAt($year_index+1));
			if (parseInt($check_yr) >= 90) {
				$yr = eval(19+data.charAt($year_index)+data.charAt($year_index+1));
			}
			else {
				$yr = eval(20+data.charAt($year_index)+data.charAt($year_index+1));
			}
		}
		else if ($year_length == 4)
			$yr = eval(data.charAt($year_index)+data.charAt($year_index+1)+data.charAt($year_index+2)+data.charAt($year_index+3));
		else
			error = 5;

		if (error != 5) {
			$yr = parseInt($yr);
			//alert("YEAR test: " + $yr);

			if($mnth>12 || $mnth<1)
				error = 2;
			if (error != 2) {
					
		                    // mnthArray[0] is january, mnthArray[11] is december
				$mnthArray = new Array(31,29,31,30,31,30,31,31,30,31,30,31);
		
				// netscape/IE number months starting with january = 0
				$mnth = $mnth-1;
				$mydate = new Date($yr,$mnth,$day);
		
				if($day > parseInt($mnthArray[parseInt($mnth)]))
					error = 3;
			}
		}
	}

	switch(error)
	{
	
	case 3: alert("The day you entered is invalid for the month.");
			if ($obj != null) {
				$theObj = eval($obj);
				//$theObj.focus();
				//$theObj.select();
			}
			return false;
			break;
	case 4: alert("The date you entered does not occur on a Friday.");
			if ($obj != null) {
				$theObj = eval($obj);
				//$theObj.focus();
				//$theObj.select();
			}
			return false;
			break;
	case 5: alert("The year you entered is invalid.");
			if ($obj != null) {
				$theObj = eval($obj);
				//$theObj.focus();
				//$theObj.select();
			}
			return false;
			break;
	}


}

function SetDays(month,listDay)
{
	
	//alert(month + "  " +listDay);
	var mnthArray = new Array(31,29,31,30,31,30,31,31,30,31,30,31);
	var dayControl = document.getElementById(listDay);
	var varChosenDay = dayControl.value;
	//alert(varChosenDay)
	if (month != -1 && dayControl.options.length-1 != mnthArray[month-1]) {
		while(dayControl.options.length) {
			//dayControl.options.remove(0);
			dayControl.options[0] = null;
		}
		//dayControl.options[0] = (new Option(String(" "),-1), -1);
		dayControl.options[0] = new Option(String(" "),-1);
		for(i=1; i <= mnthArray[month-1]; i++) {
			//dayControl.options[i] = (new Option(String(i),i), i);
			dayControl.options[i] = new Option(String(i),i);
		}
	}	
	dayControl.value = varChosenDay;
}

function CheckLeapYear(varBaseName)
{
	//The basename of the calendar control is prefixed with m_ for month, d_ for day, and y_ for year

	var monthControl = document.getElementById('m_' + varBaseName);
	var dayControl = document.getElementById('d_' + varBaseName);
	var yearControl = document.getElementById('y_' + varBaseName);
	if (monthControl.value == "2" && String(yearControl.value).length == 4 && Number(dayControl.value) > 28) {
		var varModulus = Number(yearControl.value);// % 4;
		if (varModulus != 0) {
			alert("Invalid Date!\nYou have chosen a leap year date, Feb 29, for the year " + 
				  yearControl.value + " which is not a leap year.\n\n" +
				  "Please check your entry and try again.");
		}		
	}
}