<%@language = "javascript" %>
<% /*
    $Revision$
    $Id$
    File:           INS/ins_search.asp
    Description:    Menuoptie zoeken objecten
    Parameters:     urole:      bo is objectenbeheer,
                                fo is uitleenmodule
                                fe is meterstandenbeheer
                    inspect:    als 1 dan in inspectiemode
                    diverse voorgekozen filtervelden
    Context:
    Note:

*/ %>
<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<!-- #include file="../Shared/selector.inc" -->
<!-- #include file="../Shared/afdelingselector.inc" -->
<!-- #include file="../Shared/bedrijfselector.inc" -->
<!-- #include file="../Shared/persoonselector.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/calendar.inc" -->
<!-- #include file="../Shared/dienstselector.inc" -->
<!-- #include file="../Shared/xd.inc" -->

<%

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

/***** Get webform parameters *****/
var ins_score_enabled = S("ins_score_enabled") == 1;
var urole = getQParamSafe("urole");
var fronto = (urole == "fo");
var backo = (urole == "bo" || urole == "b2");
var backo2 = (urole == "b2");
var minfo = (urole == "mi");
var frontend = (urole == "fe" || (!fronto && !backo && !minfo));

var autosearch = getQParamInt("autosearch", 0) == 1;

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 wpl_key = getQParamInt("werkplek_key", -1); // Werkplek
var deel = getQParam("deel", ""); // Identificatie
var dis_key = getQParamInt("dis_key", -1); // Discipline readonly selecteren
var disc = getQParamInt("disc", -1); // Discipline initialisatie
var srtgroep = getQParamInt("srtgroep", -1); // Groepsoort
var srtdeel = getQParamInt("srtdeel", -1); // Objectsoort
var srtdeelomschr = getQParam("srtdeelomschr", ""); // Srtdeel code en/of omschrijving
var groep = getQParam("groep", ""); // Groep, een geldige groepsnaam, case-insensitive
var ctrdisc_key_arr = getQParamIntArray("ctrdisc_key", []); // Ctrdiscipline
var srtcontrole_key = getQParamInt("srtcontrole_key", -1); // Periodieke Taak (Srtcontrole)
var dep_key = getQParamInt("dep_key", -1); // Organisatie key
var prs_key = getQParamInt("prs_key", -1); // Persoon key, kan worden overruled door "self"
//  alg_startlevel is het startniveau van de plaatsselector, -1 onderdrukt de plaatsselector expliciet 
//  als er geen locaties zijn wordt die al automatisch onderdrukt want dan sowieso zinloos (denk ITSM)
var alg_startlevel = getQParamInt("alg_startlevel", 2); // Startlevel van plaatsselector (Locatie)
var alg_endlevel = getQParamInt("alg_endlevel", 5); // Eindlevel van plaatsselector (Locatie)
var prs_geb_key = getQParamInt("verantw", -1); // Gebouw verantwoordelijke
var prs_beh_key = getQParamInt("prs_beh_key", -1); // Beheerder
var group = getQParamInt("groupby", -1); // Groepering
var eigen = getQParamInt("self", 0); // Alleen mijn bezit (beperking op urole=fo)
if (eigen) prs_key = user_key;

var insObjIncl = getQParamInt("insObjIncl", 1) == 1; // Ook onderdelen (nu default aan)
var expObjIncl = getQParamInt("expObjIncl", 0) == 1; // Ook vervallen objecten
var inacObjIncl = getQParamInt("inacObjIncl", 1) == 1; // Ook inactieve objecten
var insPhotoIncl = getQParamInt("insPhotoIncl", 1) == 1; // Ook afbeeldingen
var insSensOnly = getQParamInt("insSensOnly", 0) == 1; // Alleen sensoren
var insLentOut = getQParamInt("insLentOut", 1) == 1; // Uitgegeven
var insAvail = getQParamInt("insAvail", 1) == 1; // Beschikbaar
var lentoutAction = getQParamInt("lentoutAction", 0); // Niet in tracking zoeken

var insInspect = getQParamInt("inspect", 0) == 1; // inspectievelden toevoegen
var inspBusy = getQParamInt("inspBusy", 0) == 1; // Gestarte/begonnen en ter uitvoering staande inspecties (status 2 en 3).
var inspClosed = getQParamInt("inspClosed", 0) == 1; // Afgemelde inspecties (status 5). Actie vereist.
/***** End get webform parameters *****/

if (ctrdisc_key_arr.length == 1)
    lcl.set_dialect(ctrdisc_key_arr[0], "CTR_DISCIPLINE_KEY");
if (dis_key > 0)
    lcl.set_dialect(dis_key, "INS_DISCIPLINE_KEY");

var autfunction = "";
switch(urole)
{
  case "fe":
          autfunction = "WEB_INSUSE";
          break;
  case "fo":
          autfunction = "WEB_INSFOF";
          break;
  case "b2":
  case "bo":
          if (eigen == 1)  // icm bo
            autfunction = "WEB_INSMIN";
          else
            autfunction = "WEB_INSMAN";
          break;
}

