/*=========================================================================
OBJECT: AideSaisieListe
DESC: Affichage d'une liste filtree en fonction de la saisie (filtre sur code et libelle) 
TEST PLATFORMS: IE
CREAT S GUERY
============================================================================*/

function Item(code, libelle) {
	this.code=code;
	this.libelle=libelle;
	this.affichage=libelle;// + " (" + code + ")";
}

function CreerListe(nom, hauteur, largeur, layer) {
	this.nom=nom; this.hauteur=hauteur; this.largeur=largeur;
	this.layer=layer;
	this.search="";
	this.nb=0;//nombre d'item
	this.Add=AjouterItem;
	this.Afficher=AfficherListe;
	this.AfficherParLettre=AfficherParLettre;
	this.MAJ=MAJListe;
	this.MAJSimple=MAJSimple;
	this.Selection=Selection;
	this.CodePresent=CodePresentDsListe;
	this.end=false;
	this.creationContenu=creationContenuDiv;
	this.contenuDiv;
	this.affListe=affContenuListe;
	this.tabItem = new Array();
	
	//Tableau permettant de savoir si on a deja ajouter les escales ds la liste
	this.tabLettres = new Array(26);
	this.tabLettres['A']=0;
	this.tabLettres['B']=0;
	this.tabLettres['C']=0;
	this.tabLettres['D']=0;
	this.tabLettres['E']=0;
	this.tabLettres['F']=0;
	this.tabLettres['G']=0;
	this.tabLettres['H']=0;
	this.tabLettres['I']=0;
	this.tabLettres['J']=0;
	this.tabLettres['K']=0;
	this.tabLettres['L']=0;
	this.tabLettres['M']=0;
	this.tabLettres['N']=0;
	this.tabLettres['O']=0;
	this.tabLettres['P']=0;
	this.tabLettres['Q']=0;
	this.tabLettres['R']=0;
	this.tabLettres['S']=0;
	this.tabLettres['T']=0;
	this.tabLettres['U']=0;
	this.tabLettres['V']=0;
	this.tabLettres['W']=0;
	this.tabLettres['X']=0;
	this.tabLettres['Y']=0;
	this.tabLettres['Z']=0;
	

}


function CodePresentDsListe(champ){
	var code = champ.value;
	
//	this.affListe();
	
   	var s="";
	for (var i=0; i<this.nb; i++) {
		s+=this.tabItem[i].code+'-	';
		if(this.tabItem[i].code == code){
			//champ.value = code
			return true;
		}
	}

	//champ.value="";
	return false;
}


function AjouterItem(code, libelle) {
	var item = new Item(code, libelle);
	this.tabItem[this.nb]=item;
	this.nb++;
}

//Cette fction permet de generer la suite du select de la fction AfficherListe et la stoque ds une variable de l'objet
//Cela ds le seul but d'eviter de regenerer ce code html pr ttes les listes pr gagner en perf
function creationContenuDiv(){
	for (var i=0; i<this.nb; i++) {
			this.contenuDiv += "<OPTION value=\""+this.tabItem[i].code+"\">"+this.tabItem[i].affichage+"</OPTION>"		
	}
	this.contenuDiv += "</SELECT>";
}

function AfficherListe(formName) {


	var Z;
	if (document.layers) {
		Z="<SELECT onBlur='MasquerLayer(\""+this.layer+"\")' name='select"+this.nom+"' class='form6' size="+this.hauteur+" onClick=\"liste"+this.nom+".Selection('"+formName+"')\" onkeyup=\"quelleTouche(event, '"+this.nom+"')\" >";
	} else {
		Z="<SELECT onBlur='MasquerLayer(\""+this.layer+"\")' name='select"+this.nom+"' class='form6' size="+this.hauteur+" style='width:"+this.largeur+"' onClick='tabListes[\""+this.nom+"\"].Selection(\""+formName+"\")' "+this.nom+" onkeyup=\"quelleTouche(event, '"+this.nom+"')\" >";
	}

	Z += this.contenuDiv;

	return Z;
}

function AfficherParLettre(formName, defaultLetter) {
	var Z;
	if (document.layers) {
		Z="<SELECT name='select"+this.nom+"' class='form5' size="+this.hauteur+" >";
	} else {
		Z="<SELECT name='select"+this.nom+"' class='form5' size="+this.hauteur+" style='width:"+this.largeur+"' >";
	}
	for (var i=0; i<this.nb; i++) {
		Z+="<OPTION value=\""+this.tabItem[i].code+"\">"+this.tabItem[i].affichage+"</OPTION>"		
	}
	Z+="</SELECT>"
	document.write(Z);
	eval(formName+".select"+this.nom+".selectedIndex=-1;");

	this.MAJSimple(defaultLetter, document.forms[formName]);
}

