<%@language = "javascript" %>
<% /*
    $Revision$
    $Id$
    File:           mjb/mjb_search.asp
    Description:    Menuoptie MJOB
    Parameters:
    Context:
    Note:

*/ %>
<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<!-- #include file="../Shared/selector.inc" -->
<!-- #include file="../Shared/plaatsselector.inc" -->
<!-- #include file="../Shared/disciplineselector.inc" -->
<!-- #include file="../Shared/srtgroepselector.inc" -->
<!-- #include file="../Shared/srtdeelselector.inc" -->
<!-- #include file="../Shared/srtcontroleselector.inc" -->
<!-- #include file="../Shared/persoonselector.inc" -->
<!-- #include file="../Shared/kostenplaatsselector.inc" -->
<%

FCLTHeader.Requires({ plugins:["suggest", "jQuery", "kenmerk"],
                      js: ["../ins/ins_search.js", "jQuery-ui.js"]
                    });

/***** Get webform parameters *****/
var ins_score_enabled = S("ins_score_enabled") == 1;
var urole = getQParamSafe("urole", "bo");
var autosearch = getQParamInt("autosearch", 0) == 1;
var mode = getQParamInt("mode", 1); // Schermmode 1=Begroting (aankomende jaren) (=default), 2=Realisatie (huidige en lopende jaren, 3=Uitvoering (koppelen (uitvoerend) van taken aan een melding).
var actionpage;
switch (mode)
{
  case 1: actionpage = "mjb_search_list.asp";        // Begroting (aankomende jaren).
          break;
  case 2: actionpage = "mjb_search_list_curr.asp";   // Realisatie (huidige en lopende jaren.
          break;
  case 3: actionpage = "mjb_search_list.asp";        // Uitvoering (koppelen (uitvoerend) van taken aan een melding).
          break;
}

var deel = getQParam("deel", ""); // Identificatie
var groep = getQParam("groep", ""); // Categorie/Groep
if (groep == -1) groep = "";
var reg_key = getQParamInt("regio_key", -1); // Regio
var dist_key = getQParamInt("district_key", -1);
var loc_key = getQParamInt("locatie_key", -1); // Locatie
var bld_key = getQParamInt("gebouw_key", -1); // Gebouw
var flr_key = getQParamInt("verdieping_key", -1); // Verdieping
var room_key = getQParamInt("ruimte_key", -1); // Ruimte
var disc = getQParamInt("disc", -1); // Discipline initialisatie
var srtgroep = getQParamInt("srtgroep", -1); // Groep
var categorie_key_arr = getQParamKeyArray("categorie", []); // Categorie
var srtdeel = getQParamInt("srtdeel", -1); // Objectsoort
var prs_geb_key = getQParamInt("prs_geb_key", -1); // Gebouw verantwoordelijke
var prs_beh_key = getQParamInt("prs_beh_key", -1); // Objectbeheerder.
var groep = getQParam("groep", ""); // Groep
var srtcontrole = getQParamInt("srtcontrole", -1); // Taak
var alg_startlevel = getQParamInt("alg_startlevel", 1); // Startlevel van plaatsselector (District)
var alg_endlevel = getQParamInt("alg_endlevel", 3); // Eindlevel van plaatsselector (Gebouw)
var groupby = getQParamInt("groupby", 3);
var frequentie = getQParamInt("frequentie", -1);
var toyear = getQParamInt("toyear", new Date().getFullYear() + S("mjb_show_years"));
var kp_key_arr = getQParamIntArray("account", []);  // effectief werkt dit op dit moment maar met 1 waarde
var incbtw = getQParamInt("incbtw", 0) == 1;
var actsit = getQParamInt("actsit", 0) == 1;
var unfinalised = getQParamInt("unfinalised", 0) == 1; // Alleen onvoltooide regels tonen.
var mjbempty = getQParamInt("mjbempty", 0) == 1;
var ctrdisc_key = getQParamInt("ctrdisc_key", -1); // Ctrdiscipline