// De functie checkAutorisation(autfunction) geeft een Response.End indien er geen autorisatie voor autfunction wordt gevonden
var authparams = user.checkAutorisation(autfunction);

// Fast check to see if we have anything to do with geographics, else do nothing at all
var sql= "SELECT 1 FROM alg_locatie WHERE alg_locatie_verwijder IS NULL AND (alg_locatie_vervaldatum > SYSDATE OR alg_locatie_vervaldatum IS NULL) AND ROWNUM = 1";
var oRs = Oracle.Execute(sql);
var anygeo = !oRs.eof;
oRs.close();
%>

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

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

<% if (autosearch) { %>
      $(function() { doSubmit(); });
<%}%>

    var anysearch = false;
		function doSubmit()
		{
		    anysearch = true;
<% if (xd.enabled()) { %>
        if ($("#xd").is(":checked"))
        {
              $("form[name=u2]").attr("action", "ins_search_list_xd.asp");
        } else {
              $("form[name=u2]").attr("action", "ins_search_list.asp");
        }
<% } %>
<%      if (fronto || backo)
        { %>
          // Submitten met kenmerken
          doSubmitWithKenmerken();
<%      }
        else
        { %>
          document.forms.u2.submit();
<%      } %>
      }
      //function fcltontabshow()
      //{
      //    if (anysearch)
      //        doSubmit();
      //}

      function cadShowdiscfn()
      {
          if ($("input#disc") && $("input#disc").val())
            return $("input#disc").val();
          return -1;
      };

<%    if (fronto || backo)
      { %>
        async function myModal(init)
        {
          var url = "load_kenmerk.asp?urole=<%=urole%>"
                  + "&advanced=1"
                  + "&mode=<%=insInspect ? 3 : 1%>"
                  + "&disc="      +$("[name=disc]").val()
                  + "&srtgroep="  +$("[name=srtgroep]").val()
                  + "&srtdeel="   +$("[name=srtdeel]").val()
                  + "&objsrt_oms="+$("#srtdeelomschr").val()
<%                if (insInspect) { %>
                  + "&ctr_disc=" + $("#ctr_discipline").val() // Taakcategorie
                  + "&srtcontrole=" + $("#srtcontrole").val() // Taak
<%                }%>
                  + "&hasfilter=1";
          var titel = L("lcl_ins_kenmerken");
          await showKenmerkModal(url, titel, init)
        }
<%    } %>
      function onClickDone()
      {
        if (document.getElementById("insInspDone").checked)
        {
          $("#insLSF").hide();
          $(".donetime").show();
        }
        else
        {
          $(".donetime").hide();
          $("#insLSF").show();
        }
      }
      function onClickSuccess()
      {
        if (!document.getElementById("insSuccessY").checked && !document.getElementById("insSuccessN").checked)
          document.getElementById("insSuccessY").checked = true;
      }
      function onClickActief()
      {
        if (!document.getElementById("insActiefY").checked && !document.getElementById("insActiefN").checked)
          document.getElementById("insActiefY").checked = true;
      }
<% if (xd.enabled()) { %>
      function xdclicked()
      {
        var doxd = document.getElementById("xd").checked;
        $(".noxd").toggle(!doxd);
        $(".onlyxd").toggleClass('hidden', !doxd);
        iface_toggleSecondarySearchblock(false); // altijd uitschakelen
      }
<% } %>
    </script>
  </head>

  <body id="searchbody">
    <form name="u2" target="workFrame" action="ins_search_list.asp" method="get">
<%
        SEARCH_PAGE_START({ header: L("lcl_filterblok") });
        SEARCH_BLOCK_START();
%>
        <input type="hidden" name="urole" value="<%=urole%>">
        <input type="hidden" name="self" value="<%=eigen%>">
        <input type="hidden" name="autfunction" id="autfunction" value="<%=autfunction%>">
