// generic date arrays
month = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
days = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
monthLength = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
monthLengthLeap = new Array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

function leadingZero(number) {
	if (number < 10) {
		number = "0"+number;
	}
    return number;
}

function buildCalendar(returnToId,buildInId,displayDate,displayMonth,displayYear) {
// returnToId needs to be an object - this is the input element the date returns too
// buildInId is the element where the calendar gets spawned

//highlight form element for usability
document.getElementById(returnToId).className = "calendarReturn";

//yearDisplayLimit is the +/- year range for the year drop down
yearDisplayLimit = 10;

//create default date object
currentDate = new Date();	

//set defaults and parse them as integers
if (!displayDate) {
	displayDate = currentDate.getDate();
}
if (!displayMonth) {
	displayMonth = currentDate.getMonth();
}	
if (!displayYear) {
	displayYear = currentDate.getFullYear();
}		
displayDate = parseInt(displayDate);
displayMonth = parseInt(displayMonth);
displayYear = parseInt(displayYear);

// ensure that years and months don't get messed up when going from december to january and vice versa
if (displayMonth==12) {
	displayMonth=0;
	displayYear=displayYear+1;
}	
if (displayMonth<0) {
	displayMonth=11;
	displayYear=displayYear-1;
} 		

//get the month's starting day
displayStartDate = new Date(displayYear, displayMonth, "1");
displayStartDate = displayStartDate.getDay();

//check for leap years
if ((displayYear%4!=0) || ((displayYear%100==0) && (displayYear%400!=0))) {
	displayMonthLength = monthLength[displayMonth];
} else {
	displayMonthLength = monthLengthLeap[displayMonth];
}


buildCalendarHTML="<table summary=\"Calendar for date selection\" class=\"calendarSelectionTable\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">";
buildCalendarHTML+="<tr>";
buildCalendarHTML+="<th colspan=\"7\">";
buildCalendarHTML+="<select name=\"calendarSelectionMonth\" class=\"calendarSelectionMonth\" id=\"calendarSelectionMonth"+returnToId+"\" onchange=\"javascript:buildCalendar('"+returnToId+"','"+buildInId+"','1',this.value,'"+displayYear+"')\">";
					for (i=0;i<month.length;i++) {
						buildCalendarHTML+="<option value=\""+leadingZero(i)+"\">"+month[i]/*.substr(0,3)*/+"</option>";
					}
buildCalendarHTML+="</select>";
buildCalendarHTML+="<select name=\"calendarSelectionYear\" class=\"calendarSelectionYear\" id=\"calendarSelectionYear"+returnToId+"\" onchange=\"javascript:buildCalendar('"+returnToId+"','"+buildInId+"','1','"+displayMonth+"',this.value)\">";
					for (i=(displayYear-yearDisplayLimit);i<(displayYear+yearDisplayLimit);i++) {
						buildCalendarHTML+="<option value=\""+leadingZero(i)+"\">"+i+"</option>";
					}
buildCalendarHTML+="</select>";
buildCalendarHTML+="<br>";
buildCalendarHTML+="<a href=\"javascript:buildCalendar('"+returnToId+"','"+buildInId+"','1','"+(displayMonth-1)+"','"+displayYear+"');\" class=\"calendarSelectionPrev\">&laquo; Prev</a>";
buildCalendarHTML+="<a href=\"javascript:buildCalendar('"+returnToId+"','"+buildInId+"','1','"+(displayMonth+1)+"','"+displayYear+"');\" class=\"calendarSelectionNext\">Next&raquo;</a>";
buildCalendarHTML+="</th>";
buildCalendarHTML+="</tr>";
buildCalendarHTML+="<tr>";
buildCalendarHTML+="<th>S</th>";	
buildCalendarHTML+="<th>M</th>";
buildCalendarHTML+="<th>T</th>";
buildCalendarHTML+="<th>W</th>";
buildCalendarHTML+="<th>T</th>";
buildCalendarHTML+="<th>F</th>";
buildCalendarHTML+="<th>S</th>";	
buildCalendarHTML+="</tr>";
buildCalendarHTML+="<tr>";

//gridSize is the number of cells required for the month - it is divisible by 7.
gridSize = (Math.ceil(((displayMonthLength+displayStartDate)/7))*7);	

//show the days
for (i=1;i<=gridSize;i++) {
showDate = displayStartDate;
	// show the dates during the date range which starts on displayStartDate
	if (i>showDate&&i<=(displayMonthLength+displayStartDate)) {
		//bit of trickery to get the starting date lined up below
		buildCalendarHTML+="<th";
		// is it today?
			if (currentDate.getDate()==(i-displayStartDate) && currentDate.getMonth()==displayMonth && currentDate.getFullYear()==displayYear) {
				buildCalendarHTML+=" class=\"currentDate\"";
			}
		buildCalendarHTML+="><a href=\"javascript:setDateTo('"+returnToId+"','"+leadingZero((i-displayStartDate))+"/"+leadingZero(displayMonth+1)+"/"+displayYear+"','"+buildInId+"');\">"+(i-displayStartDate)+"</a></th>";
	} else {
	// otherwise show a null day
		buildCalendarHTML+="<th>-</th>";
	}
	// create a new row if i goes into 7 evenly, and is not the last part of the for loop
	if (i%7==0 && i!=gridSize) {
		buildCalendarHTML+="</tr>";
		buildCalendarHTML+="<tr>";
	}	
}


buildCalendarHTML+="</tr>";
buildCalendarHTML+="<tr>";
buildCalendarHTML+="<td class=\"footer\" colspan=\"7\"><a href=\"javascript:clearCalendar('"+returnToId+"','"+buildInId+"');\">Close</a></td>";
buildCalendarHTML+="</tr>";
buildCalendarHTML+="</table>";
//buildCalendarHTML="<table><tr><th>moo</th></tr></table>";
document.getElementById(buildInId).style.display = "block";

document.getElementById(buildInId).innerHTML = buildCalendarHTML;
calendarSelectionMonth = "calendarSelectionMonth"+returnToId;
calendarSelectionYear = "calendarSelectionYear"+returnToId;
document.getElementById(calendarSelectionMonth).value = leadingZero(displayMonth);
document.getElementById(calendarSelectionYear).value = displayYear;		
}

function setDateTo(formObj,date,buildInId) {
	document.getElementById(formObj).value = date;
	clearCalendar(formObj, buildInId);
}

function clearCalendar(formObj, buildInId) {
	document.getElementById(formObj).className = "whiteField";
	document.getElementById(buildInId).innerHTML = "";
}