$(function() {

$('.error').hide();
	
	function verif(email) 
	{
		
	var reg = /^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,3}$/i;
	var resultat = reg.test(email);
	//alert(resultat);
	//return (reg.exec(email)!=null);
	return resultat;
	
	} 
	
	
	
  $("#submit_sendTofriend").click(function() {
							  
		// validate and process form
		// first hide any error messages
    
	$('.error').hide();
	
	$("input#your_name").css("border", "solid #388FAA 1px");
	$("input#name_friend").css("border", "solid #388FAA 1px");	
	$("input#email_friend").css("border", "solid #388FAA 1px");	
	$("input#id_property").css("border", "solid #388FAA 1px");	
	$("input#captcha_sendTofriend").css("border", "solid #388FAA 1px");	
		
	var your_name = $("input#your_name").val();
	var name_friend = $("input#name_friend").val();
	var email_friend = $("input#email_friend").val();
	var id_property = $("input#id_property").val();
	var captcha_sendTofriend = $("input#captcha_sendTofriend").val();
	
	if (your_name == "") {
      $("label#your_name_error").show();
      $("input#your_name").focus(); 
	  $("input#your_name").css("border", "solid #F00 1px");
      return false;
    }
	
	if (name_friend == "") {
      $("label#name_friend_error").show();
      $("input#name_friend").focus(); 
	  $("input#name_friend").css("border", "solid #F00 1px");
      return false;
    }
	
	
	if (email_friend == "" || !verif(email_friend)) {
      $("label#email_friend_error").show();
      $("input#email_friend").focus();
  	  $("input#email_friend").css("border", "solid #F00 1px");
	  
      return false;
    }
		
	if (captcha_sendTofriend == "") {
      $("label#captcha_sendTofriend_error").show();
      $("input#captcha_sendTofriend").focus(); 
	  $("input#captcha_sendTofriend").css("border", "solid #F00 1px");
      return false;
    }


		var dataString = 'your_name=' + your_name + '&name_friend=' + name_friend + '&email_friend=' + email_friend+ '&captcha=' + captcha_sendTofriend + '&id_property=' + id_property;


	 $('#form_sendTofriend').html("<div id='loading' align='center'><img src='images/loading.gif' width='32' height='32'><h2>Merci de patienter un peu</h2></div>");

	
	$.ajax({
		   
      type: "POST",
      url: "ajax/sendTofriend.php",
      data: dataString,
      success: function(result) {


					if(result == "SENT"){
				  
						$('#form_sendTofriend').html("<div id='confirm_send_form' style='left:60px'></div>");
						$('#confirm_send_form').html("<h2>Merci, votre message a été <br />bien envoyé</h2>")
						.append("<p>Nous vous contacterons dans <br />les plus brefs délais !</p>")
						.hide()
						.fadeIn(1500, function() {
						$('#confirm_send_form').append("<img id='checkmark' src='images/check.png' width='35' height='35' />");
						});
			  
									
									}
			  
			  else if(result == "NOSENT-CAPTCHA") {
								
					 $('#form_sendTofriend').html("<div id='confirm_send_form' style='left:60px'></div>");
					$('#confirm_send_form').html("<h2 style='color:#F00'>Oops, le code est mal copié !</h2>")
					.append("<p>Merci de bien copier le code anti-spam <br /></p>")
					.hide()
					.fadeIn(1500, function() {
					$('#confirm_send_form').append("<img id='checkmark' src='images/error.png' width='35' height='35' />");
					});
				  
				  
				  }
			  
			  else {
				
					 $('#form_sendTofriend').html("<div id='confirm_send_form' style='left:60px'></div>");
					$('#confirm_send_form').html("<h2 style='color:#F00'>Echec d'envoi du formulaire</h2>")
					.append("<p>Merci de remplir le formulaire <br />ultérierement !</p>")
					.hide()
					.fadeIn(1500, function() {
					$('#confirm_send_form').append("<img id='checkmark' src='images/error.png' width='35' height='35' />");
					});
				  
				  
				  }
	  
	  }
	  
     });

    return false;
	});
});
