/* 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.rocklincosmeticsurgery.com/wp-content/themes/rock/contact-es.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 (){
            $("#thankyou").html(msg);
            $("#thankyou").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
  // error checking
  if(name == "Nombre" || email == "Correo" || comments == "Comentarios")
  {
    error_exists = 1;
    $("#name").before(errmess("Rellene todos los campos requeridos."));
    $("#name").prev().slideDown("slow");  
  }
  else if(!emailReg.test(email))
  {
    error_exists = 1;
    $("#em").before(errmess("Introduzca un email v&aacute;lida."));
    $("#em").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 */ 