﻿$(document).ready(function () {
    $("#MRVSaisie").live("keypress", function (e) {
        //alert(e.which);
        if ($("#MRVSaisie").val() == "") {
            if (e.which == '32') {
                e.preventDefault();
            }
        }
    });
    // Bouton CREER une alerte
    /*$("#btnChercherPourMoi").click(function (e) {
        var href = $(this).attr("href");
        e.preventDefault();
        $.ajax({
            type: "POST",
            url: "/Default.aspx/GetUserACHId",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                if (parseInt(eval('(' + data.d + ')')) > -1) {
                    idUser = parseInt(eval('(' + data.d + ')'));
                    location.href = href;
                } else { location.href = "/Inscription/InscriptionLight.aspx?from=HPV"; }
            },
            error: function (data) { return data; }
        });
    });*/
    $("#VideSelec").live("click", function (e) {
        e.preventDefault();
        LaSelection = new Array();
        $("#MRVContVille").html("");
        FermePanneauSelection();
    });

    // Carte de France
    $('area').mouseover(function (e) {
        var idRegion = $(this).attr("idRegion");
        var fichierImage = GetImageFromId(idRegion);
        if (fichierImage == "") { fichierImage = "carte-vide.png"; }
        $("#carteFrance").attr("src", "/Images/" + fichierImage);
        $("#tableCarte a[idRegion='" + idRegion + "']").css("color", "#D8000C").css("text-decoration", "underline");
    }).mouseout(function () {
        $("#carteFrance").attr("src", "/Images/carte-vide.png");
        $("#tableCarte a").css("color", "#000").css("text-decoration", "none");
    });
    $("#tableCarte a").mouseover(function () {
        var idRegion = $(this).attr("idRegion");
        var fichierImage = GetImageFromId(idRegion);
        if (fichierImage == "") { fichierImage = "carte-vide.png"; }
        $("#carteFrance").attr("src", "/Images/" + fichierImage);
        $(this).css("color", "#D8000C").css("text-decoration", "underline");
    }).mouseout(function () {
        $("#carteFrance").attr("src", "/Images/carte-vide.png");
        $(this).css("color", "#000").css("text-decoration", "none");
    });
    // Rejette toute pression sur la touche entrée
    $('input').live("keypress", function (e) {
        var keycode = (e.keyCode ? e.keyCode : e.which);
        if (keycode == '13') {
            e.preventDefault();
        }
    });
    // Le contrôle Multi Select des types de bien
    $("#EPCMultiCheck").click(function () {
        $("#EPCMultiCheck img.EPCMCbtn").attr("src", "/Images/select_btn_click.png");
        $("#EPCMultiCheck div.EPCMCConteneur").fadeIn(200);
    }).live("mouseleave", function () {
        $("#EPCMultiCheck img.EPCMCbtn").attr("src", "/Images/select_btn.png");
        $("#EPCMultiCheck div.EPCMCConteneur").fadeOut(200);
    });
    $("#EPCMultiCheck div.EPCMCHeader").mouseover(function () {
        if ($(this).text() == "Sélection multiple") {
            var texte = "<strong><u>Votre sélection</u></strong><br />";
            for (var i = 0; i < LesTypesDeBien.length; i++) {
                texte += LesTypesDeBien[i].libelle;
                if (i != LesTypesDeBien.length - 1) { texte += "<br />"; }
            }
            EPCTooltip($(this), texte, "Info", null);
        }
    }).mouseout(function () { KillTooltips(); });
    $("#EPCMultiCheck input[type='checkbox']").change(function () {
        CheckTypesDeBiens();
    });
    // Le bouton qui lance la recherche
    $("#btnRechercher").click(function (e) {
        e.preventDefault();
        // Création de l'objet SearchParams
        var sp = new Object();
        // La rubrique (ici, vente)
        sp.rubrique = parseInt("4");
        // L'utilisateur
        if (idUser > -1) { sp.ach_id = idUser; }
        // Les types de bien
        var lstTbien = "";
        if (LesTypesDeBien.length > 0) {
            if (LesTypesDeBien.length == 1) { lstTbien = LesTypesDeBien[0].id; }
            else {
                for (var i = 0; i < LesTypesDeBien.length; i++) {
                    lstTbien += LesTypesDeBien[i].id;
                    if (i != LesTypesDeBien.length - 1) {
                        lstTbien += ",";
                    }
                }
            }
        }
        sp.lstTbien = lstTbien;
        if ($.trim($("#MRVRefAnnonce").val()) != "") {
            sp.reference = $.trim($("#MRVRefAnnonce").val());
        }
        if (Mode != modes.pays) {
            if (LaSelection.length == 0) {
                if (sp.reference == null) {
                    EPCTooltip($("#MRVSaisie"), "Merci de saisir au moins une ville, un département ou une région<br />avant d'effectuer une recherche", "Error", 5);
                    return false;
                }
            }
        }
        // Le pays
        sp.pays = "fra";
        if ($("#MRVPays").val().toLowerCase() != "fra") { sp.pays = $("#MRVPays").val().toLowerCase(); }
        // Le prix mini
        var prixMini = null;
        if (parseInt($.trim($("#MRVPrixMini").val())) > 0) { prixMini = parseInt($.trim($("#MRVPrixMini").val())); }
        sp.prix_min = prixMini;
        // Le prix maxi
        var prixMaxi = null;
        if (parseInt($.trim($("#MRVPrixMaxi").val())) > 0) { prixMaxi = parseInt($.trim($("#MRVPrixMaxi").val())); }
        sp.prix_max = prixMaxi;
        // Petit test sur les prix
        if (sp.prix_min > 0 && sp.prix_max > 0) {
            if (sp.prix_min > sp.prix_max) {
                EPCTooltip($("#MRVPrixMini"), "Merci de veiller à ce que le prix minimum<br />soit inférieur au prix maximum", "Error", 5);
                return false;
            }
        }
        // Le nombre de pièces
        var lstNbPieces = null;
        var firstlstNbPieces = true;
        $("input[id^='nbPieces']").each(function () {
            if ($(this).is(':checked')) {
                if (firstlstNbPieces) { lstNbPieces = ""; firstlstNbPieces = false; }
                lstNbPieces += $(this).val() + ",";
            }
        });
        if (lstNbPieces != null && lstNbPieces != "") { lstNbPieces = lstNbPieces.substring(0, lstNbPieces.length - 1) }
        sp.lstNbPieces = lstNbPieces;
        // La surface minimale
        var surfaceMini = null;
        if (parseInt($.trim($("#MRVSurfaceMini").val())) > 0) { surfaceMini = parseInt($.trim($("#MRVSurfaceMini").val())); }
        sp.surface_min = surfaceMini;
        // localisation Type
        var locType = 1;
        switch (Mode) {
            case modes.ville:
                locType = 1;
                break;
            case modes.departement:
                locType = 2;
                break;
            case modes.region:
                locType = 3;
                break;
            case modes.pays:
                locType = -1;
                break;
        }
        sp.localisationType = locType;
        // rayon
        sp.rayon = 0;
        if (Mode == modes.ville) { if (rayonInt > 0) { sp.rayon = rayonInt; } }
        // localisation_id_rayon
        sp.localisation_id_rayon = null;
        if (Mode == modes.ville) { if (rayonInt > 0) { sp.localisation_id_rayon = localisation_id_rayon; } }
        // liste des ids
        var listeIds = "";
        if (LaSelection.length > 0) {
            if (Mode == modes.region) {
                for (var x = 0; x < LaSelection.length; x++) {
                    var result = GetDepartementsByIdRegion(LaSelection[x].id);
                    if (result.length > 0)
                        listeIds = result.join(",");
                    /*for (var j = 0; j < result.length; j++) {
                        listeIds += result[j];
                        if (j != result.length - 1) {
                            listeIds += ",";
                        }
                    }*/
                }
                //listeIds = listeIds.substring(0, listeIds.length - 1);
            } else {
                for (var y = 0; y < LaSelection.length; y++) {
                    listeIds += LaSelection[y].id;
                    if (y != LaSelection.length - 1) {
                        listeIds += ",";
                    }
                }
            }
        }
        sp.lstLocalisationId = listeIds;
        // Avec photo ?
        sp.photos = 0;
        if ($("#MRVPhotoUniquement").is(':checked')) {
            sp.photos = 1;
        }
        // Valeurs du pager
        sp.pageNumber = 1;
        sp.order_by = 1;
        sp.sort_order = 1;
        sp.top = 25;
        // Initialisation des autres valeurs
        sp.ach_id = null;
        sp.lstNbChambres = null;
        $.ajax({
            type: "POST",
            url: "/Default.aspx/CreateSearchParams",
            data: "{'p_SearchParams':'" + JSON.stringify(sp) + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function () { location.href = $("#btnRechercher").attr("href"); }
        });
    });
    // Gestion de l'onglet Carte/Critère
    $("div .MRVongletFerme").live("click", function () {
        FermePanneauSelection();
        $(this).removeClass("MRVongletFerme");
        $("div .MRVongletOuvert").removeClass("MRVongletOuvert").addClass("MRVongletFerme");
        $(this).addClass("MRVongletOuvert");
        if ($.trim($("div .MRVongletOuvert").text()) == "Carte") {
            $("#MRVContenuCritere").fadeOut("fast");
            $("#MRVContenuCarte").fadeIn("fast");
            OuvrePanneauCarte();
        } else {
            $("#MRVContenuCarte").fadeOut("fast");
            $("#MRVContenuCritere").fadeIn("fast");
            if (PanneauCarteOuvert == true) { FermePanneauCarte(); }
            if (LaSelection.length > 0) { AffichePanneauSelectionRayon(); }
        }
    });
    // Le select avec les pays
    $("#MRVPays").change(function () {
        rayonInt = -1;
        $("#MRVSaisie").flushCache();
        $("#MRVSaisie").unautocomplete();
        $("#MRVCritereGauche input[type='radio']").removeAttr("disabled");
        $("#MRVRayon").removeAttr("disabled");
        if (LaSelection.length > 0 || PanneauOuvert) {
            FermePanneauSelection();
            LaSelection = new Array();
            $("#MRVContVille").html("");
        }
        if ($(this).val() != "fra") {
            Mode = modes.pays;
            $("#MRVSaisie").val("La recherche s'effectuera sur l'ensemble du pays").css("color", "#A1A1A1");
            $("#MRVSaisie").attr("disabled", true);
            $("#MRVCritereGauche input[type='radio']").attr("disabled", true);
            $("#MRVRayon").attr("disabled", true);
            $("#MRVCritereGauche span.MRVSpanRecherche").css("color", "#545353");
            $("#MRVCritereGauche span.MRVSpanRecherche").unbind("click");
        }
        else {
            $("#MRVCritereGauche span.MRVSpanRecherche").click(function () {
                $(this).prev().click();
            });
            $("#MRVSaisie").removeAttr("disabled");
            Mode = modes.departement;
            $("#MRVCbxVille").click();
        }
    });
    // L'input Surface Minimum
    $("#MRVSurfaceMini").focus(function () {
        if ($(this).val() == "en m²") {
            $(this).css("color", "#000").val("").select();
        }
    }).blur(function () {
        if ($.trim($(this).val()) == "") {
            $(this).val("en m²").css("color", "#A1A1A1");
        } else if (isNaN(parseInt($.trim($(this).val())))) {
            $(this).val("en m²").css("color", "#A1A1A1");
        } else { $(this).val(parseInt($.trim($(this).val()))); }
    }).keypress(function (e) {
        var val = parseInt(e.which);
        if ((val < 48 || val > 57) && (val != 8)) {
            e.preventDefault();
            EPCTooltip($(this), "Seuls des chiffres (0-9) sont acceptés", "Error", 2);
        }
    });
    // Les inputs Prix Minimum et Prix maximum
    $("#MRVPrixMini, #MRVPrixMaxi").focus(function () {
        if ($(this).val() == "en €") {
            $(this).css("color", "#000").val("").select();
        }
    }).blur(function () {
        if ($.trim($(this).val()) == "") {
            $(this).val("en €").css("color", "#A1A1A1");
        } else if (isNaN(parseInt($.trim($(this).val())))) {
            $(this).val("en €").css("color", "#A1A1A1");
        } else { $(this).val(parseInt($.trim($(this).val()))); }
    }).keypress(function (e) {
        var val = parseInt(e.which);
        if ((val < 48 || val > 57) && (val != 8)) {
            e.preventDefault();
            EPCTooltip($(this), "Seuls des chiffres (0-9) sont acceptés", "Error", 2);
        }
    });
    // Le select du rayon
    $("#MRVRayon").change(function (e) {
        if ($(this).val() != "0 km") {
            show_modal("modal_villes");
            $(this).val("0 km");
        }
    });
    // Les radiobuttons villes/depts/region
    $("#MRVCritereGauche input[type='radio']").click(function () {
        $(this).attr("checked", "checked");
        if (Mode != modes.pays) {
            $("#MRVCritereGauche span.MRVSpanRecherche").css("color", "#545353");
            $(this).next().css("color", "#E53722");
            $("#MRVRayon").attr("disabled", true);
            if ($(this).next().text() == "Ville(s)") {
                if (Mode != modes.ville) { if (LaSelection.length > 0 || PanneauOuvert) { FermePanneauSelection(); LaSelection = new Array(); $("#MRVContVille").html(""); } Mode = modes.ville; ChargeAutoComplete(); $("#MRVSaisie").val("Indiquer ville(s) ou code(s) postal(aux)").css("color", "#A1A1A1"); if (LaSelection.length == 1) { $('#MRVRayon').removeAttr("disabled"); } }
            } else if ($(this).next().text() == "Département(s)") {
                if (Mode != modes.departement) { if (LaSelection.length > 0 || PanneauOuvert) { FermePanneauSelection(); LaSelection = new Array(); $("#MRVContVille").html(""); } Mode = modes.departement; ChargeAutoComplete(); $("#MRVSaisie").val("Indiquer département(s) ou numéro(s)").css("color", "#A1A1A1"); }
            } else {
                if (Mode != modes.region) { if (LaSelection.length > 0 || PanneauOuvert) { FermePanneauSelection(); LaSelection = new Array(); $("#MRVContVille").html(""); } Mode = modes.region; ChargeAutoComplete(); $("#MRVSaisie").val("Indiquer région(s)").css("color", "#A1A1A1"); }
            }
        }
    });
    $("#MRVCritereGauche span.MRVSpanRecherche").click(function () {
        $(this).prev().click();
    });
    // L'image de suppression d'une ville
    // Dans le panneau de sélection
    $(".MRVImgSuppVille").live("click", function () {
        KillTooltips();
        if (LaSelection.length > 1) {
            var no = parseInt($(this).attr("no"));
            //if (confirm("Etes-vous certain(e) de vouloir supprimer " + LaSelection[no].libelle + " de votre sélection ?")) {
            LaSelection.splice(no, 1); $(this).parent().hide("slow", function () { $(this).remove() });
            $("div .MRVDivVille").each(function (i) {
                $(this).children("img").removeAttr("no").attr("no", i.toString());
            });
            //}
            if (LaSelection.length == 1 && Mode == modes.ville) { $("#MRVRayon").removeAttr("disabled"); }
        }
        else {
            //if (confirm("Etes-vous certain(e) de vouloir supprimer " + LaSelection[0].libelle + " de votre sélection ?")) {
            LaSelection = new Array();
            $(this).parent().hide("slow", function () { $(this).remove() });
            if (PanneauOuvert) { FermePanneauSelection(); }
            //}
        }
    });
    // Les 3 bulles d'aide
    $("#MRVImgSituation").mouseover(function () {
        EPCTooltip($(this), "Veuillez indiquer la ou les villes de recherche<br />ou le ou les codes postaux de recherche.<br />Vous pouvez renseigner plusieurs lieux de recherche.", "Info", null);
    }).mouseout(function () {
        KillTooltips();
    });
    $("#MRVImgSurface").mouseover(function () {
        EPCTooltip($(this), "Veuillez indiquer la surface en m².<br />(10 000 m² = 1 hectare)", "Info", null);
    }).mouseout(function () {
        KillTooltips();
    });
    $("#MRVImgReference").mouseover(function () {
        EPCTooltip($(this), "Saisissez la référence que vous avez relevée,<br />en ligne ou sur un panneau extérieur Entreparticuliers.com,<br />pour consulter l'annonce détaillée correspondant à ce bien.", "Info", null);
    }).mouseout(function () {
        KillTooltips();
    });
    // Par défaut, le rayon est inaccessible
    $("#MRVRayon").attr("disabled", true);
    // On checke la présence d'une session de recherche
    $.ajax({
        type: "POST",
        url: "/Default.aspx/GetSearchParams",
        data: "",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            if (data.d != "null") {
                // On vient de récupèrer un objet de recherche
                var SearchParams = eval('(' + data.d + ')');
                if (SearchParams.ach_id > -1) { idUser = SearchParams.ach_id; }
                // On remplit le moteur en fonction des valeurs
                if (SearchParams.pays == null)
                    SearchParams.pays = "fra";
                $("#MRVPays").val(SearchParams.pays.toLowerCase());
                $("#MRVPays").change();
                if (SearchParams.pays.toLowerCase() == "fra") {
                    // Si c'est en France
                    switch (SearchParams.localisationType) {
                        case 1:
                            // Villes
                            $("#MRVCbxVille").click();
                            // Il faut récupérer des objets à partir des ids                            
                            $.ajax({
                                type: "POST",
                                url: "/HTTPHandlers/VillesGetByIds.ashx?ids=" + SearchParams.lstLocalisationId,
                                data: "",
                                contentType: "application/json; charset=utf-8",
                                dataType: "json",
                                success: function (data) {
                                    LaSelection = data;
                                    if (LaSelection.length >= 2) { AffichePanneauSelectionRayon(); } else {
                                        $("#MRVSaisie").unbind("focus").unbind("blur");
                                        if (LaSelection.length == 1) { $("#MRVRayon").removeAttr("disabled"); $("#MRVSaisie").css("color", "#000").val(LaSelection[0].libelle + ", ").focus(); }
                                    }
                                    ChargeAutoComplete();
                                }
                            });
                            break;
                        case 2:
                            // Departement
                            // On récupère les objets dans le tableau en dur
                            $("#MRVCbxDepartement").click();
                            var tablo = SearchParams.lstLocalisationId.split(',');
                            LaSelection = new Array();
                            var idEnCours = "";
                            for (var j = 0; j < tablo.length; j++) {
                                idEnCours = tablo[j];
                                for (var i = 0; i < departements.length; i++) {
                                    if (idEnCours == departements[i].id) {
                                        LaSelection.push(departements[i]);
                                    }
                                }
                            }
                            if (LaSelection.length >= 2) { AffichePanneauSelectionRayon(); } else {
                                $("#MRVSaisie").unbind("focus").unbind("blur");
                                if (LaSelection.length == 1) { $("#MRVSaisie").css("color", "#000").val(LaSelection[0].libelle + " (" + LaSelection[0].id + "), ").focus(); };
                            }
                            ChargeAutoComplete();
                            break;
                        case 3:
                            // Region
                            // On récupère les objets dans le tableau en dur
                            $("#MRVCbxRegion").click();
                            var tablo = null;
                            if (SearchParams.lstLocalisationId != null)
                                tablo = SearchParams.lstLocalisationId.split(',');
                            LaSelection = new Array();
                            var idEnCours = "";
                            for (var j = 0; j < tablo.length; j++) {
                                idEnCours = tablo[j];
                                idRegion = GetIdRegionByIdDepartement(idEnCours);
                                for (var i = 0; i < regions.length; i++) {
                                    if (idRegion == regions[i].id) {
                                        LaSelection.push(regions[i]);
                                    }
                                }
                            }
                            if (LaSelection.length >= 2) { AffichePanneauSelectionRayon(); } else {
                                $("#MRVSaisie").unbind("focus").unbind("blur");
                                if (LaSelection.length == 1) { $("#MRVSaisie").css("color", "#000").val(LaSelection[0].libelle + " (" + LaSelection[0].id + "), ").focus(); };
                            }
                            ChargeAutoComplete();
                            break;
                    }
                } else { Mode = modes.pays; }
                // Les types de bien
                if (SearchParams.lstTbien == null)
                    SearchParams.lstTbien = "1'2";
                var TypesDeBien = SearchParams.lstTbien.split(',');
                $("#EPCMultiCheck input[type='checkbox']").attr("checked", false);
                for (var i = 0; i < TypesDeBien.length; i++) {
                    $("#EPCMultiCheck input[type='checkbox']").each(function (j) {
                        if ($(this).val() == TypesDeBien[i]) {
                            $(this).attr('checked', true);
                        }
                    });
                }
                CheckTypesDeBiens();
                // Surface minimum
                if (parseInt(SearchParams.surface_min) > 0) { $("#MRVSurfaceMini").val(SearchParams.surface_min.toString()).css("color", "#000"); }
                // Prix minimum
                if (parseInt(SearchParams.prix_min) > 0) { $("#MRVPrixMini").val(SearchParams.prix_min.toString()).css("color", "#000"); }
                // Prix maximum
                if (parseInt(SearchParams.prix_max) > 0) { $("#MRVPrixMaxi").val(SearchParams.prix_max.toString()).css("color", "#000"); }
                // Nb de pièces
                $("input [id^='nbPieces']").attr('checked', false);
                if (SearchParams.lstNbPieces != null && SearchParams.lstNbPieces != "") {
                    var tabNbPieces = SearchParams.lstNbPieces.split(",");
                    var idCbx = "nbPieces";
                    for (var i = 0; i < tabNbPieces.length; i++) {
                        var idEnCours = "#" + idCbx + tabNbPieces[i];
                        $(idEnCours).attr('checked', true);
                    }
                }
                // Uniquement avec photo
                $("#MRVPhotoUniquement").attr('checked', false);
                if (SearchParams.photos == 1) { $("#MRVPhotoUniquement").attr('checked', true); }
                // Référence                
                if (SearchParams.reference != null) {
                    $("#MRVRefAnnonce").val(SearchParams.reference);
                }
            }
            else {
                $("#MRVCritereGauche span.MRVSpanRecherche:first").click();
                LesTypesDeBien = new Array();
                var TbienAppart = new Object();
                TbienAppart.id = 1;
                TbienAppart.libelle = "appartement";
                var TbienMaison = new Object();
                TbienMaison.id = 2;
                TbienMaison.libelle = "maison";
                LesTypesDeBien.push(TbienAppart);
                LesTypesDeBien.push(TbienMaison);
                Mode = modes.ville;
                AboSaisie();
                CheckTypesDeBiens();
            }
        }, error: function (error) { alert("error get searchparam"); }
    });
});
var idUser = -1;
// La liste d'objets représentant
// la sélection d'ids ville, dept ou region
var LaSelection = new Array();
// Le mode dans lequel on se trouve
var Mode;
// son énumération
var modes = { "ville": 0, "region": 1, "departement": 2, "pays": 3 };
// Booléen permettant de savoir si le panneau est ouvert
var PanneauOuvert = false;
var PanneauCarteOuvert = false;
// Liste des types de bien de recherche
var LesTypesDeBien = new Array();
// Le rayon
var rayonInt = -1;
// L'id de la ville sur laquelle porte le rayon
var localisation_id_rayon = null;
// Crée un tooltip
function EPCTooltip(obj, texte, type, temps) {
    KillTooltips();
    var cssClass = "EPCtooltip" + type;
    var div = "<div class='" + cssClass + "'>" + texte + "</div>";
    var offsetDepart = obj.offset();
    if (type == "Carte") { offsetDepart.top += $("#carteFrance").offset().top; offsetDepart.left += $("#carteFrance").offset().left; }   
    $("body").append(div);
    var selector = "." + cssClass;
    var widthTooltip = $(selector).outerWidth();
    var heightTooltip = $(selector).outerHeight();
    var widthObj = obj.outerWidth();
    var fleche = "<div class='EPCtooltip" + type + "Fleche'></div>";
    $("body").append(fleche);
    if (type == "Carte") {
        $("div.EPCtooltip" + type + "Fleche").css("top", offsetDepart.top + heightTooltip -1).css("left", offsetDepart.left - ((9 - widthObj) / 2)).fadeIn(200);
        $(selector).css("top", offsetDepart.top).css("left", offsetDepart.left - ((widthTooltip - widthObj) / 2)).fadeIn(200);

    } else {
        $("div.EPCtooltip" + type + "Fleche").css("top", offsetDepart.top - 6).css("left", offsetDepart.left - ((9 - widthObj) / 2)).fadeIn(200);
        $(selector).css("top", offsetDepart.top - heightTooltip - 5).css("left", offsetDepart.left - ((widthTooltip - widthObj) / 2)).fadeIn(200);
    }    
    $(selector).mouseover(function () { KillTooltips(); });
    if (parseInt(temps) > 0) { setTimeout(KillTooltips, temps * 1000); }
}
// Détruit tous les tooltips
function KillTooltips() {
    $("div[class^=EPCtooltip]").remove();
}
// Ajoute une nouvelle entrée
// au panneau déjà ouvert (avec animation, donc)
function AjoutePanneauSelection() {
    var obj = LaSelection[LaSelection.length - 1];
    var offsetDepart = $("#MRVSaisie").offset();
    var widthDepart = $("#MRVSaisie").width();
    var offsetArrivee = $("#MRVContVille .MRVDivVille:last").offset();
    var span = "<span id='epcRulez' style='white-space:nowrap;position:absolute;height:20px;font-family:Verdana, Geneva, Arial, Helvetica, sans-serif;text-align:center;line-height:20px;font-size:13px;z-index:10000;top:" + (offsetDepart.top + 1) + "px;left:" + (offsetDepart.left + (widthDepart / 2)) + "px;width:" + widthDepart + "px'>" + obj.libelle + "</span>";
    $("body").append(span);
    $("#epcRulez").width(textWidth($("#epcRulez").text(), "Verdana, Geneva, Arial, Helvetica, sans - serif", 12));
    var newText = obj.libelle;
    var AjouterTooltip = false;
    if (textWidth($("#epcRulez").text(), "Verdana, Geneva, Arial, Helvetica, sans - serif", 12) >= 150) {
        newText = obj.libelle.substring(0, 19) + "...";
        AjouterTooltip = true;
    }
    var div = '<div class="MRVDivVille"><img no="' + (LaSelection.length - 1).toString() + '" class="MRVImgSuppVille" src="/Images/picto_supprime.png" />' + newText + '</div>';
    var topVal = offsetArrivee.top + 20;
    if (topVal > 440) { topVal = 440; }
    $("#epcRulez").animate({
        top: topVal,
        left: (offsetArrivee.left + 18)
    }, 400, function () {
        $("#epcRulez").remove();
        $("#MRVContVille").append(div);
        $("#MRVSaisie").val("").css("color", "#A1A1A1").blur().focus();
        $("#MRVContVille .MRVDivVille").not("#MRVContVille .MRVDivVille:last").css("border-bottom", "1px solid #CCCCCC");
        if (AjouterTooltip) {
            ActiveTooltip($("#MRVContVille .MRVDivVille:last"), obj.libelle);
        }
        $("#MRVContVille .MRVDivVille").css("background-color", "#FFFFFF");
        $("#MRVContVille .MRVDivVille:odd").css("background-color", "#EAF0F4");
    });   
}
// Active un tooltip sur un objet
function ActiveTooltip(obj, texte) {
    obj.mouseover(function () {
        EPCTooltip($(this), texte, "Info", null);
    }).mouseout(function () {
        KillTooltips();
    });
}
// Ferme le panneau de sélection
function FermePanneauSelection() {
    if (PanneauOuvert) {
        $("#MRVPanneauSelection").animate({ left: '-=207px' }, '400');
        $("#VideSelec").remove();
        PanneauOuvert = false;
    }
}
// Fonction permettant de calculer à la volée
// la taille d'un texte en pixels
function textWidth(text, font, size) {
    var calc = '<span style="display:none;font-size:' + size + 'px;font-family:' + font + '">' + text + '</span>';
    $('body').append(calc);
    var width = $('body').find('span:last').width();
    $('body').find('span:last').remove();
    return width;
}
// Fonction en charge de l'autocomplétion
function ChargeAutoComplete() {
    var data;
    var minChar = 0;
    if (Mode == modes.region) { data = regions; } else if (Mode == modes.departement) { data = departements; }
    $("#MRVSaisie").flushCache();
    $("#MRVSaisie").unautocomplete();
    AboSaisie();
    if (Mode == modes.ville) {
        $("#MRVSaisie").autocomplete('/HTTPHandlers/VillesAutocompleteHandler.ashx', {
            dataType: "json",
            max: 21,
            multiple: true,
            multipleSeparator: ",",
            minChars: 1,
            parse: function (data) {
                var rows = new Array();
                for (var i = 0; i < data.length; i++) {
                    rows[i] = { data: data[i], value: data[i].libelle, result: data[i].libelle };
                }
                return rows;
            },
            formatItem: function (item) {
                return item.libelle;
            },
            formatResult: function (item) {
                return item.libelle;
            }
        }).result(function (e, item) {
            $("#MRVSaisie").css("color", "#000");
            if (AjouteSelection(item) == true) {
                if (Mode == modes.ville && LaSelection.length == 1) { $("#MRVRayon").removeAttr("disabled"); }
                else if (LaSelection.length == 2) { if (Mode == modes.ville) { $("#MRVRayon").attr("disabled", true); } AffichePanneauSelection(); }
                else if (LaSelection.length > 2) { AjoutePanneauSelection(); }
            }
            var el = document.getElementById("MRVSaisie");
            var t = el.value.length;
            el.focus();
            setSelectionRange(el, t, 0);
        });
    } else {
    $("#MRVSaisie").autocomplete(data, {
        matchContains: true,
        multiple: true,
        multipleSeparator: ",",
        formatItem: function (item) {
            if (Mode == modes.departement) { return item.libelle + ' (' + item.id + ')'; } else return item.libelle;
        },
        formatResult: function (item) {
            if (Mode == modes.departement) { return item.libelle + ' (' + item.id + ')'; } else return item.libelle;
        }
    }).result(function (e, item) {
        $("#MRVSaisie").css("color", "#000");
        if (AjouteSelection(item) == true) {
            if (Mode == modes.ville && LaSelection.length == 1) { $("#MRVRayon").removeAttr("disabled"); }
            else if (LaSelection.length == 2) { if (Mode == modes.ville) { $("#MRVRayon").attr("disabled", true); } AffichePanneauSelection(); }
            else if (LaSelection.length > 2) { AjoutePanneauSelection(); }
        }
        var el = document.getElementById("MRVSaisie");
        var t = el.value.length;
        el.focus();
        setSelectionRange(el, t, 0);
    });
    }
}
// Abonnement aux événements sur
// la zone de saisie
function AboSaisie() {
    $("#MRVSaisie").focus(function () {
        switch (Mode) {
            case modes.ville:
                if ($(this).val() == "Indiquer ville(s) ou code(s) postal(aux)") {
                    $(this).css("color", "#000").val("").select();
                }
                break;
            case modes.region:
                if ($(this).val() == "Indiquer région(s)") {
                    $(this).css("color", "#000").val("").select();
                }
                break;
            case modes.departement:
                if ($(this).val() == "Indiquer département(s) ou numéro(s)") {
                    $(this).css("color", "#000").val("").select();
                }
                break;
        }
    }).blur(function () {
        if (($.trim($(this).val()) == "" && LaSelection.length != 1) || ($.trim($(this).val()) != "" && LaSelection.length != 1)) {
            switch (Mode) {
                case modes.ville:
                    $(this).val("Indiquer ville(s) ou code(s) postal(aux)").css("color", "#A1A1A1");
                    break;
                case modes.region:
                    $(this).val("Indiquer région(s)").css("color", "#A1A1A1");
                    break;
                case modes.departement:
                    $(this).val("Indiquer département(s) ou numéro(s)").css("color", "#A1A1A1");
                    break;
            }
        }
        else if (($.trim($(this).val()) == "") && LaSelection.length == 1) {
            switch (Mode) {
                case modes.ville:
                    $(this).val("Indiquer ville(s) ou code(s) postal(aux)").css("color", "#A1A1A1");
                    break;
                case modes.region:
                    $(this).val("Indiquer région(s)").css("color", "#A1A1A1");
                    break;
                case modes.departement:
                    $(this).val("Indiquer département(s) ou numéro(s)").css("color", "#A1A1A1");
                    break;
            }
            LaSelection = new Array();
        }
    });
}
var regions = [{ libelle: 'Guadeloupe', id: '01' }, { libelle: 'Martinique', id: '02' }, { libelle: 'Guyane', id: '03' }, { libelle: 'La Réunion', id: '04' }, { libelle: 'Mayotte', id: '05' }, { libelle: 'Ile-de-France', id: '11' },
    { libelle: 'Champagne-Ardenne', id: '21' }, { libelle: 'Picardie', id: '22' }, { libelle: 'Haute-Normandie', id: '23' }, { libelle: 'Centre', id: '24' }, { libelle: 'Basse-Normandie', id: '25' },
    { libelle: 'Bourgogne', id: '26' }, { libelle: 'Nord-Pas-de-Calais', id: '31' }, { libelle: 'Lorraine', id: '41' }, { libelle: 'Alsace', id: '42' }, { libelle: 'Franche-Comté', id: '43' },
    { libelle: 'Pays de la Loire', id: '52' }, { libelle: 'Bretagne', id: '53' }, { libelle: 'Poitou-Charentes', id: '54' }, { libelle: 'Aquitaine', id: '72' }, { libelle: 'Midi-Pyrénées', id: '73' },
    { libelle: 'Limousin', id: '74' }, { libelle: 'Rhône-Alpes', id: '82' }, { libelle: 'Auvergne', id: '83' }, { libelle: 'Languedoc-Roussillon', id: '91' }, { libelle: "Provence-Alpes-Côte d'Azur", id: '93' },
    { libelle: 'Corse', id: '94'}];
