/*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) {
*/

function validateField(thisField) {
var testing=false;
if (testing) alert('validateField called for '+thisField.name+"\n"+validateTest[thisField.name]);//testing
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
		return true;
		}
	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) return true;
	}

	switch (typeof validateTest[thisField.name]) {
		case "string":
			validateRegExp.compile(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) return true;
	if (typeof validateMessage[thisField.name] == "string") alertMessage=validateMessage[thisField.name];
	thisField.focus();
	if (typeof(thisField.select)=='function') thisField.select();
	alert(alertMessage);
	return false;
	}

function validateForm(thisForm,beStrict) {
	var testing=false;
	var fieldsAreValid=true; 
	var validatedFieldNames=new Array();
	var fieldName;
	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;
			if (validateField(thisForm.elements[i]) == false) fieldsAreValid=false;
			}
		} // for each field
	if (fieldsAreValid == false && beStrict != true) return confirm("Some fields look like they might have incorrect information. Is it okay to submit this form anyway, or do you want to cancel and change something?");
	else return fieldsAreValid;
	}//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; }
}
//*/