//<script type="text/javascript">//fool some IDEs into formatting nicer!

/*Data used by several functions, number of days in each month
of Gregorian calendar*/

var monthDays = Array(31,28,31,30,31,30,31,31,30,31,30,31)
var leapMonthDays = Array(31,29,31,30,31,30,31,31,30,31,30,31)

/*A day has been set. If necessary we increase the month until it is
a month for which that day is a possible valid date. Since December
has 31 days (making all possible day values allowable) we can depend
on this hitting a valid month.*/
function makeDayValid(daySel, monthSel, yearSel){
	var day = daySel.selectedIndex + 1;
	var month = monthSel.selectedIndex + 1;
	var year = yearSel[yearSel.selectedIndex].value;
	//Leap year every 4 years.
	//Except every 100 years, which isn't.
	//Except every 400 years which is!.
	var isLeap = (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0));
	var dayList = (isLeap) ? leapMonthDays : monthDays;
	
	var newMonth = month;
	
	while(dayList[newMonth - 1] < day)
		newMonth++;
		
	if(newMonth != month)
		monthSel.selectedIndex = newMonth - 1;
}

/*A month has been set. If the day is greater than the highest allowable
we reduce it to the highest allowable*/
function makeMonthValid(daySel, monthSel, yearSel){
	var day = daySel.selectedIndex + 1;
	var month = monthSel.selectedIndex + 1;
	var year = yearSel[yearSel.selectedIndex].value;
	//Leap year every 4 years.
	//Except every 100 years, which isn't.
	//Except every 400 years which is!.
	var isLeap = (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0));
	var dayList = (isLeap) ? leapMonthDays : monthDays;
	
	var highestDay = dayList[month - 1];
	
	if (highestDay < day)
		daySel.selectedIndex = highestDay - 1;
}

/*A year has been set. If the date is -02-29 and the year is not a leap-year
make the day 28*/
function makeYearValid(daySel, monthSel, yearSel){
	var year = yearSel[yearSel.selectedIndex].value;
	//Leap year every 4 years.
	//Except every 100 years, which isn't.
	//Except every 400 years which is!.
	var isLeap = (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0));
	
	if (!isLeap && monthSel.selectedIndex == 1 && daySel.selectedIndex == 28)
		daySel.selectedIndex = 27;
}

function W3CDTF(day, month, year){
	return year + '-' + ((month > 9) ? month : '0' + month) + '-' + ((day > 9) ? day : '0' + day)
}

function verifySearchForm(form){
	with(form){
		if	(!words.value.length && !phrase.value.length){
			alert("You must enter something to search for.");
			words.focus();
			return false;
		}
		else if (fromYear.selectedIndex > toYear.selectedIndex || (fromYear.selectedIndex == toYear.selectedIndex && fromMonth.selectedIndex > toMonth.selectedIndex) || (fromYear.selectedIndex == toYear.selectedIndex && fromMonth.selectedIndex == toMonth.selectedIndex && fromDay.selectedIndex > toDay.selectedIndex)){
			alert('The "to" date must be later or equal to the "from" date.');
			fromDay.focus();
			return false;
		}
		else
			return true;
	}
}
function changeSearchCat(form, searchCat){
	var sNewURL = "?showForm=true";
	sNewURL += "&secat=" + searchCat;

	with(form){
		sNewURL += "&words=" + words.value;
		sNewURL += "&searchType=" + ((searchType[0].checked)? "any" : "all");
		sNewURL += "&phrase=" + phrase.value;
		sNewURL += "&exclude=" + exclude.value;
		sNewURL += "&resource=" + ((resource[0].checked)? "docs" : "files");
		sNewURL += "&order=" + order[order.selectedIndex].value;
		sNewURL += "&from=" + W3CDTF(fromDay.selectedIndex+1, fromMonth.selectedIndex+1, fromYear[fromYear.selectedIndex].value);
		sNewURL += "&to=" + W3CDTF(toDay.selectedIndex+1, toMonth.selectedIndex+1, toYear[toYear.selectedIndex].value);
		sNewURL += "&resultCount=" +	resultCount[resultCount.selectedIndex].value;

		//skip past searchCat options not selected.
		for (var idx=0; idx<searchCat.length && !searchCat[idx].checked; idx++);

		sNewURL += "&searchCat="	+searchCat[idx].value;
	}
	
	sNewURL +="#searchCats";

	location.href = sNewURL;

	return false;
}