var mjbMoved   = (getQParamInt("mjbMoved", 0) == 1);
var mjbFreezed = (getQParamInt("mjbFreezed", 0) == 1);
var mjbXcped   = (getQParamInt("mjbXcped", 0) == 1);
var mjbStatus2 = (getQParamInt("mjbStatus2", 1) == 1);
var mjbStatus3 = (getQParamInt("mjbStatus3", 1) == 1);

var mjb_freeze_year = Math.max(S("mjb_freeze_year"), new Date().getFullYear());

if (ctrdisc_key <= 0) // Als er maar eentje is selecteer die automatisch
{
    var sql = "SELECT ins_discipline_key"
            + "  FROM ctr_disc_params cdp, ctr_discipline cd"
            + " WHERE cdp.ctr_ins_discipline_key = cd.ins_discipline_key"
            + "   AND cd.ins_discipline_verwijder IS NULL"
            + "   AND cdp.ctr_disc_params_ismjob = 1";
    var oRs = Oracle.Execute(sql);
    if (!oRs.Eof)
    {
        var key = oRs("ins_discipline_key").Value;
        oRs.MoveNext();
        if (oRs.Eof)
            ctrdisc_key = key;
    }
    oRs.Close();
}
if (ctrdisc_key > 0)
    lcl.set_dialect(ctrdisc_key, "INS_TAB_DISCIPLINE_KEY");

// Er zijn INSUSE en CTRUSE rechten nodig.
var autfunctionINSUSE = "WEB_INSUSE";
var authparamsINSUSE = user.checkAutorisation("WEB_INSUSE");
if (mode == 1)
  var authparamsCTRUSESEN = user.checkAutorisation(["WEB_CTRUSE", "WEB_CTRSEN"]);
else // mode == 2 || mode == 3
  var authparamsCTRBOF = user.checkAutorisation("WEB_CTRBOF");
var authparamsCTRUSE = user.checkAutorisation("WEB_CTRUSE", true);
var authparamsCTRBOF = user.checkAutorisation("WEB_CTRBOF", true);
var authparamsCTRSEN = user.checkAutorisation("WEB_CTRSEN", true);

var hasReadCTRUSE = authparamsCTRUSE && authparamsCTRUSE.ALGreadlevel < 9 && authparamsCTRUSE.PRSreadlevel < 9;
var hasReadCTRBOF = authparamsCTRBOF && authparamsCTRBOF.ALGreadlevel < 9 && authparamsCTRBOF.PRSreadlevel < 9;
var hasReadCTRSEN = authparamsCTRSEN && authparamsCTRSEN.ALGreadlevel < 9 && authparamsCTRSEN.PRSreadlevel < 9;

if (!hasReadCTRUSE && hasReadCTRSEN)
{
  var sql = "SELECT COUNT(*) aantal"
          + "     , MIN(ins_scenario_key) scen_key_min"
          + "  FROM ins_scenario"
          + " WHERE ins_scenario_key > 1";
  var oRs = Oracle.Execute(sql);
  var hasScenarios = oRs("aantal").Value;
  if (hasScenarios)
    scen_key_min = oRs("scen_key_min").Value;
}

var scen_key = getQParamInt("scenario", (hasReadCTRUSE? 1 : scen_key_min)); // Scenario

// Als ik alleen CTRUSE/CTRBOF read rechten heb dan mag ik alleen de Actuele situatie (scenario = 1) zien.
// Als ik alleen CTRSEN read rechten heb dan mag ik alleen de Scenario's (scenario > 1) zien.
user.anything_todo_or_abort(((hasReadCTRUSE || hasReadCTRBOF) && scen_key == 1) || (hasReadCTRSEN && scen_key > 1));

%>

<html>
  <head>
