/*Configuration instructions:

Set values in calling page:

validateTest : hash of field.name=>regexpString | function (anonymous/literal inline function)
validateConditionally : hash of field.name=>regexpString | function (anonymous/literal inline function)
validateMessage : hash of field.name=>textString

*/

var validateTest=new Array();
var validateConditionally=new Array(); 
var validateMessage=new Array();

/*.
testRegExp=new RegExp("[\\d]");
if (testRegExp.test("3")==true && testRegExp.test("z")==false) {
*/
// added optional second argument 'individualAlerts' which if set false
// will not display an alert and will return a string -- empty if the field
// is valid or else the message.
function validateField(thisField) {
var testing=false;
if (testing) alert('validateField called for '+thisField.name+"\n"+validateTest[thisField.name]);//testing

var individualAlerts=true;
if (arguments.length > 1)
{
	individualAlerts= arguments[1] ? true : false;
}
var valid, conditional;
var thisFieldNamevalue=getValueByFieldName(thisField.name);//considers checkboxes, radios
var validateRegExp = new RegExp;
var alertMessage="'"+thisField.name+"' is not formatted in the expected way.";
	if (validateTest[thisField.name] == null) {
		if (testing) alert('No test defined for '+thisField.name);//testing
		if (individualAlerts) return true;
		else return '';
		}
	if(validateConditionally[thisField.name] != null)
	{
	switch (typeof validateConditionally[thisField.name]) {
		case "string":
			validateRegExp.compile(validateConditionally[thisField.name]);
			conditional = validateRegExp.test(thisFieldNamevalue);
if (testing) alert("validateCondition regular expression tested...\nvalue: "+thisFieldNamevalue+"\nRegExp: "+validateConditionally[thisField.name]+"\nResult: "+conditional);//testing
			break;
		case "function":
			conditional = validateConditionally[thisField.name](thisField);
if (testing) alert("validateCondition function tested...\nvalue: "+thisFieldNamevalue+"\nFunction: "+validateConditionally[thisField.name]+"\nResult: "+conditional);//testing
			break;
		default:
if (testing) alert('validateConditionally found for '+thisField.name+', but of unusable type ('+typeof validateTest[thisField.name]+')');//testing
			conditional = true; //if we don't know how to test, go ahead and validate
		}//switch
	if (conditional == false)
	{
		if (individualAlerts) return true;
		else return '';
	}
	}

	switch (typeof validateTest[thisField.name]) {
		case "string":
//		alert('Trying to compile regular expression: '+validateTest[thisField.name]);
//			validateRegExp.compile(validateTest[thisField.name]);
			validateRegExp = new RegExp(validateTest[thisField.name]);
			valid = validateRegExp.test(thisFieldNamevalue);
if (testing) alert("Regular expression tested...\nvalue: "+thisFieldNamevalue+" ("+typeof(thisFieldNamevalue)+")"+"\nRegExp: "+validateTest[thisField.name]+"\nResult: "+valid);//testing
			break;
		case "function":
			valid = validateTest[thisField.name](thisField);
			break;
		default:
if (testing) alert('Test found for '+thisField.name+', but of unusable type ('+typeof validateTest[thisField.name]+')');//testing
			valid = true; //if we don't know how to test, trust the user
		}

	if (valid) 
	{
		if (individualAlerts) return true;
		else return '';
	}
	if (typeof validateMessage[thisField.name] == "string") alertMessage=validateMessage[thisField.name];
	thisField.focus();
	if (typeof(thisField.select)=='function') thisField.select();
	if (individualAlerts)
	{
		alert(alertMessage);
		return false;
	}
	else {
		return alertMessage;
	}
	}

function validateForm(thisForm,beStrict) {
	var testing=false;
	var validatedFieldNames=new Array();
	var fieldName;
	var tmp;
	var formAlertMessages='';

for (i=0 ; i < thisForm.elements.length ; i++) {
		fieldName=thisForm.elements[i].name;
		if(validatedFieldNames[fieldName]!=null) 
		{
			if (testing) alert ('validateForm(): skipping field '+fieldName);
			continue;
		}
		if (validateTest[fieldName] != null) {
			if (testing) alert ('validateForm(): checking field '+fieldName);
			validatedFieldNames[fieldName]=true;
			tmp=validateField(thisForm.elements[i], false);
			if ('' != tmp) formAlertMessages += "\r\n>> "+tmp;
			}
		} // for each field
		if ('' != formAlertMessages)
		{
			//if (beStrict)
			{
				alert ("Please complete the required fields.");
				return false;
			}
		//	else
			{
				return confirm("Please complete the required fields. Is it okay to submit this form anyway, or do you want to cancel and change something?\r\n\r\n"+formAlertMessages);
			}
		}
		return true;
	}//f validateForm
/*
}//if a simple regexp test passed
else {
alert ('JavaScript is outdated in your browser. Important form validation and calculations will not happen. Please consider upgrading your browser to better use this and other sites. Thanks.');
function validateField() { return true; }
function validateForm() { return true; }
}
//*/