<!--


/* Data validation script

To set up, place similar arrays on your form page :

validatePhones 	= new Array("phone","cell","fax","pager","phone2")
validateThese 	= new Array("name", "address", "city" ,"zip", "phone")
validateNumbers = new Array("zip")
validateRadios 	= new Array("role")
validateSelect 	= new Array("state","relationship")
validateDates 	= new Array("date")


*/
validatePhones 		= new Array()
validateThese 		= new Array()
validateNumbers 	= new Array()
validateRadios 		= new Array()
validateSelect 		= new Array()
validateDates 		= new Array()
validateEmails 		= new Array()
validateSize 		= new Array()
validateForcedSize 	= new Array()

requiredMsg 		= "- Please fill out all fields marked as required\n"
numberMsg 			= "- You entered a value that was not a number in a field that only accepts numbers\n"
phoneMsg 			= "- Please be sure that the Phone Number fields are in the correct format - (xxx) xxx-xxxx\n"
dateMsg 			="- Please be sure that the Date fields are in the correct format - mm/dd/yyyy\n"
emailMsg 			= "- Please be sure that the email address that you entered is in the correct format.\n"
sizeMsg 			= "- Please enter the correct number of characters for this field\n"
forcedSizeMsg 		= "- Please enter the correct number of characters for this field\n"

