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

     SUBMIT-form
*/ %>

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<!-- #include file="../Shared/WPFunctions.inc" -->
<!-- #include file="../Shared/save2db.inc" -->
<!-- #include file="alg.inc" -->

<% FCLTHeader.Requires({ plugins:["jQuery"] }); %>
<%
var wp_key = getQParamInt( "wp_key" );

var virtueel = (getFParamInt("has_wp_virt", 0) == 1) && (Request.Form("wp_virt").count == 1); // virtueel

// Bij impliciet altijd flex
var wp_type = (S("prs_werkplek_implicit") == 1)?1:getFParamInt("wp_type");

var authparams = alg.checkAutorisation();

var fields =  [ { dbs: "prs_werkplek_volgnr",       typ: "number",  frm: "wp_volgnr",       track: L("lcl_estate_wp_seq") },
                { dbs: "prs_werkplek_omschrijving", typ: "varchar", frm: "wp_oms",          track: L("lcl_workplace") },
                { dbs: "prs_werkplek_vastopp",      typ: "number",  frm: "wp_vstopp_v",     track: L("lcl_estate_wp_fixed_v") },
                { dbs: "prs_werkplek_opp",          typ: "number",  frm: "wp_opp",          track: L("lcl_estate_wp_area")},
                { dbs: "prs_werkplek_virtueel",     typ: "number",  val: (virtueel? 1 : 0), track: L("lcl_estate_wp_virt") },
                { dbs: "prs_werkplek_type",         typ: "number",  val: wp_type,           track: L("lcl_wptype") },
                { dbs: "prs_alg_ruimte_key",        typ: "key",     frm: "ruimtekey"}
                /*
                { dbs: "PRS_WERKPLEK_MODULE",     typ: "varchar", frm: "" },
                { dbs: "PRS_WERKPLEK_GETEKEND", typ: "date",  frm: "" },
                { dbs: "PRS_WERKPLEK_DWGX", typ: "number",  frm: "" },
                { dbs: "PRS_WERKPLEK_DWGY", typ: "number",  frm: "" },
                { dbs: "PRS_WERKPLEK_DWGZ", typ: "number",  frm: "" },
                { dbs: "PRS_WERKPLEK_DWGROTATIE", typ: "number",  frm: "" },
                { dbs: "PRS_WERKPLEK_DWGSCHAAL", typ: "number",  frm: "" }
                */
              ];

var warning = "";
if (wp_key > 0)
  {
    var algUpd = buildTrackingUpdate("prs_werkplek", " prs_werkplek_key = " + wp_key, fields);

    var err = Oracle.Execute(algUpd.sql, true);
    if (err.friendlyMsg)
      warning = err.friendlyMsg;

    // Is de ruimte aangepast?
    var room_key_new = getFParamInt("ruimtekey");
    var sql = "SELECT prs_alg_ruimte_key"
            + "  FROM prs_werkplek"
            + " WHERE prs_werkplek_key = " + wp_key;
    var oRs = Oracle.Execute(sql);
    var room_key_old = oRs("prs_alg_ruimte_key").Value;
    oRs.Close();

    if (room_key_old != room_key_new)
    { // Werkplek verplaatsen
      wp_key = WPMove_PRS_WERKPLEK(wp_key, room_key_new);
    }

    if (algUpd && algUpd.trackarray.length)
    {   // Tracking alleen op wijzigingen
        shared.trackaction("ALGWUP", wp_key, algUpd.trackarray.join("\n"));
    }
  }
else
  {
    fields.push({  dbs: "prs_werkplek_key",         typ: "key",      seq: "prs_s_prs_werkplek_key" });
    var regIns = buildInsert("prs_werkplek", fields);
    var wp_key = regIns.sequences["prs_werkplek_key"];

    sql = regIns.sql;
    var err = Oracle.Execute(sql, true);
    if (err.friendlyMsg)
        warning = err.friendlyMsg;
  }
  %>
// TODO: checkboxen ook saven? Jos?
<html>
  <head>
    <%  FCLTHeader.Generate() %>
    <script>
        $(function ()
        {
          FcltMgr.closeDetail(window, { wp_key: <%=wp_key%>,
                                        warning: "<%=safe.jsstring(warning)%>",
                                        keepForm: <%=warning? "true" : "false"%>
                                      });
        });
    </script>
  </head>
  <body>
  </body>
</html>
<% ASPPAGE_END(); %>
