//Initialisation de la page
$().ready(function()
{

	// ----------------------------------------
	//Initialisation des elements
	$('#intitule_detail').hide();
	$('.detail_vente').hide();
	$('.detail_location').hide();


	$("#diagnostic_type").change(function() {maj_liste_diagnostiques();});
	$("#vente_annee_construction").change(function() {maj_liste_diagnostiques();});
	$("#vente_departement").change(function() {maj_liste_diagnostiques();});
	$("#vente_copropriete").change(function() {maj_liste_diagnostiques();});
	$("#vente_installation_gaz").change(function() {maj_liste_diagnostiques();});
	$("#vente_installation_electrique").change(function() {maj_liste_diagnostiques();});
	$("#location_annee_construction").change(function() {maj_liste_diagnostiques();});

	// ----------------------------------------
	// validateurs js
	var validator = $("#inscription").validate({
		errorPlacement: function(error, element) {
			error.appendTo( element.parent("td") );
		},
		debug:false,
		meta: "validate",

		rules:{
			// -----  Bloc 1 -------
			//diagnostic_type
			diagnostic_type : {
				required:true,
				range:[1,3]
			},

			//diagnostic_type_logement
			diagnostic_type_logement : {
				required:true,
				range:[1,2]
			},

			//diagnostic_nombre_pieces
			diagnostic_nombre_pieces : {
				required:true,
				range:[1,6]
			},

			//diagnostic_surface_habitable
			diagnostic_surface_habitable : {
				required:true,
				number:true
			},

			//diagnostic_delai_realisation
			diagnostic_delai_realisation : {
				required:true,
				range:[1,3]
			},

			// -----  Bloc 2 -------

			// ********** Si type_diagnostique == 1 (vente) ************
			//vente_annee_construction
			vente_annee_construction : {
				required:function(){
					var diagnostic_type = $("#diagnostic_type").val();
					return (diagnostic_type == 1);
				},
				range:[1,3]
			},

			//vente_departement
			vente_departement : {
				required:function(){
					var diagnostic_type = $("#diagnostic_type").val();
					return (diagnostic_type == 1);
				},
				digits:true
			},

			//vente_code_postal
			vente_code_postal : {
				required:function(){
					var diagnostic_type = $("#diagnostic_type").val();
					return (diagnostic_type == 1);
				},
				rangelength:[0,99000],
				minlength:5,
				maxlength:5,
				digits:true
			},

			//vente_copropriete
			vente_copropriete : {
				required:function(){
					var diagnostic_type = $("#diagnostic_type").val();
					return (diagnostic_type == 1);
				},
				range:[1,2]
			},

			//vente_installation_gaz
			vente_installation_gaz : {
				required:function(){
					var diagnostic_type = $("#diagnostic_type").val();
					return (diagnostic_type == 1);
				},
				range:[1,2]
			},

			//vente_installation_electrique
			vente_installation_electrique : {
				required:function(){
					var diagnostic_type = $("#diagnostic_type").val();
					return (diagnostic_type == 1);
				},
				range:[1,2]
			},

			// ********** Si type_diagnostique == 2 (location) ************
			//location_annee_construction
			location_annee_construction : {
				required:function(){
					var diagnostic_type = $("#diagnostic_type").val();
					return (diagnostic_type == 2);
				},
				range:[1,2]
			},


			// -----  Bloc 3 -------
			//situation_immobiliere
			situation_immobiliere : {
				required:true,
				range:[1,4]
			},

			//civilite
			civilite : {
				required:true,
				range:[1,3]
			},

			//Nom
			nom : {
				required:true,
				rangelength:[2,30]
			},

			//prenom
			prenom : {
				required:true,
				rangelength:[2,30]
			},

			//adresse
			adresse : {
				required:true,
				rangelength:[2,200]
			},

			//adresse2
			adresse2 : {
				required:false,
				rangelength:[2,200]
			},

			//code_postal
			code_postal : {
				required:true,
				rangelength:[0,99000],
				minlength:5,
				maxlength:5,
				digits:true
			},

			//pays_residence
			pays_residence : {
				required:true
			},

			//email
			email : {
				required:true,
				rangelength:[2,75],
				email: true
			},

			//tel1
			tel1: {
				required:true,
				telephone:'tel1'
			},

			//tel2
			tel2: {
				required:false,
				telephone:'0101010101'
			},

			//heure_disponibilite
			heure_disponibilite : {
				required:true,
				range:[1,6]
			},

			// -----  Bloc 4 -------
			//optin_solutiondevis
			optin_solutiondevis : {
				required:true,
				range:[1,2]
			},
			//optin_partenaire
			optin_partenaire : {
				required:true,
				range:[1,2]
			}
		},
		invalidHandler: function() {
			//alert(validator.numberOfInvalids() + " field(s) are invalid");
		}
	});


	$.validator.addMethod(
	"telephone",
	function(value) {
		if(value == '')
		{
			return true;
		}
		else
		{
			var re = new RegExp("^0[1-9][0-9]{8}$");
			return re.test(value);
		}
	},
	"Please check your input."
	);
});

