// ********** BEGIN VALIDATION FUNCTIONS ***********
function runValidation() {

		// Add the error messages, if any
	var obj_Error	= new ErrorMsg();
	obj_Error.AddCategory();
	for ( var i = 1; i < ( numChecks + 1 ); i++ )  {
		if ( checkFields[i] != "" ) obj_Error.AddMessage( checkFields[i] );
	}

		// Display errors
	if( obj_Error.errors == false ) {
		document.forms[0].submit();
	} else {
		obj_Error.DisplayMessage();
		return false;
	}
}

	// ===== Check if field is required =====
function isRequired ( fieldName, fieldDescription, errorLabel ) {
	errorMsg	= "";
	fieldValue	= document.forms[0][fieldName].value;
	if(( fieldValue == "" ) || ( fieldValue == " " )) {
		errorMsg = "\"" + fieldDescription +  "\"  - " + errorLabel + "";
	}
	return errorMsg ;
}

	// ===== Validate field minimum and maximum length =====
function validateMinMax ( fieldName, fieldDescription, minLength, maxLength ) {
	errorMsg	= "";
	fieldValue	= document.forms[0][fieldName].value;
	fieldLength	= document.forms[0][fieldName].value.length;
	if ( fieldLength < minLength ) {
		errorMsg = "\"" + fieldDescription +  "\"  - Please enter at least " + minLength + " characters.";
	} else if (( fieldLength > maxLength ) && ( maxLength > 0 )) {
		errorMsg = "\"" + fieldDescription +  "\"  - Please enter less than " + maxLength + " characters.";
	}
	return errorMsg ;
}

	// ===== Check if netry contains only valid characters =====
function checkValidChars( fieldName, fieldDescription, errorLabel ) {
	errorMsg	= "";
	fieldValue	= document.forms[0][fieldName].value;
	fieldLength	= document.forms[0][fieldName].value.length;
	for( var i=0; i<fieldLength; i++ ) {
		if ( validChars.indexOf( fieldValue.charAt( i )) == -1 ) {
			errorMsg = "\"" + fieldDescription +  "\" - " + errorLabel + "";
		}
	}
	return errorMsg ;
}

	// ===== Check if entry contains entirely identical characters =====
