/* 
* JavaScript Document
* gregmerriman.co.uk
* jquery.validate.conatct.js
* 
* includes/js/jquery.validate.conatct.js
* 
* Author    :   Greg Merriman
* Copyright	:		Copyright 2009 (c) Greg Merriman Web Development
* Created   :   Wed 18 Nov 2009
* Modified  :   Wed 18 Nov 2009
* 
*/

 
// When the DOM is ready...
$(function(){

  //global vars
  var contactform = $("#contactform");
  
  var name = $("#contactform #name");
  var nameInfo = $("#contactform #nameInfo");
  
  var email = $("#contactform #email");
  var emailInfo = $("#contactform #emailInfo");
  
  var website = $("#contactform #website");
  var websiteInfo = $("#contactform #websiteInfo");
  
  var organisation = $("#contactform #organisation");
  var orgInfo = $("#contactform #orgInfo");
  
  var message = $("#contactform #message");
  var messageInfo = $("#contactform #messageInfo");
  
  var verification = $("#contactform #code");
  var verifyInfo = $("#contactform #verifyInfo"); 
  
   
  //validate the name input
  function validateName()
  { 
    //if it's NOT valid
    if($("#contactform #name").val().length <= 3)
    {
      name.addClass("error");
      name.removeClass("ok");
      nameInfo.text("Enter your fullname...");
      nameInfo.addClass("error");  
      nameInfo.removeClass("ok");
      return false;
    }
    //if it's valid
    else
    {
      name.addClass("ok");
      name.removeClass("error");
      nameInfo.text("Great name! I find Greg a bit boring...");
      nameInfo.addClass("ok");
      nameInfo.removeClass("error");
      return true;
    }
  }
  
  
  //validate the email input
  function validateEmail()
  { 
    //if it's NOT valid
    if (($("#contactform #email").val().length <= 3) || (isValidEmailAddress($("#contactform #email").val()) != true)) 
    {
      email.addClass("error");
      email.removeClass("ok");
      emailInfo.text("Enter a valid email address...");
      emailInfo.addClass("error");  
      emailInfo.removeClass("ok");
      return false;
    }
    //if it's valid
    else
    {
      email.addClass("ok");
      email.removeClass("error");
      emailInfo.text("Super. We should continue this conversation...");
      emailInfo.addClass("ok");
      emailInfo.removeClass("error");
      return true;
    }
  }
  
  
  //validate the website input
  function validateWebsite()
  { 
    //if it's NOT valid
    if (($("#contactform #website").val().length <= 3) || (isValidWebsite($("#contactform #website").val()) != true)) 
    {
      website.addClass("error");
      website.removeClass("ok");
      websiteInfo.text("Enter you web address (if you have one)...");
      websiteInfo.addClass("error");  
      websiteInfo.removeClass("ok");
      return false;
    }
    //if it's valid
    else
    {
      if ($("#contactform #website").val() != 'www.yourwebsite.com')
      {
        website.addClass("ok");
        website.removeClass("error");
        websiteInfo.text("Interesting choice. I'll stop by sometime...");
        websiteInfo.addClass("ok");
        websiteInfo.removeClass("error");
        return true;
      }
    }
  }
  
  
  //validate the organisation input
  function validateOrg()
  { 
    //if it's NOT valid
    if ($("#contactform #organisation").val().length < 3) 
    {
      organisation.addClass("error");
      organisation.removeClass("ok");
      orgInfo.text("Enter you company or organisation name...");
      orgInfo.addClass("error");  
      orgInfo.removeClass("ok");
      return false;
    }
    //if it's valid
    else
    {
      organisation.addClass("ok");
      organisation.removeClass("error");
      orgInfo.text("Wow! You guys rock!");
      orgInfo.addClass("ok");
      orgInfo.removeClass("error");
      return true;
    }
  }
  
  
  //validate the message input
  function validateMessage()
  { 
    //if it's NOT valid
    if ($("#contactform #message").val().length < 3) 
    {
      message.addClass("error");
      message.removeClass("ok");
      messageInfo.text("Enter your message...");
      messageInfo.addClass("error");  
      messageInfo.removeClass("ok");
      return false;
    }
    //if it's valid
    else
    {
      message.addClass("ok");
      message.removeClass("error");
      messageInfo.text("OK. Get it sent and we'll see what happens...");
      messageInfo.addClass("ok");
      messageInfo.removeClass("error");
      return true;
    }
  }
  
  
  //validate the verification input
  function validateVerification()
  { 
    //if it's NOT valid
    if ($("#contactform #code").val().length != 5) 
    {
      verification.addClass("error");
      verification.removeClass("ok");
      verifyInfo.text("Enter the verification code...");
      verifyInfo.addClass("error");  
      verifyInfo.removeClass("ok");
      return false;
    }
    //if it's valid
    else
    {
      verification.addClass("ok");
      verification.removeClass("error");
      verifyInfo.text("Thank you. I'll check the code shortly...");
      verifyInfo.addClass("ok");
      verifyInfo.removeClass("error");
      return true;
    }
  }
  
  
  // helper functions
  
  function isValidEmailAddress( emailAddress )
  {
    var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    return pattern.test(emailAddress);
  }
  
  function isValidWebsite( url )
  {
    var pattern = new RegExp(/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{4}/);
    return pattern.test(url);
  }
  
  
  //now manage form events
  
  //Validate the name field in: blur and keyup events.
  //Validate the email field in: blur and keyup events.
  //Validate the website field in: blur event.
  //Validate the organisation field in: blur event.
  //Validate the message field in: blur event.
  //Validate all required fields in: submit form event.          
  
  //On blur
  name.blur(validateName);
  email.blur(validateEmail);
  website.blur(validateWebsite);
  organisation.blur(validateOrg);
  message.blur(validateMessage);
  verification.blur(validateVerification);
  
  //On key press
  name.keyup(validateName);
  email.keyup(validateEmail);
  //website.keyup(validateWebsite);
  //organisation.keyup(validateOrg);
  //message.keyup(validateMessage)
  
  
  //add some extra html for returning error on submission
  $('input[type="submit"]').after('<p class="couldnotsend"></p>');
  $("#contactform p.couldnotsend").hide();
  
  
  var count = 0;
  
  //On Submitting
  contactform.submit(function()
  {
    count = count + 1;
     
    if (validateName() & validateEmail() & validateMessage() & validateVerification())
    {
      $('input[type="submit"]', this).replaceWith('<p class="sending"><em>Sending your message...</em></p>');
      return true;
    }
    else
    {
      $("#contactform p.couldnotsend").html('<em>Your message could not be sent (sending attempt ' + count + '). Check the form for errors...</em>');
      $("#contactform p.couldnotsend").show();
      return false;
    }
  });

});