<%@ LANGUAGE = JavaScript %>
<% /*
     $Revision$
     $Id$
*/ %>

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

<%
FCLTHeader.Requires({ plugins: ["suggest"],
                      js: ["dep_select.js", "autocomplete.js"]
                    })

var update = getQParamInt("update", 0) == 1;
var resign = getQParamInt("resign", 0) == 1;
var outlet_key = getQParamInt("outlet", -1);

var autfunction = "WEB_INSMAN";
var authparams = user.checkAutorisation(autfunction);

sql = "SELECT ins_ol_kenmerk_key"
    + " FROM ins_ol_kenmerk"
    + " WHERE ins_ol_kenmerk_naam = 'SRTDEEL_OUTLET'";
oRs = Oracle.Execute(sql);
var srtdeel_key_outlet = oRs(0).value;

sql = "SELECT ins_ol_kenmerk_key"
    + " FROM ins_ol_kenmerk"
    + " WHERE ins_ol_kenmerk_naam = 'SRTDEEL_WALLPLATE'";
oRs = Oracle.Execute( sql );
var srtdeel_key_wallplate = oRs(0).value;
%>
<html>
  <head>
<%  FCLTHeader.Generate(); %>

    <script type="text/javascript">
      // vroeger in iface.js
      function enableButton(id,enable,fn)
      {
        bb = document.getElementById(id + "_bg");
        if( bb )
        {
          if( enable ) {
            bb.className = 'nbutton';
            bb.onclick = fn;
          } else {
            bb.className = 'nbuttondisabled';
            bb.onclick = none;
          }
        }
      }

      function PortsUploaded(name, port)
      {
        new_ports = window.frames.hidFrame.opt_ports;
        connect = $("[name='" + name + "con']")[0];
        sel = $("[name='" + name + "']")[0];
        sel.options.length = new_ports.length;
        for (i in new_ports)
        {
          opt_i = sel.options[i];
          np_i = new_ports[i];
          opt_i.text = np_i.text;
          opt_i.value = np_i.value;
          opt_i.parent_key = np_i.parent_key;
          opt_i.ex_value = np_i.ex_value;
          if (opt_i.text==port || (port==-1 && opt_i.value == ""))
          {
            opt_i.selected = true;
            if (np_i.ex_value != -1 && np_i.ex_value != '') connect.value = 1;
          }
        }
        reset_cc();
      }

      function updatePort(name, port)
      {
        if (port == '' || port == null) port = -1;
        if (document.u2 && document.u2[name + "part"])
        {
          var module = parseInt( document.u2[name + "part"].value, 10 ) || -1;
          var outlet = document.u2.outlet.value;
          inc_cc();
          var u = 'load_ports.asp?module=' + module + '&name=' + name + '&port=' + port + '&outlet=' + outlet;
          window.document.getElementById("hidFrame").src = u;
        }
      }

      function updateCon(name)
      {
        new_ports = window.frames.hidFrame.opt_ports;
        connect = $("[name='" + name + "con']")[0];
        sel = $("[name='" + name + "']")[0];
        sel.options.length = new_ports.length;
        for (i in new_ports)
        {
          np_i = new_ports[i];
          sel.ex_value = np_i.ex_value;
          if (sel.value == np_i.value)
          {
            if (np_i.ex_value != -1 && np_i.ex_value != '') connect.value = 1;
            else connect.value = 0;
          }
        }
      }

      function loadKenmerk()
      {
        var loc = -1;
        if(document.forms.u2.sort) sort = document.forms.u2.sort.value;
        if(document.forms.u2.loc) loc = document.forms.u2.loc.value;

        var o_key = sgOutlet.getValue();
        //var w_key = opts(opts.selectedIndex).value2;

        if (isNaN(o_key) || o_key <= 0)
        {
          o_key = -1;
          w_key = -1;
        }
        else
          if (isNaN(w_key)) w_key = -1;

        // load outlet kenmerken
        var s = "load_owkenmerk.asp?loc=" + loc + "&tablename=OutletTable&start_row=3&serienr=1&sort=" + <%=srtdeel_key_outlet%> + ((isNaN(o_key) || o_key <= 0)? "&reado=1" : "") + "&obj_key=" + o_key;
        window.document.getElementById("hidFrameKenmerk1").src = s;

        // load wallplate kenmerken
        var s = "load_owkenmerk.asp?sort=" + <%=srtdeel_key_wallplate%> + "&reado=1&tablename=WallplateTable&start_row=5&serienr=2&obj_key=" + w_key;
        window.document.getElementById("hidFrameKenmerk2").src = s;
      }

      function changeOutlet(o_key)
      {
        if (!isNaN(o_key) && o_key > 0)
        {
          document.forms.u2.update.value = 1;
        }
        else
        {
          document.forms.u2.update.value = 0;
          o_key = -1;
          w_key = -1;
        }
        if (isNaN(w_key)) w_key = -1;

        inc_cc();
        var s = "load_outlet.asp?sort=" + <%=srtdeel_key_outlet%> + "&o_key=" + o_key + "&w_key=" + w_key;
        window.document.getElementById("hidFrameKenmerk3").src = s;
      }

      function changeButtons()
      {
        var o_key = sgOutlet.getValue();
        ///var w_key = opts(opts.selectedIndex).value2;

        if (!isNaN(o_key) && o_key > 0)
        {
          enableButton('bsubmit', true, doSubmit);
          enableButton('bresign', true, doResign);
        }
        else
        {
          enableButton('bsubmit', false, '');
          enableButton('bresign', false, '');
        }
      }

      // Clientside version
      function replace(str, s1, s2)
      {
        var i, r, pos = 0;
        if (str == null) str=''; else str = '' + str;
        while((i = str.indexOf(s1, pos)) >= 0)
        {
          l = str.length; r = '';
          if( i > 0 ) r = r + str.substring(0, i);
          r = r + s2;
          if ( i < l-1) r = r + str.substring(i + s1.length, l);
          str = r; pos = i - s1.length + s2.length + 1;
        }
        return str;
      }

      function CheckPort()
      {
        ///var opts = document.forms.u2.outlet.options;
        var o_key = sgOutlet.getValue();
        var o_txt = document.getElementById("sgOutlet").value;
        var portidvalue = '' + document.forms.u2.portid.value;

        var portpart = document.forms.u2[portidvalue + "part"].options;
        var port = portpart(portpart.selectedIndex).text;

        var portcon = document.forms.u2[portidvalue + "con"].value;
        if (portcon == 1)
        {
          alert(L("lcl_out_port_in_use"));
          return false;
        }
        if (port != '' && port.substring(0, 4) != replace(o_txt, '-', '').substring(0, 4))
        {
          if (confirm(L("lcl_out_port") + " " + port + L("lcl_out_same_building") + o_txt + ". " + L("lcl_out_proceed")))
            return true;
          else
            return false;
        }
        // else Controle OK of niets ingevuld
        return true;
      }

      function doSubmit()
      {
        if (CheckPort())
          document.forms.u2.submit();
      }

      function doResign()
      {
        var o_key = sgOutlet.getValue();
        var o_txt = document.getElementById("sgOutlet").value;

        if (!isNaN(o_key) && o_key > 0)
        {
          if (confirm("Opzeggen Outlet " + o_txt))
          {
            document.forms.u2.update.value = 0;
            document.forms.u2.resign.value = 1;
            document.forms.u2.submit();
          }
        }
      }

      var sgOutlet;
      function BodyLoad()
      {
          document.getElementById("sgOutlet").focus();
          changeOutlet();
          enableButton('bsubmit', false, '');
          enableButton('bresign', false, '');
          enableButton('bcancel', false, '');
          sgOutlet = new Suggest({objectName: "sgOutlet",
                                  keyField: document.getElementById("outlet"),
                                  queryField: document.getElementById("sgOutlet"),
                                  queryUrl: "../shared/Suggest/SuggestOutlet.asp?alg_level_read=<%=authparams.ALGreadlevel%>",
                                  initKey: -1}
                                );
      }
    </script>
  </head>

  <body id="editbody" onLoad="BodyLoad()">
