// JavaScript Document
function toolBox(formObj, requireFieldsArray, checkFieldsForArray) {
	//properties
	this.formObj = formObj;
	this.requireFieldsArray = requireFieldsArray;
	this.checkFieldsForArray = checkFieldsForArray;
	
	this.isEmptyErrorMsg = "";
	this.isEmailErrorMsg = "";
	this.isSelectErrorMsg = "";
	this.isAlphaErrorMsg = "";
	this.isNumErrorMsg = "";
	this.isAlphaNumErrorMsg = "";
	
	/*
	 This is the errorFlagArray.  It holds the flag signaling whether to output certain error messages.  The following is a description of the 		     flags at each array index.
	 		index 0 - Holds the required field is empty flag.
			index 1 - Holds the email syntax flag.
			index 2 - Holds the HTML select form flag.
			index 3 - Holds if the field value is alphabetical only flag.
			index 4 - Holds if the field value is numeric only flag.
			index 5 - Holds if the field value is alphabetical/numeric/./# flag.
	*/
	this.errorFlagArray = new Array(0, 0, 0, 0, 0, 0);
	this.errorMsgArray = new Array();
	
	this.masterErrorMsg = "";
	
	//methods
	//trim method
	this.trim = function(formElementObj) {
		formElementObj.value = formElementObj.value.replace(/^\s+|\s+$/,'');
		formElementObj.value = formElementObj.value.replace(/^\s+|\s+$/,'');
	}
	
	//isEmpty method
	this.isEmpty = function(formElementObj) {	
		if(formElementObj.value == "") {
			if(this.errorFlagArray[0] == 0) 
				this.isEmptyErrorMsg = "\nThe following fields are empty:\n";
			
			this.isEmptyErrorMsg = this.isEmptyErrorMsg + "- " + formElementObj.name + "\n"; 
			
			this.errorFlagArray[0] = 1;
			this.errorMsgArray[0] = this.isEmptyErrorMsg;
		}
	}
	
	this.isEmail = function(formElementObj) {
		var pattern = new RegExp("[a-zA-Z][a-zA-Z0-9]*@[a-zA-Z0-9]+\.[a-zA-Z]+");
		
		if(!pattern.test(formElementObj.value) && formElementObj.value != "") {
			if(this.errorFlagArray[1] == 0)
				this.isEmailErrorMsg = "\nYou did not enter a valid e-mail address.\n";
			
			this.isEmailErrorMsg = this.isEmailErrorMsg + "- " + formElementObj.value + "\n";
			
			this.errorFlagArray[1] = 1;
			this.errorMsgArray[1] = this.isEmailErrorMsg;
		}
	}
	
	this.isSelect = function(formElementObj) {
		
		if(formElementObj.value == -1) {
			
			if(this.errorFlagArray[2] == 0)
				this.errorMsgArray[2] = "\nYou must select a choice from the drop-down box.\n";
			
			this.errorFlagArray[2] = 1;
		}
	}
	
	this.isAlpha = function(formElementObj) {
		
		var pattern = new RegExp("[a-zA-Z]+");
		
		if(!pattern.test(formElementObj.value) && formElementObj.value != "") {
			if(this.errorFlagArray[3] == 0)
				this.isAlphaErrorMsg = "\nThe following fields must contain only alphabetical characters.\n";
			
			this.isAlphaErrorMsg = this.isAlphaErrorMsg + "- " + formElementObj.name + "\n";
			
			this.errorFlagArray[3] = 1;
			this.errorMsgArray[3] = this.isAlphaErrorMsg;
		}
	}
	
	this.isNum = function(formElementObj) {
		var pattern = new RegExp("[0-9]+");
	
		if(!pattern.test(formElementObj.value) && formElementObj.value != "") {
			if(this.errorFlagArray[4] == 0)
				this.isNumErrorMsg = "\nThe following fields must contain only numeric characters.\n";
			
			this.isNumErrorMsg = this.isNumErrorMsg + "- " + formElementObj.name + "\n";
			
			this.errorFlagArray[4] = 1;
			this.errorMsgArray[4] = this.isNumErrorMsg;
		}
	}
	
	this.isAlphaNum = function(formElementObj) {
		var pattern = new RegExp("[a-zA-Z0-9\.\#]+");
		
		if(!pattern.test(formElementObj.value) && formElementObj.value != "") {
			if(this.errorFlagArray[5] == 0)
				this.isAlphaNumErrorMsg = "\nThe following fields can only contain alphanumeric characters, '.', and '#'.\n";
			
			this.isAlphaNumErrorMsg = this.isAlphaNumErrorMsg + "- " + formElementObj.name + "\n";
			
			this.errorFlagArray[5] = 1;
			this.errorMsgArray[5] = this.isAlphaNumErrorMsg;
		}
	}
	
	this.outputErrors = function() {
		for(var i = 0; i < this.errorFlagArray.length; i++) {
			if(this.errorFlagArray[i] == 1) 
				this.masterErrorMsg = this.masterErrorMsg + this.errorMsgArray[i];
		}
		
		if(this.masterErrorMsg != "") {
			alert(this.masterErrorMsg);
			return false;
		} else {
			return true;
		}
	}

}

function run_validator(toolBoxObj) {
	
	//loop through each field of the form
	for(var i = 0; i < toolBoxObj.formObj.length; i++) {
		
		//trim leading and trailing spaces
		toolBoxObj.trim(toolBoxObj.formObj.elements[i]);
		
		if(toolBoxObj.requireFieldsArray[i] == "R") {	//if the field is required, does not allow empty fields
			//check if empty
			toolBoxObj.isEmpty(toolBoxObj.formObj.elements[i]);
		} 
		
		switch(toolBoxObj.checkFieldsForArray[i]) {
			case "isAlpha":
				toolBoxObj.isAlpha(toolBoxObj.formObj.elements[i]);
			  	break;   
			case "isAlphaNum":
				toolBoxObj.isAlphaNum(toolBoxObj.formObj.elements[i]);
				break;
			case "isNum":
				toolBoxObj.isNum(toolBoxObj.formObj.elements[i]);
				break;
			case "isEmail":
				toolBoxObj.isEmail(toolBoxObj.formObj.elements[i]);
				break;
			case "isSelect":
				toolBoxObj.isSelect(toolBoxObj.formObj.elements[i]);
				break;
		}
		
	}
	
	return toolBoxObj.outputErrors()? true : false;

}