<%
        if (fronto)
        {
            %><input type="hidden" name="free" value=""><%
        }
            if (xd.enabled())
                RWCHECKBOXTR("xd", "fldxd", L("lcl_xd_crossdomain"), false, {html: "onclick='xdclicked()'"});
        if (!frontend)
        {
%>
              <!-- Object -->
              <tr class="primsearch">
                <td class="label"><label for="deel"><%=L("lcl_obj_identification")%></label></td>
                <td><input type="text" class="fldshort wildcard" id="deel" name="deel" value="<%=safe.htmlattr(deel)%>"></td>
              </tr>
              <tr class="primsearch">
                <td class="label"><label for="deel_opm"><%=L("lcl_ins_opmerking")%></label></td>
                <td><input type="text" class="fld wildcard" id="deel_opm" name="deel_opm" value=""></td>
              </tr>

              <!-- Discipline -->
<%            if (insInspect)
                // Interne uitvoerder (backo2), alleen de taken van de eigen bedrijfdienstlocaties en daar de ins_disciplines van (IB2),
                // anders alleen de disciplines met inspecties (I).
                var dfiltercode = backo2? "IB2" : "I";
              else
                var dfiltercode = null;
              FCLTdisciplineselector("disc",
                                     "sgDisc",
                                     { label: L("lcl_discipline"),
                                       disc_key: dis_key > 0? dis_key : disc,
                                       module: "INS",
                                       filtercode: dfiltercode,
                                       perslidKey: user_key,
                                       autfunctionKey: authparams.autfunctionkey,
                                       autRW: "R",
                                       onChange: "onChangeDiscipline", // disgroepChanged
                                       readonly: dis_key > 0,
                                       trclass: "primsearch noxd",
                                       whenEmpty: L("lcl_search_generic"),  // want dit is een filter
                                       multitoggle: true,
                                       sgRelType: "srtgroep, srtdeel"   // Wijzigen van discipline heeft effect op de srtgroepselector en de srtdeelselector.
                                     });
%>

              <!-- Soort groep -->
<%            var dfiltercode = null;
              if (urole == "fo")
                var dfiltercode = "LEND";
              else if (backo2)
                // Interne uitvoerder (backo2), alleen de taken van de eigen bedrijfdienstlocaties en daar de ins_disciplines van (IB2),
                var dfiltercode = "B2";

              FCLTsrtgroepselector("srtgroep",
                                   "sgSrtgroep",
                                   { label: L("lcl_obj_group"),
                                     srtgroep_key: srtgroep,
                                     autfunction: autfunction,
                                     onChange: "onChangeSrtgroep",
                                     multitoggle: true,
                                     urlAdd: [{urlParam: "disc_key", field: "disc"}],
                                     trclass: "primsearch noxd",
                                     filtercode: dfiltercode,
                                     whenEmpty: L("lcl_search_generic"),  // want dit is een filter
                                     sgRelType: "srtdeel"   // wijzigen van groep heeft effect op de srtdeelselector.
                                   });
            }
%>
              <!-- Objectsoort -->
<%
            if (frontend)
            {
                sql = "SELECT DISTINCT s.ins_srtdeel_key"
                    + ", s.ins_srtdeel_code || ' ' || " + lcl.xsqla('s.ins_srtdeel_omschrijving', 's.ins_srtdeel_key')
                    + " FROM ins_v_aanwezigsrtdeel s"
                    + ", ins_v_aanwezigsrtgroep g"
                    + ", ins_v_srtdeel_kenmerk sk "
                    + " WHERE s.ins_srtgroep_key = g.ins_srtgroep_key"
                    + " AND sk.ins_srtdeel_key = s.ins_srtdeel_key"
                    + " AND sk.ins_kenmerk_meetwaarde = 1"  //pf:?
                    + " AND g.ins_discipline_key IN"
                      + " (SELECT gb.ins_discipline_key"
                      + " FROM fac_v_webgebruiker gb"
                      + ", fac_functie f"
                      + " WHERE prs_perslid_key = " + user_key
                      + " AND fac_functie_code = " + safe.quoted_sql(autfunction)
                      + " AND f.fac_functie_key = gb.fac_functie_key"
                      + " AND gb.fac_gebruiker_alg_level_read < 9)"
                    + " ORDER BY UPPER(s.ins_srtdeel_code || ' ' || " + lcl.xsql('s.ins_srtdeel_omschrijving', 's.ins_srtdeel_key') + ")";
                FCLTselector("srtdeel", // obj_sort
                             sql,
                             { initKey: srtdeel,
                               label: L("lcl_obj_sort"),
                               multi: true,
                               trclass:"primsearch noxd",
                               emptyOption: ""
                             });
            }
            else
            {
              var dfiltercode = null;
              if (urole == "fo")
                var dfiltercode = "LEND";
              else if (backo2)
                // Interne uitvoerder (backo2), alleen de taken van de eigen bedrijfdienstlocaties en daar de ins_disciplines van (IB2),
                var dfiltercode = "B2";

              FCLTsrtdeelselector("srtdeel",
                            "sgSrtdeel",
                            { label: L("lcl_obj_sort"),
                              srtdeel_key: srtdeel,
                              module: "INS",
                              autfunction: autfunction,
                              onChange: "onChangeSrtdeel",
                              multitoggle: true,
                              urlAdd: [{urlParam: "srtgroep_key", field: "srtgroep"},
                                       {urlParam: "disc_key", field: "disc"}],
                              trclass: "primsearch noxd",
                              filtercode: dfiltercode,
                              whenEmpty: L("lcl_search_generic")  // want dit is een filter
                            });
%>
              <!-- Objectsoort code en omschrijving-->
              <tr class="primsearch noxd">
                <td class="label"><label for="srtdeelomschr"><%=L("lcl_ins_objsrt_code")%></label></td>
                <td><input type="text" class="fldshort wildcard" name="srtdeelomschr" id="srtdeelomschr" value="<%=safe.htmlattr(srtdeelomschr)%>"></td>
              </tr>
<%
            }
