$(function ()
{
   var errorBox = $("#error-panel");
   if (!errorBox.hasClass("keep-error"))
      errorBox.fadeOut(1);
   
   $(".fresh", "#contact-right").focus(hidePreview);
   $(".contact-input", "#contact-right").blur(resetPreview);
   $(".contact-text", "#contact-right").blur(resetPreview);
   
   $("#contact-right form").submit(function()
   {
      var nameBox = $("#name");
      var emailBox = $("#email");
      var messageBox = $("#message");      
      
      var name = $.trim(nameBox.val());
      var email = $.trim(emailBox.val());
      var message = $.trim(messageBox.val());
      
      if ((name == "" || email =="" || message == "") ||
         (nameBox.hasClass("fresh") || emailBox.hasClass("fresh") || messageBox.hasClass("fresh")))
      {
         errorBox.html("A valid name, email, and message are all required before I can send your message")
            .fadeIn("slow").fadeTo(10000,1).fadeOut("slow");
         return false;
      }
      
      var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
      if (!emailPattern.test(email))
      {
         errorBox.html("Your email does not appear valid<br />Please correct it or we will not be able to get back in touch with you!")
            .fadeIn("slow").fadeTo(10000,1).fadeOut("slow");
         return false;
      }
            
      return true;
   });
});

function hidePreview()
{
   $(this).val("").removeClass("fresh").unbind("focus");      
}

function resetPreview()
{
   if ($(this).val() == "")
   {
      var val = $(this).attr("id");
      if (val != "phone")
         val += " *";
         
      $(this).val(val).addClass("fresh").focus(hidePreview);
   }
}

