// JavaScript Document

function formValidator(){
	// collect field data and save it to a variable to use throughout the functions
	var Name = document.getElementById('name');
    var emailAddress = document.getElementById('emailaddress');
	var message = document.getElementById('message');
	
	// Check each input in the order that it appears in the form!
		if(isBlank(Name, "Please enter a name so we can contact you")){
					if(emailValidation(emailAddress, "E-mail address must be a valid format")){
						if(isBlank(message, "You forgot to enter your message")){
									
								return true;
						}
					}
	             }
	return false;
	
}


// function to ensure that the email address entered is in correct format
function emailValidation(field, helperMsg){
	var emailValid = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if(field.value.match(emailValid)){
		return true;
	}else{
		alert(helperMsg);
		field.focus();
		return false;
	}

	
}


// function to ensure that the field is not left blank
function isBlank(field, helperMsg){
	if(field.value.length == 0){
		alert(helperMsg);
		field.focus(); // set the focus to this input
		return false;
	}
	return true;
}