// jQuery.noConflict();
jQuery(document).ready(function($){

	// texte d'aide
	$(".help").each(function(){ 
		$(this).focus(function(){
			$(this).next().fadeIn(); 
		});
		$(this).blur(function(){
			$(this).next().fadeOut(); 
		});		
	 });
	
	$("input").searchField(); // texte d'exemple
	
	$(".datePicker").datepicker(); // calendrier
	
	//////////// Validation //////////////////
	
	/*jQuery.validator.setDefaults({
	debug: true,
	success: "formSuccess",
	});
	jQuery.validator.addClassRules({
  		zip: {
   		 digits: true,
  		  minlength: 5,
  		  maxlength: 5
 		 }
	});
	jQuery.validator.addClassRules("phone", {
		 required : true,
		 digits: true,
		minlength: 10,
  		 maxlength: 14

	});
	*/
	
	// méthode qui vérifie la validité du numéro de téléphone 
	// format accepté :
	/*
		00-00-00-00-00
		00 00 00 00 00 (1 ou plusieurs espaces autorisés)
		0000000000 (sans espaces)
	*/
	// pour info : this.replace(/^\s+|\s+$/g,""); = enleve espace avant et après (=trim en php)	
	jQuery.validator.addMethod("telFormat", function(value, element) { 
		value = value.replace(/^\s+|\s+$/g,"");
		return this.optional(element) || value.match(/^\d{10}$/) || value.match(/^\d{2}-\d{2}-\d{2}-\d{2}-\d{2}$/) || value.match(/^\d{2}( )*\d{2}( )*\d{2}( )*\d{2}( )*\d{2}$/); 
	}, "Votre numéro de téléphone est incorrect.");

	// méthode qui vérifie la validité du code postal (5 chiffres)
	jQuery.validator.addMethod("cpFormat", function(value, element) { 
		return this.optional(element) || value.match(/^\d{5}$/);
	}, "Votre code postal est incorrect.");
	
	jQuery.validator.addMethod("nomFormat", function(value, element) { 
		return this.optional(element) || !value.match("Nom");
	}, "Merci de remplir le champ.");
	
	jQuery.validator.addMethod("emailFormat", function(value, element) { 
		return this.optional(element) || !value.match("mon@email.com");
	}, "Votre email est incorrect.");

	
	$("#demandeEnLigneForm").validate({
		rules:{ 
            Tel:{ 
                required:true,
				telFormat:true 
            },
			Email:{
				required:true,
				email:true,
				emailFormat:true 
			},
			CodePostal:{
				required:true,
				cpFormat:true
			},
			Nom:{
				required:true,
				nomFormat:true
			}
		},
		messages:{ 
            Tel:{ 
                required:"Veuillez saisir votre numéro de téléphone.",
				telFormat:"Votre numéro de téléphone est incorrect." 
            },
			Email:{
				required:"Veuillez saisir votre email.",
				email:"Votre email est incorrect." ,
				emailFormat:"Votre email est incorrect." 
			},
			CodePostal:{
				required:"Veuillez saisir votre code postal.",
				cpFormat:"Votre code postal est incorrect."
			},
			Nom:{
				required:"Merci de remplir le champ.",
				nomFormat:"Merci de remplir le champ."
			}
		},
		errorPlacement: function(error, element) {
				error.appendTo( element.parent("p").next("div") );
			}
	});
	
	
	$(".formText").blur(function() {
 	 $(this).valid();
	});
	/*$("#demandeEnLigneForm").validate({
   rules: {
     name: {
       required: true,
       minlength: 2
     }
   },
   messages: {
     name: {
       required: "We need your email address to contact you",
       minlength: jQuery.format("At least {0} characters required!")
     }
   }
})*/


	/////////////// Fin /////////////////////
	
	///// Plier-Déplier ///////
	/*$("#listSwitchContent .switchcontent").hide();
	$(".headers").click(function(){
	  $(this).next(".switchcontent").slideToggle("normal");
	  return false;
	});*/
	
	//// Add row /////
	var template = jQuery.format($("#template").val());
	function addRow() {
		$(template(i++)).appendTo("#voscreditsContainer");
	}
	
	var i = 1;
	// start with one row
	addRow();
	// add more rows on click
	$("#add").click(addRow);

});


/* AJAX pour autocomplétion commune selon code postal  + selection département de l'agence */
function fillCity(){

	$("#CodePostal").change(  function() {
		
			$.ajax({
			   type: "POST",
			   //url: "/index.php?page=shop/inandfiFormAjax",
			   url: "/franchises/ajax/inandfiFormAjax.php",
			   data: "cp=" + $("#CodePostal").val(),
			   dataType: "json",
			   /*success: function(data){
			     alert( "Data Saved: " +  data.city );
				 
			   }*/
			   complete: function(request, settings){
					var cities = request.responseText;		
					var tabCities = cities.split('!!');	
					var options = '';
					for (var i = 0; i < tabCities.length; i++) {
				        options += '<option value="' + tabCities[i] + '">' + tabCities[i] + '</option>';
				      }
				   $("#Ville").html(options);
				   }
			 });
			 
			//changeAgenceSelect();
	});
}

function changeAgenceSelect(){
	var cp = $("#CodePostal").val();
	var departement = cp.substr(0,2);
	
	$('#selectAgence0').val(departement);
	document.getElementById("selectAgence0").onchange();
}


function addEvent(element, eventName, callback)
{
if (element.addEventListener)
element.addEventListener(eventName, callback, false);
else if (element.attachEvent)
element.attachEvent('on'+eventName, callback);
}

addEvent(window, "load", fillCity);

