// JavaScript (JQuery/JS) Document

$("document").ready(function(){
//Add JQuery code here (if you want)					
	$("#contactForm a.contact_submit").click(function(event){
		event.preventDefault();
		if (checkContact() != false) { $("#contactForm").submit(); }
	});						
});


function checkContact() {
// A fairly typical validation function

var msg = ""; //Set initial error message to blank

 if (trim($("#fullname").val()) == "") { //Check if the string is empty once the blank spaces are trimmed out
  msg += "* Please enter your name \r\n";
 }

 if (CheckEmail($("#email").val()) == false) { //Check if the string looks like a typical email address, also trims out blank spaces
  msg += "* Please enter your email address \r\n";
 }

 if (trim($("#message").val()) == "") { //Check if the string is empty once the blank spaces are trimmed out
  msg += "* Please enter your message \r\n";
 }

 if (msg != "") { //If the message isn't blank anymore...
  alert("Errors were detected...\r\n\r\n" + msg); //Display the message
  return false; //Tell the form not to submit
 }

return true; //Otherwise tell the form validation is successful

}

function trim(s){
//Function for trimming out blank spaces
return s.replace(/^\s*(.*?)\s*$/,"$1")
}

function CheckEmail(email) {
AtPos = email.indexOf("@") //Check if theres an "@" symbol
StopPos = email.lastIndexOf(".") //Check if theres an "." symbol, get the position of the last one if there is
Message = "";

if (trim(email) == "") {
Message = "Not a valid Email address" + "\n"
}

if (AtPos == -1 || StopPos == -1) { // If theres either no "@" or no "." in the string..
Message = "Not a valid email address" //Add an error to the error variable
}

if (StopPos < AtPos) { // If the "@" is after the last "."
Message = "Not a valid email address" //Add an error to the error variable
}

if (StopPos - AtPos == 1) { //If the string is only "@." then 
Message = "Not a valid email address" //Add an error to the error variable
}

if (Message != '') { //if the error message is NOT blank
return false;	//Return false - bad email address
}
return true; //Return true - email address "looks" okay
}