var departements = [{ libelle: 'Ain', id: '01' }, { libelle: 'Aisne', id: '02' }, { libelle: 'Allier', id: '03' }, { libelle: 'Alpes-de-Haute-Provence', id: '04' }, { libelle: 'Hautes-Alpes', id: '05' }
, { libelle: 'Alpes-Maritimes', id: '06' }, { libelle: 'Ardèche', id: '07' }, { libelle: 'Ardennes', id: '08' }, { libelle: 'Ariège', id: '09' }, { libelle: 'Aube', id: '10' }, { libelle: 'Aude', id: '11' }
, { libelle: 'Aveyron', id: '12' }, { libelle: 'Bouches-du-Rhône', id: '13' }, { libelle: 'Calvados', id: '14' }, { libelle: 'Cantal', id: '15' }, { libelle: 'Charente', id: '16' }, { libelle: 'Charente-Maritime', id: '17' }
, { libelle: 'Cher', id: '18' }, { libelle: 'Corrèze', id: '19' }, { libelle: 'Corse-du-Sud', id: '2A' }, { libelle: 'Haute-Corse', id: '2B' }, { libelle: "Côte-d'Or", id: '21' }, { libelle: "Côtes-d'Armor", id: '22' }
, { libelle: 'Creuse', id: '23' }, { libelle: 'Dordogne', id: '24' }, { libelle: 'Doubs', id: '25' }, { libelle: 'Drôme', id: '26' }, { libelle: 'Eure', id: '27' }, { libelle: 'Eure-et-Loir', id: '28' }
, { libelle: 'Finistère', id: '29' }, { libelle: 'Gard', id: '30' }, { libelle: 'Haute-Garonne', id: '31' }, { libelle: 'Gers', id: '32' }, { libelle: 'Gironde', id: '33' }, { libelle: 'Hérault', id: '34' }
, { libelle: 'Ille-et-Vilaine', id: '35' }, { libelle: 'Indre', id: '36' }, { libelle: 'Indre-et-Loire', id: '37' }, { libelle: 'Isère', id: '38' }, { libelle: 'Jura', id: '39' }, { libelle: 'Landes', id: '40' }
, { libelle: 'Loir-et-Cher', id: '41' }, { libelle: 'Loire', id: '42' }, { libelle: 'Haute-Loire', id: '43' }, { libelle: 'Loire-Atlantique', id: '44' }, { libelle: 'Loiret', id: '45' }, { libelle: 'Lot', id: '46' }
, { libelle: 'Lot-et-Garonne', id: '47' }, { libelle: 'Lozère', id: '48' }, { libelle: 'Maine-et-Loire', id: '49' }, { libelle: 'Manche', id: '50' }, { libelle: 'Marne', id: '51' }, { libelle: 'Haute-Marne', id: '52' }
, { libelle: 'Mayenne', id: '53' }, { libelle: 'Meurthe-et-Moselle', id: '54' }, { libelle: 'Meuse', id: '55' }, { libelle: 'Morbihan', id: '56' }, { libelle: 'Moselle', id: '57' }, { libelle: 'Nièvre', id: '58' }
, { libelle: 'Nord', id: '59' }, { libelle: 'Oise', id: '60' }, { libelle: 'Orne', id: '61' }, { libelle: 'Pas-de-Calais', id: '62' }, { libelle: 'Puy-de-Dôme', id: '63' }, { libelle: 'Pyrénées-Atlantiques', id: '64' }
, { libelle: 'Hautes-Pyrénées', id: '65' }, { libelle: 'Pyrénées-Orientales', id: '66' }, { libelle: 'Bas-Rhin', id: '67' }, { libelle: 'Haut-Rhin', id: '68' }, { libelle: 'Rhône', id: '69' }, { libelle: 'Haute-Saône', id: '70' }
, { libelle: 'Saône-et-Loire', id: '71' }, { libelle: 'Sarthe', id: '72' }, { libelle: 'Savoie', id: '73' }, { libelle: 'Haute-Savoie', id: '74' }, { libelle: 'Paris', id: '75' }, { libelle: 'Seine-Maritime', id: '76' }
, { libelle: 'Seine-et-Marne', id: '77' }, { libelle: 'Yvelines', id: '78' }, { libelle: 'Deux-Sèvres', id: '79' }, { libelle: 'Somme', id: '80' }, { libelle: 'Tarn', id: '81' }, { libelle: 'Tarn-et-Garonne', id: '82' }
, { libelle: 'Var', id: '83' }, { libelle: 'Vaucluse', id: '84' }, { libelle: 'Vendée', id: '85' }, { libelle: 'Vienne', id: '86' }, { libelle: 'Haute-Vienne', id: '87' }, { libelle: 'Vosges', id: '88' }
, { libelle: 'Yonne', id: '89' }, { libelle: 'Territoire de Belfort', id: '90' }, { libelle: 'Essonne', id: '91' }, { libelle: 'Hauts-de-Seine', id: '92' }, { libelle: 'Seine-Saint-Denis', id: '93' }, { libelle: 'Val-de-Marne', id: '94' }
, { libelle: "Val-d'Oise", id: '95' }, { libelle: 'Guadeloupe', id: '971' }, { libelle: 'Martinique', id: '972' }, { libelle: 'Guyane', id: '973' }, { libelle: 'La Réunion', id: '974' }, { libelle: 'Mayotte', id: '976'}];
function close_modal() {
    $('#mask').fadeOut(200);
    $('#modal_villes').fadeOut(200);
}
function show_modal(modal_id) {
    $('#mask').css({ 'height': $(document).height(), 'width': $(document).width(), 'display': 'block', opacity: 0 });
    $('#mask').fadeTo(200, 0.75);
    $('#' + modal_id).fadeIn(200);
    $("#modal_villes").css("left", (($('#mask').outerWidth() - $("#modal_villes").outerWidth()) / 2) + "px");
    var ville = LaSelection[0].libelle;
    var t = ville.split('(');
    ville = $.trim(t[0]);
    var t2 = t[1];
    var t3 = t2.split(')');
    var codepostal = t3[0];
    var t4 = $("#MRVRayon").val().split(" ");
    var rayon = t4[0];
    $("#modal_villes div.header").text("les communes dans un rayon de " + rayon + " km autour de " + ville);
    rayonInt = parseInt(rayon);
    localisation_id_rayon = LaSelection[0].id;
    var img = "<img alt='' src='/Images/villesLoader.gif' style='width:60px;height:64px;position:relative;top:118px;left:212px;' />";
    $("#divVillesRayon").html(img);
    $.ajax({
        type: "POST",
        url: "/HTTPHandlers/VillesRayonHandler.ashx?ville=" + ville + "&codepostal=" + codepostal + "&distance=" + rayon,
        data: "",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            FillListeRayon(data);
        }
    });
}
function FillListeRayon(data) {
    var divs = "";
    for (var i = 0; i < data.length; i++) {
        divs += "<div class='ville'><input id='cbx" + i.toString() + "' type='checkbox' style='height:18px;line-height:18px;float:left;margin-left:5px' value='" + data[i].id + "' /><label for='cbx" + i.toString() + "' style='height:18px;line-height:18px;margin-left:5px;float:left;width:430px;overflow:hidden;'>" + data[i].libelle + "</label><div class='clr'></div></div>";
    }
    $("#divVillesRayon").html(divs);
    $("#divVillesRayon .ville").not("#MRVContVille .ville:last").css("border-bottom", "1px solid #CCCCCC");
    $("#divVillesRayon .ville:odd").css("background-color", "#DEE6EC");
    $(".toutCocher,.toutDecocher").mouseover(function () {
        $(this).css("text-decoration", "underline").css("font-weight", "bold");
    }).mouseout(function () {
        $(this).css("text-decoration", "none").css("font-weight", "normal");
    }).click(function () {
        if ($(this).attr("class") == "toutDecocher") {
            $("#modal_villes input[type='checkbox']").attr("checked", false);
        }
        else {
            $("#modal_villes input[type='checkbox']").attr("checked", true);
        }
    });
    $("#ModalBtnValider").click(function (e) {
        e.preventDefault();
        $("#modal_villes input[type='checkbox']:checked").each(function () {
            var obj = new Object();
            var t = $(this).next().text().split(')');
            obj.libelle = t[0] + ")";
            obj.id = $(this).val();
            LaSelection.push(obj);
            // AjouteSelection(obj);
        });
        close_modal(); 
        if (LaSelection.length > 1) { $("#MRVRayon").attr("disabled", true); AffichePanneauSelectionRayon(); }
    });
}
function AffichePanneauSelectionRayon() {
    if (!PanneauOuvert) {
        $("#MRVContVille").html("");
        $("#MRVPanneauSelection").animate({ left: '+=207px' }, '400');
        var tooltip = LaSelection.length < 100;         
        for (var i = 0; i < LaSelection.length; i++) {
            var newText = LaSelection[i].libelle;
            var div = '<div class="MRVDivVille"><img no="' + i.toString() + '" class="MRVImgSuppVille" src="/Images/picto_supprime.png" />' + newText + '</div>';
            $("#MRVContVille").append(div);
            if (tooltip) {
                if (textWidth($("#MRVContVille div.MRVDivVille:last").text(), "Verdana, Geneva, Arial, Helvetica, sans - serif", 12) >= 150) {
                    newText = LaSelection[i].libelle.substring(0, 19) + "...";
                    $("#MRVContVille div.MRVDivVille:last").html('<img no="' + i.toString() + '" class="MRVImgSuppVille" src="/Images/picto_supprime.png" />' + newText);
                    ActiveTooltip($("#MRVContVille div.MRVDivVille:last"), LaSelection[i].libelle);
                } 
            }
        }
        $("#MRVSaisie").val("").css("color", "#A1A1A1").blur().focus();
        $("#MRVContVille .MRVDivVille").not("#MRVContVille .MRVDivVille:last").css("border-bottom", "1px solid #CCCCCC");
        PanneauOuvert = true;
        $("#MRVContVille .MRVDivVille").css("background-color", "#FFFFFF");
        $("#MRVContVille .MRVDivVille:odd").css("background-color", "#EAF0F4");
        $("<center><a style='font-weight:bold;margin-top:3px;' href='' id='VideSelec'><img class='MRVImgSuppVille' style='margin-top:2px' src='/Images/picto_supprime.png' alt='' />Vider ma sélection</a></center>").insertAfter("#MRVContVille");
    }    
}
function AffichePanneauSelection() {
    if (!PanneauOuvert) {
        var v1 = LaSelection[0];
        var v2 = LaSelection[1];
        $("#MRVPanneauSelection").animate({ left: '+=207px' }, '400');
        var div1 = '<div class="MRVDivVille"><img no="0" class="MRVImgSuppVille" src="/Images/picto_supprime.png" />' + v1.libelle + '</div>';
        var div2 = '<div class="MRVDivVille"><img no="1" class="MRVImgSuppVille" src="/Images/picto_supprime.png" />' + v2.libelle + '</div>';
        $("#MRVContVille").append(div1);
        $("<center><a style='font-weight:bold;margin-top:3px;' href='' id='VideSelec'><img class='MRVImgSuppVille' style='margin-top:2px' src='/Images/picto_supprime.png' alt='' />Vider ma sélection</a></center>").insertAfter("#MRVContVille");
        var newText = "";
        if (textWidth($("#MRVContVille div.MRVDivVille:last").text(), "Verdana, Geneva, Arial, Helvetica, sans - serif", 12) >= 150) {
            newText = LaSelection[0].libelle.substring(0, 19) + "...";
            $("#MRVContVille div.MRVDivVille:last").html('<img no="0" class="MRVImgSuppVille" src="/Images/picto_supprime.png" />' + newText);
            ActiveTooltip($("#MRVContVille div.MRVDivVille:last"), LaSelection[0].libelle);
        }
        $("#MRVContVille").append(div2);
        if (textWidth($("#MRVContVille div.MRVDivVille:last").text(), "Verdana, Geneva, Arial, Helvetica, sans - serif", 12) >= 150) {
            newText = LaSelection[1].libelle.substring(0, 19) + "...";
            $("#MRVContVille div.MRVDivVille:last").html('<img no="1" class="MRVImgSuppVille" src="/Images/picto_supprime.png" />' + newText);
            ActiveTooltip($("#MRVContVille div.MRVDivVille:last"), LaSelection[1].libelle);
        }
        $("#MRVSaisie").val("").css("color", "#A1A1A1").blur().focus();
        $("#MRVContVille .MRVDivVille").not("#MRVContVille .MRVDivVille:last").css("border-bottom", "1px solid #CCCCCC");
        PanneauOuvert = true;
        $("#MRVContVille .MRVDivVille").css("background-color", "#FFFFFF");
        $("#MRVContVille .MRVDivVille:odd").css("background-color", "#EAF0F4");
    }
}
function GetDepartementsByIdRegion(idRegion) {
    var retour = new Array();
    switch (idRegion) {
        case "42":
            retour = ["67", "68"];
            break;
        case "72":
            retour = ["24", "33", "40", "47", "64"];
            break;
        case "83":
            retour = ["03", "15", "43", "63"];
            break;
        case "26":
            retour = ["21", "58", "71", "89"];
            break;
        case "53":
            retour = ["22", "29", "35", "56"];
            break;
        case "24":
            retour = ["18", "28", "36", "37", "41", "45"];
            break;
        case "21":
            retour = ["08", "10", "51", "52"];
            break;
        case "94":
            retour = ["2A", "2B"];
            break;
        case "43":
            retour = ["25", "39", "70", "90"];
            break;
        case "11":
            retour = ["75", "91", "92", "93", "77", "94", "95", "78"];
            break;
        case "91":
            retour = ["11", "30", "34", "48", "66"];
            break;
        case "74":
            retour = ["19", "23", "87"];
            break;
        case "41":
            retour = ["54", "55", "57", "88"];
            break;
        case "73":
            retour = ["09", "12", "31", "32", "46", "65", "81", "82"];
            break;
        case "31":
            retour = ["59", "62"];
            break;
        case "25":
            retour = ["14", "50", "61"];
            break;
        case "23":
            retour = ["27", "76"];
            break;
        case "52":
            retour = ["44", "49", "53", "72", "85"];
            break;
        case "22":
            retour = ["02", "60", "80"];
            break;
        case "54":
            retour = ["16", "17", "79", "86"];
            break;
        case "93":
            retour = ["04", "05", "06", "13", "83", "84"];
            break;
        case "82":
            retour = ["01", "07", "26", "38", "42", "69", "73", "74"];
            break;
        case "01":
            retour = ["971"];
            break;
        case "03":
            retour = ["973"];
            break;
        case "02":
            retour = ["972"];
            break;
        case "04":
            retour = ["974"];
            break;
    }
    return retour;
}
function GetIdRegionByIdDepartement(idDepartement) {
    var retour = "";
    switch (idDepartement) {
        case "67", "68":
            retour = "42";
            break;
        case "24", "33", "40", "47", "64":
            retour = "72";
            break;
        case "03", "15", "43", "63":
            retour = "83";
            break;
        case "21", "58", "71", "89":
            retour = "26";
            break;
        case "22", "29", "35", "56":
            retour = "53";
            break;
        case "18", "28", "36", "37", "41", "45":
            retour = "24";
            break;
        case "08", "10", "51", "52":
            retour = "21";
            break;
        case "2A", "2B":
            retour = "94";
            break;
        case "25", "39", "70", "90":
            retour = "43";
            break;
        case "75", "91", "92", "93", "77", "94", "95", "78":
            retour = "11";
            break;
        case "11", "30", "34", "48", "66":
            retour = "91";
            break;
        case "19", "23", "87":
            retour = "74";
            break;
        case "54", "55", "57", "88":
            retour = "41";
            break;
        case "09", "12", "31", "32", "46", "65", "81", "82":
            retour = "73";
            break;
        case "59", "62":
            retour = "31";
            break;
        case "14", "50", "61":
            retour = "25";
            break;
        case "27", "76":
            retour = "23";
            break;
        case "44", "49", "53", "72", "85":
            retour = "52";
            break;
        case "02", "60", "80":
            retour = "22";
            break;
        case "16", "17", "79", "86":
            retour = "54";
            break;
        case "04", "05", "06", "13", "83", "84":
            retour = "93";
            break;
        case "01", "07", "26", "38", "42", "69", "73", "74":
            retour = "82";
            break;
        case "971":
            retour = "01";
            break;
        case "973":
            retour = "03";
            break;
        case "972":
            retour = "02";
            break;
        case "974":
            retour = "04";
            break;
    }
    return retour;
}
// Pour le multi select
// Permet de générer la liste des types de biens
function CheckTypesDeBiens() {
    LesTypesDeBien = new Array();
    $("#EPCMultiCheck input:checked").each(function () {
        var Tbien = new Object();
        Tbien.id = $(this).val();
        Tbien.libelle = $(this).next().text();
        LesTypesDeBien.push(Tbien);
    });
    if (LesTypesDeBien.length > 0) {
        var texte = "";
        var ids = "";
        if (LesTypesDeBien.length == 2) {
            for (var i = 0; i < LesTypesDeBien.length; i++) {
                ids += LesTypesDeBien[i].id;
            }
            if (ids == "12" || ids == "21") { $("#EPCMultiCheck div.EPCMCHeader").html("Appartement/Maison"); } else {
                $("#EPCMultiCheck div.EPCMCHeader").html("Sélection multiple");
            }
        } else {
            if (LesTypesDeBien.length > 1) { $("#EPCMultiCheck div.EPCMCHeader").html("Sélection multiple"); } else { $("#EPCMultiCheck div.EPCMCHeader").html(LesTypesDeBien[0].libelle); }
        }
    } else { $("#EPCMultiCheck div.EPCMCHeader").html("Aucune sélection"); }
}
// Checke les doublons avant d'ajouter à la sélection
function AjouteSelection(obj) {
    var retour = false;
    if (LaSelection.length > 0) {
        var trouve = false;
        for (var i = 0; i < LaSelection.length; i++) {
            if (LaSelection[i].id == obj.id && LaSelection[i].libelle == obj.libelle) {
                trouve = true;
                break;
            }
        }
        if (trouve) {
            var texte = "";
            switch (Mode) {
                case modes.ville:
                    texte = "La ville que vous avez sélectionnée figure déjà dans votre sélection";
                    break;
                case modes.departement:
                    texte = "Le département que vous avez sélectionné figure déjà dans votre sélection";
                    break;
                case modes.region:
                    texte = "La région que vous avez sélectionnée figure déjà dans votre sélection";
                    break;
            }
            EPCTooltip($("#MRVSaisie"), texte, "Info", 3);
            if (LaSelection.length == 1) {
                var t = $("#MRVSaisie").val().split(',');
                if (t.length > 1) { $("#MRVSaisie").val(t[0] + ",").focus(); }

            } else { $("#MRVSaisie").val("").focus(); }
            
        } else { LaSelection.push(obj); retour = true; }
    } else { LaSelection.push(obj); retour = true; }
    return retour;
}
function OuvrePanneauCarte() {
    $("#MRVPanneauCarte").animate({ left: '+=207px' }, '400');
    PanneauCarteOuvert = true;
}
function FermePanneauCarte() {
    $("#MRVPanneauCarte").animate({ left: '-=207px' }, '400');
    PanneauCarteOuvert = false;
}
function GetImageFromId(idRegion) {
    var retour = "";
    switch (idRegion) {
        case "01" :
            retour = "Guadeloupe.png";
            break;
        case "02":
            retour = "Martinique.png";
            break;
        case "03":
            retour = "Guyane.png";
            break;
        case "04":
            retour = "Reunion.png";
            break;
        case "11":
            retour = "IleDeFrance.png";
            break;
        case "21":
            retour = "ChampagneArdenne.png";
            break;
        case "22":
            retour = "Picardie.png";
            break;
        case "23":
            retour = "HauteNormandie.png";
            break;
        case "24":
            retour = "Centre.png";
            break;
        case "25":
            retour = "BasseNormandie.png";
            break;
        case "26":
            retour = "Bourgogne.png";
            break;
        case "31":
            retour = "NordPasDeCalais.png";
            break;
        case "41":
            retour = "Lorraine.png";
            break;
        case "42":
            retour = "Alsace.png";
            break;
        case "43":
            retour = "FrancheComte.png";
            break;
        case "52":
            retour = "PaysDeLaLoire.png";
            break;
        case "53":
            retour = "Bretagne.png";
            break;
        case "54":
            retour = "PoitouCharentes.png";
            break;
        case "72":
            retour = "Aquitaine.png";
            break;
        case "73":
            retour = "MidiPyrenees.png";
            break;
        case "74":
            retour = "Limousin.png";
            break;
        case "82":
            retour = "RhonesAlpes.png";
            break;
        case "83":
            retour = "Auvergne.png";
            break;
        case "91":
            retour = "LanguedocRoussillon.png";
            break;
        case "93":
            retour = "ProvenceAlpesCoteDAzur.png";
            break;
        case "94":
            retour = "Corse.png";
            break;
    }
    return retour;
}





