//---------------------------------------------------------------------------------------------------//
//                                                                                                   //
//                          Javascript Form Validator by Keith Mountifield                           //
//                                          Copyright 2005                                           //
//                                                                                                   //
//---------------------------------------------------------------------------------------------------//

//----------------------------------------------------------------------------------------------------
//Create the formConfig object
function formConfig(formid,NumRequiredFields){
	this.formId = formid;
	this.requiredFields = NumRequiredFields;
}
//----------------------------------------------------------------------------------------------------
function requiredField(fieldId,validationType,fieldLabel,minChars){
	this.fieldid =  fieldId;
	this.fieldType = validationType;
	this.fieldLabel = fieldLabel;
	this.minimumChars = minChars;
}
//----------------------------------------------------------------------------------------------------
function validateMyForm() {
	//start Looping through the required fields
	//alert('Correct');
	alertErrorMessage = 'Sorry! There were Error in you submission:\n\n'
	for (fieldcount = 1; fieldcount <= myRequired.requiredFields; fieldcount ++){
		thisFieldObj = 'field' + fieldcount;
		thisFieldID = eval(thisFieldObj).fieldid;
		highlightTarget = eval(thisFieldObj).fieldLabel;
		//alert(highlightTarget);
		if (!validateFieldData(thisFieldObj)) {
			alertErrorMessage += thisFieldID + ' was not completed correctly.\n';
			var errors = true;
			document.getElementById(highlightTarget).className = 'RequiredError';
			
		} else {
			document.getElementById(highlightTarget).className = 'RequiredNormal';
			
		}
	}
	
	//If there have been errors, show an alert box with details, otherwise allow the submission to continue
	alertErrorMessage += '\n\nPlease Correct the errors before re-submitting the form.\n\nThank You!';
	if (errors){
		alert(alertErrorMessage);
		return false;
	} else {
		return true;
	}
}
//----------------------------------------------------------------------------------------------------
function initValidator() {
	//This function simply ensures that there are no memory leaks that may prevent the script working correctly
	alertErrorMessage = '';
}
//----------------------------------------------------------------------------------------------------
function validateFieldData(myField){
	//Validate the data within the field - returns true if the data is valid and flase if itis not
	
	myFieldId = eval(myField).fieldid;
	fieldValidationType = eval(myField).fieldType;

	//Checks for custom field types and acts accordingly
	if (eval(myField).fieldType == 'radiogroup'){
		fieldType = 'radiogroup';
	} else {
		fieldType = document.getElementById(myFieldId).type;
	}
	
	//This is the main switch that checks for what type of field and how to validate it.
	switch (fieldType) {
	
		//Simply checks that a checkbox is chosen
		case 'checkbox':
			if(document.getElementById(myFieldId).checked){
				return true;
			} else {
				return false;
			}
		break;
		//Checks that a text field has any content.
		case 'text':
			//--- This section checks the validation type and checks the content of the field accordingly
			switch (eval(myField).fieldType) {
				case 'text':
					if (document.getElementById(myFieldId).value){
						return true;
					} else {
						return false;
					}
				break;
				case 'number':
					if (document.getElementById(myFieldId).value.search(/^[0-9]{myfield.minimumChars}$/) >=0){
						return true;
					} else {
						return false;
					}
				break;
				case 'email':
					if (document.getElementById(myFieldId).value.search(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/) >= 0){
						return true;
					} else {
						return false;
					}
				break;
				default:
					return false;
			//--- End text checking
			}
		break;
		//Checks Text Areas
		case 'textarea':
			//--- This section checks the validation type and checks the content of the field accordingly
			switch (eval(myField).fieldType) {
				case 'text':
					if (document.getElementById(myFieldId).value){
						return true;
					} else {
						return false;
					}
				break;
				case 'number':
					if (document.getElementById(myFieldId).value.search(/^[0-9]{myfield.minimumChars}$/)){
						return true;
					} else {
						return false;
					}
				break;
				case 'email':
					if (document.getElementById(myFieldId).value.search(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/)){
						return true;
					} else {
						return false;
					}
				break;
				default:
					return false;
			//--- End text checking
			}
		break;
		case 'radiogroup':
			return validateRadioGroup(myFieldId);			
		break;
		case 'select-one':
			if (document.getElementById(myFieldId).value){
				return true;
			} else {
				return false;
			}
		break;
	}
}
//----------------------------------------------------------------------------------------------------
function validateRadioGroup(idLeader) {
//This function checks to see whether any of the options in a group of radio buttons has been selected
	var idCount = 1;
	var myElementId = idLeader + idCount;
	while(document.getElementById(myElementId)){
		if (document.getElementById(myElementId).checked){
			return true;
		}
		idCount ++;
		myElementId = idLeader + idCount;
	}
	return false;
}
//----------------------------------------------------------------------------------------------------
