// JavaScript Document

function formValidator(){
	// collect field data and save it to a variable to use throughout the functions
	var otherName = document.getElementById('sender');
    var emailAddress = document.getElementById('senderaddr');
	var message = document.getElementById('comments');
	
	// Check each input in the order that it appears in the form!
		if(isAlphabet(otherName, "Please enter your name ")){
					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 all data entries is a Alpha character
function isAlphabet(field, helperMsg){
	var alphaExp = /^[a-zA-Z]+$/;
	if(field.value.match(alphaExp)){
		return true;
	}else{
		alert(helperMsg);
		field.focus();
		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;
}