<%@ language = "JavaScript" %>
<% /*
     $Revision$
     $Id$

    File:        plaatsselector.asp
    Description: /
    Parameters:  startlevel:      begin met kiezen vanaf startlevel
                 eindlevel:       begin met kiezen tot en met eindlevel
                 roo_key:         ruimte_key
                 flr_key:         verdieping_key
                 bld_key:         gebouw_key
                 loc_key:         locatie_key
                 filtCode:        filterCode (bijvoorbeeld 'RESB')
                 act_key:         activity_key voor scoping
                 return_to:       url waar naar terug te keren na plaatsselectie (incl querystring)
                 return_from      url waar het scherm dat de plaatsselector aanroept terug moet keren (alsof de plaatsselector niet gebruikt is).
                 autlevel:        autlevel
    Context:     /
    Note:        /

*/ %>

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/discx3d.inc" -->
<!-- #include file="../Shared/selector.inc" -->
<!-- #include file="./mobile.inc" -->
<!-- #include file="./iface.inc" -->
<!-- #include file="./resultset_table.inc" -->
<!-- #include file="../Shared/Suggest/plaatsFilter.inc" -->

<%
var prs_obj = new Perslid(user_key);
var urole = "fe"; // getQParamSafe("urole", "fe");
var zoom = getQParamFloat("zoom", 2);

// TODO: filtCode per alg_level?
var filtCode        = getQParamSafe("filtCode", "").toUpperCase();
var binding         = getQParamSafe("bind", "").toUpperCase();
var act_key         = getQParamInt("act_key", -1);
var startlevel      = getQParamInt("startlevel", 2); // Default: Locatie
var eindlevel       = getQParamInt("eindlevel", 5);  // Default: Ruimte
var qrc             = getQParamInt("qrc", 0) != 0;
var return_to       = getQParam("return_to", "");
var return_from     = getQParam("return_from", "");
var bCad            = getQParamInt("cad", 0) == 1 && user.has("WEB_CADUSE");
var autlevel        = getQParamInt("autlevel", 9);
var autoopen        = getQParamInt("autoopen", 0);
var werkLocatieAsDefault = S("pda_prefer_current_werkplek") == 1;
var stdm_key        = getQParamInt("stdm", -1);

var plaats_params = { regio_key:      getQParamInt("reg_key", -1)
                    , district_key:   getQParamInt("dis_key", -1)
                    , locatie_key:    getQParamInt("loc_key", -1)
                    , gebouw_key:     getQParamInt("bld_key", -1)  // geen ter_key?
                    , verdieping_key: getQParamInt("flr_key", -1)
                    , ruimte_key:     getQParamInt("roo_key", -1)
                    }

var transitParam_arr    = ["zoom", "disc", "startlevel", "eindlevel", "autlevel", "filtCode", "bind", "return_to", "return_from", "reg_key", "dis_key", "loc_key", "bld_key", "flr_key", "roo_key", "act_key", "stdm", "cad"];
var extTransitParam_arr = ["disc", "stdm", "reg_key", "dis_key", "loc_key", "bld_key", "flr_key", "roo_key", "cad", "ins_key", "srtobj"];
var fltTransitParam = buildTransitParam(["zoom", "reg_key", "dis_key", "startlevel", "eindlevel", "autlevel", "disc", "act_key", "stdm", "cad", "filtCode", "return_to", "return_from"]);

function setTransitParams(org_transit, change_transit_obj, fromstart)
{   // change_transit_obj: {"alg_key": <new value>, ..}
    var t = buildTransitParam(org_transit, change_transit_obj);
    if (fromstart && t.length && t.substr(0,1) == "&")
    {   // haal het eerste &-teken weg
        t = t.replace("&", "");
    }
    return t;
}

function getAlgLevel(p_autoopen, p_startlevel, p_plaats)
{
    if (p_autoopen > 0) {
        if (p_plaats.bekend.alg_level >= p_autoopen) { // We weten het schijnbaar al nauwkeuriger dan 'autoopen'
            return p_plaats.bekend.alg_level + 1;
        } else {
            return p_autoopen;
        }
    }
    var v_level = p_startlevel;
    if (p_plaats.ruimte_key > -1)
        v_level = 6; // Werkplek -> Unsupported
    else if (p_plaats.verdieping_key > -1)
        v_level = 5; // Ruimte
    else if (p_plaats.gebouw_key > -1)
        v_level = 4; // Verdieping
    else if (p_plaats.locatie_key > -1)
        v_level = 3; // Gebouw of terrein..
    else if (p_plaats.district_key > -1)
        v_level = 2; // Locatie
    else if (p_plaats.regio_key > -1)
        v_level = 1; // District

    if (v_level < p_startlevel)
        v_level = p_startlevel;

    return v_level;
}

