<!-- //
//
//	JavaScript functions to support date selection and handling
//	© 2002-2009 FDF Infonet
//
function IsLeapYear(yearValue){
	//Returns true/false - the specified year is a leap year
	var LeapYear = false;
	if ( (yearValue%100 != 0 || yearValue%400 == 0) && yearValue%4 == 0 ) {
        LeapYear=true;
    }
	return LeapYear;
}

function DaysInMonth(monthValue, yearValue){
	//Returns the number of days in the month for the specified month and year combination
	//monthValue is a number 1-12; yearValue is a 4 digit number
	//
	var MaxDays = 31;

	if (monthValue==2) {
	    if (IsLeapYear(yearValue)) {
	        MaxDays=29;
	    } else {
	        MaxDays=28;
	    }
	}
	if (monthValue==4 || monthValue==6 || monthValue==9 || monthValue==11) {
	        MaxDays=30;
	}
	return MaxDays;
}

function MonthDays(StartYear, StartMonth, StartDays) {
	var day     = StartDays.options[StartDays.selectedIndex].value;
	var MaxDays = DaysInMonth(StartMonth.options[StartMonth.selectedIndex].value, StartYear.options[StartYear.options.selectedIndex].value);

	StartDays.length = 28
	for (var i = 29; i <= MaxDays; i++) {
		StartDays.length += 1
		var newOption = new Option(i); StartDays.options[i-1] = newOption; StartDays.options[i-1].value = i;
	}
	if (day > MaxDays) {day = MaxDays}
	if (day > 0){StartDays.selectedIndex = day - 1}
	DayOfWeek(StartYear, StartMonth, StartDays);
}

//DPA 21/11/2002 Added procedures

function IsDate (StartDay, StartMonth, StartYear) {
	var status = false;
//Can't use	try/catch/finally since not all browsers have support (eg. IE4)
//so check for numeric arguments then check the date components come back the way they went in

	if (isNaN(StartDay) || isNaN(StartMonth) || isNaN(StartYear)) {
		status = false;
	} else {
		var TestDate = new Date(Number(StartYear), (Number(StartMonth) - 1), Number(StartDay));
		
		if (TestDate.getDate() != Number(StartDay) || TestDate.getMonth() != (Number(StartMonth) - 1) || ( (TestDate.getFullYear() != Number(StartYear)) && (TestDate.getYear() != Number(StartYear)) )) {
			status = false;
		} else {
			status = true;
		}
	}
	return status;
}
function SetDayName(DayName) {
//Function to set a display field to the name of the day selected
//no-op by default
//Supercede this definition if you have a display field to use
//single line function of the form
//
//	function SetDayName(DayName){dayfield.value = DayName;}
//
//where 'dayfield' is the field in which you want to display the name of the selected day
//
	return true;
}

function DayOfWeek(StartYear, StartMonth, StartDay) {
	var Days   = new Array ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
	var sDay   = StartDay.options[StartDay.selectedIndex].value;
	var sMonth = StartMonth.options[StartMonth.selectedIndex].value;
	var sYear  = StartYear.options[StartYear.selectedIndex].value;

	if (IsDate(sDay, sMonth, sYear)) {
		//Make the date
		var TheDate = new Date(Number(sYear), (Number(sMonth) - 1), Number(sDay));
		//Extract the day of the week
		var TheDay = TheDate.getDay();
		SetDayName(Days[TheDay]);
	}
}

function CheckDateRange(StartDate, EndDate, MaxDays) {
var MillisecondsInDay = 1000*60*60*24
var Status;
//	alert (MaxDays);
//	alert (StartDate.toString());
//	alert (EndDate.toString());

	if (EndDate.getTime() >= StartDate.getTime()) {
		//End date after start date - check number of dates
		if ((MaxDays!= 0) && (((EndDate.getTime() - StartDate.getTime())/MillisecondsInDay) + 1) > MaxDays) {
			//Date range exceeds max. number of days
			Status = false;
			alert ('Invalid date range: Range exceeds maximum of ' + MaxDays.toString() + ' days');
		} else {
			Status = true;
		}
	} else {
		//End date is before start date
		Status = false;
		alert ('Invalid date range: Start Date is after End Date');
	}
	return Status;
}

function GetDateSelect(Prefix){
//DPA 04/04/2006 Added function
	//Return currently selected date for the DateSelect with specified Prefix

	var DayValue;
	var MonthValue;
	var YearValue;
	var TheDate;
	var DayElement;
	var MonthElement;
	var YearElement;
	var i;

	TheDate = "";
	//Cycle through all matching select boxes
	DayElement   = document.getElementsByName(Prefix + "Day");
	MonthElement = document.getElementsByName(Prefix + "Month");
	YearElement  = document.getElementsByName(Prefix + "Year");
	//
	for (i=0; i<DayElement.length; i++){
		if ((DayElement[i] != undefined) && (MonthElement[i] != undefined) && (YearElement[i] != undefined)){
			//Get the select list values for year and month
			YearValue = GetSelectedOption(YearElement[i]);
			MonthValue = GetSelectedOption(MonthElement[i]);
			DayValue = GetSelectedOption(DayElement[i]);
			TheDate = DayValue + "/" + MonthValue + "/" + YearValue;
		}
	}
	if (i==0){
		//Don't have individual elements; try for a single date element or elements
		DayElement = document.getElementsByName(Prefix + "Date");
		for (i=0; i<DayElement.length; i++){
			switch (DayElement[i].type){
			case "select-one":
				//Scan options for the required type id value
				TheDate = GetSelectedOption(DayElement[i]);
				break;
			default:
				//Get the value
				TheDate = DayElement[i].value;
			}
		}
	}
	return TheDate
}

function SetDateSelect(Prefix, DefaultDate){
	//Set initial date for the DateSelect with specified Prefix
	//DefaultDate is a string of the form dd/mm/yyyy
	var DayValue;
	var MonthValue;
	var YearValue;
	var TheDate;
	var DayElement;
	var MonthElement;
	var YearElement;
	var i;
	
	//Reformat and split up the date into components
	TheDate      = Format_Date(DefaultDate);
	DayValue     = TheDate.substring(0,  2);
	MonthValue   = TheDate.substring(3,  5);
	YearValue    = TheDate.substring(6, 10);
	//Cycle through all matching select boxes
	DayElement   = document.getElementsByName(Prefix + "Day");
	MonthElement = document.getElementsByName(Prefix + "Month");
	YearElement  = document.getElementsByName(Prefix + "Year");
	//
	for (i=0; i<DayElement.length; i++){
		if ((DayElement[i] != undefined) && (MonthElement[i] != undefined) && (YearElement[i] != undefined)){
			//Set the select list values for year and month
			SetSelectedOption(YearElement[i], YearValue);
			SetSelectedOption(MonthElement[i], MonthValue);
			//Ensure correct days in month set up for the specified date
			MonthDays(YearElement[i], MonthElement[i], DayElement[i]);
			//Now set the day of the month
			SetSelectedOption(DayElement[i], DayValue);
		}
	}
//DPA 19/03/2006 Add check for single date element
	if (i==0){
		//Don't have individual elements; try for a single date element or elements
		DayElement = document.getElementsByName(Prefix + "Date");
		for (i=0; i<DayElement.length; i++){
			switch (DayElement[i].type){
			case "select-one":
				//Scan options for the required type id value
				SetSelectedOption(DayElement[i], TheDate);
				break;
			case "radio":
				//Scan for a button with the correct value and select it
				SetRadioButton(DayElement[i], TheDate);
				break;
			default:
				//Set the value
				DayElement[i].value = TheDate;
			}
		}
	}
}
// -->