﻿/* Configuration */
var bufferLogin = false;

// Messages du système
var msg01_01 = "Identification incorrect";
var msg02_01 = "Un e-mail contenant les information relative à votre compte vient de vous être envoyé à cette adresse.";
var msg02_02 = "Cette adresse e-mail n'est pas connue ou n'est pas correctement formatée.";
var msg03_01 = "Un e-mail contenant les information relative à votre compte vient de vous être envoyé à cette adresse.";
var msg03_02 = "Les informations que vous avez saisies sont incorrectes.";
var msg03_03 = "Veuillez saisir le même mot de passe dans le champ précu pour le nouveau mot de passe et sa confirmation";

var TTLCookie = 1; // durée de vie du cookie en mois
var URLForgottenPwd = "/fr/mot-de-passe-oublie/index.htm"; // URL de la page de mot de passe oublié
var URLAdminUser = "/fr/gestion-des-utilisateurs/index.htm"; // URL de la page de gestion des utilisateurs
var URLAdminDoc = "/fr/gestion-des-documents/index.htm"; // URL de la page de gestion des documents
var URLListDoc = "/fr/liste-des-documents/index.htm"; // URL de la page de liste des documents
var URLChangePwd = "/fr/mot-de-passe-oublie/"; // URL de la page de changement de mot de passe
var URLAdminFormation = "/admin_formation.htm"; // URL de la page de gestion des formations

var POPUP = null;
var TEXTBOX = null;

/* Fonctions */

$(document).ready(function() {
    /* Chargement du block "menu espace privé" s'il existe */
    if($("#div_login").length > 0) {
        loadLoginBlock();
    }
    /* Identification automatique si besoin */
    if(getCook("login") != null && getCook("login") != "" && !bufferLogin)
    {
        postLogin(getCook("login"), getCook("pwd"));
    }
    /* Chargement du block "menu admin user" s'il existe */
    if($("#div_admin_user").length > 0) {
        loadAdminUser();
    }
    /* Chargement du block "menu admin doc" s'il existe */
    if($("#div_admin_doc").length > 0) {
        loadAdminDoc();
    }
    /* Chargement du block "document" s'il existe */
    if($("#div_documents").length > 0) {
        loadDoc();
    } 
    /* Chargement du block "pwd oublie" s'il existe */
    if($("#div_forgot_pwd").length > 0) {
        loadPwdOublie();
    }
    /* Chargement du block "pwd changement" s'il existe */
    if($("#div_change_pwd").length > 0) {
        loadPwdChange();
    } 
    /* Chargement du block "moteur" s'il existe */
    if($("#div_moteur").length > 0) {
        loadMoteur();
    }   
    /* Chargement du block "admin formation" s'il existe */
    if($("#div_admin_formation").length > 0) {
        loadAdminFormation();
    }          
});

function loadMoteur() {
    $.ajax({
        type: "POST",
        url: "/private_zone/ashx/moteur_load.ashx", 
        async: true,
        cache: false,
        success: function(data) {
            $("#div_moteur").html(data);
            $("#div_tabs").tabs({
                show: function(event, ui) {
                    var sHref = ui.tab.href;
                    sHref = sHref.substring(sHref.indexOf("#"), sHref.length)
                    switch(sHref) {
                        case "#tabs-1":
                            $("#tabs_2").animate({ backgroundColor: "#FFFFFF", color: "#000000" }, 300);
                            $("#tabs_3").animate({ backgroundColor: "#FFFFFF", color: "#000000" }, 300);
                            break;
                        case "#tabs-2":
                            $("#tabs_1").animate({ backgroundColor: "#FFFFFF", color: "#000000" }, 300);
                            $("#tabs_3").animate({ backgroundColor: "#FFFFFF", color: "#000000" }, 300);
                            LoadResultats();
                            break;
                        case "#tabs-3":
                            $("#tabs_1").animate({ backgroundColor: "#FFFFFF", color: "#000000" }, 300);
                            $("#tabs_2").animate({ backgroundColor: "#FFFFFF", color: "#000000" }, 300);
                            break;
                    };
                }
            });
            $("#div_tabs LI").mouseover(function() {
                if(!$(this).hasClass("ui-tabs-selected"))
                {
                    $("a", this).animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                }
            });
            $("#div_tabs LI").mouseout(function() {
                if(!$(this).hasClass("ui-tabs-selected"))
                {
                    $("a", this).animate({ backgroundColor: "#FFFFFF", color: "#000000" }, 300);
                }
            });
            $("#chk_multi_off").click(function() {
                $("#chk_multi_on")[0].checked = !$("#chk_multi_off")[0].checked;
            });
            $("#chk_multi_on").click(function() {
                $("#chk_multi_off")[0].checked = !$("#chk_multi_on")[0].checked;
            });
            $("#form_recherche").ajaxForm();
        }
    });
}