function getBekendLevel(p_plaats)
{
    var min_level = -1;
    if (p_plaats.ruimte_key > -1)
        min_level = 5;
    else if (p_plaats.verdieping_key > -1)
        min_level = 4;
    else if (p_plaats.gebouw_key > -1)
        min_level = 3;
    else if (p_plaats.locatie_key > -1)
        min_level = 2;
    else if (p_plaats.district_key > -1)
        min_level = 1;
    else if (p_plaats.regio_key > -1)
        min_level = 0;

    return getZoeklijst(min_level);
}

// TODO: verbeteren of bij ./User.inc erbij
function getDistrictKey()
{
  var initLoc = prs_obj.alg_locatie_key({ "withcurrent": werkLocatieAsDefault });
  if (initLoc == -1)
      return -1;

  var sqlDis = "SELECT alg_district_key FROM alg_v_aanweziglocatie WHERE alg_locatie_key = " + initLoc;
  var oRsDis = Oracle.Execute(sqlDis);
  var initDis = oRsDis(0).Value || -1;
  oRsDis.Close();
  return initDis;
}

function getZoeklijst(p_alg_level)
{
    var v_obj = { alg_level: p_alg_level };
    switch(p_alg_level)
    {
        case 6: v_obj.keyName = "wrk_key"; v_obj.subject = L("lcl_select_werkplek");   v_obj.initKey = -1;                                                                                                                       break;
        case 5: v_obj.keyName = "roo_key"; v_obj.subject = L("lcl_select_ruimte");     v_obj.initKey = prs_obj.alg_ruimte_key({ "withcurrent": werkLocatieAsDefault });     v_obj.prefix = "r"; v_obj.field = "alg_ruimte_key";     v_obj.view = "fac_v_my_rooms";      break;
        case 4: v_obj.keyName = "flr_key"; v_obj.subject = L("lcl_select_verdieping"); v_obj.initKey = prs_obj.alg_verdieping_key({ "withcurrent": werkLocatieAsDefault }); v_obj.prefix = "v"; v_obj.field = "alg_verdieping_key"; v_obj.view = "fac_v_my_floors";     break;
        case 3: v_obj.keyName = "bld_key"; v_obj.subject = L("lcl_select_gebouw");     v_obj.initKey = prs_obj.alg_gebouw_key({ "withcurrent": werkLocatieAsDefault });     v_obj.prefix = "g"; v_obj.field = "alg_gebouw_key";     v_obj.view = "fac_v_my_buildings";  break;
        case 2: v_obj.keyName = "loc_key"; v_obj.subject = L("lcl_select_locatie");    v_obj.initKey = prs_obj.alg_locatie_key({ "withcurrent": werkLocatieAsDefault });    v_obj.prefix = "l"; v_obj.field = "alg_locatie_key";    v_obj.view = "fac_v_my_locations";  break;
        case 1: v_obj.keyName = "dis_key"; v_obj.subject = L("lcl_select_district");   v_obj.initKey = getDistrictKey();                                                    v_obj.prefix = "d"; v_obj.field = "alg_district_key";   v_obj.view = "fac_v_my_districts";  break;
        case 0: v_obj.keyName = "reg_key"; v_obj.subject = L("lcl_select_regio");      v_obj.initKey = -1;                                                                  v_obj.prefix = "d"; v_obj.field = "alg_regio_key";      v_obj.view = "fac_v_my_regions";    break;
    }
    return v_obj;
}

