//////////////////////////////////////////////////////////////////////
// Sert de backup, lorsqu'on entre autre chose qu'un nombre
//////////////////////////////////////////////////////////////////////
var inputBackup;

function inputOnKeyDown (inputName) {
  inputBackup = inputName.value;
}

//////////////////////////////////////////////////////////////////////
// Verifie que le contenu d'un champs input est bien un nombre
// Le Formatte type x.xxx.xxx,xx
//////////////////////////////////////////////////////////////////////
function inputOnKeyUp (inputName) {	
  if (window.event.type == "keyup" & window.event.keyCode > 31) {
    var nombre;
	nombre = inputName.value;
	if (isNaN (toSt (nombre))) {
		showAlert ();
//		inputName.value = "0,00";
inputName.value = toMontant (toSt (inputBackup));
		return
	} 
    else {
      inputName.value = toMontant (toSt (nombre));
	}
  }
}

//////////////////////////////////////////////////////////////////////
// Supprime les points dans un nombre
// Change les virgules en point ensuite
//////////////////////////////////////////////////////////////////////
function toSt (aNum) {
  while (aNum.indexOf (".") > 0) {
    aNum = aNum.replace (".", "");
  }
  aNum = aNum.replace (",", ".");
  return aNum;
}

//////////////////////////////////////////////////////////////////////
// Affichage d'un message d'alerte 
// Si plus tard, plusieurs langue, changer la fonction ci-dessous
//////////////////////////////////////////////////////////////////////
function showAlert () {
  alert ("Entrez un nombre positif ou égal à 0 !");
}

//////////////////////////////////////////////////////////////////////
// Formate le nombre en separant tous les 3 chiffres avec un point
// et ne laisse que 2 decimales
//////////////////////////////////////////////////////////////////////
function toMontant (aNum) {
  var tabString = new Array ();
  var retour, car, j;
	
  tabString = aNum.split (".");
  retour = "";
  j = 0;
  for (var i = 0; i < tabString [0].length; i++) {
    car = tabString [0].charAt (tabString [0].length - 1 - i);
	if (j == 3) {
  	  j = 1;
	  retour = car + "." + retour;
  	}
	else {
	  retour = car + retour;
  	  j = j + 1;
	}
  }
  if (tabString [1] != null) {
    retour = retour + "," + tabString [1].substring (0, 2);
  }
  return retour;
}

//////////////////////////////////////////////////////////////////////
// Verifie que le contenu d'un champs input est bien avec deux decimales
//////////////////////////////////////////////////////////////////////
function inputOnBlur (inputName) {	
  inputName.value = toMontant (toSt (inputName.value));
  if (inputName.value == "") {
    inputName.value = "0,00";
  }
  else {
    if (inputName.value.indexOf (",") == -1) {
      inputName.value = inputName.value + ",00";
    }
    else {
      if (inputName.value.indexOf (",") == 0) {
        inputName.value = "0" + inputName.value;
      }
      else {
        if (inputName.value.length - inputName.value.indexOf (",") == 2) {        
          inputName.value = inputName.value + "0";
        }
        else {
          if (inputName.value.length - inputName.value.indexOf (",") == 1) {        
            inputName.value = inputName.value + "00";
          }
        }
      }
    }
  }
}

//////////////////////////////////////////////////////////////////////
// Place la valeur dans le backup et selectionne le champs
//////////////////////////////////////////////////////////////////////
function inputOnFocus (inputName) {
  inputBackup = inputName.value;
  inputName.select ();
}

//////////////////////////////////////////////////////////////////////
// Permet le passage d'option d'un multiselect a l'autre
//////////////////////////////////////////////////////////////////////
function moveOption (fromSelect, toSelect) {
  var tFromSelectText = new Array();
  var tFromSelectValue = new Array();
  var tToSelectText = new Array();
  var tToSelectValue = new Array();

  var indexToSelect = 0;
  var indexFromSelect = 0;

    for (i = 0; i < toSelect.options.length; i++) {
      tToSelectText [indexToSelect] = toSelect.options [i].text;
      tToSelectValue [indexToSelect] = toSelect.options [i].value;
      indexToSelect++;
    }

    for (i = 0; i < fromSelect.options.length; i++) {
      if (fromSelect.options [i].selected && fromSelect.options [i].value != "") {    
        tToSelectText [indexToSelect] = fromSelect.options [i].text;
        tToSelectValue [indexToSelect] = fromSelect.options [i].value;
        indexToSelect++;
      }
      else {
        tFromSelectText [indexFromSelect] = fromSelect.options [i].text;
        tFromSelectValue [indexFromSelect] = fromSelect.options [i].value;
        indexFromSelect++;
      }
    }

  toSelect.length = 0;
  fromSelect.length = 0;
  // les tableaux sont prets, on recree les options
  for (i = 0; i < tFromSelectText.length; i++) {
	var no = new Option ();
	no.value = tFromSelectValue [i];
	no.text = tFromSelectText [i];
    fromSelect.options.add (no);
  }

  for (i = 0; i < tToSelectText.length; i++) {
	var no = new Option ();
	no.value = tToSelectValue [i];
	no.text = tToSelectText [i];
    toSelect.options.add (no);
  }
}