function LoadResultats() {
    var queryString = $("#form_recherche").formSerialize();
    $.ajax({
        type: "POST",
        url: "/private_zone/ashx/resultat_load.ashx",
        data: queryString,
        async: false,
        cache: false,
        success: function(data) {
            $("#tabs-2").html(data);
        }
    });
}

function showResultats() {
    $("#div_tabs").tabs("select", "tabs-2");
    $("#tabs_1").animate({ backgroundColor: "#FFFFFF", color: "#000000" }, 300);
    $("#tabs_2").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
    $("#tabs_3").animate({ backgroundColor: "#FFFFFF", color: "#000000" }, 300);
}

function LoadDetails(_ID) {
    $("#tabs_1").animate({ backgroundColor: "#FFFFFF", color: "#000000" }, 300);
    $("#tabs_2").animate({ backgroundColor: "#FFFFFF", color: "#000000" }, 300);
    $("#tabs_3").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
    $("#div_tabs").tabs("select", "tabs-3");
    
     $.ajax({
        type: "POST",
        url: "/private_zone/ashx/detail_load.ashx", 
        data: "id=" + _ID,
        async: false,
        cache: false,
        success: function(data) {
	    while(data.indexOf("||") > -1) {
                data = data.replace("||", "<br/>");
            }
            $("#tabs-3").html(data);
            $("#div_tabs_details").tabs({
                show: function(event, ui) {
                    var sHref = ui.tab.href;
                    sHref = sHref.substring(sHref.indexOf("#"), sHref.length)
                    switch(sHref) {
                        case "#tabs_details-1":
                            $("#tabs_details_2").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_3").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_4").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_5").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_6").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_7").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_8").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            break;
                        case "#tabs_details-2":
                            $("#tabs_details_1").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_3").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_4").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_5").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_6").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_7").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_8").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            break;
                        case "#tabs_details-3":
                            $("#tabs_details_1").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_2").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_4").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_5").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_6").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_7").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_8").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            break;
                        case "#tabs_details-4":
                            $("#tabs_details_1").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_2").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_3").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_5").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_6").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_7").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_8").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            break;
                        case "#tabs_details-5":
                            $("#tabs_details_1").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_2").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_3").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_4").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_6").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_7").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_8").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            break;
                        case "#tabs_details-6":
                            $("#tabs_details_1").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_2").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_3").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_4").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_5").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_7").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_8").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            break;
                        case "#tabs_details-7":
                            $("#tabs_details_1").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_2").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_3").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_4").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_5").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_6").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_8").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            break;
                        case "#tabs_details-8":
                            $("#tabs_details_1").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_2").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_3").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_4").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_5").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_6").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            $("#tabs_details_7").animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                            break;
                    };
                }
            });
            $("#div_tabs_details LI").mouseover(function() {
                if(!$(this).hasClass("ui-tabs-selected"))
                {
                    $("a", this).animate({ backgroundColor: "#FFFFFF", color: "#000000" }, 300);
                }
            });
            $("#div_tabs_details LI").mouseout(function() {
                if(!$(this).hasClass("ui-tabs-selected"))
                {
                    $("a", this).animate({ backgroundColor: "#A20000", color: "#FFFFFF" }, 300);
                }
            });
        }
    });   
}

function loadAdminFormation() {
    $("#div_admin_formation").html("");
    $.ajax({
        type: "POST",
        url: "/private_zone/ashx/load_admin_formation.ashx", 
        async: true,
        cache: false,
        success: function(data) {
            $("#div_admin_formation").html(data);
                /* Gestion popup */
                POPUP = $("#div_popup");
                if(POPUP != null) {
                    POPUP.dialog({
                        autoOpen: false,
                        modal: true,
                        close: function(event, ui) {
                            loadAdminUser();
                        }
                    });    
                }
        }
    });
}

