// author: Mike Mertsock, mjm2@alfred.edu
// search form (classnotes/searchForm.cfm) validation script

document.forms.searchForm.keywords.focus();
if (document.forms.searchForm.keywords.value != "")
	document.forms.searchForm.keywords.select();

function checkValues() {
	var theForm = document.forms.searchForm;
	//the keywords field is required only when searching by events
	var keywordsRequired = theForm.srchEvents.checked;
	//did the keywords field fail validation?
	var keywordsInvalid = false;
	
	//trim theForm.keywords: see the file trim.js
	theForm.keywords.value = theForm.keywords.value.trim();
	
	//if the person entered a 2-digit year, convert it to 4-digit; etc.
	if (theForm.srchYear.value.charAt(0) == "'") //remove apostrophes
		theForm.srchYear.value = theForm.srchYear.value.substring(1);
	var yrNum = parseInt(theForm.srchYear.value);
	if (!isNaN(yrNum)) {
		if (yrNum < 100) {//convert 2 digit to 4 digit years
			yrNum = (yrNum < 20 ? yrNum + 2000 : yrNum + 1900);
			theForm.srchYear.value = yrNum;
		}
		if (yrNum >= 100 && yrNum < 1836) //remove completly invalid years
			theForm.srchYear.value = "";
	} else {
		theForm.srchYear.value = "";
		//user will be notified further down if the now-blank field makes submission no good
	}
	
	//check for bad keywords value (too short, etc.)
	if (theForm.keywords.value.length < 2) 
		keywordsInvalid = true;
	//make sure at least 1 checkbox is checked
	if (!(theForm.srchNotes.checked || 
	theForm.srchEvents.checked || 
	theForm.srchCorrs.checked)) {
		alert("Choose at least one category to search.");
		return false;
	}
	
	//check if required text fields aren't invalid
	if (keywordsRequired && keywordsInvalid) {
		alert("When searching Events, enter at least 2 characters in the keywords box.");
		theForm.keywords.focus();
		return false;
	}
	if (keywordsInvalid && theForm.srchYear.value == "") {
		alert("You need to enter keywords (at least 2 characters) and/or a Class Year to seach by.");
		theForm.keywords.focus();
		return false;
	}
	
	return true;
	
}
