// JavaScript (JQuery/JS) Document

$("document").ready(function(){
//Add JQuery code here (if you want)					

 $("form#checkout a.submit").click(function(event){
	event.preventDefault();
	if (validateCheckout() == true && confirm("Are you sure you wish to proceed?")) {	
	 $(this).parents("form").submit();
	}
 });

 $("form#signup_form a.submit").click(function(event){
	event.preventDefault();
	if (validateSignup() == true) {	
	 $(this).parents("form").submit();
	}
 });

 $("form#contactForm a.submit").click(function(event){
	event.preventDefault();
	if (validateContact() == true) {	
	 $(this).parents("form").submit();
	}
 });

 $("form#checkout input").each(function(){
	$(this).attr("id", $(this).attr("name"));	
 });

 $("form#signup_form input").each(function(){
	$(this).attr("id", $(this).attr("name"));	
 });

 $("form#contactForm input").each(function(){
	$(this).attr("id", $(this).attr("name"));	
 });

 $("form#contactForm textarea").each(function(){
	$(this).attr("id", $(this).attr("name"));	
 });


 $("input#fullname").blur(function(){
  $("input#name").val($(this).val());
 });
							
});

function validateCheckout() {
// 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 full name \r\n";
 }

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

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

 if (CheckEmail($("#email").val()) == "") { //Check if the string is empty once the blank spaces are trimmed out
  msg += "* Please check that the email address provided is valid \r\n";
 }

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

 if (trim($("#email").val()) != trim($("#email2").val())) { //Check if the string is empty once the blank spaces are trimmed out
  msg += "* Please make sure your email addresses match \r\n";
 }

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

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

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

 if (trim($("#country").val()) == "") { //Check if the string is empty once the blank spaces are trimmed out
  msg += "* Please enter your country for delivery \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 validateSignup() {
// A fairly typical validation function

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

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

 if ($("#signup_form input#nofill").val() != "") { //Check if the string is empty once the blank spaces are trimmed out
  msg += "* Unknown error occurred! \r\n";
 }

 if (CheckEmail($("#signup_form input#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 validateContact() {
// A fairly typical validation function

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

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

 if ($("#contactForm input#nofill").val() != "") { //Check if the string is empty once the blank spaces are trimmed out
  msg += "* Unknown error occurred! \r\n";
 }

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

 if (CheckEmail($("#contactForm input#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
}