function loadLoginBlock() {
    $.ajax({
        type: "POST",
        url: "/private_zone/ashx/load_identification.ashx", 
        async: true,
        cache: false,
        success: function(data) {
            $("#div_login").html(data);
        }
    });
}

function clickLogin() {
    postLogin($("#txtLogin")[0].value, $.md5($("#txtPwd")[0].value));
}

function postLogin(sLogin, sPwd) {
    $.ajax({
        type: "POST",
        url: "/private_zone/ashx/login.ashx", 
        data: "login=" + sLogin + "&pwd=" + sPwd,
        async: false,
        cache: false,
        success: function(data) {
            switch(data) {
                case "OK":
                    if($("#chkRemind").length > 0 && $("#chkRemind")[0].checked) {
                        setCook("login", $("#txtLogin")[0].value);
                        setCook("pwd", $.md5($("#txtPwd")[0].value));
                    }
                    bufferLogin = true;
                    loadLoginBlock();
                    break;
                case "NOK":
                    bufferLogin = false;
                    $("#msgAlert01").html(msg01_01);
                    break;
            }
        }
    });
}

function clickLogout() {
    $.ajax({
        type: "POST",
        url: "/private_zone/ashx/logout.ashx", 
        async: false,
        cache: false,
        success: function(data) {
            setCook("login", "");
            setCook("pwd", "");
            bufferLogin = false;
            loadLoginBlock();
        }
    });
}

function clickForgottenPwd() {
    document.location.href = URLForgottenPwd;
}

function clickAdminFormation() {
    document.location.href = URLAdminFormation;
}

function clickSendNewPassword() {
    $.ajax({
        type: "POST",
        url: "/private_zone/ashx/new_pwd.ashx",
        data: "mail=" + $("#txtMail")[0].value,
        async: false,
        cache: false,
        success: function(data) {
            switch(data) {
                case "OK":
                    $("#msgAlert02").html(msg02_01);
                    break;
                case "NOK":
                    $("#msgAlert02").html(msg02_02);
                    break;
            }
        }
    });
}

function clickChangePassword() {
    if($.trim($("#txtNewPwd1")[0].value) != '' && $("#txtNewPwd1")[0].value == $("#txtNewPwd2")[0].value) {
        $.ajax({
            type: "POST",
            url: "/private_zone/ashx/change_pwd.ashx",
            data: "old=" + $("#txtOldPwd")[0].value + "&new=" + $("#txtNewPwd1")[0].value,
            async: false,
            cache: false,
            success: function(data) {
                switch(data) {
                    case "OK":
                        $("#txtOldPwd")[0].value = "";
                        $("#txtNewPwd1")[0].value = "";
                        $("#txtNewPwd2")[0].value = "";
                        $("#msgAlert03").html(msg03_01);
                        break;
                    case "NOK":
                        $("#msgAlert03").html(msg03_02);
                        break;
                }
            }
        });    
    }
    else {
        $("#msgAlert03").html(msg03_03);
    }
}

function loadAdminUser() {
    $.ajax({
        type: "POST",
        url: "/private_zone/ashx/load_admin_user.ashx", 
        async: true,
        cache: false,
        success: function(data) {
            $("#div_admin_user").html(data);
                /* Gestion popup */
                POPUP = $("#div_popup");
                if(POPUP != null) {
                    POPUP.dialog({
                        autoOpen: false,
                        modal: true,
                        close: function(event, ui) {
                            loadAdminUser();
                        }
                    });    
                }
        }
    });
}

function clickAdminUser() {
    document.location.href = URLAdminUser;
}

function clickAdminDoc() {
    document.location.href = URLAdminDoc;
}

function clickListDoc() {
    document.location.href = URLListDoc;
}

function clickChangePwd() {
    document.location.href = URLChangePwd;
}
/* Admin Users */

function add_user_click() {
    $.ajax({
        type: "POST",
        url: "/private_zone/ashx/user_add.ashx",
        data: "nom=" + $("#new_nom")[0].value + "&prenom=" + $("#new_prenom")[0].value + "&role=" + $("#new_role")[0].value + "&nomrole=" + $("#new_role")[0].childNodes[$("#new_role")[0].selectedIndex].innerHTML + "&orga=" + $("#new_orga")[0].value + "&nomorga=" + $("#new_orga")[0].childNodes[$("#new_orga")[0].selectedIndex].innerHTML + "&pwd=" + $("#new_pwd")[0].value + "&email=" + $("#new_email")[0].value,
        async: true,
        cache: false,
        complete: function(_xhr) {
            loadAdminUser();
        }
    });
}