function validateForm(theForm){



	errors="The following errors occured:\n"
	isError = false
	dateErrors=""
	firstError = ""
	//////////////////////
	emptyError = false
	numberError = false
	radioError = false
	phoneError = false
	roleError = false
	sizeError = false
	dateError = false
	emailError = false
	forcedSizeError = false
	
	//validate the following items to because they are required
		
	if(validateThese.length > 0){
		for (i = 0 ; i < validateThese.length; i++){
			if(theForm[validateThese[i]].value == ""){
				
				emptyError = true
				isError = true
				if(firstError == ""){firstError = validateThese[i]}
			}
		
		}
	}
	
	//validation to check that numbers are entered in these fields 
	
	
	if(validateNumbers.length > 0){	
	
		for (i = 0 ; i < validateNumbers.length; i++){
			if (isNaN(theForm[validateNumbers[i]].value)){
				numberError = true
				isError = true
				if(firstError == ""){firstError = validateNumbers[i]}
			}
		
		}
	}
	
	//validate radio button groups to make sure something has been selected
		
	
	if(validateRadios.length > 0){
	  for (i = 0 ; i < validateRadios.length; i++){
		theGrp = theForm[validateRadios[i]]
		thisCheckCount = 0
		if(theGrp.length){
			for(z=0; z < theGrp.length; z++){
				
				if (theGrp[z].checked){
				thisCheckCount++
				}
			}	
			if (thisCheckCount == 0 ){
				emptyError = true
				isError = true
				if(firstError == ""){firstError = validateRadios[i]}
			}
		}else{
			if(!theGrp.checked){
			
				emptyError = true
				isError = true	
			}
		}
	
	  }
	}
	
	//validate select box
	
	
	
	if(validateSelect.length > 0){
	 	for(i=0; i < validateSelect.length; i ++){
		  theFormField = theForm[validateSelect[i]]
		  if(theFormField.length == 0) {
		  	emptyError = true
			isError = true
			if(firstError == ""){firstError = validateSelect[i]}
		  }else if(theFormField[theFormField.selectedIndex].value == ""){
			  emptyError = true
			  isError = true
			  if(firstError == ""){firstError = validateSelect[i]}
		  }
		}
	}
	
	
	//validate phone numbers
	
	if(validatePhones.length > 0){
	 for(i=0; i < validatePhones.length; i++){
		theFormField = theForm[validatePhones[i]]
		if(theFormField.value != ""){
		  //call phoneValidator function which is located in a separate function
		  phoneTest = phoneValidator(theFormField.value) 
		  if(phoneTest=="false" && theFormField.value.length != 0){
			  
                          phoneError = true
			  isError = true
			  if(firstError == ""){firstError = validatePhones[i]}
                         
		  }else if(theFormField.value.length != 0){
		  	theFormField.value = phoneTest
		  }
		}
	  }
	}
	
	
	//validate allowed field length
	
	if(validateSize.length > 0) {
		for(i=0; i < validateSize.length; i++) {
	
			
                        var SizeList = validateSize[i].split("|")
				theFormField = theForm[SizeList[0]]
				SizeAllowed = SizeList[1]
			if(theFormField.value != "") {
                                SizeEntered = theFormField.value
				
				if (SizeEntered.length > SizeAllowed) {
					sizeError = true
					isError = true
					firstError = SizeList[0]
				}
			}
	
		}	
	}
	
	//validate forced size
		
		if(validateForcedSize.length > 0) {
		for(i=0; i < validateForcedSize.length; i++) {
	
			
         var ForcedSizeList = validateForcedSize[i].split("|")
				theFormField = theForm[ForcedSizeList[0]]
				SizeRequired = ForcedSizeList[1]
			if(theFormField.value != "") {
                                forcedSizeEntered = theFormField.value
				
				if (forcedSizeEntered.length != SizeRequired) {
					forcedSizeError = true
					isError = true
					firstError = ForcedSizeList[0]
				}
			}
	
		}	
	}
	
	
	//validate date fields
	/*According to the Gregorian calendar, 
	which is the civil calendar in use today, 
	years evenly divisible by 4 are leap years, 
	with the exception of centurial years that 
	are not evenly divisible by 400. Therefore, 
	the years 1700, 1800, 1900 and 2100 are not l
	eap years, but 1600, 2000, and 2400 are leap years.
	*/
	if(validateDates.length > 0){
	 for(i=0; i < validateDates.length; i ++){
	 	
		theFormField = theForm[validateDates[i]]
		if(theFormField.value != ""){	
			isLeapYear = false
			var dateList = theFormField.value.split("/")
			if (dateList.length != 3) {
				dateErrors += "\n-You must enter a valid date\n In the form 'MM/DD/YYYY'"
				dateError = true
				isError = true
				if(firstError == ""){firstError = validateDates[i]}
			}else{
				mm = dateList[0]
				dd = dateList[1]
				yy = dateList[2]
			
				if (isNaN(mm)){
					//errors+="\n-You must enter a number for the month"
					dateError = true
					isError = true
					if(firstError == ""){firstError = validateDates[i]}
				}else{
					if(mm<1 || mm>12){
					//errors+="\n-You must enter a valid month between 1 and 12"
					dateError = true
					isError = true
					if(firstError == ""){firstError = validateDates[i]}
					}
				}
				if (isNaN(dd)){
					//errors+="\n-You must enter a number for the day"
					dateError = true
					isError = true
					if(firstError == ""){firstError = validateDates[i]}
				}else{
					
					maxDays = 31
					if(!isNaN(mm)){
						if(mm==4 || mm== 6 || mm == 9 || mm==11){
							maxDays = 30
						}else if(mm==2){
							maxDays = 29
						}
					}
					if(dd<1 || dd>maxDays){
					
					dateError = true
					isError = true
					if(firstError == ""){firstError = validateDates[i]}
					}
				}
				if (isNaN(yy)){
					//errors+="\n-You must enter a number for the year"
					dateError = true
					isError = true
					if(firstError == ""){firstError = validateDates[i]}
				}else{
					if(yy==""){
					//errors+="\n-You must enter a valid year"
					dateError = true
					isError = true
					if(firstError == ""){firstError = validateDates[i]}
					}else if(yy.length != 4){
					//errors+="\n-You must enter a four digit year"
					dateError = true
					isError = true
					if(firstError == ""){firstError = validateDates[i]}
					}else{
					// Valid year
					//Check for leap year
						if(yy % 4 == 0){
							//could be leap year
							isLeapYear = true
							if((yy % 100 == 0) && (yy % 400 != 0)){
								isLeapYear = false
							}
						}
						
					}
				
				}
				if((mm == 2) && (dd == 29) && !isLeapYear){
					dateError = true
					isError = true
					if(firstError == ""){firstError = validateDates[i]}
				}

				
				
			}
		}
		}
	  }
	  
	  //validate emails
	  //This could be done with regular expressions however certain versions of IE 5.x
	  //did not support them correctly.
	  if(validateEmails.length > 0){
		 for(i=0; i < validateEmails.length; i ++){
			theFormField = theForm[validateEmails[i]]
			if(theFormField.value != ""){
				if(theFormField.value.length < 5){
					emailError = true
					isError = true
					if(firstError == ""){firstError = validateEmails[i]}
				}else{
					atLoc1 = theFormField.value.indexOf('@')
					atLoc2 = theFormField.value.lastIndexOf('@')
					dotLoc = theFormField.value.lastIndexOf('.')
					
					if(atLoc1 != atLoc2){
						//two @ symbols
						emailError = true
						isError = true
						if(firstError == ""){firstError = validateEmails[i]}
					}else if(atLoc1==-1 || atLoc1 == 0 || atLoc1 == (theFormField.value.length-1)){
						//no @ symbols or it is the first or last character
						emailError = true
						isError = true
						if(firstError == ""){firstError = validateEmails[i]}
					}else if(dotLoc <= (atLoc1 +1)){
					//the only dot is before the @ symbol
						emailError = true
						isError = true
						if(firstError == ""){firstError = validateEmails[i]}
					}else if(dotLoc == 0 || dotLoc == (theFormField.value.length-1)){
					// dot is the last character
						emailError = true
						isError = true
						if(firstError == ""){firstError = validateEmails[i]}
					}
				
				}
			
			}
		}
	  }	
	
	
	if(isError){
		if(emptyError){
			errors += requiredMsg + "\n"
		}
		if(numberError){
			errors += numberMsg + "\n"
		}
		if(phoneError){
			errors +=  phoneMsg+ "\n"
		}
		if (dateError){
			errors += dateMsg + "\n"
			//errors += dateErrors
		}
		if (emailError){
			errors += emailMsg + "\n"
		}
		if (sizeError){
			errors += sizeMsg + "\n"
		}
		if (forcedSizeError){
			errors += forcedSizeMsg + "\n"
		}
		alert(errors)
		
                if(firstError != ""){
                    if(theForm[firstError].type){
                            if(theForm[firstError].type == "text" || theForm[firstError].type == "textarea" || theForm[firstError].type == "select-one" || theForm[firstError].type == "select-multiple"){
                                            theForm[firstError].focus()
                            }
                    }

                    if(theForm[firstError].type== "text" || theForm[firstError].type== "password"){
                            theForm[firstError].select()
                    }
                }
		return false
	}else{
		if(doSubmit(theForm)){
			return true
		}else{
			//alert("An Error Occured")
			return false
		}
	}
}


