$(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;
	} 
	

  $(".button").click(function() {
		// validate and process form
		// first hide any error messages
    
	$('.error').hide();
	
	$("input#name").css("border", "solid #7DBF10 1px");
	$("input#email").css("border", "solid #7DBF10 1px");
	$("input#tel").css("border", "solid #7DBF10 1px");
	$("select#project").css("border", "solid #7DBF10 1px");
	$("select#beginProject").css("border", "solid #7DBF10 1px");
	$("textarea#description").css("border", "solid #7DBF10 1px");
	
	var name = $("input#name").val();
	var email = $("input#email").val();	
	var tel = $("input#tel").val();
	var project = $("select#project").val();
	var beginProject = $("select#beginProject").val();
	var description = $("textarea#description").val();
	
	
	
	if (name == "") {
      $("label#name_error").show();
      $("input#name").focus(); 
	  $("input#name").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 (tel == "" || isNaN(tel)) {
      $("label#tel_error").show();
      $("input#tel").focus();
	  $("input#tel").css("border", "solid #F00 1px");
      return false;
    }
	
	if (project == "-1") {
      $("label#project_error").show();
      $("select#project").focus();
  	  $("select#project").css("border", "solid #F00 1px");
	  
      return false;
    }
	
	if (beginProject == "-1") {
		
      $("label#beginProject_error").show();
      $("select#beginProject").focus();
  	  $("select#beginProject").css("border", "solid #F00 1px");
	  
      return false;
    }
	
	
	
	
		
		var dataString = 'name='+ name + '&email=' + email + '&tel=' + tel + '&project=' + project + '&beginProject=' + beginProject + '&description=' + description ;
		
	
	
	 $('#devis').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/mailer_devis.php",
      data: dataString,
      success: function(result) {


				if(result == "SENT"){
				  
						$('#devis').html("<div id='confirm_send_form_devis'></div>");
						$('#confirm_send_form_devis').html("<h2>Merci, votre demande 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_devis').append("<img id='checkmark_devis' src='images/check.png' width='35' height='35' />");
						});
			  
									
									}
			  else {
				
				
					 $('#devis').html("<div id='confirm_send_form_devis'></div>");
					$('#confirm_send_form_devis').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_devis').append("<img id='checkmark_devis' src='images/error.png' width='35' height='35' />");
					});
				  
				  
				  }
	  
	  
	  }
	  
     });

    return false;
	});
});
runOnLoad(function(){
  $("input#name").select().focus();
});
