/*
 * revalidateDate
 * --------------
 * Called when day, month, or year of a singleton date has changed.  Makes sure
 * that the date is still valid and that the day selector contains the correct
 * number of days for the current month.
 */
function revalidateDate(formName, elemName) {
	var formDay = document[formName][elemName + "_day"];
	var formMonth = document[formName][elemName + "_month"];
	var formYear = document[formName][elemName + "_year"];

	var endDayOfMonth = numDaysIn(formMonth.options[formMonth.selectedIndex].value, 
	                              formYear.options[formYear.selectedIndex].value);
	if (formDay.options[formDay.selectedIndex].value > endDayOfMonth) {
		formDay.selectedIndex = endDayOfMonth - 1;
	}
	formDay.options.length = endDayOfMonth;
	for (i = 27; i < endDayOfMonth; i++) {
		formDay.options[i].text = i + 1;
		formDay.options[i].value = i + 1;
	}
}

/*
 * revalidateDatePair
 * ------------------
 * Called when day, month, or year of one of the dates of a pair has changed.
 * Makes sure that the date is still valid and that the day selector contains
 * the correct number of days for the current month.  Then, checks to make
 * sure the start date is less than the end date, and if not, adjusts the
 * proper date to compensate.
 */
function revalidateDatePair(formName, startDateElem, endDateElem, startDateChanged) {
	if (startDateChanged) revalidateDate(formName, startDateElem);
	else revalidateDate(formName, endDateElem);
	
	var startFormDay = document[formName][startDateElem + "_day"];
	var startFormMonth = document[formName][startDateElem + "_month"];
	var startFormYear = document[formName][startDateElem + "_year"];
	var endFormDay = document[formName][endDateElem + "_day"];
	var endFormMonth = document[formName][endDateElem + "_month"];
	var endFormYear = document[formName][endDateElem + "_year"];

	var startDate = new Date(startFormYear.options[startFormYear.selectedIndex].value,
	                         startFormMonth.options[startFormMonth.selectedIndex].value,
	                         startFormDay.options[startFormDay.selectedIndex].value);
	var endDate = new Date(endFormYear.options[endFormYear.selectedIndex].value,
	                       endFormMonth.options[endFormMonth.selectedIndex].value,
	                       endFormDay.options[endFormDay.selectedIndex].value);
	
	if (startDate.getTime() > endDate.getTime()) {
		if (startDateChanged) {
			endFormDay.options.length = startFormDay.options.length;
			for (i = 27; i < startFormDay.options.length; i++) {
				endFormDay.options[i].text = i + 1;
				endFormDay.options[i].value = i + 1;
			}
			endFormDay.selectedIndex = startFormDay.selectedIndex;
			endFormMonth.selectedIndex = startFormMonth.selectedIndex;
			endFormYear.selectedIndex = startFormYear.selectedIndex;
		} else {
			startFormDay.options.length = endFormDay.options.length;
			for (i = 27; i < endFormDay.options.length; i++) {
				startFormDay.options[i].text = i + 1;
				startFormDay.options[i].value = i + 1;
			}
			startFormDay.selectedIndex = endFormDay.selectedIndex;
			startFormMonth.selectedIndex = endFormMonth.selectedIndex;
			startFormYear.selectedIndex = endFormYear.selectedIndex;
		}
	}
}

/*
 * inPresentOrPast
 * ---------------
 * Boolean function that makes sure the date in question is not in the future
 * (useful for such things as ensuring that results are not entered for a workout that 
 * has not yet taken place).
 */

function inPresentOrPast(day, month, year, currentDay, currentMonth, currentYear) {
	if (year > currentYear) return false;
	if (year < currentYear) return true;
	if (month > currentMonth) return false;
	if (month < currentMonth) return true;
	if (day > currentDay) return false;
	return true;
}

/*
 * inPresentOrFuture
 * ---------------
 * Boolean function that makes sure the date in question is not in the past
 * (useful for such things as ensuring that a workout is not scheduled in the
 * past).
 */

function inPresentOrFuture(day, month, year, currentDay, currentMonth, currentYear) {
	if (year < currentYear) return false;
	if (year > currentYear) return true;
	if (month < currentMonth) return false;
	if (month > currentMonth) return true;
	if (day < currentDay) return false;
	return true;
}


/*
 * utilty functions
 * ----------------
 */
function numDaysIn(month, year) {
	if (month == 3 || month == 5 || month == 8 || month == 10) return 30;
	else if ((month == 1) && leapYear(year)) return 29;
	else if (month == 1) return 28;
	else return 31;
}

function leapYear(year) {
	if (((year % 4 == 0) && year % 100 != 0) || year % 400 == 0)
		return true;
	else
		return false;
}
