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

    File:               fac_usrrap.asp
    Description:        Select a report and execute it
    Parameters:
      rap_name=WOCO_RAP_GEBOUW          // view name; does not need to be in FAC_USRRAP; can be a wildcard
      autoexec=1                        // in combination with rap_name
      autolistbox=1                     // in combination with rap_name
*/
%>

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<!-- #include file="../Shared/rapportselector.inc" -->

<%
FCLTHeader.Requires ({ plugins: [],
                       js: []
                     });

var p_bAutoexec = (getQParamInt("autoexec", 0) == 1)
var fclt_key = getQParamInt("fclt_key",  -1);
var p_fcode = getQParam("fcode", "");

var bSingleReport = false; // Autoexecute when single report
if (!p_bAutoexec)
{
  var sql;
  sql = "SELECT count(*) aantal, MIN(fac_usrrap_key) usrrap_key"
      + "  FROM fac_usrrap "
      + "  WHERE (fac_functie_key IS NULL"
      + "     OR fac_functie_key IN "
      + "             (SELECT fac_functie_key FROM fac_v_webgebruiker"
      + "                WHERE prs_perslid_key = " + user_key + "))";
  var oRs = Oracle.Execute(sql);
  if (oRs("aantal").value == 1)
    Response.Redirect("fac_usrrap_search.asp?usrrap_key=" + oRs("usrrap_key").Value);
}
%>
<html>
  <head>
<%  FCLTHeader.Generate() %>
    <script type="text/javascript">

      // After a report has been selected show the description and filter fields
      // for this report in filterframe
      function emptyFilterAndReport()
      {
        if ($("#usrrap_key").val() > 0)
            document.forms.u2.submit();
      }

    </script>
  </head>

  <body id="searchbody">
    <form name="u2" method="get" action="fac_usrrap_search.asp" onsubmit='return false'>
      <% SEARCH_PAGE_START({ header: L("lcl_filterblok") });
      SEARCH_BLOCK_START(); %>
            <input type="hidden" name="fclt_key" value="<%=fclt_key%>">
      <%
      if (!p_bAutoexec)
      { 
            // Select all reports the user has autorization for
            // FAC_USERRAP_FUNCTIE==0 -->Normal View
            // FAC_USERRAP_FUNCTIE==2 -->Stored procedure
            FCLTrapportselector("usrrap_key",
                                "sgRapport",
                                { label: L("lcl_usrrap_report"),
                                  rapportKey: -1,
                                  fcode: p_fcode,
                                  onChange: "emptyFilterAndReport",
                                  extraParamField: "autorefresh",
                                  required: true
                                });

          SEARCH_BLOCK_END();
          SEARCH_BLOCK_START();
      }
      else
      {
        usrrap_key = -1;
        sql  = "SELECT fac_usrrap_key"
             + " FROM fac_usrrap "
             + "  WHERE (fac_functie_key IS NULL"
             + "     OR fac_functie_key IN "
             + "             (SELECT fac_functie_key FROM fac_v_webgebruiker"
             + "                WHERE prs_perslid_key = " + user_key + "))"
             + (p_rap_name != ""
               ? " AND fac_usrrap_view_name = " + safe.quoted_sql(p_rap_name)
               : " AND 1 = 0");
        oRs = Oracle.Execute(sql);
        if (!oRs.eof)
        {
          usrrap_key = oRs("fac_usrrap_key").value;
        }
%>
        <input type="hidden" name="usrrap_key" value="<%=usrrap_key%>">
<%SEARCH_BLOCK_END();
      }
SEARCH_PAGE_END();%>
    </form>
  </body>
</html>
<% ASPPAGE_END(); %> 
