$(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_order").click(function() {
							  
		// validate and process form
		// first hide any error messages
    
	$('.error').hide();
	
	$("input#name").css("border", "solid #388FAA 1px");
	$("input#country").css("border", "solid #388FAA 1px");	
	$("input#mobile").css("border", "solid #388FAA 1px");	
	$("input#email").css("border", "solid #388FAA 1px");
	$("textarea#message").css("border", "solid #388FAA 1px");
	$("input#captcha").css("border", "solid #388FAA 1px");
		
	var name = $("input#name").val();
	var message = $("textarea#message").val();
	var country = $("input#country").val();
	var mobile = $("input#mobile").val();
	var email = $("input#email").val();
	var reference = $("input#reference").val();
	var captcha = $("input#captcha").val();
	
	
	if (name == "") {
      $("label#name_error").show();
      $("input#name").focus(); 
	  $("input#name").css("border", "solid #F00 1px");
      return false;
    }
	
	if (country == "") {
      $("label#country_error").show();
      $("input#country").focus(); 
	  $("input#country").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 (message == "") {
      $("label#message_error").show();
      $("textarea#message").focus(); 
	  $("textarea#message").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 = 'message=' + message + '&name=' + name + '&country=' + country + '&mobile=' + mobile + '&email=' + email + '&captcha=' + captcha + '&reference=' + reference;


	 $('#form_interested').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/order.php",
      data: dataString,
      success: function(result) {


					if(result == "SENT"){
				  
						$('#form_interested').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_interested').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_interested').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;
	});
});