function nom_change(oObj, nID) {
    $.ajax({
        type: "POST",
        url: "/private_zone/ashx/user_change.ashx",
        data: "id=" +  nID + "&field=" + "nom" + "&value=" + oObj.value,
        async: true,
        cache: false
    });
}

function prenom_change(oObj, nID) {
    $.ajax({
        type: "POST",
        url: "/private_zone/ashx/user_change.ashx",
        data: "id=" +  nID + "&field=" + "prenom" + "&value=" + oObj.value,
        async: true,
        cache: false
    });
}

function role_change(oObj, nID) {
    _Value2 = oObj.childNodes[oObj.selectedIndex].textContent;
    if(_Value2 == undefined) {
        _Value2 = oObj.childNodes[oObj.selectedIndex].innerHTML;
    }
    $.ajax({
        type: "POST",
        url: "/private_zone/ashx/user_change.ashx",
        data: "id=" +  nID + "&field=" + "role" + "&value=" + oObj.value + "&value2=" + _Value2,
        async: true,
        cache: false
    });
}

function orga_change(oObj, nID) {
    _Value2 = oObj.childNodes[oObj.selectedIndex].textContent;
    if(_Value2 == undefined) {
        _Value2 = oObj.childNodes[oObj.selectedIndex].innerHTML;
    }
    $.ajax({
        type: "POST",
        url: "/private_zone/ashx/user_change.ashx",
        data: "id=" +  nID + "&field=" + "orga" + "&value=" + oObj.value + "&value2=" + _Value2,
        async: true,
        cache: false
    });
}

function accept_pwd_click(nID) {
    _VAL = $("#field_pwd_" + nID + " input")[0].value;
    if($.trim(_VAL) != "") {
        $.ajax({
            type: "POST",
            url: "/private_zone/ashx/user_change.ashx",
            data: "id=" +  nID + "&field=" + "pwd" + "&value=" + _VAL,
            async: true,
            cache: false
        });
        $("#field_pwd_" + nID).hide(); 
        $("#bt_pwd_" + nID).show();   
    }
}

function cancel_pwd_click(nID) {
    $("#field_pwd_" + nID).hide();
    $("#bt_pwd_" + nID).show();
    $("#field_pwd_" + nID)[0].value = "";
}

function change_pwd_click(nID) {
    $("#bt_pwd_" + nID).hide();
    $("#field_pwd_" + nID).show();
    $("#field_pwd_" + nID)[0].value = "";
}

function email_change(oObj, nID) {
    $.ajax({
        type: "POST",
        url: "/private_zone/ashx/user_change.ashx",
        data: "id=" +  nID + "&field=" + "email" + "&value=" + oObj.value,
        async: true,
        cache: false
    });
}

function remove_user_click(nID) {
    $.ajax({
        type: "POST",
        url: "/private_zone/ashx/user_change.ashx",
        data: "id=" +  nID + "&field=" + "inactive",
        async: true,
        cache: false,
        complete: function(xhr) {
            loadAdminUser();
        }
    });
}

function admin_orga_click() { 
    

    if(POPUP.html() == "") {
        $.ajax({
            type: "POST",
            url: "/private_zone/ashx/load_admin_orga.ashx",
            async: true,
            cache: false,
            success: function(data) {
                 POPUP.html(data);
                 TEXTBOX = $("#txt_new");
            }
        });  
    }          

    POPUP.dialog("open");
}

function admin_cat_click() { 
    if(POPUP.html() == "") {
        $.ajax({
            type: "POST",
            url: "/private_zone/ashx/load_admin_cat.ashx",
            async: true,
            cache: false,
            success: function(data) {
                 POPUP.html(data);
                 TEXTBOX = $("#txt_new");
            }
        }); 
    }          

    POPUP.dialog("open");
}
function listitem_add(sTable) {
    _url = "";
    if(sTable == "organismes") {
        _url = "/private_zone/ashx/load_admin_orga.ashx";
    }
    else {
        _url = "/private_zone/ashx/load_admin_cat.ashx";
    }
    $.ajax({
        type: "POST",
        url: "/private_zone/ashx/listiem_action.ashx",
        data: "action=" + "add" + "&table=" + sTable + "&value=" + TEXTBOX[0].value,
        async: false,
        cache: false,
        complete: function(xhr) {
            $.ajax({
                type: "POST",
                url: _url,
                async: false,
                cache: false,
                success: function(data) {
                     POPUP.html(data);
                }
            }); 
        }
    });
}

