﻿var mdpOK = '0';
var abnID = '0';
var prmID = '0';
var isUnite = '0';
var ip = '';

// Remise à plat du MDP inscription
function resetMDP() 
{
    hideUnite();
    mdpOK = '0';
}

// Vérification du MDP inscription
function verifMDP(isSansMdP, mdpInscription)
{
    mdpOK = 0;
    var mdpi = $('#MDP_INSCRIPTION').val();
    if (isSansMdP) {
        mdpi = mdpInscription;
    }
    if (mdpi == '') {
        $('#attente-validation').html($('#hideen-val-nok').val());
        return;
    }
    $('#attente-validation').html($('#hideen-attente').val());
    $.ajax({
        url: 'Ajax/gereIdent.aspx',
        type: 'POST',
        dataType: 'xml',
        timeout: 1000,
        async: false,
        data: 'action=mdp&mdp=' + mdpi + '&ip=' + ip, 
        error: function(){
            return;
        },
        success: function(xml){
            mdpOK = $('root/retour', xml).text();
            abnID = $('root/abnID', xml).text();
            prmID = $('root/prmID', xml).text();
            var addUnite = $('root/isUnite', xml).text();
            if (mdpOK == '1') {
                $('#attente-validation').html($('#hideen-val-ok').val());
                if (isUnite == '0' && addUnite == '1') {
                    isUnite = '1';
                    showUnite($('root/unite', xml).text());
                }
            } else {
                resetMDP();
                $('#attente-validation').html($('#hideen-val-nok').val());
            }
        }
    });
}

// Affiche le ligne de la combo unité
function showUnite(html) {
    $('#lgn5').hide();
    $('#lgn6').show();
    if (html != '') {
        $('#cbo-choix-unite').html(html);
    }
}
// Cache le ligne de la combo unité
function hideUnite() {
    isUnite = 0;
    $('#lgn5').show();
    $('#lgn6').hide();
    $('#cbo-choix-unite').html('');
}

// Validation du formulaire
function validForm(isLogin, isSansMdP, mdpInscription)
{
    verifMDP(isSansMdP, mdpInscription);
    var isFormOK = verifForm(isLogin);
    if (mdpOK == 1 && isFormOK == 1) {
        document.forms['inscription'].submit();
    } else {
        return;
    }
}

// Vérification du formulaire
function verifForm(isLogin)
{
    var isOK = 1;
    if ($('#NOM').val() == '') {
        $('#td-nom').attr('class', 'labelRouge');
        isOK = 0;
    } else {
        $('#td-nom').attr('class', 'label');
    }
    if ($('#PRENOM').val() == '') {
        $('#td-prenom').attr('class', 'labelRouge');
        isOK = 0;
    } else {
        $('#td-prenom').attr('class', 'label');
    }
    if ($('#EMAIL').val() == '') {
        $('#td-mail').attr('class', 'labelRouge');
        isOK = 0;
    } else {
        $('#td-mail').attr('class', 'label');
    }
    if (isLogin == 1 && $('#MDP').val() == '') {
        $('#td-mdp').attr('class', 'labelRouge');
        isOK = 0;
    } else {
        $('#td-mdp').attr('class', 'label');
    }
    if (isUnite == 1) {
        var selectUnt = document.getElementById('UNT_ID');
        if (selectUnt.selectedIndex <= 0) {
            $('#td-unite').attr('class', 'labelRouge');
            isOK = 0;
        } else {
            $('#td-unite').attr('class', 'label');
        }
    }
    if (isOK == 1) {
        $('#h-err').html('&nbsp;');
    } else {
        $('#h-err').html($('#hidden-err').val());
    }
    return isOK;
}


// Script se déclenchant à la lecture de la page
function onLoadOps(isChpObligOK) {
    if (isUnite == '0') {
        hideUnite();
    } else {
        showUnite('');
    }
    if (isChpObligOK != 1) {
        verifForm();
    }
}