//Functions to help with date drop boxes
//
//Figures out if the given year is a leap year
function isLeap(year) {

	if (Math.floor(year/2000) == year/2000)
		return true;
	else if (Math.floor(year/100) == year/100)
		return false;
	else if (Math.floor(year/4) == year/4)
		return true;
	else
		return false;
}

//The array that keeps track of all the bits and pieces
var dates = new Array();

//Adds a new date the array of dates we are taking care of.
function addDate(name, cDay, cMonth, cYear, hasBlank, showDay) {
	dates[name] = new Array();
	var thisDate = dates[name];
	thisDate.cDay = cDay;
	thisDate.cMonth = cMonth;
	thisDate.cYear = cYear;
	thisDate.currMon = 1 + hasBlank;		//if Jan then 31 days is right
	thisDate.hasBlank = hasBlank;
	thisDate.showDay = showDay;
	if (cYear.options) 
		//It's a select box
		thisDate.currYea = cYear.options[cYear.selectedIndex].text;
	else 
		thisDate.currYea = cYear.value;
	if (cDay) {
		thisDate.currDay = cDay.selectedIndex;
		thisDate.currDays = 31;
		updateDays(name);
	}
}

//updates the number of days in the cDay box according
//to the contents of the other two.
function updateDays(name) {
	var thisDate = dates[name];
	if (thisDate.currDay || thisDate.currDay == 0) {
//		alert('selectedIndex of thisDate.cMonth is: '+thisDate.cMonth.selectedIndex);
		var month = thisDate.cMonth.selectedIndex - thisDate.hasBlank;
//		alert(thisDate.currMon + ":" + month);
		//Are we looking at only a change of year, and it isn't Feb
		if (!thisDate.showDay && (thisDate.currMon == month) && (thisDate.currMon != 1))
			return;

		thisDate.currMon = month;

		//Either Feb or a change of month
		var nDays=31;
		//Get the year
		var year;
		if (thisDate.cYear.options) 
			year = thisDate.cYear.options[thisDate.cYear.selectedIndex].text;
		else
			year = thisDate.cYear.value;
//		alert('month is: '+month+', year is: '+year+', has blank is: '+thisDate.hasBlank);
		switch(month) {
			case 1: if (!parseInt(year))
					nDays = 29
				else if (isLeap(year))
					nDays = 29;
				else
					nDays = 28;
				break;
			case 3:
			case 5:
			case 8:
			case 10:
				nDays = 30;
				break;
		}
		//Has it changed
		if (!thisDate.showDay && thisDate.currDays == nDays)
			return;
		else {
			var oldSelected = thisDate.cDay.selectedIndex;
			if (thisDate.showDay) {
				//Need to redraw the whole box
				thisDate.cDay.options.length = 0;
				if (thisDate.hasBlank)
					thisDate.cDay.options[0] = new Option("Day", "-");
				for (var i=0; i<nDays; i++) {
					var dd = i+1;
					var d = new Date(year, month, dd);
					var dayIndex = d.getDay();
					var dayString = (thisDate.showDay < 3) ? daysAbbr[dayIndex] : days[dayIndex];
					if (thisDate.showDay == 1 || thisDate.showDay == 3)
						dayString += ", " + dd;
					else
						dayString = dd + ", " + dayString;
					thisDate.cDay.options[i+thisDate.hasBlank] = new Option((month > -1 && (dayIndex || dayIndex == 0)) ? dayString : dd, dd);
				}
			} else if (nDays < thisDate.currDays) 
				thisDate.cDay.length=nDays + thisDate.hasBlank;
			else {
				for (var i=thisDate.currDays; i<nDays; i++) 
					thisDate.cDay.options[i+thisDate.hasBlank] = new Option(i+1)
			}
			if (oldSelected > thisDate.cDay.options.length-1)
				thisDate.cDay.selectedIndex = thisDate.cDay.options.length-1;
			else
				thisDate.cDay.selectedIndex = oldSelected;
		}
		thisDate.currDays = nDays;
	}
}

function checkNum(e) {
        var charCode = (navigator.appName == "Netscape") ? e.which : e.keyCode;
        if (charCode > 31 && (charCode < 48 || charCode > 57)) {
                return false;
        }
        return true;
}

//Days of the week
var daysAbbr = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
var days = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");



