<%@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 fac_function_key of -1 als alles toegestaan
                        autRW: verplicht indien autfunctionKey > -1 is. Geeft aan of er read or write autorisatie geldt.
                        autfunction : verplicht:
    Globals:
    Context:
    Note:
    TODO: Vergelijk autorisatie in ../MLD/UploadDisciplines.asp
*/ %>

<%
JSON_Result = true;
%>

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

<%
var chars = getQParam("k", "");
var autfunctionKey = getQParamInt("autfunctionKey");
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 module = getQParam("module");
var ins_key = getQParamInt("ins_key", -1);
var extracode = getQParam("extracode", "");
var perslidKey = getQParamInt("perslidKey"); // Voor wie is de melding
var filtcode = getQParam("filtcode", "");

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

  return "SELECT DISTINCT td.ins_discipline_key"
       + ", CASE "
         + " WHEN sd.ins_srtdiscipline_prefix IS NULL"
         + " THEN ''"
         + " ELSE sd.ins_srtdiscipline_prefix || '-'"
         + " END || " + lcl.xsql('td.ins_discipline_omschrijving', 'td.ins_discipline_key') + " discipline"
         + ", " + lcl.xsqla('sd.ins_srtdiscipline_omschrijving', 'sd.ins_srtdiscipline_key')
       + extraInf.selectstr
       + " 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_wild("%" + chars + "%")
         + " OR UPPER(" + lcl.xsql('sd.ins_srtdiscipline_omschrijving', 'sd.ins_srtdiscipline_key') + ") LIKE " + safe.quoted_sql_wild("%" + chars + "%") +")"
       + filtClause.where
       + extraInf.wherestr;
}

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

var params = { autfunctionKey: autfunctionKey,
               autRW: autRW,
               srtdisc_key: srtdisc_key,
               disc_key: disc_key,
               ins_key: ins_key,
               perslidKey: perslidKey };

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 discipline";

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