/* ***************************************************************************** 
	Checks the form to make sure it's a valid object and then sets up
	the validator object.
	
	input:
		formName		name of the form to be validated
	sets:
		formObject.onsubmit	call formSubmitHandler
		addValidation		call addValidation
***************************************************************************** */
function Validator(formName) {
	this.formObject		= document.forms[formName];
	if(!this.formObject) {
		alert("Error: cound not get Form object "+formName);
		return;
	}
	this.formObject.onsubmit	= formSubmitHandler;
	this.addValidation		= addValidation;
}

/* ***************************************************************************** 
	Uses a for loop to check each form element for verification
	
	Also added a radio button check into this method.  The radio button
	check goes through each element and checks to see if it's a radio
	button.  If a radio button is found to be checked, it will set to true,
	if not, false will be returned at the end.
***************************************************************************** */
function formSubmitHandler() {
	//Check fields
	for(var i=0;i<this.elements.length;i++) {
		if(this.elements[i].validationSet &&
			!this.elements[i].validationSet.validate() &&
			this.elements[i].type!="radio")
			return false;
	}
	//Check Radio Buttons
	var verifyRadio		= false;
	var radioName		= "";
	for(var j=0;j<this.elements.length;j++)
		if(this.elements[j].type == "radio" )
			if(this.elements[j].checked == true)
				verifyRadio = true;				
			else
				radioName	= this.elements[j].name;
	if(!verifyRadio)
		alert("Please select "+radioName);
	return verifyRadio;
}

/* ***************************************************************************** 
	Checks to make sure both the form and object are good, then checks
	to see if a validationSet exist.  If the set doesn't exist it creates
	one.  Lastly, add new validation to validationSet.
***************************************************************************** */
function addValidation(itemName,validationType,errorMessage) {
	if(!this.formObject) {
		alert("Error: cound not get Form object "+formName);
		return;
	}
	var itemObject=this.formObject[itemName];
	if(!itemObject) {
		alert("Error: cound not get input object "+itemName);
		return;
	}
	if(!itemObject.validationSet)
		itemObject.validationSet = new ValidationSet(itemObject);
	itemObject.validationSet.add(validationType,errorMessage);
}

/* ***************************************************************************** 
	Creates a new ValidationSet object
	
	vSet		Array to hold objects
	.add		method that calls addValidationDescription
	.addValidate	method that calls vSetValidate
	itemObject	equals the itemObject that was passed into the
			function
***************************************************************************** */
function ValidationSet(itemObject) {
	this.vSet		= new Array();
	this.add		= addValidationDescription;
	this.validate		= vSetValidate;
	this.itemObject		= itemObject;
}

/* *****************************************************************************
	Adds a validation description to the end of a validation set.
***************************************************************************** */
function addValidationDescription(description,error) {
	this.vSet[this.vSet.length] = 
		new ValidationDescription(this.itemObject,description,error);	
}

/* *****************************************************************************
	When validate is called for an object, loop through each item in the
	validation set and validate each condition.
***************************************************************************** */
function vSetValidate() {
	for(var i=0;i<this.vSet.length;i++) {
		if(!this.vSet[i].validate())
			return false;
	}
	return true;
}

/* *****************************************************************************
	Validation description object.  Takes the inputs of item,
	validation type, and error message and creates an object to contain
	these values.
***************************************************************************** */
function ValidationDescription(inputItem,validateType,errorMessage) {
	this.validateType	= validateType;
	this.errorMessage	= errorMessage;
	this.itemObject		= inputItem;
	this.validate		= vDescValidate;	
}

/* ***************************************************************************** 
	Checks to see if value is valid and if invalid, return false and set
	focus to the item.  If valid, return true.	
***************************************************************************** */
function vDescValidate() {
	if(!ValidateData(this.validateType,this.itemObject,this.errorMessage)) {
		this.itemObject.focus();
		return false;
	}
	return true;
}

/* ***************************************************************************** 
	Helper function for vDescValidate()
	
	input:
		validateType	Type of validation to perform
		objectValue	Object to validate
		errorString	string to print out on error		
***************************************************************************** */
function ValidateData(validateType,itemObject,errorString) {
	// Check to see if there is an equal sign in the validateType
	// If there is, then seperated it into a command and a commandValue
	// If there is not, just set command equal to validateType
	var pos = validateType.search("=");
	var command = "";
	var commandValue = "";
	if(pos >= 0) {
		command		= validateType.substring(0,pos);
		commandValue	= validateType.substr(pos+1);
	}
	else
		command = validateType
	
	// Figure out which type of validation to use
	switch(command) {
		case "req":
		case "required": {
			// Checks to see if a required field exist by
			// checking to see if the field value.length
			// is zero.
			if(eval(itemObject.value.length)==0) {
				if(!errorString	|| errorString.length == 0)
					errorString = itemObject.name + 
					" : Required Field";
				alert(errorString);
				return false;
			}
			break;
		} //End case required
	} //End Switch
	return true;
}