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

    File:               SuggestDistrict.asp
    Description:        Suggereer districten
    Parameters:         k             De eerste paar letters
                        autlevel    : optioneel: rechten 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 filtcode = getQParam("filtcode", "");

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

var filtClause = getFiltClauseDistrict(filtcode);

if (regkey > 0)
{ // Regio (parent) gegeven
  filtClause += " AND d.alg_regio_key = " + regkey;
}


if (autlevel > -1)
{
      filtClause +=" AND alg_district_key IN "
                 + "(SELECT alg_district_key FROM fac_v_my_districts "
                 + " WHERE prs_perslid_key = " + user_key
                 + " AND niveau = " + autlevel + ") ";
}

sql = "SELECT d.alg_district_key"
    + ", d.alg_district_omschrijving naam"
    + ", r.alg_regio_omschrijving"
    + ", NULL extra "
    + "  FROM alg_v_aanwezigdistrict d"
    + ", alg_v_aanwezigregio r"
    + " WHERE d.alg_regio_key = r.alg_regio_key"
    + " AND alg_district_upper LIKE " + safe.quoted_sql_wild(chars + "%")
    + filtClause
    + " ORDER BY 2"
WriteResult(sql, bAll, "naam", "alg_district_key", "alg_regio_omschrijving", "extra");
%>
