//**********************************************************************
//Calendaring Javascript FUNCTIONS
//**********************************************************************
function makeArray(n)
{
	this.length = n;
	return this;
}

// global declarations and initialization
function init ()
{
	theDate         = new Date();
	today           = new Date();
	currMonth       = today.getMonth();
	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(today.getYear(),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 (monthFldName, dateFldname, yearFldName, formNumber, tableColor)
{
	init();
	//NQF: begin
	formNum = formNumber
	//added to accept LHS variable of this type : arr(x)
	for (var j=0; j< document.forms[formNum].length ; j++)
	{
		if ( document.forms[formNum].elements[j].name.toLowerCase() == monthFldName.toLowerCase())
			monthFld = "elements["+j+"]";
		if ( document.forms[formNum].elements[j].name.toLowerCase() == dateFldname.toLowerCase() )
			dateFld = "elements["+j+"]";
		if ( document.forms[formNum].elements[j].name.toLowerCase() == yearFldName.toLowerCase() )
			yearFld = "elements["+j+"]";
	}

	//NQF: end
	windowOptions  = "toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,copyhistory=yes,width=300,height=200";
	// calendarWindow = this.open("","calendarWindow",windowOptions);
	calendarWindow = this.open("../index/calendar.html","calendarWindow",windowOptions);
	calendarWindow.callingForm = this;
	redraw(tableColor);
}

// repaint the calendar
function redraw(tableColor)
{
	calendarWindow.callingForm = this;
	//Y2K Browser compatibility issues. getYear() function works for IE.. but had to be
	//altered to appear correctly for Netscape.
	if (navigator.appName =='Netscape' )
	{
		newYear = today.getYear();
		if (newYear <= 1900)
		newYear = 1900 + newYear;
	}
	else newYear = today.getYear();
	firstOfMonth = new Date(newYear,dispMonth,1);
	//End Y2K Browser fix.
	calendarWindow.document.open();
	drawCalendar(firstOfMonth, tableColor);
	calendarWindow.document.write(htmlBuffer);
	calendarWindow.document.close();
	calendarWindow.callingForm = this;
	calendarWindow.focus();
}

// fill the calling forms date and month
function fillDate(filler, filler1)
{
	var m = monthNum;
	var d = filler;
	var y = filler1;

	eval("document.forms[" + formNum + "]." + monthFld + ".value=" + m);
	eval("document.forms[" + formNum + "]." + dateFld  + ".value=" + d);
	eval("document.forms[" + formNum + "]." + yearFld  + ".value=" + filler1);

	calendarWindow.close();
}

// set the month
function changeMonth (increment, tableColor)
{
	var loopNumTimes = increment;
	if( loopNumTimes < 0 ) loopNumTimes = -loopNumTimes;

	for(i=0; i<loopNumTimes; i++)
	{
		nextMonth = dispMonth;
		if( increment < 0 ) nextMonth--;
		else nextMonth++;
		// use the next two lines of code to limit the number of months
		// that a user can cycle forward or back
		// if they exceed that number, it wraps to the current month
		//if ((nextMonth-currMonth >= 13) || (currMonth-nextMonth >= 13))
		//	nextMonth = currMonth;
		dispMonth = nextMonth;
		document.cookie="dispMonth="+nextMonth;
	}
	redraw(tableColor);
}

// generate the calendar document
function drawCalendar (theDate, tableColor)
{
	monthNum = theDate.getMonth() + 1;
	htmlBuffer  = "<HTML>";
	//
	htmlBuffer += "<HEAD><title>Calendar</title><style>th { font-size:10px }th { color:#FFFFFF }th { font-family:verdana, sans-serif;}";
	htmlBuffer += "td { font-size:10px }td { color:#000000 }td { font-family:verdana, sans-serif;font-weight:bold;}</style></HEAD>";
	htmlBuffer += "<BODY BGCOLOR=#FFFFFF>";
	htmlBuffer += "<FORM>";
	htmlBuffer += "<CENTER><TABLE BORDER=0 CELLPADDING=2 CELLSPACING=2 WIDTH=270 BGCOLOR='" + tableColor + "'>"
	htmlBuffer += "<TH COLSPAN=7 align=center><INPUT TYPE=BUTTON NAME=yearDn VALUE=\"<<\" onClick=callingForm.changeMonth(-12,'" + tableColor + "')>&nbsp;<INPUT TYPE=BUTTON NAME=monthDn VALUE=\"<\" onClick=callingForm.changeMonth(-1,'" + tableColor + "')>&nbsp;";
	htmlBuffer += monthName[monthNum];
	// getYear returns the year in year - 1900
	// this causes ugly display
	var tempYear = theDate.getYear()
	if ( tempYear <= 1900 ) tempYear += 1900;
	htmlBuffer += " " + tempYear;
	htmlBuffer += "&nbsp;<INPUT TYPE=BUTTON NAME=monthUp VALUE=\">\" onClick=callingForm.changeMonth(1,'" + tableColor + "')>&nbsp;<INPUT TYPE=BUTTON NAME=monthUp VALUE=\">>\" onClick=callingForm.changeMonth(12,'" + tableColor + "')>";
	htmlBuffer += "</TR><TR>";
	htmlBuffer += "<TH>S</TH><TH>M</TH><TH>T</TH><TH>W</TH><TH>T</TH><TH>F</TH><TH>S</TH></TR><TR BGCOLOR=WHITE>";
	drawBody(theDate);
	htmlBuffer += "</TABLE>";
	htmlBuffer += "</BODY></HTML>";
}

// generate the calendar body
function drawBody (theDate)
{
	thisMonth = theDate.getMonth();
	thisDate  = theDate.getDate();
	thisYear  = theDate.getYear();
	if(thisYear <= 1900 )
		thisYear += 1900;
		
	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 += "<a href='javascript:onClick=callingForm.fillDate(" + date + "," + thisYear + ");'>";
				htmlBuffer += date;
				htmlBuffer += "</a>";
			}
			htmlBuffer   += "</TD>";
			
			// increment the date
			newTime = theDate.getTime() + msPerDay;
			theDate.setTime(newTime);
			// check for DST
			if (theDate.getHours() != 23) theDate.setTime(newTime + 3600000);
		}
		htmlBuffer += "<TR BGCOLOR=WHITE>";
	}
	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);
	}
}
//**********************************************************************
//END Calendaring Javascript FUNCTIONS
//**********************************************************************

