function formater_montant(montant, precision) {
    if (!isNaN(montant)) {
        if (isNaN(precision))
            precision = 2;
        inf1 = montant < 1 ? "0" : "";
        montant = ""+(Math.round(montant*Math.pow(10, precision)));
        if (montant.indexOf(".") != -1)
            montant = montant.substring(0, montant.indexOf("."));
        if (montant.length <= precision) {
            while(montant.length < precision)
                montant = "0"+montant;
            montant = "0."+montant;
        }
        else
            montant = montant.substring(0, montant.length-precision) + "." + montant.substring(montant.length-precision);
    }
    return(montant);
}

function isDate(datejjmmaaaa) {
    var dt = datejjmmaaaa.split("/");
    loc_date  = new Date(dt[2], dt[1]-1, dt[0]);
    return((loc_date.getDate() == dt[0]) && (loc_date.getMonth()+1 == dt[1]) && (loc_date.getFullYear() == dt[2]));
}

function dateCompare(datejjmmaaaa_debut, datejjmmaaaa_fin, operateur) {
    dt = datejjmmaaaa_debut.split("/");
    loc_date_debut  = dt[2]*10000 + dt[1]*100 + dt[0]*1;
    dt = datejjmmaaaa_fin.split("/");
    loc_date_fin = dt[2]*10000 + dt[1]*100 + dt[0]*1;
    eval("result = loc_date_debut "+operateur+" loc_date_fin;");
    return(result);
}

function isEmail(email) {
    var reg = /^[\w.-]+@[\w.-]+\.\w{1,4}$/;
    var reg2 = /[.@]{2,}/;
    return((reg.exec(email) != null) && (reg2.exec(email) == null))
}

function EstSirenValide(siren) {
    var estValide;
    if ( (siren.length != 9) || (isNaN(siren)) )
        estValide = false;
    else {
        // Donc le SIREN est un numérique à 9 chiffres
        var somme = 0;
        var tmp;
        for (var cpt = 0; cpt<siren.length; cpt++) {
            if ((cpt % 2) == 1) { // Les positions paires : 2ème, 4ème, 6ème et 8ème chiffre
                tmp = siren.charAt(cpt) * 2; // On le multiplie par 2
                if (tmp > 9) 
                    tmp -= 9;  // Si le résultat est supérieur à 9, on lui soustrait 9
            }
            else
                tmp = siren.charAt(cpt);
            somme += parseInt(tmp);
        }
        if ((somme % 10) == 0)
            estValide = true;  // Si la somme est un multiple de 10 alors le SIREN est valide 
        else
            estValide = false;
    }
    return estValide;
}


function EstSiretValide(siret) {
    var estValide;
    if ( (siret.length != 14) || (isNaN(siret)) )
        estValide = false;
    else {
       // Donc le SIRET est un numérique à 14 chiffres
       // Les 9 premiers chiffres sont ceux du SIREN (ou RCS), les 4 suivants
       // correspondent au numéro d'établissement
       // et enfin le dernier chiffre est une clef de LUHN. 
        var somme = 0;
        var tmp;
        for (var cpt = 0; cpt<siret.length; cpt++) {
            if ((cpt % 2) == 0) { // Les positions impaires : 1er, 3è, 5è, etc... 
                tmp = siret.charAt(cpt) * 2; // On le multiplie par 2
                if (tmp > 9) 
                    tmp -= 9;  // Si le résultat est supérieur à 9, on lui soustrait 9
            }
            else
                tmp = siret.charAt(cpt);
            somme += parseInt(tmp);
        }
        if ((somme % 10) == 0)
            estValide = true; // Si la somme est un multiple de 10 alors le SIRET est valide 
        else
            estValide = false;
    }
    return estValide;
}
  

//
// Gestion des formulaires multilangues
//

var multi_champ = [];

function init_champ(champ, valeur) {
    multi_champ[multi_champ.length] =  [champ, valeur, 1];
    affiche_drapeau(champ, true);
    affiche_champ(champ, true);
    affiche_drapeau(champ, false);
    affiche_champ(champ, false);
}

function affiche_drapeau(champ, principal) {
    var drapeau = '';
    if (principal) {
        drapeau = "<img src=images/flag_"+multi_langue[0][0]+".gif alt='"+multi_langue[0][1]+"'>";
    }
    else {
        for(i=1; i<multi_langue.length; i++) {
            pos = get_pos(champ);
            if (pos >= 0) {
                if (i > 1)
                    drapeau += '<img src=images/espaceur.gif width=2>';
                if (i == multi_champ[pos][2])
                    drapeau += "<img src=images/flag_"+multi_langue[i][0]+".gif alt='"+multi_langue[i][1]+"'>";
                else {
                    drapeau += "<a href='javascript:change_secondaire(\""+champ+"\", "+i+");'><img src=images/flag_"+multi_langue[i][0]+".gif alt='"+multi_langue[i][1]+"'";
                    drapeau += " style='filter:alpha(opacity=40);opacity:0.4' border=0></a>";
                }
            }
        }
    }

    eval(champ+(principal ? "_principal" : "_secondaire")+".innerHTML = \""+drapeau.replace(new RegExp('"', 'gi'), '\\\"')+"\";");
}

function get_pos(champ) {
    var i = -1;
    for (c=0; c<multi_champ.length; c++)
        if (multi_champ[c][0] == champ)
            i = c;
    return(i);
}

function get_champ(champ, principal) {
    var txt= ''
    var pos = get_pos(champ);
    var langue = multi_langue[principal ? 0 : multi_champ[pos][2]][0];
    var tmp = multi_champ[pos][1].split('~^');
    for(i=1; i<tmp.length; i+=2)
        if (tmp[i] == langue)
            txt = tmp[i-1];
    return(txt);
}

function affiche_champ(champ, principal) {
    txt = get_champ(champ, principal);
    window.document.forms['NF_FORM'][champ+(principal ? "_principal" : "_secondaire")].value = txt;
}

function change_secondaire(champ, i) {
    multi_champ[get_pos(champ)][2] = i;
    affiche_drapeau(champ, false);
    affiche_champ(champ, false);
}

function set_champ(objet) {
    var principal = objet.name.indexOf("_principal") > 0;
    var champ = principal ? objet.name.substring(0, objet.name.length-10) : objet.name.substring(0, objet.name.length-11);
    var pos = get_pos(champ);
    var langue = multi_langue[principal ? 0 : multi_champ[pos][2]][0];
    var tmp = multi_champ[pos][1].split('~^');
    for(i=1; i<tmp.length; i+=2)
        if (tmp[i] == langue)
            tmp[i-1] = objet.value;
    multi_champ[pos][1] = tmp.join('~^');
    window.document.forms['NF_FORM'][multi_champ[pos][0]].value = multi_champ[pos][1];
}
  
function textCounter(field, countfield, maxlimit) {
    if(field.value.length > maxlimit)
        field.value = field.value.substring(0, maxlimit);
    countfield.value = maxlimit - field.value.length;
}