var buff = '';

var sep1 = '.';

var sep2 = '/';

/*---------------------------------------------------------------------------*/

function ConfirmeAction(Texte){

        /*

          Utilisation dans un <A HREF> : onclick='return ConfirmeAction(\"Suppression confirmée ?\");'

          Permet d'arréter le chainage de l'URL si return false (et donc stoppe l'action)

        */

        if (confirm(Texte)=="1"){

                return true;

        }else{

                return false;

        }

}

/*---------------------------------------------------------------------------*/

function isVide(lib) {

        if(lib==""){

                return true;

        } else

                return false;

}

/*---------------------------------------------------------------------------*/

function Majuscule(champ) {

        champ.value=champ.value.toUpperCase();

}

/*---------------------------------------------------------------------------*/

function Lettre1Majuscule(champ) {

        champ.value=champ.value.substr(0,1).toUpperCase() + champ.value.substr(1).toLowerCase();

}

/*---------------------------------------------------------------------------*/

function isValidEmail(email,videAutorise){

        var filter=/^.+@.+\..{2,3}$/

        if(email=="") return videAutorise;

    if (filter.test(email))

            return true

    else{

            alert("Adresse e-mail invalide");

            return false;

    }

}

/*---------------------------------------------------------------------------*/

function isValidCp(cp,videAutorise) {

        if(cp.value=="") return videAutorise;

        var msk1 = /^(\d{5})$/;

        var matchArray = cp.value.match(msk1);

        if (matchArray == null) {

                return false;

        }

        return true;

}

/*---------------------------------------------------------------------------*/

function isValidTelFax(telFax,videAutorise) {

        buff = '';

        var msk1 = /^(\d{2})( |.)(\d{2})( |.)(\d{2})( |.)(\d{2})( |.)(\d{2})$/;

        var msk2 = /^(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/;

        if(telFax=="") return videAutorise;

        var matchArray = telFax.match(msk1);

         if (matchArray != null) {

                n1 = matchArray[1];

                n2 = matchArray[3];

                n3 = matchArray[5];

                n4 = matchArray[7];

                n5 = matchArray[9];

        }

        if (matchArray == null){

                matchArray = telFax.match(msk2);

                if (matchArray != null) {

                        n1 = matchArray[1];

                        n2 = matchArray[2];

                        n3 = matchArray[3];

                        n4 = matchArray[4];

                        n5 = matchArray[5];

                }

        }

        if (matchArray == null){

                return false;

        }

        buff = n1+sep1+n2+sep1+n3+sep1+n4+sep1+n5;

        return true;

}

/*---------------------------------------------------------------------------*/

function formatteTelFax(telFax,separateur,videAutorise){

        sep1 = separateur;

        if(isValidTelFax(telFax.value,videAutorise)){

                telFax.value = buff;

                return true;

        }

        return false;

}

/*---------------------------------------------------------------------------*/

function isValideHeure(heure,videAutorise){

   /*

    Teste la validité d'une heure saisie dans 'ChampHeure' (h ou h:m ou hh:mm)

    Si invalide : Vide le champ , empèche le focus de partir du champ, et retourne false

    Si valide : formatte la saisie en hh:mm

        Refuse les heures vides

   */

        buff = '';

        var hh_mm = /^(\d{1,2})(:| )(\d{1,2})$/;

        var hh    = /^(\d{1,2})$/;

        if(heure=="") return videAutorise;

        var matchArray = heure.match(hh);

        if (matchArray != null) {

                if(heure<=24 && heure >= 0){

                        if(heure.length<2) heure = "0" + heure;

                        heure = heure+":00";

                        buff = heure;

                        return true;

                } else hh_mm = "xxx";

          }

        matchArray = heure.match(hh_mm);

        if (matchArray != null) {

                var h = matchArray[1];

                var m = matchArray[3];

                if(h<=24 && h >= 0 && m >= 0 && m < 60){

                        if(m.length<2) m = "0" + m;

                        if(h.length<2) h = "0" + h;

                        heure = h+":"+m;

                        buff = heure;

                        return true;

                }

        }

        alert(heure + " : heure invalide");

        return false;

}

/*---------------------------------------------------------------------------*/

function formatteHeure(champHeure,videAutorise){

        if(isValideHeure(champHeure.value,videAutorise)){

                champHeure.value = buff;

                return true;

        }

        champHeure.focus();

        return false;

}

/*---------------------------------------------------------------------------*/

function isValidDate(dateStr,videAutorise) {

   /*

    Teste la validité d'une date saisie dans 'ChampDate' (jjmmaaaa, ou jj-mm-aaaa, ou jj/mm/aaaa)

    Si invalide : Vide le champ , empèche le focus de partir du champ, et retourne false

    Si valide : formatte la saisie en jj/mm/aaaa

    S'utilise dans un champ formulaire avec [onblur="isValidDate(this);"]

          Refuse les dates vides

          @1 : le champ date

          @2 : le séparateur (/ ou -)

   */

           buff = '';

        var j_m_a   = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2,4})$/;

        var jmaa    = /^(\d{2})(\d{2})(\d{2})$/;

        var jmaaaa  = /^(\d{2})(\d{2})(\d{4})$/;



        if (dateStr=="") return videAutorise;



        var matchArray = dateStr.match(j_m_a);

         if (matchArray != null) {

                day   = matchArray[1];

                month = matchArray[3];

                year  = matchArray[4];

        }

        if (matchArray == null && dateStr.length == 6){

                matchArray = dateStr.match(jmaa);

                if (matchArray != null) {

                        day   = matchArray[1];

                        month = matchArray[2];

                        year  = matchArray[3];

                }

        }

        if (matchArray == null && dateStr.length == 8){

                matchArray = dateStr.match(jmaaaa);

                if (matchArray != null) {

                        day   = matchArray[1];

                        month = matchArray[2];

                        year  = matchArray[3];

                }

        }

        var msg = "";

        if (matchArray != null){

                if (month < 1 || month > 12) msg = "Mois : Saisie invalide";

                if (day < 1 || day > 31) msg = "Jour : Saisie invalide";

                if ((month==4 || month==6 || month==9 || month==11) && day==31) msg = "Mois "+month+" : moins de 31 jours!";

                if (month == 2) {

                        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));

                        if (day>29 || (day==29 && !isleap)) {

                                msg = "Février " + year + " : moins de " + day + " jours!";

                        }

                }

        } else msg = dateStr + " : Date invalide";



        if(msg == ""){

                if(day.length<2) day = "0" + day;

                if(month.length<2) month = "0" + month;

                if(year<=99) year = "20" + year;

                dateStr = day + sep2 + month + sep2 + year;

                buff = dateStr;

                return true;

        } else {

                return false;

        }

}

