// moteur-recherche.js
// Pour personnaliser les listes déroulantes Municipalité et Modèle
// Formulaire : Moteur de recherche

var ajaxRequete;

function initialiserFormMoteurRecherche() {
	if (document.getElementById) {
		
		var objRegion 		= document.getElementById("regions");
		var objMunicipalite = document.getElementById("municipalites");
		var objMarque 		= document.getElementById("marques");
		var objModele 		= document.getElementById("modeles");
		var objPaiementDe	= document.getElementById("paiement_de");
		var objPaiementA	= document.getElementById("paiement_a");
		var objMoisDe		= document.getElementById("mois_de");
		var objMoisA		= document.getElementById("mois_a");
		
		if (objRegion && objMunicipalite && objMarque && objModele && 
			objPaiementDe && objPaiementA && objMoisDe && objMoisA) {
			try {	// Opera 8.0+, Firefox, Safari
				ajaxRequete = new XMLHttpRequest();
			} catch (e){
				try {	// Internet Explorer
					ajaxRequete = new ActiveXObject("Msxml2.XMLHTTP");
				} catch (e) {
					try {
						ajaxRequete = new ActiveXObject("Microsoft.XMLHTTP");
					} catch (e){
						return;
					}
				}
			}

			if (objRegion.selectedIndex < 1) {
				objMunicipalite.options.length = 1;
				objMunicipalite.disabled = true;
			}
			
			if (objMarque.selectedIndex < 1) {
				objModele.options.length = 1;
				objModele.disabled = true;
			}
			
			addListener(objRegion, 'change', eventAfficherListeMunicipalites(objRegion, objMunicipalite));
			addListener(objMunicipalite, 'change', eventRetenirValeur('municipalites', objMunicipalite));
			addListener(objMarque, 'change', eventAfficherListeModeles(objMarque, objModele));
			addListener(objModele, 'change', eventRetenirValeur('modeles', objModele));
			addListener(objPaiementDe, 'change', eventRetenirValeur('paiement_de', objPaiementDe));
			addListener(objPaiementA, 'change', eventRetenirValeur('paiement_a', objPaiementA));
			addListener(objMoisDe, 'change', eventRetenirValeur('mois_de', objMoisDe));
			addListener(objMoisA, 'change', eventRetenirValeur('mois_a', objMoisA));
		}
	}
}

function eventAfficherListeMunicipalites(objRegion, objMunicipalite) {
	return function() {
		afficherListeMunicipalites(objRegion, objMunicipalite);	
	}	
}

function eventAfficherListeModeles(objMarque, objModele) {
	return function() {
		afficherListeModeles(objMarque, objModele);	
	}	
}

function eventRetenirValeur(type, obj) {
	return function() {
		retenirValeur(type, obj);	
	}	
}

function afficherListeMunicipalites(objRegion, objMunicipalite) {

	var index = objRegion.selectedIndex;
	var data;
	
	if (index < 1) {
		objMunicipalite.options.length = 1;
		objMunicipalite.disabled = true;
	}		

	ajaxRequete.open("POST", "/includes/ajax/moteur_recherche.php", true);
	ajaxRequete.onreadystatechange = function(){
		if(ajaxRequete.readyState == 4){
			eval(ajaxRequete.responseText);
		}
	}

	ajaxRequete.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
	data = "regions="+escape(objRegion.options[index].value); 
	ajaxRequete.send(data);
}

function afficherListeModeles(objMarque, objModele) {

	var index = objMarque.selectedIndex;
	var data;
	
	if (index < 1) {
		objModele.options.length = 1;
		objModele.disabled = true;
	}		

	ajaxRequete.open("POST", "/includes/ajax/moteur_recherche.php", true);
	ajaxRequete.onreadystatechange = function(){
		if(ajaxRequete.readyState == 4){
			eval(ajaxRequete.responseText);
		}
	}

	ajaxRequete.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
	data = "marques="+escape(objMarque.options[index].value); 
	ajaxRequete.send(data);
}

function retenirValeur(type, obj) {

	ajaxRequete.open("POST", "/includes/ajax/moteur_recherche.php", true);
	ajaxRequete.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
	data = type+"="+escape(obj.options[obj.selectedIndex].value);
	ajaxRequete.send(data);
}

addListener(window, 'load', initialiserFormMoteurRecherche);