function listitem_change(sTable, sID, sValue) {
    _url = "";
    if(sTable == "organismes") {
        _url = "/private_zone/ashx/load_admin_orga.ashx";
    }
    else {
        _url = "/private_zone/ashx/load_admin_cat.ashx";
    }
    $.ajax({
        type: "POST",
        url: "/private_zone/ashx/listiem_action.ashx",
        data: "action=" + "mod" + "&table=" + sTable + "&id=" + sID + "&value=" + sValue,
        async: false,
        cache: false,
        complete: function(xhr) {
            $.ajax({
                type: "POST",
                url: _url,
                async: false,
                cache: false,
                success: function(data) {
                     POPUP.html(data);
                }
            }); 
        }
    });
}

function listitem_delete(sTable, sID) {
    _url = "";
    if(sTable == "organismes") {
        _url = "/private_zone/ashx/load_admin_orga.ashx";
    }
    else {
        _url = "/private_zone/ashx/load_admin_cat.ashx";
    }
    $.ajax({
        type: "POST",
        url: "/private_zone/ashx/listiem_action.ashx",
        data: "action=" + "del" + "&table=" + sTable + "&id=" + sID,
        async: false,
        cache: false,
        complete: function(xhr) {
            $.ajax({
                type: "POST",
                url: _url,
                async: false,
                cache: false,
                success: function(data) {
                     POPUP.html(data);
                }
            }); 
        }
    });
}

function listitem_active(sTable, sID, sValue) {
    _url = "";
    if(sTable == "organismes") {
        _url = "/private_zone/ashx/load_admin_orga.ashx";
    }
    else {
        _url = "/private_zone/ashx/load_admin_cat.ashx";
    }
    $.ajax({
        type: "POST",
        url: "/private_zone/ashx/listiem_action.ashx",
        data: "action=" + "act" + "&table=" + sTable + "&id=" + sID + "&value=" + sValue,
        async: false,
        cache: false,
        complete: function(xhr) {
            $.ajax({
                type: "POST",
                url: _url,
                async: false,
                cache: false,
                success: function(data) {
                    POPUP.html(data);
                }
            }); 
        }
    });
}

function listitem_desactive(sTable, sID, sValue) {
    _url = "";
    if(sTable == "organismes") {
        _url = "/private_zone/ashx/load_admin_orga.ashx";
    }
    else {
        _url = "/private_zone/ashx/load_admin_cat.ashx";
    }
    $.ajax({
        type: "POST",
        url: "/private_zone/ashx/listiem_action.ashx",
        data: "action=" + "des" + "&table=" + sTable + "&id=" + sID + "&value=" + sValue,
        async: false,
        cache: false,
        complete: function(xhr) {
            $.ajax({
                type: "POST",
                url: _url,
                async: false,
                cache: false,
                success: function(data) {
                    POPUP.html(data);
                }
            }); 
        }
    });
}

/* Admin Doc */

function loadAdminDoc() {
    $.ajax({
        type: "POST",
        url: "/private_zone/ashx/load_admin_doc.ashx", 
        async: true,
        cache: false,
        success: function(data) {
            $("#div_admin_doc").html(data);
            POPUP = $("#div_popup");
            if(POPUP != null) {
                POPUP.dialog({
                    autoOpen: false,
                    modal: true,
                    close: function(event, ui) {
						loadAdminDoc();
                    }
                });    
            }
            if($(".doc_form").length > 0) {
                $(".doc_form").ajaxForm({
                    success: function(responseText, statusText, xhr, $form) {
                        if(responseText != "OK") {
                            alert(responseText);
                        }
                        loadAdminDoc();
                    }
                });        
            }
        }
    });
 }

function doc_titre_change(oObj, nID) {
    $.ajax({
        type: "POST",
        url: "/private_zone/ashx/doc_change.ashx",
        data: "id=" +  nID + "&field=" + "titre" + "&value=" + oObj.value,
        async: true,
        cache: false
    });
}

