// JavaScript Document

// FONCTION PERMETTANT DE RETROUVER L'ID DEMANDE
function findid(chaine, id){

	// on cherche la position de l'id
	var poset = chaine.indexOf(id);
	// on scinde en deux la chaine en utilisant l'id comme separateur
	premchaine = chaine.substring(0,poset);
	secondchaine = chaine.substring(poset+5,chaine.length);
	
	// on prend la seconde chaine et on regarde si il y a un & si oui alors on recupere la premier partie pour avoir l'id si il n'y en a pas la chaine restante c'est l'id
	 if (secondchaine.indexOf("&")!=-1){
	 
	 	
	 	// on cherche la position du &
		var poset2 = secondchaine.indexOf("&");
		// on scinde en deux la chaine en utilisant l'id comme separateur
		idvalue = secondchaine.substring(0,poset2);
		
	 	
	 } else {
	 	idvalue = secondchaine ; 		
	 }
		
	return idvalue;
}

// FONCTION DYNAMIQUE GENERANT LA SECONDE LISTE DEROULANTE EN FONCTION DU CHOIX DE LA PREMIERE
function filltheselect(liste, valeurchoix , nondefini){
	
	switch (liste){ 
   		
		case "dest1": 
			
			// on nettoie les listes deroulantes qui suivent
			  raz("dest2"); 
			  raz("idz"); 
			  raz("idv"); 
			
			//alert (valeurchoix);
			
			
			if (valeurchoix == " ") {
  				return;
 			}
		
			choix = findid(valeurchoix, "idr1");
		
			// on rend visible toutes les listes deroulantes
			document.getElementById("celldest2").style.display="block";
			document.getElementById("celldest2text").style.display="block";
			document.getElementById("cellidv").style.display="block";
			document.getElementById("cellidvtext").style.display="block";
			document.getElementById("cellidz").style.display="block";
			document.getElementById("cellidztext").style.display="block";
			
			new_option = new Option(nondefini,""); 
			document.getElementById("dest2").options[document.getElementById("dest2").length]=new_option; 
			
			// la variable vide permet de savoir si la liste deroulante suivante est vide ou non, dans le cas positif on ne la fait pas apparaitre
			var vide='oui';
			var i=0; 
			// cree la liste deroulante comportant R2 R3 R4
			while (i<tabR2.length){
			  
				if (tabR2[i][3]==choix){
				 new_option = new Option(tabR2[i][1],valeurchoix+"&idr2="+tabR2[i][0]); 
					//document.inscription.elements["dest2"].options[document.inscription.elements["dest2"].length]=new_option; 
					document.getElementById("dest2").options[document.getElementById("dest2").length]=new_option; 
					
					vide = "non";
					var z=0;
					var espace="  ";		
					while (z<tabR3.length){
					
							if(tabR3[z][4]==tabR2[i][0]){
								new_option = new Option("-- "+tabR3[z][1]+" ",valeurchoix+"&idr2="+tabR3[z][4]+"&idr3="+tabR3[z][0]); 
								document.getElementById("dest2").options[document.getElementById("dest2").length]=new_option;
								
								vide = "non";
								var y=0;		
								while (y<tabR4.length){
					
									if(tabR4[y][5]==tabR3[z][0]){
										new_option = new Option("------ "+tabR4[y][1],valeurchoix+"&idr2="+tabR3[z][4]+"&idr3="+tabR3[z][0]+"&idr4="+tabR4[y][0]); 
										document.getElementById("dest2").options[document.getElementById("dest2").length]=new_option;
										
										vide = "non";
									}
									y++;
								}
								
							}
						z++;
					}	
				
				} 
				i++;
			 }
			 
			 // si cette liste est vide on la cache
			 if (vide=="oui"){
			 	document.getElementById("celldest2").style.display="none";
				document.getElementById("celldest2text").style.display="none";
			 }
			 
		new_option = new Option(nondefini,""); 
		document.inscription.elements["idz"].options[document.inscription.elements["idz"].length]=new_option;
			 
		// la variable vide permet de savoir si la liste deroulante suivante est vide ou non, dans le cas positif on ne la fait pas apparaitre
			var vide='oui';				 
			  var r=0;
			  // cree la liste deroulante comportant Z et MR (zones et meta region)		
				while (r<tabZMR.length){
					
					if(tabZMR[r][3]==choix){
					var vide='non';	
						if (tabZMR[r][7]=="Z"){
						
							new_option = new Option(tabZMR[r][1],tabZMR[r][0]); 
							document.inscription.elements["idz"].options[document.inscription.elements["idz"].length]=new_option;
						
						} 
						
					}
				r++;
					
				} 
				// si cette liste est vide on la cache
			  if (vide=="oui"){
			 	document.getElementById("cellidz").style.display="none";
				document.getElementById("cellidztext").style.display="none";
			 }  
			 
			 new_option = new Option(nondefini,""); 
			document.getElementById("idv").options[document.getElementById("idv").length]=new_option;
			var vide = "oui";	
			 var v=0;
			 // cree la liste deroulante comportant les villes		
				while (v<tabV.length){
					
					if(tabV[v][3]==choix){
					vide = "non";
						new_option = new Option(tabV[v][1],tabV[v][0]); 
						document.getElementById("idv").options[document.getElementById("idv").length]=new_option;
					}
				v++;
					
				}
				// si cette liste est vide on la cache
				if (vide=="oui"){
				document.getElementById("cellidv").style.display="none";
				document.getElementById("cellidvtext").style.display="none";
		} else {
				document.getElementById("cellidv").style.display="block";
				document.getElementById("cellidvtext").style.display="block";
			
		}
		 
		  break; 
		  
		  
		  
	   case "dest2": 
	 
	  // on vide la liste deroulante qui affiche les villes 
		raz("idv"); 
	   
	  
	   if (valeurchoix.indexOf("idr3")!=-1){
	
		
			
		 	id = findid(valeurchoix, "idr3");
			idparent = findid(valeurchoix, "idr2");	
			idgrandparent =  findid(valeurchoix, "idr1");
			
			
		  	new_option = new Option(nondefini,""); 
			document.getElementById("idv").options[document.getElementById("idv").length]=new_option;
		  	 	var z=0;	
		  		var vide='oui';
		  		while (z<tabV.length){
				
						if((tabV[z][5]==id)&&(tabV[z][4]==idparent)&&(tabV[z][3]==idgrandparent)){
							var vide='non';
							new_option = new Option(tabV[z][1],tabV[z][0]); 
							document.getElementById("idv").options[document.getElementById("idv").length]=new_option;
						}
					z++;
				}
		  
		  } else if (valeurchoix.indexOf("idr2")!=-1){
	
					
			
			id = findid(valeurchoix, "idr2");
			idparent = findid(valeurchoix, "idr1");
			
			
			
		  	new_option = new Option(nondefini,""); 
			document.getElementById("idv").options[document.getElementById("idv").length]=new_option;
		  	 	var z=0;	
		  		var vide='oui';
		  		while (z<tabV.length){
				
						if((tabV[z][4]==id)&&(tabV[z][3]==idparent)){
							var vide='non';
							new_option = new Option(tabV[z][1],tabV[z][0]); 
							document.getElementById("idv").options[document.getElementById("idv").length]=new_option;
						}
					z++;
				}
		  
		  } 
		
		  
		 if (vide=="oui"){
				document.getElementById("cellidv").style.display="none";
				document.getElementById("cellidvtext").style.display="none";
		} else {
				document.getElementById("cellidv").style.display="block";
				document.getElementById("cellidvtext").style.display="block";
			
		}	

		  break; 
   } 
} 


// fonction qui permet de nettoyer une liste deroulante
function raz(liste){
	l=document.getElementById(liste).length; 
	for (i=l; i>=0; i--) 
   	document.getElementById(liste).options[i]=null;
}    

