var Ld1Id='';
var Ld2Id='';
var Ld3Id='';
var Ld4Id='';
var id_liste='';

function ValideLd2(val) {
	Ld1Id=val; //id_département
	id_liste='2';//Utilisé dans la fonction ChargeLd() pour identifier la liste déroulante
	var LD_URL = '../php/recherche/ValideLd2.php?Ld1='+Ld1Id;
	ObjetXHR(LD_URL)
	// Réinitialisation de Ld3 si modification de LD1 après passage en Ld2
	if (Ld2Id!='') {ValideLd3('');	}
}

function ValideLd3(val) {
	Ld2Id=val; //id_commune
	id_liste='3'; //Utilisé dans la fonction ChargeLd() pour identifier la liste déroulante
	var LD_URL = '../php/recherche/ValideLd3.php?Ld1='+Ld1Id+'&Ld2='+Ld2Id;
	if (Ld2Id=='') {var LD_URL = '../php/recherche/ValideLd3.php';}	
	ObjetXHR(LD_URL)
}

function ValideLd4(val) {
	Ld3Id=val; //id_commune
	id_liste='4'; //Utilisé dans la fonction ChargeLd() pour identifier la liste déroulante
	var LD_URL = '../php/recherche/ValideLd4.php?Ld1='+Ld1Id+'&Ld2='+Ld2Id+'&Ld3='+Ld3Id;
	if (Ld3Id=='') {var LD_URL = '../php/recherche/ValideLd4.php';}	
	ObjetXHR(LD_URL)
}

function ObjetXHR(LD_URL) {
	//creation de l'objet XMLHttpRequest
	if (window.XMLHttpRequest) { // Mozilla,...
		xmlhttp=new XMLHttpRequest();
		if (xmlhttp.overrideMimeType) {
			xmlhttp.overrideMimeType('text/xml');
		}	
		xmlhttp.onreadystatechange=ChargeLd;
		xmlhttp.open("GET", LD_URL, true);
		xmlhttp.send(null);
	} else if (window.ActiveXObject) { //IE 
		xmlhttp=new ActiveXObject('Microsoft.XMLHTTP'); 
		if (xmlhttp) {
			xmlhttp.onreadystatechange=ChargeLd;
			xmlhttp.open('GET', LD_URL, false);
			xmlhttp.send();
		}
	}
	// Bouton non apparent car modification de LD1 ou Ld2
	document.getElementById('buttons').style.display='none';
}

// fonction pour manipuler l'appel asynchrone
function ChargeLd() {
	if (xmlhttp.readyState==4) { 
		if (xmlhttp.status==200) { 
			//span id="niv2" ou "niv3"
			document.getElementById('niv'+id_liste).innerHTML=xmlhttp.responseText; 
			if (xmlhttp.responseText.indexOf('disabled')<=0) {
				//focus sur liste déroulante 2 ou 3
				document.getElementById('Liste'+id_liste).focus(); 
			}	
		}
	}
}

function Affiche_Btn() {
	document.getElementById('buttons').style.display='inline';
}

function visibilite(thingId)
{
	var targetElement;
	targetElement = document.getElementById(thingId) ;
		if (targetElement.style.display == "none")
			{
			targetElement.style.display = "" ;
			} else {
			targetElement.style.display = "none" ;
			}
}
function unvisibilite(thingId)
{
	var targetElement;
	targetElement = document.getElementById(thingId) ;
		if (targetElement.style.display == "")
			{
			targetElement.style.display = "none" ;
			} else {
			targetElement.style.display = "" ;
			}
}

