/*
==================================================
// PROGRAM: garbageOUT_V3
// AUTHOR:	csimmons.net
// DATE:	01.09.2003, 07.23.2003, 12.08.2004
//			10.17.2005
// VERSION: 3.1
// PURPOSE: Validates form data
// NOTES:	Firefox compliant - see Firefox Note
//================================================
Begin Configuration File Notes
------------------------------------------------
	BASICS:
	
	There are 3 steps:
	
	1. You must define the form's  name, total number of 
	  fields to check, and the conditions array on the 
	  HTML page calling this script.  Instructions for 
	  defining each of these elements are located in the 
	  CONDITIONS section below.
	  
	2. Immediately following the definitions include this script 
	  in the HTML file using the syntax for including external
	  .js files.
	
	3. Add the following to your form tag:
	
		onsubmit = "return garbageOUT(1)"
------------------------------------------------	
	CONDITIONS:
	
	- NOTE: You are only changing the value of X and the array values.
	- DO NOT change "CheckField" to your field's name.
	- Define the arrays in the HTML page that calls this script using the following rules:	
	   1. Enter each field on a separate line.
	   2. X is the field number.  Begin with 1 and increment X by 1 on each subsequent line.
	   3. Put true for the values you want to check based on the following key (unless the
	      key indicates that a number is required:

			INDEX KEY:
			CheckFieldX[0] = Field Name (from the form)
			CheckFieldX[1] = Field Description (Used in the error message to the user)		
			CheckFieldX[2] = Check for Blanks
			CheckFieldX[3] = Check for Email
			CheckFieldX[4] = Check for Numeric Characters
			CheckFieldX[5] = Check for Alpha Characters
			CheckFieldX[6] = Check for Number >= array index 7.  NUMBER REQUIRED!!!						
			CheckFieldX[7] = Number to use with array index 6.  NUMBER REQUIRED!!!
			
	- A sample array might look like:

			CheckField1 = ["FN", "First Name", true, false, false, true, falses, false, false]
------------------------------------------------
	OTHER VARIABLES:

Form Name:
Total # of Fields to be checked
TotalFields = ##;
------------------------------------------------
End Configuration Notes
================================================
Firefox Note:
Form Name No Longer needed
strFormName + "." + fieldName  has been replaced by: "document.getElementById('" + fieldName + "')"
================================================
DON'T EDIT BELOW OR YOU COULD WRECK IT
================================================
*/

// Get to work!
function garbageOUT(isOn){
//================================================
// Is garbageOUT turned on?  If not end the script
	if (isOn != 1){
		return;
	}	
	
// Variables
	var emailFilter=/^.+@.+\..{2,3}$/;
	var strMsg = ''
	
//================================================
// Begin to LOOP through the CONDITIONS
	
	 for (var count = 1; count <= TotalFields; count++){
	 
	 	var fieldName = eval("CheckField" + count + "[0]")
		var fieldDesc = eval("CheckField" + count + "[1]")
	
//================================================
// Check For Blanks 
// Uses Array Position 2
		switch(eval("CheckField" + count + "[2]")) {
		case true :
			if (eval("document.getElementById('" + fieldName + "')" + ".value") == ''){				
				strMsg = strMsg + 'You have not entered data in field [' + fieldDesc + '].\n'
			}
			break;
		}
//================================================
	// Check For Valid Email Address
	// Uses Array Position 3	
		switch(eval("CheckField" + count + "[3]")) {
		case true :
			if (!(emailFilter.test(eval("document.getElementById('" + fieldName + "')" + ".value")))) { 
		    	strMsg = strMsg + 'Please enter a valid email address.\n';
			}	
			break;
		}
//================================================
// Check For NUMERIC characters
// Uses Array Position 4	
		switch(eval("CheckField" + count + "[4]")) {
		case true :
			var num_valid = "0123456789."
			var upass_string = eval("document.getElementById('" + fieldName + "')" + ".value")
			for (var i = 0; i < upass_string.length; i++) {
				if (num_valid.indexOf(upass_string.charAt(i)) < 0) {
					strMsg = strMsg + 'Numeric characters only in field [' + fieldDesc + '].\n'
				break;
				}
			}
			break;
		}
//================================================	
// Check For ALPHA characters
// Uses Array Position 5	
		switch(eval("CheckField" + count + "[5]")) {
		case true :
			var alph_valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
			var upass_string = eval("document.getElementById('" + fieldName + "')" + ".value")
			for (var i = 0; i < upass_string.length; i++) {
				if (alph_valid.indexOf(upass_string.charAt(i)) < 0) {
					strMsg = strMsg + 'Alpha characters only in field [' + fieldDesc + '].\n'
				break;
				}
			}
			break;
		}
//================================================
// Check For LowNum <= X
//  Uses Array Positions 6, 7
		switch(eval("CheckField" + count + "[6]")) {
			case true :
			var numInRange = eval("document.getElementById('" + fieldName + "')" + ".value")
			var LowNum = eval("CheckField" + count + "[7]")
				if (numInRange < LowNum){
					strMsg = strMsg + 'Input must be equal to or greater than ' + LowNum + ' for [' + fieldDesc + '].\n'
				} // nested if
				break;
		} // goes with switch
//================================================
// Check For Select 
// Uses Array Position 8
		switch(eval("CheckField" + count + "[8]")) {
		case true :
			if (eval("document.getElementById('" + fieldName + "')" + ".selectedIndex") == 0){				
				strMsg = strMsg + 'You have not made a selection for field [' + fieldDesc + '].\n'
			}
			break;
		}		
//================================================	
	 } //Goes with Opening For Loop
// End CONDITIONS
		
// Give final error message or submit
	if (strMsg == ""){
		return true;
		}else{
		alert(strMsg)
		return false;
	}

} // goes with main function