// this function checkes to see if a date is valid
// it returns an empty string if all is ok, or a non-empty string describing errors
// accepts the values of the month, day, and year fields as arguments
// assumes that month and day have maxlengths of 2 and that year has a maxlength of 4
// requires a 4-digit year to be passed or an error will be returned
// requires the isNumeric() function
function checkValidDate(month, day, year)
{
	var daysInMonth = 31; // the most common case - we will change it where needed
	var emsg = '';

	if( !isNumeric(month) || month.length < 1 || month < 1 || month > 12 )
	{
		emsg += 'You entered an invalid month.\n';
		return emsg;
	}

	// 30 days have September, April, June, and November
	if( month == '04' || month == '4' ) daysInMonth = 30;
	else if( month == '06' || month == '6' ) daysInMonth = 30;
	else if( month == '09' || month == '9' ) daysInMonth = 30;
	else if( month == '11' ) daysInMonth = 30;
	else if( month == '02' || month == '2' )
	{
		daysInMonth = 28;
		// if the year is divisible by 400 or divisible by 4 and not by 100, it's a leap year
		if( year % 400 == 0 ) daysInMonth = 29;
		else if( year % 4 == 0 && year % 100 != 0 ) daysInMonth = 29;
	}

	if( !isNumeric(day) || day.length < 1 || day < 1 || day > daysInMonth ) emsg += 'You entered an invalid day - only 1-' + daysInMonth + ' are valid.\n';
	if( !isNumeric(year) || year.length != 4 ) emsg += 'Please enter a valid 4-digit year.\n';

	return emsg;
}

// returns true if the character passed is a digit, false otherwise
function isDigit(c)
{
	return ((c >= "0") && (c <= "9"));
}

// this function returns true if all characters in field are numeric, false otherwise
// requires the isDigit function
function isNumeric(field)
{
	for (i = 0; i < field.length; i++)
	{
		var c = field.charAt(i);
		if (!isDigit(c)) return false;
	}
	return true;
}