/*---------------------------------------------------------------------------*/

function formatteDate(champDate,separateur,videAutorise){

        sep2 = separateur;

        if(isValidDate(champDate.value,videAutorise)){

                champDate.value = buff;

                return true;

        }

        return false;

}

/*---------------------------------------------------------------------------*/

function hasValue(obj) {

        /*

        Retourne true si obj possède une valeur.

        (checkbox et radio cochés, combo et liste avec un

        élément sélectionné, zones de texte non vide)

        */

        if (obj.type == "text" || obj.type == "password" || obj.type == "textarea" || obj.type == "file")        {

                if (obj.value.length == 0) return false;else return true;



        } else if(obj.type == "select-one"){

                if(obj.selectedIndex >= 0) return true;else return false;



        } else if(obj.type == "select-multiple"){

                if( obj.selectedIndex == -1) return false;else return true;



        }        else if (obj.type == "radio" || obj.type == "checkbox")        {

                if (obj[0]) {

                        for (i=0; i < obj.length; i++) {

                                if (obj[i].checked)

                                        return true;

                        }

                } else {

                        return (obj.checked);

                }

                return false;

        }

}

/*---------------------------------------------------------------------------*/

function isInteger(object_value,videAutorise) {

        /*

        Retourne true si object_value est un entier

        */

        if (object_value.length == 0) return videAutorise;



        var decimal_format = ".";

        var check_char;



        check_char = object_value.indexOf(decimal_format);

        if (check_char < 1)

                return isNombre(object_value);

        else

                return false;

}

/*---------------------------------------------------------------------------*/

function isNombre(object_value,videAutorise) {

        /*

        Retourne true si object_value est un nombre

        (séparateur décimal '.' autorisé)

        */

        if (object_value.length == 0) return videAutorise;



        var start_format = " .+-0123456789";

        var number_format = " .0123456789";

        var check_char;

        var decimal = false;

        var trailing_blank = false;

        var digits = false;



        check_char = start_format.indexOf(object_value.charAt(0));

        if (check_char == 1)

                decimal = true;

        else if (check_char < 1)

                return false;



        for (var i = 1; i < object_value.length; i++)        {

                check_char = number_format.indexOf(object_value.charAt(i))

                if (check_char < 0) {

                        return false;

                } else if (check_char == 1)        {

                        if (decimal)

                                return false;

                        else

                                decimal = true;

                } else if (check_char == 0) {

                        if (decimal || digits)

                        trailing_blank = true;

                }        else if (trailing_blank) {

                        return false;

                } else {

                        digits = true;

                }

        }



        return true;

}

/*---------------------------------------------------------------------------*/

function isDansIntervalle(object_value, min_value, max_value) {

        /*

        Retourne true si object_value est un entier

        compris entre min_value et max_value

        */

        if(!isNombre(object_value,false)) return false;

        if (min_value != null) {

                if (object_value < min_value)

                        return false;

        }



        if (max_value != null) {

                if (object_value > max_value)

                        return false;

        }



        return true;

}

/*---------------------------------------------------------------------------*/

function ConcatValueCheckbox(Forme,nomChp){

        /*

                Retourne la liste (chaine. Séparateur ',') des values des checkbox cochés, de nom 'nomChp', de la forme 'Forme'

        */

        var sel="";virgule="";

        for (i=0; i < Forme.elements.length; i++){

                if (Forme.elements[i].type == "checkbox")

                    if (Forme.elements[i].checked &&  (Forme.elements[i].name == nomChp || nomChp == "")){

                        sel = sel + virgule + Forme.elements[i].value;

                                virgule=",";

                        }

        }

        return sel;

}

/*---------------------------------------------------------------------------*/

function Bisextile(annee){

    var isleap = (annee % 4 == 0 && (annee % 100 != 0 || annee % 400 == 0));

    return isleap;

}

/*---------------------------------------------------------------------------*/


