// -- Gestion des listes d'échange ---

sortitems = 1;  // Tri automatiques des listes ? (1=oui ou 0=non)

//Fonction permettant de sélectionner tous les éléments d'une liste
function clicToutSelectListe(idListe)
{
	var liste = document.getElementById(idListe);
	
	for(var i=0; i<liste.options.length; i++)
	{
		liste.options[i].selected = true;
	}
}

//Fonction permettant de désélectionner tous les éléments d'une liste
function clicToutDeselectListe(idListe)
{
	var liste = document.getElementById(idListe);
	
	for(var i=0; i<liste.options.length; i++)
	{
		liste.options[i].selected = false;
	}
}

//Fonction permettant de concaténer les valeurs d'une liste dans un champ caché 
function remplirConcat(idChampConcat,idListe)
{
	var champConcat = document.getElementById(idChampConcat);
	var liste = document.getElementById(idListe);
	
	champConcat.value="_$_";
	for(var k=0; k<liste.options.length; k++)
	{
		champConcat.value += liste.options[k].value + "_-_" + liste.options[k].text + "_$_";
	}
}

//Fonction qui passe tous les éléments de la liste de choix à la liste de sélection
function clicToutAjoutEchange(idListeGauche,idListeDroite,idChampConcat)
{
	clicToutSelectListe(idListeGauche);
	clicAjoutEchange(idListeGauche,idListeDroite,idChampConcat);
	return false;
}

//Fonction qui passe un élément de la liste de choix à la liste de sélection
function clicAjoutEchange(idListeGauche,idListeDroite,idChampConcat)
{
	var listeGauche = document.getElementById(idListeGauche);
	var listeDroite = document.getElementById(idListeDroite);
	var champConcat = document.getElementById(idChampConcat);
	
	for(var i=0; i<listeGauche.options.length; i++)
	{
		var dejaSelect = false;
		if(listeGauche.options[i].selected && listeGauche.options[i].value != "")
		{ 
			for (var j=0; j<listeDroite.options.length; j++)
			{
				// vérification pour savoir si l'élément à déplacer existe déjà à droite
				if (listeGauche.options[i].value == listeDroite.options[j].value)
				{
					dejaSelect = true;
				}
			}
			if (!dejaSelect == true)
			{
				var no = new Option();
				no.value = listeGauche.options[i].value;
				no.text = listeGauche.options[i].text;
				listeDroite.options[listeDroite.options.length] = no;
				//listeGauche.options[i].value = "";
				//listeGauche.options[i].text = "";	
			}
		}
	}
	
	//reformaterListe(listeGauche);		//formatage de la liste de gauche après ajout
	trierListeEchange(listeDroite);		//tri des éléments de la liste de droite
	
	clicToutDeselectListe(idListeGauche);
	clicToutDeselectListe(idListeDroite);
	
	// concaténation des éléments sélectionnés
	remplirConcat(idChampConcat,idListeDroite);
	
	return false;
}

//Fonction qui supprime tous les éléments de la liste
function clicToutSupprEchange(idListeGauche,idListeDroite,idChampConcat)
{
	clicToutSelectListe(idListeDroite);
	clicSupprEchange(idListeGauche,idListeDroite,idChampConcat);
	return false;
}

//Fonction qui supprime un élément de la liste
function clicSupprEchange(idListeGauche,idListeDroite,idChampConcat)
{
	var listeGauche = document.getElementById(idListeGauche);
	var listeDroite = document.getElementById(idListeDroite);
	var champConcat = document.getElementById(idChampConcat);
	
	for(var i=0; i<listeDroite.options.length; i++)
	{
		if(listeDroite.options[i].selected && listeDroite.options[i].value != "")
		{ 
			var no = new Option();
			no.value = listeDroite.options[i].value;
			no.text = listeDroite.options[i].text;
			//listeGauche.options[listeGauche.options.length] = no;
			listeDroite.options[i].value = "";
			listeDroite.options[i].text = "";	
		}
	}
	
	reformaterListe(listeDroite);		//formatage de la liste de droite après suppression
	//trierListeEchange(listeGauche);	//tri des éléments de la liste de gauche
	
	clicToutDeselectListe(idListeGauche);
	clicToutDeselectListe(idListeDroite);
	
	// concaténation des éléments sélectionnés
	remplirConcat(idChampConcat,idListeDroite);
	
	return false;
}

//Fonction qui reformate une liste après un ajout/suppression
function reformaterListe(box)
{
	for(var i=0; i<box.options.length; i++)
	{
		if(box.options[i].value == "")
		{
			for(var j=i; j<box.options.length-1; j++)
			{
				box.options[j].value = box.options[j+1].value;
				box.options[j].text = box.options[j+1].text;
			}
			var ln = i;
			break;
		}
	}
	if(ln < box.options.length)
	{
		box.options.length -= 1;
		reformaterListe(box);
	}
}

//Fonction de tri de la liste
function trierListeEchange(box)
{
	if (sortitems)
	{
		trierListe(box)
	}
}

// Fonctions de tri (quicksort)

function trierListe(box)
{
	quicksort(box,0,box.length)
}

function quicksort(box,lower,upper)
{
	if (lower < upper)
	{
	    middle = partition(box, lower, upper)
	    quicksort(box, lower, middle)
	    quicksort(box, middle + 1, upper)
	}
}

function partition(box,lower,upper)
{
	pivot = box.options[lower].text
	ptr = lower
	for (i = lower + 1; i < upper; i++)
	{
	    if (box.options[i].text.toUpperCase() < pivot.toUpperCase())
		{
			ptr ++
			swap(box, ptr, i)
		}
	}
	swap(box, lower, ptr)
	return ptr
}
    
function swap(box,i,j)
{
	tmp = box.options[i].text
	tmp2 = box.options[i].value
	box.options[i].text = box.options[j].text
	box.options[i].value = box.options[j].value
	box.options[j].text = tmp
	box.options[j].value = tmp2
}
