$(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_suggest").click(function() {
							  
		// validate and process form
		// first hide any error messages
    
	$('.error').hide();
	
	
	$("select#category").css("border", "solid #388FAA 1px");
	$("textarea#description").css("border", "solid #388FAA 1px");
	$("input#first_name").css("border", "solid #388FAA 1px");
	$("input#last_name").css("border", "solid #388FAA 1px");
	$("select#situation").css("border", "solid #388FAA 1px");	
	$("input#surface").css("border", "solid #388FAA 1px");
	$("input#price").css("border", "solid #388FAA 1px");
	$("input#city").css("border", "solid #388FAA 1px");
	$("input#tel").css("border", "solid #388FAA 1px");
	$("input#mobile").css("border", "solid #388FAA 1px");	
	$("input#email").css("border", "solid #388FAA 1px");
	$("input#captcha").css("border", "solid #388FAA 1px");
		
	var operation = $("input#operation").val();
	var category = $("select#category").val();	
	var situation = $("select#situation").val();	
	var first_name = $("input#first_name").val();
	var last_name = $("input#last_name").val();
	var description = $("textarea#description").val();
	var cp = $("input#cp").val();
	var price = $("input#price").val();
	var surface = $("input#surface").val();
	var city = $("input#city").val();
	var tel = $("input#tel").val();
	var mobile = $("input#mobile").val();
	var email = $("input#email").val();
	var captcha = $("input#captcha").val();
	
	if (category == "-1") {
      $("label#category_error").show();
      $("select#category").focus(); 
	  $("select#category").css("border", "solid #F00 1px");
      return false;
    }
	
	if (description == "") {
      $("label#description_error").show();
      $("textarea#description").focus(); 
	  $("textarea#description").css("border", "solid #F00 1px");
      return false;
    }
	
	if (situation == "-1") {
      $("label#situation_error").show();
      $("select#situation").focus(); 
	  $("select#situation").css("border", "solid #F00 1px");
      return false;
    }
	
	if (first_name == "") {
      $("label#first_name_error").show();
      $("input#first_name").focus(); 
	  $("input#first_name").css("border", "solid #F00 1px");
      return false;
    }
	
	if (last_name == "") {
      $("label#last_name_error").show();
      $("input#last_name").focus(); 
	  $("input#last_name").css("border", "solid #F00 1px");
      return false;
    }
	
	if (tel != "" && isNaN(tel)) {
      $("label#tel_error").show();
      $("input#tel").focus(); 
	  $("input#tel").css("border", "solid #F00 1px");
      return false;
    }
	
	if (mobile == "" || isNaN(mobile)) {
      $("label#mobile_error").show();
      $("input#mobile").focus(); 
	  $("input#mobile").css("border", "solid #F00 1px");
      return false;
    }
	
	if (email == "" || !verif(email)) {
      $("label#email_error").show();
      $("input#email").focus();
  	  $("input#email").css("border", "solid #F00 1px");
	  
      return false;
    }
	
	if (captcha == "") {
      $("label#captcha_error").show();
      $("input#captcha").focus(); 
	  $("input#captcha").css("border", "solid #F00 1px");
      return false;
    }


		var dataString = 'operation='+ operation + '&category=' + category + '&description=' + description + '&first_name=' + first_name + '&last_name=' + last_name + '&price=' + price + '&city=' + city + '&tel=' + tel + '&surface=' + surface + '&mobile=' + mobile + '&email=' + email + '&situation=' + situation + '&captcha=' + captcha;
		
	
	
	 $('#suggest-page').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/suggest.php",
      data: dataString,
      success: function(result) {


					if(result == "SENT"){
				  
						$('#suggest-page').html("<div id='confirm_send_form'></div>");
						$('#confirm_send_form').html("<h2>Merci, votre proposition a été <br />bien envoyée</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") {
								
					 $('#suggest-page').html("<div id='confirm_send_form'></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 {
				
					 $('#suggest-page').html("<div id='confirm_send_form'></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;
	});
});
