// open calendar based on current month/year
function OpenCalendar(which) {

	// create references to form date objects
	var DaysObject = eval("document.editform." + which + "_day");
	var MonthObject = eval("document.editform." + which + "_month");
	var YearObject = eval("document.editform." + which + "_year");

	// get values from the month/year objects
	var day = DaysObject[DaysObject.selectedIndex].value;
	var month = MonthObject[MonthObject.selectedIndex].value;
	var year = YearObject[YearObject.selectedIndex].value;
	
	// open calendar window
	var CalendarWindow = window.open("calendar.cgi?which=" + which + "&amp;month=" + month + "&amp;day=" + day + "&amp;year=" + year,"Calendar","width=200,height=220");
	
	// set focus to calendar window
	CalendarWindow.focus();
}

// set days in month for selected month/year
function DaysInMonth(month,year) {
	var DaysInMonth = 
		month == "April" || month == "June" || month == "September" || month == "November" ? 30 :
		month == "February" && (year/4) != Math.floor(year/4) ? 28 : 
		month == "February" && (year/4) == Math.floor(year/4) ? 29 : 
		31;	// defaults to 31
	return DaysInMonth;
}

// set option list for selected month/year
function ChangeOptionDays(which) {
	
	// create references to form date objects
	var DaysObject = eval("document.editform." + which + "_day");
	var MonthObject = eval("document.editform." + which + "_month");
	var YearObject = eval("document.editform." + which + "_year");
	
	// get text label from the month/year objects
	var month = MonthObject[MonthObject.selectedIndex].text;
	var year = YearObject[YearObject.selectedIndex].text;
	
	// get number of days based on month/year
	var DaysForThisSelection = DaysInMonth(month,year);
	
	// get current number of day in day object
	var CurrentDaysInSelection = DaysObject.length;

	// if the current number of days is greater 
	// than the total possible days based upon
	// month/year, then remove the extra day 
	// options.
	if (CurrentDaysInSelection > DaysForThisSelection) {
		for (i = 0; i < (CurrentDaysInSelection-DaysForThisSelection); i++) {
			DaysObject.options[DaysObject.options.length - 1] = null;
		}
	}
	
	// if the current number of days is less 
	// than the total possible days based upon
	// month/year, then add extra days options
	// to the end of the month object.
	if (DaysForThisSelection > CurrentDaysInSelection) {
		for (i = 0; i < (DaysForThisSelection-CurrentDaysInSelection); i++) {
			NewOption = new Option(DaysObject.options.length + 1);
			DaysObject.options[DaysObject.options.length] = NewOption;
		}
	}
	
	// if the day object has no selection, set
	// the selection to the first option value
	if (DaysObject.selectedIndex < 0) DaysObject.selectedIndex == 0;
}

// pass date value to the form from calendar.cgi
function PassDate(which,month,day,year){
	// check for opener window
	if (opener){
		// the opener window object is still open
		
		// create references to form date objects
		var MonthSelect = eval("opener.document.editform." + which + "_month");
		var DaySelect = eval("opener.document.editform." + which + "_day");
		var YearSelect = eval("opener.document.editform." + which + "_year");
		
		// because the month and day objects have 
		// values that start at one we can select
		// the option based on the objects index
		var MonthOption = eval("MonthSelect.options[" + (month - 1) + "]");
		var DayOption = eval("DaySelect.options[" + (day - 1) + "]");
		
		// because the years option values are 
		// unpredictable, we have to loop through
		// each options value to match the year to
		// the index of the matching option value
		var YearOption = "";
		for (var i = 0; i < YearSelect.length; i++) {
			if (YearSelect.options[i].text == year) YearOption = YearSelect.options[i];
		}
		
		// set the date objects' selected option
		MonthOption.selected = true;
		DayOption.selected = true;
		YearOption.selected = true;
		
		// close the calendar window
		self.close();
	}
	else alert("An error occured while attempting to pass the date information to the form (error: opener window not found)");
}

