

//////////////> valForm - form validation script <///////////////
///////// place the following style script in form page /////////
/////////// the fields array can be as long as required /////////
/*///////////////////////////////////////////////////////////////
<script language="JavaScript" type="text/javascript">
function initValForm(){
	thisForm = document.forms[0];
	errMsg = "You have not filled out all the required fields \ncorrectly. Please double check the following \nfields: \n\n";	
	fieldsArray[0] = new fieldRecord("entrantFname","First Name","str");
	fieldsArray[1] = new fieldRecord("stateId","State","int");
	fieldsArray[2] = new fieldRecord("entrantEmail","Email","eml");
	}
</script>
*////////////////////////////////////////////////////////////////
////////// and then place the following in the form tag /////////
///////////////// onsubmit="return valForm()" ///////////////////
/////////////////////////////////////////////////////////////////

var errMsg;
var thisForm;
var fieldsArray = new Array();
var errorSwitch;
var blah = "bling";

//master function
function valForm(){
	errorSwitch = 0;
	initValForm();
	testVals();
	outputMsg();
	if 	(errorSwitch){
		alert(errMsg);
		return false
		}
	}

// set up function for second level arrays
function fieldRecord(fieldName, logicalName, test) {
	this.fieldName = fieldName;
	this.logicalName = logicalName;
	this.test = test;
	this.error = 0; //default error to false
	}

//loop through fieldsArray and run the appropriate test for each field
//update the error status for each field
function testVals(){
	for (i=0;i<fieldsArray.length;i++){
		var thisType = eval("thisForm." + fieldsArray[i].fieldName + ".type");
		var thisError = fieldsArray[i].error;
		var thisTest = fieldsArray[i].test;
		
		if 	(thisType == "select-one") {
			var thisVal = eval("thisForm." + fieldsArray[i].fieldName + ".options[thisForm." + fieldsArray[i].fieldName + ".selectedIndex].value")
			} else {
			var thisVal = eval("thisForm." + fieldsArray[i].fieldName + ".value")
			}
		
		if 	(thisTest == "str") {
				thisError = strTest(thisVal);
			} else if	(thisTest == "int") {
				thisError = intTest(thisVal);			
			} else if	(thisTest == "eml") {
				thisError = emlTest(thisVal);
			} else {
				alert('unknown test');
				break
			}
			
		fieldsArray[i].error = thisError;
		}
	}

//strTest tests that the field is not blank
function strTest(thisVal){
	thisError = 0;
	if 	(thisVal == "") {
		thisError = 1;
		}
	return thisError;
	}

//intTest tests that the field is not blank and that it is a number
function intTest(thisVal){
	thisError = 0;
	if 	(thisVal == "" || isNaN(thisVal)) {
		thisError = 1;
		}
	return thisError;
	}

//emlTest tests that the field is not blank and that it contains @ & .
function emlTest(thisVal){
	thisError = 0;
	if 	((thisVal == "" || thisVal.indexOf('@', 0) == -1) || thisVal.indexOf('.') == -1) {
		thisError = 1;
		}
	return thisError;
	}

//check the error code on each field and build error message
function outputMsg(){
	for (i=0;i<fieldsArray.length;i++){
	 if (fieldsArray[i].error){
	 		errorSwitch = 1;
	 		errMsg = errMsg + "- " + fieldsArray[i].logicalName + "\n";
	 		}
		}
	}
	
function confirmAction(item,action,url) {
	var text = "Are you sure you want to " + action + " " + item + "?";
	if (confirm(text)) {
		window.location = url;
	}
}
/////////////////////////////////////////////////////////////////