%>
            <tr class="primsearch">
              <td class="label"><label for="note_text"><%=L("lcl_ins_note_tracking")%></label></td>
              <td><input type="text" class="fld wildcard" id="note_text" name="note_text" value=""></td>
            </tr>
<%

            // <!-- Groepering -->
            if (!insInspect && backo)
            {
              sql = "SELECT       -1, "  + safe.quoted_sql(L("lcl_ins_object"))     + ", 0 FROM DUAL"  
                  + " UNION SELECT 1, "  + safe.quoted_sql(L("lcl_discipline"))     + ", 1 FROM DUAL"
                  + " UNION SELECT 2, "  + safe.quoted_sql(L("lcl_obj_group"))      + ", 2 FROM DUAL"
                  + " UNION SELECT 3, "  + safe.quoted_sql(L("lcl_obj_sort"))       + ", 3 FROM DUAL"
                  + " UNION SELECT 11, " + safe.quoted_sql(L("lcl_prs_organisatie"))+ ", 6 FROM DUAL"
                  + " UNION SELECT 13, " + safe.quoted_sql(L("lcl_ins_manager"))    + ", 8 FROM DUAL";
              // Als er geen geo is, is groeperen daarop zinloos
              // Als via de parameter is gekozen dat je hier geen plaats wilt hebben is het ook logisch deze groepering achterwege te laten.
              if (anygeo && alg_startlevel >= 0)
              {
                  sql += " UNION SELECT 5, "  + safe.quoted_sql(L("lcl_mi_location"))    + ", 4 FROM DUAL"
                       + " UNION SELECT 6, "  + safe.quoted_sql(L("lcl_bld_terra"))      + ", 5 FROM DUAL"
                       + " UNION SELECT 12, " + safe.quoted_sql(L("lcl_bld_ter_verantw"))+ ", 7 FROM DUAL";
              }
              sql += " ORDER BY 3";
              FCLTselector("groupby",
                           sql,
                           { initKey: group>0? group : -1,
                             label: L("lcl_ins_group"),
                             trclass: "secsearch noxd"
                           });
            }

            if (insInspect) // Ook inspectiezoekvelden
            {
                if (ctrdisc_key_arr.length > 0)
                    var ctr_disc_sql = ctrdisc_key_arr.join(",");
                else
                    var ctr_disc_sql = "SELECT w.ins_discipline_key"
                                     + "  FROM fac_v_webgebruiker w"
                                     + "     , fac_functie f"
                                     + " WHERE w.fac_functie_key = f.fac_functie_key "
                                     + "   AND f.fac_functie_code = 'WEB_CTRUSE'"
                                     + "   AND w.fac_gebruiker_prs_level_read < 9"
                                     + "   AND w.fac_gebruiker_alg_level_read < 9"
                                     + "   AND w.prs_perslid_key = " + user_key;

              if (ctrdisc_key_arr.length == 0)
              {
                // <!-- Controle Type -->
                sql = "SELECT ins_discipline_key"
                    + "     , ins_discipline_omschrijving"
                    + "  FROM ctr_discipline"
                    + " WHERE ins_discipline_key IN (" + ctr_disc_sql + ")"
                    + (backo2 // Interne uitvoerder, alleen de taken van de eigen bedrijfdienstlocaties en daar de ctr_disciplines van.
                        ? " AND ins_discipline_key IN (SELECT isc.ctr_discipline_key"
                          + "                            FROM ins_srtcontrole isc"
                          + "                               , ins_v_allsrtinstallatie asi"
                          + "                           WHERE asi.ins_srtinstallatie_key = isc.ins_srtinstallatie_key"
                          + "                             AND asi.niveau = isc.ins_srtcontrole_niveau"
                          + "                             AND ctr_discipline_key IN (SELECT w.ins_discipline_key"
                          + "                                                         FROM fac_v_webgebruiker w"
                          + "                                                            , fac_functie f"
                          + "                                                        WHERE w.fac_functie_key = f.fac_functie_key"
                          + "                                                          AND f.fac_functie_code = 'WEB_CTRUSE'"
                          + "                                                          AND w.fac_gebruiker_prs_level_read < 9"
                          + "                                                          AND w.fac_gebruiker_alg_level_read < 9"
                          + "                                                          AND w.prs_perslid_key = " + user_key + ")"
                          + "                             AND isc.prs_dienst_key IN (SELECT DISTINCT d.prs_dienst_key"
                          + "                                                          FROM prs_bedrijfdienstlocatie bdl"
                          + "                                                             , prs_dienst d"
                          + "                                                         WHERE bdl.prs_dienst_key = d.prs_dienst_key"
                          + "                                                           AND d.prs_dienst_key = isc.prs_dienst_key"
                          + "                                                           AND bdl.prs_bedrijf_key IN (SELECT cp.prs_bedrijf_key"
                          + "                                                                                        FROM prs_contactpersoon cp"
                          + "                                                                                       WHERE cp.prs_perslid_key = " + user_key + ")))"
                        : "")
                    + " ORDER BY 2";
                FCLTselector("ctr_discipline",
                             sql,
                             { label: L("ctr_discipline"),
                               emptyOption: L("lcl_search_generic"),
                               multi: true
                             });
              }
              else
              { %>
                <input type="hidden" name="ctr_discipline" id="ctr_discipline" value="<%=ctrdisc_key_arr%>">
<%            }

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

              // <!-- Groep-->
              // Keuze uit alle gebruikte groepen, dynamisch bepaald. Er kan een (1) defaultwaarde meegegeven zijn,
              // dan wordt die geselecteerd en kun je ook geen andere opties meer kiezen. Bruikbaar om logische
              // scheiding in het menu aan te kunnen brengen.
              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 ctr_discipline_key IN (" + ctr_disc_sql + ")"
                      + (backo2 // Interne uitvoerder, alleen de diensten van de eigen bedrijfdienstlocaties.
                        ? " AND isc.prs_dienst_key IN (SELECT DISTINCT d.prs_dienst_key"
                          + "                            FROM prs_bedrijfdienstlocatie bdl"
                          + "                               , prs_dienst d"
                          + "                           WHERE bdl.prs_dienst_key = d.prs_dienst_key"
                          + "                             AND d.prs_dienst_key = isc.prs_dienst_key"
                          + "                             AND bdl.prs_bedrijf_key IN (SELECT cp.prs_bedrijf_key"
                          + "                                                           FROM prs_contactpersoon cp"
                          + "                                                          WHERE cp.prs_perslid_key = " + user_key + "))"
                        : "")
                      + "   AND xcp.ins_scenario_key(+) = 1"
                      + "   AND COALESCE (xcp.ins_srtcontroledl_xcp_groep, isc.ins_srtcontrole_groep) IS NOT NULL"
                      + (groep != "" ? " AND UPPER(COALESCE(xcp.ins_srtcontroledl_xcp_groep, isc.ins_srtcontrole_groep)) = " + safe.quoted_sql_upper(groep) : "")
                      + " ORDER BY UPPER (COALESCE(xcp.ins_srtcontroledl_xcp_groep, isc.ins_srtcontrole_groep))";
              FCLTselector("groep",
                           sql,
                           { label: L("ins_srtcontrole_groep"),
                             emptyOption: (groep == "" ? L("lcl_search_generic"): null),
                             trclass: "primsearch noxd"
                           });

             FCLTdienstselector("dienst_key","dienst_key",
                         { label: L("lcl_prs_companies_dienst"),
                           dienstKey : -1,
                           not_onChange: "onChangedDienst()",
                           trclass:"secsearch noxd",
                           filtercode: (backo2 ? "OWN" : null),
                           whenEmpty: L("lcl_search_generic")
                         });
              <!-- Onderhouds bedrijf -->
              // Geen dienst ingevuld: Alle bedrijven zijn mogelijk.
              // 1 Dienst ingevoerd: De bedrijven die de aangegeven dienst uitvoeren zijn mogelijk.
              // Filtercode:
              // (B2) Interne uitvoerder, alleen de bedrijven van de eigen bedrijfdienstlocaties.
              // (DC) Bedrijven die diensten (bedrijfdienstlocatie) leveren die ingezet worden bij periodieke taken plus bedrijven met een contract op inspecteerbare objecten en objectsoorten.
              FCLTbedrijfselector("cnt_bedrijf_key",
                                  "sgBedrijf",
                                  { companyKey: -1,
                                    label: L("lcl_company"),
                                    trclass: "primsearch noxd",
                                    filtercode: backo2? "B2" : "DC",
                                    urlAdd: [{urlParam: "dienst", field: "dienst_key"}],
                                    startempty: true,
                                    whenEmpty: L("lcl_search_generic")
                                  });

            }
            if (ins_score_enabled && !fronto)
            {
              // Conditie score
%>            <tr class="secsearch fldfitness_score" id="fitness_score">
                <td class="label">
                  <label for="fitness_score1_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>
<%
              // Grenswaarde conditie score
%>            <tr class="secsearch fldgwfitness_score" id="gwfitness_score">
                <td class="label">
                  <label for="gwfitness_score1_from"><%=L("ins_srtdeel_mjb_grensscore1")%></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="gwfitness_score1_from"
                         name="gwfitness_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="gwfitness_score1_through"
                         name="gwfitness_score1_through"
                         class="fldshort number "
                         value=""
                         maxlength="3">
                </td>
              </tr>
<%
              // Prioriteitsscore
%>            <tr class="secsearch fldpriority_score" id="priority_score">
                <td class="label">
                  <label for="priority_score2_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>
<%          }
            if (backo)
            { %>
              <!-- Ook onderdelen -->
              <tr class="secsearch">
                <td></td>
                <td align="left">
                  <input type="checkbox" class="fldinsobjincl" name="insObjIncl" id="insObjIncl" value="1" <%=insObjIncl? "checked" : ""%>>
                  <label for="insObjIncl"><%=L("lcl_subobjects_incl")%></label>
                </td>
              </tr>
<%          if (!insInspect) { %>
              <!-- Vervallen objecten -->
              <tr class="secsearch">
                <td></td>
                <td align="left">
                  <input type="checkbox" class="fldexpobjincl" name="expObjIncl" id="expObjIncl" value="1" <%=expObjIncl? "checked" : ""%>>
                  <label for="expObjIncl"><%=L("lcl_expobjects_incl")%></label>
                </td>
              </tr>
              <!-- Inactieve objecten -->
              <tr class="secsearch">
                <td></td>
                <td align="left">
                  <input type="checkbox" class="fldinacobjincl" name="inacObjIncl" id="inacObjIncl" value="1" <%=inacObjIncl? "checked" : ""%>>
                  <label for="inacObjIncl"><%=L("lcl_inactiveobjects_incl")%></label>
                </td>
              </tr>
<%          }
          }

          if (S("ins_deel_flags") > 1) // Flags checkboxen -->
          {
              for (flagkey=0; flagkey < S("ins_deel_flags"); flagkey++)
              {
                    var label = "<label for='flag" + flagkey + "'>"
                              + "  <span class='insflag" + flagkey + " inss'>"
                              +      I("fa-fclt-flag" + (flagkey == 0 ? "0" : ""))
                              + "  </span> " + safe.html(L("lcl_ins_flag" + flagkey))
                              + "</label>";
                    RWCHECKBOXTR("flag" + flagkey, "fldcheck", (flagkey == 0 ? L("lcl_ins_flags") : ""), 1, { twocols: true, posthtml: label });
              }
          }

          SEARCH_BLOCK_END();
          SEARCH_BLOCK_START();

          if (anygeo && (backo || frontend || fronto))
          {
                if (backo)
                  {
                    if (alg_startlevel >= 0)
                    FCLTplaatsselector(authparams.ALGreadlevel, { regiokey: reg_key,
                                                                  districtkey: dist_key,
                                                                  locatiekey: loc_key,
                                                                  gebouwkey: bld_key,
                                                                  verdiepingkey: flr_key,
                                                                  ruimtekey: room_key,
                                                                  werkplekkey: wpl_key, // indien eindlevel 5 is, dan wordt deze parameter niet gebruikt
                                                                  terrein: true,
                                                                  multitoggle: 5,
                                                                  multionchange: true,
                                                                  onRegChange: alg_startlevel==0? "UnSelectOwner()" : null,
                                                                  onDisChange: alg_startlevel==1? "UnSelectOwner()" : null,
                                                                  onLocChange: alg_startlevel==2? "UnSelectOwner()" : null,
                                                                  onGebChange: alg_startlevel==3? "UnSelectOwner()" : null,
                                                                  onVerChange: alg_startlevel==4? "UnSelectOwner()" : null,
                                                                  onRuiChange: alg_startlevel==5? "UnSelectOwner()" : null,
                                                                  escalateOnChUp: true, // ook onLocChange als je gebouw wijzigt
                                                                  startlevel: alg_startlevel,
                                                                  eindlevel: alg_endlevel, // ruimte/werkplek
                                                                  filtercode: backo2? "INCVRB2" : "INCVR",
                                                                  trclass: "noxd",
                                                                  cadSelect: true,
                                                                  cadShowdiscfn: true,
                                                                  whenEmpty: L("lcl_search_generic")  // want filter
                                                                });
                  }
                  else
                  {
                    if (alg_startlevel >= 0)
                    FCLTplaatsselector(authparams.ALGreadlevel, { locatiekey: loc_key,
                                                                  startlevel: alg_startlevel,
                                                                  eindlevel: (frontend ? 2 : 3), // frontend: locatie(2), fronto: gebouw(3)
                                                                  multitoggle: 5,
                                                                  multionchange: true,
                                                                  trclass: "noxd",
                                                                  whenEmpty: L("lcl_search_generic"),  // want filter
                                                                  cadSelect: true,
                                                                  cadShowdiscfn: true,
                                                                  filtercode: "EXP"
                                                                });
                  }
                  // 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: authparams.PRSreadlevel,
                                         whenEmpty: L("lcl_search_generic")  // want filter
                                       });
            }

            // Fast check to see if we have anything to do with personal objects.
            // Could also be checked on ins_srtdeel_binding if performance becomes an issue.
            // If not, we can suppress a few filters.
            sql= "SELECT 1 FROM ins_deel WHERE ins_deel_verwijder IS NULL AND ins_alg_ruimte_type IN ('A','P') AND ROWNUM = 1";
            oRs = Oracle.Execute(sql);
            var anypersonal = !oRs.eof;
            oRs.close();
            if (backo && anypersonal)
            {
              //<!-- Afdeling/Organisatie -->
                FCLTafdelingselector("sDept_key", // organisation
                                     "sgDept",
                                     { departmentKey: dep_key,
                                       label: L("lcl_prs_organisatie"), // Organisatie, is eigenlijk afdeling: dep_name_level2
                                       autlevel: authparams.PRSreadlevel, // Was voorheen niet aanwezig
                                       trclass: (insInspect?"secsearch":"primsearch") + " noxd",
                                       onChange: "UnSelectPos",
                                       whenEmpty: L("lcl_search_generic")  // want filter
                                     });

            }

            if (!frontend && anypersonal)
            {
              //<!-- Persoon -->
                FCLTpersoonselector("sName_key", // requestor
                                    "sgPerson",
                                    { perslidKey: prs_key,
                                      readonly: eigen == 1,
                                      label: L("lcl_obj_bind_p"),
                                      onChange: (fronto? null : "UnSelectPos"),
                                      trclass: ((insInspect&&!eigen)?"secsearch":"primsearch") + " noxd",
                                      autlevel: authparams.PRSreadlevel,
                                      whenEmpty: L("lcl_search_generic")  // want filter
                                    });

            }
            if (prs_beh_key > 0)
            {
              %><input type='hidden' name='sBeh_key' value='<%=prs_beh_key%>'><%
            }
            else
            {
               sql= "SELECT 1"
                  + " FROM ins_deel"
                  + " WHERE prs_perslid_key_beh IS NOT NULL"
                  + " AND ins_deel_verwijder IS NULL"
                  + " AND ROWNUM = 1";
               oRs = Oracle.Execute(sql);
               var anyobjman = !oRs.eof;
               oRs.close();
              if (anyobjman)
              {
               // <!-- Beheerder -->
               FCLTpersoonselector("sBeh_key", // manager
                                   "sgBeh",
                                   { label: L("lcl_ins_manager"),
                                     filtercode: "INS",  // TODO: voor inspectievariant een ander (scherper) filter?
                                     trclass: "primsearch noxd",
                                     autlevel: authparams.PRSreadlevel,
                                     whenEmpty: L("lcl_search_generic")  // want filter
                                   });
              }
            }
            if (backo)
            {
              // met afbeeldingen, alleen sensoren
%>
              <tr class="secsearch">
                <td></td>
                <td align="left">
                  <input type="checkbox" class="insphotoincl" name="insPhotoIncl" id="insPhotoIncl" value="1" <%=insPhotoIncl? "checked" : ""%>>
                  <label for="insPhotoIncl"><%=L("lcl_photos_incl")%></label>
                </td>
              </tr>
              <tr class="secsearch">
                <td></td>
                <td align="left">
                  <input type="checkbox" class="fldinssensoronly" name="insSensOnly" id="insSensOnly" value="1" <%=insSensOnly? "checked" : ""%>>
                  <label for="insSensOnly"><%=L("lcl_sensors_only")%></label>
                </td>
              </tr>
<%
            }
            if (fronto)
            { %>
              <!-- Uitgegeven, beschikbaar -->
              <tr class="primsearch">
                <td></td>
                <td align="left">
                  <input type="checkbox" class="fldinslentout" name="insLentOut" id="insLentOut" value="1" onClick="checkLentOut(1);" <%=insLentOut? "checked" : ""%>>
                  <label for="insLentOut"><%=L("lcl_obj_lent_out")%></label>
                  <input type="checkbox" class="fldinsavail" name="insAvail" id="insAvail" value="1" onClick="checkLentOut(2);" <%=insAvail? "checked" : ""%>>
                  <label for="insAvail"><%=L("lcl_obj_available")%></label>
                </td>
              </tr>
<%
              var sql = "SELECT 0, '' FROM DUAL UNION "
                      + "SELECT 1, " + safe.quoted_sql(L("lcl_noti_INSOUT")) + " FROM DUAL UNION "
                      + "SELECT 2, " + safe.quoted_sql(L("lcl_noti_INSINN")) + " FROM DUAL UNION "
                      + "SELECT 3, " + safe.quoted_sql(L("lcl_noti_INSOUT") + "/" + L("lcl_noti_INSINN")) + " FROM DUAL";
              FCLTselector( "lentoutAction"
                          , sql
                          , { initKey: lentoutAction,
                               label: L("lcl_ins_actie"),
                               multi: false,
                               trclass:"secsearch noxd",
                               emptyOption: null
                             });
              // periode van actie
              FCLTcalendar( "lentoutDate_from"
                          , { label : L("lcl_ins_action_from")
                            , initEmpty: true
                            , trclass: "secsearch"
                            });
              FCLTcalendar( "lentoutDate_to"
                          , { label: L("lcl_ins_action_to")
                            , initEmpty: true
                            , trclass: "secsearch"
                            });
            }
            var today = new Date();
            if (insInspect) // Ook inspectiezoekvelden
            { %>
              <tr class="primsearch">
                <td class="label">
                      <label><%=L("lcl_ins_controle")%></label>
                </td>
                <td>
                  <input type="hidden" name="inspect" value="1">
                  <table>
                    <tr class="insD">
                      <td>
                        <input type="checkbox" class="fldcheck" name="insInspDone" id="insInspDone" value="1" onClick="onClickDone()">
                      </td>
                      <td>
                        <label for="insInspDone"><%=L("lcl_ins_controle_done")%></label>
                      </td>
                    </tr>
                    <tr class="primsearch donetime" style="display:none">
                      <td></td>
                      <td>
                      <label for="show_donedate_from"><%=L("lcl_period_from")%></label>
                      </td><td>
<%                    // <!-- Datum -->
                      FCLTcalendar( "donedate_from",
                                   { datum: new Date(today.getTime() - 2*24*60*60*1000),
                                     volgnr: 1,
                                     maxFuture: 0,
                                     nofollow: true
                                   });
%>
                      </td>
                    </tr>
                    <tr class="primsearch donetime" style="display:none">
                      <td></td>
                      <td>
                      <label for="show_donedate_from"><%=L("lcl_period_to")%></label>
                      </td><td>
<%                    // <!-- Datum tot -->
                      FCLTcalendar( "donedate_to",
                                   { datum: new Date(today.getTime() - 1*24*60*60*1000),
                                     volgnr: 2,
                                     maxFuture: 0,
                                     nofollow: true
                                   });
%>
                  </table>
                  <div id="insLSF">
                    <table>
                      <!-- Gestarte/begonnen inspecties (status 2)) -->
                      <tr><td><input type="checkbox" class="fldcheck" name="insInspBusy" id="insInspBusy" value="1" checked <%=inspBusy? "checked" : ""%>></td><td><label for="insInspBusy"><%=L("lcl_ins_controle_busy")%></label></td></tr>
                      <!-- Actie vereist (Afgemelde inspecties (status 5)) -->
                      <tr><td align="left"><input type="checkbox" class="fldcheck" name="insInspClosed" id="insInspClosed" value="1" checked <%=inspClosed? "checked" : ""%>></td><td><label for="insInspClosed"><%=L("lcl_ins_controle_act_req")%></label></td></tr>
                      <!-- Binnenkort. Zonder begindatum is te laat + binnenkort. Zonder einddatum is binnenkort + toekomst -->
                      <tr><td><input type="checkbox" class="fldcheck" name="insInspSoon" id="insInspSoon" value="1" CHECKED></td><td><label for="insInspSoon"><%=L("lcl_ins_controle_soon")%></label></td></tr>
                      <tr>
                        <td></td>
                        <td>
                        <label for="show_inspsoon_from"><%=L("lcl_period_from")%></label>
                        </td><td>
