<%@language = "javascript" %>
<% /*
    $Revision$
    $Id$
    File:           INS/ins_search.asp
    Description:    Menuoptie zoeken objecten
    Parameters:     urole:      bo is objectenbeheer,
                                fo is uitleenmodule
                                fe is ????
                    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/calendar.inc" -->
<!-- #include file="../Shared/xd.inc" -->

<%

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

/***** Get webform parameters *****/
var urole = getQParamSafe("urole");
var fronto = (urole == "fo");
var backo = (urole == "bo");
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", ""); // Identieficatie
var dis_key = getQParamInt("dis_key", -1); // Discipline readonly selecteren
var disc = getQParamInt("disc", -1); // Discipline initialisatie
var srtgroep = getQParamInt("srtgroep", -1); // Groep
var srtdeel = getQParamInt("srtdeel", -1); // Objectsoort
var srtdeelomschr = getQParam("srtdeelomschr", ""); // Srtdeel code en/of omschrijving
var groep = getQParam("groep", ""); // Groep
var controletype = getQParamInt("controletype", -1); // Controle type
var dep_key = getQParamInt("dep_key", -1); // Organisatie key
var prs_key = getQParamInt("prs_key", -1); // Persoon key
var alg_startlevel = getQParamInt("alg_startlevel", 2); // Startlevel van plaatsselector (Locatie)
var alg_endlevel = getQParamInt("alg_endlevel", 5); // Eindlevel van plaatsselector (Locatie)
var prs_beh_key = getQParamInt("prs_beh_key", -1); // Beheerder

var insObjIncl = getQParamInt("insObjIncl", 0) == 1; // Ook onderdelen
var expObjIncl = getQParamInt("expObjIncl", 0) == 1; // Ook vervallen objecten
var insLentOut = getQParamInt("insLentOut", 1) == 1; // Uitgegeven
var insAvail = getQParamInt("insAvail", 1) == 1; // Beschikbaar

var insInspect = getQParamInt("inspect", 0) == 1; // inspectievelden toevoegen
var inspBusy = getQParamInt("inspBusy", 0) == 1; // Gestarte/begonnen inspecties (status 2).
var inspClosed = getQParamInt("inspClosed", 0) == 1; // Afgemelde inspecties (status 5). Actie vereist.

/***** End get webform parameters *****/

var autfunction = "";
switch(urole)
{
  case "fe": autfunction = "WEB_INSUSE";
           break;
  case "fo": autfunction = "WEB_INSFOF";
           break;
  case "bo": 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);
%>

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

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

<% if (autosearch) { %>
      $(document).ready(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 (backo)
        { %>
          // Submitten met kenmerken
          doSubmitWithKenmerken();
<%      }
        else
        { %>
          document.forms.u2.submit();
<%      } %>
      }
      //function fcltontabshow()
      //{
      //    if (anysearch)
      //        doSubmit();
      //}

<%    if (backo)
      { %>
        function myModal()
        {
          var url = "load_kenmerk.asp?urole=<%=urole%>"
                  + "&advanced=1"
                  + "&disc="      +$("#disc").val()
                  + "&srtgroep="  +$("#srtgroep").val()
                  + "&srtdeel="   +$("#srtdeel").val()
                  + "&objsrt_oms="+$("#srtdeelomschr").val();
          var titel = "<%=L("lcl_ins_kenmerken")%>";
          showKenmerkModal(url, titel)
        }
<%    } %>
      function onClickDone()
      {
        if (document.getElementById("insInspDone").checked)
        {
          $("#insLSF").hide();
          $(".donetime").show();
        }
        else
        {
          $(".donetime").hide();
          $("#insLSF").show();
        }
      }
<% if (xd.enabled()) { %>
      function xdclicked()
      {
        var doxd = document.getElementById("xd").checked;
        $(".noxd").toggle(!doxd);
        $(".onlyxd").toggle(doxd);
        iface_toggleSecondarySearchblock(false); // altijd uitschakelen
      }
<% } %>
    </script>
  </head>

  <body id="searchbody">
    <div id="search">
      <form name="u2" target="workFrame" action="ins_search_list.asp" method="get">
        <input type=hidden name="urole" value="<%=urole%>">
        <input type=hidden name="autfunction" id="autfunction" value="<%=autfunction%>">
<%
        if (fronto)
        { %>
          <input type=hidden name=free value="">
<%      } %>
<%    BLOCK_START("searchtable", L("lcl_filterblok"));%>
        <tr>
          <td class="searchkolom1"><!-- start column 1 -->
            <table><!-- x rijen, 2 kolommen: label + veld -->
<%
            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="flddeel" 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" name="deel_opm" value=""></td>
              </tr>

              <!-- Discipline -->
<%            FCLTdisciplineselector("disc",
                                     "sgDisc",
                                     { label: L("lcl_discipline"),
                                       disc_key: dis_key > 0? dis_key : disc,
                                       module: "INS",
                                       filtercode: insInspect? "I" : null,  // dan alleen disciplines met inspecties
                                       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
                                     });
%>

              <!-- Soort groep -->
<%            FCLTsrtgroepselector("srtgroep",
                                   "sgSrtgroep",
                                   { label: L("lcl_obj_group"),
                                     srtgroep_key: srtgroep,
                                     autfunction: autfunction,
                                     onChange: "onChangeSrtgroep",
                                     urlAdd: [{urlParam: "disc_key", field: "disc"}],
                                     trclass: "primsearch noxd",
                                     filtercode: urole == "fo" ? "LEND" : null,
                                     whenEmpty: L("lcl_search_generic")  // want dit is een filter
                                   });
            }
