// JavaScript Document
//Author : Charpentier Loïc
$(document).ready(function() {
						   
	var error=false;
	/*********************************
	*Gestion des données du formulaire
	**********************************/
	$("#formContact").submit(function()
		{		
			var subForm=true;
			if (!is_tel($("#fieldTel").val())) {
				$("#v_tel").empty();
				$("#v_tel").append("<img src=\"images/error.png\" alt=\"Telephone incorrect\" title=\"Telephone incorrect\" border=\"0\" />");
				$("#v_tel").fadeIn("slow");
				setTimeout("$(\"#v_tel\").fadeOut(\"fast\")",6000);
				subForm=false;
			}
			if (!is_email($("#fieldEmail").val())) {
				$("#v_email").empty();
				$("#v_email").append("<img src=\"images/error.png\" alt=\"Email incorrect\" title=\"Email incorrect\" border=\"0\" />");
				$("#v_email").fadeIn("slow");
				setTimeout("$(\"#v_email\").fadeOut(\"fast\")",6000);
				subForm=false;
			}
			if (is_empty($("#fieldNom").val())) {
				$("#v_nom").empty();
				$("#v_nom").append("<img src=\"images/error.png\" alt=\"Veuillez saisir votre nom\" title=\"Veuillez saisir votre nom\" border=\"0\" />");
				$("#v_nom").fadeIn("slow");
				setTimeout("$(\"#v_nom\").fadeOut(\"fast\")",6000);
				subForm=false;
			}
			if (is_empty($("#fieldPrenom").val())) {
				$("#v_prenom").empty();
				$("#v_prenom").append("<img src=\"images/error.png\" alt=\"Veuillez saisir votre prenom\" title=\"Veuillez saisir votre prenom\" border=\"0\" />");
				$("#v_prenom").fadeIn("slow");
				setTimeout("$(\"#v_prenom\").fadeOut(\"fast\")",6000);
				subForm=false;
			}

			
			
			if(subForm)
			{
				$.post("action/contactSend.php", 
					{ 
						cNom: $("#fieldNom").val(),
						cPrenom: $("#fieldPrenom").val(), 
						cSociete: $("#fieldSociete").val(), 
						cAdresse: $("#fieldAdresse").val(), 
						cVille: $("#fieldVille").val(),
						cPays: $("#fieldPays").val(),
						cCp: $("#fieldCp").val(), 
						cEmail: $("#fieldEmail").val(), 
						cTel: $("#fieldTel").val(), 
						cDemande: $("#fieldDemande").val(),
						cCiv:$("input[@name='civ']:checked").val()
					}, 
					function(data){ 
						if(data=="ok")
						{
							
							var msg=" ont été transmises avec succès. A bientôt !";
							invalidData(msg);
						}
						else if(data=="no")
						{
							var msg=" comportent des erreurs";
							invalidData(msg);	
						}
						else
						{
							var msg=" n'ont pas être transmises; Un problème technique est survenu, veuillez contacter le webmaster.";
							invalidData(msg);
						}
					} 
				);
			}
			else
			{
				var msg=" comportent des erreurs";
				invalidData(msg);
			}
			return false;
		});
	
	/*
	* Fonction de gestion d'echec
	* Parametre : String
	*/
	function invalidData(val)
	{
		$("#legend").fadeOut("slow",function(){
			$("#legend").html("Vos coordonn&eacute;es "+val);
			$("#legend").fadeIn("slow",function(){
				setTimeout("$(\"#legend\").html(\"Vos coordonn&eacute;es\")",4500);								
			});					  
		});
	}
	
	/*
	* Verification du masque email
	* Parametre : String
	* Retour : Boolean
	*/
	function is_email(ch) {
		var str = new String(ch);
		if (str.match('^[-_\.0-9a-zA-Z]{1,}@[-_\.0-9a-zA-Z]{1,}[\.][0-9a-zA-Z]{2,}$'))return true;
		return false;
	}
	
	/*
	* Verification d'une chaine vide
	* Parametre : String
	* Retour : Boolean
	*/
	function is_empty(ch)
	{
		var str = new String(ch);
		if (str.match('^[ ]{1,}$') || str=="")return true;
		return false;
	}
	
	/*
	* Verification du masque telephone
	* Parametre : String
	* Retour : Boolean
	*/
	function is_tel(ch) {
		var str = new String(ch);
		if (str.match('[0-9+() ]{1,}$'))return true;
		return false;
	}
	
	/*
	* Verification du masque code postal
	* Parametre : String
	* Retour : Boolean
	*/
	function is_cp(ch) {
		var str = new String(ch);
		if (str.match('^[0-9]{5}$'))return true;
		return false;
	}
	
	
	
});