<%  buttons = [{title: L("lcl_submit"),      icon:"opslaan.png", action: "doSubmit()" },
               {title: L("lcl_out_resign"),  icon:"reject.png", action: "doResign()"}];
    IFRAMER_HEADER(L("lcl_bo_out_title"), buttons);
%>
    <form name="u2" action="outlet.asp" method="get">
      <div id="edit">
<%      if ((update || resign) && !isNaN(outlet_key) && outlet_key > 0)
        {
          var knum = 1;
          while (true)
          {
            var ktn = Request.QueryString("k" + knum + "key1").Count;
            var kvn = Request.QueryString("k" + knum + "val1").Count;

            if (ktn < 1 && kvn < 1) break;

            var kt = getQParamInt("k" + knum + "key1");
            var kvc = 0;
            if (resign)
              var kv = '';
            else
            {
              var kv = "" + getQParam("k" + knum + "val1");
              if (Request.QueryString("k" + knum + "val1con").Count)
              {
                kvc = "" + getQParam("k" + knum + "val1con");
                if (kvc == 1) __Log("Module en poort worden niet opgeslagen, zijn al in gebruik"); // extra check
              }
            }
            kv = parseDocumentPath(kv);

            knum++;
            if (!isNaN(kt) && kt > 0 && !(kvn < 1) && kvc != 1 /*&& kv != ''*/)
            {
              sql = "begin akz.Ins_Update_Kenmerkdeel_WEB(null, " + outlet_key + ", " + kt + ", "
                  + (kv == ''? 'null' : safe.quoted_sql(kv)) + ", '" + (this.S("ins_history")? this.S("ins_history") : 1) + "', '0'); end;";
              oRs = Oracle.Execute(sql);
            }
          }
        }
