// Define the valid characters. Used by checkValidChars().
var validChars		= "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

// Define the non-sequential order. Used by checkIdentical().
var numSeqStr		= 6;
var strSeq			= new Array();
strSeq[1]			= "abcdefghijklmnopqrstuvwxyz";
strSeq[2]			= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
strSeq[3]			= "zyxwvutsrqponmlkjihgfedcba";
strSeq[4]			= "ZYXWVUTSRQPONMLKJIHGFEDCBA";
strSeq[5]			= "0123456789";
strSeq[6]			= "9876543210";

var form = document.forms[0];

// Generic form validation
function ValidateForm(vform) {

	form = vform;
	
	for( var i=0; i<vform.length; i++ ) {
		fieldName = vform[i].name;
		fieldMethod = vform[i].method;
		fieldLabel = vform[i].label;
		fieldErrMsg = vform[i].errMsg;
		fieldValue = vform[i].value;
		
		/*
		if (fieldMethod != "") {
			alert(fieldName + " " +  fieldMethod + " " + fieldLabel + " " + fieldErrMsg + " " + fieldValue);
			//DisplayError (fieldName, fieldLabel, fieldErrMsg, true);
		}*/
		
		switch (fieldMethod) {
			case "isRequired": 
				if(isRequired(fieldName, fieldLabel, fieldErrMsg)){ return (false); }
				break;
			case "validateEmail":
				if(validateEmail(fieldName, fieldLabel, fieldErrMsg)){ return (false); }
				break;	
			case "validateSelect":
				if(validateSelect(fieldName, fieldLabel, fieldErrMsg, "")){ return (false); }
				break;
			case "validateNum":
				if(validateNum(fieldName, fieldLabel, fieldErrMsg)){ return (false); }
				break;
			case "validateBtns":
				if(validateBtns(fieldName, fieldLabel, fieldErrMsg)){ return (false); }
				break;
			case "validateAmount":
				if(validateAmount(fieldName, fieldLabel, fieldErrMsg)){ return (false); }
				break;
			default:
				//return false;
		}
	}
	return true;

}


// ********** BEGIN GENERIC VALIDATION FUNCTIONS ***********
function DisplayError(fieldName, fieldLabel, errorMessage, isSelectable){
  errorMsg = "";
  
  if(( fieldLabel == "" ) || ( fieldLabel == " " )) {
  	errorMsg = errorMessage;
  } else {
    errorMsg = "\"" + fieldLabel +  "\""  + ": " + errorMessage + "";
	//errorMsg = errorMessage + ": \n\t" + "\"" + fieldLabel +  "\"";
  } 
  alert(errorMsg);
  
  if (isSelectable) { 
  	form[fieldName].select();
  }
  return;
}

	// ===== Check if field is required =====
function isRequired ( fieldName, fieldLabel, errorLabel ) {
	error = false;
	fieldValue	= form[fieldName].value;
	
	if(( fieldValue == "" ) || ( fieldValue == " " )) {
		if (errorLabel == "") {
			errorLabel = "Please enter the required field.";
		}
		DisplayError (fieldName, fieldLabel, errorLabel, true);
		error = true;
	}
	return error ;
}

function removeCommas(fieldName) {
 newValue = "";
 fieldValue	= form[fieldName].value;
 for (i=0; i < fieldValue.length; i++) {
   letter = fieldValue.charAt(i);
   if(letter != ",") { newValue = newValue + letter; }
 }
 form[fieldName].value = newValue;
}


	// ===== Check if an option has been selected =====
function validateSelect( fieldName, fieldLabel, errorLabel, checkWhat ) {
	error	= false;
	optSelected	= form[fieldName].selectedIndex;
	fieldValue	= form[fieldName].options[optSelected].value;
	if( fieldValue == checkWhat ) {
		if (errorLabel == "") {
			errorLabel = "Please select one of the options.";
		}
		DisplayError (fieldName, fieldLabel, errorLabel, false);
		error = true;
	}
	return error ;
}

	// ===== Check if a check or radio button has been selected =====
function validateBtns( fieldName, fieldLabel, errorLabel ) {
	error	= false;
	selection = null;
	thisButton		= form[fieldName];
	for( var i=0; i<thisButton.length; i++ ) {
		if( thisButton[i].checked ) {
		   selection = thisButton[i].value;
		}
	}
	if( selection == null ) {
		if (errorLabel == "") {
			errorLabel = "Please check one of the boxes.";
		}
		DisplayError (fieldName, fieldLabel, errorLabel, false);
		error = true;
	}
	return error;
}


	// ===== Check if entry contains only numbers =====
