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

    File:               SuggestDiscipline.asp
    Description:        Suggereer Disciplines
    Parameters:         k             De eerste paar letters
                        filtcode beperk de lijst met getoonde locaties TO BE DEFINED
                        perslidKey  : verplicht  Voor wie is de discipline
                        autfunctionKey: verplicht string met fac_function_keys of -1 als alles toegestaan
                        autRW: verplicht indien autfunctionKey > -1 is. Geeft aan of er read or write autorisatie geldt.
    Globals:
    Context:
    Note:
*/ %>

<%
JSON_Result = true;
%>

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

<%
var chars = getQParam("k", "");
var autfunctionKeyArray = getQParamIntArray("autfunctionKey", null, true); // pAll = true want dan wordt de waarde -1 gewoon doorgegeven.
var autRW = getQParam("autRW", "W");
var bAll = getQParamInt("SuggestAll", 0) == 1;
var srtdisc_key = getQParamInt("srtdisc_key", -1);
var disc_key = getQParamInt("disc_key", -1);
var disc_old = getQParamInt("disc_old", -1);
var module = getQParam("module");
var ins_key_arr = getQParamIntArray("ins_key_arr", []);
var loc_key = getQParamInt("lockey", -1);
var bld_key = getQParamInt("bldkey", -1);
var flr_key = getQParamInt("flrkey", -1);
var room_key = getQParamInt("roomkey", -1);
var disc = getQParamInt("disc", -1);
var extracode = getQParam("extracode", "");
var perslidKey = getQParamInt("perslidKey"); // Voor wie is de melding
var behandelteam_key = getQParamInt("behandelteam_key", -1);
var filtcode = getQParam("filtcode", "");
var cndfilter = getQParam("cndfilter", 0) == 1;
var ref_type = getQParam("ref_type", "");   // In Case Of a new call for a contract

function getSql(chars, params, extracode)
{
  var filtClause = getFiltClauseDiscipline(filtcode, params);
  var extraInf = getExtraInfDiscipline(extracode);

  return "SELECT DISTINCT td.ins_discipline_key, "
       + (params.srtdisc_key > 0
         ? ""
         : "NVL2(sd.ins_srtdiscipline_prefix, sd.ins_srtdiscipline_prefix || '-', '') || ")
       + lcl.xsql('td.ins_discipline_omschrijving', 'td.ins_discipline_key') + " discipline"
       + ", " + lcl.xsqla('sd.ins_srtdiscipline_omschrijving', 'sd.ins_srtdiscipline_key')
       + extraInf.selectstr
       + ", ins_discipline_volgnr"
       + " FROM ins_tab_discipline td"
       + ", ins_srtdiscipline sd"
       + filtClause.from
       + extraInf.fromstr
       + " WHERE td.ins_srtdiscipline_key = sd.ins_srtdiscipline_key(+)" // Alleen bij MLD is de ins_srtdiscipline_key (altijd) ingevuld
       + "   AND td.ins_discipline_verwijder IS NULL"
       + "   AND (UPPER(sd.ins_srtdiscipline_prefix || '-' || " + lcl.xsql('td.ins_discipline_omschrijving', 'td.ins_discipline_key') +") LIKE " + safe.quoted_sql_wild2(chars)
         + "   OR UPPER(" + lcl.xsql('sd.ins_srtdiscipline_omschrijving', 'sd.ins_srtdiscipline_key') + ") LIKE " + safe.quoted_sql_wild2(chars) +")"
       + filtClause.where
       + (cndfilter
         ? " AND sd.ins_srtdiscipline_key != " + S("cnd_gebreken_srtdisc_key")
         : "")
       + extraInf.wherestr;
}

var chars = chars.replace(/\*/g,"%");
var mod = module.split(",");

var params = { autfunctionKeyArray: autfunctionKeyArray,
               autRW: autRW,
               srtdisc_key: srtdisc_key,
               disc_key: disc_key,
               disc_old: disc_old,
               disc: disc,
               ins_key_arr: ins_key_arr,
               loc_key: loc_key,
               bld_key: bld_key,
               flr_key: flr_key,
               room_key: room_key,
               perslidKey: perslidKey,
               behandelteam_key: behandelteam_key,
               ref_type: ref_type };

var sql = "";
if (mod.length == 0)
  sql = getSql(chars, params, extracode);
else
{
  for (var i = 0; i < mod.length; i++)
  {
    params.module = mod[i];
    sql += (sql != ""? " UNION " : "") + getSql(chars, params, extracode);
  }
}
sql += " ORDER BY ins_discipline_volgnr, discipline";

WriteResult(sql, bAll, "discipline", "ins_discipline_key", "ins_srtdiscipline_omschrijving", "extra");
%>
<% ASPPAGE_END(); %>