<%  FCLTHeader.Generate(); %>

    <script type="text/javascript">
      var urole = "<%=urole%>";
      var scen_key = <%=scen_key%>;

  		function doSubmit()
	  	{
        document.forms.u2.submit();
      }

      var selectedScen = true;
      function onChangeScenario()
      {
        if ($("#scenario").val() != 1)
        {
          if (!selectedScen)
          {
            $("#actsit").prop("checked", false);
            selectedScen = true;
          }
          $(".tractsit").show();
          $("#showunfin").prop("checked", false);
          $(".trshowunfin").hide();
          $("#mjbFreezed").prop("checked", false);
          $("#checdfreezed").hide();
        }
        else
        {
          $(".tractsit").hide();
          $("#actsit").prop("checked", true);
          $(".trshowunfin").show();
          selectedScen = false;
          $("#checdfreezed").show();
        }
      }

      function onChangeGroupBy()
      {
        if ($("#groupby").val() == 7 || $("#groupby").val() == 9)
        {
          $("#fitness_score").show();
          $("#fitness_score_neg").show();
          $("#fitness_score_pos").show();
          $("#priority_score").show();
        }
        else
        {
          if ($("#groupby").val() == 2 || $("#groupby").val() == 3)
            $("#fitness_score").show();
          else
            $("#fitness_score").hide();
          $("#fitness_score_neg").hide();
          $("#fitness_score_pos").hide();
          $("#priority_score").hide();
        }
      }

      function checkCheckboxes()
      {
<%    if (mode == 3) { /* Only applicable in mode == 3 */ %>
        if (!document.getElementById("mjbStatus2").checked &&
            !document.getElementById("mjbStatus3").checked)
        {
          document.getElementById("mjbStatus2").checked = true;
        }
<%    } %>
      }

      $(function()
      {
        if (scen_key == 1) $(".tractsit").hide();
        $("#actsit").prop("checked", <%=actsit? "true" : "false"%>);
        selectedScen = false;
<%      if (autosearch)
        { %>
          window.afterfiltersready = function () { doSubmit() };
<%      } %>
<%      if (ins_score_enabled)
        { %>
          onChangeGroupBy();
<%      } %>
        checkCheckboxes();

      });
    </script>
  </head>

  <body id="searchbody">
    <form name="u2" target="workFrame" action="<%=actionpage%>" method="get">
<%      SEARCH_PAGE_START({ header: L("lcl_filterblok") });
          SEARCH_BLOCK_START(); %>
        <input type="hidden" name="mode" value="<%=mode%>">
        <input type="hidden" name="urole" value="<%=urole%>">
        <input type="hidden" name="alg_startlevel" value="<%=alg_startlevel%>">
        <input type="hidden" name="alg_endlevel" value="<%=alg_endlevel%>">
        <input type="hidden" name="ctrdisc_key" value="<%=ctrdisc_key%>">
