var numSelect = -1;
var motSel = "";
var actionEnCours = "ajout";
var tmp = "";

window.onkeypress = gestKey;

function gestKey(evenement)
{
	//alert("Vous avez appuyé sur une touche avec la valeur " + evenement.which);
	if (evenement.which == 13){
		switch(actionEnCours){
			case "ajout":
				ajout();
				break;
			case "modif":
				modif()
				break;
		}
		return false;
	}
	else if (evenement.which == 35) return false;
	window.captureEvents(Event.KEYPRESS);
	window.onkeypress = sortie;
}

function maj(){
	numSelect = document.forms['formMC'].listMot.selectedIndex;
	motSel = document.forms['formMC'].listMot.options[numSelect].text;
	document.forms['formMC'].mot.value = motSel;
	document.forms['formMC'].mot.select(true);
	actionEnCours = "ajout";
}

function verifNoExist(valeur){
	for (i=0; i<document.forms['formMC'].listMot.length; i++){
		if (document.forms['formMC'].listMot.options[i].text == valeur) return true;
	}
	return false;
}

function ajout(){
	if (document.forms['formMC'].mot.value != ""){
		existe = verifNoExist(document.forms['formMC'].mot.value);
		if (existe == false){ 
			newIndex = document.forms['formMC'].listMot.length;
			newOpt = "mot_" + (newIndex-1);
			newValue = document.forms['formMC'].mot.value;
			var opt = new Option(newValue, newOpt);
			document.forms['formMC'].listMot.options.add(opt, newIndex);
			//document.forms['formMC'].listMot.options[newIndex].selected = true;
			document.forms['formMC'].mot.value = "";
			document.forms['formMC'].listMot.options[numSelect].selected = false;
			maj();
		}
		else alert("Cette valeur existe déjà.");
	}
}

function suppr(){
	if(document.forms['formMC'].listMot.selectedIndex != -1){
		msg = "\"" + motSel + "\" va être supprimer de la liste.\r\nCliquez sur OK pour continuer.";
		go = window.confirm(msg);
		if (go) document.forms['formMC'].listMot.options[numSelect] = null;
		if (document.forms['formMC'].listMot.length !=0){
			if (numSelect == 0) newSelect = 0;
			else newSelect = numSelect -1;
			document.forms['formMC'].listMot.options[newSelect].selected = true;
			maj();
		}
		else{
			document.forms['formMC'].mot.value = "";
			numSelect = -1;
		}
	}
}

function modif(){
	if(document.forms['formMC'].listMot.selectedIndex != -1){
		document.forms['formMC'].listMot.options[numSelect].text = document.forms['formMC'].mot.value;
		document.forms['formMC'].mot.value = "";
		document.forms['formMC'].listMot.options[numSelect].selected = false;
		maj();
		actionEnCours = "modif";
	}
}

function ordonn(sens){
	if(document.forms['formMC'].listMot.selectedIndex != -1 && ((sens == "bas" && numSelect != document.forms['formMC'].listMot.length-1) || (sens == "ht" && numSelect != 0))){
		if (sens == "ht") indexModif = numSelect - 1;
		else indexModif = numSelect + 1;
		tmp = document.forms['formMC'].listMot.options[indexModif].text;
		document.forms['formMC'].listMot.options[indexModif].text = document.forms['formMC'].listMot.options[numSelect].text;
		document.forms['formMC'].listMot.options[numSelect].text = tmp;
		document.forms['formMC'].listMot.options[indexModif].selected = true;
		maj();
	}	
}

function envoi(){
	var chaine = "";
	// On parcourt la liste,et on met à jour le champ qui servira pour la maj de la base
	for (i=0; i<document.forms['formMC'].listMot.length; i++){
		if (chaine == "") chaine = document.forms['formMC'].listMot.options[i].text;
		else chaine = chaine + "#" + document.forms['formMC'].listMot.options[i].text;
	}
	document.forms['formMC'].listTrans.value = chaine;
	return true;	
}

function verif_champ(){
	if (document.forms['formMC'].nom.value == ""){
		alert("Vous n'avez pas renseigné votre nom.");
		return false;
	}
	else return true;
}