<%                      FCLTcalendar("inspsoon_from",
                                     { datum: today,
                                       initEmpty: true,
                                       minDate: new Date(),
                                       volgnr: 3,
                                       nofollow: true
                                     });
%>
                        </td>
                      </tr>
                      <tr>
                        <td></td>
                        <td>
                        <label for="show_inspsoon_to"><%=L("lcl_period_to")%></label>
                        </td><td>
<%                      var soondate = new Date(new Date().setDate(today.getDate() + S("ins_inspect_soon_days")));
                        FCLTcalendar("inspsoon_to",
                                     { datum:  soondate,
                                       volgnr: 4,
                                       nofollow: true
                                     });
%>
                        </td>
                      </tr>
                    </table>
                  </div>
                </td>
              </tr>
              <tr class="primsearch donetime" style="display:none">
                <td class="label">
                      <label><%=L("ins_controlemode_success")%></label>
                </td>
                <td>
                  <input type="hidden" name="inspect" value="1">
                  <table>
                    <tr class="insD">
                      <td>
                        <input type="checkbox" class="fldcheck" name="insSuccessY" id="insSuccessY" value="1" checked onClick="onClickSuccess()">
                      </td>
                      <td>
                        <label for="insSuccessY"><%=L("lcl_Yes")%></label>
                      </td>
                      <td>
                        <input type="checkbox" class="fldcheck" name="insSuccessN" id="insSuccessN" value="1" checked onClick="onClickSuccess()">
                        <label for="insSuccessN"><%=L("lcl_No")%></label>
                      </td>
                      <td>
                      </td>
                    </tr>
                  </table>
                </td>
              </tr>
<%

            } %>

<%    SEARCH_BLOCK_END();

      var buttons = [{title: L("lcl_overview_list"),  action: "doSubmit()", importance: 1, icon: "fa-clipboard-list" }];

      if (fronto || backo)
          buttons.push ( {title: L("lcl_shared_more"),  action:"iface_toggleSecondarySearchblock()", id : "btnMoreLess", importance: 3, icon: "fa-chevron-right" } );
          buttons.push ( {title: L("lcl_shared_advanced"), action:"myModal()", id: "btnAdvanced", xclass: "noxd", importance: 3, icon: "fa-filter" } );
      SIMPLE_BLOCK_START();
        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(); %>
