function setAction(theForm,thePage) {
/*
 * This function sets a given form's action to a given page
 *
 * theForm		the form to be submitted
 * thePage		the page to submit the form to
 *
 * Francois Suter, Cobweb, October 2002
 */
	theForm.action = thePage;
}

function setFocus() {
/*
 * This function looks for the form named "postForm" and sets the focus on the first text field (or similar) that it finds
 *
 * Francois Suter, Cobweb, February 2002
 */
	if (document.inputForm) {
		with (document.inputForm) {
			for (i = 0; i < elements.length; i++) {
				if (elements[i].type == "text" || elements[i].type == "password" || elements[i].type == "textarea") {
					elements[i].focus();
					elements[i].select();
					return;
				}
			}
		}
	}
}

function daysInMonth(aMonth,aYear) {
/*
 * This function returns how many days there are in a given month of a given year
 * (obviously this is mostly about February and leap years)
 *
 * aMonth	month number (from 1 to 12)
 * aYear	4-digit year number
 *
 * Francois Suter, Cobweb, April 2002
 */
	if (aMonth < 1 || aMonth > 12) aMonth = 1;
	if (aMonth == 2) {
		leap1 = aYear % 4;
		leap2 = aYear % 100;
		leap3 = aYear % 400;
		if (leap3 == 0) {
			numDays = 29;
		}
		else if (leap2 == 0) {
			numDays = 28;
		}
		else if (leap1 == 0) {
			numDays = 29;
		}
		else {
			numDays = 28;
		}
	}
	else if (aMonth == 4 || aMonth == 6 || aMonth == 9 || aMonth == 11) {
		numDays = 30;
	}
	else {
		numDays = 31;
	}
	return numDays;
}

function changeDays(theMonthField,theYearField,theDayField) {
/*
 * This function is used to change the number of days in a days of month drop-down list as a function of the month
 * and the year (for leap years).
 *
 * theMonthField	the form field containing the relevant month value
 * theYearField		the form field containing the relevant year value
 * theDayField		the form field to be modified
 *
 * Francois Suter, Cobweb, April 2002
 */
	theMonth = theMonthField.options[theMonthField.selectedIndex].value;
/*
 * If no year is defined, set theYear to zero. Of course, this means it will be considered a leap year by the daysInMonth() function
 */
	if (theYearField == null) {
		theYear = 0;
	}
	else {
		theYear = theYearField.options[theYearField.selectedIndex].value;
	}
	numDays = daysInMonth(theMonth,theYear);
/*
 * Remember currently selected item (or max days if new number of days is smaller that current index)
 * Also remember if the menu had a first empty element, to put that element back
 */
	theDay = theDayField.options[theDayField.selectedIndex].value;
	if (theDay > numDays) theDay = numDays;
	if (theDayField.options[0].value == 0) {
		hasEmpty = true;
	}
	else {
		hasEmpty = false;
	}
	theDayField.options.length = 0;
/*
 * Set new menu options with first empty element or not
 * Set selection when day matches
 */
	counter = 0;
	if (hasEmpty) {
		if (theDay == 0) {
			theDayField.options[0] = new Option("--", 0, true, true);
		}
		else {
			theDayField.options[0] = new Option("--", 0);
		}
		counter++;
	}
	for (i = 0; i < numDays; i++) {
		dayNumber = i + 1;
		if (theDay == dayNumber) {
			theDayField.options[counter] = new Option(dayNumber, dayNumber, true, true);
		}
		else {
			theDayField.options[counter] = new Option(dayNumber, dayNumber);
		}
		counter++;
	}
}

function changeDaysWithLimits(theMonthField,theYearField,theDayField,startLimit,endLimit) {
/*
 * This function is used to change the number of days in a days of month drop-down list as a function of the month
 * and the year (for leap years). It also takes into account a date range limit
 *
 * theMonthField	the form field containing the relevant month value
 * theYearField		the form field containing the relevant year value
 * theDayField		the form field to be modified
 * startLimit		start of date range as a Date object (null if no limit)
 * endLimit			end of date range as a Date object (null if no limit)
 *
 * Francois Suter, Cobweb, September 2003
 */
	theMonth = theMonthField.options[theMonthField.selectedIndex].value;
	theYear = theYearField.options[theYearField.selectedIndex].value;
/*
 * First initialize the limits if they are null
 * Lower limit is current date if year is current year and Jan 1 if in the future
 * Upper limit is Dec 31 of the year
 */
	currentDate = new Date();
	currentYear = currentDate.getFullYear();
	if (startLimit == null) {
		if (theYear == currentYear) {
			startLimit = currentDate;
		}
		else {
			startLimit = new Date(theYear,0,1);
		}
	}
	if (endLimit == null) endLimit = new Date(theYear,11,31);
/*
 * Calculate minimum and maximum days for given month and year taking limits into account
 * 1 is added to the limit dates month numbers because menus expect months from 1 to 12 and not from 0 to 11
 */
	if (theMonth == startLimit.getMonth() + 1 && theYear == startLimit.getFullYear()) {
		minDay = startLimit.getDate();
	}
	else {
		minDay = 1;
	}
	if (theMonth == endLimit.getMonth() + 1 && theYear == endLimit.getFullYear()) {
		maxDay = endLimit.getDate();
	}
	else {
		maxDay = daysInMonth(theMonth,theYear);
	}
	numDays = maxDay - minDay + 1;
/*
 * Remember currently selected day. Check day against possible range of days calculated above
 * and adjust as necessary (if theDay is 0, this must be preserved)
 */
	theDay = theDayField.options[theDayField.selectedIndex].value;
	if (theDay > 0 && theDay < minDay) theDay = minDay;
	if (theDay > maxDay) theDay = maxDay;
/*
 * Set flag if list has empty first item so that it is restored
 */
	if (theDayField.options[0].value == 0) {
		hasEmpty = true;
	}
	else {
		hasEmpty = false;
	}
/*
 * Set new menu options with first empty element or not
 * Set selection when day matches
 */
	theDayField.options.length = 0;
	counter = 0;
	if (hasEmpty) {
		if (theDay == 0) {
			theDayField.options[0] = new Option("--", 0, true, true);
		}
		else {
			theDayField.options[0] = new Option("--", 0);
		}
		counter++;
	}
	else {
		for (i = 0; i < numDays; i++) {
			dayNumber = minDay + i;
			if (theDay == dayNumber) {
				theDayField.options[counter] = new Option(dayNumber, dayNumber, true, true);
			}
			else {
				theDayField.options[counter] = new Option(dayNumber, dayNumber);
			}
			counter++;
		}
	}
}