function Selection(formName) {
	var monSelect=document.forms[formName].elements["select"+this.nom];
	if(monSelect.options.length==0) return false;

	var selection = monSelect.options[monSelect.options.selectedIndex].text;
	document.forms[formName].elements[this.nom].value = selection;

	var index = parseInt(selection.lastIndexOf('('));
	var aAfficher = selection.substring(index+1, index+4);
	
	document.forms[formName].elements[this.nom+"Code"].value = aAfficher;

	this.search=monSelect.options[monSelect.options.selectedIndex].text;
	this.end=true;
	MasquerLayer(this.layer);
	
	//Si this.nom est defini on doit mettre à jour les liste de classes de transport
	if(typeof this.nom != "undefined"){

		var nomChamp = this.nom

		//on recupere le numero parcours et le numero trajet
		var temp = nomChamp.substring(nomChamp.indexOf('[')+1);
		var numParcours = nomChamp.substring(nomChamp.indexOf('[')+1, nomChamp.indexOf('[')+2);
		var numTrajet = temp.substring(temp.indexOf('[')+1, temp.indexOf('[')+2);

	}
	//On redonne le focus au champ de saisi pour qu'une fois qu'on le quitte les appels AJax puissent se faire
	if (isFocusEnabled==1) {
		document.forms[formName].elements[this.nom].focus();
	}
}



function MAJListe(txtSearch,monForm) {

	if (txtSearch!=this.search) {
		this.end=false;
		this.search=txtSearch;
		var o;

		monForm.elements["select"+this.nom].options.length=0; 
		for (var i=0; i<this.nb; i++) {
			if ( this.tabItem[i].libelle.substring(0,txtSearch.length).toUpperCase()==txtSearch.toUpperCase() ) {

				o=new Option(this.tabItem[i].affichage, this.tabItem[i].code);
				monForm.elements["select"+this.nom].options[monForm.elements["select"+this.nom].options.length]=o;
			}
			else if ( this.tabItem[i].code.substring(0,txtSearch.length).toUpperCase()==txtSearch.toUpperCase() ) {
				o=new Option(this.tabItem[i].affichage, this.tabItem[i].code);
				monForm.elements["select"+this.nom].options[monForm.elements["select"+this.nom].options.length]=o;
			}
		}
		if (monForm.elements["select"+this.nom].options.length==1) {
			monForm.elements["select"+this.nom].selectedIndex=0;
			this.Selection(monForm.name,1);
		}
		if (monForm.elements["select"+this.nom].options.length==0) {
			this.end=true;
		}
		//Si la taille est superieure a 1 alrs aucune escale n'est selectionne
		//On vide le champ cache contenant potentiellement un code escale obsolete
		if (monForm.elements["select"+this.nom].options.length>1) {
			document.forms[monForm.name].elements[this.nom+"Code"].value="";
		}
	}
	
}


// Reduction de la liste des elements presents sur la base du debut de la chaine en ne gardant que les elements qui matchent.
function MAJSimple(txtSearch,monForm) {

	if (txtSearch!=null) {
		this.end=false;
		this.search=txtSearch;
		var o;
		monForm.elements["select"+this.nom].options.length=0; 
		for (var i=0; i<this.nb; i++) {
			if ( this.tabItem[i].libelle.substring(0,txtSearch.length).toUpperCase()==txtSearch.toUpperCase() ) {
				o=new Option(this.tabItem[i].affichage, this.tabItem[i].code);
				monForm.elements["select"+this.nom].options[monForm.elements["select"+this.nom].options.length]=o;
			}

		}
		
		if (monForm.elements["select"+this.nom].options.length==1) {
			//monForm.elements["select"+this.nom].selectedIndex=0;
		}
		if (monForm.elements["select"+this.nom].options.length==0) {
			this.end=true;
		}
	}
}

function AfficherLayer(nomlayer) {
	if (document.all) {
		if (nomlayer!="") {document.all[nomlayer].style.visibility="visible";}
	}
	if (document.layers) {
		if (nomlayer!="") {document.layers[nomlayer].visibility="show";}
	}
	else {
		if (nomlayer!="") {document.getElementById(nomlayer).style.visibility="visible";}
	}
}

function MasquerLayer(nomlayer) {

	if (document.all) {
		if (nomlayer!="") {document.all[nomlayer].style.visibility="hidden";}
	}
	if (document.layers) {
		if (nomlayer!="") {document.layers[nomlayer].visibility="hide";}
	}
	else {
			//alert(document.getElementById(nomlayer).style.visibility);
		if (nomlayer!="") {document.getElementById(nomlayer).style.visibility="hidden";}
	}

}

function affContenuListe(){

   	var s="";
	for (var i=0; i<this.nb; i++) {
		s+=this.tabItem[i].code+'-	';
	}
	alert(s);
}






