// JavaScript Document
// Last modified 20091113 ge 
//  function library for form buy.html
//
// 1. Find conditionally required fields and store in an array
// 2. Pass the array to a check of conditional fields
// 3. If form submission passes that check, pass to a check of required fields
//
// Currently works only for a single set of fields where we require
// at least one to be filled in.
//
// functions defined in formcheck.js:
//
//  toggleRequired(formname,fieldname) 
//  makeRequired
//  unrequire
//  clearopt
//  clearopts
//  clearlastopt
//  setopt
//  checkRequiredFields
//  checkEmail

function findConditionalFields(formname) {
	// first reset any highlighted errors
	document.getElementById('errordiv').className = "resetplain";
	document.getElementById('conditionalmsg').className = "resetplain";
	// for each field in form, check if it is required
	// and add its name to an array
	var i;
	var curfield;
	var curfieldname;
	var f = document.forms[formname];
	var conditionals = new Array();
	// loop through form looking for text fields of class "conditionalrequired"
	for (i=0;i<f.elements.length;i++) {
		curfield = f.elements[i];
		curfieldname = curfield.name;
		if (curfield.className == "conditionalrequired") {
			conditionals.push(curfieldname);
		} // end if
	} // end for
	return checkConditionalFields(formname, conditionals);
}

function checkConditionalFields(formname, fieldarray) {
	// for each field the fields array, check if it has a value
	// if none has a value, return an error and set the focus to
	// the first field listed in the array
	var i;
	var j = 0;
	var k = fieldarray.length;
	var curfieldname;
	var focusfield;
	var outstring = new String();
	outstring = "You must include one of the fields: \n";
	var f = document.forms[formname];
	// loop through conditionally required fields looking for blanks
	for (i=0;i<k;i++) {
		curfieldname = fieldarray[i];
		if (document.forms[formname][curfieldname].value == "") {
			// increment count of blank fields
			j++;
			outstring += curfieldname + "\n";
		} // end if
	} // end for
	if (j == k) {
		// all conditionally required fields are blank
		// switch the fixed error message to displayable
		// place the focus into the first conditionally required field
		document.getElementById('conditionalmsg').className = "visibility";
		focusfield = fieldarray[0];
		document.forms[formname][focusfield].focus();
		return false;
	} // end if
	else {
		// at least one conditionally required field is nonblank
		document.getElementById('conditionalmsg').className = "resetplain";
		return checkRequiredFieldsNoAlert(formname);
	} // end else
} // end function

function checkRequiredFieldsNoAlert(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;
		// simple check on whether library card number is numeric
		if (curfieldname == "Card_Number" && (!(is_numeric(curfield.value)))) {
			outstring = 'Please check your library card number - it does not appear to be a valid card number';
			document.getElementById('errordiv').innerHTML = outstring.valueOf();
			document.getElementById('errordiv').className = "visibility";
			document.forms[formname][curfieldname].focus();
			return false;
			}
		// pass email address to email checker if it's not blank
		if (curfieldname == "Patron_Email" && curfield.value != "") {
			if (!checkEmail(curfield.value)) {
				outstring = 'Please check your email address - it does not appear to be a valid email address';
				document.getElementById('errordiv').innerHTML = outstring.valueOf();
				document.getElementById('errordiv').className = "visibility";
				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
			// 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
			document.getElementById('errordiv').innerHTML = outstring.valueOf();
			document.getElementById('errordiv').className = "visibility";
			// 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
				//
				outstring = outstring.replace(/\_/g, " ");
				// display the message
				document.getElementById('errordiv').innerHTML = outstring.valueOf();
				document.getElementById('errordiv').className = "visibility";
			 	return false;
			 } // end if
		 } // end if
	} // end for
	// otherwise, everything looks fine; submit the form
	return true;
} // end function