%>
              <!-- 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
            {
              FCLTsrtdeelselector("srtdeel",
                            "sgSrtdeel",
                            { label: L("lcl_obj_sort"),
                              srtdeel_key: srtdeel,
                              module: "INS",
                              autfunction: autfunction,
                              onChange: "onChangeSrtdeel",
                              urlAdd: [{urlParam: "srtgroep_key", field: "srtgroep"},
                                       {urlParam: "disc_key", field: "disc"}],
                              trclass: "primsearch noxd",
                              filtercode: urole == "fo" ? "LEND" : null,
                              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="fldsrtdeel" name="srtdeelomschr" id="srtdeelomschr" value="<%=safe.htmlattr(srtdeelomschr)%>"></td>
              </tr>
<%
            }

        if (insInspect) // Ook inspectiezoekvelden
        {
          if (controletype == -1)
          {
            // <!-- Controle Type -->
            sql = "SELECT         1, " + safe.quoted_sql(L("lcl_ins_srtcontrole_insp")) + " FROM DUAL" // Controle.
                + " UNION SELECT  2, " + safe.quoted_sql(L("lcl_ins_srtcontrole_repl")) + " FROM DUAL" // Vervanging.
                + " UNION SELECT  3, " + safe.quoted_sql(L("lcl_ins_srtcontrole_cert")) + " FROM DUAL" // Certificering.
                + " ORDER BY 2";
            FCLTselector("controletype",
                         sql,
                         { label: L("lcl_ins_srtcontrole_insptype"),
                           emptyOption: L("lcl_all")
                         });
          }
          else
          { %>
            <input type=hidden name="controletype" id="controletype" value="<%=controletype%>">
<%        }

          var sql = "SELECT ins_srtcontrole_key,"
//                  + lcl.xsqla('ins_srtcontrole_omschrijving', 'ins_srtcontrole_key') + ","
                  + lcl.xsql('ins_srtcontrole_omschrijving', 'ins_srtcontrole_key')
                  + "||DECODE(ins_srtcontrole_niveau, 'S', ' '||asi.srtdeel_oms, 'G', ' '||asi.srtgroep_oms, 'D', ' '||asi.discipline_oms, '') ins_srtcontrole_omschrijving"
                  +  ","
                  + "       ins_srtcontrole_periode"
                  + "  FROM ins_srtcontrole isc, ins_v_allsrtinstallatie asi"
                  + "  WHERE asi.ins_srtinstallatie_key = isc.ins_srtinstallatie_key"
                  + "  AND asi.niveau = isc.ins_srtcontrole_niveau"
                  + "  ORDER BY UPPER("+lcl.xsql('ins_srtcontrole_omschrijving', 'ins_srtcontrole_key')+")";
          FCLTselector("sel_srtcontrole",
                       sql,
                       { label: L("lcl_ins_controle_srt"),
                         emptyOption: L("lcl_all")
                       });

          // <!-- Groep-->
          var sql = "SELECT MIN(ins_srtcontroledl_xcp_key)"
                  + "     , xcp.ins_srtcontroledl_xcp_groep"
                  + "  FROM ins_srtcontroledl_xcp xcp"
                  + " WHERE xcp.ins_srtcontroledl_xcp_groep IS NOT NULL"
                  + " GROUP BY xcp.ins_srtcontroledl_xcp_groep"
                  + " ORDER BY UPPER(xcp.ins_srtcontroledl_xcp_groep)";
          FCLTselector("groep",
                       sql,
                       { label: L("lcl_ins_controle_groep"),
                         emptyOption: L("lcl_all"),
                         trclass: "secsearch noxd"
                       });

          <!-- Onderhouds bedrijf -->
          FCLTbedrijfselector("cnt_bedrijf_key",
                              "sgBedrijf",
                              { companyKey: -1,
                                label: L("lcl_company"),
                                trclass: "primsearch noxd",
                                filtercode: "CI", // Contract onderhoud inspectieobjecten
                                whenEmpty: L("lcl_search_generic")
                              });

        }

        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>
              <!-- 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>
<%          } %>
            </table>
          </td><!-- end column 1 -->

          <!-- Second column -->
          <td valign=top>
            <table><!-- x rijen, 2 kolommen: label + veld -->

<%          if (backo || frontend || fronto)
            { %>
<%                if (backo)
                  {
                    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,
                                                                  onLocChange: "UnSelectOwner()",
                                                                  escalateOnChUp: true, // ook onLocChange als je gebouw wijzigt
                                                                  startlevel: alg_startlevel,
                                                                  eindlevel: alg_endlevel, // ruimte/werkplek
                                                                  filtercode: "INCVR",
                                                                  trclass: "noxd",
                                                                  whenEmpty: L("lcl_search_generic")  // want filter
                                                                });
                  }
                  else if (frontend)
                  {
                    FCLTplaatsselector(authparams.ALGreadlevel, { locatiekey: loc_key,
                                                                  startlevel: alg_startlevel,
                                                                  eindlevel: 2, // locatie
                                                                  trclass: "noxd",
                                                                  whenEmpty: L("lcl_search_generic")  // want filter
                                                                });
                  }
                  else //fronto
                  {
                    FCLTplaatsselector(authparams.ALGreadlevel, { locatiekey: loc_key,
                                                                  startlevel: alg_startlevel,
                                                                  eindlevel: 3, // gebouw
                                                                  trclass: "noxd",
                                                                  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,
                                      label: L("lcl_obj_bind_p"),
                                      onChange: (fronto? null : "UnSelectPos"),
                                      trclass: (insInspect?"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 (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 today = new Date();
            if (insInspect) // Ook inspectiezoekvelden
            { %>
              <tr class="primsearch">
                <td class="label">
                      <label><%=L("lcl_ins_controle")%>:&nbsp;</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_from")%></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,
                                       minDate: new Date(),
                                       volgnr: 4,
                                       nofollow: true
                                     });
%>
                        </td>
                      </tr>
                    </table>
                  </div>
                </td>
              </tr>
<%          } %>
            </table>
          </td><!-- end column 2-->
        </tr>

<%    BLOCK_END();

      var buttons = [{title: L("lcl_search"),  action: "doSubmit()" }];
      if (backo)
      {
          buttons.push ( {title: L("lcl_shared_more"),  action:"iface_toggleSecondarySearchblock()", id : "btnMoreLess" } );
          buttons.push ( {title: L("lcl_obj_advanced"), action:"myModal()", id: "btnAdvanced", xclass: "noxd" } );
      }
      CreateButtons(buttons, { entersubmit: true} );
%>
      </form>

    </div><!--search-->

    <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>