%>
        <table cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td></td>
          <td colspan=2><%=L("lcl_location")%>:&nbsp;<input style="color:red" type="text" value="" name="loccode" size="20" maxlength="30" readonly style="width:200px"></td>
        </tr>
        <tr><td>&nbsp;</td></tr>
        <tr>
          <td valign="top">
            <table cellpadding="0" cellspacing="0" border="0">
            <tr>
              <td colspan=2>
                <table cellpadding="0" cellspacing="0" border=0 id=OutletTable>
                <tr>
<%                sql = " SELECT id.ins_deel_key"
                      + ", id.ins_deel_omschrijving"
                      + ", id.ins_deel_parent_key"
                      + " FROM ins_deel id"
                      + " WHERE id.ins_srtdeel_key = " + srtdeel_key_outlet
                      + " AND id.ins_deel_parent_key IS NOT NULL"
                      + " AND id.ins_deel_verwijder IS NULL"
                      + ((authparams.ALGreadlevel >= 0)
                        ? " AND id.ins_alg_ruimte_key IN (SELECT alg_ruimte_key FROM fac_v_my_rooms"
                   	      + " WHERE prs_perslid_key = " + user_key + " AND niveau=" + authparams.ALGreadlevel + ")"
                   	    : ""); %>
                  <td valign=top align="right" style="padding-left:5px;"><b><%=L("lcl_out_outletnr")+':&nbsp;'%></b></td>
                  <td>
                    <input type="text" value="" name="sgOutlet" id="sgOutlet" size="40" maxlength="30"
                           class="suggest"
                           sgonChange="changeOutlet" onFocus="this.select()">
                    <input type="hidden" name="outlet" id="outlet">
                  </td>
                </tr>
                <tr>
                  <td></td>
                  <td name=warning id=warning>&nbsp;</td>
                </tr>
                </table>
              </td>
            </tr>
            </table>
          </td>

          <td style="width:10%">&nbsp;</td>

          <td valign="top">
            <table cellpadding="0" cellspacing="0" border="0">
            <tr>
              <td colspan=2>
                <table cellpadding="0" cellspacing="0" border=0 id=WallplateTable>
                <tr>
                  <td align="right"><nobr><b><%=L("lcl_out_wallplate")%>:&nbsp;</b></nobr></td>
                  <td><input type="text" value="" name="wallplate" size="20" maxlength="30" readonly style="width:200px"></td>
                </tr>
                <tr>
                  <td align="right"><nobr><b><%=L("lcl_room")%>:&nbsp;</b></nobr></td>
                  <td><input type="text" value="" name="room" size="20" maxlength="30" readonly style="width:200px"></td>
                </tr>
                <tr>
                  <td align="right"><nobr><b><%=L("lcl_out_renter_room")%>:&nbsp;</b></nobr></td>
                  <td><input type="text" value="" name="renter" size="20" maxlength="30" readonly style="width:200px"></td>
                </tr>
                <tr>
                  <td>&nbsp;</td>
                  <td>&nbsp;</td>
                </tr>

                <tr>
                  <td>
                    <input type=submit value="" style="width:0" id=SUBMIT1 name=SUBMIT1>
                    <input type='hidden' name='update' value='0'>
                    <input type='hidden' name='resign' value='0'>
                    <input type='hidden' name='portid' value=''>
                  </td>
                </tr>
                </table>
              </td>
            </tr>
            <table>
          </td>
        </tr>
        </table>
      </div>
<% IFACE.FORM_END(); %>
    </form>

    <iframe src="../Shared/empty.asp" frameborder="0" id="hidFrame" style="display:none"></iframe>
    <iframe src="../Shared/empty.asp" frameborder="0" id="hidFrameKenmerk" style="display:none"></iframe>
    <iframe src="../Shared/empty.asp" frameborder="0" id="hidFrameKenmerk1" style="display:none"></iframe>
    <iframe src="../Shared/empty.asp" frameborder="10" id="hidFrameKenmerk2" style="display:none"></iframe>
    <iframe src="../Shared/empty.asp" frameborder="10" id="hidFrameKenmerk3" style="display:none"></iframe>
  </body>
</html>