function phoneValidator(field){
			
			//phone: /^(\d{10}|\d{3}-\d{3}-\d{4}|\(\d{3}\)\s*\d{3}-\d{4})$/
			//This function originally started out using a regular expression
			//but certain versions of IE 5.x did not support it correctly so it was
			//changed to be more widely supported
			numCount = 0
			newString = "("
			newString = ""	// REMOVE PARENS SO WE CA SEARCH BY AREA CODE
						  
			
			for(var i=0; i<field.length; i ++){
				if (newString.length == 3){
					newString += "-" 	// REMOVE PARENS SO WE CA SEARCH BY AREA CODE - was ") "
				}else if (newString.length==7){ // REMOVE PARENS SO WE CA SEARCH BY AREA CODE - was ") "
					newString += "-"
				}
				thisChar = field.charAt(i)
					if(!isNaN(thisChar) && thisChar !=" "){
						numCount++
						newString += thisChar
						
					}
			}
			if (numCount == 10){
				 
				return newString
				
			}else{
				return "false"
			}
			//var rex=new RegExp(/^(\d{10}|\d{3}-\d{3}-\d{4}|\(\d{3}\)\s*\d{3}-\d{4})$/);
			//alert(field)
			/*
			if (rex.test(field)){
				return true
			}else{
				return false
			}
			*/
		}
		
		
		function doSubmit(theForm){
			return true
		}