var longmonths = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var shortmonths = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
var today = new Date();
var thedate = longmonths[today.getMonth()] + " " + today.getDate() + ", 19" + today.getYear();
var thismonth = longmonths[today.getMonth()];
if (today.getMonth() == 0) var lastmonth = longmonths[11];
else var lastmonth = longmonths[today.getMonth() - 1];


function dateSelect(name, selectedMonth, selectedDay, selectedYear, monthLength, startYear, endYear) {
	lastDay = getLastDay(selectedMonth, selectedYear);
	returnString = monthSelect(name, selectedMonth, monthLength);
	returnString += daySelect(name, selectedDay, lastDay);
	returnString += yearSelect(name, selectedYear, startYear, endYear);	
	return returnString;
}


function monthSelect(name, selectedMonth, monthLength) {
	if (monthLength == "S") months = shortmonths;
	else months = longmonths;
	returnString = "<select name = \"" + name + "Month\" onchange=\"changeDaySelect('" + name + "')\">"
	for (m = 1; m < 13; m ++) {
		if (m == selectedMonth)
		{ returnString += "<option value=\"" + m + "\" selected>" + months[m-1] }
		else
		{ returnString += "<option value=\"" + m + "\">" + months[m-1] }
	}
	returnString += "</select>"
	return returnString;
}

function daySelect(name, selectedDay, lastDay) {
	returnString = "<select name = \"" + name + "Day\">"	
	for (d = 1; d <= lastDay; d ++) {
		if (d == selectedDay)
		{ returnString += "<option value=\"" + d + "\" selected>" + d }
		else
		{ returnString += "<option value=\"" + d + "\">" + d }
	}
	returnString += "</select>"
	return returnString;
}

function yearSelect(name, selectedYear, startYear, endYear) {
	returnString = "<select name = \"" + name + "Year\" onchange=\"changeDaySelect('" + name + "')\">"
	for (y = startYear; y <= endYear; y ++) {
		if (y == selectedYear)
		{ returnString += "<option value=\"" + y + "\" selected>" + y }
		else
		{ returnString += "<option value=\"" + y + "\">" + y }
	}
	returnString += "</select>"
	return returnString;
}

function getLastDay(month, year) {
	theDate = new Date(year, month, 1);
	theDate2 = theDate.getTime() - (1000 * 60 * 60 * 24);
	theDate.setTime(theDate2);
	return theDate.getDate();
}

function changeDaySelect(name) {
	prefix = "document.forms[0]." + name;
	theMonth = eval(prefix + "Month[" + prefix + "Month.selectedIndex].value");
	theYear = eval(prefix + "Year[" + prefix + "Year.selectedIndex].value");
	theLastDay = getLastDay(theMonth, theYear);
	if (eval(prefix + "Day[" + prefix + "Day.selectedIndex].value") > theLastDay) {
		eval(prefix + "Day.selectedIndex = " + (theLastDay - 1));
	}
	eval(prefix + "Day.length = " + theLastDay);
	for (daynum = 1; daynum <= theLastDay; daynum++) {
		eval(prefix + "Day.options[" + (daynum - 1) + "].value = \"" + daynum + "\"");
		eval(prefix + "Day.options[" + (daynum - 1) + "].text = \"" + daynum + "\"");
	}
}

function setMonth() {
	prefix = "document.forms[0].Start";
	theMonth = eval(prefix + "Month[" + prefix + "Month.selectedIndex].value");
	theYear = eval(prefix + "Year[" + prefix + "Year.selectedIndex].value");
	document.forms[0].StartMonth.selectedIndex = theMonth - 1;
	changeDaySelect("Start");
	document.forms[0].EndMonth.selectedIndex = theMonth - 1;
	changeDaySelect("End");
	document.forms[0].StartDay.selectedIndex = 0;
	document.forms[0].EndDay.selectedIndex = getLastDay(theMonth, theYear) - 1;	
}

function setYear() {
	prefix = "document.forms[0].Start";
	theYear = eval(prefix + "Year[" + prefix + "Year.selectedIndex].value");
	document.forms[0].StartMonth.selectedIndex = 0;
	changeDaySelect("Start");
	document.forms[0].EndMonth.selectedIndex = 11;
	changeDaySelect("End");
	document.forms[0].StartDay.selectedIndex = 0;
	document.forms[0].EndDay.selectedIndex = 30;	
}
