
//  Objet de formatage des champs numériques,
//  il définit le séparateur de milliers, le séparateur des décimaux et le nombre de décimaux à afficher.
function FormatNumerique(separateurMilliers, separateurDecimal, nbDecimaux, unite){
  this.separateurM = separateurMilliers;
  this.separateurD = separateurDecimal;
  this.nbD = nbDecimaux;
  this.unite = unite;
}

/**
 * Fonction qui crée un objet FormatNumerique à partir d'un format passé au format txt en paramètre.
 * Ce format doit etre composé de chiffres ainsi que les séparateurs désirés.
 * Ex : 0 000,0 ou 0.000 ou 0,000,000.00
 */
function initFormatNumerique(val){
  chiffres = "#";
  sepM = "";
  sepD = "";
  sepDok = false;
  nbD = 0;
  unite = "";
  for(i=val.length-1; i>=0; i--){
    car = val.charAt(i);
    if(chiffres.indexOf(car) == -1){
      if(sepDok == false){
        if(car == "," || car =="."){
          sepD = car;
          sepDok = true;
        }else if(car == " " || car ==","){
          sepM = car;
          break;
        }else{
          unite = unite+car;
        }
      }else{
        if(car == sepD){
          sepD = "";
        }
        sepM = car;
        break;
      }
    }
    if(sepDok == false){
      if(car == '#'){
        nbD = nbD + 1;
      }
    }
  }
  if(sepD == ""){
    nbD = 0;
  }
  return new FormatNumerique(sepM, sepD, nbD, unite);
}
/**
 * Retire tous les caractères qui ne sont pas des chiffres dans une chaine de caractère
 */
function retireCharsNonNumeriques(val){
  entier = "";
  for(i=0; i<val.length; i++){
    car = val.charAt(i);
    if("0123456789".indexOf(car) != -1){
      entier = entier + car;
    }
  }
  return entier;
}

/**
 * Fonction de formatage de la partie entière d'un nombre.
 * prends en paramètre la valeur entière et un objet de type FormatNumerique.
 */
function formatPartieEntiere(val, format){
  cpt = 1;
  entier = "";
  for(i=val.length-1; i>=0; i--){
    car = val.charAt(i);
    entier = car + entier;
    if(cpt == 3 && i !=0){
      entier = format.separateurM + entier;
      cpt = 0;
    }
    cpt++;
  }
  return entier;
}

/**
 * Fonction de formatage de la partie décimale d'un nombre.
 *  prends en paramètre la valeur entière et un objet de type FormatNumerique.
 */
function formatPartieDecimale(val, format){
  bool = true
  index0 = 0;
  nb0 = "";
  if(val.length == format.nbD){
    return val;
  }else if(val.length < format.nbD+1){
    for(i=0; val.length<format.nbD; i++){
      val = val + "0";
    }
	return val;
  }else{
    while(bool = true){
      if(val.charAt(index0) == "0"){
	    nb0 = nb0+"0";
	  }else{
	    bool = true;
	    break;
	  }
	  index0 = index0+1;
    }
	decimaux = val;
	if(val.length > format.nbD){
      decimaux = val.substring(0, format.nbD)+"."+val.substring(format.nbD, val.length);
	}
    num = Math.round(decimaux);
	cas9 = "1";
	for(j=0; j<format.nbD; j++){
	  cas9 = cas9 + "0";
	}
	cas0 = "1";
	for(j=0; j<format.nbD-1; j++){
	  cas0 = cas0 + "0";
	}

	if(num == Math.round(cas9)){
	  return num - 1+'';
	}else{
	if(num != Math.round(cas0)){ // Modif patrice cas partie décimale = 096 à arrondir sur 2 chiffre
      decimaux = nb0+""+num;
	}else{
	  decimaux = ""+num
	}
      for(i=0; decimaux.length<format.nbD; i++){
        decimaux = decimaux + "0";
      }
      return decimaux;
	}
  }
}

/**
 * Fonction Principale de formatage des champs numeriques
 * elle prends en parametre une velaur et un format en txt
 * elle retourne la valeur formatée.
 */