function doc_desc_change(oObj, nID) {
    $.ajax({
        type: "POST",
        url: "/private_zone/ashx/doc_change.ashx",
        data: "id=" +  nID + "&field=" + "desc" + "&value=" + oObj.value,
        async: true,
        cache: false
    });
}

function doc_cat_change(oObj, nID) {
    _Value2 = oObj.childNodes[oObj.selectedIndex].textContent;
    if(_Value2 == undefined) {
        _Value2 = oObj.childNodes[oObj.selectedIndex].innerHTML;
    }
    $.ajax({
        type: "POST",
        url: "/private_zone/ashx/doc_change.ashx",
        data: "id=" +  nID + "&field=" + "cat" + "&value=" + oObj.value + "&value2=" + _Value2,
        async: true,
        cache: false
    });
}

function doc_role_click(oObj, nID) {
    _Value2 = oObj.parentNode.childNodes[oObj.parentNode.childNodes.length - 1].textContent;
    if(_Value2 == undefined) {
        _Value2 = oObj.parentNode.innerText;
    }
    $.ajax({
        type: "POST",
        url: "/private_zone/ashx/doc_change.ashx",
        data: "id=" +  nID + "&field=" + "role" + "&value=" + oObj.value + "&value2=" + _Value2,
        async: true,
        cache: false
    });
}

function doc_activate(nID) {
    $.ajax({
        type: "POST",
        url: "/private_zone/ashx/doc_change.ashx",
        data: "id=" +  nID + "&field=" + "act",
        async: true,
        cache: false,
        complete: function(xhr) {
            loadAdminDoc();
        }
    });
}

function doc_desactivate(nID) {
    $.ajax({
        type: "POST",
        url: "/private_zone/ashx/doc_change.ashx",
        data: "id=" +  nID + "&field=" + "des",
        async: true,
        cache: false,
        complete: function(xhr) {
            loadAdminDoc();
        }
    });
}

function doc_delete(nID) {
    if(confirm("voulez-vous supprimer ce document?")) {
        $.ajax({
            type: "POST",
            url: "/private_zone/ashx/doc_change.ashx",
            data: "id=" +  nID + "&field=" + "del",
            async: true,
            cache: false,
            complete: function(xhr) {
                loadAdminDoc();
            }
        });
    }
}

/* Documents */

function loadDoc() {
    $.ajax({
        type: "POST",
        url: "/private_zone/ashx/load_doc.ashx", 
        async: true,
        cache: false,
        success: function(data) {
            $("#div_documents").html(data);
        }
    });
}

function loadPwdOublie() {
    $.ajax({
        type: "POST",
        url: "/private_zone/ashx/forget_pwd.ashx", 
        async: true,
        cache: false,
        success: function(data) {
            $("#div_forgot_pwd").html(data);
        }
    });
}

function loadPwdChange() {
    $.ajax({
        type: "POST",
        url: "/private_zone/ashx/change_pwd_form.ashx", 
        async: true,
        cache: false,
        success: function(data) {
            $("#div_change_pwd").html(data);
        }
    });
}
/* Utilities */

function setCook(nom, valeur) {
    date = new Date;
    date.setMonth(date.getMonth() + TTLCookie);
    document.cookie = nom + "=" + escape(valeur) + "; expires=" + date.toGMTString();
}

function getCook(nom) {
    deb = document.cookie.indexOf(nom + "=");
    if (deb >= 0) {
        deb += nom.length + 1;
        fin = document.cookie.indexOf(";", deb);
        if (fin < 0) {
            fin = document.cookie.length; 
        }
        return unescape(document.cookie.substring(deb, fin));
    }
    return "";
}

/* Formations */
var tabWym = new Array();

function add_formation_click() {
    tabWym = new Array();
    $("#div_admin_formation").html("");
    $.ajax({
        type: "POST",
        url: "/private_zone/ashx/edit_formation.ashx", 
        async: false,
        cache: false,
        data: "id=0",
        success: function(data) {
            $("#div_admin_formation").html(data);
            /* Wymeditor */
            $(".wymeditor").wymeditor({
                html: "",
                basePath: "/private_zone/wymeditor/",
                cssPath: "/private_zone/wymeditor/skins/default/skin.css",
                containersHtml: "",
                classesHtml: "",
                postInit: function(wym) {
                    tabWym[wym._element[0].id] =  wym;
                }
            });
             /* Form */
            $("#form_edit").ajaxForm();
            /* Gestion popup */
            POPUP = $("#div_popup");
            if(POPUP != null) {
                POPUP.dialog({
                    autoOpen: false,
                    modal: true,
                    close: function(event, ui) {
                    
                    }
                });    
            }
        }
    });
}