function checkIdentical( fieldName, fieldDescription, errorLabel ) {
	errorMsg	= "";
	fieldValue	= document.forms[0][fieldName].value;
	fieldLength	= document.forms[0][fieldName].value.length;

	if (( fieldLength > 1 ) && ( isComposedOfChars( fieldValue.charAt( 0 ), fieldValue ))) {
		errorMsg = "\"" + fieldDescription +  "\" - " + 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, fieldDescription, errorLabel ) {
	errorMsg	= "";
	fieldValue	= document.forms[0][fieldName].value;
	fieldLength	= document.forms[0][fieldName].value.length;

	if ( fieldLength > 1 ) {
		for ( var i = 1; i < ( numSeqStr + 1 ); i++ )  {
			if ( strSeq[i].indexOf( fieldValue ) != -1 ) {
				errorMsg = "\"" + fieldDescription +  "\" - " + errorLabel + "";
			}
		}
	}
	return errorMsg ;
}

	// ===== Check if an option has been selected =====
function validateSelectBox( fieldName, fieldDescription, errorLabel, checkWhat ) {
	errorMsg	= "";
	optSelected	= document.forms[0][fieldName].selectedIndex;
	fieldValue	= document.forms[0][fieldName].options[optSelected].value;
	if( fieldValue == checkWhat ) {
		errorMsg = "\"" + fieldDescription +  "\" - " + errorLabel + "";
	}
	return errorMsg ;
}

	// ===== Check if a checkbox or radio button has been selected =====
function validateBtnsBox( fieldName, fieldDescription, errorLabel ) {
	errorMsg	= "";
	selection = null;
	thisButton		= document.forms[0][fieldName];
	for( var i=0; i<thisButton.length; i++ ) {
		if( thisButton[i].checked ) {
		   selection = thisButton[i].value;
		}
	}
	if( selection == null ) {
		errorMsg = "\"" + fieldDescription +  "\" - " + errorLabel + "";
	}
	return errorMsg;
}

	// ===== Check if entry contains only numbers =====
function validateNumBox( fieldName, fieldDescription, errorLabel ) {
	errorMsg	= "";
	fieldValue	= document.forms[0][fieldName].value;
	fieldLength	= document.forms[0][fieldName].value.length;

	for( var i = 0; i < fieldLength; i++ ) {
		var ch = fieldValue.substring( i, i + 1 );
		if (( ch < "0" ) || ( ch > "9" )) {
			errorMsg = "\"" + fieldDescription +  "\" - " + errorLabel + "";
		}
	}
	return errorMsg ;
}

	// ===== Validate an email address =====
function validateEmailBox ( fieldName, fieldDescription, errorLabel ) {
	errorMsg	= "";
	fieldValue	= document.forms[0][fieldName].value;
	fieldLength	= document.forms[0][fieldName].value.length;

	if ( fieldLength > 0 ) {

		if (( errorLabel == "" ) || ( errorLabel == null )) {

			errorLabel1	= "This is not a valid email address.";			// Not valid
			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 = "\"" + fieldDescription +  "\"  - " + errorLabel2 + "";
			} else if ( fieldValue.indexOf( "." ) == -1 ) {
				errorMsg = "\"" + fieldDescription +  "\"  - " + errorLabel3 + "";
			} else if ( fieldValue.charAt( 0 ) == " " ) {
				errorMsg = "\"" + fieldDescription +  "\"  - " + errorLabel4 + "";
			} else if ( fieldValue.charAt( 0 ) == "@" ) {
				errorMsg = "\"" + fieldDescription +  "\"  - " + errorLabel5 + "";
			} else if ( fieldValue.charAt( 0 ) == "." ) {
				errorMsg = "\"" + fieldDescription +  "\"  - " + errorLabel6 + "";
			}

		} else {

			if (( fieldValue.indexOf( "@" ) == -1 ) || 					// Missing "@"
				( fieldValue.indexOf( "." ) == -1 ) || 					// Missing "."
				( fieldValue.charAt( 0 ) == "@" ) || 					// Starts with "@"
				( fieldValue.charAt( 0 ) == "." ) 						// Starts with "."
				) {
				errorMsg = "\"" + fieldDescription +  "\"  - " + errorLabel1 + "";
			}
		}
	}
	return errorMsg ;
}
// *********** END VALIDATION FUNCTIONS ************


// ************ BEGIN OBJECT FUNCTIONS *************
function AddCategory( str_Name ) {
		// Creates a new category object at the next space in the array
	this.obj_Errors[this.obj_Errors.length] = new NewCategory( str_Name );
	this.NewCategory = NewCategory;
}

function NewCategory( str_Name) {
		//  Initializes the category values
	this.str_Name		= str_Name;
	this.str_Error		= "";
}

function AddMessage( str_Msg ) {
	for ( var i = 0; i < this.obj_Errors.length; i++ )  {
		this.obj_Errors[i].str_Error += errorBullet;
		this.obj_Errors[i].str_Error += str_Msg + "\n";
		this.errors = true;
		return true;
	}
}

function ErrorMsg() {
		//  This is the object you create to keep track of errors in the document
	this.obj_Errors		= new Array();
	this.errors			= false;
	this.AddCategory	= AddCategory;
	this.AddMessage		= AddMessage;
	this.DisplayMessage	= DisplayMessage;
}
// ************* END OBJECT FUNCTIONS **************


function DisplayMessage() {
		// Displays the error messages.
		// If none, false is returned and no message is displayed.
	if ( this.errors == false ) {
		return false;
	} else {
		var str_msg = "";
		str_msg += errorLine1 + "\n";
		str_msg += errorLine2 + "\n";
		for ( var i = 0; i < this.obj_Errors.length; i++ ) {			// Go through all of the objects
			if ( this.obj_Errors[i].str_Error != '' ) {					// If errors, write the errors
				str_msg += "\n" + errorLine3 + "\n";
				str_msg += this.obj_Errors[i].str_Error + "\n";
			}
		}
		alert( str_msg );												// Display the error message
		return true;
	}
}
