  jQuery.fn.hint = function () {
    return this.each(function (){
      // get jQuery version of 'this'
      var t = jQuery(this);
      // get it once since it won't change
      var title = t.attr('title');
      // only apply logic if the element has the attribute
      if (title) {
        // on blur, set value to title attr if text is blank
        t.blur(function (){
          if (t.val() == '') {
            t.val(title);
            t.addClass('blur');
          }
        });
        
        // on focus, set value to blank if current value
        // matches title attr
        t.focus(function (){
          if (t.val() == title) {
            t.val('');
            t.removeClass('blur');
          }
        });
    
        // clear the pre-defined text when form is submitted
        t.parents('form:first()').submit(function(){
          if (t.val() == title) { 
            t.val('');
            t.removeClass('blur');
          }
        });
    
        // now change all inputs to title
        t.blur();
      }
    });
  }


  $(document).ready(function() {
    $('input[title!=""]').hint();
    
    // Email Form Submit
    $('#emailFormSubmit').click( function() {
      if (($('#firstname').val() == "") || ($('#firstname').val() == $('#firstname').attr('title'))) {
        alert("Please enter your first name.");
        $('#firstname').focus();
      }
      else if (($('#surname').val() == "") || ($('#surname').val() == $('#surname').attr('title'))) {
        alert("Please enter your surname.");
        $('#surname').focus();
      }
      else if ( ($('#email').val() == "") || ($('#email').val() == $('#email').attr('title')) ) {
        alert("Please enter your email address");
        $('#email').focus();
      }
      else if (!isEmailAddr($('#email').val()))  {
        alert("Please enter a complete email address in the form: yourname@yourdomain.com");
        $('#email').focus();
      }
      else if ($('#email').val().length < 6) {
        alert("Please enter at least 6 characters in the \"email\" field.");
        $('#email').focus();
      }
      else {
        alert("Thank You!");
        $('#emailForm').submit();
      }
    });
    
    
    // Coupon Form Submit
    
    
    
    // Address Form Submit
    //$('#addressFormSubmit').click( function() {
      
      
    //});
    
    
    
    // Delivery Address Different
    $('#deliveryDifferent').click( function() {
      if ($('#deliveryDifferent:checked').val() != undefined) {
        $('#deliveryInformation').removeClass('noshow');
        $('#deliveryInformation').addClass('show');
      }
      else {
        $('#deliveryInformation').removeClass('show');
        $('#deliveryInformation').addClass('noshow');
      }
    });
    
    
    
    // Billing Country is USA
    $('#billingCountry').change( function() {
      if ($('#billingCountry').val() == 223) {
        $('#billingState').removeClass('noshow');
        $('#billingState').addClass('show-row');
      }
      else {
        $('#billingState').removeClass('show-row');
        $('#billingState').addClass('noshow');
      }
    });
    
    
    // Delivery Country is USA
    $('#deliveryCountry').change( function() {
      if ($('#deliveryCountry').val() == 223) {
        $('#deliveryState').removeClass('noshow');
        $('#deliveryState').addClass('show-row');
      }
      else {
        $('#deliveryState').removeClass('show-row');
        $('#deliveryState').addClass('noshow');
      }
    });
    
  });
  
  
  
  
  function isEmailAddr(email) {
    var result = false;
    var theStr = new String(email);
    var index = theStr.indexOf("@");
    if (index > 0) {
      var pindex = theStr.indexOf(".",index);
      if ((pindex > index+1) && (theStr.length > pindex+1))
        result = true;
    }
    return result;
  }
  
  function validateCheckoutForm(theForm) {
    if (theForm.customerFName.value == "") {
      alert("Please fill in your firstname.");
      theForm.customerFName.focus();
      return(false);
    }    
    else if (theForm.customerSName.value == "") {
      alert("Please fill in your surname.");
      theForm.customerSName.focus();
      return(false);
    }
    else if (theForm.customerEmail.value == "") {
      alert("Please fill in your email address.");
      theForm.customerEmail.focus();
      return(false);
    }
    else if (!isEmailAddr(theForm.customerEmail.value))  {
      alert("Please enter a complete email address in the form: yourname@yourdomain.com");
      theForm.customerEmail.focus();
      return(false);
    }
    else if (theForm.billingAddress.value == "") {
      alert("Please fill in your billing address.");
      theForm.billingAddress.focus();
      return(false);
    }
    else if (theForm.billingCity.value == "") {
      alert("Please fill in your billing town/city.");
      theForm.billingCity.focus();
      return(false);
    }
    else if (theForm.billingPostCode.value == "") {
      alert("Please fill in your billing postcode.");
      theForm.billingPostCode.focus();
      return(false);
    }
    else if (theForm.billingCountry.value =="") {
      alert("Please choose a country.");
      theForm.billingCountry.focus();
      return(false);
    }
    else if ( ($('#billingCountry').val() == 223) && (theForm.billingState.value == "") ) { // USA
      alert("Please fill in your billing state.");
      theForm.billingState.focus();
      return(false);
    }
    
    
    else if (($('#deliveryDifferent:checked').val() != undefined) && (theForm.deliveryFName.value == "")) {
      alert("Please fill in your delivery firstname.");
      theForm.deliveryFName.focus();
      return(false);
    }
    else if (($('#deliveryDifferent:checked').val() != undefined) && (theForm.deliverySName.value == "")) {
      alert("Please fill in your delivery surname.");
      theForm.deliverySName.focus();
      return(false);
    }
    else if (($('#deliveryDifferent:checked').val() != undefined) && (theForm.deliveryAddress.value == "")) {
      alert("Please fill in your delivery address.");
      theForm.deliveryAddress.focus();
      return(false);
    }
    else if (($('#deliveryDifferent:checked').val() != undefined) && (theForm.deliveryCity.value == "")) {
      alert("Please fill in your delivery town/city.");
      theForm.deliveryCity.focus();
      return(false);
    }
    else if (($('#deliveryDifferent:checked').val() != undefined) && (theForm.deliveryPostCode.value == "")) {
      alert("Please fill in your delivery postcode.");
      theForm.deliveryPostCode.focus();
      return(false);
    }
    else if (($('#deliveryDifferent:checked').val() != undefined) && (theForm.deliveryCountry.value == "")) {
      alert("Please choose a country.");
      theForm.deliveryCountry.focus();
      return(false);
    }    
    else if ( ($('#deliveryDifferent:checked').val() != undefined) && ($('#deliveryCountry').val() == 223) && (theForm.deliveryState.value == "") ) { // USA
      alert("Please fill in your delivery state.");
      theForm.deliveryState.focus();
      return(false);
    }
    
    else {
      return(true);
    }
  }
      
      
      
  function validateCouponForm(theForm) {
    if (theForm.couponCode.value == "") {
      alert("Please fill in the coupon code.");
      theForm.couponCode.focus();
      return(false);
    }
    else {
      return(true);
    }
  }