function getOnroerendgoed(p_onrgoed)
{
    var alg_type, alg_key;
    switch(p_onrgoed.bekend.alg_level) {
        case 5: alg_type = "'R'";     alg_key = p_onrgoed.ruimte_key;      break;
        case 4: alg_type = "'V'";     alg_key = p_onrgoed.verdieping_key;  break;
        case 3: alg_type = "'G','T'"; alg_key = p_onrgoed.gebouw_key;      break;  // terrein key staat in gebouw!
        case 2: alg_type = "'L'";     alg_key = p_onrgoed.locatie_key;     break;
        case 1: alg_type = "'D'";     alg_key = p_onrgoed.district_key;    break;
    }

    var onrgoed_geg = [ {key: -1, oms: ""}, {key: -1, oms: ""}, {key: -1, oms: ""}, {key: -1, oms: ""}
                      , {key: -1, oms: ""}, {key: -1, oms: ""}, {key: -1, oms: ""}
                      ]

    if (alg_key > 0)
    {
        var sql = "SELECT *"
                + "  FROM (SELECT 'D'                 alg_type"
                    + "         , d.alg_district_key  onrgoed_keys"
                    + "         , d.alg_district_key  dis_key"
                    + "         , NULL                loc_key"
                    + "         , NULL                bld_key"
                    + "         , NULL                flr_key"
                    + "         , NULL                roo_key"
                    + "         , d.alg_district_omschrijving"                    + " dis_oms"
                    + "         , " + safe.quoted_sql(L("lcl_select_locatie"))    + " loc_oms"
                    + "         , " + safe.quoted_sql(L("lcl_select_gebouw"))     + " bld_oms"
                    + "         , " + safe.quoted_sql(L("lcl_select_verdieping")) + " flr_oms"
                    + "         , " + safe.quoted_sql(L("lcl_select_ruimte"))     + " roo_oms"
                    + "      FROM alg_district d"
                    + "     UNION"
                    + "    SELECT 'L' alg_type"
                    + "         , l.alg_locatie_key   onrgoed_keys"
                    + "         , l.alg_district_key  dis_key"
                    + "         , l.alg_locatie_key   loc_key"
                    + "         , NULL                bld_key"
                    + "         , NULL                flr_key"
                    + "         , NULL                roo_key"
                    + "         , l.alg_district_omschrijving dis_oms"
                    + "         , l.alg_locatie_omschrijving loc_oms"
                    + "         , " + safe.quoted_sql(L("lcl_select_gebouw"))     + " bld_oms"
                    + "         , " + safe.quoted_sql(L("lcl_select_verdieping")) + " flr_oms"
                    + "         , " + safe.quoted_sql(L("lcl_select_ruimte"))     + " roo_oms"
                    + "      FROM alg_v_locatie_gegevens l"
                    + "     UNION"
                    + "    SELECT o.alg_type"
                    + "         , o.alg_onroerendgoed_keys onrgoed_keys"
                    + "         , l.alg_district_key       dis_key"
                    + "         , l.alg_locatie_key        loc_key"
                    + "         , o.alg_gebouw_key         bld_key"
                    + "         , o.alg_verdieping_key     flr_key"
                    + "         , o.alg_ruimte_key         roo_key"
                    + "         , l.alg_district_omschrijving dis_key"
                    + "         , l.alg_locatie_omschrijving loc_oms"
                    + "         , CASE WHEN (o.alg_gebouw_key IS NULL AND o.alg_terreinsector_key IS NULL)"
                    + "                THEN " + safe.quoted_sql(L("lcl_select_gebouw"))
                    + "                ELSE CASE WHEN o.alg_terreinsector_key IS NULL"
                    + "                          THEN o.alg_gebouw_code ||' '|| o.alg_gebouw_naam"  // eigenlijk S("alg_geb_string")
                    + "                          ELSE o.alg_terreinsector_code ||' '|| o.alg_terreinsector_naam"  // eigenlijk S("alg_ter_string")
                    + "                     END"
                    + "            END bld_oms"
                    + "         , CASE WHEN o.alg_verdieping_key IS NULL"
                    + "                THEN " + safe.quoted_sql(L("lcl_select_verdieping"))
                    + "                ELSE o.alg_verdieping_omschrijving"// eigenlijk S("alg_ver_string")
                    + "            END flr_oms"
                    + "         , CASE WHEN o.alg_ruimte_nr IS NULL"
                    + "                THEN " + safe.quoted_sql(L("lcl_select_ruimte"))
                    + "                ELSE o.alg_ruimte_nr ||' '|| o.alg_ruimte_omschrijving"// eigenlijk S("alg_rui_string")
                    + "            END roo_oms"
                    + "      FROM alg_v_allonrgoed_gegevens o"
                    + "         , alg_v_locatie_gegevens l"
                    + "     WHERE o.alg_locatie_key = l.alg_locatie_key"
                + "       )"
                + " WHERE alg_type IN (" + alg_type + ")"
                + (alg_key > 0 ? " AND onrgoed_keys = " + alg_key : "");
        var oRs = Oracle.Execute(sql);
        if (!oRs.eof)
        {
            onrgoed_geg = [ {key: -1, oms: ""}
                          , {key: oRs("dis_key").Value, oms: oRs("dis_oms").Value}
                          , {key: oRs("loc_key").Value, oms: oRs("loc_oms").Value}
                          , {key: oRs("bld_key").Value, oms: oRs("bld_oms").Value}
                          , {key: oRs("flr_key").Value, oms: oRs("flr_oms").Value}
                          , {key: oRs("roo_key").Value, oms: oRs("roo_oms").Value}
                          , {key: -1, oms: "" }
                          ]
        }
        oRs.Close();
    }
    return onrgoed_geg;
}

