/**
 * This function validates that the email input has valid contents.
 * @param {Object} element
 * @return {boolean} true if input value matches an email pattern.
 * @author KLE
 */
  function ValidateEmail(element)
  {
   var valid  = false;
   var match  = isValidEmailAddress(element.value);
   
   if ((match == false)|| (element.value ==""))
   {
    $(".emailOK").hide();
    $(".emailKO").fadeIn("slow");
    $(".error").fadeIn("slow");
   }
   else
   {
    $(".emailOK").fadeIn("slow");
    $(".emailKO").hide();
    $(".error").hide();
    
    var valid = true;
   }
   return valid;
  }	
  
  /**
 * This function validates that the element string matches an email pattern 
 * Ref: the email regexp used is from http://www.reynoldsftw.com/2009/03/live-email-validation-with-jquery/  
 * @param {Object} element
 * @return {boolean} true if input value matches an email pattern.
 * @author KLE
 */		
  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);
  }
  
 /**
 * This function checks if the subject contains a valid value.
 * @param {object} element
 * @return {boolean} true if the subject isn't the default choice, false otherwise 
 * @author KLE
 */	  
  
  function ValidateSubject(element)
  {
   var valid = false;
   var init  = "default";
   
   if (element.value == init)
   {
    $(".subOK").hide();
    $(".subKO").show();
    $(".error").fadeIn("slow");
   }
   else
   {
    $(".subOK").show();
    $(".subKO").hide();
    $(".error").hide();
    var valid = true;
   }
   return valid;
  }		
  
   /**
 * This function match if the textarea isn't empty
 * @param {object} element
 * @return {boolean} true if the textarea isn't empty
 * @author KLE
 */	 
  function textArea(element)
  {
   var valid  = false;
   
   if (element.value == "")
   {
    $(".areaOK").hide();
    $(".areaKO").show();
    $(".error").fadeIn("slow");
   }
   else
   {
    $(".areaOK").show();
    $(".areaKO").hide();
    $(".error").hide();
    var valid = true;
   }
   return valid;
  }	
  
   /**
 * This function match if all fields are valid
 * @return {boolean} true if all fields are valid
 * @author KLE
 */	 
  function valid()
   {
      if (( ValidateEmail(document.getElementById("email")) != true)||(ValidateSubject(document.getElementById("subject")) != true)||(textArea(document.getElementById("area")) != true))
      {
       $(".error").fadeIn("slow");
      }
      else
      {
       $(".error").hide();
      }
    }
