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

    File:               SuggestVerdieping.asp
    Description:        Suggereer verdiepingen
    Parameters:         k             De eerste paar letters
                        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 stdm_key = getQParamInt("stdm_key", -1);
var person_key = getQParamInt("personfor_key", -1);
if (person_key == -1) person_key = getQParamInt("person_key", -1);
var issuer_search = getQParamInt("issuer_search", 0);
var search_person = (person_key > 0 && issuer_search == 1);
if (person_key > 0 && issuer_search == 1) { autlevel = new Perslid(person_key).checkAutorisation("WEB_MLDUSE").ALGwritelevel; }
var scope = getQParamInt("scope", 0);

var params = { act_key: act_key, stdm_key: stdm_key, scope:scope };

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)
{
  filtClause.where +=" AND v.alg_verdieping_key IN"
                    + " (SELECT alg_verdieping_key FROM fac_v_my_floors"
                    + "   WHERE prs_perslid_key = " + (search_person ? person_key : user_key)
                    + "     AND niveau = " + autlevel + ") ";
}

// Als gebouw bekend is kies je uit de verdiepingnummers, maar anders prefixen met de gebouwcode
var zoeksqlstring = (gebkey > 0
                    ? S("alg_ver_string")
                    : " g.alg_gebouw_code || '-' || " + S("alg_ver_string")
                    );

    sql = "SELECT v.alg_verdieping_key"
        + ", " + zoeksqlstring + " code"
        + ",      v.alg_verdieping_omschrijving naam"
        + "  FROM alg_v_aanwezigverdieping v"
        + ",      alg_gebouw g"
        + filtClause.from
        + " WHERE (UPPER("+zoeksqlstring+") LIKE " + safe.quoted_sql_wild2(chars) + ") "
        + filtClause.where
        + "   AND g.alg_gebouw_key = v.alg_gebouw_key"
        + " ORDER BY g.alg_gebouw_code, v.alg_verdieping_volgnr";

WriteResult2(sql, { bAll:          bAll,
                    bForcedAll:    gebkey > -1,
                    fldName:       "code",
                    fldKey:        "alg_verdieping_key",
                    fldDesc:       "naam",
                    fldExtraParam: "code"  // waarvoor?
                  }
              );

%>
<% ASPPAGE_END(); %>
