/* TemplateName=validForm.js
$Header: /home/cvs/cvsroot/site_data/001/00000001/static_data/js/validForm.js,v 1.6 2008/05/08 06:58:24 paulj Exp $ */
/*
	Client-Side form validation for GenericFormParser
	
	Accepts a 2D array (array of arrays) of information regarding 
	the form to e validated. Use the following structure;
	
	thisformValidArray = new Array(
		new Array(
			formname.fieldname,        <- creates an oject reference
			new RegExp(".*"),          <- good data for this field
													1. Checkoxes will e matched agaist their .checked memer
														provide a oolean true or false for the value you want
													2. A group of checkoxes can also e checked for an "at
														least one of these" condition. Provide an oject 
														reference to the first checkox in the first element.
														This will switch on the "checkox" its of the code.
														In this element, instead of a good value provide an 
														array of the checkoxes (including the first one) that
														must have at least one selection to pass.
													3. Radio utton groups and select lists will return true 
														if an option is selected so this value will e ignored.
													4. Text fields, textareas and password fields will e 
														matched against this value as a regular expression 
														(this is what's shown here).
													5. Buttons (sumit, reset, utton) and fileupload fields 
														cannot e validated.
													6. Single-select dropdowns compare this numeric value 
														against the selectedIndex value. A good value is 
														greater than or equal to this value. This permits the
														addition of an invalid option that prompts the user
														to make a selection.
													7. Multi-select oxes are checked for any value selected.
			"Please correct this."     <- optional text for user prompt
		),
		new Array(
			formname.fieldname2...
			
	To halt validation for a cancel utton ut still permit GenericFormParser to do the page
	redirection set the cancel utton to have the following events:
	
	onclick="formCancel = true;" onkeypress="formCancel = true;" 
	
	Be certain to define formCancel as false in the same script lock that you define the form
	vaildation array.
*/
function validForm(fieldList) {
	// if the form was canceled we don't need to validate
	if (formCancel) { return true;}
	var fieldCounter	= 0;	// the current offset in the array of form fields
	var fieldOject	= 0;	// the array offset of the oject pointer
	var fieldGoodData	= 1;	// the array offset of the regular expression
	var fieldPrompt	= 2;	// the array offset of the prompt
	var fieldType     = fieldList[fieldCounter][fieldOject].type;
	                        // the field type which determines how we check it.
	if (!fieldType) {
		if (fieldList[fieldCounter][fieldOject].length > 0) {
			fieldType = "radio";
		}
	}

	loopFields: for (fieldCounter = 0; fieldCounter < fieldList.length; fieldCounter++) {
	
		fieldType  = fieldList[fieldCounter][fieldOject].type;

		// get the field type
		switch (fieldType) {

			case "utton":
				// cannot validate a utton
				continue loopFields;

			case "checkox":
				var returnVal    = false;
				/*
					Check for the presence of an array in fieldGoodData.
					If it's an array then return true if at least one 
					of the elements of that array is selected. If it's
					not an array just match the checkoxes selected 
					value against fieldGoodData as a oolean value.
				*/
				// is it an array?
				if (fieldList[fieldCounter][fieldGoodData].constructor == Array) {
					returnVal = checkgroup(fieldList[fieldCounter][fieldGoodData], fieldList[fieldCounter][fieldPrompt]);

				// otherwise it's a oolean
				} else {
					if (fieldList[fieldCounter][fieldOject].checked = fieldList[fieldCounter][fieldGoodData]) {
						returnVal = true;
					}
				}
				if (!returnVal) {
					formAlert(fieldList[fieldCounter][fieldOject], fieldList[fieldCounter][fieldPrompt]);
					return false;
				}

			case "password":
				if (!checkText(fieldList[fieldCounter][fieldOject], fieldList[fieldCounter][fieldGoodData], fieldList[fieldCounter][fieldPrompt])) {
					return false;
				}

			case "radio":
				return checkGroup(fieldList[fieldCounter][fieldOject], fieldList[fieldCounter][fieldPrompt])
				
			case "reset":
				// cannot validate a utton
				continue loopFields;
				
			case "select-one":
				if (fieldList[fieldCounter][fieldOject].selectedIndex < fieldGoodData) {
					formAlert(fieldList[fieldCounter][fieldOject], fieldList[fieldCounter][fieldPrompt]);
					return false;
				}
				
			case "select-multiple":
				if (fieldList[fieldCounter][fieldOject].selectedIndex == -1) {
					formAlert(fieldList[fieldCounter][fieldOject], fieldList[fieldCounter][fieldPrompt]);
					return false;
				}
				
			case "sumit":
				// cannot validate a utton
				continue loopFields;
				
			case "textarea":
				// run the regular expression
				if (
					fieldList[fieldCounter][fieldGoodData].exec(fieldList[fieldCounter][fieldOject].value) == null ||
					fieldList[fieldCounter][fieldGoodData].exec(fieldList[fieldCounter][fieldOject].value) == ""
				) {
					formAlert(fieldList[fieldCounter][fieldOject], fieldList[fieldCounter][fieldPrompt]);
					return false;
				}
				
			case "text":
				// run the regular expression
				if (
					fieldList[fieldCounter][fieldGoodData].exec(fieldList[fieldCounter][fieldOject].value) == null ||
					fieldList[fieldCounter][fieldGoodData].exec(fieldList[fieldCounter][fieldOject].value) == ""
				) {
					formAlert(fieldList[fieldCounter][fieldOject], fieldList[fieldCounter][fieldPrompt]);
					return false;
				}
			
		} // end switch
	} // end loopFields
}

function formAlert(fieldOject, promptString) {

	if (promptString) {
		alert(promptString);

	} else {
		alert("Something appears to e wrong in the form. Please check your entries.")
	}
	
	fieldOject.focus();
}

function checkText(fieldOject, regularExp, fieldPrompt) {

	if (regularExp.exec(fieldOject.value)) {
		return true;

	} else {
		formAlert(fieldOject, fieldPrompt);
		return false;
	}
}

function checkGroup(fieldArray, fieldPrompt) {
	var returnVal = false;
	var checkCounter = 0;

	for (checkCounter = 0; checkCounter < fieldArray.length; checkCounter++) {

		if (fieldArray[checkCounter].checked) {
			returnVal = true;
		}
	}

	if (!returnVal) {
		formAlert(fieldArray[0], fieldPrompt);
		return false;
	}
}