function getLevelSql(p_alg_level, filt_params, key_params)
{
    function withAutorisationLevel(autlevel, key_params)
    {
        if (filt_params.autlevel == -1) // Authorized for all
        {
            sql_a = " AND 1 = 1";
        }
        else if (filt_params.autlevel >= 0 && filt_params.autlevel <= 5)
        {
            sql_a = " AND " + key_params.lijst.prefix + "." + key_params.lijst.field + " IN"
                  + " (SELECT " + key_params.lijst.field
                  + "    FROM " + key_params.lijst.view
                  + "   WHERE prs_perslid_key = " + user_key
                  + "     AND niveau = " + filt_params.autlevel
                  + " )";
        }
        else if (filt_params.autlevel > 5) // Not authorized
        {
            sql_a = " AND 1 = 0";
        }

        return sql_a;
    }

    var filtClause;
    var sql_p = "";
    var level_sql = "";
    if (key_params.bekend.alg_level > -1) {
        level_sql = " AND " + key_params.bekend.prefix + "." + key_params.bekend.field + " = ";
        switch(key_params.bekend.alg_level) {
            case 5: level_sql += key_params.ruimte_key;     break;
            case 4: level_sql += key_params.verdieping_key; break;
            case 3: level_sql += key_params.gebouw_key;     break;
            case 2: level_sql += key_params.locatie_key;    break;
            case 1: level_sql += key_params.district_key;   break;
            case 0: level_sql += key_params.regio_key;      break;
        }
    }

    switch(alg_level) {
        case 6: break;
        case 5:
            if (filt_params.bCad)
            {
                var sqlv = "SELECT 1"
                         + "  FROM cad_tekening ct"
                         + " WHERE ct.alg_verdieping_key = " + key_params.verdieping_key;
                var oRs = Oracle.Execute(sqlv);
                if (!oRs.Eof)
                {
                    sql_p = "SELECT -2 roo_key"
                          + "     , " + safe.qL("lcl_point_cad") + " naam"
                          + "     , 1 volgorde"
                          + "     , 'R' alg_type"
                          + "  FROM DUAL"
                          + " UNION ";
                }
                oRs.Close();
            }

            filtClause = getFiltClauseRuimte(filt_params.filtCode, filt_params.filtParams);
            sql_p = sql_p
                  + "SELECT DISTINCT"
                  + "       r.alg_ruimte_key roo_key"
                  + "     , " + S("alg_rui_string") + " naam"
                  + "     , 2 volgorde"
                  + "     , 'R' alg_type"
                  + "  FROM alg_v_aanwezigruimte r"
                  + "     , alg_v_aanwezigsrtruimte sr"
                  + "     , alg_v_aanwezigverdieping v"
                  + "     , alg_v_aanweziggebouw g"
                  + "     , alg_v_aanweziglocatie l"
                  + "     , alg_v_aanwezigdistrict d"
                  + (filtClause.from || "")
                  + " WHERE v.alg_verdieping_key = r.alg_verdieping_key"
                  + "   AND sr.alg_srtruimte_key(+) = r.alg_srtruimte_key"
                  + "   AND g.alg_gebouw_key = v.alg_gebouw_key"
                  + "   AND l.alg_locatie_key = g.alg_locatie_key"
                  + "   AND d.alg_district_key = l.alg_district_key"
                  + "   AND (l.alg_locatie_vervaldatum IS NULL OR l.alg_locatie_vervaldatum > TRUNC(SYSDATE))"
                  + "   AND (g.alg_gebouw_vervaldatum IS NULL OR g.alg_gebouw_vervaldatum > TRUNC(SYSDATE))"
                  + level_sql
                  + (filtClause.where || "")
                  + withAutorisationLevel(filt_params.autlevel, key_params);
            break;
        case 4:
            filtClause = getFiltClauseVerdieping(filt_params.filtCode, filt_params.filtParams);
            sql_p = "SELECT DISTINCT"
                  + "       v.alg_verdieping_key flr_key"
                  + "     , " + S("alg_ver_string") + " naam"
                  + "     , v.alg_verdieping_volgnr volgorde"  // de sortering..
                  + "     , 'V' alg_type"
                  + "  FROM alg_v_aanwezigverdieping v"
                  + "     , alg_v_aanweziggebouw g"
                  + "     , alg_v_aanweziglocatie l"
                  + "     , alg_v_aanwezigdistrict d"
                  + (filtClause.from || "")
                  + " WHERE g.alg_gebouw_key = v.alg_gebouw_key"
                  + "   AND l.alg_locatie_key = g.alg_locatie_key"
                  + "   AND d.alg_district_key = l.alg_district_key"
                  + "   AND (l.alg_locatie_vervaldatum IS NULL OR l.alg_locatie_vervaldatum > TRUNC(SYSDATE))"
                  + "   AND (g.alg_gebouw_vervaldatum IS NULL OR g.alg_gebouw_vervaldatum > TRUNC(SYSDATE))"
                  + level_sql
                  + (filtClause.where || "")
                  + withAutorisationLevel(filt_params.autlevel, key_params);
            break;
        case 3:
            filtClause = getFiltClauseGebouw(filt_params.filtCode, filt_params.filtParams);
            var sql_g = "SELECT DISTINCT"
                      + "       g.alg_gebouw_key bld_key"
                      + "     , " + S("alg_geb_string") + " naam"
                      + "     , 1 volgorde"
                      + "     , 'G' alg_type"
                      + "  FROM alg_v_aanweziggebouw g"
                      + "     , alg_srtgebouw sg"
                      + "     , alg_v_aanweziglocatie l"
                      + "     , alg_v_aanwezigdistrict d"
                      + (filtClause.from || "")
                      + " WHERE l.alg_locatie_key = g.alg_locatie_key"
                      + "   AND g.alg_srtgebouw_key = sg.alg_srtgebouw_key(+)"
                      + "   AND d.alg_district_key = l.alg_district_key"
                      + level_sql
                      + "   AND (l.alg_locatie_vervaldatum IS NULL OR l.alg_locatie_vervaldatum > TRUNC(SYSDATE))"
                      + "   AND (g.alg_gebouw_vervaldatum IS NULL OR g.alg_gebouw_vervaldatum > TRUNC(SYSDATE))"
                      + (filtClause.where_geb || "")
                      + withAutorisationLevel(filt_params.autlevel, key_params);

            if (filt_params.eindlevel > 3) {
                sql_p = sql_g; // Als we een Verdieping of Ruimte nodig hebben moeten we geen Terreinen aanbieden
            } else {
                var sql_t = "SELECT DISTINCT"
                        + "       t.alg_terreinsector_key bld_key"
                        + "     , " + S("alg_ter_string") + " naam"
                        + "     , 2 volgorde"
                        + "     , 'T' alg_type"
                        + "  FROM alg_v_aanwezigterreinsector t"
                        + "     , alg_srtterreinsector st"
                        + "     , alg_v_aanweziglocatie l"
                        + "     , alg_v_aanwezigdistrict d"
                        + (filtClause.from || "")
                        + " WHERE l.alg_locatie_key = t.alg_locatie_key"
                        + "   AND t.alg_srtterreinsector_key = st.alg_srtterreinsector_key(+)"
                        + "   AND d.alg_district_key = l.alg_district_key"
                        + "   AND (l.alg_locatie_vervaldatum IS NULL OR l.alg_locatie_vervaldatum > TRUNC(SYSDATE))"
                        + "   AND (t.alg_terreinsector_vervaldatum IS NULL OR t.alg_terreinsector_vervaldatum > TRUNC(SYSDATE))"
                        + (key_params.bekend.alg_level == 3 ? "   AND t.alg_terreinsector_key = " + key_params.gebouw_key : level_sql)
                        + (filtClause.where_ter || "");
                switch (binding)
                {
                    case "G": sql_p = sql_g; break;
                    case "T": sql_p = sql_t; break;                     // de T-case werkt volgens mij niet
                    default:  sql_p = sql_g + " UNION " + sql_t;        // de T-case werkt volgens mij niet
                }
            }
            break;
        case 2:
            // We willen met eindlevel == 2 ook locaties vinden zonder gebouwen.
            if (filt_params.filtCode == "EXP" && filt_params.eindlevel == 2)
                filtClause = { from: "", where: "" };
            else
                filtClause = getFiltClauseLocatie(filt_params.filtCode, filt_params.filtParams);
            sql_p = "SELECT l.alg_locatie_key loc_key"
                  + "     , " + S("alg_loc_string") + " naam"
                  + "     , 1 volgorde"
                  + "     , 'L' alg_type"
                  + "     , alg_locatie_plaats"
                  + "  FROM alg_v_aanweziglocatie l"
                  + "     , alg_v_aanwezigdistrict d"
                  + (filtClause.from || "")
                  + " WHERE d.alg_district_key = l.alg_district_key"
                  + "   AND (l.alg_locatie_vervaldatum IS NULL OR l.alg_locatie_vervaldatum > TRUNC(SYSDATE))"
                  + level_sql
                  + (filtClause.where || "")
                  + withAutorisationLevel(filt_params.autlevel, key_params);
            break;
        case 1:
            filtClause = getFiltClauseDistrict(filt_params.filtCode, filt_params.filtParams);
            sql_p = "SELECT d.alg_district_key dis_key"
                  + "     , d.alg_district_omschrijving naam"
                  + "     , 1 volgorde"
                  + "     , 'D' alg_type"
                  + "  FROM alg_v_aanwezigdistrict d"
                  + " WHERE 1 = 1"
                  + level_sql
                  + (filtClause || "")
                  + withAutorisationLevel(filt_params.autlevel, key_params);
            break;
        case 0: break;
    }

    sql_p += " ORDER BY 3, 2";

    return sql_p;
}

