
var listeCritaires = new Array() ;
function initCritaires()
{
    listeCritaires[listeCritaires.length] = CreateNewParam("Viager", "viager", "1");
    listeCritaires[listeCritaires.length] = CreateNewParam("RDC", "rdc", "1");
    listeCritaires[listeCritaires.length] = CreateNewParam("Balcon", "blc", "1");
    listeCritaires[listeCritaires.length] = CreateNewParam("Terrace", "terr", "1");
    listeCritaires[listeCritaires.length] = CreateNewParam("Garage", "grg", "1");
    listeCritaires[listeCritaires.length] = CreateNewParam("Parking", "prk", "1");
    listeCritaires[listeCritaires.length] = CreateNewParam("Cave", "cave", "1");
    listeCritaires[listeCritaires.length] = CreateNewParam("Jardin", "jrdn", "1");
}

function initTypeImmo() {
    listeCritaires[listeCritaires.length] = CreateNewParam("Maison", "maison", "2");
    listeCritaires[listeCritaires.length] = CreateNewParam("Studio", "studio", "2");
    listeCritaires[listeCritaires.length] = CreateNewParam("T1", "t1", "2");
    listeCritaires[listeCritaires.length] = CreateNewParam("T2", "t2", "2");
    listeCritaires[listeCritaires.length] = CreateNewParam("T3", "t3", "2");
    listeCritaires[listeCritaires.length] = CreateNewParam("T4", "t4", "2");
    listeCritaires[listeCritaires.length] = CreateNewParam("T5", "t5", "2");
    listeCritaires[listeCritaires.length] = CreateNewParam("T6", "t6", "2");
}

function Contains(arr , element) {
    var result = false;
    if(arr != null)
    {
        for (var i = 0; i < arr.length; i++) {
            if (arr[i] == element) result = true;
        }
    }
    return result;
}


var GetCritairesValues;
function isCritaireExiste(key)
{
    if(GetCritairesValues == null)
    {
        // recuperation des critaires
        GetCritairesValues = new Array();
        if (getUrlVar("cr") != null) GetCritairesValues = getUrlVar("cr").split(",");
    }
    return Contains(GetCritairesValues,key);
}


function CreateNewParam(lib, key, type)
{
    
    var result = new Object();
    result.label = lib;
    result.key = key;
    result.crType = type;
    result.value = isCritaireExiste(key);
    return result;
}

function CreateCritairesDiv() {
    var result = "";
    for (i = 0; i < listeCritaires.length; i++) {
        var cr = listeCritaires[i];
        if (cr.crType == '1') result += CreateHtmlParamElement(cr);
    }
    return "<div>" + result + "</div>";
}


function CreateTypeImmoDiv() {
    var result = "";
    for (i = 0; i < listeCritaires.length; i++) {
        var cr = listeCritaires[i];
        if (cr.crType == '2')result += CreateHtmlParamElement(cr);
    }
    return result;
}

function CreateHtmlParamElement(cr) {
    var selected = cr.value ? "checked='checked'" : "";
    var result = "<div class='critereElement'><input type='checkbox' " + selected + " onclick='ParamChange(\"" + cr.key + "\", this.checked)'/> " + cr.label + "</div>";
    return result;
}

function ParamChange(key, value) {
    for (i = 0; i < listeCritaires.length; i++) {
        var cr = listeCritaires[i];
        if (cr.key == key ) {
            cr.value = value;
            break;
        }
    }
    AnalyseSearchParams();
}

function debugCritaires() {
    
}

function RefreshZonesCritaires() {
    
    document.getElementById('zoneCritaires').innerHTML = CreateCritairesDiv();
    document.getElementById('zonetypeImmo').innerHTML = CreateTypeImmoDiv();
}



var currentSearchLink = "";
function AnalyseSearchParams() {

    var result = "";
    var query = "";
    var host = HostUrlJs;
    
    var strTypes = "";
    var strCritaires = "";
    var strLocalisation = "";

    var linkTypes = "";
    var linkCritaires = "";
    var linkLocalisation = "";
    
    for (i = 0; i < listeCritaires.length; i++) {
        var cr = listeCritaires[i];
        if (cr.value) {
            if (cr.crType == "1") {
                strCritaires += (strCritaires.length > 0 ? ", " : "") + cr.label;
                linkCritaires += (linkCritaires.length > 0 ? "," : "") + cr.key;
            } else if (cr.crType == "2") {
                strTypes += (strTypes.length > 0 ? ", " : "") + cr.label;
                linkCritaires += (linkCritaires.length > 0 ? "," : "") + cr.key;
            }
        }
    }


    if (currentLocalisation.idCommune > 0) {
        strLocalisation = "Commune: <b>" + currentLocalisation.libCommune + "</b>";
        linkLocalisation = "idCommune=" + currentLocalisation.idCommune;
    }
    else if (currentLocalisation.idDepartement > 0) {
    strLocalisation = "Departement: <b>" + currentLocalisation.libDepartement + "</b>";
        linkLocalisation = "idDepartement=" + currentLocalisation.idDepartement;
    }
    else if (currentLocalisation.idRegion > 0) {
    strLocalisation = "Region: <b>" + currentLocalisation.libRegion + "</b>";
        linkLocalisation = "idRegion=" + currentLocalisation.idRegion;
    }

    result += (result.length > 0 && strLocalisation.length > 0 ? "<br/>" : "") + strLocalisation;
    result += (result.length > 0 && strTypes.length > 0 ? "<br/>" : "") + (strTypes.length > 0 ? "Type immobilier: <b>" + strTypes +"</b>" : "");
    result += (result.length > 0 && strCritaires.length > 0 ? "<br/>" : "") + (strCritaires.length > 0 ? "Critaires: <b>" + strCritaires + "</b>" : "");
    
    query += linkCritaires.length > 0 ? "cr=" + linkCritaires : "";
    query += (query.length > 0 && linkLocalisation.length > 0 ? "&" : "") + linkLocalisation;

    currentSearchLink = HostUrlJs + "?" + query;
    document.getElementById('InfoSearch').innerHTML = result;
}


function Search() {
    if (currentSearchLink.length > 0) {
        document.location = currentSearchLink;
    }
}

function getUrlVar(name) {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m, key, value) {
        vars[key] = value;
    });
    return vars[name]; 
}

