/* 
TO USE VALIDATION:
Include this file on page requiring validation
Add array of fields to validate to page that has form
Array name should be requiredFields
	var requiredFields = new Array();
Fill array with requiredField objects (one for each field)            
requiredFields.push(new requiredField("field_id_here","","field_label_here"));

Add error messages span to fill with messages on error
<span id="errorMessages"></span>
*/

/* 
name is id of field
type is field type or format - what data is in this field.
type can be left blank if no format validation is required.
type can be: email
message is the name of the field that will appear before the error message to incident field with problem
*/
function requiredField(name,type,message){
	this.name = name;
	this.type = type;
	this.message = message;	
}

/* validation function */
function validateFormData(){
	var errorMessages = new Array();
	//using data found on page, go through list of required  fields
	for(rf=0;rf<requiredFields.length;rf++)
	{
		//if a field is not filled		
		if(document.getElementById(requiredFields[rf].name).value == "") {
			errorMessages.push(requiredFields[rf].message + " is Required");			
		} else { //else if field is filled
			//see if there is a type for this field and do validation for that type
			if(requiredFields[rf].type !="")
			{
				var formatCorrect = true;
				switch(requiredFields[rf].type) {
					case "email": formatCorrect=validateEmailAddress(document.getElementById(requiredFields[rf].name).value); break;
					case "phone": formatCorrect=validatePhone(document.getElementById(requiredFields[rf].name).value); break;
					case "checkbox": formatCorrect=document.getElementById(requiredFields[rf].name).checked; break;
				}
				
				if(!formatCorrect){
					//add format error message
					if(requiredFields[rf].type!="checkbox")
						errorMessages.push(requiredFields[rf].message + " must be in a valid format");
					else
						errorMessages.push(requiredFields[rf].message);
				}
			}
		}
	}
	
	//if there are any errors
	if(errorMessages.length>0) {
		//display error messages
		var errorOutput = "<table id=\"ValidationSummary1\" class=\"ValidationSummary\" onclick=\"toggleDiv('ValidationSummary1');\" style=\"display: block; white-space: nowrap;\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\"><tr><td><ul>";
		for(em=0; em<errorMessages.length; em++) {errorOutput += "<li>"+errorMessages[em]+"</li>";}
		errorOutput += "</ul></td></tr></table>";
		document.getElementById("errorMessages").innerHTML = errorOutput;
		
		//go to top of page
		window.scrollTo(0,0);
		var objDiv = document.getElementById("mainContentBodySmall");
		objDiv.scrollTop = 0;
		
		return false
	}

	return true;
}

function validateEmailAddress(testString){
	//see if testString is a valid email address
	var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[(2([0-4]\d|5[0-5])|1?\d{1,2})(\.(2([0-4]\d|5[0-5])|1?\d{1,2})){3} \])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return( re.test(testString) );
}

function validatePhone(testString){
	var re=/(\d{3}).*(\d{3}).*(\d{4})/;
	return( re.test(testString) );
}