$(function() {
  $('.error').hide();
  $('input.text-input').css({backgroundColor:"#FFFFFF"});
  $('input.text-input').focus(function(){
    $(this).css({borderColor:"#71B434"});
  });
  $('input.text-input').blur(function(){
    $(this).css({borderColor:"#d7d7d8"});
  });

	function isValidEmailAddress(emailAddress) {
 		var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
 		return pattern.test(emailAddress);
	}
	function isValidTel(telephone) {
		var patternTel = new RegExp(/^(01|02|03|04|05|06|08)(([\.\-\/])?[0-9][0-9]){4}/gi);
		return patternTel.test(telephone);
	}


  $(".button").click(function() {
		// validate and process form
		// first hide any error messages
    $('.error').hide();
		
	  var nom = $("input#field_nom").val();
		if (nom == "") {
      $("label#nom_error").show();
      $("input#field_nom").focus();
      return false;
    }
	
	var prenom = $("input#field_prenom").val();
	var societe = $("input#field_societe").val();
	var adresse = $("input#field_adresse").val();
	
	var tel = $("input#field_tel").val();
	/*if (tel == "") {
      $("label#tel_error").show();
      $("input#field_tel").focus();
      return false;
    }*/
	if(tel != 0)
	{
		if(isValidTel(tel))
		{} else {
			$("label#tel_error").show();
			$("input#field_tel").focus();
			return false;
		}
	} else {
		 $("label#tel_error").show();
		 $("input#field_tel").focus();
		 return false;		
	}
	
	var email = $("input#field_email").val();
	/*if (email == "") {
      $("label#email_error").show();
      $("input#field_email").focus();
      return false;
    }*/
	if(email != 0)
	{
		if(isValidEmailAddress(email))
		{} else {
			$("label#email_error").show();
			$("input#field_email").focus();
			return false;
		}
	} else {
		 $("label#email_error").show();
		 $("input#field_email").focus();
		 return false;		
	}

	
		
	var dataString = 'nom='+ nom + '&prenom=' + prenom + '&societe=' + societe + '&adresse=' + adresse + '&tel=' + tel + '&email=' + email;
		//alert (dataString);return false;
		
	$.ajax({
      type: "POST",
      url: "bin/process.php",
      data: dataString,
      success: function() {
        $('#contact_form').html("<div id='message'></div>");
        $('#message').html("<h3>Votre message a bien été envoyé !</h3>")
        .append("<p>Nous prendrons prochainement contact avec vous.</p>")
        .hide()
        .fadeIn(1000, function() {
          $('#message').append("<img id='checkmark' src='images/check.jpg' />");
        });
		//$(location).attr('href','contact-merci.html');
      }
     });
    return false;
	});
});
runOnLoad(function(){
  $("input#field_nom").select().focus();
});
