function gestion_aide(obj_img_aide, id_aide, id_conteneur_aide, messageid){

  var id_sauvegardee=document.getElementById('id_img_aide_active').value;

  if (obj_img_aide.id!=id_sauvegardee){                         
    if (id_sauvegardee!='') document.getElementById(id_sauvegardee).src='images/point.png';
    obj_img_aide.src='images/point_over.png';
    document.getElementById('id_img_aide_active').value=obj_img_aide.id;
    document.getElementById(id_conteneur_aide).style.visibility='visible';
    document.getElementById(id_aide).style.top='0px';
    document.getElementById(id_aide).innerHTML=messages[messageid][1];
  }
  else{
    obj_img_aide.src='images/point.png';
    document.getElementById(id_aide).innerHTML='';
    document.getElementById(id_aide).style.top='0px';
    document.getElementById(id_conteneur_aide).style.visibility='hidden';
    document.getElementById('id_img_aide_active').value='';
  }

}
function gestion_aide_exploitation(objet){
  if (document.getElementById('texte_aide_exploit').style.visibility=='hidden'){ 
    objet.src='images/point_over.png';
    document.getElementById('texte_aide_exploit').style.visibility='visible';
  }
  else{
    objet.src='images/point.png';
    document.getElementById('texte_aide_exploit').style.visibility='hidden';
  }
}
function gestion_aide_over_exploitation(objet,etat){
  if (document.getElementById('texte_aide_exploit').style.visibility=='hidden'){
    if (etat=='over') objet.src='images/point_over.png';
    else objet.src='images/point.png';
  }
  else{
    if (etat=='over') objet.src='images/point.png';
    else objet.src='images/point_over.png';
  }
}

function centerPopup(element) {
    var height=document.getElementById(element).offsetHeight;//hauteur de l'élément à positionner
    var width=document.getElementById(element).offsetWidth;//largeur de l'élément à positionner
    
    myParent=document.getElementById(element).parentNode;
    var pHeight=myParent.offsetHeight;//Hauteur de l'élément parent
    //var pHeight=screen.height;
    var pWidth=myParent.offsetWidth;//Largeur de l'élément parent
    //var pWidth=screen.width;

    var sTop=myParent.scrollTop;//Hauteur de défilement de l'élément parent
    var sLeft=myParent.scrollLeft;//Longueur de défilement de l'élément parent
    
    var posY=(pHeight/2)-(height/2)+sTop;//Calcul de la position en Y
    var posX=(pWidth/2)-(width/2)+sLeft;//Calcul de la position en X

    document.getElementById(element).style.top=posY+"px";
    document.getElementById(element).style.left=posX+"px";
}

 //Les fonctions suivantes changent les images en fonction des options choisies.
function changerImage(x,y,z){
  var path=x+"_"+y+".gif";
  //alert(path);
  document.getElementById(z).src=path;
}

function changerImage2(x,y,z){
  if (x=='PERM' && z=='selectOccupMens'){
    x=document.getElementById('selectOccupHebdo').value;
  }
  if (z=='selectOccupMens' || (document.getElementById('selectOccupMens').value=='PERM' && z=='selectOccupHebdo')){
    var path="images/formulaire/"+y+"_"+x+".gif";
    document.getElementById(y).src=path;
  }
}

//fonctions JS qui vérifient la validité des données saisies par l'utilisateur.


