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

    File:               SuggestLocatie.asp
    Description:        Suggereer locaties
    Parameters:         k             De eerste paar letters
                        filtcode beperk de lijst met getoonde locaties TO BE DEFINED
                        diskey      : optioneel de districtkey waarbinnen de locaties
                        autlevel    : optioneel: schrijfrechten op dit niveau nodig
    Globals:
    Context:
    Note:
    TODO:               extraInf updaten of verwijderen
*/ %>
<%
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 filtcode = getQParam("filtcode", "");
var extracode = getQParam("extraCode", "");
var act_key = getQParamInt("act_key", -1);

var params = { act_key: act_key };

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

var filtClause = getFiltClauseLocatie(filtcode, params);

var extraInf = ", 'Extra' extra";
if (extracode != "")
{
  switch(extracode)
  {
    case "C": extraInf = ", alg_locatie_plaats extra";
              break;
  }
}

if (diskey > 0)
{ // District (parent) gegeven
  filtClause.where += " AND alg_district_key = " + diskey;
}
else if (regkey > 0)
{ // Regio gegeven
  filtClause.where += " AND alg_district_key IN"
                    + " (SELECT alg_district_key"
                    + " FROM alg_v_aanwezigdistrict"
                    + " WHERE alg_regio_key = " + regkey + ")";
}

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

  sql = "SELECT l.alg_locatie_key"
      + ", " + S("alg_loc_string") + " naam"
      + ", alg_locatie_code code"
      + ", alg_locatie_postcode postcode"
      + extraInf
      + " FROM alg_v_aanweziglocatie l"
      + filtClause.from
      + " WHERE (UPPER(" + S("alg_loc_string") + ") LIKE " + safe.quoted_sql_wild(chars + "%")
      + "       OR UPPER(alg_locatie_code) LIKE " + safe.quoted_sql_wild(chars + "%") + ")"
      + filtClause.where
      + " ORDER BY 2"
  WriteResult(sql, bAll, "naam", "alg_locatie_key", "code", "postcode", "extra");
%>