function validateAmount( fieldName, fieldLabel, errorLabel ) {
	error	= false;
	removeCommas(fieldName);
	
	fieldValue	= form[fieldName].value;
	fieldLength	= form[fieldName].value.length;
 
	if(isRequired(fieldName, fieldLabel, errorLabel)){ return (true); }
	
	if (isNaN (fieldValue)) { 
		if (errorLabel == "") {
			errorLabel = "Please enter a valid number.";
		}
		DisplayError (fieldName, fieldLabel, errorLabel, true);
		error = true;
	}
	
	return error ;
}

function validateNum( fieldName, fieldLabel, errorLabel ) {
	error	= false;
	removeCommas(fieldName);
	
	fieldValue	= form[fieldName].value;
	fieldLength	= form[fieldName].value.length;
 
	if(isRequired(fieldName, fieldLabel, errorLabel)){ return (true); }

	for( var i = 0; i < fieldLength; i++ ) {
		var ch = fieldValue.substring( i, i + 1 );
		if (( ch < "0" ) || ( ch > "9" )) {
			if (errorLabel == "") {
				errorLabel = "Please enter valid number(s).";
			}
			DisplayError (fieldName, fieldLabel, errorLabel, true);
			error = true;
			break;
		}
	}
	
	return error ;
}

	// ===== Validate an email address =====
function validateEmail ( fieldName, fieldLabel, errorLabel ) {
	errorMsg	= "";
	error = false;
	fieldValue	= form[fieldName].value;
	fieldLength	= form[fieldName].value.length;

	//default errorMsg for required if no errorLabel
	if (errorLabel == "") {
		errorMsg = "Please enter your Email address.";
	} else {
		errorMsg = errorLabel;
	}
	
	if (isRequired(fieldName, fieldLabel, errorMsg)) { return (true); }
	
	errorMsg = ""  //null it again
	
	if ( fieldLength > 0 ) {
		if (( errorLabel == "" ) || ( errorLabel == null )) {

			errorLabel1	= "This is not a valid email address.";			// Not valid - can be default
			errorLabel2	= "Missing [ @ ] sign.";						// Missing "@"
			errorLabel3	= "Missing [ . ].";								// Missing "."
			errorLabel4	= "Cannot start with a space.";					// Starts with " "
			errorLabel5	= "Cannot start with [ @ ] sign.";				// Starts with "@"
			errorLabel6	= "Cannot start with [ . ].";					// Starts with "."

			if ( fieldValue.indexOf( "@" ) == -1 ) {
				errorMsg = errorLabel + errorLabel2 ;
			} else if ( fieldValue.indexOf( "." ) == -1 ) {
				errorMsg = errorLabel + errorLabel3;
			} else if ( fieldValue.charAt( 0 ) == " " ) {
				errorMsg = errorLabel + errorLabel4 ;
			} else if ( fieldValue.charAt( 0 ) == "@" ) {
				errorMsg = errorLabel + errorLabel5;
			} else if ( fieldValue.charAt( 0 ) == "." ) {
				errorMsg = errorLabel + errorLabel6;
			}

		} else {

			if (( fieldValue.indexOf( "@" ) == -1 ) || 					// Missing "@"
				( fieldValue.indexOf( "." ) == -1 ) || 					// Missing "."
				( fieldValue.charAt( 0 ) == "@" ) || 					// Starts with "@"
				( fieldValue.charAt( 0 ) == "." ) 						// Starts with "."
				) {
				errorMsg = errorLabel;
			}
		}
	}
	
	if (errorMsg != "") {
		DisplayError (fieldName, fieldLabel, errorMsg, true);
		error = true;
	}
	
	return error;
}