function HTMLentities (texte) {

//texte = texte.replace(/#/g,'&#35;'); // 160 A0
//texte = texte.replace(/\n/g,'&#92;n'); // 160 A0
//texte = texte.replace(/\r/g,'&#92;r'); // 160 A0

texte = texte.replace(/&/g,'&amp;'); // 38 26
texte = texte.replace(/"/g,'&quot;'); // 34 22
texte = texte.replace(/</g,'&lt;'); // 60 3C
texte = texte.replace(/>/g,'&gt;'); // 62 3E

texte = texte.replace(/\242/g,'&cent;');
texte = texte.replace(/\243/g,'&pound;');
texte = texte.replace(/\&euro;/g,'&euro;');
texte = texte.replace(/\245/g,'&yen;');
texte = texte.replace(/\260/g,'&deg;');
//texte = texte.replace(/\274/g,'&frac14;');
texte = texte.replace(/\274/g,'&OElig;');
//texte = texte.replace(/\275/g,'&frac12;');
texte = texte.replace(/\275/g,'&oelig;');
//texte = texte.replace(/\276/g,'&frac34;');
texte = texte.replace(/\276/g,'&Yuml;');
texte = texte.replace(/\241/g,'&iexcl;');
texte = texte.replace(/\253/g,'&laquo;');
texte = texte.replace(/\273/g,'&raquo;');
texte = texte.replace(/\277/g,'&iquest;');
texte = texte.replace(/\300/g,'&Agrave;');
texte = texte.replace(/\301/g,'&Aacute;');
texte = texte.replace(/\302/g,'&Acirc;');
texte = texte.replace(/\303/g,'&Atilde;');
texte = texte.replace(/\304/g,'&Auml;');
texte = texte.replace(/\305/g,'&Aring;');
texte = texte.replace(/\306/g,'&AElig;');
texte = texte.replace(/\307/g,'&Ccedil;');
texte = texte.replace(/\310/g,'&Egrave;');
texte = texte.replace(/\311/g,'&Eacute;');
texte = texte.replace(/\312/g,'&Ecirc;');
texte = texte.replace(/\313/g,'&Euml;');
texte = texte.replace(/\314/g,'&Igrave;');
texte = texte.replace(/\315/g,'&Iacute;');
texte = texte.replace(/\316/g,'&Icirc;');
texte = texte.replace(/\317/g,'&Iuml;');
texte = texte.replace(/\320/g,'&ETH;');
texte = texte.replace(/\321/g,'&Ntilde;');
texte = texte.replace(/\322/g,'&Ograve;');
texte = texte.replace(/\323/g,'&Oacute;');
texte = texte.replace(/\324/g,'&Ocirc;');
texte = texte.replace(/\325/g,'&Otilde;');
texte = texte.replace(/\326/g,'&Ouml;');
texte = texte.replace(/\330/g,'&Oslash;');
texte = texte.replace(/\331/g,'&Ugrave;');
texte = texte.replace(/\332/g,'&Uacute;');
texte = texte.replace(/\333/g,'&Ucirc;');
texte = texte.replace(/\334/g,'&Uuml;');
texte = texte.replace(/\335/g,'&Yacute;');
texte = texte.replace(/\336/g,'&THORN;');
texte = texte.replace(/\337/g,'&szlig;');
texte = texte.replace(/\340/g,'&agrave;');
texte = texte.replace(/\341/g,'&aacute;');
texte = texte.replace(/\342/g,'&acirc;');
texte = texte.replace(/\343/g,'&atilde;');
texte = texte.replace(/\344/g,'&auml;');
texte = texte.replace(/\345/g,'&aring;');
texte = texte.replace(/\346/g,'&aelig;');
texte = texte.replace(/\347/g,'&ccedil;');
texte = texte.replace(/\350/g,'&egrave;');
texte = texte.replace(/\351/g,'&eacute;');
texte = texte.replace(/\352/g,'&ecirc;');
texte = texte.replace(/\353/g,'&euml;');
texte = texte.replace(/\354/g,'&igrave;');
texte = texte.replace(/\355/g,'&iacute;');
texte = texte.replace(/\356/g,'&icirc;');
texte = texte.replace(/\357/g,'&iuml;');
texte = texte.replace(/\360/g,'&eth;');
texte = texte.replace(/\361/g,'&ntilde;');
texte = texte.replace(/\362/g,'&ograve;');
texte = texte.replace(/\363/g,'&oacute;');
texte = texte.replace(/\364/g,'&ocirc;');
texte = texte.replace(/\365/g,'&otilde;');
texte = texte.replace(/\366/g,'&ouml;');
texte = texte.replace(/\370/g,'&oslash;');
texte = texte.replace(/\371/g,'&ugrave; ');
texte = texte.replace(/\372/g,'&uacute;');
texte = texte.replace(/\373/g,'&ucirc;');
texte = texte.replace(/\374/g,'&uuml;');
texte = texte.replace(/\375/g,'&yacute;');
texte = texte.replace(/\376/g,'&thorn;');
texte = texte.replace(/\377/g,'&yuml;');
return texte;
}


function HTMLentitiesdecode (texte) {

//texte = texte.replace(/#/g,'&#35;'); // 160 A0
//texte = texte.replace(/\n/g,'&#92;n'); // 160 A0
//texte = texte.replace(/\r/g,'&#92;r'); // 160 A0

texte = texte.replace(/&amp;/g,'&'); // 38 26
texte = texte.replace(/&quot;/g,'"'); // 34 22
texte = texte.replace(/&lt;/g,'<'); // 60 3C
texte = texte.replace(/&gt;/g,'>'); // 62 3E

texte = texte.replace(/&cent;/g,'\242');
texte = texte.replace(/&pound;/g,'\243');
texte = texte.replace(/&euro;/g,'\&euro;');
texte = texte.replace(/&yen;/g,'\245');
texte = texte.replace(/&deg;/g,'\260');
//texte = texte.replace(/\274/g,'&frac14;');
texte = texte.replace(/&OElig;/g,'\274');
//texte = texte.replace(/\275/g,'&frac12;');
texte = texte.replace(/&oelig;/g,'\275');
//texte = texte.replace(/\276/g,'&frac34;');
texte = texte.replace(/&Yuml;/g,'\276');
texte = texte.replace(/&iexcl;/g,'\241');
texte = texte.replace(/&laquo;/g,'\253');
texte = texte.replace(/&raquo;/g,'\273');
texte = texte.replace(/&iquest;/g,'\277');
texte = texte.replace(/&Agrave;/g,'\300');
texte = texte.replace(/&Aacute;/g,'\301');
texte = texte.replace(/&Acirc;/g,'\302');
texte = texte.replace(/&Atilde;/g,'\303');
texte = texte.replace(/&Auml;/g,'\304');
texte = texte.replace(/&Aring;/g,'\305');
texte = texte.replace(/&AElig;/g,'\306');
texte = texte.replace(/&Ccedil;/g,'\307');
texte = texte.replace(/&Egrave;/g,'\310');
texte = texte.replace(/&Eacute;/g,'\311');
texte = texte.replace(/&Ecirc;/g,'\312');
texte = texte.replace(/&Euml;/g,'\313');
texte = texte.replace(/&Igrave;/g,'\314');
texte = texte.replace(/&Iacute;/g,'\315');
texte = texte.replace(/&Icirc;/g,'\316');
texte = texte.replace(/&Iuml;/g,'\317');
texte = texte.replace(/&ETH;/g,'\320');
texte = texte.replace(/&Ntilde;/g,'\321');
texte = texte.replace(/&Ograve;/g,'\322');
texte = texte.replace(/&Oacute;/g,'\323');
texte = texte.replace(/&Ocirc;/g,'\324');
texte = texte.replace(/&Otilde;/g,'\325');
texte = texte.replace(/&Ouml;/g,'\326');
texte = texte.replace(/&Oslash;/g,'\330');
texte = texte.replace(/&Ugrave;/g,'\331');
texte = texte.replace(/&Uacute;/g,'\332');
texte = texte.replace(/&Ucirc;/g,'\333');
texte = texte.replace(/&Uuml;/g,'\334');
texte = texte.replace(/&Yacute;/g,'\335');
texte = texte.replace(/&THORN;/g,'\336');
texte = texte.replace(/&szlig;/g,'\337');
texte = texte.replace(/&agrave;/g,'\340');
texte = texte.replace(/&aacute;/g,'\341');
texte = texte.replace(/&acirc;/g,'\342');
texte = texte.replace(/&atilde;/g,'\343');
texte = texte.replace(/&auml;/g,'\344');
texte = texte.replace(/&aring;/g,'\345');
texte = texte.replace(/&aelig;/g,'\346');
texte = texte.replace(/&ccedil;/g,'\347');
texte = texte.replace(/&egrave;/g,'\350');
texte = texte.replace(/&eacute;/g,'\351');
texte = texte.replace(/&ecirc;/g,'\352');
texte = texte.replace(/&euml;/g,'\353');
texte = texte.replace(/&igrave;/g,'\354');
texte = texte.replace(/&iacute;/g,'\355');
texte = texte.replace(/&icirc;/g,'\356');
texte = texte.replace(/&iuml;/g,'\357');
texte = texte.replace(/&eth;/g,'\360');
texte = texte.replace(/&ntilde;/g,'\361');
texte = texte.replace(/&ograve;/g,'\362');
texte = texte.replace(/&oacute;/g,'\363');
texte = texte.replace(/&ocirc;/g,'\364');
texte = texte.replace(/&otilde;/g,'\365');
texte = texte.replace(/&ouml;/g,'\366');
texte = texte.replace(/&oslash;/g,'\370');
texte = texte.replace(/&ugrave;/g,'\371');
texte = texte.replace(/&uacute;/g,'\372');
texte = texte.replace(/&ucirc;/g,'\373');
texte = texte.replace(/&uuml;/g,'\374');
texte = texte.replace(/&yacute;/g,'\375');
texte = texte.replace(/&thorn;/g,'\376');
texte = texte.replace(/&yuml;/g,'\377');
return texte;
}

//Fonction qui vérifie la saisie de caractères
function saisieNotNul(nomchamp,texte){
  if (texte==""){
      alert("Veuillez entrer une valeur");
      document.getElementById(nomchamp).focus();
      document.getElementById(nomchamp).select();
  }
}

//Fonction qui vérifie la saisie d'un caractère invalide.
//x : élément à être vérifié.
function verif(x){
	if (isNaN(x.value)){
		alert("Vous avez saisi un caractère invalide !");
		x.value = "";
        x.focus(); 
		x.select();
	}		
}








// Fonction qui vérifie la saisie de caractères numériques sans message d'alerte (durant la frappe normalement onkeyup)
// remplacement de la virgule par le point, suppression du dernier caractère frappé si ce n'est pas un chiffre
// mise à zéro si champ vide (il s'agit d'un champ numérique)
function QuietSaisieNum(champ){
  var valeur=champ.value;

  if (valeur!=""){
    valeur = valeur.replace(",",".");    
    champ.value=valeur;
    var re = /^[0-9\.\-]*$/;
    if (!re.test(valeur)){
      champ.value = valeur.substr(0, valeur.length-1);
      if (champ.value == "") champ.value=0;
      champ.focus();
      //champ.select();  
    }
  }
  else champ.value = 0;
}

function QuietSaisieNumStrict(champ){
  var valeur=champ.value;

  if (valeur!=""){
    var re = /^[0-9\-]*$/;
    if (!re.test(valeur)){
      champ.value = valeur.substr(0, valeur.length-1);
      if (champ.value == "") champ.value=0;
      champ.focus();
      //champ.select();  
    }
  }
  else champ.value = 0;
}

// Fonction de vérification lorsqu'on quitte le champ de saisie (normalement onblur) sans message d'alerte
// on s'occupe d'abord de la mise en forme des décimales selon ce qui a été récupéré dans la table _general_parametres_valeurs
// puis on vérifie que la valeur du champ correspond à l'interval de valeurs qu'on s'est fixé
function verif_sortie_champ_numerique(champ,min,max,pas,valeur_defaut){
  if (champ.disabled==false && champ.readOnly==false){
      var valeur=champ.value;

      // si le champ contient une valeur (vérifiée par la fonction QuietSaisieNum)
      if (valeur!=""){
          // si on a oublié de coller des valeurs décimales derrière le point, on supprime le point
          if (valeur[valeur.length-1]==".") champ.value = valeur.substr(0, valeur.length-1);  

          var re = /^[0-9\.\-]*$/;
          if (!re.test(valeur)){
              if (valeur_defaut!='') champ.value = valeur_defaut;
              else champ.value=0;
              return;
          }

          // détermination du nombre de décimales à prendre en compte (nombre_decimales)
          if (pas==1) var nombre_decimales=0;
          else{
              pas=" "+pas;
              var reg=new RegExp("[.]+", "g");
              var tableau=pas.split(reg);
              var nombre_decimales=tableau[1].length;
          }

          var deci=Math.round( Math.pow(10,nombre_decimales)*(Math.abs(valeur)-Math.floor(Math.abs(valeur)))) ; 
          var val=Math.floor(Math.abs(valeur));
          if ((nombre_decimales==0)||(deci==Math.pow(10,nombre_decimales))) {val=Math.floor(Math.abs(valeur)); deci=0;}
          var val_format=val+"";
          var nb=val_format.length;
          for (var i=1;i<4;i++) {
            if (val>=Math.pow(10,(3*i))) {
                val_format=val_format.substring(0,nb-(3*i))+val_format.substring(nb-(3*i));
            }
          }
      
          // si des décimales ont été voulues
          if (nombre_decimales>0) {
              // placement des zéros non significatifs
              var decim="";
              for (var j=0;j<(nombre_decimales-deci.toString().length);j++) {decim+="0";}
              deci=decim+deci.toString();
              var add_deci=0;
              for (var j=0;j<(deci.toString().length);j++) {add_deci+=parseInt(deci[j]);}

              val_format=val_format+"."+deci;
          }
      
          // ajout du signe moins en cas de valeur négative
          if (parseFloat(valeur)<0) {
              val_format="-"+val_format;
          }

          champ.value=val_format;
      }
      // si le champ est vide, on l'initialise à zéro (champ numérique)
      else champ.value = 0;
  
      // pour que l'interval fonctionne, il faut que les deux valeurs soient renseignées
      if (min!="" && max!=""){
          // on transforme les chaînes de caractères en numériques
          min=parseFloat(min);
          max=parseFloat(max);
          champ.value=parseFloat(champ.value);
    
          // si on est en dehors des clous
          if(champ.value < min || champ.value > max){
              //if (valeur_defaut!="") champ.value = valeur_defaut;
              if(champ.value > max) champ.value = max;
              else champ.value = min;
          }
    }
  }
}







//Fonction qui vérifie les limites inférieur/supérieur d'une valeur numérique.
//x : élément.
//lb : limite inférieure.
//ub : limite supérieure.
function limiterValeur(x, lb, ub){
  if (x.value!=""){
	if(x.value < lb || x.value > ub){
		alert("Veuillez saisir une valeur de "+x.name+" comprise entre "+lb+" et "+ub);
		x.value = "";
        x.focus(); 
        x.select();
	}
  }
}

//Fonction qui vérifie les limites inférieur/supérieur d'une valeur numérique et qui positionne la valeur par défaut.
function limiterValeurSaisie(x, min, max, valeur_defaut){
  //if (x.value!=""){
  if (min!="" && max!=""){
    if(x.value < min || x.value > max || x.value==""){
        alert("Veuillez saisir une valeur de "+x.name+" comprise entre "+min+" et "+max);
        x.value = valeur_defaut;
        x.focus(); 
        x.select();
    }
  }
  //}
}

//Fonction qui vérifie la saisie de caractères faisant partie d'une liste donnée.
function limiterSaisie(x, liste){
  if (x.value!=""){
      var donnees=liste.split('|');
      var drap=false;
      for(i=0;i<donnees.length-1;i++) {
          if (x.value==donnees[i]) drap=true;
      }
      if (drap==false){
          alert("Veuillez saisir une valeur autorisée");
          x.value = "";
          x.focus(); 
          x.select();
      }
  }
}

//Fonction qui vérifie la saisie de caractères alphabétiques
function saisieAdresseInternet(nomchamp,texte){
  if (texte!=""){
    var re = /^[0-9a-zA-Z\-\.\s]*$/;
    if (!re.test(texte)){
      alert("Veuillez n'entrer que des caractères alphabétiques");
      //document.getElementById(nomchamp).value="";
      document.getElementById(nomchamp).focus();
    }
  }
}

//Fonction qui vérifie la saisie de caractères alphabétiques
function saisieAlpha(nomchamp,texte){
  if (texte!=""){
    var re = /^[a-zA-ZÉÈËÊéèëêÀÄÂàäâÏÎïîÖÔöôÙÜÛùüûÇç();%°<>=?!\.\[\]\/\-\'\s]*$/;
    if (!re.test(texte)){
	  alert("Veuillez n'entrer que des caractères alphabétiques");
	  //document.getElementById(nomchamp).value="";
	  document.getElementById(nomchamp).focus();
	}
  }
}

//Fonction qui vérifie la saisie de caractères alphanumériques
function saisieAlphaNum(nomchamp,texte){
  if (texte!=""){
    var re = /^[0-9a-zA-ZÉÈËÊéèëêÀÄÂàäâÏÎïîÖÔöôÙÜÛùüûÇç();%°<>=?!\.\[\]\/\-\'\,\s]*$/;
    if (!re.test(texte)){
	  alert("Veuillez n'entrer que des caractères alphanumériques");
	  //document.getElementById(nomchamp).value="";
	  document.getElementById(nomchamp).focus();
	}
  }
}

//Fonction qui vérifie la saisie de caractères alphanumériques
function saisieAlphaNumPas(nomchamp,texte){
  if (texte!=""){
    var re = /^[0-9a-zA-Z\.\@\-\_\s]*$/;
    if (!re.test(texte)){
      alert("Veuillez n'entrer que des caractères alphanumériques non accentués ainsi que les symboles @ . - et _");
      //document.getElementById(nomchamp).value="";
      document.getElementById(nomchamp).focus();
    }
  }
}

//Fonction qui vérifie la saisie de caractères alphanumériques sans caractères spéciaux ni accents
function saisieAlphaNumStrict(nomchamp,texte){
  if (texte!=""){
    var re = /^[0-9a-zA-Z\s]*$/;
    if (!re.test(texte)){
      alert("Veuillez n'entrer que des caractères alphanumériques non accentués");
      //document.getElementById(nomchamp).value="";
      document.getElementById(nomchamp).focus();
      document.getElementById(nomchamp).select();  
    }
  }
}

//Fonction qui vérifie la saisie d'un département
function saisieDep(nomchamp,texte){
  if (texte!=""){
    if (texte<1000 || texte>95999 || texte.length<5){
      alert("Veuillez entrer un code postal valide");
      //document.getElementById(nomchamp).value="";
      document.getElementById(nomchamp).focus();
      document.getElementById(nomchamp).select();  
    }
  }
}

//Fonction qui vérifie la saisie de numéros de téléphone
function saisieNumTel(nomchamp,texte){
  if (texte!=""){
    texte = texte.replace("(","");
    texte = texte.replace(")","");
    document.getElementById(nomchamp).value=texte;
    // +33 avec ou non un séparateur ou bien un 0
    // un chiffre de 0 à 6 ou 8
    // une série de 4 : séparatur optionnel et 2 chiffres
    var re =/^((\+33[-.\s]?)|0)[0-689]([-.\s]?\d{2}){4}$/;
    if (!re.test(texte)){
      alert("Veuillez entrer un numéro valide\n0102030405\n01 02 03 04 05\n+33 1.02.03.04.05");
      //document.getElementById(nomchamp).value="";
      document.getElementById(nomchamp).focus();
      document.getElementById(nomchamp).select();  
    }
  }
}

function remplace_virgule(nomchamp){
    document.getElementById(nomchamp).value = document.getElementById(nomchamp).value.replace(",", "."); 
}

//Fonction qui vérifie la saisie de caractères numériques
function saisieNum(nomchamp,texte){
  if (texte!=""){
    texte = texte.replace(",",".");    
    document.getElementById(nomchamp).value=texte;
    var re = /^[0-9\.\-]*$/;
    if (!re.test(texte)){
      alert("Veuillez n'entrer que des caractères numériques");
      //document.getElementById(nomchamp).value="";
      document.getElementById(nomchamp).value = texte.substr(0, texte.length-1);
      if (document.getElementById(nomchamp).value == "") document.getElementById(nomchamp).value=0;
      document.getElementById(nomchamp).focus();
      //document.getElementById(nomchamp).select();  
    }
  }
  else document.getElementById(nomchamp).value = 0;
}

//Fonction qui vérifie la saisie de caractères numériques
function saisieNumStrict(nomchamp,texte){
  if (texte!=""){
    var re = /^[0-9]*$/;
    if (!re.test(texte)){
      alert("Veuillez n'entrer que des entiers numériques");
      //document.getElementById(nomchamp.id).value="";
      document.getElementById(nomchamp).value = texte.substr(0, texte.length-1);
      if (document.getElementById(nomchamp).value == "") document.getElementById(nomchamp).value=0;
      document.getElementById(nomchamp).focus();
//      document.getElementById(nomchamp).select();  
    }
  }
  else document.getElementById(nomchamp).value = 0;
}

//Fonction qui vérifie la saisie de caractères numériques
function saisieNumPas(nomchamp,valeur,pas){
  if (valeur!=""){
    valeur = valeur.replace(",",".");
        
    var re = /^[0-9\.]*$/;
    
    if (!re.test(valeur)){
      alert("Veuillez n'entrer que des caractères numériques");
      //document.getElementById(nomchamp).value="";
      document.getElementById(nomchamp).value = valeur.substr(0, valeur.length-1);
      if (document.getElementById(nomchamp).value == "") document.getElementById(nomchamp).value=0;
      document.getElementById(nomchamp).focus();
//      document.getElementById(nomchamp).select();  
    }
    else{

      if (pas==1) var decimal=0;
      else{
        pas=" "+pas;
        var reg=new RegExp("[.]+", "g");
        var tableau=pas.split(reg);
        var decimal=tableau[1].length;
      }

      var deci=Math.round( Math.pow(10,decimal)*(Math.abs(valeur)-Math.floor(Math.abs(valeur)))) ; 
      var val=Math.floor(Math.abs(valeur));
      if ((decimal==0)||(deci==Math.pow(10,decimal))) {val=Math.floor(Math.abs(valeur)); deci=0;}
      var val_format=val+"";
      var nb=val_format.length;
      for (var i=1;i<4;i++) {
        if (val>=Math.pow(10,(3*i))) {
            val_format=val_format.substring(0,nb-(3*i))+val_format.substring(nb-(3*i));
        }
      }
      if (decimal>0) {
        var decim=""; 
        for (var j=0;j<(decimal-deci.toString().length);j++) {decim+="0";}
        deci=decim+deci.toString();
        val_format=val_format+"."+deci;
      }
      if (parseFloat(valeur)<0) {val_format="-"+val_format;}

      document.getElementById(nomchamp).value=val_format;
    }
  }
  else document.getElementById(nomchamp).value = 0;
}

//Fonction qui vérifie la saisie d'un nombre donné de chiffres
function countNum(nomchamp,num,nombre){
  if (num!=""){
    eval("var re = /^[0-9]{"+nombre+"}$/;");
    if (!re.test(num)){
      alert("veuillez entrer "+nombre+" chiffres");
      //document.getElementById(nomchamp).value="";
      document.getElementById(nomchamp).focus();
    }
  }
}

// limite le nombre de caractères (ici pour une textarea)
function limite(zone,max){
    if(zone.value.length>=max){zone.value=zone.value.substring(0,max);}
}

// un click sur un bouton radio appel cette fonction pour placer la valeur du bouton dans le champ caché dont l'id est égal à nomchamp
function gestion_radios(nomchamp,valeur){
  var element=document.getElementsByName(nomchamp);
  element.value=valeur; 
}

// un click sur une case à cocher appel cette fonction pour placer la valeur de l'état checked de la case dans le champ caché dont l'id est égal à la concaténation de nomchamp+clé
function gestion_coches(nomchamp,cle,checked){
  //var champ="select_"+nomchamp+"_"+cle;
  if (nomchamp!='') var champ=nomchamp+"_"+cle;
  else var champ=cle;
  if (checked==true) document.getElementById(champ).value=1;
  else document.getElementById(champ).value=0;
  //alert(champ+"="+document.getElementById(champ).value); 
}

// un click sur une case à cocher appel cette fonction pour placer la valeur de l'état checked de la case dans le champ caché dont l'id est égal à la concaténation de nomchamp+clé
function gestion_coches_oui(nomchamp,cle,checked){
  //var champ="select_"+nomchamp+"_"+cle;
  if (nomchamp!='') var champ=nomchamp+"_"+cle;
  else var champ=cle;
  if (checked==true) document.getElementById(champ).value='oui';
  else document.getElementById(champ).value='non';
  //alert(champ+"="+document.getElementById(champ).value); 
}

 function nl2br_12(str) {
   if(typeof(str)=="string") return str.replace(/(\r\n)|(\n\r)|\r|\n/g,"<BR />");
   else return str;
 }
 
function surfacefenetrenulle(){
alert("Vous devez saisir une surface de fenêtre >0 lors de l'étape Forme lors de la saisie détaillée de l'enveloppe");
}

function FullScreen(){
  this.moveTo(0,0);
  resizeTo(screen.availWidth,screen.availHeight);
}

/******************** movelayer *************************/
var Timer;
var Pas = 3;
function moveLayer(Sens) {
    Objet=document.getElementById("aide");
    if(parseInt(Objet.style.top) + (Pas*Sens)>0)  {
        clearTimeout(Timer);
    }
    else if(parseInt(Objet.style.top) + (Pas*Sens)<-(Objet.offsetHeight-document.getElementById("support").offsetHeight)) {
        clearTimeout(Timer);
    }
    else {
        Objet.style.top = (parseInt(Objet.style.top) + (Pas*Sens)) + "px";
    }
    Timer = setTimeout("moveLayer(" + Sens + ");", 30);
}



/************************ Envois de formulaires **********************/
function envoyer_form_principal(sens) {
      if (sens=='pre') document.getElementById('page').value--;
      else document.getElementById('page').value++;
      document.forms['form1'].submit();
}
function envoi_etape(etape,page){
      document.getElementById('etape').value=etape;
      document.getElementById('page').value=page;
      document.forms['form1'].submit();
}
 
// Envoi de données en GET vers l'édition PDF ou l'enregistrement XML
function go_form(target,type){
    if (document.getElementById('nom').value=="" || document.getElementById('prenom').value==""){
      alert("Veuillez entrer un nom et un prénom pour la génération automatique du nom du fichier.");
      if (document.getElementById('nom').value=="") document.getElementById('nom').focus();
      else document.getElementById('prenom').focus(); 
    }
    else{
      var parametres="";
      for (var i=0; i<document.form1.elements.length; i++){
        //alert(document.form1.elements[i].value);
        if (document.form1.elements[i].id=="commentaires"){
          var temp=nl2br_12(document.form1.elements[i].value);
          parametres+=document.form1.elements[i].name+"="+temp+"&"; 
        }
        else if (document.form1.elements[i].id!="") parametres+=document.form1.elements[i].name+"="+document.form1.elements[i].value+"&";
      }
      var taille=parametres.length-1;
      parametres=parametres.substring(0, taille);
      target=target+"?"+parametres;
      //alert(target);
      
      if (type=="pdf") NewWin=window.open(target,'NWin');
      else location.href=target+"?"+parametres;
      // le rafraîchissement automatique de la page fait apparaître un message à chaque fois, on préfère afficher l'icône du mail même si le fichier risque de ne pas être créé
      if (document.getElementById('email').value!="") document.getElementById('affiche_email').innerHTML="<a href=\"mailto:"+document.getElementById('email').value+"?subject=Bilan de consommation&body=Madame, Monsieur, veuillez trouver ci-joint, l'estimation de votre bilan énergétique.\"><img src=\"images/minimail.png\" style=\"border:0;\" alt=\"Envoyer un email\"></a>";
      //self.location.reload();
    }
}

function go_simple(target,type){
      if (type=="pdf") NewWin=window.open(target,'PDF_CUBE');
      else if (type=="xml"){
          var num_version=document.getElementById('version').value;
          NewWin=window.open(target+'?projet_version='+num_version,'XML_CUBE');
      }
      else location.href=target;
      //self.location.reload();
}

// capture d'un évènement clavier keydown selon les navigateurs
function initEvent(){
    if (document.addEventListener) document.addEventListener("keydown",keydown,false);
    else if (document.attachEvent) document.attachEvent("onkeydown", keydown);
    else document.onkeydown = keydown;
}

function keydown(e){
    if (!e) e = event;
    //if (e.keyCode == 8) eventReturn(); 
    if (e.keyCode == 13) eventEnter();
}















/****************************************************************************************************************************************/
// gestion des arrondis
/****************************************************************************************************************************************/

function intval( mixed_var, base ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: stensi
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: intval('Kevin van Zonneveld');
    // *     returns 1: 0
    // *     example 2: intval(4.2);
    // *     returns 2: 4
    // *     example 3: intval(42, 8);
    // *     returns 3: 42
    // *     example 4: intval('09');
    // *     returns 4: 9
 
    var tmp;
 
    var type = typeof( mixed_var );
 
    if(type == 'boolean'){
        if (mixed_var == true) {
            return 1;
        } else {
            return 0;
        }
    } else if(type == 'string'){
        tmp = parseInt(mixed_var * 1);
        if(isNaN(tmp) || !isFinite(tmp)){
            return 0;
        } else{
            return tmp.toString(base || 10);
        }
    } else if(type == 'number' && isFinite(mixed_var) ){
        return Math.floor(mixed_var);
    } else{
        return 0;
    }
}
 
function floatval(mixed_var) {
    // +   original by: Michael White (http://getsprink.com)
    // %        note 1: The native parseFloat() method of JavaScript returns NaN when it encounters a string before an int or float value.
    // *     example 1: floatval('150.03_page-section');
    // *     returns 1: 150.03
    // *     example 2: floatval('page: 3');
    // *     returns 2: 0
    // *     example 2: floatval('-50 + 8');
    // *     returns 2: -50
 
    return (parseFloat(mixed_var) || 0);
}
 
/**
 * Arrondit à la précision supérieure
 * @param float value Valeur à arrondir
 * @param float precision Précision (défaut: 1)
 * @return float
 */
 
function ceilp(value, precision) {
    var decimales=1;
    if (isNaN(precision) || precision=='') precision=1;
    precision = Math.abs(intval(precision));
    for(i=1; i<=precision; i++){
      decimales=decimales*10;
    }
    value = floatval(value);
    if (precision<=1) {
        value = Math.ceil(value);
    } else {
        value = Math.ceil(value*decimales)/decimales; 
    }
    return value;
}
 
/**
 * Arrondit à la précision inférieure
 * @param float value Valeur à arrondir
 * @param float precision Précision (défaut: 1)
 * @return float
 */
function floorp(value, precision) {
    var decimales=1;
    if (isNaN(precision) || precision=='') precision=1;
    precision = Math.abs(intval(precision));
    for(i=1; i<=precision; i++){
      decimales=decimales*10;
    }
    value = floatval(value);
    if (precision<=1) {
        value = Math.floor(value);
    } else {
        value = Math.floor(value*decimales)/decimales;
    }
    return value;
}
/****************************************************************************************************************************************/



/****************************************************************************************************************************************/
// Validité d'email (a changer si on trouve mieux)
/****************************************************************************************************************************************/

/*
 function CheckMail(nomchamp,str) {
  if (str!=""){
    var mail = new RegExp("^[-a-z0-9_\.]{3,}@[-a-zA-Z0-9_]{3,}\.[a-z]{2,4}$");
    if (!mail.test(str)){
      alert("veuillez entrer une adresse mail valide");
      document.getElementById(nomchamp).focus();
    }
  }
}
*/



//Fonction pour vérification de l'intégrité de l'adresse email saisie.
//Script récupéré sur internet. Il suffit de faire emailCheck("email").

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- V1.1.3: Sandeep V. Tamhankar (stamhankar@hotmail.com) -->
<!-- Original:  Sandeep V. Tamhankar (stamhankar@hotmail.com) -->
<!-- Changes:
/* 1.1.4: Fixed a bug where upper ASCII characters (i.e. accented letters
international characters) were allowed.

1.1.3: Added the restriction to only accept addresses ending in two
letters (interpreted to be a country code) or one of the known
TLDs (com, net, org, edu, int, mil, gov, arpa), including the
new ones (biz, aero, name, coop, info, pro, museum).  One can
easily update the list (if ICANN adds even more TLDs in the
future) by updating the knownDomsPat variable near the
top of the function.  Also, I added a variable at the top
of the function that determines whether or not TLDs should be
checked at all.  This is good if you are using this function
internally (i.e. intranet site) where hostnames don't have to 
conform to W3C standards and thus internal organization e-mail
addresses don't have to either.
Changed some of the logic so that the function will work properly
with Netscape 6.

1.1.2: Fixed a bug where trailing . in e-mail address was passing
(the bug is actually in the weak regexp engine of the browser; I
simplified the regexps to make it work).

1.1.1: Removed restriction that countries must be preceded by a domain,
so abc@host.uk is now legal.  However, there's still the 
restriction that an address must end in a two or three letter
word.

1.1: Rewrote most of the function to conform more closely to RFC 822.

1.0: Original  */
// -->

<!-- Begin
function emailCheck (nomchamp,emailStr) {

if (emailStr!=""){
  /* The following variable tells the rest of the function whether or not
  to verify that the address ends in a two-letter country or well-known
  TLD.  1 means check it, 0 means don't. */

  var checkTLD=1;

  /* The following is the list of known TLDs that an e-mail address must end with. */

  var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

  /* The following pattern is used to check if the entered e-mail address
  fits the user@domain format.  It also is used to separate the username
  from the domain. */

  var emailPat=/^(.+)@(.+)$/;

  /* The following string represents the pattern for matching all special
  characters.  We don't want to allow special characters in the address. 
  These characters include ( ) < > @ , ; : \ " . [ ] */

  var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

  /* The following string represents the range of characters allowed in a 
  username or domainname.  It really states which chars aren't allowed.*/

  var validChars="\[^\\s" + specialChars + "\]";

  /* The following pattern applies if the "user" is a quoted string (in
  which case, there are no rules about which characters are allowed
  and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
  is a legal e-mail address. */

  var quotedUser="(\"[^\"]*\")";

  /* The following pattern applies for domains that are IP addresses,
  rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
  e-mail address. NOTE: The square brackets are required. */

  var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

  /* The following string represents an atom (basically a series of non-special characters.) */

  var atom=validChars + '+';

  /* The following string represents one word in the typical username.
  For example, in john.doe@somewhere.com, john and doe are words.
  Basically, a word is either an atom or quoted string. */

  var word="(" + atom + "|" + quotedUser + ")";

  // The following pattern describes the structure of the user

  var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

  /* The following pattern describes the structure of a Style11 symbolic
  domain, as opposed to ipDomainPat, shown above. */

  var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

  /* Finally, let's start trying to figure out if the supplied address is valid. */

  /* Begin with the coarse pattern to simply break up user@domain into
  different pieces that are easy to analyze. */
 
  var matchArray=emailStr.match(emailPat);

  if (matchArray==null) {

    /* Too many/few @'s or something; basically, this address doesn't
    even fit the general mould of a valid e-mail address. */
   
    //alert("Email address seems incorrect (check @ and .'s)");
    alert("Adresse e-mail invalide !");
		document.getElementById(nomchamp).value="";
    return false;
  }
	
  var user=matchArray[1];
  var domain=matchArray[2];

  // Start by checking that only basic ASCII characters are in the strings (0-127).

  for (i=0; i<user.length; i++) {
    if (user.charCodeAt(i)>127) {
      //alert("This username contains invalid characters.");
      alert("Adresse e-mail incorrecte !");
			document.getElementById(nomchamp).value="";
      return false;
    }
  }
	
  for (i=0; i<domain.length; i++) {
    if (domain.charCodeAt(i)>127) {
      //alert("This domain name contains invalid characters.");
      alert("Adresse e-mail incorrecte !");
			document.getElementById(nomchamp).value="";
      return false;
    }
  }

  // See if "user" is valid 
  if (user.match(userPat)==null) {
    // user is not valid
    alert("The username doesn't seem to be valid.");
		document.getElementById(nomchamp).value="";
    return false;
  }

  /* if the e-mail address is at an IP address (as opposed to a symbolic
  host name) make sure the IP address is valid. */
 
  var IPArray=domain.match(ipDomainPat);
  if (IPArray!=null) {
    // this is an IP address
    for (var i=1;i<=4;i++) {
      if (IPArray[i]>255) {
        //alert("Destination IP address is invalid!");
        alert("Adresse e-mail incorrecte !");
				document.getElementById(nomchamp).value="";
        return false;
      }
    }
    return true;
  }

  // Domain is symbolic name.  Check if it's valid.
 
  var atomPat=new RegExp("^" + atom + "$");
  var domArr=domain.split(".");
  var len=domArr.length;
	
  for (i=0;i<len;i++) {
    if (domArr[i].search(atomPat)==-1) {
      alert("The domain name does not seem to be valid.");
			document.getElementById(nomchamp).value="";
      return false;
    }
  }

  /* domain name seems valid, but now make sure that it ends in a
  known top-level domain (like com, edu, gov) or a two-letter word,
  representing country (uk, nl), and that there's a hostname preceding 
  the domain or country. */

  if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) {
    //alert("The address must end in a well-known domain or two letter " + "country.");
    alert("Adresse e-mail incomplète !");
		document.getElementById(nomchamp).value="";
    return false;
  }

  // Make sure there's a host name preceding the domain.

  if (len<2) {
    //alert("This address is missing a hostname!");
    alert("Adresse e-mail incorrecte !");
		document.getElementById(nomchamp).value="";
    return false;
  }
}
  // If we've gotten this far, everything's valid!
  return true;
}
//  End -->

/****************************************************************************************************************************************/


