/* 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 be validated. Use the following structure;
	
	thisformValidArray = new Array(
		new Array(
			formname.fieldname,        <- creates an object reference
			new RegExp(".*"),          <- good data for this field
													1. Checkboxes will be matched agaist their .checked member
														provide a boolean true or false for the value you want
													2. A group of checkboxes can also be checked for an "at
														least one of these" condition. Provide an object 
														reference to the first checkbox in the first element.
														This will switch on the "checkbox" bits of the code.
														In this element, instead of a good value provide an 
														array of the checkboxes (including the first one) that
														must have at least one selection to pass.
													3. Radio button groups and select lists will return true 
														if an option is selected so this value will be ignored.
													4. Text fields, textareas and password fields will be 
														matched against this value as a regular expression 
														(this is what's shown here).
													5. Buttons (submit, reset, button) and fileupload fields 
														cannot be 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 boxes are checked for any value selected.
			"Please correct this."     <- optional text for user prompt
		),
		new Array(
			formname.fieldname2...
			
	To halt validation for a cancel button but still permit GenericFormParser to do the page
	redirection set the cancel button to have the following events:
	
	onclick="formCancel = true;" onkeypress="formCancel = true;" 
	
	Be certain to define formCancel as false in the same script block 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 fieldObject	= 0;	// the array offset of the object pointer
	var fieldGoodData	= 1;	// the array offset of the regular expression
	var fieldPrompt	= 2;	// the array offset of the prompt
	var fieldType     = fieldList[fieldCounter][fieldObject].type;
	                        // the field type which determines how we check it.
	if (!fieldType) {
		if (fieldList[fieldCounter][fieldObject].length > 0) {
			fieldType = "radio";
		}
	}

	loopFields: for (fieldCounter = 0; fieldCounter < fieldList.length; fieldCounter++) {
	
		fieldType  = fieldList[fieldCounter][fieldObject].type;

		// get the field type
		switch (fieldType) {

			case "button":
				// cannot validate a button
				continue loopFields;

			case "checkbox":
				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 checkboxes selected 
					value against fieldGoodData as a boolean value.
				*/
				// is it an array?
				if (fieldList[fieldCounter][fieldGoodData].constructor == Array) {
					returnVal = checkgroup(fieldList[fieldCounter][fieldGoodData], fieldList[fieldCounter][fieldPrompt]);

				// otherwise it's a boolean
				} else {
					if (fieldList[fieldCounter][fieldObject].checked = fieldList[fieldCounter][fieldGoodData]) {
						returnVal = true;
					}
				}
				if (!returnVal) {
					formAlert(fieldList[fieldCounter][fieldObject], fieldList[fieldCounter][fieldPrompt]);
					return false;
				}

			case "password":
				if (!checkText(fieldList[fieldCounter][fieldObject], fieldList[fieldCounter][fieldGoodData], fieldList[fieldCounter][fieldPrompt])) {
					return false;
				}

			case "radio":
				return checkGroup(fieldList[fieldCounter][fieldObject], fieldList[fieldCounter][fieldPrompt])
				
			case "reset":
				// cannot validate a button
				continue loopFields;
				
			case "select-one":
				if (fieldList[fieldCounter][fieldObject].selectedIndex < fieldGoodData) {
					formAlert(fieldList[fieldCounter][fieldObject], fieldList[fieldCounter][fieldPrompt]);
					return false;
				}
				
			case "select-multiple":
				if (fieldList[fieldCounter][fieldObject].selectedIndex == -1) {
					formAlert(fieldList[fieldCounter][fieldObject], fieldList[fieldCounter][fieldPrompt]);
					return false;
				}
				
			case "submit":
				// cannot validate a button
				continue loopFields;
				
			case "textarea":
				// run the regular expression
				if (
					fieldList[fieldCounter][fieldGoodData].exec(fieldList[fieldCounter][fieldObject].value) == null ||
					fieldList[fieldCounter][fieldGoodData].exec(fieldList[fieldCounter][fieldObject].value) == ""
				) {
					formAlert(fieldList[fieldCounter][fieldObject], fieldList[fieldCounter][fieldPrompt]);
					return false;
				}
				
			case "text":
				// run the regular expression
				if (
					fieldList[fieldCounter][fieldGoodData].exec(fieldList[fieldCounter][fieldObject].value) == null ||
					fieldList[fieldCounter][fieldGoodData].exec(fieldList[fieldCounter][fieldObject].value) == ""
				) {
					formAlert(fieldList[fieldCounter][fieldObject], fieldList[fieldCounter][fieldPrompt]);
					return false;
				}
			
		} // end switch
	} // end loopFields
}

function formAlert(fieldObject, promptString) {

	if (promptString) {
		alert(promptString);

	} else {
		alert("Something appears to be wrong in the form. Please check your entries.")
	}
	
	fieldObject.focus();
}

function checkText(fieldObject, regularExp, fieldPrompt) {

	if (regularExp.exec(fieldObject.value)) {
		return true;

	} else {
		formAlert(fieldObject, 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;
	}
}