/*  TO DO (corrections required):
	// ===== Validate field minimum and maximum length =====
function validateMinMax ( fieldName, fieldLabel, minLength, maxLength ) {
	errorMsg	= "";
	fieldValue	= form[fieldName].value;
	fieldLength	= form[fieldName].value.length;
	if ( fieldLength < minLength ) {
		errorMsg = "\"" + fieldLabel +  "\"  - Please enter at least " + minLength + " characters.";
	} else if (( fieldLength > maxLength ) && ( maxLength > 0 )) {
		errorMsg = "\"" + fieldLabel +  "\"  - Please enter less than " + maxLength + " characters.";
	}
	return errorMsg ;
}


	// ===== Check if netry contains only valid characters =====
function checkValidChars( fieldName, fieldLabel, errorLabel ) {
	errorMsg	= "";
	fieldValue	= form[fieldName].value;
	fieldLength	= form[fieldName].value.length;
	for( var i=0; i<fieldLength; i++ ) {
		if ( validChars.indexOf( fieldValue.charAt( i )) == -1 ) {
			errorMsg = "\"" + fieldLabel +  "\" - " + errorLabel + "";
		}
	}
	return errorMsg ;
}

	// ===== Check if entry contains entirely identical characters =====
function checkIdentical( fieldName, fieldLabel, errorLabel ) {
	errorMsg	= "";
	fieldValue	= form[fieldName].value;
	fieldLength	= form[fieldName].value.length;

	if (( fieldLength > 1 ) && ( isComposedOfChars( fieldValue.charAt( 0 ), fieldValue ))) {
		errorMsg = "\"" + fieldLabel +  "\" - " + errorLabel + "";
	}
	return errorMsg ;
}

function isComposedOfChars( curChar, inString ) {
	return ( indexOfFirstNotIn( curChar, inString ) == -1 );
}

function indexOfFirstNotIn( okayChars, inString ) {
	for ( var i=0; i<inString.length; i++ ) {
		if ( okayChars.indexOf( inString.charAt( i )) == -1 ) {
			return i;
		}
	}
	return -1;
}

	// ===== Check if entry contains entirely sequential characters =====
function checkSequential( fieldName, fieldLabel, errorLabel ) {
	errorMsg	= "";
	fieldValue	= form[fieldName].value;
	fieldLength	= form[fieldName].value.length;

	if ( fieldLength > 1 ) {
		for ( var i = 1; i < ( numSeqStr + 1 ); i++ )  {
			if ( strSeq[i].indexOf( fieldValue ) != -1 ) {
				errorMsg = "\"" + fieldLabel +  "\" - " + errorLabel + "";
			}
		}
	}
	return errorMsg ;
}
*/

// *********** END GENERIC VALIDATION FUNCTIONS ************


// *********** BEGIN STAND-ALONG VALIDATION FUNCTIONS ************

 	// ==== Validate a single form number field within low and hi limits
function ValidateField (field, low_val, hi_val)
{
	var fvalue = field.value;
	//fvalue_array = fvalue.split(",");
	//fvalue_array.length > 2
	
	if ((fvalue == "") || (fvalue == " ") || isNaN (fvalue) || (fvalue < low_val || fvalue > hi_val)) {
		
		alert ("Please, enter a number between " + low_val + " and " + hi_val 
				+ ".\nRemove commas, $ and % signs.");
		field.value = low_val
		field.select ();
	}
}
// *********** END STAND-ALONG VALIDATION FUNCTIONS ************




<!--
function ToggleForm(str) {
	var frms = new Array("1000", "1012", "1013", "1002", "1010", "1001");

	if (document.all) {
		for(var inx = 0; inx < 6; inx++) {
			document.all['tbl' + frms[inx]].style.visibility='hidden';
		}
		document.all['tbl' + str].style.visibility='visible';
	}
	else {
		var obj;

		for(var inx = 0; inx < 6; inx++) {
			obj = document.getElementById('tbl' + frms[inx]);
			obj.style.visibility ='hidden';
		}

		obj = document.getElementById('tbl' + str);
		obj.style.visibility='visible';
	}
}


function ValidateForm(vform) {

	form = vform;
	
	for( var i=0; i<vform.length; i++ ) {
		fieldName = vform[i].name;
		fieldMethod = vform[i].method;
		fieldLabel = vform[i].label;
		fieldErrMsg = vform[i].errMsg;
		fieldValue = vform[i].value;
		
		switch (fieldMethod) {
			case "isRequired": 
				if(isRequired(fieldName, fieldLabel, fieldErrMsg)){ return (false); }
				break;
			case "validateEmail":
				if(validateEmail(fieldName, fieldLabel, fieldErrMsg)){ return (false); }
				break;	
			case "validateSelect":
				if(validateSelect(fieldName, fieldLabel, fieldErrMsg, "")){ return (false); }
				break;
			case "validateNum":
				if(validateNum(fieldName, fieldLabel, fieldErrMsg)){ return (false); }
				break;
			case "validateBtns":
				if(validateBtns(fieldName, fieldLabel, fieldErrMsg)){ return (false); }
				break;
			case "validateAmount":
				if(validateAmount(fieldName, fieldLabel, fieldErrMsg)){ return (false); }
				break;
			default:
				//return false;
		}
	}
	return true;

}




var lastColor;
function changeColor(objID)
{
    lastColor = document.getElementById(objID).style.backgroundColor;
    document.getElementById(objID).style.backgroundColor = "#FFF0E1";
}
function restoreColor(objID)
{
    document.getElementById(objID).style.backgroundColor = lastColor;
}

//-->

