//Form validation for the JRH Asset Management Contact Page
//Developer: Tait Chambers, WebDuck Designs 2007
//This is only the initial validation, assuming the information passes here, it will be validated again via PHP


/*
function to validate the contact form input, if each field validates correctly the form will submit. If there is an error, 
the error array will be added to and the form will not submit. The errors for the appropriate fields will display instead
*/
function contactValidate(){
	//first establish all the variables
	var nameField, emailField, phoneField, phoneField_2, phoneField_3, refField, commentsField, testDigit, allow, phoneNumber, testNumber, eDigit;
	//make the success error invisible
	document.getElementById('unSuccess').style.display = "none";
	
	//turn the errors variable into a new array with a length of 5
	var errors = new Array(5);
	//assign the values to each variable corresponding to the appropriate field
	nameField = document.contactForm.Name.value;
	emailField = document.contactForm.Email.value;
	phoneField = document.contactForm.Phone.value;
	phoneField_2 = document.contactForm.Phone_2.value;
	phoneField_3 = document.contactForm.Phone_3.value;
	commentsField = document.contactForm.Comments.value;
	//below are the regular expressions for looking validation comparison
	var pattern = /[0-9]/;  //regular expression looking for a digit between 0 and 9
	var emailPattern = /\w+@\w+\.\w{1,3}/;
	
	//validate the name field first
	//first check to make sure the field is not empty
	if(nameField == ''){
		document.getElementById('nameError').style.display = "inline";
		errors[0] = 'yes';
	}else{
		//check to see if a number was entered
		for(j=0; j<nameField.length; j++){
			testDigit = nameField.charAt(j);
			if(pattern.test(testDigit)){
				errors[0] = 'yes';
				break;
			}
		}
		if(errors[0] == 'yes'){
			document.getElementById('nameError').style.display = "inline";
		}else{
			document.getElementById('nameError').style.display = "none";
			errors[0] = '';
		}
	}
	
	
	//validate the phone fields. 
	//Make sure all three fields are not empty
	if(phoneField != '' || phoneField_2 != '' || phoneField_3 != ''){
		//check to make sure the proper amount of digits is entered in each field
		if(phoneField.length != 3 || phoneField_2.length != 3 || phoneField_3.length != 4){
			document.getElementById('phoneError').style.display = "inline";
			errors[2] = 'yes';
		}else{
			//the fields contain the proper amount of digits, check to make sure they are numbers
			//first combine all the fields into one big string so it can be run through making sure there are not non-digits
			phoneNumber = phoneField+phoneField_2+phoneField_3;
			for(i=0; i<phoneNumber.length; i++){
				testNumber = phoneNumber.charAt(i);
				if(!pattern.test(testNumber)){   //check to see if the current digit does NOT match the pattern. If it dont' then it is not a number
					errors[2] = 'yes';
					break;
				}
			}
			if(errors[2] == 'yes'){
				document.getElementById('phoneError').style.display = "inline";
			}else{
				document.getElementById('phoneError').style.display = "none";
				errors[2] = '';
			}
		}
	}
	
	//validate the comments field
	//make sure the field is not empty. Since it's a comments field it can really contain anything so there is not other validation
	if(commentsField == ''){
		document.getElementById('commentsError').style.display = "inline";
		errors[3] = 'yes';
	}else{
		document.getElementById('commentsError').style.display = "none";
		errors[3] = '';
	}
	
	//validate the email field.
	//make sure the email field is not empty
	if(emailField == ''){
		document.getElementById('emailError').style.display = "inline";
		errors[4] = 'yes';
	}else{
		//the field is not empty so time to compare it against the email reg expression
		if(!emailPattern.test(emailField)){
			errors[4] = 'yes';
			document.getElementById('emailError').style.display = "inline";
		}else{
			document.getElementById('emailError').style.display = "none";
			errors[4] = '';
		}
	}
	
	//check the errors array
	for(i=0; i<5; i++){
		if(errors[i] == 'yes'){
			allow = 'no';
		}
	}
	
	//check the allow flag. This flag determines whether or not to submit the information
	if(allow == 'no'){
		return false;
	}else{
		return true;
	}



}