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

    File:               prs/prs_perslid_substitutes.asp
    Description:        Interface to add substitutes of a person
    Parameters:
        pkey            Optional PRS_PERSLID_KEY
        mode            'modify','save'
    Context:            From perslid.asp or from self (form-submit)
    Note:               If Substitute (Vervolg) button is visible, you have the authority to substitute
                        So no authority check in this file

*/ %>

<!--#include file="../Shared/common.inc"-->
<!--#include file="../Shared/iface.inc"-->
<!--#include file="../Shared/persoonselector.inc"-->

<%
FCLTHeader.Requires({ plugins:["jQuery"],
                      js: ["../prs/sel_substitute_tab.js"]
                    })
%>

<html>
  <head>
<%  FCLTHeader.Generate(); %>
    <script type="text/javascript">
      function prs_submit () {
          //u2.submit();  // TODO
          SaveSubstitutes();
      }

      function prs_cancel()
      {
          FcltMgr.closeDetail(window, { cancel: true } );
      }
    </script>
  </head>

<%
  // Form: on submit prssubstitutes.asp.  QueryString: on initial call prssubstitutes.asp
  var mode = getFParam("mode", getQParam("mode", "modify"));

  // Form: on submit prssubstitutes.asp.  QueryString: on initial call prssubstitutes.asp
  var pkey = getFParamInt("pkey", getQParamInt("pkey"));
%>

  <body class="modal" id="mod_substitutes">
<%  if (mode == 'save')
    {
      var substitutes_key_arr = getFParamIntArray("substitutes_string", "");

      // Delete de oude vervangers
      sql = "DELETE FROM prs_collega"
          + " WHERE prs_perslid_key = " + pkey;
      Oracle.Execute(sql);

      // Sla de nieuwe vervangers op
      for (i = 0; i < substitutes_key_arr.length; i++)
      {
        sql = "INSERT INTO prs_collega ("
            + "prs_perslid_key"
            + ", prs_perslid_key_alt"
            + ") VALUES ("
            + pkey
            + ", " + substitutes_key_arr[i]
            + ")";
        Oracle.Execute(sql);
      }
%>
      <script type="text/javascript">
        FcltMgr.closeDetail(window, { cancel: true } );
      </script>
<%
    }
    else // mode == 'modify'
    {  %>
      <form name=u2 method=post target='hidFrame' onsubmit="return false">
        <input type="hidden" name="mode" id="mode" value="save">
        <input type="hidden" name="prs_key" id="prs_key" value="<%=pkey%>">
        <input type="hidden" name="substitutes_string" id="substitutes_string" value="">
        <table width="100%">
        <tr>
          <td valign=top>
<%
            BLOCK_START("", L("lcl_prs_vervangers"));

            sql = "SELECT " + S("prs_dep_string") + " prs_afdeling_naam"
                   + ", " + S("prs_pers_string") + " prs_perslid_naam"
                   + " FROM prs_v_afdeling d"
                   + ", prs_perslid p"
                   + " WHERE prs_afdeling_verwijder IS NULL"
                   + " AND p.prs_afdeling_key = d.prs_afdeling_key"
                   + " AND p.prs_perslid_key = " + pkey;
            oRs = Oracle.Execute(sql);
            if (!oRs.eof)
            {
              dept_name = oRs(0).value;
              prs_name = oRs(1).value;
            }

            FCLTpersoonselector(
                       "person",
                       "sgPerson",
                     {
                       label: L("lcl_prs_substitute"),
                       extracode: "A",
                       extraParamField: "dept",
                       fieldNameKey: "prs_key",
                       filtercode: "SUBS",
                       urlAdd: [{urlParam: "prs_key",  field: "prs_key",     init: pkey}]
                     });
%>
          <td>
            <input type=Image
                   src="..\Pictures\plus.png"
                   title="<%=L("lcl_prs_subst_add_btn")%>"
                   OnClick="AddItem();"
                   class="button"
                   id=Button1
                   name=Button1>
          </td>
        </tr>

        <tr>
          <td colspan="2">
            <table width="100%">
            <tr>
              <td>
                <!-- SELECTED ITEMS -->
                <label><%=L("lcl_prs_substitutes")%>:</label>
                <table id="sel_items" cellspacing=0 cellpadding=0 width="100%">
                <tr>
                  <th width=15></th>
                  <th><%=L("lcl_prs_name")%> </th>
                  <th><%=L("lcl_prs_organisatie")%> </th>
                </tr>
                </table>
              </td>
            </tr>
            </table>
          </td>
        </tr>

        <br><!--hier komen de geselecteerde vervangers-->
<%
        BLOCK_END();

        var buttons =  [ {title: L("lcl_submit"), action: "prs_submit()" },
                         {title: L("lcl_cancel"), action: "prs_cancel()" }
                       ];
        CreateButtons(buttons);
IFACE.FORM_END();
%>
      </form>
      <iframe src="../Shared/empty.asp" frameborder="10" name="hidFrame" id="hidFrame" style="display:none"></iframe>

      <script type="text/javascript">
<%      // Bestaande vervangers weergeven
        sql = "SELECT pc.prs_perslid_key_alt"
            + ", " + S("prs_pers_string") + " prs_perslid_naam"
            + ", " + S("prs_dep_string") + " prs_afdeling_naam"
            + " FROM prs_collega pc"
            + ", prs_perslid p"
            + ", prs_v_afdeling d"
            + " WHERE pc.prs_perslid_key_alt = p.prs_perslid_key"
            + " AND p.prs_afdeling_key = d.prs_afdeling_key"
            + " AND pc.prs_perslid_key = " + pkey
            + " ORDER BY 2";
        oRs = Oracle.Execute(sql);
        while (!oRs.eof)
        { %>
          AddItem(<%=oRs(0).value%>, "<%=safe.jsstring(oRs(1).value)%>", "<%=safe.jsstring(oRs(2).value)%>");
<%       oRs.MoveNext();
        } %>
      </script>
<%  } %>
  </body>
</html>
