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

    File:               mld_object.asp
    Description:        (Modal)dialoog om objecten aan melding te kunnen koppelen
    Globalen:
        mld_key         Nodig?
    Context:
    Note:
*/
%>

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/selector.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<!-- #include file="./mld.inc" -->
<!-- #include file="../Shared/get_objecten_sql.inc" -->

<%
FCLTHeader.Requires({ plugins: ["jQuery"] });

var mld_key = getQParamInt("mld_key");
// Selects all objects that are present in a given room and/or owned by a given person
// Since FSN#150, this is not restricted by any Objects authorizations.
var alg_key = getQParamInt("alg_key", -1);
var alg_niveau = getQParam("alg_niveau", -1);
var prs_key = getQParamInt("person", -1);
var stdmld_key = getQParamInt("stdm_key");
var inst_key = '';
var inst_type = '';
%>

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

      $(document).ready(function ()
                        {
                          var params = FcltMgr.dialogArguments();
                          $("#fldobjExist").html(params.obj_html); // De options (html) komen tussen <SELECT id="fldobjExist" ...> en </SELECT>
                          $('#fldobjExist').change(show_info_exist);
                          $('#fldobjPossible').change(show_info_possible);
                          //
                          if ($('#fldobjExist option').length == 1)
                          {
                            $('#fldobjExist option').eq(0).attr('selected', 'selected');
                            show_info_exist();
                          }
                          else
                          {
                            if ($('#fldobjPossible option').length == 1)
                            {
                              $('#fldobjPossible').eq(0).attr('selected', 'selected');
                              show_info_possible();
                            }
                          }
                          $("#fldobjPossible").filterByText("#autofilter", true);
                          $("#autofilter").focus();

                          //addTooltip(); // Wegens performance hier niet.
                        });

        function delayed(delay, fn)
        {
            if (window.timerID)
                clearTimeout(window.timerID);
            window.timerID = setTimeout(fn, delay);
        }
        function doFilter(deze)
        {
            var deze = $("#autofilter");
            var valThis = deze.val().toLowerCase();
            $('table.rstable>tbody>tr').each(function(){
                var text = $(this).text().toLowerCase();
                $(this).toggle(text.indexOf(valThis) > -1);
           });
        }

      function addTooltip()
      { // De gekoppelde objecten krijgen ze clientside door via dialogArguments.
        // Zoek de kenmerken erbij in de lijst met mogelijke objecten.
        var objexist_key;
        var objposib_key;
        $('#fldobjExist option').each(function(j, selected)
            {
                objexist_key = $(selected).val();
                var poss = $('#fldobjPossible option[value='+objexist_key+']');
                $(selected).attr("title", poss.attr("title"));
            });
      }

      function show_info_possible()
      {
        var sel = $('#fldobjPossible :selected');
        show_info(sel);
      }
      function show_info_exist()
      {
        var sel = $('#fldobjExist :selected');
        show_info(sel);
      }
      function show_info(obj)
      {
        if (obj.length == 1)
        {
          var ins_key = obj[0].value;
          var dest = "mld_load_obj_info.asp?ins_key=" + ins_key
                   + "&max_rows=3"; // maximaal aantal te tonen kenmerken
          $("#obj_info").load(dest);
        }
        else
           $("#obj_info").html("");
        FcltMgr.resized(window);
      }




      function moveRight(obj)
      {
        var object_key_str = new Array();
        var object_text_str = new Array();
        var object_title_str = new Array();
        var object_uitvoertijd_str = new Array();
        $('#fldobjPossible :selected').each(function(j, selected)
                                            {
                                              object_key_str[j] = $(selected).val();
                                              object_text_str[j] = $(selected).text();
                                              object_title_str[j] = $(selected).attr("title");
                                              object_uitvoertijd_str[j] = $(selected).attr("uitvoertijd");
                                            });

        var exist = false;
        for (var i = 0; i < object_key_str.length; i++)
        {
          exist = false;
          $('#fldobjExist option').each(function(j, selected)
                                           {
                                             if (object_key_str[i] == $(selected).val() && object_text_str[i] == $(selected).text())
                                             exist = true;
                                           });

          if (!exist)
          {
            $('#fldobjExist').append($('<option>', { value: object_key_str[i]
                                                   , text:  object_text_str[i]
                                                   , title: object_title_str[i]
                                                   , uitvoertijd: object_uitvoertijd_str[i]
                                                   }));
          }
        }
      }

      function removeObj(obj)
      {
        $('#fldobjExist option:selected').remove();
      }

      function obj_info()
      {
         var sel = $('#fldobjPossible :selected');
         if (sel.length > 0)
         {
            var ins_key = sel[0].value;
            var url = "appl/ins/ins_deel.asp?urole=bo&ins_key=" + ins_key;
            FcltMgr.openDetail(url, "");
         }
      }
    </script>
  </head>
  <body class="modal" id="mod_mldobj">
    <form name="u2" action="mld_object.asp?submit=1&mld_key=<%=mld_key%>" method="post" onsubmit="return false;">
<%    BLOCK_START("mld_selobj", L("lcl_mld_objects_select")) %>
      <tr>
        <td width="45%">
          <img title='<%=L("lcl_mld_deel_details")%>' class='details' src='<%=rooturl%>/appl/Pictures/detail.png' onclick='obj_info()'>&nbsp;
            <label class="details"><%=L("lcl_mld_objects_available") + ':'%></label><br>
            <input id='autofilter' type='text' placeholder='<%=L("lcl_autofilter")%>'>
<%
          if (!isNaN(alg_key))
          {
            var sql = get_objecten_sql(alg_key, alg_niveau, prs_key, null, stdmld_key);
            FCLTselector("fldobjPossible", sql, {startmulti: true, size: 10, extraParamValue: "uitvoertijd"} );
%>
            </td>
            <td class="fldobjmidcolumn">
              <div class="mv r" onclick="moveRight()"><%=L("lcl_mld_object_add")%></div>
              <div class="mv l" onclick="removeObj()"><%=L("lcl_mld_object_remove")%></div>
            </td>
            <td width="45%"><label><%=L("lcl_mld_objects_selected") + ':'%></label><br>
<%            sql = "SELECT 1 FROM DUAL WHERE 1=0"; // We krijgen ze clientside door via dialogArguments
              FCLTselector("fldobjExist", sql, {startmulti: true, size: 12});
          }
%>
        </td>
      </tr>
      <tr>
        <td colspan='3'>
          <div id="obj_info"><br><br><br><br><br></div>
        </td>
      </tr>
<%    BLOCK_END()

    var buttons = [{title: L("lcl_submit"), icon: "opslaan.png", action: "FcltMgr.closeDetail(window, { obj_html: $('#fldobjExist').html() })"},
                   {title: L("lcl_cancel"), icon: "undo.png", action: "FcltMgr.closeDetail(window, { cancel: true })"}];
    CreateButtons(buttons);
IFACE.FORM_END();
%>
    </form>
  </body>
</html>