<%
        var start_year = new Date().getFullYear();
        if (mode == 1)
          // Vorig jaar wil de klant soms zien (DEMO#65665) om te zien of er nog inspecties in dat jaar lopen.
          // Dit deed men door het freeze jaar op vorig jaar te zetten.
          // Maar sinds het freeze jaar niet meer in het verleden mag liggen (FLCT#83238), heeft dat geen effect meer.
          // Daarom laten we vorig jaar maar altijd zien.
          start_year--;
        if (S("mjb_start_year") >= start_year)
          start_year = S("mjb_start_year");

        if (mode == 1)
        { %>
          <input type="hidden" name="fromyear" value="<%=start_year%>">
<%      }
        //else if (mode == 2) // FCLTselector verderop.
        //else if (mode == 3) // Dan wordt door mjb_search_list.asp altijd het freeze jaar genomen.

              RWFIELDTR("deel", "fld", L("lcl_obj_identification"), deel, { trclass: "primsearch", datatype: "text" });
              FCLTplaatsselector(authparamsINSUSE.ALGreadlevel,
                                 { regiokey: reg_key,
                                   districtkey: dist_key,
                                   locatiekey: loc_key,
                                   gebouwkey: bld_key,
                                   verdiepingkey: flr_key,
                                   ruimtekey: room_key,
                                   terrein: true,
                                   escalateOnChUp: true, // ook onLocChange als je gebouw wijzigt
                                   startlevel: alg_startlevel,
                                   eindlevel: alg_endlevel, // ruimte/werkplek
                                   multitoggle: true,
                                   multionchange: true,
                                   filtercode: "INCVR",
                                   whenEmpty: L("lcl_search_generic")  // want filter
                                 });
              //  Discipline
              FCLTdisciplineselector("disc",
                                     "sgDisc",
                                     { label: L("lcl_discipline"),
                                       disc_key: disc,
                                       module: "INS",
                                       filtercode: "I",  // alleen disciplines met inspecties
                                       perslidKey: user_key,
                                       autfunctionKey: authparamsINSUSE.autfunctionkey,
                                       autRW: "R",
                                       onChange: "onChangeDiscipline", // disgroepChanged
                                       multitoggle: true,
                                       trclass: "primsearch",
                                       whenEmpty: L("lcl_search_generic"),
                                       sgRelType: "srtgroep, srtdeel"   // Wijzigen van discipline heeft effect op de srtgroepselector en de srtdeelselector.
                                     });
              //  Soort groep
              FCLTsrtgroepselector("srtgroep",
                                   "sgSrtgroep",
                                   { label: L("lcl_obj_group"),
                                     srtgroep_key: srtgroep,
                                     autfunction: autfunctionINSUSE,
                                     onChange: "onChangeSrtgroep",
                                     multitoggle: true,
                                     urlAdd: [{urlParam: "disc_key", field: "disc"}],
                                     trclass: "primsearch",
                                     whenEmpty: L("lcl_search_generic"),
                                     sgRelType: "srtdeel"   // wijzigen van groep heeft effect op de srtdeelselector.
                                   });

              // Objectsoort
              FCLTsrtdeelselector("srtdeel",
                            "sgSrtdeel",
                            { label: L("lcl_obj_sort"),
                              srtdeel_key: srtdeel,
                              module: "INS",
                              autfunction: autfunctionINSUSE,
                              onChange: "onChangeSrtdeel",
                              multitoggle: true,
                              urlAdd: [{urlParam: "srtgroep_key", field: "srtgroep"},
                                       {urlParam: "disc_key", field: "disc"}],
                              trclass: "primsearch",
                              whenEmpty: L("lcl_search_generic")  // want dit is een filter
                            });

              var sql = "SELECT alg_srtgebouw_key"
                      + ", " + lcl.xsqla("alg_srtgebouw_omschrijving", "alg_srtgebouw_key")
                      + "  FROM alg_srtgebouw"
                      + " WHERE alg_srtgebouw_verwijder IS NULL"
                      + " ORDER BY UPPER(" + lcl.xsql("alg_srtgebouw_omschrijving", "alg_srtgebouw_key") + ")";
              FCLTselector("srtgebouw",
                           sql,
                           { initKey: -1,
                             label: L("lcl_estate_gebouw_srtgebouw"),
                             multi: true,
                             trclass: "primsearch",
                             emptyOption: L("lcl_search_generic")
                           });

              // Gebouw verantwoordelijke
              // Dit veld is alleen zichtbaar indien er minimaal één gebouwverantwoordelijke geconfigureerd is (suppressNoValues).
              FCLTpersoonselector( "verantw",
                                   "sgVerantw",
                                   { label: L("lcl_bld_ter_verantw"),
                                     perslidKey: prs_geb_key,
                                     filtercode: "GTV", // Gebouw/Terrein Verantwoordelijke
                                     trclass: "primsearch noxd",
                                     suppressNoValues: true,
                                     autlevel: authparamsINSUSE.PRSreadlevel,
                                     whenEmpty: L("lcl_search_generic")  // want filter
                                   });

              // Contractbeheerders
              FCLTpersoonselector("sBeh_key", // manager
                                   "sgBeh",
                                   { label: L("lcl_ins_manager"),
                                     perslidKey: prs_beh_key,
                                     filtercode: "INS",
                                     trclass: "primsearch",
                                     autlevel: authparamsINSUSE.PRSreadlevel,
                                     whenEmpty: L("lcl_search_generic")  // want filter
                                   });

              if (ins_score_enabled)
              {
                // Conditiescore
%>              <tr class="primsearch fldfitness_score" id="fitness_score">
                        <td class="label">
                          <label for="show_fitness_score_from"><%=L("lcl_mjb_fitness_score")%></label>
                        </td>
                  <td class="score">
                    <%=L("lcl_mjb_from")%>
                    <input type="text"
                           onChange="fvalid=checkKenmerk(this, false, 'N', 1, 1, 1, 6)"
                           onBlur="checkKenmerk(this, true, 'N', 1, 1, 1, 6)"
                           id="fitness_score1_from"
                           name="fitness_score1_from"
                           class="fldshort number "
                           value=""
                           maxlength="3">
                    &nbsp;<%=L("lcl_mjb_through")%>&nbsp;
                    <input type="text"
                           onChange="fvalid=checkKenmerk(this, false, 'N', 1, 1, 1, 6)"
                           onBlur="checkKenmerk(this, true, 'N', 1, 1, 1, 6)"
                           id="fitness_score1_through"
                           name="fitness_score1_through"
                           class="fldshort number "
                           value=""
                           maxlength="3">
                  </td>
                </tr>
<%
                // Negatieve afwijking van de conditie score
%>              <tr class="primsearch fldfitness_score_neg" id="fitness_score_neg">
                  <td class="label">
                      &nbsp;&nbsp;• <%=L("lcl_mbj_negative_dev")%>
                  </td>
                  <td class="score">
                    <%=L("lcl_mjb_from")%>
                    <input type="text"
                           onChange="fvalid=checkKenmerk(this, false, 'N', 1, 1, 1, 5)"
                           onBlur="checkKenmerk(this, true, 'N', 1, 1, 1, 5)"
                           id="fitness_score1_from_neg"
                           name="fitness_score1_from_neg"
                           class="fldshort number "
                           value=""
                           maxlength="3">
                    &nbsp;<%=L("lcl_mjb_through")%>&nbsp;
                    <input type="text"
                           onChange="fvalid=checkKenmerk(this, false, 'N', 1, 1, 1, 5)"
                           onBlur="checkKenmerk(this, true, 'N', 1, 1, 1, 5)"
                           id="fitness_score1_through_neg"
                           name="fitness_score1_through_neg"
                           class="fldshort number "
                           value=""
                           maxlength="3">
                  </td>
                </tr>
<%
                // Positieve afwijking van de conditie score
%>              <tr class="primsearch fldfitness_score_pos" id="fitness_score_pos">
                  <td class="label">
                      &nbsp;&nbsp;• <%=L("lcl_mjb_positive_dev")%>
                  </td>
                  <td class="score">
                    <%=L("lcl_mjb_from")%>
                    <input type="text"
                           onChange="fvalid=checkKenmerk(this, false, 'N', 1, 1, 1, 5)"
                           onBlur="checkKenmerk(this, true, 'N', 1, 1, 1, 5)"
                           id="fitness_score1_from_pos"
                           name="fitness_score1_from_pos"
                           class="fldshort number "
                           value=""
                           maxlength="3">
                    &nbsp;<%=L("lcl_mjb_through")%>&nbsp
                    <input type="text"
                           onChange="fvalid=checkKenmerk(this, false, 'N', 1, 1, 1, 5)"
                           onBlur="checkKenmerk(this, true, 'N', 1, 1, 1, 5)"
                           id="fitness_score1_through_pos"
                           name="fitness_score1_through_pos"
                           class="fldshort number "
                           value=""
                           maxlength="3">
                  </td>
<%
                // Prioriteitsscore
%>              <tr class="primsearch fldpriority_score" id="priority_score">
                  <td class="label">
                    <label for="show_priority_score_from"><%=L("lcl_mjb_priority_score")%></label>
                  </td>
                  <td class="score">
                    <%=L("lcl_mjb_from")%>
                    <input type="text"
                           onChange="fvalid=checkKenmerk(this, false, 'N', 1, 0, 1, 6)"
                           onBlur="checkKenmerk(this, true, 'N', 1, 0, 1, 9)"
                           id="priority_score2_from"
                           name="priority_score2_from"
                           class="fldshort number "
                           value=""
                           maxlength="1">
                    &nbsp;<%=L("lcl_mjb_through")%>&nbsp
                    <input type="text"
                           onChange="fvalid=checkKenmerk(this, false, 'N', 1, 0, 1, 6)"
                           onBlur="checkKenmerk(this, true, 'N', 1, 0, 1, 9)"
                           id="priority_score2_through"
                           name="priority_score2_through"
                           class="fldshort number "
                           value=""
                           maxlength="1">
                  </td>
                </tr>
<%
              }

              // Scenarios
              if (mode == 1)
              { // Scenario's zijn er alleen voor de toekomst.
                sql = "SELECT ins_scenario_key"
                    + "     , ins_scenario_omschrijving"
                    + "  FROM ins_scenario"
                    + " WHERE ins_scenario_verwerkt IS NULL"
                    + "   AND ins_scenario_key <> 1" // Niet de actuele.
                    + (!hasReadCTRSEN
                      ? " AND 1 = 0"
                      : "") // Alleen als ik CTRSEN rechten heb mag ik scenario's zien.
                    + " ORDER BY ins_scenario_omschrijving";
                var oRs = Oracle.Execute(sql);
                if (!oRs.Eof)
                {
                  FCLTselector("scenario",
                               sql,
                               { label: L("lcl_mjb_scenario"),
                                 initKey: scen_key,
                                 //multi: true, // Voor nu niet multi.
                                 trclass:"primsearch",
                                 emptyKey: hasReadCTRUSE? 1 : null,
                                 emptyOption: hasReadCTRUSE? L("lcl_search_generic") : null,
                                 onChange: hasReadCTRUSE? "onChangeScenario()" : null
                               });
                  if (hasReadCTRUSE)
                    RWCHECKBOXTR("actsit", "fldactsit", L("lcl_mjb_also_act_situation"), actsit, { html: " value='1' ", trclass: "primsearch tractsit"});

                }
                oRs.Close();
                RWCHECKBOXTR("mjbshowempty", "fldmjbempty", L("lcl_mjb_empty_lines_too"), mjbempty, { html: " value='1' ", trclass: "primsearch"});
              }
              if (mode != 3)
                RWCHECKBOXTR("showunfin", "fldshowunfin", L("lcl_mjb_show_unfinalised"), unfinalised, { html: " value='1' ", trclass: "primsearch trshowunfin"}); // Niet tonen bij scenario's

            SEARCH_BLOCK_END();
            // Start responsive search_column 2
            SEARCH_BLOCK_START();

              // <!-- Taakcategorie -->
              var sql = "SELECT i.ins_discipline_key,"
                      + "       i.ins_discipline_omschrijving"
                      + "  FROM ins_tab_discipline i, ctr_disc_params c"
                      + " WHERE i.ins_discipline_key = c.ctr_ins_discipline_key(+)"
                      + "   AND c.ctr_disc_params_ismjob = 1"
                      + "   AND i.ins_discipline_module = 'CTR'"
                      + "   AND ins_discipline_verwijder IS NULL"
                      + "  ORDER BY ins_discipline_omschrijving";

              FCLTselector("categorie",
                           sql,
                           { initKey: categorie_key_arr,
                           	 label: L("ctr_discipline"),
                             emptyOption: L("lcl_search_generic"),
                             multi: true,
                             trclass: "primsearch"
                           });

              // Periodieke taken.
              FCLTsrtcontroleselector("srtcontrole",
                            "sgSrtcontrole",
                            { label: L("lcl_ins_controle_srt"),
                              srtcontrole_key: srtcontrole,
                              multitoggle: true,
                              urlAdd: [{urlParam: "ctrdisc_key_arr", field: "categorie"}],
                              trclass: "primsearch",
                              filtercode: "MJOB",
                              whenEmpty: L("lcl_search_generic")  // want dit is een filter
                            });

              // <!-- Groep-->
              var sql = "SELECT DISTINCT COALESCE(xcp.ins_srtcontroledl_xcp_groep, isc.ins_srtcontrole_groep)"
                      + "     , COALESCE(xcp.ins_srtcontroledl_xcp_groep, isc.ins_srtcontrole_groep)"
                      + "  FROM ins_srtcontrole isc"
                      + "     , ins_srtcontroledl_xcp xcp"
                      + " WHERE isc.ins_srtcontrole_key = xcp.ins_srtcontrole_key(+)"
                      + "   AND xcp.ins_scenario_key(+) = 1"
                      + "   AND COALESCE (xcp.ins_srtcontroledl_xcp_groep, isc.ins_srtcontrole_groep) IS NOT NULL"
                      + "  ORDER BY UPPER (COALESCE(xcp.ins_srtcontroledl_xcp_groep, isc.ins_srtcontrole_groep))";
              FCLTselector("groep",
                           sql,
                           { initKey: groep,
                           	 label: L("ins_srtcontrole_groep"),
                             multi: true,
                             emptyOption: L("lcl_search_generic"),
                             trclass: "primsearch"
                           });

              sql = "SELECT DISTINCT COALESCE(xcp.ins_srtcontroledl_xcp_periode, isc.ins_srtcontrole_periode) periode_key"
                  + "     , COALESCE(xcp.ins_srtcontroledl_xcp_periode, isc.ins_srtcontrole_periode) || " +  safe.qL("lcl_ins_controle_period_rearyear") + " periode"
                  + "  FROM ins_srtcontrole isc"
                  + "     , ins_srtcontroledl_xcp xcp"
                  + " WHERE isc.ins_srtcontrole_key = xcp.ins_srtcontrole_key"
                  + "   AND xcp.ins_scenario_key = 1"
                  + "   AND COALESCE (xcp.ins_srtcontroledl_xcp_periode, isc.ins_srtcontrole_periode) > 0"
                  + " ORDER BY 1";

              FCLTselector("frequentie", // Frequentie
                           sql,
                           { initKey: frequentie,
                             label: L("lcl_mjb_frequency"),
                             multi: true,
                             trclass:"primsearch",
                             emptyOption: L("lcl_search_generic")
                           });

              FCLTkostenplaatsselector("account",
                                       "sgAccount",
                                       user_key,
                                       { label: L("lcl_prs_kosten"),
                                         kostenplaatsKey: kp_key_arr.join(","),  // dit werkt toch helemaal niet?
                                         filtercode: "A", // Alle kostenplaatsen tonen.
                                         trclass: "noxd",
                                         multitoggle: true,
                                         whenEmpty: L("lcl_search_generic")  // want filter
                                        });

              if (mode == 1)
              { // periode alleen van belang bij plannen (en/of verleden?)
                var sqljaren = [];
                for (var i = 0; i <= S("mjb_show_years"); i++)
                    sqljaren.push("SELECT {0}, '{0}' FROM DUAL".format(mjb_freeze_year + i));
                FCLTselector("toyear",
                             sqljaren.join(" UNION "),
                             { initKey: toyear,
                               label: L("lcl_mjb_uptoyear")
                             });
              }
              else if (mode == 2 || mode == 3)
              { // Mode 2: Alle mogelijke jaren die een laatste gestarte, ter uitvoering staande, gereedgemelde of voltooide inspectie van een taak bevatten.
                // Mode 3: Alle mogelijke jaren die een laatste gestarte of ter uitvoering staande inspectie van een taak bevatten.
                var sqljaren = "SELECT DISTINCT EXTRACT(YEAR FROM dsc.ins_deelsrtcontrole_freezedate) freezedate"
                             + "     , '' || EXTRACT(YEAR FROM dsc.ins_deelsrtcontrole_freezedate) freezedate_oms"
                             + "  FROM ins_deelsrtcontrole dsc"
                             + "     , ins_deel d"
                             + " WHERE dsc.ins_deel_key = d.ins_deel_key"
                             + "   AND dsc.ins_deelsrtcontrole_key = (SELECT MAX(id2.ins_deelsrtcontrole_key)"
                             + "                                       FROM ins_deelsrtcontrole id2"
                             + "                                      WHERE id2.ins_deel_key = dsc.ins_deel_key"
                             + "                                        AND id2.ins_srtcontrole_key = dsc.ins_srtcontrole_key"
                             + "                                        AND id2.ins_scenario_key = 1)"
                             + "   AND dsc.ins_deelsrtcontrole_freezedate IS NOT NULL"
                             + (mode == 3
                               ? " AND dsc.ins_deelsrtcontrole_status IN (2, 3)"
                               : "")
                             + " ORDER BY freezedate";
                FCLTselector("fromyear",
                             sqljaren,
                             { //initKey: new Date().getFullYear(),
                               label: L("lcl_mjb_fromyear")
                             });
              }

              RWCHECKBOXTR("incbtw", "fldincbtw", L("lcl_mjb_inclusiefbtw"), incbtw, { html: " value='1' "});
              sql = "SELECT         1, " + safe.qL("lcl_district")         + ", 1 FROM DUAL"
                  + " UNION SELECT  2, " + safe.qL("lcl_location")         + ", 2 FROM DUAL"
                  + " UNION SELECT  3, " + safe.qL("lcl_bld_terra")        + ", 3 FROM DUAL"
                  + " UNION SELECT  4, " + safe.qL("lcl_discipline")       + ", 4 FROM DUAL"
                  + " UNION SELECT  5, " + safe.qL("lcl_obj_group")        + ", 5 FROM DUAL"
                  + " UNION SELECT  6, " + safe.qL("lcl_obj_sort")         + ", 6 FROM DUAL"
                  + " UNION SELECT  7, " + safe.qL("lcl_ins_object")       + ", 7 FROM DUAL"
                  + (mode == 2
                    ? " UNION SELECT  8, " + safe.qL("lcl_mjb_melding")       + ", 8 FROM DUAL"
                    : "")
                  + " UNION SELECT  9, " + safe.qL("lcl_ins_controle_srt") + ", 9 FROM DUAL";

              FCLTselector("groupby",
                           sql,
                           { initKey: groupby,
                             label: L("lcl_mjb_group"),
                             onChange: (ins_score_enabled? "onChangeGroupBy()" : null)
                           });

              if (mode == 1)
              { // ook de verschillende bewerkingstatussen alleen tijdens plannen
%>
                <!-- Status checkboxen -->
                <tr>
                  <td class="label">
                    <label for="statusboxes"><%=L("lcl_mjb_status_mjob")%></label>
                  </td>
                  <td>
                    <div id="statusboxes">
                      <input type="checkbox" class="fldcheck" name="mjbMoved" id="mjbMoved" value="1" <%=mjbMoved? "checked" : ""%>><label for="mjbMoved"><%=L("lcl_mjb_moved")%></label>
                      <span id="checdfreezed">
                        <input type="checkbox" class="fldcheck" name="mjbFreezed" id="mjbFreezed" value="1" <%=mjbFreezed? "checked" : ""%>><label for="mjbFreezed"><%=L("lcl_mjb_started_in_execution")%></label>
                      </span>
                      <input type="checkbox" class="fldcheck" name="mjbXcped"  id="mjbXcped" value="1" <%=mjbXcped ? "checked" : " "%>><label for="mjbXcped"><%=L("lcl_mjb_overruled_xcp")%></label>
                    </div>
                  </td>
                </tr>
<%
              }
              else if (mode == 3)
              {
%>
                <tr>
                  <td class="label">
                    <label for="statusboxes"><%=L("lcl_mjb_status_mjob")%></label>
                  </td>
                  <td>
                    <div id="statusboxes">
                      <input type="checkbox" class="fldcheck" name="mjbStatus2" id="mjbStatus2" onClick="checkCheckboxes();" value="1" <%=mjbStatus2? "checked" : ""%>><label for="mjbStatus2"><%=L("lcl_mjb_started")%></label>
                      <input type="checkbox" class="fldcheck" name="mjbStatus3"  id="mjbStatus3" onClick="checkCheckboxes();" value="1" <%=mjbStatus3 ? "checked" : " "%>><label for="mjbStatus3"><%=L("lcl_mjb_in_execution")%></label>
                    </div>
                  </td>
                </tr>
<%            }

            SEARCH_BLOCK_END();
            SIMPLE_BLOCK_START();

              var buttons = [{title: L("lcl_overview_list"),  action: "doSubmit()", icon: "fa-clipboard-list" }];
              CreateButtons(buttons, { entersubmit: true, showIcons: true} );

            SIMPLE_BLOCK_END();
        SEARCH_PAGE_END();
%>
      </form>
    <div id="result">
      <iframe width="100%" height="100%"
              src="../Shared/empty.asp"
              name="workFrame" id="workFrame"
              onload='FcltMgr.iframeLoaded(this)'
              frameborder="0" scrolling="no">
      </iframe>
    </div>
  </body>
</html>
<% ASPPAGE_END(); %>
