$.ajaxSetup({
    beforeSend: function () {
        $("#waitajaxOverlay").show();
    },
    complete: function () {
        $("#waitajaxOverlay").hide();
    },
    error: function (jqXHR, exception) {
        var msg = 'Erreur d\'appel AJAX
';
        if (jqXHR.status === 0) {
            msg += 'Connexion impossible';
        } else if (jqXHR.status == 404) {
            msg += 'Page non trouvée';
        } else if (jqXHR.status == 500) {
            msg += 'Erreur interne serveur';
        } else if (exception === 'parsererror') {
            msg += 'Erreur JSON';
        } else if (exception === 'timeout') {
            msg += 'Temps de connexion trop long';
        } else if (exception === 'abort') {
            msg += 'Requête AJAX interrompue';
        } else {
            msg += 'Erreur : ' + jqXHR.responseText;
        }
        warn_txt(msg);
        $("#waitajaxOverlay").hide();
    }
});
// MAJ ville en fonction département
function init_ajax_ville() {
    $("#dep").change(function () {
        var val_codepays;
        var val_dep;
        var val_ville;
        var $ville = $("#ville");
        $ville.empty();
        val_ville = $ville.val();
        val_dep = $("#dep").val();
        if (val_dep != '') {
            val_codepays = $("#pays").val();
            $.ajax({
                url: "https://intraservice.olsy.ch/ajax/recupville.php",
                data: "dep=" + val_dep + "_" + val_codepays,
                type: "GET",
                success: function (data) {
                    var i;
                    var opt_val;
                    var options;
                    $ville.append('');
                    if (data != '') {
                        options = data.split(";");
                        for (i = 0; i < options.length; i++) {
                            opt_val = options[i].split("="); // value = text
                            $ville.append('');
                        }
                        $ville.val(val_ville);
                    }
                }
            });
        }
    });
}
// MAJ département en fonction pays
function init_ajax_dep() {
    $("#pays").change(function () {
        var val_codepays;
        var val_ville;
        var $ville;
        var $dep = $("#dep");
        var val_dep = $dep.val();
        $dep.empty();
        $dep.append('');
        $ville = $("#ville");
        val_ville = $ville.val();
        $ville.empty();
        $ville.append('');
        val_codepays = $("#pays").val();
        if (val_codepays != '') {
            $.ajax({
                url: "https://intraservice.olsy.ch/ajax/recupdep.php",
                data: "pays=" + val_codepays,
                type: "GET",
                success: function (data) {
                    var i;
                    var opt_val;
                    var options;
                    if (data != '') {
                        options = data.split(";");
                        for (i = 0; i < options.length; i++) {
                            opt_val = options[i].split("="); // value = text
                            $dep.append('');
                        }
                    }
                    $dep.val(val_dep);
                    $ville.val(val_ville);
                    $("#waitajaxOverlay").hide();
                }
            });
        }
    });
}
// MAJ département en fonction pays (pas de message "inconnu" - Ne tient pas compte d'une zone villle)
function init_ajax_dep2() {
    $("#pays").change(function () {
        var val_codepays;
        var $dep = $("#dep");
        var val_dep = $dep.val();
        $dep.empty();
        $dep.append('');
        val_codepays = $("#pays").val();
        if (val_codepays != '') {
            $.ajax({
                url: "https://intraservice.olsy.ch/ajax/recupdep2.php",
                data: "pays=" + val_codepays,
                type: "GET",
                success: function (data) {
                    var i;
                    var opt_val;
                    var options;
                    if (data != '') {
                        options = data.split(";");
                        for (i = 0; i < options.length; i++) {
                            opt_val = options[i].split("="); // value = text
                            $dep.append('');
                        }
                        $dep.val(val_dep);
                    }
                }
            });
        }
    });
}
// Récupération des départements en zone de chalandise en fonction pays (Fiche client)
function init_ajax_dep_zc_pays_cli() {
    $("#pays").change(function () {
        var val_codepays;
        var val_ville;
        var $ville;
        var $dep = $("#dep");
        var val_dep = $dep.val();
        $dep.empty();
        $dep.append('');
        $ville = $("#ville");
        val_ville = $ville.val();
        $ville.empty();
        $ville.append('');
        val_codepays = $("#pays").val();
        if (val_codepays != '') {
            $.ajax({
                url: "https://intraservice.olsy.ch/ajax/recupdepzccli.php",
                data: "pays=" + val_codepays,
                type: "GET",
                success: function (data) {
                    var i;
                    var opt_val;
                    var options;
                    if (data != '') {
                        options = data.split(";");
                        for (i = 0; i < options.length; i++) {
                            opt_val = options[i].split("="); // value = text
                            $dep.append('');
                        }
                    }
                    $dep.val(val_dep);
                    $ville.val(val_ville);
                    $("#waitajaxOverlay").hide();
                }
            });
        }
    });
}
// Récupération des départements en zone de chalandise en fonction pays
function init_ajax_dep_zc_pays() {
    $("#pays").change(function () {
        var val_codepays;
        var $zone = $("#zone");
        $zone.empty();
        $zone.append('');
        val_codepays = $("#pays").val();
        if (val_codepays != '') {
            $.ajax({
                url: "https://intraservice.olsy.ch/ajax/recupdepzc.php",
                data: "pays=" + val_codepays,
                type: "GET",
                success: function (data) {
                    var i;
                    var opt_val;
                    var options;
                    if (data != '') {
                        options = data.split(";");
                        for (i = 0; i < options.length; i++) {
                            opt_val = options[i].split("="); // value = text
                            $zone.append('');
                        }
                    }
                }
            });
        }
    });
}
// MAJ départements en fonction pays (Onglet "zone de chalandise" des agences)
function init_ajax_depzc() {
    $("#payszc").change(function () {
        var val_codepayszc;
        var $depzc = $("#depzc");
        $depzc.empty();
        val_codepayszc = $("#payszc").val();
        if (val_codepayszc != '') {
            $.ajax({
                url: "https://intraservice.olsy.ch/ajax/recupdep.php",
                data: "pays=" + val_codepayszc,
                type: "GET",
                success: function (data) {
                    var i;
                    var opt_val;
                    var options;
                    if (data != '') {
                        options = data.split(";");
                        for (i = 0; i < options.length; i++) {
                            opt_val = options[i].split("="); // value = text
                            $depzc.append('');
                        }
                    }
                }
            });
        }
    });
}
// Récupération des départements en zone de chalandise en fonction pays (pour les sous-traitants)
function init_ajax_dep_zc_pays_2() {
    $("#pays").change(function () {
        var val_codepays;
        var $zone = $("#zone");
        $zone.empty();
        $zone.append('');
        val_codepays = $("#pays").val();
        if (val_codepays != '') {
            $.ajax({
                url: "https://intraservice.olsy.ch/ajax/recupdepzc2.php",
                data: "pays=" + val_codepays,
                type: "GET",
                success: function (data) {
                    var i;
                    var opt_val;
                    var options;
                    if (data != '') {
                        options = data.split(";");
                        for (i = 0; i < options.length; i++) {
                            opt_val = options[i].split("="); // value = text
                            $zone.append('');
                        }
                    }
                }
            });
        }
    });
}
// Récupération des départements en zone de chalandise en fonction agence (pour export)
function init_ajax_dep_zc_pays_3() {
    $("#age").change(function () {
        var val_codepays;
        var val_age;
        var $dep = $("#dep");
        $dep.empty();
        $dep.append('');
        val_age = $("#age").val();
        val_codepays = $("#pays").val();
        if (val_codepays != '') {
            $.ajax({
                url: "https://intraservice.olsy.ch/ajax/recupdepzc5.php",
                data: "age=" + val_age + "&pays=" + val_codepays,
                type: "GET",
                success: function (data) {
                    var i;
                    var opt_val;
                    var options;
                    if (data != '') {
                        options = data.split(";");
                        for (i = 0; i < options.length; i++) {
                            opt_val = options[i].split("="); // value = text
                            $dep.append('');
                        }
                    }
                }
            });
        }
    });
}
// Récupération des départements en zone de chalandise en fonction pays (pour export)
function init_ajax_dep_zc_pays_4() {
    $("#pays").change(function () {
        var val_codepays;
        var val_age;
        var $dep = $("#dep");
        $dep.empty();
        $dep.append('');
        val_age = $("#age").val();
        val_codepays = $("#pays").val();
        if (val_age != '') {
            $.ajax({
                url: "https://intraservice.olsy.ch/ajax/recupdepzc5.php",
                data: "age=" + val_age + "&pays=" + val_codepays,
                type: "GET",
                success: function (data) {
                    var i;
                    var opt_val;
                    var options;
                    if (data != '') {
                        options = data.split(";");
                        for (i = 0; i < options.length; i++) {
                            opt_val = options[i].split("="); // value = text
                            $dep.append('');
                        }
                    }
                }
            });
        }
    });
}
// MAJ départements couverts en fonction pays (Onglet "zone de chalandise" des agences - clic sur "Ajouter")
function init_ajax_add_depzc() {
    $("#addzc").click(function () {
        var i, len;
        var existe;
        var new_value;
        var str_dep;
        var $depzcsel = [];
        var $codepayszcsel = $("#payszc").find('option:selected').val();
        if (($codepayszcsel) && ($codepayszcsel != '')) {
            // On vérifie (si demandé), si pas déjà sélectionné
            $('#depzc').find('option:selected').each(function() {
                $depzcsel.push($(this).val());
            });
            if ($depzcsel.length > 0) {
                str_dep = '';
                new_value = '';
                existe = false;
                for (i = 0, len = $depzcsel.length; i < len; i++) {
                    if ($depzcsel[i] != '') {
                        existe = false;
                        new_value = $depzcsel[i] + '_' + $codepayszcsel;
                        $('#zone').find('option').each(function () {
                            if ($depzcsel[i] == new_value) {
                                existe = true;
                            }
                        });
                        if (existe == false) {
                            str_dep += $depzcsel[i] + '_';
                        }
                    }
                }
                if (str_dep != '') {
                    // Faire l'appel Ajax
                    $.ajax({
                        url: "https://intraservice.olsy.ch/ajax/recupdepzc3.php",
                        data: "txt=" + str_dep + $codepayszcsel,
                        type: "GET",
                        success: function (data) {
                            var i;
                            var opt_val;
                            var options;
                            var $zone;
                            $zone = $('#zone');
                            if (data != '') {
                                $zone.empty();
                                options = data.split(";");
                                for (i = 0; i < options.length; i++) {
                                    opt_val = options[i].split("="); // value = text
                                    $zone.append('');
                                }
                            }
                            else {
                                $zone.append('');
                            }
                        }
                    });
                }
                else {
                    warn_txt('Déjà choisi');
                }
            }
            else {
                warn_txt('Impossible d\'ajouter');
            }
        }
    });
}
// MAJ départements couverts en fonction pays (Onglet "zone de chalandise" des agences - clic sur "Supprimer")
function init_ajax_del_depzc() {
    $("#delzc").click(function () {
        var str_dep;
        var $zone = $("#zone");
        var $zonelng = $zone.find("option").length;
        var $zonesel = $zone.find('option:selected').val();
        if ($zonelng > 1) {
            if (($zonesel) && ($zonesel != '')) {
                str_dep = '';
                $zone.find('option:selected').each(function () {
                    if (this.value != '') {
                        if (str_dep != '') {
                            str_dep += '§';
                        }
                        str_dep += this.value;
                    }
                });
                if (str_dep != '') {
                    // Faire l'appel Ajax
                    $.ajax({
                        url: "https://intraservice.olsy.ch/ajax/recupdepzc4.php",
                        data: "txt=" + str_dep,
                        type: "GET",
                        success: function (data) {
                            $('#zone').find('option:selected').remove();
                        }
                    });
                }
            }
            else {
                warn_txt('Rien de choisi');
            }
        }
        else {
            warn_txt('Impossible de supprimer');
        }
    });
}
// MAJ département et zone chalandise en fonction pays
function init_ajax_dep_et_zc() {
    $("#pays").change(function () {
        var val_codepays;
        var $zonetxt;
        var $zone;
        var $ville;
        var $dep = $("#dep");
        $dep.empty();
        $dep.append('');
        $ville = $("#ville");
        $ville.empty();
        $ville.append('');
        $zone = $("#zone");
        $zone.empty();
        $zone.append('');
        $zonetxt = $("#zonetxt");
        $zonetxt.empty();
        val_codepays = $("#pays").val();
        if (val_codepays != '') {
            $.ajax({
                url: "https://intraservice.olsy.ch/ajax/recupdep.php",
                data: "pays=" + val_codepays,
                type: "GET",
                success: function (data) {
                    var i;
                    var opt_val;
                    var options;
                    if (data != '') {
                        options = data.split(";");
                        for (i = 0; i < options.length; i++) {
                            opt_val = options[i].split("="); // value = text
                            $dep.append('');
                            $zone.append('');
                        }
                    }
                }
            });
        }
    });
}
// Récupération d'une liste d'agences couvrant un département sur un pays
function init_ajax_age_dep_pays() {
    $("#pays").change(function () {
        var val_codepays;
        var $age = $("#age");
        $age.empty();
        $age.append('');
        val_codepays = $("#pays").val();
        if (val_codepays != '') {
            $.ajax({
                url: "https://intraservice.olsy.ch/ajax/recupage.php",
                data: "pays=" + val_codepays,
                type: "GET",
                success: function (data) {
                    var i;
                    var opt_val;
                    var options;
                    if (data != '') {
                        options = data.split(";");
                        for (i = 0; i < options.length; i++) {
                            opt_val = options[i].split("="); // value = text
                            $age.append('');
                        }
                    }
                }
            });
        }
    });
}
// Récupération d'une liste d'agences couvrant un département sur un pays
function init_ajax_age_dep_pays2() {
    $("#dep").change(function () {
        var val_codeage;
        var val_codepays;
        var val_dep;
        var $prop = $("#prop");
        $prop.empty();
        val_dep = $("#dep").val();
        if (val_dep != '') {
            val_codepays = $("#pays").val();
            val_codeage = $("#ca").val();
            $.ajax({
                url: "https://intraservice.olsy.ch/ajax/recupage2.php",
                data: "paysdep=" + val_codepays + "_" + val_dep + "_" + val_codeage,
                type: "GET",
                success: function (data) {
                    var i;
                    var opt_val;
                    var options;
                    if (data != '') {
                        options = data.split(";");
                        for (i = 0; i < options.length; i++) {
                            opt_val = options[i].split("="); // value = text
                            $prop.append('');
                        }
                    }
                }
            });
        }
    });
}
// Récupération d'une liste de contrats de report
// et Récupération d'une liste de contrats de déduction
function init_ajax_cocl() {
    $("#codecocl").change(function () {
        var $idsite;
        var val_codeages;
        var val_codeage;
        var val_code;
        var val_idsite;
        var val_codecli;
        var to_affect;
        var val_codecocl;
        var val_coclded;
        var val_tded;
        var $lsttded;
        var $lsthnc = $("#lsthnc");
        var val_hnc = $lsthnc.val();
        $lsthnc.empty();
        $lsttded = $("#lsttded");
        val_tded = $lsttded.val();
        $lsttded.empty();
        val_codecocl = $("#codecocl").val();
        if (val_codecocl != '') {
            to_affect = false;
            val_coclded = $("#coclded").val();
            val_codecli = $("#codecli").val();
            $idsite = $("#idsite");
            val_idsite = parseInt($idsite.val());
            if ($idsite.val() == '') {
                val_idsite = -2; // =0
            }
            val_code = $("#code").val();
            val_codeage = $("#codeage").val();
            val_codeages = $("#agesite").val();
            $.ajax({
                url: "https://intraservice.olsy.ch/ajax/recupcocl_rep.php",
                data: "nco=" + val_codecocl + "_" + val_codecli + "_" + val_idsite + "_" + val_code,
                type: "GET",
                success: function (data) {
                    var i;
                    var opt_val;
                    var tab_options;
                    if (data != '') {
                        // alert(data);
                        tab_options = data.split(";");
                        to_affect = false;
                        for (i = 0; i < tab_options.length; i++) {
                            opt_val = tab_options[i].split("="); // value = text
                            if (opt_val[0] != '') {
                                if (opt_val[0] == val_tded) {
                                    to_affect = true;
                                }
                            }
                            $lsthnc.append('');
                        }
                        if (tab_options.length != 1) {
                            if (to_affect == true) {
                                $lsthnc.val(val_hnc);
                            }
                            else {
                                $lsthnc.val('');
                            }
                        }
                    }
                }
            });
            $.ajax({
                url: "https://intraservice.olsy.ch/ajax/recupcocl_ded.php",
                data: "nco=" + val_codecocl + "_" + val_codecli + "_" + val_idsite + "_" + val_code,
                type: "GET",
                success: function (data) {
                    var i;
                    var opt_val;
                    var tab_options;
                    if (data != '') {
                        // alert(data);
                        tab_options = data.split(";");
                        to_affect = false;
                        for (i = 0; i < tab_options.length; i++) {
                            opt_val = tab_options[i].split("="); // value = text
                            if (opt_val[0] != '') {
                                if (opt_val[0] == val_tded) {
                                    to_affect = true;
                                }
                            }
                            $lsttded.append('');
                        }
                        if (tab_options.length != 1) {
                            if (to_affect == true) {
                                $lsttded.val(val_tded);
                            }
                            else {
                                $lsttded.val('');
                                $lsttded.val(val_coclded);
                            }
                        }
                    }
                }
            });
            $.ajax({
                url: "https://intraservice.olsy.ch/ajax/recupcocl_site.php",
                data: "nco=" + val_codecocl + "_" + val_codecli + "_" + val_idsite + "_" + val_codeage + "_" + val_codeages,
                type: "GET",
                success: function (data) {
                    var i;
                    var opt_val;
                    var tab_options;
                    if (data != '') {
                        //alert(data);
                        tab_options = data.split(";");
                        to_affect = false;
                        for (i = 0; i < tab_options.length; i++) {
                            opt_val = tab_options[i].split("="); // value = text
                            switch (opt_val[0]) {
                                case 'chx' :
                                    if (parseInt(opt_val[1]) == 1) {
                                        init_fen_scli('tdtitsite', 'fscomp', 910, 650, 'int');
                                        set_on_cursor('tdtitsite');
                                        init_fen_scli('tddetsite', 'fscomp', 910, 650, 'int');
                                        set_on_cursor('tddetsite');
                                        $("#choixsiteoui").show();
                                        $("#choixsitenon").hide();
                                    }
                                    else {
                                        $("#tdtitsite").unbind();
                                        $("#tddetsite").unbind();
                                        $("#choixsiteoui").hide();
                                        $("#choixsitenon").show();
                                    }
                                    break;
                                case 'id' :
                                    $("#idsite").val(opt_val[1]);
                                    if ((parseInt(opt_val[1]) != val_idsite) && (val_idsite != -2)) {
                                        warn_txt('Ce choix ne couvre pas le site actuellement sélectionné');
                                    }
                                    break;
                                case 'nsite' :
                                    $("#nomsite").html(opt_val[1]);
                                    break;
                            }
                        }
                    }
                }
            });
        }
        if (val_codecocl != '') {
            $("#res").show();
            $("#report_cocl").show();
        }
        else {
            $("#res").hide();
            $("#report_cocl").hide();
        }
    });
}
// Récupération d'une liste de contrats
function init_ajax_cocl3() {
    $("#codecocl").change(function () {
        var val_codecocl;
        var $lsthnc = $("#lsthnc");
        var val_hnc = $lsthnc.val();
        $lsthnc.empty();
        val_codecocl = $("#codecocl").val();
        if (val_codecocl != '') {
            $.ajax({
                url: "https://intraservice.olsy.ch/ajax/recupcocl3.php",
                data: "nco=" + val_codecocl,
                type: "GET",
                success: function (data) {
                    var i;
                    var opt_val;
                    var options;
                    if (data != '') {
                        options = data.split(";");
                        for (i = 0; i < options.length; i++) {
                            opt_val = options[i].split("="); // value = text
                            $lsthnc.append('');
                        }
                        $lsthnc.val(val_hnc);
                    }
                }
            });
        }
    });
}
// Récupération d'une date d'envoi et assignation en minimum sur zone de choix de date
function init_ajax_cocl4() {
    $("#codecocl").change(function () {
        var $signataire;
        var val_codecocl;
        var $datesigf= $("#datesigf");
        $datesigf.val('');
        $signataire= $("#signataire");
        val_codecocl = $("#codecocl").val();
        if (val_codecocl != '') {
            $.ajax({
                url: "https://intraservice.olsy.ch/ajax/recupcocl_dteenv.php",
                data: "nco=" + val_codecocl,
                type: "GET",
                success: function (data) {
                    if (data != '') {
                        $datesigf.datepicker( "option", "minDate", data );
                        $datesigf.datepicker( "option", "disabled", false );
                        readok_id('datesigf-signataire');
                    }
                    else
                    {
                        $datesigf.datepicker( "option", "disabled", true );
                        $signataire.val('');
                        readonly_id('datesigf-signataire');
                    }
                }
            });
        }
        else
        {
            $datesigf.datepicker( "option", "disabled", true );
            $signataire.val('');
            readonly_id('datesigf-signataire');
        }
    });
}
// Récupération d'une date d'envoi et assignation en minimum sur zone de choix de date
function init_ajax_cocla4() {
    $("#codecocl").change(function () {
        var $signataire;
        var val_codecocl;
        var $datesigf= $("#datesigf");
        $datesigf.val('');
        $signataire= $("#signataire");
        val_codecocl = $("#codecocl").val();
        if (val_codecocl != '') {
            $.ajax({
                url: "https://intraservice.olsy.ch/ajax/recupcocla_dteenv.php",
                data: "ncoa=" + val_codecocl,
                type: "GET",
                success: function (data) {
                    if (data != '') {
                        alert(data);
                        $datesigf.datepicker( "option", "minDate", data );
                        $datesigf.datepicker( "option", "disabled", false );
                        readok_id('datesigf-signataire');
                    }
                    else
                    {
                        $datesigf.datepicker( "option", "disabled", true );
                        $signataire.val('');
                        readonly_id('datesigf-signataire');
                    }
                }
            });
        }
        else
        {
            $datesigf.datepicker( "option", "disabled", true );
            $signataire.val('');
            readonly_id('datesigf-signataire');
        }
    });
}
// Récupération d'une liste de techniciens sur une agence
function init_ajax_tec() {
    $("#codeage").change(function () {
        var val_agesite;
        var val_codeage;
        var $codetec = $("#codetec");
        var val_codetec = $codetec.val();
        $codetec.empty();
        $codetec.append('');
        val_codeage = $("#codeage").val();
        if (val_codeage != '') {
            val_agesite = $("#agesite").val();
            $.ajax({
                url: "https://intraservice.olsy.ch/ajax/recuptec.php",
                data: "ca=" + val_codeage + '_' + val_agesite,
                type: "GET",
                success: function (data) {
                    var i;
                    var opt_val;
                    var options;
                    if (data != '') {
                        options = data.split(";");
                        for (i = 0; i < options.length; i++) {
                            opt_val = options[i].split("="); // value = text
                            $codetec.append('');
                        }
                        $codetec.val(val_codetec);
                    }
                }
            });
        }
    });
}
// Récupération d'une liste de techniciens sur une agence gestionnaire de site
function init_ajax_tec_agesite() {
    $("#agesite").change(function () {
        var val_codeage;
        var val_agesite;
        var $codetec = $("#codetec");
        var val_codetec = $codetec.val();
        $codetec.empty();
        $codetec.append('');
        val_agesite = $("#agesite").val();
        if (val_agesite != '') {
            val_codeage = $("#codeage").val();
            $.ajax({
                url: "https://intraservice.olsy.ch/ajax/recuptec.php",
                data: "ca=" + val_codeage + '_' + val_agesite,
                type: "GET",
                success: function (data) {
                    var i;
                    var opt_val;
                    var options;
                    if (data != '') {
                        options = data.split(";");
                        for (i = 0; i < options.length; i++) {
                            opt_val = options[i].split("="); // value = text
                            $codetec.append('');
                        }
                        $codetec.val(val_codetec);
                    }
                }
            });
        }
    });
}
// Récupération d'une liste de techniciens sur une agence
function init_ajax_tec2() {
    $("#codeage").change(function () {
        var val_codeage;
        var $codetec = $("#codetec");
        var val_codetec = $codetec.val();
        $codetec.empty();
        $codetec.append('');
        val_codeage = $("#codeage").val();
        if (val_codeage != '') {
            $.ajax({
                url: "https://intraservice.olsy.ch/ajax/recuptec2.php",
                data: "ca=" + val_codeage,
                type: "GET",
                success: function (data) {
                    var i;
                    var opt_val;
                    var options;
                    if (data != '') {
                        options = data.split(";");
                        for (i = 0; i < options.length; i++) {
                            opt_val = options[i].split("="); // value = text
                            $codetec.append('');
                        }
                        $codetec.val(val_codetec);
                    }
                }
            });
        }
    });
}
// Récupération d'une liste de techniciens (mode tableau) sur une agence
function init_ajax_tec3() {
    $("#codeage").change(function () {
        var val_td;
        var val_agesite;
        var idx;
        var oldcheck;
        var val_codeage;
        var $tabtectselect = $("#tabtectselect");
        $tabtectselect.empty();
        val_codeage = $("#codeage").val();
        if (val_codeage != '') {
            // on sauve les valeurs cochées
            oldcheck = [];
            idx = 0;
            // On balaye l'id du tableau des techniciens possibles
            $("#tabtectselec").find("input[type=checkbox]:checked").each(function () {
                var val_id;
                var $id = ($(this).attr("id"));
                if ($id == 'tecaff') {
                    val_id = $(this).val();
                    if (val_id != '-1') {
                        oldcheck[idx] = val_id;
                        idx++;
                    }
                }
            });
            val_agesite = $("#agesite").val();
            val_td = $("#td").val();
            $.ajax({
                url: "https://intraservice.olsy.ch/ajax/recuptec3.php",
                data: "ca=" + val_codeage + '_' + val_agesite + '_' + val_td,
                type: "GET",
                success: function (data) {
                    if (data != '') {
                        $tabtectselect.append(data);
                        // On remet les valeurs (si longueur oldcheck >0, c'est qu'il y avait des techniciens choisis)
                        if (oldcheck.length > 0) {
                            // On balaye l'id du tableau des techniciens possibles
                            $("#tabtectselect").find("input[type=checkbox]:checked").each(function () {
                                var val_id;
                                var j;
                                var $id = ($(this).attr("id"));
                                if ($id == 'tecaff') {
                                    val_id = $(this).val();
                                    if (val_id != '-1') {
                                        for (j = 0; j < oldcheck.length; j++) {
                                            if (oldcheck[j] == val_id) {
                                                $(this).attr('checked', true);
                                            }
                                        }
                                    }
                                    else {
                                        $(this).attr('checked', true);
                                    }
                                }
                            });
                        }
                        else {
                            // On balaye l'id du tableau des techniciens possibles
                            $("#tabtectselect").find("input[type=checkbox]:checked").each(function () {
                                var val_id;
                                var $id = ($(this).attr("id"));
                                if ($id == 'tecaff') {
                                    val_id = $(this).val();
                                    if (val_id == '-1') {
                                        $(this).attr('checked', true);
                                    }
                                }
                            });
                        }
                    }
                }
            });
        }
    });
}
// Récupération d'une liste de techniciens (mode tableau) sur une agence
function init_ajax_tec3_agesite() {
    $("#agesite").change(function () {
        var val_td;
        var val_codeage;
        var idx;
        var oldcheck;
        var val_agesite;
        var $tabtectselect = $("#tabtectselect");
        $tabtectselect.empty();
        val_agesite = $("#agesite").val();
        if (val_agesite != '') {
            // on sauve les valeurs cochées
            oldcheck = [];
            idx = 0;
            // On balaye l'id du tableau des techniciens possibles
            $tabtectselect.find("input[type=checkbox]:checked").each(function () {
                var val_id;
                var $id = ($(this).attr("id"));
                if ($id == 'tecaff') {
                    val_id = $(this).val();
                    if (val_id != '-1') {
                        oldcheck[idx] = val_id;
                        idx++;
                    }
                }
            });
            val_codeage = $("#codeage").val();
            val_td = $("#td").val();
            $.ajax({
                url: "https://intraservice.olsy.ch/ajax/recuptec3.php",
                data: "ca=" + val_codeage + '_' + val_agesite + '_' + val_td,
                type: "GET",
                success: function (data) {
                    if (data != '') {
                        $tabtectselect.append(data);
                        // On remet les valeurs (si longueur oldcheck >0, c'est qu'il y avait des techniciens choisis)
                        if (oldcheck.length > 0) {
                            // On balaye l'id du tableau des techniciens possibles
                            $tabtectselect.find("input[type=checkbox]:checked").each(function () {
                                var j;
                                var val_id;
                                var $id = ($(this).attr("id"));
                                if ($id == 'tecaff') {
                                    val_id = $(this).val();
                                    if (val_id != '-1') {
                                        for (j = 0; j < oldcheck.length; j++) {
                                            if (oldcheck[j] == val_id) {
                                                $(this).attr('checked', true);
                                            }
                                        }
                                    }
                                    else {
                                        $(this).attr('checked', true);
                                    }
                                }
                            });
                        }
                        else {
                            // On balaye l'id du tableau des techniciens possibles
                            $tabtectselect.find("input[type=checkbox]:checked").each(function () {
                                var val_id;
                                var $id = ($(this).attr("id"));
                                if ($id == 'tecaff') {
                                    val_id = $(this).val();
                                    if (val_id == '-1') {
                                        $(this).attr('checked', true);
                                    }
                                }
                            });
                        }
                    }
                }
            });
        }
    });
}
// Récupération d'une liste d'interventions
function init_ajax_int() {
    $("#codeage").change(function () {
        var val_codeage;
        var $codeint = $("#codeint");
        $codeint.empty();
        $codeint.append('');
        $codeint.append('');
        val_codeage = $("#codeage").val();
        if (val_codeage != '') {
            $.ajax({
                url: "https://intraservice.olsy.ch/ajax/recupint.php",
                data: "age=" + val_codeage,
                type: "GET",
                success: function (data) {
                    var i;
                    var opt_val;
                    var options;
                    if (data != '') {
                        options = data.split(";");
                        for (i = 0; i < options.length; i++) {
                            opt_val = options[i].split("="); // value = text
                            $codeint.append('');
                        }
                    }
                }
            });
        }
    });
}
// Récupération d'une liste de types licences en fonction code marque
function init_ajax_types() {
    $("#codema").change(function () {
        var val_codema;
        var $codetl = $("#codetl");
        var val_codetl = $codetl.val();
        $codetl.empty();
        $codetl.append('');
        val_codema = $("#codema").val();
        if (val_codema != '') {
            $.ajax({
                url: "https://intraservice.olsy.ch/ajax/recuptypes.php",
                data: "mque=" + val_codema,
                type: "GET",
                success: function (data) {
                    var i;
                    var opt_val;
                    var options;
                    if (data != '') {
                        options = data.split(";");
                        for (i = 0; i < options.length; i++) {
                            opt_val = options[i].split("="); // value = text
                            $codetl.append('');
                        }
                        $codetl.val(val_codetl);
                    }
                }
            });
        }
    });
}
// Récupération d'une liste de clients
function init_ajax_cli() {
    $("#rech").keyup(function () {
        var lst_chx;
        var val_client;
        var $lstcli;
        var val_rech = $("#rech").val();
        var $lstchx = $("#lstchx");
        if (val_rech.length > 2) {
            $lstcli = $("#lstcli");
            $lstcli.empty();
            val_client = $("#cc").val();
            lst_chx = val_client;
            if ($lstchx.length > 0) {
                $lstchx.find("option").each(function () {
                    if (this.value != '') {
                        lst_chx = lst_chx + '-' + this.value;
                    }
                });
            }
            lst_chx = encodeURIComponent(lst_chx);
            $.ajax({
                url: "https://intraservice.olsy.ch/ajax/recupcli.php",
                data: "txt=" + val_rech + '_' + lst_chx,
                type: "GET",
                success: function (data) {
                    var i;
                    var opt_val;
                    var options;
                    if (data != '') {
                        options = data.split(";");
                        for (i = 0; i < options.length; i++) {
                            opt_val = options[i].split("="); // value = text
                            $lstcli.append('');
                        }
                    }
                }
            });
        }
    });
}
// Enregistrement d'un log sur envoi de mail interne
function init_ajax_mail_int() {
    $("#valmailint").click(function () {
        var val_code = $("#code").val();
        /*
         var val_maildest = $("#maildest").val();
         var val_mailcc = $("#mailcc").val();
         var val_mailbcc = $("#mailbcc").val();
         $.ajax({
         url: "
        https://intraservice.olsy.ch/ajax/sendmail.php",
         data: "prm="+'1&'+val_code+'&'+val_maildest+'&'+val_mailcc+'&'+val_mailbcc,
         type: "GET",
         success: function() {
         */
                init_new_window('mailint_maj.php?ci=' + val_code, 'mailint', 800, 700, 'yes');
        /*fensaisie('
        mailint_maj.php?ci='+val_code,800,700,'status=no,scrollbars=yes,menubar=no,statusbar=no,location=no,locationbar=no');*/
        /*
         }
         });
         */
    });
}
// Enregistrement d'un log sur envoi de mail externe
function init_ajax_mail_ext() {
    $("#valmailext").click(function () {
        var dialog;
        var val_maildest = $("#maildest").val();
        var val_mailcc = $("#mailcc").val();
        var val_mailbcc = $("#mailbcc").val();
        var msg = 'Vous allez envoyer un mail par votre client de messagerie habituel';
        dialog = new Messi(msg,
            {
                title: 'Confirmer',
                buttons: [{id: 0, label: 'Confirmer', val: 'Y'},
                    {id: 1, label: 'Annuler', val: 'N'}],
                callback: function (val) {
                    if (val == 'Y') {
                        window.location.href = 'mailto:' + val_maildest + '?cc=' + val_mailcc + '&bcc=' + val_mailbcc;
                    }
                    else {
                        return false;
                    }
                }
            });
    });
}
// Récupération d'une liste de contacts de marque
function init_ajax_cmque() {
    $("#mque").change(function () {
        var val_mque;
        var $cmque = $("#cmque");
        $cmque.empty();
        val_mque = $("#mque").val();
        if (val_mque != '') {
            $.ajax({
                url: "https://intraservice.olsy.ch/ajax/recupcmque.php",
                data: "mque=" + val_mque,
                type: "GET",
                success: function (data) {
                    var i;
                    var opt_val;
                    var options;
                    if (data != '') {
                        options = data.split(";");
                        for (i = 0; i < options.length; i++) {
                            opt_val = options[i].split("="); // value = text
                            $cmque.append('');
                        }
                        chk_addctact('mque', 'addcmque');
                        /*verif_lnk_addctact('0','mque','addcmque');*/
                    }
                }
            });
        }
    });
}
// Récupération d'une liste de contacts de donneur d'ordres
function init_ajax_cdo() {
    $("#do").change(function () {
        var val_do;
        var $cdo = $("#cdo");
        $cdo.empty();
        val_do = $("#do").val();
        if (val_do != '') {
            $.ajax({
                url: "https://intraservice.olsy.ch/ajax/recupcdo.php",
                data: "do=" + val_do,
                type: "GET",
                success: function (data) {
                    var i;
                    var opt_val;
                    var options;
                    if (data != '') {
                        options = data.split(";");
                        for (i = 0; i < options.length; i++) {
                            opt_val = options[i].split("="); // value = text
                            $cdo.append('');
                        }
                        chk_addctact('do', 'addcdo');
                        /*verif_lnk_addctact('0','do','addcdo');*/
                    }
                }
            });
        }
    });
}
// Récupération d'une liste de contacts de sous-traitants
function init_ajax_cst() {
    $("#st").change(function () {
        var val_st;
        var $cst = $("#cst");
        $cst.empty();
        val_st = $("#st").val();
        if (val_st != '') {
            $.ajax({
                url: "https://intraservice.olsy.ch/ajax/recupcst.php",
                data: "st=" + val_st,
                type: "GET",
                success: function (data) {
                    var i;
                    var opt_val;
                    var options;
                    if (data != '') {
                        options = data.split(";");
                        for (i = 0; i < options.length; i++) {
                            opt_val = options[i].split("="); // value = text
                            $cst.append('');
                        }
                        chk_addctact('st', 'addcst');
                        /*verif_lnk_addctact('0','st','addcst');*/
                    }
                }
            });
        }
    });
}
function UpdlstTrat() {
    var affok;
    var i;
    var $rat;
    var val_typdest = $("#typdest");
    var val_dest = $("#dest");
    // RAZ type document rattaché
    var $typrat = $("#typrat");
    $typrat.empty();
    // RAZ document rattaché
    $rat = $("#rat");
    $rat.empty();
    $rat.append('');
    // Construction de la liste des types de documents rattachables
    // On crée la première ligne de choix
    $typrat.append('');
    if ((val_typdest.length > 0) && (val_dest.length > 0)) {
        // On crée les lignes suivantes
        for (i = -1; i < tab_trat.length; i++) {
            if (isNaN(i) == false) {
                if (tab_trat[i]) {
                    affok = true;
                    switch (parseInt(val_dest)) {
                        case 3 :
                        case 4 :
                            break;
                        case 10 :
                        case 11 :
                            if ((parseInt(i) != 2) && (parseInt(i) != -1)) {
                                affok = false;
                            }
                            break;
                        case 5 :
                        case 9 :
                            switch (parseInt(val_typdest)) {
                                case 3 :
                                case 4 :
                                    if ((parseInt(i) != 2) && (parseInt(i) != -1)) {
                                        affok = false;
                                    }
                                    break;
                                default :
                                    break;
                            }
                            break;
                    }
                    if (affok == true) {
                        $typrat.append('');
                    }
                }
            }
        }
    }
}
// Récupération d'une liste de destinataires
function init_ajax_tdest(updtrat) {
    $("#typdest").change(function () {
        var val_rat;
        var val_typrat;
        var val_typdest;
        var $dest = $("#dest");
        $dest.empty();
        $dest.append('');
        val_typdest = $("#typdest").val();
        val_typrat = $("#typrat").val();
        val_rat = $("#rat").val();
        if (val_typdest != '') {
            $.ajax({
                url: "https://intraservice.olsy.ch/ajax/recuptdest.php",
                data: "tdest=" + val_typdest + "_" + val_typrat + "_" + val_rat,
                type: "GET",
                success: function (data) {
                    var i;
                    var opt_val;
                    var options;
                    if (data != '') {
                        options = data.split(";");
                        for (i = 0; i < options.length; i++) {
                            opt_val = options[i].split("="); // value = text
                            $dest.append('');
                        }
                        if (parseInt(updtrat) == 1) {
                            UpdlstTrat();
                        }
                    }
                }
            });
        }
    });
}
// Récupération d'une liste de destinataires (Planning)
function init_ajax_tdest2(typ, code, updtrat) {
    $("#typdest").change(function () {
        var val_code;
        var val_typ;
        var val_rat;
        var val_typdest;
        var $dest = $("#dest");
        $dest.empty();
        $dest.append('');
        val_typdest = $("#typdest").val();
        if (val_typdest != '') {
            val_rat = $("#rat").val();
            val_typ = typ;
            val_code = code;
            $.ajax({
                url: "https://intraservice.olsy.ch/ajax/recuptdest.php",
                data: "tdest=" + val_typdest + '_' + val_rat + '_' + val_typ + '_' + val_code,
                type: "GET",
                success: function (data) {
                    var i;
                    var opt_val;
                    var options;
                    if (data != '') {
                        alert(data);
                        options = data.split(";");
                        for (i = 0; i < options.length; i++) {
                            opt_val = options[i].split("="); // value = text
                            $dest.append('');
                        }
                        if (parseInt(updtrat) == 1) {
                            UpdlstTrat();
                        }
                    }
                }
            });
        }
    });
}
// Récupération d'une liste de types de rattachement et rattachements en fonction destinataire choisi
function init_ajax_dest(updtrat) {
    $("#dest").change(function () {
        if (parseInt(updtrat) == 1) {
            UpdlstTrat();
        }
    });
}
// MAJ liste des codes rattachés en fonction type rattachement choisi
function init_ajax_trat() {
    $("#typrat").change(function () {
        var val_typrat;
        var $rat = $("#rat");
        $rat.empty();
        $rat.append('');
        val_typrat = $("#typrat").val();
        if (val_typrat != '') {
            $.ajax({
                url: "https://intraservice.olsy.ch/ajax/recuptrat.php",
                data: "trat=" + val_typrat,
                type: "GET",
                success: function (data) {
                    var i;
                    var opt_val;
                    var options;
                    if (data != '') {
                        options = data.split(";");
                        for (i = 0; i < options.length; i++) {
                            opt_val = options[i].split("="); // value = text
                            $rat.append('');
                        }
                    }
                }
            });
        }
    });
}
// MAJ liste des codes rattachés en fonction type rattachement choisi (Planning)
function init_ajax_trat2() {
    $("#typrat").change(function () {
        var val_dest;
        var val_typdest;
        var val_typrat;
        var $rat = $("#rat");
        var $rdv = $("#rdv");
        $rdv.empty();
        $rdv.append('');
        clear_form_elements('detrdv');
        $("#lnkplan").hide();
        $rat.empty();
        $rat.append('');
        val_typrat = $("#typrat").val();
        if (val_typrat != '') {
            val_typdest = $("#typdest").val();
            val_dest = $("#dest").val();
            $.ajax({
                url: "https://intraservice.olsy.ch/ajax/recuptrat2.php",
                data: "trat=" + val_typrat + "_" + val_typdest + "_" + val_dest,
                type: "GET",
                success: function (data) {
                    var i;
                    var opt_val;
                    var options;
                    if (data != '') {
                        options = data.split(";");
                        for (i = 0; i < options.length; i++) {
                            opt_val = options[i].split("="); // value = text
                            $rat.append('');
                        }
                    }
                }
            });
        }
        chk_disp_lnkplan();
        chk_ida();
    });
}
// MAJ liste des rdvs possibles en fonction code rattachement choisi
// MAJ du titre en fonction code rattachement choisi
// MAJ du détail en fonction code rattachement choisi
function init_ajax_rdv_tit_detail() {
    $("#rat").change(function () {
        var val_typrat;
        var val_rat;
        var $detail;
        var $tit;
        var $rdv;
        clear_form_elements('detrdv');
        $rdv = $("#rdv");
        $rdv.empty();
        $rdv.append('');
        $tit = $("#tit");
        $tit.empty();
        $detail = $("#detail");
        $detail.empty();
        val_rat = $("#rat").val();
        if (val_rat != '') {
            val_typrat = $("#typrat").val();
            $.ajax({
                url: "https://intraservice.olsy.ch/ajax/recuprdv.php",
                data: "rat=" + val_typrat + "_" + val_rat,
                type: "GET",
                success: function (data) {
                    var i;
                    var opt_val;
                    var options;
                    if (data != '') {
                        options = data.split(";");
                        for (i = 0; i < options.length; i++) {
                            opt_val = options[i].split("="); // value = text
                            $rdv.append('');
                        }
                    }
                }
            });
            $.ajax({
                url: "https://intraservice.olsy.ch/ajax/recupplantitre2.php",
                data: "rat=" + val_typrat + "_" + val_rat,
                type: "GET",
                success: function (data) {
                    if (data != '') {
                        $tit.val(data);
                    }
                }
            });
            $.ajax({
                url: "https://intraservice.olsy.ch/ajax/recupplandetail2.php",
                data: "rat=" + val_typrat + "_" + val_rat,
                type: "GET",
                success: function (data) {
                    if (data != '') {
                        $detail.val(data);
                    }
                }
            });
        }
        chk_disp_lnkplan();
        chk_ida();
    });
}
// MAJ liste des renseignements complémentaires en fonction RDV choisi (Edition évènement)
function init_ajax_rens() {
    $("#rdv").change(function () {
        var dialog;
        var msg;
        var $urgence = $("#urgence");
        var $date = $("#date");
        var $heure = $("#heure");
        var val_rdv = $("#rdv").val();
        var $dur = $("#dur");
        var $codelieu = $("#codelieu");
        if (val_rdv != '') {
            msg = "Les paramètres de date, d\'heure de rendez-vous
et de durée vont être changés
Merci de confirmer";
            dialog = new Messi(msg,
                {
                    title: 'Confirmer',
                    buttons: [{id: 0, label: 'Confirmer', val: 'Y'},
                        {id: 1, label: 'Annuler', val: 'N'}],
                    callback: function (val) {
                        if (val == 'Y') {
                            $urgence.val('');
                            $date.val('');
                            $heure.val('');
                            $dur.val('');
                            $codelieu.val('');
                            $.ajax({
                                url: "https://intraservice.olsy.ch/ajax/recupplanurg.php",
                                data: "rdv=" + val_rdv,
                                type: "GET",
                                success: function (data) {
                                    if (data != '') {
                                        $urgence.val(data);
                                    }
                                }
                            });
                            $.ajax({
                                url: "https://intraservice.olsy.ch/ajax/recupplandte.php",
                                data: "rdv=" + val_rdv,
                                type: "GET",
                                success: function (data) {
                                    if (data != '') {
                                        $date.val(data);
                                    }
                                }
                            });
                            $.ajax({
                                url: "https://intraservice.olsy.ch/ajax/recupplanheure.php",
                                data: "rdv=" + val_rdv,
                                type: "GET",
                                success: function (data) {
                                    if (data != '') {
                                        $heure.val(data);
                                    }
                                }
                            });
                            $.ajax({
                                url: "https://intraservice.olsy.ch/ajax/recupplandur.php",
                                data: "rdv=" + val_rdv,
                                type: "GET",
                                success: function (data) {
                                    if (data != '') {
                                        $dur.val(data);
                                    }
                                }
                            });
                            $.ajax({
                                url: "https://intraservice.olsy.ch/ajax/recupplanlieu.php",
                                data: "rdv=" + val_rdv,
                                type: "GET",
                                success: function (data) {
                                    if (data != '') {
                                        $codelieu.val(data);
                                    }
                                }
                            });
                        }
                    }
                });
        }
        else {
            $urgence.val('');
            $date.val('');
            $heure.val('');
            $dur.val('');
            $codelieu.val('');
            chk_ida();
        }
        chk_disp_lnkplan();
    });
}
// MAJ liste des renseignements complémentaires en fonction RDV choisi (détail horaire)
function init_ajax_rens2() {
    $("#rdv").change(function () {
        var $heured;
        var $heurea;
        var $codelieu;
        var $date;
        var val_rdv = $("#rdv").val();
        if (val_rdv != '') {
            $date = $("#date");
            $date.val('');
            $codelieu = $("#codelieu");
            $codelieu.val('');
            $heurea = $("#heurea");
            $heurea.val('');
            $heured = $("#heured");
            $heured.val('');
            $.ajax({
                url: "https://intraservice.olsy.ch/ajax/recupplandte.php",
                data: "rdv=" + val_rdv,
                type: "GET",
                success: function (data) {
                    if (data != '') {
                        $date.val(data);
                    }
                }
            });
            $.ajax({
                url: "https://intraservice.olsy.ch/ajax/recupplanlieu.php",
                data: "rdv=" + val_rdv,
                type: "GET",
                success: function (data) {
                    if (data != '') {
                        $codelieu.val(data);
// On contrôle s'il faut afficher le détail de déplacement ou pas
                        chk_codelieu('codelieu', 'onglet_detdepl', 'detdepl');
                    }
                }
            });
            $.ajax({
                url: "https://intraservice.olsy.ch/ajax/recupplanheure.php",
                data: "rdv=" + val_rdv,
                type: "GET",
                success: function (data) {
                    if (data != '') {
                        $heurea.val(data);
                    }
                }
            });
            $.ajax({
                url: "https://intraservice.olsy.ch/ajax/recupplanheuref.php",
                data: "rdv=" + val_rdv,
                type: "GET",
                success: function (data) {
                    if (data != '') {
                        $heured.val(data);
                    }
                }
            });
        }
        else {
            $("#date").val('');
            $("#codelieu").val('');
            $("#heurea").val('');
            $("#heured").val('');
            $("#a_mt").val('');
            $("#a_heured").val('');
            $("#a_heuref").val('');
            $("#a_km").val('');
            $("#a_cd").val('');
            $("#a_fa").val('');
            $("#a_fad").val('');
            $("#r_mt").val('');
            $("#r_heured").val('');
            $("#r_heuref").val('');
            $("#r_km").val('');
            $("#r_cd").val('');
            $("#r_fa").val('');
            $("#r_fad").val('');
            $("#det_depl").hide();
        }
    });
}
// Récupération d'une liste de sous-traitants en fonction d'un code spécialité
function init_ajax_st() {
    $("#codesp").change(function () {
        var $nivsp;
        var $codesp;
        var txtnivsp;
        var toutes;
        var txtsp;
        var txtspfinal;
        var val_depcli;
        var val_payscli;
        var val_codesp;
        var $codest = $("#codest");
        var val_codest = $codest.val();
        $codest.empty();
        $codest.append('');
        $codesp = $('#codesp');
        val_codesp = $codesp.val();
        if (val_codesp != '') {
            val_payscli = $("#payscli").val();
            val_depcli = $("#depcli").val();
            txtspfinal = '';
            // Récupération des codes spécialité sélectionnés
            if ($codesp.length > 0) {
                txtsp = '';
                toutes = false;
                $codesp.find("option:selected").each(function () {
                    if ($(this).val() == '*') {
                        toutes = true;
                    }
                    txtsp += $(this).val() + "_";
                });
                if (toutes == true) {
                    txtsp = '*_';
                }
                txtspfinal = txtsp.substring(0, txtsp.length - 1);
            }
            // Récupération des niveaux sélectionnés
            $nivsp = $("#nivsp");
            if ($nivsp.length > 0) {
                txtnivsp = '';
                toutes = false;
                $nivsp.find("option:selected").each(function () {
                    if ($(this).val() == '*') {
                        toutes = true;
                    }
                    txtnivsp += $(this).val() + "_";
                });
                if (toutes == true) {
                    txtnivsp = '*_';
                }
                txtspfinal += '&nsp=' + txtnivsp.substring(0, txtnivsp.length - 1);
            }
            if ((txtsp.length > 0) && (txtnivsp.length > 0)) {
                $.ajax({
                    url: "https://intraservice.olsy.ch/ajax/recupst.php",
                    data: "sp=" + val_payscli + "_" + val_depcli + "_" + txtspfinal,
                    type: "GET",
                    success: function (data) {
                        var i;
                        var opt_val;
                        var options;
                        if (data != '') {
                            options = data.split(";");
                            for (i = 0; i < options.length; i++) {
                                opt_val = options[i].split("="); // value = text
                                $codest.append('');
                            }
                            $codest.val(val_codest);
                        }
                    }
                });
            }
        }
    });
}
// Récupération d'une liste de sous-traitants en fonction d'un niveau spécialité
function init_ajax_st2() {
    $("#nivsp").change(function () {
        var $nivsp;
        var $codesp;
        var txtnivsp;
        var toutes;
        var txtsp;
        var txtspfinal;
        var val_depcli;
        var val_payscli;
        var val_nivsp;
        var $codest = $("#codest");
        var val_codest = $codest.val();
        $codest.empty();
        $codest.append('');
        $codesp = $('#codesp');
        val_nivsp = $codesp.val();
        if (val_nivsp != '') {
            val_payscli = $("#payscli").val();
            val_depcli = $("#depcli").val();
            txtspfinal = '';
            // Récupération des codes spécialité sélectionnés
            if ($codesp.length > 0) {
                txtsp = '';
                toutes = false;
                $codesp.find("option:selected").each(function () {
                    if ($(this).val() == '*') {
                        toutes = true;
                    }
                    txtsp += $(this).val() + "_";
                });
                if (toutes == true) {
                    txtsp = '*_';
                }
                txtspfinal = txtsp.substring(0, txtsp.length - 1);
            }
            // Récupération des niveaux sélectionnés
            $nivsp = $("#nivsp");
            if ($nivsp.length > 0) {
                txtnivsp = '';
                toutes = false;
                $nivsp.find("option:selected").each(function () {
                    if ($(this).val() == '*') {
                        toutes = true;
                    }
                    txtnivsp += $(this).val() + "_";
                });
                if (toutes == true) {
                    txtnivsp = '*_';
                }
                txtspfinal += '&nsp=' + txtnivsp.substring(0, txtnivsp.length - 1);
            }
            if ((txtsp.length > 0) && (txtnivsp.length > 0)) {
                $.ajax({
                    url: "https://intraservice.olsy.ch/ajax/recupst.php",
                    data: "sp=" + val_payscli + "_" + val_depcli + "_" + txtspfinal,
                    type: "GET",
                    success: function (data) {
                        var i;
                        var opt_val;
                        var options;
                        if (data != '') {
                            options = data.split(";");
                            for (i = 0; i < options.length; i++) {
                                opt_val = options[i].split("="); // value = text
                                $codest.append('');
                            }
                            $codest.val(val_codest);
                        }
                    }
                });
            }
        }
    });
}
// Récupération des compléments à saisir en fonction code type objet
function init_ajax_to() {
    $("#coderto").change(function () {
        var val_coderto = $("#coderto").val();
        if (val_coderto != '') {
            $.ajax({
                url: "https://intraservice.olsy.ch/ajax/recuprto.php",
                data: "to=" + val_coderto,
                type: "GET",
                success: function (data) {
                    var i;
                    var options;
                    if (data != '') {
                        options = data.split(";");
                        for (i = 0; i < options.length; i++) {
                            switch (i) {
// Comteneur : oui/non
                                case 0 :
                                    $("#tocont").val(options[i]);
                                    chk_aff_messcont('tocont');
                                    chk_aff_icocont('tocont');
                                    break;
// Composant : oui/non
                                case 1 :
                                    $("#tocomp").val(options[i]);
                                    chk_aff_messcomp('tocomp');
                                    break;
// Marque : oui/non
                                case 2 :
                                    $("#tomque").val(options[i]);
                                    chk_aff_mque('tomque');
                                    break;
// NS : oui/non
                                case 3 :
                                    $("#tons").val(options[i]);
                                    chk_aff_ns('tons');
                                    break;
// Mobile : oui/non
                                case 4 :
                                    $("#tomob").val(options[i]);
                                    chk_aff_icomobimmob('tomob');
                                    break;
// Assureur : oui/non
                                case 5 :
                                    $("#toassu").val(options[i]);
                                    chk_aff_icoassu('toassu');
                                    break;
                            }
                        }
                    }
                }
            });
        }
        else {
            $("#tocont").val('0');
            chk_aff_messcont('tocont');
            chk_aff_icocont('tocont', true);
            $("#tocomp").val('0');
            chk_aff_messcomp('tocomp');
            $("#tomque").val('0');
            chk_aff_mque('tomque');
            $("#tons").val('0');
            chk_aff_ns('tons');
            $("#tomob").val('0');
            chk_aff_icomobimmob('tomob', true);
            $("#toassu").val('0');
            chk_aff_icoassu('toassu');
        }
    });
}
// MAJ des objets en fonction type objet((Nomenclature objet de relevés de parcs)
function init_ajax_robj_objachoisir() {
    $("#objto").change(function () {
        var val_codeto;
        var val_id;
        var $objachoisir = $("#objachoisir");
        $objachoisir.empty();
        $objachoisir.append('');
        val_id = parseInt($("#objid").val());
        val_codeto = $("#objto").val();
        if ((val_id != 0) && (val_codeto != '')) {
            $.ajax({
                url: "https://intraservice.olsy.ch/ajax/recupobjac.php",
                data: "id=" + val_id + "&to=" + val_codeto,
                type: "GET",
                success: function (data) {
                    var i;
                    var opt_val;
                    var options;
                    if (data != '') {
                        options = data.split(";");
                        for (i = 0; i < options.length; i++) {
                            opt_val = options[i].split("="); // value = text
                            $objachoisir.append('');
                        }
                    }
                }
            });
        }
    });
}
// MAJ liste objets enfants sur nomenclatures (Nomenclature objet de relevés de parcs - clic sur "ajouter")
function init_ajax_robj_add_enfant() {
    $("#addobj").click(function () {
        var existe;
        var $idobjachoisir;
        $idobjachoisir = $('#objachoisir').find('option:selected').val();
        if (($idobjachoisir) && ($idobjachoisir != '')) {
            existe = false;
            $('#objenfants').find('option').each(function () {
                if (this.value == $idobjachoisir) {
                    existe = true;
                    return false;
                }
            });
            if (existe == false) {
                // Faire l'appel Ajax
                $.ajax({
                    url: "https://intraservice.olsy.ch/ajax/recupobjenfants.php",
                    data: "id=" + $idobjachoisir,
                    type: "GET",
                    success: function (data) {
                        var i;
                        var opt_val;
                        var options;
                        var $objenfants;
                        if (data != '') {
                            $objenfants = $('#objenfants');
                            $objenfants.empty();
                            $objenfants.append('');
                            options = data.split(";");
                            for (i = 0; i < options.length; i++) {
                                opt_val = options[i].split("="); // value = text
                                $objenfants.append('');
                            }
                        }
                    }
                });
            }
            else {
                warn_txt('Déjà choisi');
            }
        }
        else {
            warn_txt('Impossible d\'ajouter');
        }
    });
}
// MAJ liste objets enfants sur nomenclatures (Nomenclature objet de relevés de parcs - clic sur "supprimer")
function init_ajax_robj_del_enfant() {
    $("#delobj").click(function () {
        var $objenfants = $('#objenfants');
        var $objenfantslng = $objenfants.find("option").length;
        var $objenfantssel = $objenfants.find('option:selected').val();
        if ($objenfantslng > 1) {
            if (($objenfantssel) && ($objenfantssel != '')) {
                // Faire l'appel Ajax
                $.ajax({
                    url: "https://intraservice.olsy.ch/ajax/recupobjenfants2.php",
                    data: "id=" + $objenfantssel,
                    type: "GET",
                    success: function (data) {
                        $('#objenfants').find('option:selected').remove();
                    }
                });
            }
            else {
                warn_txt('Rien de choisi');
            }
        }
        else {
            warn_txt('Impossible de supprimer');
        }
    });
}
// MAJ d'un évènement sur Drag&Drop sur planning
function ajax_evt_update($idevt, $iddest) {
    var $tab_idfrom = $idevt.split('_');
    $.ajax({
        url: "https://intraservice.olsy.ch/ajax/planupd.php?",
        data: "id=" + $tab_idfrom[1] + '-' + $iddest,
        type: "GET",
        success: function (data) {
            var url_cour;
            if (data.trim() != '') {
                warn_txt(data);
            }
            url_cour = location.href;
            location.href = url_cour;
        }
    });
}
// MAJ d'un évènement sur redimensionnement sur planning
function ajax_evt_update2($idevt, $iddest) {
    var $tab_idfrom = $idevt.split('_');
    $.ajax({
        url: "https://intraservice.olsy.ch/ajax/planupd2.php?",
        data: "id=" + $tab_idfrom[1] + '-' + $iddest,
        type: "GET",
        success: function (data) {
            var url_cour;
            if (data.trim() != '') {
                warn_txt(data);
            }
            url_cour = location.href;
            location.href = url_cour;
        }
    });
}
// MAJ d'un évènement sur validation nouvelle heure sur planning
function ajax_evt_update3($idevt, $iddest) {
    var $url_cour;
    $.ajax({
        url: "https://intraservice.olsy.ch/ajax/planupd3.php?",
        data: "id=" + $idevt + '-' + $iddest,
        type: "GET",
        success: function (data) {
            // var url_cour;
            if (data.trim() != '') {
                warn_txt(data);
            }
            $url_cour = location.href;
            location.href = $url_cour;
        }
    });
}
// Contrôle de validité d'un événement (mode édition), en fonction des plages horaires d'un technicien
// Sur changement de date
function ajax_chk_evt_date($idevt) {
    var $val_dur;
    var $val_heure;
    var $val_dest;
    var $val_typd;
    var $val_date = $('#date').val();
    if ($val_date != '') {
        $val_typd = parseInt($('#typdest').val());
        if (($val_typd == 1) || ($val_typd == 3)) {
            $val_dest = $('#dest').val();
            if ($val_dest != '') {
                $val_heure = $('#heure').val();
                $val_dur = parseInt($('#dur').val());
                // Faire l'appel Ajax
                $.ajax({
                    url: "https://intraservice.olsy.ch/ajax/planchk.php",
                    data: "id=" + $idevt + '_' + $val_typd + '_' + $val_dest + '_' + $val_date + '_' + $val_heure + '_' + $val_dur,
                    type: "GET",
                    success: function (data) {
                        var $enr = $('#enr');
                        if (data.trim() != '') {
                            warn_txt(data);
                            $enr.css("backgroundColor", "#66CC99");
                            $enr.attr("disabled", "disabled");
                        }
                        else {
                            $enr.removeAttr("disabled");
                            $enr.css("backgroundColor", "#666666");
                        }
                    }
                });
            }
        }
    }
}
// Contrôle de validité d'un événement (mode édition), en fonction des plages horaires d'un technicien
// Sur changement d'heure
function init_ajax_chk_evt_heure($idevt) {
    $("#heure").change(function () {
        var $val_dur;
        var $val_date;
        var $val_dest;
        var $val_typd;
        var $val_heure = $('#heure').val();
        if ($val_heure != '') {
            $val_typd = parseInt($('#typdest').val());
            if (($val_typd == 1) || ($val_typd == 3)) {
                $val_dest = $('#dest').val();
                if ($val_dest != '') {
                    $val_date = $('#date').val();
                    $val_dur = parseInt($('#dur').val());
// Faire l'appel Ajax
                    $.ajax({
                        url: "https://intraservice.olsy.ch/ajax/planchk.php",
                        data: "id=" + $idevt + '_' + $val_typd + '_' + $val_dest + '_' + $val_date + '_' + $val_heure + '_' + $val_dur,
                        type: "GET",
                        success: function (data) {
                            var $enr = $('#enr');
                            if (data.trim() != '') {
                                warn_txt(data);
                                $('#heure').val('');
                                $enr.css("backgroundColor", "#66CC99");
                                $enr.attr("disabled", "disabled");
                            }
                            else {
                                $enr.removeAttr("disabled");
                                $enr.css("backgroundColor", "#666666");
                            }
                        }
                    });
                }
            }
        }
    });
}
// Contrôle de validité d'un événement (mode édition), en fonction des plages horaires d'un technicien
// Sur changement de durée
function init_ajax_chk_evt_duree($idevt) {
    $("#dur").change(function () {
        var $val_heure;
        var $val_date;
        var $val_dest;
        var $val_typd;
        var $val_dur = $('#dur').val();
        if ($val_dur != '') {
            $val_typd = parseInt($('#typdest').val());
            if (($val_typd == 1) || ($val_typd == 3)) {
                $val_dest = $('#dest').val();
                if ($val_dest != '') {
                    $val_date = $('#date').val();
                    $val_heure = $('#heure').val();
                    // Faire l'appel Ajax
                    $.ajax({
                        url: "https://intraservice.olsy.ch/ajax/planchk.php",
                        data: "id=" + $idevt + '_' + $val_typd + '_' + $val_dest + '_' + $val_date + '_' + $val_heure + '_' + $val_dur,
                        type: "GET",
                        success: function (data) {
                            var $enr = $('#enr');
                            if (data.trim() != '') {
                                warn_txt(data);
                                $val_dur.val('');
                                $enr.css("backgroundColor", "#66CC99");
                                $enr.attr("disabled", "disabled");
                            }
                            else {
                                $enr.removeAttr("disabled");
                                $enr.css("backgroundColor", "#666666");
                            }
                        }
                    });
                }
            }
        }
    });
}
// Pour ajout spécialité dans fiche sous-traitant
function init_ajax_st1(id, idtochange) {
    $('#' + id).click(function () {
        var val_sel = $('#spec').val();
        if (val_sel != '') {
            if (idtochange != '') {
                $('#' + idtochange).empty();
            }
// Faire l'appel Ajax
            $.ajax({
                url: "https://intraservice.olsy.ch/ajax/update1_session_st_sp.php",
                data: "sp=" + val_sel,
                type: "GET",
                success: function (data) {
                    if (data.trim() != '') {
                        if (idtochange != '') {
                            $('#' + idtochange).html(data);
                            $('#spec').val('');
                            init_autoajax_classe_st2('falselnkimgajax', 'tab_sp');
                            init_autoajax_classe_st3('falselnksel', 'tab_sp');
                        }
                    }
                }
            });
        }
    });
}
// Pour suppression spécialité dans fiche sous-traitant
function init_autoajax_classe_st2(classe, idtochange) {
    if (classe != '') {
        $('.' + classe).click(function () {
            var prm = $(this).attr('prmajax');
            if (prm != undefined) {
                if (idtochange != '') {
                    $('#' + idtochange).empty();
                }
// Faire l'appel Ajax
                $.ajax({
                    url: "https://intraservice.olsy.ch/ajax/update2_session_st_sp.php",
                    data: "sp=" + prm,
                    type: "GET",
                    success: function (data) {
                        if (data.trim() != '') {
                            if (idtochange != '') {
                                $('#' + idtochange).html(data);
                                init_autoajax_classe_st2('falselnkimgajax', 'tab_sp');
                                init_autoajax_classe_st3('falselnksel', 'tab_sp');
                            }
                        }
                    }
                });
            }
        });
    }
}
// Pour modification niveau spécialité dans fiche sous-traitant
function init_autoajax_classe_st3(classe, idtochange) {
    if (classe != '') {
        $('.' + classe).change(function () {
            var $val_id = $(this).attr('id');
            var tab_id = $val_id.split('_');
            var val_sel = $(this).val();
            if (idtochange != '') {
                $('#' + idtochange).empty();
            }
// Faire l'appel Ajax
            $.ajax({
                url: "https://intraservice.olsy.ch/ajax/update3_session_st_sp.php",
                data: "sp=" + tab_id[1] + '&niv=' + val_sel,
                type: "GET",
                success: function (data) {
                    if (data.trim() != '') {
                        if (idtochange != '') {
                            $('#' + idtochange).html(data);
                            init_autoajax_classe_st2('falselnkimgajax', 'tab_sp');
                            init_autoajax_classe_st3('falselnksel', 'tab_sp');
                        }
                    }
                }
            });
        });
    }
}