function mod_formation_click(_ID) {
    tabWym = new Array();
    $("#div_admin_formation").html("");
    $.ajax({
        type: "POST",
        url: "/private_zone/ashx/edit_formation.ashx", 
        async: false,
        cache: false,
        data: "id=" + _ID,
        success: function(data) {
            $("#div_admin_formation").html(data);
            /* Wymeditor */
            $(".wymeditor").wymeditor({
                html: "",
                basePath: "/private_zone/wymeditor/",
                cssPath: "/private_zone/wymeditor/skins/default/skin.css",
                containersHtml: "",
                classesHtml: "",
                postInit: function(wym) {
                    tabWym[wym._element[0].id] =  wym;
                }
            });
             /* Form */
            $("#form_edit").ajaxForm();
            /* Gestion popup */
            POPUP = $("#div_popup");
            if(POPUP != null) {
                POPUP.dialog({
                    autoOpen: false,
                    modal: true,
                    close: function(event, ui) {
                    
                    }
                });    
            }
            /* Tabs */
            $("#div_tabs_admin").tabs();
        }
    });
}

function del_formation_click(_ID) {
    if(confirm("Voulez vous vraiment supprimer cette formation?")) {
        $.ajax({
            type: "POST",
            url: "/private_zone/ashx/delete_formation.ashx",
            data: "id=" + _ID,
            async: false,
            cache: false,
            success: function(data) {
                $("#div_change_pwd").html(data);
            }
        });
        loadAdminFormation();
    }
}

function saveFormation() {
    var queryString = $("#form_edit").formSerialize();
    queryString += "&wym_detail=" + Base64.encode( tabWym["wym_detail"].html());
    queryString += "&wym_acces=" + Base64.encode(tabWym["wym_acces"].html());
    queryString += "&wym_remediation=" + Base64.encode(tabWym["wym_remediation"].html());
    queryString += "&wym_competence=" + Base64.encode(tabWym["wym_competence"].html());
    queryString += "&wym_pratique=" + Base64.encode(tabWym["wym_pratique"].html());
    queryString += "&wym_certification=" + Base64.encode(tabWym["wym_certification"].html());
    queryString += "&wym_passerelle=" + Base64.encode(tabWym["wym_passerelle"].html());
    $.ajax({
        type: "POST",
        url: "/private_zone/ashx/save_formation.ashx",
        data: queryString,
        async: false,
        cache: false,
        success: function(data) {
            if(data != "") {
                alert(data);
            }
            else {
                loadAdminFormation();
            }
        }
    });
}

var Base64 = {

    // private property
    _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

    // public method for encoding
    encode: function(input) {
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;

        input = Base64._utf8_encode(input);

        while (i < input.length) {

            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);

            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;

            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }

            output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

        }

        return output;
    },

    // public method for decoding
    decode: function(input) {
        var output = "";
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0;

        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

        while (i < input.length) {

            enc1 = this._keyStr.indexOf(input.charAt(i++));
            enc2 = this._keyStr.indexOf(input.charAt(i++));
            enc3 = this._keyStr.indexOf(input.charAt(i++));
            enc4 = this._keyStr.indexOf(input.charAt(i++));

            chr1 = (enc1 << 2) | (enc2 >> 4);
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
            chr3 = ((enc3 & 3) << 6) | enc4;

            output = output + String.fromCharCode(chr1);

            if (enc3 != 64) {
                output = output + String.fromCharCode(chr2);
            }
            if (enc4 != 64) {
                output = output + String.fromCharCode(chr3);
            }

        }

        output = Base64._utf8_decode(output);

        return output;

    },

    // private method for UTF-8 encoding
    _utf8_encode: function(string) {
        string = string.replace(/\r\n/g, "\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if ((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode: function(utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while (i < utftext.length) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if ((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i + 1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i + 1);
                c3 = utftext.charCodeAt(i + 2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}
