﻿
//var globale
var bgColorAlert = "#fcdcdc";

function Controle(TabInputText, TabInputSelect, idAlerte) {
    
    Vide = false;
    document.getElementById(idAlerte).innerHTML = "";
    
    //Tous les Inputs Text rempli
    if (TabInputText != "") {

        for (i = 0; i < TabInputText.length; i++) {
            document.getElementById(TabInputText[i]).style.background = "";
            
            if (document.getElementById(TabInputText[i]).value == "") {
                document.getElementById(TabInputText[i]).style.background = bgColorAlert;
                Vide = true;
            }
        }
    }
    
    //Tous les Inputs Select rempli
    if (TabInputSelect != "") {
        for (i = 0; i < TabInputSelect.length; i++) {
            document.getElementById(TabInputSelect[i]).style.background = "";
            if (document.getElementById(TabInputSelect[i]).options[document.getElementById(TabInputSelect[i]).selectedIndex].value == "") {
                document.getElementById(TabInputSelect[i]).style.background = bgColorAlert;
                Vide = true;
            }
        }
    }

    //controle vide
    if (!Vide) {
        return true;
    }
    else {
        document.getElementById(idAlerte).innerHTML = "Veuillez remplir les champs obligatoires";
        return false;
    }

}

function checkEmail(mEmail, idAlerte) {
    if ((document.getElementById(mEmail).value.indexOf("@") >= 0) && (document.getElementById(mEmail).value.indexOf(".") >= 0)) {
        document.getElementById(idAlerte).innerHTML = "";
        document.getElementById(idAlerte).className = "";
        return true; 
    }
    else {
        document.getElementById(mEmail).style.background = bgColorAlert;
        document.getElementById(idAlerte).innerHTML = "Le format de l'adresse E-mail est incorrect !";
        return false; 
    }
}


function check_champ_nombre(champ) {
    var chiffres = new RegExp("[0-9\.]"); /* Modifier pour :chiffres = new RegExp("[0-9]");  */
    var verif;
    var points = 0; /* Supprimer cette ligne */

    for (x = 0; x < champ.value.length; x++) {
        verif = chiffres.test(champ.value.charAt(x));
        if (champ.value.charAt(x) == ".") { points++; } /* Supprimer cette ligne */
        if (points > 1) { verif = false; points = 1; } /* Supprimer cette ligne */
        if (verif == false) { champ.value = champ.value.substr(0, x) + champ.value.substr(x + 1, champ.value.length - x + 1); x--; }
    }
}

function check_champ_nombre2(champ) {
    var chiffres = new RegExp("[0-9]");
    var verif;
    var points = 0; /* Supprimer cette ligne */

    for (x = 0; x < champ.value.length; x++) {
        verif = chiffres.test(champ.value.charAt(x));
        if (champ.value.charAt(x) == ".") { points++; } /* Supprimer cette ligne */
        if (points > 1) { verif = false; points = 1; } /* Supprimer cette ligne */
        if (verif == false) { champ.value = champ.value.substr(0, x) + champ.value.substr(x + 1, champ.value.length - x + 1); x--; }
    }
}

function GenePwd(Input, longueur) {
    //keylist = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456879"
    keylist = "0123456879"
    temp = ''
    for (i = 0; i < longueur; i++)
        temp += keylist.charAt(Math.floor(Math.random() * keylist.length))

    document.getElementById(Input).value = temp;
}

function DisplayHidden(IdDiv, typeDisplay) {    
    mDiv = document.getElementById(IdDiv);
    if (mDiv.style.display == "none")
        mDiv.style.display = typeDisplay;
    else
        mDiv.style.display = "none";
}

function Display(IdDiv, typeDisplay) {
    mDiv = document.getElementById(IdDiv);
    mDiv.style.display = typeDisplay;
}

//format les champs num&eacute;riques
function champ_millier(mNbr) {
    var reg = new RegExp(" ", "g"); //cherche les blanc
    var mNbr2 = mNbr + "";
    nbr = mNbr2.replace(reg, "");
    
    nbr2 = "";
    mdecimal = "";
    j = 0;

    // si il ya le point => prend seulement les entiers
    if (nbr.indexOf(".", 0) != -1) {
        nb = nbr;
        nbr = nbr.substring(0, nbr.indexOf(".", 0));
        mdecimal = nb.substring(nb.lastIndexOf("."));
    }

    for (i = nbr.length; i >= 1; i--) {
        if (j == 3) {
            nbr2 = nbr.substring(i, i - 1) + " " + nbr2;
            j = 1;
        }
        else {
            nbr2 = nbr.substring(i, i - 1) + "" + nbr2;
            j++;
        }
    }
    return nbr2 + "" + mdecimal;
}