function changeMonths(theMonthField,theYearField,startLimit,endLimit) {
/*
 * This function is used to change the list of months in a drop down menu depending on the year when there
 * are date range limits imposed
 *
 * theMonthField	the form field containing the relevant month value
 * theYearField		the form field containing the relevant year value
 * startLimit		start of date range as a Date object
 * endLimit			end of date range as a Date object
 *
 * NOTE: this function expects a global array named allMonthNames that would contain the names of all the months
 * If this array is missing the function will use the month numbers instead
 *
 * Francois Suter, Cobweb, September 2003
 */
	theMonth = theMonthField.options[theMonthField.selectedIndex].value; // Remember currently selected month
	theYear = theYearField.options[theYearField.selectedIndex].value;
/*
 * First initialize the limits if they are null
 * Lower limit is current date if year is current year and Jan 1 if in the future
 * Upper limit is Dec 31 of the year
 */
	currentDate = new Date();
	currentYear = currentDate.getFullYear();
	if (startLimit == null) {
		if (theYear == currentYear) {
			startLimit = currentDate;
		}
		else {
			startLimit = new Date(theYear,0,1);
		}
	}
	if (endLimit == null) endLimit = new Date(theYear,11,31);
/*
 * Calculate minimum and maximum months for given year taking limits into account
 * Add 1 to limit months because JavaScript months range from 0 to 11 and we want 1 to 12
 */
	if (theYear == startLimit.getFullYear()) {
		minMonth = startLimit.getMonth() + 1;
	}
	else {
		minMonth = 1;
	}
	if (theYear == endLimit.getFullYear()) {
		maxMonth = endLimit.getMonth() + 1;
	}
	else {
		maxMonth = 12;
	}
	numMonths = maxMonth - minMonth + 1;
/*
 * Remember if the menu had a first empty element, to put that element back
 */
	if (theMonthField.options[0].value == 0) {
		hasEmpty = true;
	}
	else {
		hasEmpty = false;
	}
/*
 * Check if currently selected month is out of range. If yes, set to start or end of range
 * We must be cautious with lower end of range, because selected item may be the first empty element
 * if there is one
 */
	if (theMonth > 0 && theMonth < minMonth) theMonth = minMonth;
	if (theMonth > maxMonth) theMonth = maxMonth;
/*
 * Set new menu options with first empty element or not
 * If month matches selected month set select flag to true
 */
	theMonthField.options.length = 0;
	counter = 0;
	if (hasEmpty) {
		if (theMonth == 0) {
			theMonthField.options[counter] = new Option("--", 0, true, true);
		}
		else {
			theMonthField.options[counter] = new Option("--", 0);
		}
		counter++;
	}
	for (i = 0; i < numMonths; i++) {
		monthNumber = minMonth + i;
		if (allMonthNames != null && allMonthNames[monthNumber] != null) {
			monthName = allMonthNames[monthNumber];
		}
		else {
			monthName = monthNumber;
		}
		if (monthNumber == theMonth) {
			theMonthField.options[counter] = new Option(monthName, monthNumber, true, true);
		}
		else {
			theMonthField.options[counter] = new Option(monthName, monthNumber);
		}
		counter++;
	}
}

function changeDaysWithNames(theForm,monthFieldName,yearFieldName,dayFieldName) {
/*
 * This function is similar to changeDays(), but is called with the field names as argument rather
 * than the fields themselves. It match the fields with the names and calls changeDays with the
 * found fields.
 *
 * theForm			the affected form
 * monthFieldName	name of the form field containing the relevant month value
 * yearFieldName	name of the form field containing the relevant year value
 * dayFieldName		name of the form field to be modified
 *
 * Francois Suter, Cobweb, November 2002
 */
	theMonthField = findFieldByName(theForm,monthFieldName);
	theYearField = findFieldByName(theForm,yearFieldName);
	theDayField = findFieldByName(theForm,dayFieldName);
	changeDays(theMonthField,theYearField,theDayField);
}

function findFieldByName(theForm,theName) {
/*
 * This function finds a field within a form according to its name and returns the field's object
 *
 * theForm	the form to look into
 * theName	the name of the field to find
 *
 * Francois Suter, Cobweb, November 2002
 */
	for (i = 0; i < theForm.elements.length; i++) {
		if (theForm.elements[i].name == theName) return theForm.elements[i];
	}
	return null;
}

function popupWindow(theUrl) {
/*
 * This function pops up a small window
 *
 * Francois Suter, Cobweb, January 2002
 */
	window.open(theUrl,'ALSA','width=650,height=600,menubar=no,status=no,toolbar=no,scrollbars=yes,resizable=yes');
}
