// JavaScript (JQuery/JS) Document

$("document").ready(function(){
//Add JQuery code here (if you want)					
	$("form#contact .submit").click(function(event){
 	 event.preventDefault();
	 if (checkContact() == true) {
	  $("form#contact").submit();
	 }
	});

	$("form#signup input:button").click(function(event){
 	 event.preventDefault();
	 if (checkSignup() == true) {
	  $("form#signup").submit();
	 }
	});
});

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

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

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

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

 if (trim($("#contact #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 checkSignup() {
// A fairly typical validation function

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

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

 if (CheckEmail($("#signup_email").val()) == false) { //Check if the string looks like a typical email address, also trims out blank spaces
  msg += "* Please enter your email \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
}