plaats_params.bekend = getBekendLevel(plaats_params);
var alg_level = getAlgLevel(autoopen, startlevel, plaats_params); // Welk niveau moeten we nu kiezen?

if (alg_level > eindlevel) {
    Response.Redirect(return_to);
} else if (alg_level <= eindlevel && alg_level >= startlevel) {
    var filter_params = { bCad:       bCad
                        , filtCode:   filtCode
                        , filtParams: { "act_key": act_key,
                                        "stdm_key": stdm_key,
                                        "terrein": true }
                        , startlevel: startlevel
                        , eindlevel:  eindlevel
                        , autlevel:   autlevel
                        };
    plaats_params.onrgoed = getOnroerendgoed(plaats_params);
    plaats_params.lijst = getZoeklijst(alg_level);

    var sql_p = getLevelSql(alg_level, filter_params, plaats_params);

    var sql_count = "SELECT COUNT(1)"
                  + "  FROM (" + sql_p + ")";
    var oRs_count = Oracle.Execute(sql_count);
    if (oRs_count(0).Value == 1)
    {   // Op dit niveau is er exact 1 gevonden. Ga naar volgend niveau.
        oRs_count.Close();
        var oRs_p = Oracle.Execute(sql_p);
        var url = fnLink(oRs_p);
        oRs_p.Close();
        Response.Redirect(url);
    }
    else
    {   // Op dit niveau zijn er meerdere mogelijkheden. Maak een keuzelijst.
        oRs_count.Close();
    }
}
else
{
  Response.Write("alg_level < startlevel...?");
}