function formatNumerique(val, valformat){
  if(val.indexOf("E") != -1){
      return val;
    }
  if(valformat.length == 0){
    return val;
  }
  chiffresvalue = "0123456789";
  var premscar = "";
  if(val.indexOf('-') == 0){
    premscar = "-";
    val = val.substring(1);
  }
  var monformat = initFormatNumerique(valformat);
  decimaux = "";
  entiers = "";
  separateur = false;
  for(i=val.length-1; i>=0; i--){
    car = val.charAt(i);
    if(chiffresvalue.indexOf(car) != -1){
      decimaux = car + decimaux;
    }
    else{
      if(car == "," || car == "."){
        separateur = true;
        entiers = val.substring(0, i);
        break;
      }
    }
  }
  if(separateur == false){
  entiers = decimaux;
  decimaux = "0";
  }
  entiers = retireCharsNonNumeriques(entiers);
  entiers = formatPartieEntiere(entiers, monformat);
  decimaux = formatPartieDecimale(decimaux, monformat);
  if(decimaux == "-1"){
  entiers = Math.round(entiers)+1;
  decimaux = "";
  for(i=0; i<(monformat.nbD); i++){
      decimaux = decimaux + "0";
    }
  }
  if(monformat.separateurD == ""){
    return premscar + entiers
  }else{
    return premscar + entiers + monformat.separateurD + decimaux+" "+monformat.unite;
  }
}

/**
 * Formatage et mise à jour d'un champ numérique
 */
function transformeNumerique(val, formattxt, id){
 val = formatNumerique(val, formattxt);
 document.getElementById(id).value = val;
 if(document.getElementById(id+"onglet")){
   document.getElementById(id+"onglet").value = val;
 }
}

/**
 * Retire le formatage d'un champ numérique
 */
function transformeNumeriquePourCalcul(val, valformat){
 chiffres ="0123456789."
 if(valformat){
   formattmp = initFormatNumerique(valformat);
   if(formattmp.unite != ""){
     val = replaceAll(val, formattmp.unite, '');
   }
   if(formattmp.separateurM != ""){
     val = replaceAll(val, formattmp.separateurM, '');
   }
   if(formattmp.separateurD != ""){
     val = replaceAll(val, formattmp.separateurD, '.');
   }else{
     val = val + ".0";
   }
 }
 return val;
}


/**
 * formatage des champs de type téléphone
 */
function transformeChampTelephone(val, format, id){
  if(document.getElementById('checktel'+id).checked){
	// On formate le numero de tel
	onNeFaitRien = false;
    if(format.length == 0){
      format = val;
    }else if(val.length == 0){
      format = val;
    }else{
      j=0;
      chiffres = "+0123456789";
      val2 = "";
      for(i=0; i<val.length; i++){
        car = val.charAt(i);
        if(chiffres.indexOf(car) != -1){
          val2 = val2 + car;
        }
      }
      nbchiffre = val2.length;
      for(i=0; i<format.length; i++){
        car = format.charAt(i);
        if(car == "#"){
          format = format.substring(0, i) + val2.charAt(j) + format.substring(i+1, format.length);
          j++;
        }
      }
      if(nbchiffre > j){
    	 // Il y a plus de chiffre que de # 
    	  onNeFaitRien = true;
      }
    }
    if(!onNeFaitRien){
      // cas normal
      document.getElementById(id).value = format;
      if(document.getElementById(id+"onglet")){
        document.getElementById(id+"onglet").value = format;
      }
      document.getElementById("labeltel"+id).innerHTML="<img src='"+contextpath+"/images/formater.gif' width='16px' height='16px'/>";
    }else{
      // Cas anormal
      //alert("Impossible de formater ce numéro");
      document.getElementById(id).value = val;
      if(document.getElementById(id+"onglet")){
        document.getElementById(id+"onglet").value = val;
      }
      document.getElementById("labeltel"+id).innerHTML="<img src='"+contextpath+"/images/nonformater.gif' width='16px' height='16px'/>";
    }
  }else{
    document.getElementById("labeltel"+id).innerHTML="<img src='"+contextpath+"/images/nonformater.gif' width='16px' height='16px'/>";
  }
}




/**
 * Formatage des champs de type code postal
 */
function calculCP(val, format){
  if(format.length == 0){
    format = val;
  }else if(val.length == 0){
    format = val;
  }else{
    valtmp = replaceAll(val, " ","");
    formattmp = replaceAll(format, " ","");
    if(valtmp.length != formattmp.length){
      return val;
    }
    j=0;
    chiffres = "0123456789";
    val2 = "";
    for(i=0; i<val.length; i++){
      car = val.charAt(i);
      if(chiffres.indexOf(car) != -1){
        val2 = val2 + car;
      }
    }
    for(i=0; i<format.length; i++){
      car = format.charAt(i);
      if(car == "#"){
        format = format.substring(0, i) + val2.charAt(j) + format.substring(i+1, format.length);
        j++;
      }
    }
  }
  return format;
}

/**
 * Formatage et maj d'un champ de type code postal.
 */
function formatCP(val, format, id){
if(document.getElementById('checkcpcodepost').checked){
  var codepost = calculCP(val, format);
  document.getElementById(id).value = codepost;
  document.getElementById("labelcpf").style.display="block";
  document.getElementById("labelcpnf").style.display="none";
}else{
  document.getElementById("labelcpnf").style.display="block";
  document.getElementById("labelcpf").style.display="none";
}
}

