// JavaScript Document
// Last modified 20081113 ge 
//  function library for forms feedback.html, notify.html

function toggleRequired(formname,fieldname) {
	// toggle "required" class depending on
	// whether it was already set
	if (document.forms[formname][fieldname].className == "required") {
		document.forms[formname][fieldname].className = "unhighlight";
	}
	else {
		document.forms[formname][fieldname].className = "required";
		document.forms[formname][fieldname].focus();
	}
} // end function

function makeRequired(formname,fieldname) {
	// add "required" highlight to a
	// conditionally required field
	document.forms[formname][fieldname].className = "required";
	document.forms[formname][fieldname].focus();
} // end function

function unrequire(formname,fieldname) {
	// remove "required" highlight from a
	// conditionally required field
	document.forms[formname][fieldname].className = "unhighlight";
} // end function

function clearopt(formname,fieldname) {
	// clear a single named option
	document.forms[formname][fieldname].checked = 0;
} // end function

function clearopts(formname,fieldname) {
	// clear all the options except the last one
	var o = document.forms[formname][fieldname].length -1;
	//
	for (var i=0; i<o; i++) {
		document.forms[formname][fieldname][i].checked = 0;
	}
} // end function

function clearlastopt(formname,fieldname) {
	// clear only the last option
	var o = document.forms[formname][fieldname].length -1;
	document.forms[formname][fieldname][o].checked = 0;
} // end function

function setopt(formname,fieldname,fieldvalue) {
	// clear all selections for an option set
	var o = document.forms[formname][fieldname].length;
	for (var i=0; i<o; i++) {
		// select a named option
		//if (document.forms[formname][fieldname][i].value == fieldvalue) {
		//	document.forms[formname][fieldname][i].checked = 1;
		//}
		//else {
		//	document.forms[formname][fieldname][i].checked = 0;
		//}
		document.forms[formname][fieldname][i].checked = document.forms[formname][fieldname][i].value == fieldvalue;
	}
} // end function

function checkRequiredFields(formname) {
	// for each field in form, check if it is required
	// if so, check if it is empty
	// if an empty field is found, display an error message
	// and move the cursor to the empty field
	var i;
	var curfield;
	var curfieldname;
	var outstring = new String();
	var f = document.forms[formname];
	// Loop 1: loop through form looking for required text fields that are blank / invalid
	for (i=0;i<f.elements.length;i++) {
		curfield = f.elements[i];
		curfieldname = curfield.name;
		// pass email address to email checker if it's not blank
		if (curfieldname == "Patron_Email" && curfield.value != "") {
			if (!checkEmail(curfield.value)) {
				alert('Please check your email address - it does not appear to be a valid email address');
				document.forms[formname][curfieldname].className="required";
				document.forms[formname][curfieldname].focus();
				return false;
			}
		} // end if
		// look for simple text and textarea fields whose class is "required" but fields are blank
		if (curfield.className == "required" && curfield.value == "") {
			// create an error message
			outstring = curfieldname+" must be included.\n";
			// translate out any underscore from fieldname in message
			outstring = outstring.replace(/\_/g, " ");
			// display the message
			alert(outstring.valueOf());
			// move to the field that is missing data
			document.forms[formname][curfieldname].focus();
			// don't submit the form
			return false;
		} // end if
	} // end for
	// Loop 2: loop through form looking for radio and checkbox sets where a selection is required but none was made
	for (i=0;i<f.elements.length;i++) {
		checkflag = false;
		curfield = f.elements[i];
		curfieldname = curfield.name;
		curfieldtype = curfield.type;
		// All required radio and checkbox sets must have a class of boxrequired
		// and all selections in the set must share the same name.
		// Differentiate them in HTML by id.
		if (curfield.className == "boxrequired") {
			 // inner loop looking for all other items in the set
			 for (i=0;i<f.elements.length;i++) {
				 if (f.elements[i].name == curfieldname && f.elements[i].checked == true) {
					 // if we find any one item in the set that's checked, flip the flag
					checkflag = true;
				 } // end if
			 } // end for
			 if (checkflag == false) {
				 // if we never flipped the flag, we're missing a required checked box or button
				 // create an error message
				outstring = "Please make a selection for "+curfieldname+".\n";
				// translate out any underscore from fieldname in message
				//
				// n.b. Do not use multiple underscores in field names.  The built-in JavaScript replace function
				// only replaces the first occurrence, and I have not written a looping replaceAll function
				// for this script.
				outstring = outstring.replace(/\_/, " ");
				// display the message
				alert(outstring.valueOf());
			 	return false;
			 } // end if
		 } // end if
	} // end for
	// otherwise, everything looks fine; submit the form
	return true;
} // end function

function checkEmail(email) {
	// check if an email address is obviously malformed
	
	if (email.indexOf('@',0) > 0) {
		if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,9})+$/.test(email)){
			return false;
		} // end if
		else { 
			return true;
		}
	} // end if	
	else {
		return false;
	} // end else

} // end function

