var JQUENL = jQuery.noConflict();
JQUENL(document).ready(function() {
	
	JQUENL("#message_succes").hide();
	JQUENL("#message_warning").hide();
	
	function SlideOutRedirection(LeContMessage, LienRedirection){
		var MessageUn="#"+LeContMessage;
		setTimeout(function(){JQUENL(MessageUn).slideUp("slow", function () {});window.location=LienRedirection;}, 1000);
	}
	
	
	function SlideOutNormal(LeContMessage){
  		setTimeout(function(){JQUENL("#"+LeContMessage).slideUp("slow", function () {})}, 1000);
	}
	
	function SetFielNormal(LField){
  		var LeField="#"+LField;
		JQUENL(LeField).focus(function() {
			JQUENL(this).animate({backgroundColor: '#ffffff'}, 2000);
		});
	}
	
	function SetFielError(LField){
  		var LeField="#"+LField;
		JQUENL(LeField).animate({backgroundColor: '#ff0000'}, 2000);
	}
	
	function startSending(){
		JQUENL('#conteneur_uploder').show();
		JQUENL('#conteneur_messages').hide();
	}
	
	JQUENL('#dialog_commander_telephone').dialog({
		autoOpen: false,
		width:380,
		height:350,
		minHeight:350,
		maxHeight:350,
		hide: 'clip',
		resizable: false,
		modal: true
	});
	
	JQUENL('#dialog_commander_ligne').dialog({
		autoOpen: false,
		width:990,
		height:620,
		minHeight:620,
		maxHeight:620,
		hide: 'clip',
		resizable: false,
		modal: true,
		buttons: { 
			"Valider": function() { 
				startSending();
				var a,b,c,d,e,f,i;
				var h = 0 ;
				var result = '';
				var nom_commander = JQUENL("#nom_commander").val() ;
				var prenom_commander = JQUENL("#prenom_commander").val() ;
				var adresse_commander = JQUENL("#adresse_commander").val() ;
				var code_postal_commander = JQUENL("#code_postal_commander").val() ;
				var ville_commander = JQUENL("#ville_commander").val() ;
				var telephone_commander = JQUENL("#telephone_commander").val() ;
				var email_commander = JQUENL("#email_commander").val() ;
				
				var verif = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]{2,}[.][a-zA-Z]{2,3}$/;
		
				if (nom_commander == '')  { 
					a = 1;
					SetFielError("nom_commander");
				}else{ a = 0; }
				if (prenom_commander == '')  { 
					b = 1;
					SetFielError("prenom_commander");
				}else{ b = 0; }
				if (adresse_commander == '')  { 
					c = 1;
					SetFielError("adresse_commander");
				}else{ c = 0; }
				if (code_postal_commander == '')  { 
					d = 1;
					SetFielError("code_postal_commander");
				}else{ d = 0; }
				if (ville_commander == '')  { 
					e = 1;
					SetFielError("ville_commander");
				}else{ e = 0; }
				if (telephone_commander == '')  { 
					f = 1;
					SetFielError("telephone_commander");
				}else{ f = 0; }
				if (verif.exec(email_commander) == null)  { 
					i = 1;
					SetFielError("email_commander");
				}else{ i = 0; }
		
				if( a==0 && b==0 && c==0 && d==0 && e==0 && f==0 && i==0 ){   
					var lesInfosForm = JQUENL('#form_commander_ligne').serialize();
					var order = lesInfosForm;
					JQUENL.post("requests/commander-ligne.php", order, function(theResponse){
  						JQUENL('#conteneur_uploder').hide();
						JQUENL("#conteneur_messages").html(theResponse);
						JQUENL('#conteneur_messages').show();
						SlideOutRedirection('conteneur_messages', 'index.php');
					});
				}else{			
					result = '<div class="message warning"><h2>Attention!</h2><p>Veuillez verifier les champs en rouge</p></div>';
  					JQUENL('#conteneur_uploder').hide();
					JQUENL("#conteneur_messages").html(result);
					JQUENL('#conteneur_messages').show();
					SlideOutNormal('conteneur_messages');
				}
			},
			"Annuler": function() { 
				JQUENL(this).dialog('close');
			}
		}
	});
	
	JQUENL("#btn_commander_telephone").click(function() {
		JQUENL('#dialog_commander_telephone').dialog('open');	
		return false;
	});
	
	JQUENL("#btn_commander_ligne").click(function() {
		var couleur_exterieur_vehicule = JQUENL("#couleur_exterieur_vehicule").attr("value");
		var couleur_interieur_vehicule = JQUENL("#couleur_interieur_vehicule").attr("value");
		var order = 'couleur_exterieur_vehicule='+couleur_exterieur_vehicule+'&couleur_interieur_vehicule='+couleur_interieur_vehicule;
		JQUENL.post("forms/formulaire-commander-ligne.php", order, function(theResponse){
			JQUENL("#dialog_commander_ligne").html(theResponse);
		});
		JQUENL('#dialog_commander_ligne').dialog('open');	
		return false;
	});
	
	SetFielNormal("nom_commander");
	SetFielNormal("prenom_commander");
	SetFielNormal("adresse_commander");
	SetFielNormal("code_postal_commander");
	SetFielNormal("ville_commander");
	SetFielNormal("telephone_commander");
	SetFielNormal("email_commander");
	
	
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	JQUENL(".nous_contactez").click(function() {
		JQUENL('#dialog_form_contact').dialog('open');
		return false;
	});
	
	SetFielNormal("civilite_contact");
	SetFielNormal("prenom_contact");
	SetFielNormal("nom_contact");
	SetFielNormal("email_contact");
	SetFielNormal("message_contact");
	
	JQUENL('#dialog_form_contact').dialog({
		autoOpen: false,
		width:760,
		height:630,
		minHeight:630,
		maxHeight:630,
		show: 'clip',
		hide: 'clip',
		resizable: false,
		modal: true,
		buttons: { 
			"Valider": function() { 
				startSending();
				var a,b,c,d,e;
				var h = 0 ;
				var result = '';
				var civilite_contact = JQUENL("#civilite_contact").val() ;
				var prenom_contact = JQUENL("#prenom_contact").val() ;
				var nom_contact = JQUENL("#nom_contact").val() ;
				var email_contact = JQUENL("#email_contact").val() ;
				var message_contact = JQUENL("#message_contact").val() ;
				
				var verif = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]{2,}[.][a-zA-Z]{2,3}JQUENL/;
		
				if (civilite_contact == '')  { 
					a = 1;
					SetFielError("civilite_contact");
				}else{ a = 0; }
				if (prenom_contact == '')  { 
					b = 1;
					SetFielError("prenom_contact");
				}else{ b = 0; }
				if (nom_contact == '')  { 
					c = 1;
					SetFielError("nom_contact");
				}else{ c = 0; }
				if (message_contact == '')  { 
					e = 1;
					SetFielError("message_contact");
				}else{ e = 0; }
		
				if( a==0 && b==0 && c==0 && e==0 ){   
					var lesInfosForm = JQUENL('#form_contact').serialize();
					var order = lesInfosForm;
					JQUENL.post("requests/contact.php", order, function(theResponse){
  							JQUENL('.conteneur_uploder').hide();
							JQUENL(".conteneur_messages").html(theResponse);
							JQUENL('.conteneur_messages').show();
							SlideOutRedirection('conteneur_messages', 'index.php');
					});
				}else{			
					result = '<div class="message warning"><h2>Attention!</h2><p>Veuillez verifier les champs en rouge</p></div>';
  					JQUENL('.conteneur_uploder').hide();
					JQUENL(".conteneur_messages").html(result);
					JQUENL('.conteneur_messages').show();
					SlideOutNormal('conteneur_messages');
				}
			},
			"Annuler": function() { 
				JQUENL('#dialog_form_contact').dialog('close');
			}
		}
	});
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
});