%>
<html>
  <head>
<% FCLTMHeader.Generate({ title: plaats_params.lijst.subject }); %>
<script>
        function show_level(p_level)
        {
            var url = "<%=rooturl%>/appl/pda/plaatsselector.asp"
                    + "?autoopen=" + p_level
                    + "<%=fltTransitParam%>";
<%          if (plaats_params.bekend.alg_level > -1) { %>
            var dit_level    = p_level - 1;
            var bekend_level = <%=plaats_params.bekend.alg_level%>;
            var bekend_onrgd = <%=JSON.stringify(plaats_params.onrgoed)%>;
            if (bekend_level < dit_level)
                dit_level = bekend_level;
            if (dit_level >= <%=startlevel%> && dit_level < 6) {
                var dit_pname = [null, null, "loc_key", "bld_key", "flr_key", "roo_key"][dit_level];
                url += "&"+ dit_pname + "=" + bekend_onrgd[dit_level].key;
            }
<%          } %>
            window.location.replace(url);
            return false;
        }
</script>
  </head>
  <body>
<%
    PAGE_START({id: "page-plaats-"+alg_level});
      HEADER({title: plaats_params.lijst.subject, back:!qrc, home:!qrc });
      CONTENT_START();

        function fnIcon(oRs) {
            if (oRs("naam").value == L("lcl_point_cad")) {
                return I("fa-map-marker-alt");
            }
            return null;
        }
        function fnLink(oRs)
        {
          var alg_key     = oRs(plaats_params.lijst.keyName).Value;
          var changeparam = '{"' + plaats_params.lijst.keyName + '": ' + alg_key + '}';

          if (alg_key == -2 && bCad && alg_level == 5)
          {
            return  "../pda/plattegrond.asp?mode=0&scenario_key=0&nothema=1"
                  + "&verdieping_key=" + plaats_params.verdieping_key
                  + "&return_to=" + safe.url(return_to)
                  + "&zoom=" + zoom;
          }
          else
          {
            var alg_type = oRs("alg_type").Value;
            if (alg_type == "T")
                eindlevel = 3;
            return ( alg_level == eindlevel
                   ? return_to + "&alg_type=" + alg_type + "&return_from=" + safe.url(return_from) + setTransitParams(extTransitParam_arr, JSON.parse(changeparam), false)
                   : "?autoopen="+(alg_level+1) + setTransitParams(transitParam_arr, JSON.parse(changeparam), false)
                   );
          }
        }

        var plaats = new ResultsetTable({ sql: sql_p
                                        , keyColumn: plaats_params.lijst.keyName
                                        , initKey:   plaats_params.lijst.initKey
                                        , linkColumn: fnLink
                                        , iconColumn: fnIcon
                                        , headerColumn: "naam"
                                        , ID: "plaatstable"
                                        , showAll: true
                                        , emptyWhenNoResult: true
                                        });


        var beschikbaar = true;
        for (var i=startlevel; i<=eindlevel; i++)
        {
            if (i == alg_level)
            {
                beschikbaar = plaats.processResultset() != -1;
            }
            else
            {
                if (beschikbaar)
                {
                    var btn_text = "";
                    switch (i)
                    {
                        case 2: btn_text = "<b>"+L("lcl_location")+ ": </b>" + safe.html(plaats_params.onrgoed[2].oms);
                                BUTTON(btn_text, {click: "show_level(2)", icon: "fa-location-arrow"});
                                break;
                        case 3: btn_text = "<b>"+L("lcl_building")+ ": </b>" + safe.html(plaats_params.onrgoed[3].oms);
                                BUTTON(btn_text, {click: "show_level(3)", icon: "fa-building"});
                                break;
                        case 4: btn_text = "<b>"+L("lcl_floor")   + ": </b>" + safe.html(plaats_params.onrgoed[4].oms);
                                BUTTON(btn_text, {click: "show_level(4)", icon: "fa-th-list"});
                                break;
                        case 5: btn_text = "<b>"+L("lcl_room")    + ": </b>" + safe.html(plaats_params.onrgoed[5].oms);
                                BUTTON(btn_text, {click: "show_level(5)", icon: "fa-map-marker"});
                                break;
                    }
                }
            }
        }

      CONTENT_END();
      FOOTER();
    PAGE_END();
PDA_PAGE_END(); %>
	</body>
</html>
<% ASPPAGE_END(); %>