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

    File:               deelwerk_meterial.asp
    Description:        (Modal)dialoog om materialen aan haltewerkzaamheden urenlijst te kunnen koppelen
    Globalen:
        matkeystr       String van keys met geselecteerde materialen
    Context:
    Note:
*/
%>

<!-- #include file="../../../appl/shared/common.inc" -->

<% FCLTHeader.Set({root: "../../../appl/"}); %>

<!-- #include file="../../../appl/Shared/selector.inc" -->
<!-- #include file="../../../appl/Shared/iface.inc" -->

<%
var mld_material_fac_usrtab_key = 881;

FCLTHeader.Requires({ plugins: ["jQuery"],
                      js: []
                    })

var matkeystr = getQParamIntArray("matkeystr", []);
var matamountstr = getQParamArray("matamountstr", []);
%>

<html>
  <head>
<%  FCLTHeader.Generate() %>
    <script>
      var matkeystrArr = "<%=matkeystr.join(",")%>".split(",");
      var matamountstrArr = "<%=matamountstr.join(",")%>".split(",");

      function moveRight(obj)
      {
        var object_key_str = new Array();
        var object_text_str = new Array();
        $('#matPossible :selected').each(function(j, selected)
                                            {
                                              object_key_str[j] = $(selected).val();
                                              object_text_str[j] = $(selected).text();
                                            });

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

          if (!exist)
            $('#matExist').append($(document.createElement("option")).attr("value", object_key_str[i]).text(object_text_str[i]));
        }
      }

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

      function obj_info()
      {
         var sel = $('#matPossible :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, "");
         }
      }

      function makeMaterialHtml(matExistArray)
      {
        var matHtml = "";
        for (var i = 0; i < matExistArray.length; i++)
        {
          matHtml += "<tr id='mat" + matExistArray[i].key + "' valign='top'>"
                   + "<td>"
                   + "<input class='fldmaterialen number' type='text' name='amount" + matExistArray[i].key + "'"
                   + " id='amount" + matExistArray[i].key + "' value='" + matExistArray[i].amount + "'"
	                 + " index='" + i + "' maxlength='4' onChange='onChangeAmount();'"
                   + " onKeyPress='checkKey();'>&nbsp;</td>"
	                 + "<td>" + matExistArray[i].descr + "</td></tr>";
        }
        return "<table>" + matHtml + "</table>";
      }

      function submit()
      {
        function materialStrings(key_str, descr_str, amount_str)
        {
          this.key_str = key_str;
          this.descr_str = descr_str;
          this.amount_str = amount_str;
        }

        function material(key, amount, descr)
        {
          this.key = key;
          this.amount = amount;
          this.descr = descr;
        }

        var matExistKeyStr = "";
        var matExistTxtStr = "";
        var matExistAmountStr = "";
        var matExistArray = new Array();
        $("#matExist option").each(function (index, domEle)
                                   {
                                     var amount = 1;
                                     for (var i = 0; i < matkeystrArr.length; i++)
                                     {
                                       if (matkeystrArr[i] == domEle.value)
                                       {
                                         amount = matamountstrArr[i];
                                         break;
                                       }
                                     }
                                     matExistArray[matExistArray.length] = new material(domEle.value, amount, domEle.text);
                                   })
        var matHtml = makeMaterialHtml(matExistArray);
        FcltMgr.closeDetail(window, { matHtml: matHtml, matExistArray: matExistArray })

      }
    </script>
  </head>
  <body class="modal" id="mod_mldobj"><%/*TODO stijl*/%>
    <form name="u2" action="deelwerk_material.asp?submit=1&matkeystr=<%=matkeystr.join(",")%>" method="post">
<%    BLOCK_START("mld_selobj", L("lcl_mld_objects_select")) %>
      <tr>
        <td style="width:45%" align="center">
          <label class="details"><%=L("lcl_mld_objects_available") + ':'%></label> <img class='details' src='<%=rooturl%>/appl/Pictures/detail.png' onclick='obj_info()'><br>
<%        sql = "SELECT ud.fac_usrdata_key"
              + ", ud.fac_usrdata_omschr"
              + " FROM fac_usrdata ud"
              + " WHERE ud.fac_usrtab_key = " + mld_material_fac_usrtab_key
              + " AND ud.fac_usrdata_verwijder IS NULL"
              + " ORDER BY UPPER(ud.fac_usrdata_omschr)";

          FCLTselector("matPossible", sql, {multi: true, size: 20} );
%>
        </td>
        <td>
          <div onclick="moveRight()" style="cursor:pointer"><%=L("lcl_mld_object_add")%></div>
          <p/>
          <div onclick="removeObj()" style="cursor:pointer"><%=L("lcl_mld_object_remove")%></div>
        </td>
        <td style="width:45%" align="center">
          <label><%=L("lcl_mld_objects_selected") + ':'%></label><br/>
<%        sql = "SELECT ud.fac_usrdata_key"
              + ", ud.fac_usrdata_omschr"
              + " FROM fac_usrdata ud"
              + " WHERE ud.fac_usrtab_key = " + mld_material_fac_usrtab_key
              + " AND ud.fac_usrdata_key IN (" + (matkeystr.join(",")||-1) + ")"
              + " AND ud.fac_usrdata_verwijder IS NULL"
              + " ORDER BY UPPER(ud.fac_usrdata_omschr)";
          FCLTselector("matExist", sql, {multi: true, size:20});
%>
        </td>
      </tr>
<%    BLOCK_END() %>
      <div id="buttons" style="clear:left">
<%
      var buttons = [{title: L("lcl_submit"), icon: "opslaan.png", action: "submit()"},
                     {title: L("lcl_cancel"), icon: "undo.png", action: "FcltMgr.closeDetail(window, { cancel: true })"}];
      CreateButtons(buttons);
%>
    </div>
    </form>
  </body>
</html>
