/* CONTACT FORM HANDLING
Installation: Modify the areas with a *** next to the field to the address for your site -David
 */

function sendwithajax()
{

  var error_exists;

  // if there are previous errors, clear them
  if($("div.error").size() > 0)
  {
    $("div.error").slideUp("medium",function () {
      $("div.error").remove();
      error_exists = validateInput();
      if(!error_exists)
        sendInfo();
    });
  }
  else // no previous errors
  {
      error_exists = validateInput();
      if(!error_exists)
        sendInfo();
  }

  return false;
}

function sendInfo()
{
  var name = $("#name").val();
  var email = $("#em").val();
  var phone = $("#po").val();
  var femail = $("#email").val();
  var fphone = $("#phone").val();
  var comments = $("#comments").val();

         $.ajax({
         type: "POST",
         url: "http://www.uniseo.ca/demo/wp-content/themes/demo/contact.php", // ***** Change to current site's url
         data: "name="+name+"&email="+email+"&phone="+phone+"&comments="+comments+"&submit=submit&femail="+femail+"&fphone="+fphone,
         success: function(msg){
            $("#contactbox").slideUp("medium",function (){
            $("#contactbox").html(msg);
            $("#contactbox").css("text-align","center");
            $("#contactbox").slideDown("medium");
           });
         }
        });
}

function validateInput()
{

  var name = $("#name").val();
  var email = $("#em").val();
  var phone = $("#po").val();
  var comments = $("#comments").val();
  
  var error_exists = 0;
  var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
  
  // error checking
  
  // check name
  if(name == "Name")
  {
    error_exists = 1;
    $("#name").before(errmess("Please enter your name."));
    $("#name").prev().slideDown("slow");  
  }
  
  // check email
  if(email == "Email")
  {
    error_exists = 1;
    $("#em").before(errmess("Please enter your email address."));
    $("#em").prev().slideDown("slow");
  }
  else if(!emailReg.test(email))
  {
    error_exists = 1;
    $("#em").before(errmess("Please enter a valid email address."));
    $("#em").prev().slideDown("slow");
  }
  
  // check comments
  if(comments == "Comments")
  {
    error_exists = 1;
    $("#comments").before(errmess("Please enter a comment."));
    $("#comments").prev().slideDown("slow");
  }
  
  return error_exists;

}

  function errmess(message)
  {
    return "<div class=\"error\">"+message+"</div>";
  }

	function clearText(thefield){
	if (thefield.defaultValue==thefield.value)
	thefield.value = ""
	}

	function getText(thefield){
	if (thefield.value == "")
	thefield.value = thefield.defaultValue
	}
	
/* END CONTACT FORM HANDLING */
