// Create an array monthLength in which the lengths of the 12 months are stored.
// This is to check if the day the user has entered exists in the month the user has entered.
var monthLength = new Array(31,28,31,30,31,30,31,31,30,31,30,31);

// This function requires a name to be specified. This name is the general part of the names of the select boxes.
// In this case it is 'lower' or 'upper'.
function checkDate(name){

	var x = fnGetFormObject();
	var day = fnGetDay(name);
	var month = fnGetMonth(name);
	var year = fnGetYear(name);

	// If the user hasn't chosen an option in one or more of the select boxes, return false because the date is wrong in any case.
	if (!day || !month || !year)
		return false;
		
	// Now we're sure the user has actually entered a date. First of all we check for leap years.
	// If the year the user entered is divisble by 4, the year is a leap year and the length of February (month 1 in the array!) should become 29.
	if (year/4 == parseInt(year/4))
		monthLength[1] = 29;
		
	// Now check if the day the user entered fits in the month entered. If it doesn't we return false since the date doesn't exist.
	if (day > monthLength[month-1])
		return false;

	// Restore the original 28 to month[1]. This is so that future calls to the script will start with the original value.
	monthLength[1] = 28;
	
	// If we made it to here the date exists so return true immediately.
	return true;
}

// Validate the Search form
function fnSubmit(){

	var frm = document.SearchForm;
	//event.returnValue = false;
	if(frm.strPublishedWhen_past.checked && frm.strPublishedWhen_past_range.selectedIndex == 0){
	//in the past, but no range selected
		alert("Please select a published date range.");
		frm.strPublishedWhen_past_range.focus();
		return false;
	}else if(frm.strPublishedWhen_custom.checked){
		if(!dateSelected("lower") && !dateSelected("upper")){
		//no lower or upper date ranges selected
			alert("Please select published date ranges.");
			frm.lowerDay.focus();
			return false;
		}else if(dateSelected("lower") && !checkDate("lower")){
		//no valid lower date
			alert("Please select a valid 'On or after' date.");
			frm.lowerDay.focus();
			return false;
		}else if(dateSelected("upper") && !checkDate("upper")){
			alert("Please select a valid 'And before' date.");
			frm.upperDay.focus();
			return false;
		}
		if(dateSelected("lower") && checkDate("lower") && dateSelected("upper") && checkDate("upper")){
			if(!validRange()){
				alert("The 'On or after' date must be before the 'And before' date.");
				return false;
			}
		}
	}else{
		return true;
	}
}

// Check to see if a date has been selected.
function dateSelected(name){
	var x = fnGetFormObject();
	var day = fnGetDay(name);
	var month = fnGetMonth(name);
	var year = fnGetYear(name);
	
	return (day || month || year)?true:false;
}

// Assign form object.
function fnGetFormObject(){
	var obj = document.SearchForm.elements;
	return obj;
}

// Assign 'day' value.
function fnGetDay(name){
	var x = fnGetFormObject();
	var day = parseInt(x[name+"Day"].options[x[name+"Day"].selectedIndex].value);
	return day;
}

// Assign 'month' value.
function fnGetMonth(name){
	var x = fnGetFormObject();
	var month = parseInt(x[name+"Month"].options[x[name+"Month"].selectedIndex].value);
	return month;
}

// Assign 'year' value.
function fnGetYear(name){
	var x = fnGetFormObject();
	var year = parseInt(x[name+"Year"].options[x[name+"Year"].selectedIndex].value);
	return year;
}

// Check to see if lower date is before upper date.
function validRange(){
	var x = fnGetFormObject();
	var name;

	name = "lower"
	var day = fnGetDay(name);
	var month = fnGetMonth(name);
	var year = fnGetYear(name);
	
	name = "upper"
	var day2 = fnGetDay(name);
	var month2 = fnGetMonth(name);
	var year2 = fnGetYear(name);

	var date1 = new Date();
	date1.setYear(year);
	date1.setMonth(month-1);
	date1.setDate(day);

	var date2 = new Date();
	date2.setYear(year2);
	date2.setMonth(month2-1);
	date2.setDate(day2);

	return (date1<date2)?true:false;
}

