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

    File:               fac_usrrap_worker.asp (JSON result)
    Description:        Get all data to fill a filter-list-box
    Parameters:
        colName         Columname to create listbox for
        usrrap_key      Key to user report table
    Context:            onClick on question mark next to filter field

*/
var JSON_Result = true;
%>

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="./fac_usrrap.inc" -->
<!-- #include file="../Shared/json2.js" -->

<%
    var p_colName      = getQParam("colName");
    var usrrap_key     = getQParamInt("usrrap_key");
    var aantalcolvalue = getQParamInt("aantalcolvalue", 0);

    var fac_usrrap = usrrap.fac_usrrap_info(usrrap_key);

    var p_tableName = fac_usrrap.view_name;
    var func_mode = fac_usrrap.functie;

    if (func_mode&8) {
        p_tableName = usrrap.tabelizedViewname(p_tableName);
        func_mode = func_mode - 8;  // bitje verbruikt
    }

    var i = p_tableName.indexOf('.'); // Naam kan met care. beginnen. Dat moet er eerst af.
    if (i != -1){
        p_tableName = p_tableName.substring(i+1);
    }

    usrrap.requireFilterCol(p_tableName, p_colName); // Safety check

    var sqlWhere = "";
    if (fac_usrrap.fac_functie_key)
        sqlWhere = usrrap.create3Dclause(p_tableName, fac_usrrap.fac_functie_key);

    sql = "SELECT DISTINCT(" + p_colName + ") FROM " + p_tableName;
    if (sqlWhere != "")
        sqlWhere = " WHERE " + sqlWhere;

    // Andere ingevulde filterwaarden meenemen in de query (WHERE/AND)
    for (var i = 1; i <= aantalcolvalue; i++)
    {
      var colname  = getQParam("colname" + i);
      var colvalue  = getQParam("colvalue" + i);
      if (p_colName != colname)
      {
        usrrap.requireFilterCol(p_tableName, colname); // Safety check voor kolomnaam
        sqlWhere += (sqlWhere == ""? " WHERE " : " AND ") + " UPPER(" + colname + ") LIKE " + safe.quoted_sql_wild(colvalue);
      }
    }
    sql += sqlWhere;

    sql += " ORDER BY 1";
    oRs = Oracle.Execute(sql);

    var wd = []; // Worker Data
    while (!oRs.eof)
    {
        wd.push(oRs.Fields(0).value);
        oRs.MoveNext();
    }
    oRs.close();

    result = { wd: wd };
    Response.Write(JSON.stringify(result));
%>