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

    File:               SuggestVerdieping.asp
    Description:        Suggereer verdiepingen
    Parameters:         k             De eerste paar letters
                        filtcode beperk de lijst met getoonde locaties TO BE DEFINED

                        autlevel    : optioneel: schrijfrechten op dit niveau nodig
    Globals:
    Context:
    Note:
*/ %>

<%
JSON_Result = true;
%>

<!-- #include file="../../Shared/common.inc" -->
<!-- #include file="./suggest.inc" -->
<!-- #include file="./plaatsFilter.inc" -->

<%
var chars = getQParam("k", "");
var autlevel = getQParamInt("autlevel", -1);
var bAll = getQParamInt("SuggestAll", 0) == 1;
var regkey = getQParamInt("regkey", "-1");
var diskey = getQParamInt("diskey", "-1");
var lockey = getQParamInt("lockey", "-1");
var gebkey = getQParamInt("gebkey", "-1");
var filtcode = getQParam("filtcode", "");
var act_key = getQParamInt("act_key", -1);

var params = { act_key: act_key };

chars = chars.replace(/\*/g,"%");

var filtClause = getFiltClauseVerdieping(filtcode, params);

if (gebkey > 0)
{ // Gebouw (parent) gegeven
  filtClause.where += " AND v.alg_gebouw_key = " + gebkey;
}
else if (lockey > 0)
{ // Locatie gegeven
  filtClause.where += " AND v.alg_gebouw_key IN"
                    + " (SELECT g.alg_gebouw_key"
                    + " FROM alg_v_aanweziggebouw g"
                    + " WHERE g.alg_locatie_key = " + lockey + ")";
}
else if (diskey > 0)
{ // District gegeven
  filtClause.where += " AND v.alg_gebouw_key IN"
                    + " (SELECT g.alg_gebouw_key"
                    + " FROM alg_v_aanweziggebouw g"
                    + ", alg_v_aanweziglocatie l"
                    + " WHERE g.alg_locatie_key = l.alg_locatie_key"
                    + " AND l.alg_district_key = " + diskey + ")";
}
else if (regkey > 0)
{ // Regio gegeven
  filtClause.where += " AND v.alg_gebouw_key IN"
                    + " (SELECT g.alg_gebouw_key"
                    + " FROM alg_v_aanweziggebouw g"
                    + ", alg_v_aanweziglocatie l"
                    + ", alg_v_aanwezigdistrict d"
                    + " WHERE g.alg_locatie_key = l.alg_locatie_key"
                    + " AND l.alg_district_key = d.alg_district_key"
                    + " AND d.alg_regio_key = " + regkey + ")";
}


if (autlevel > -1) // Efficiente versie van FAC_V_MY_PR**S_AFDELINGEN_WRITE
{
  filtClause.where +=" AND v.alg_verdieping_key IN"
                    + " (SELECT alg_verdieping_key FROM fac_v_my_floors"
                    + " WHERE prs_perslid_key = " + user_key
                    + " AND niveau = " + autlevel + ") ";
}

    sql = "SELECT v.alg_verdieping_key"
        + (gebkey > -1 ?", v.alg_verdieping_code" : ", g.alg_gebouw_code||'-'||v.alg_verdieping_code") + " code"
        + ",      v.alg_verdieping_omschrijving naam"
        + "  FROM alg_v_aanwezigverdieping v"
        + "     , alg_gebouw g"
        + filtClause.from
        + " WHERE (UPPER(v.alg_verdieping_code) LIKE " + safe.quoted_sql_wild(chars + "%")
        + "    OR UPPER(v.alg_verdieping_omschrijving) LIKE " + safe.quoted_sql_wild(chars + "%") + ")"
        + filtClause.where
        + "   AND g.alg_gebouw_key = v.alg_gebouw_key"
        + " ORDER BY g.alg_gebouw_code, v.alg_verdieping_volgnr";

WriteResult(sql, bAll || gebkey > -1, "naam", "alg_verdieping_key", "code", "code");
%>