function maj_code_postal(id_element, id_resultat, ville, nom_param, test_pays)
{
	var code_postal = $("#"+id_element).val();
	if(typeof(test_pays)!='undefined')
	{
		var pays = $("#"+test_pays+" option:selected").val(); //alert('test sur le pays : '+pays);
		if(pays == "FR"){var complete_cp = 1;}
		else{var complete_cp = 0;}
	}
	else
	{
		var complete_cp = 1;
	}

	$("#"+id_resultat).html('<img src="http://static.ad-mastering.net/formulaires_v4/communs/img/loading.gif" />');
	$.ajax({
		url: 'maj_code_postal.inc.php',
		async: true,
		type: 'POST',
		data: { "code_postal": code_postal, "ville": ville, "nom_param": nom_param, "complete_cp":  complete_cp},
		dataType: 'html',
		success: function(data){
			//alert(data);
			$("#"+id_resultat).html(data);
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			//alert(XMLHttpRequest+'\n'+textStatus+'\n'+errorThrown);
			$("#"+id_resultat).html('<input type="text" name="'+nom_param+'" id="'+nom_param+'" />');
		}
	});
}

function afficher_detail()
{
	$('#intitule_detail').hide();
	$('.detail_vente').hide();
	$('.detail_location').hide();

	var diagnostic_type = $('#diagnostic_type').val();//alert(diagnostic_type);

	if(diagnostic_type == 1)
	{
		$('#intitule_detail').show();
		$('.detail_vente').show();
	}
	else if(diagnostic_type == 2)
	{
		$('#intitule_detail').show();
		$('.detail_location').show();
	}
}

function maj_liste_diagnostiques()
{
	var diagnostic_type = $("#diagnostic_type").val();//alert(diagnostic_type);
	var vente_annee_construction = $("#vente_annee_construction").val();
	var vente_departement = $("#vente_departement").val();
	var vente_copropriete = $("#vente_copropriete").val();
	var vente_installation_gaz = $("#vente_installation_gaz").val();
	var vente_installation_electrique = $("#vente_installation_electrique").val();
	var location_annee_construction = $("#location_annee_construction").val();
	var diagnostic_type_logement = $("#diagnostic_type_logement").val();

	$("#bloc_choix_diagnostic").html('<img src="http://static.ad-mastering.net/formulaires_v4/communs/img/loading.gif" /> Veuillez patienter pendant que nous recherchons les diagnostics');
	$.ajax({
		url: 'calcul_diagnostic.php',
		async: true,
		type: 'POST',
		data: {
		"diagnostic_type": diagnostic_type,
		"vente_annee_construction": vente_annee_construction,
		"vente_departement": vente_departement,
		"vente_copropriete": vente_copropriete,
		"vente_installation_gaz": vente_installation_gaz,
		"vente_installation_electrique": vente_installation_electrique,
		"location_annee_construction": location_annee_construction,
		"diagnostic_type_logement": diagnostic_type_logement
		},
		dataType: 'html',
		success: function(data){
			//alert(data);
			$("#bloc_choix_diagnostic").html(data);
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			//alert(XMLHttpRequest+'\n'+textStatus+'\n'+errorThrown);
			$("#bloc_choix_diagnostic").html('Erreur');
		}
	});
}
