// JavaScript Document

function formValidator(){
	// Make quick references to the form fields
	var name = document.getElementById('name');
	var email = document.getElementById('email');
	var subject = document.getElementById('subject');
	var comments = document.getElementById('comments');
	
	// Check each input in the order that it appears in the form!
	if(isEmpty(name, "Please fill in the name!") == true){
		return false;
	} else if(isEmpty(email, "Please enter a valid email address!") == true){
		return false;
	} else if(emailValidation(email, "Please enter a valid email address!") == true){
		return false;
	} else if(isEmpty(subject, "Please fill in the subject!") == true){
		return false;
	} else if(isEmpty(comments, "Please fill in the comments!") == true){
		return false;
	} else {
	return true;
	}
}


// If the length of the element's string is 0 then display helper message
function isEmpty(elem, helperMsg){
	if(elem.value.length == 0){
		alert(helperMsg);
		elem.focus();
		return true;// set the focus to this input
	}
	return false;
}

// If the "@" is the first letters and the "." come before the @
function emailValidation(elem,helperMsg){
with (elem){
apos=value.indexOf("@");
dotpos=value.lastIndexOf(".");
if (apos<1||dotpos-apos<2) {
	alert(helperMsg);
  	return true;
  } else {
	  return false;
	  }
	}
}