var testWindow;
var formType;
var fName;
function update(themonth,theday){
document.forms[fName].elements[formType+'Day'].options[theday - 1].selected = true;
document.forms[fName].elements[formType+'Month'].options[themonth].selected = true;
testWindow.close();
}
function isLeapYear( yrStr ) {
	var leapYear = false;
	var year = parseInt( yrStr, 10 );
	// every fourth year is a leap year
	if ( year % 4 == 0 ) {
		leapYear = true;
		// unless it's a multiple of 100
		if( year % 100 == 0 ) {
			leapYear = false;
			// unless it's a multiple of 400
			if( year % 400 == 0 ) {
				leapYear=true;
			}
		}
	}
	return leapYear;
}
function loadCalendar(viewMonth) {
testWindow.document.close();
testWindow.document.open();
var day_of_week = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
var month_of_year = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var dateObj = new Date();
var year = dateObj.getYear(); // Returns year
var month = dateObj.getMonth(); // Returns month (0-11)
var today = dateObj.getDate(); // Returns day (1-31)
var weekday = dateObj.getDay(); // Returns day (1-31)
var DAYS_OF_WEEK = 7; // "constant" for number of days in a week
var minMonth = dateObj.getMonth();
var maxMonth = month-1;
var thisYear = year;

var cal; // Used for printing
if (viewMonth < month) {
year = year + 1;
}
if(year < 1900){
year += 1900; //done to solve the problem of netscape and mozilla returning year in form 10X for years > 2000
}
month = viewMonth;
var DAYS_OF_MONTH = 31; // "constant" for number of days in a month
if (month == 1) {
	if (isLeapYear(year)) {
		DAYS_OF_MONTH = 29;
	} else {
		DAYS_OF_MONTH = 28;
	}
}
if (month == 3 | month == 5 | month == 8 | month == 10) {
	DAYS_OF_MONTH = 30;
}
dateObj.setDate(1); // Start the calendar day at '1'
dateObj.setMonth(month); // Start the calendar month at now
dateObj.setYear((year));
// VARIABLES FOR FORMATTING
var prevM = month - 1;
var nextM = month + 1;
if (nextM == 12) {
nextM = 0;
}
if (prevM == -1) {
prevM = 11;
}
// BEGIN CODE FOR CALENDAR
cal = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">' + '\n';
cal += '<html xmlns="http://www.w3.org/1999/xhtml">' + '\n';
cal += '<head>' + '\n';
cal += '<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />' + '\n';
cal += '<title>Calendar</title>' + '\n';
cal += '<script type="text/javascript" src="../newincludes/newincludes/calendar.js"></script>' + '\n';
cal += '<link href="../newincludes/newincludes/calendar.css" rel="stylesheet" type="text/css" />' + '\n';
cal += '</head>' + '\n';
cal += '<body>' + '\n';
cal += '<div id="c">' + '\n';
cal += '<p id="m">';
if ((year == thisYear) && (month == minMonth) ) {
	cal += '<span id="prX">Next Month</span>';
} else {
	cal += '<a id="pr" href="javascript:;" onclick="window.opener.loadCalendar(' + prevM + ');">Previous Month</a>';
}
if ((month == maxMonth) && (year > thisYear)) {
	cal += '<span id="nxX">Next Month</span>';
} else {
	cal += '<a id="nx" href="javascript:;" onclick="window.opener.loadCalendar(' + nextM + ');">Next Month</a>';
}
cal += month_of_year[month] + ' ' + year;
cal += '</p>' + '\n';
for(index=0; index < DAYS_OF_WEEK; index++) { // PRINTS DAY NAMES
	cal += '<div class="dn">' + day_of_week[index] + '</div>' + '\n';
}
cal += '<div id="ds">';
for(index=0; index < dateObj.getDay(); index++) {// FILL IN BLANK GAPS UNTIL FIRST DAY OF THE MONTH
	cal += '<span class="x"> </span>';
}
for(index=0; index < DAYS_OF_MONTH; index++) {// PRINTS EACH DAY IN CALENDAR
	var day = dateObj.getDate();
	cal += '<a class="d" href="javascript:;" onclick="window.opener.update(' + month + ',' + day + ')"  >' + day + '</a>';
	dateObj.setDate(dateObj.getDate()+1);
}
cal += '</div>' + '\n';
cal += '</div>' + '\n';
cal += '</body>' + '\n';
cal += '</html>';
//cal = '<html><body>' + dateObj.getMonth(); '</body></html>';
// PRINT CALENDAR
testWindow.document.write(cal);
}
function openCalendar(formName,type) {
formType=type;
fName=formName;
testWindow = window.open('','dates','width=238,height=246,resizable=1,status=1,menubar=0,location=0');
loadCalendar(document.forms[fName].elements[formType+'Month'].selectedIndex);
}
