/*

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(){

	//form has been submitted, hide the success or unsuccess error messages

	if(document.getElementById('successError')){

		document.getElementById('successError').style.display = "none";

	}

	if(	document.getElementById('UnsuccessError')){

		document.getElementById('UnsuccessError').style.display = "none";

	}

	//first establish all the variables

	var nameField, emailField, commentsField, testDigit, allow, phoneNumber;

	//turn the errors variable into a new array with a length of 5

	var errors = new Array(3);

	//assign the values to each variable corresponding to the appropriate field

	nameField = document.contact_form.Name.value;

	emailField = document.contact_form.Email.value;

	commentsField = document.contact_form.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 = "block";

		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 = "block";

		}else{

			document.getElementById('nameError').style.display = "none";

			errors[0] = '';

		}

	}

//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('txtError').style.display = "block";

		errors[1] = 'yes';

	}else{

		document.getElementById('txtError').style.display = "none";

		errors[1] = '';

	}

	//validate the email field.

	//make sure the email field is not empty

	if(emailField == ''){

		document.getElementById('emailError').style.display = "block";

		errors[2] = 'yes';

	}else{

		//the field is not empty so time to compare it against the email reg expression

		if(!emailPattern.test(emailField)){

			errors[2] = 'yes';

			document.getElementById('emailError').style.display = "block";

		}else{

			document.getElementById('emailError').style.display = "none";

			errors[2] = '';

		}

	}

	//check the errors array

	for(i=0; i<errors.length; 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;

	}

}
