function multiselect(aSel) {
	this.selectbox = aSel;	// listebox
	this.nb = aSel.length;	// nb de liste (profondeur)
	this.liste = [];		// arboresence
	this.defaut = new Array(this.nb);	// valeur par defaut
	this.memo_profondeur = { niveau: 0, idp: 0 };	// profondeur

	// mémorise et retourne dans quelle profondeur de l'arbo on se trouve
	this.profondeur = function(idp) {
		var bTrouve = false;
		if(idp > 0) {
			if (this.memo_profondeur.idp != idp) {
				this.memo_profondeur.niveau = this.est_au_niveau(idp, 0);
				this.memo_profondeur.idp = idp;
			}
		}
		else {
			this.memo_profondeur.idp = 0;
			this.memo_profondeur.niveau = 0;
		}

		return this.memo_profondeur.niveau;
	};

	// compte à quel niveau se trouvre un noeud (récusive)
	this.est_au_niveau = function(idp, iNiveau) {
		var bParent = false;
		if (idp > 0) {
			for (var i=0; i<this.liste.length; i++) {
				if (this.liste[i].id == idp) {
					iNiveau++;
					idp = this.liste[i].idp;
					bParent = true;
					break;
				}
			}
		}
		return bParent ? this.est_au_niveau(idp, iNiveau) : iNiveau;
	};

	this.valeur_par_defaut = function(iListe, valDefaut) {
		this.defaut[iListe - 1] = valDefaut;
	};

	// retourne la valeur id de la selection d'une liste
	this.est_selectionne = function(liste) {
		// TODO : revoir tout l'algo sur un array liste[n][n] au lieu d'un tableau listant tout les niveau
		// basé sur la valeur, donc si 2 valeurs identiques sur un même niveau (même de branches différentes) = bug
		//var iSelection = this.selectbox[liste].selectedIndex;
		var sValue = this.selectbox[liste].value;
		
		var id = 0, cpt = 1;
		for(var i=0;i<this.liste.length;i++) {
			oliste = this.liste[i];			
			if (oliste.profondeur == liste && sValue == oliste.valeur ) {				
					id = oliste.id;
				cpt++;
			}
		}		
		return id;
	}

	// ajoute un noeud à l'arbo
	this.add = function(id, idp, texte, valeur) {
		valeur = valeur != "" ? valeur : texte;
		var prof = this.profondeur(idp);
		this.liste[this.liste.length] = {
			profondeur: prof,
			id: id,
			idp: idp,
			texte: texte,
			valeur: valeur,
			defaut: (this.defaut[prof] == valeur)
		};
	};

	// initialise
	this.init = function() {
		var oliste, cpt = 1;

		// remplit la première liste uniquement
		for(var i=0;i<this.liste.length;i++) {
			oliste = this.liste[i];
			if (oliste.profondeur == 0) {
				this.selectbox[0].options[cpt++] = new Option(oliste.texte, oliste.valeur); //, oliste.defaut, oliste.defaut);
				if (oliste.defaut) this.selectbox[0].selectedIndex = cpt-1; // @#$ IE6
			}
		}

		// événements sur les listes
		for (var i = 0;i < this.nb; i++) {		
			this.selectbox[i].instance = this;
			this.selectbox[i].liste = i + 1;			
			this.selectbox[i].onchange = function() {
				this.instance.change(this.liste);
			}
		}
	};

	// garde la première valeur (choississez, tous...)
	// cpt = 1
	this.raz = function(oSel) {
		var aSave = [oSel.options[0].text, oSel.options[0].value];
		oSel.options.length = 0;
		oSel.options[0] = new Option(aSave[0], aSave[1], true, true);
	};
	
	// change les listes en cascades
	this.change = function(liste) {
		var oliste, cpt, iSelection;
		for (var i = liste;i < this.nb; i++) {		
			cpt = 1;
			iSelection = this.est_selectionne(i - 1);	
			this.raz(this.selectbox[i]);
			
			if( iSelection == 1){
				document.getElementById('ville_cache_select').style.display = "block";
				document.getElementById('dpt_cache_select').style.display = "none";
				document.getElementById('align_cache_select').style.display = "none";
				for(var j=0;j<this.liste.length;j++) {
					oliste = this.liste[j];	
	
					if (oliste.profondeur == i && oliste.idp == iSelection) {
						this.selectbox[i].options[cpt++] = new Option( oliste.texte, oliste.valeur); //, oliste.defaut, oliste.defaut);
						if (oliste.defaut){ 
							this.selectbox[i].selectedIndex = cpt-1; // @#$ IE6
						}
					}
				}
			} else if( iSelection != 0){
				document.getElementById('ville_cache_select').style.display = "none";
				document.getElementById('dpt_cache_select').style.display = "block";
				document.getElementById('align_cache_select').style.display = "none";
				if( document.getElementById('cvtheque_region').options[document.getElementById('cvtheque_region').selectedIndex].value == '2'){
					document.getElementById('cvtheque_departement').options.length = 1;
					document.getElementById('cvtheque_departement').options[1] = new Option( '75 - Paris', '75');
					document.getElementById('cvtheque_departement').options[2] = new Option( '77 - Seine-et-Marne', '77');
					document.getElementById('cvtheque_departement').options[3] = new Option( '78 - Yvelines', '78');
					document.getElementById('cvtheque_departement').options[4] = new Option( '92 - Hauts-de-Seine', '92');
					document.getElementById('cvtheque_departement').options[5] = new Option( '93 - Seine-St-Denis', '93');
					document.getElementById('cvtheque_departement').options[6] = new Option( '94 - Val de Marne', '94');
					document.getElementById('cvtheque_departement').options[7] = new Option( '95 - Val d\'oise', '95');
				} else if( document.getElementById('cvtheque_region').options[document.getElementById('cvtheque_region').selectedIndex].value == '3') {
					document.getElementById('cvtheque_departement').options.length = 1;
					document.getElementById('cvtheque_departement').options[1] = new Option( '01 - Ain', '01');
					document.getElementById('cvtheque_departement').options[2] = new Option( '02 - Aisne', '02');
					document.getElementById('cvtheque_departement').options[3] = new Option( '03 - Allier', '03');
					document.getElementById('cvtheque_departement').options[4] = new Option( '04 - Alpes-de-Haute-Provence', '04');
					document.getElementById('cvtheque_departement').options[5] = new Option( '05 - Hautes-Alpes', '05');
					document.getElementById('cvtheque_departement').options[6] = new Option( '06 - Alpes Maritimes', '06');
					document.getElementById('cvtheque_departement').options[7] = new Option( '07 - Ardèche', '07');
					document.getElementById('cvtheque_departement').options[8] = new Option( '08 - Ardennes', '08');
					document.getElementById('cvtheque_departement').options[9] = new Option( '09 - Ariège', '09');
					document.getElementById('cvtheque_departement').options[10] = new Option( '10 - Aube', '10');
					document.getElementById('cvtheque_departement').options[11] = new Option( '11 - Aude', '11');
					document.getElementById('cvtheque_departement').options[12] = new Option( '12 - Aveyron', '12');
					document.getElementById('cvtheque_departement').options[13] = new Option( '13 - Bouches-Du-Rhône', '13');
					document.getElementById('cvtheque_departement').options[14] = new Option( '14 - Calvados', '14');
					document.getElementById('cvtheque_departement').options[15] = new Option( '15 - Cantal', '15');
					document.getElementById('cvtheque_departement').options[16] = new Option( '16 - Charente', '16');
					document.getElementById('cvtheque_departement').options[17] = new Option( '17 - Charente-Maritime', '17');
					document.getElementById('cvtheque_departement').options[18] = new Option( '18 - Cher', '18');
					document.getElementById('cvtheque_departement').options[19] = new Option( '19 - Corrèze', '19');
					document.getElementById('cvtheque_departement').options[20] = new Option( '2A - Corse du Sud', '2A');
					document.getElementById('cvtheque_departement').options[21] = new Option( '2B - Corse du Nord', '2B');
					document.getElementById('cvtheque_departement').options[22] = new Option( '21 - Cotes-d\'Or', '21');
					document.getElementById('cvtheque_departement').options[23] = new Option( '22 - Cote\'Armor', '22');
					document.getElementById('cvtheque_departement').options[24] = new Option( '23 - Creuse', '23');
					document.getElementById('cvtheque_departement').options[25] = new Option( '24 - Dordogne', '24');
					document.getElementById('cvtheque_departement').options[26] = new Option( '25 - Doubs', '25');
					document.getElementById('cvtheque_departement').options[27] = new Option( '26 - Drome', '26');
					document.getElementById('cvtheque_departement').options[28] = new Option( '27 - Eure', '27');
					document.getElementById('cvtheque_departement').options[29] = new Option( '28 - Eure-et-Loir', '28');
					document.getElementById('cvtheque_departement').options[30] = new Option( '29 - Finistere', '29');
					document.getElementById('cvtheque_departement').options[31] = new Option( '30 - Gard', '30');
					document.getElementById('cvtheque_departement').options[32] = new Option( '31 - Haute-Garonne', '31');
					document.getElementById('cvtheque_departement').options[33] = new Option( '32 - Gers', '32');
					document.getElementById('cvtheque_departement').options[34] = new Option( '33 - Gironde', '33');
					document.getElementById('cvtheque_departement').options[35] = new Option( '34 - Hérault', '34');
					document.getElementById('cvtheque_departement').options[36] = new Option( '35 - Ille-et-Vilaine', '35');
					document.getElementById('cvtheque_departement').options[37] = new Option( '36 - Indre', '36');
					document.getElementById('cvtheque_departement').options[38] = new Option( '37 - Indre-et-Loire', '37');
					document.getElementById('cvtheque_departement').options[39] = new Option( '38 - Isère', '38');
					document.getElementById('cvtheque_departement').options[40] = new Option( '39 - Jura', '39');
					document.getElementById('cvtheque_departement').options[41] = new Option( '40 - Landes', '40');
					document.getElementById('cvtheque_departement').options[42] = new Option( '41 - Loir-et-Cher', '41');
					document.getElementById('cvtheque_departement').options[43] = new Option( '42 - Loire', '42');
					document.getElementById('cvtheque_departement').options[44] = new Option( '43 - Hautes-Loire', '43');
					document.getElementById('cvtheque_departement').options[45] = new Option( '44 - Loire-Atlantique', '44');
					document.getElementById('cvtheque_departement').options[46] = new Option( '45 - Loiret', '45');
					document.getElementById('cvtheque_departement').options[47] = new Option( '46 - Lot', '46');
					document.getElementById('cvtheque_departement').options[48] = new Option( '47 - Lot-et-Garonne', '47');
					document.getElementById('cvtheque_departement').options[49] = new Option( '48 - Lozère', '48');
					document.getElementById('cvtheque_departement').options[50] = new Option( '49 - Maine-et-Loire', '49');
					document.getElementById('cvtheque_departement').options[51] = new Option( '50 - Manche', '50');
					document.getElementById('cvtheque_departement').options[52] = new Option( '51 - Marne', '51');
					document.getElementById('cvtheque_departement').options[53] = new Option( '52 - Haute-Marne', '52');
					document.getElementById('cvtheque_departement').options[54] = new Option( '53 - Mayenne', '53');
					document.getElementById('cvtheque_departement').options[55] = new Option( '54 - Meurthe-et-Moselle', '54');
					document.getElementById('cvtheque_departement').options[56] = new Option( '55 - Meuse', '55');
					document.getElementById('cvtheque_departement').options[57] = new Option( '56 - Morbihan', '56');
					document.getElementById('cvtheque_departement').options[58] = new Option( '57 - Moselle', '57');
					document.getElementById('cvtheque_departement').options[59] = new Option( '58 - Nievre', '58');
					document.getElementById('cvtheque_departement').options[60] = new Option( '59 - Nord', '59');
					document.getElementById('cvtheque_departement').options[61] = new Option( '60 - Oise', '60');
					document.getElementById('cvtheque_departement').options[62] = new Option( '61 - Orne', '61');
					document.getElementById('cvtheque_departement').options[63] = new Option( '62 - Pas-De-Calais', '62');
					document.getElementById('cvtheque_departement').options[64] = new Option( '63 - Puy-de-Dome', '63');
					document.getElementById('cvtheque_departement').options[65] = new Option( '64 - Pyrénées-Atlantiques', '64');
					document.getElementById('cvtheque_departement').options[66] = new Option( '65 - Hautes-Pyrénées', '65');
					document.getElementById('cvtheque_departement').options[67] = new Option( '66 - Pyrénées-Orientales', '66');
					document.getElementById('cvtheque_departement').options[68] = new Option( '67 - Bas-Rhin', '67');
					document.getElementById('cvtheque_departement').options[69] = new Option( '68 - Haut-Rhin', '68');
					document.getElementById('cvtheque_departement').options[70] = new Option( '69 - Rhône', '69');
					document.getElementById('cvtheque_departement').options[71] = new Option( '70 - Haute-Saône', '70');
					document.getElementById('cvtheque_departement').options[72] = new Option( '71 - Saône-et-Loire', '71');
					document.getElementById('cvtheque_departement').options[73] = new Option( '72 - Sarthe', '72');
					document.getElementById('cvtheque_departement').options[74] = new Option( '73 - Savoie', '73');
					document.getElementById('cvtheque_departement').options[75] = new Option( '74 - Haute-Savoie', '74');
					document.getElementById('cvtheque_departement').options[76] = new Option( '76 - Seine-Maritime', '76');
					document.getElementById('cvtheque_departement').options[77] = new Option( '79 - Deux Sèvres', '79');
					document.getElementById('cvtheque_departement').options[78] = new Option( '80 - Somme', '80');
					document.getElementById('cvtheque_departement').options[79] = new Option( '81 - Tarn', '81');
					document.getElementById('cvtheque_departement').options[80] = new Option( '82 - Tarn-et-Garonne', '82');
					document.getElementById('cvtheque_departement').options[81] = new Option( '83 - Var', '83');
					document.getElementById('cvtheque_departement').options[82] = new Option( '84 - Vaucluse', '84');
					document.getElementById('cvtheque_departement').options[83] = new Option( '85 - Vendee', '85');
					document.getElementById('cvtheque_departement').options[84] = new Option( '86 - Vienne', '86');
					document.getElementById('cvtheque_departement').options[85] = new Option( '87 - Haute-Vienne', '87');
					document.getElementById('cvtheque_departement').options[86] = new Option( '88 - Vosges', '88');
					document.getElementById('cvtheque_departement').options[87] = new Option( '89 - Yonne', '89');
					document.getElementById('cvtheque_departement').options[88] = new Option( '90 - Territoire de Belfort', '90');
				}
			} else {
				document.getElementById('cvtheque_departement').options.length = 1;
				document.getElementById('ville_cache_select').style.display = "none";
				document.getElementById('dpt_cache_select').style.display = "none";
				document.getElementById('align_cache_select').style.display = "block";
			}
		}
	};
	
	this.selectionner= function(iListe, sVal) {
		var oLst = this.selectbox[iListe -1];		
		for(var i=0;i<oLst.options.length;i++) {		
			if (oLst.options[i].value == sVal) {
				oLst.selectedIndex = i;		
				this.change(iListe);
				break;
			}
		}
	};
}
