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

     SUBMIT-form
*/ %>

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

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

var onrgoedlvl = "T";
var this_alg = alg.func_enabled(ter_key, onrgoedlvl);
user.auth_required_or_abort(this_alg.writeman || (ter_key>0&&this_alg.writeuse));

  var fields =  [ { dbs: "alg_locatie_key", typ: "key",  frm: "locatiekey", track: true /* afzonderlijke tracking, wel oldjsval */ },
                  { dbs: "ALG_TERREINSECTOR_OMSCHRIJVING", typ: "varchar",  frm: "ter_oms" },
                  { dbs: "ALG_TERREINSECTOR_CODE", typ: "varchar",  frm: "ter_code" },
                  { dbs: "ALG_TERREINSECTOR_NAAM", typ: "varchar",  frm: "ter_naam" },
                  { dbs: "ALG_SRTTERREINSECTOR_KEY", typ: "key",  frm: "ter_srkey" },
                  { dbs: "ALG_TERREINSECTOR_OPPERVLAK", typ: "float",  frm: "ter_opp" },
                  { dbs: "ALG_TERREINSECTOR_OMTREK", typ: "float",  frm: "ter_omt" },
                  { dbs: "ALG_TERREINSECTOR_ORDERNR", typ: "varchar",  frm: "ter_ordnr" },
                  { dbs: "PRS_KOSTENPLAATS_KEY", typ: "key",  frm: "prs_kstn" },
                  { dbs: "PRS_AFDELING_KEY", typ: "key", frm: "prs_afd" }];

var warning = "";
if (ter_key > 0)
  {
    var terUpd = buildTrackingUpdate("alg_terreinsector", "alg_terreinsector_key = " + ter_key, fields);

    var err = Oracle.Execute(terUpd.sql, true);
    if (err.friendlyMsg)
    {
        warning = err.friendlyMsg;
    }
    else
    {
        // We staan locatiewijziging toe. Wel zorgen dat redundante velden blijven kloppen
        var loc_key = getFParamInt("locatiekey");
        if (loc_key != terUpd.oldjsvals["alg_locatie_key"])
        {
            sql = "UPDATE mld_melding"
                + "   SET mld_alg_locatie_key = " + loc_key
                + " WHERE mld_alg_onroerendgoed_keys = " + ter_key;
            err = Oracle.Execute(sql, true);
            if (!err.friendlyMsg)
            {
                sql = "UPDATE ins_deel"
                    + "   SET ins_alg_locatie_key = " + loc_key
                    + " WHERE ins_alg_ruimte_key = " + ter_key
                    + "   AND ins_alg_ruimte_type = 'T'";
                err = Oracle.Execute(sql, true);
            }
            if (err.friendlyMsg)
            {
                warning = err.friendlyMsg;
            }
        }
    }
  }
else
  {
    fields.push({  dbs: "alg_terreinsector_key",         typ: "key",      seq: "alg_s_alg_onroerendgoed_keys" });
  var regIns = buildInsert("alg_terreinsector", fields);
  var ter_key = regIns.sequences["alg_terreinsector_key"];

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

  if (!warning)
  {
      currentKenmerkenSQL = " SELECT k.ALG_kenmerk_key " +
                            "      , ok.alg_onrgoedkenmerk_waarde" +
                            "   FROM ALG_ONRGOEDKENMERK ok, ALG_KENMERK k " +
                            "  WHERE  k.ALG_KENMERK_KEY = ok.ALG_KENMERK_KEY " +
                            "    AND ALG_ONRGOEDKENMERK_VERWIJDER IS NULL " +
                            "    AND ALG_ONRGOED_NIVEAU = 'T' " +
                            "    AND ALG_ONRGOED_KEY = " + ter_key;
      saveFlexKenmerken(ter_key, "ALG_ONRGOEDKENMERK", "ALG_ONRGOED_KEY",
                                "ALG_ONRGOEDKENMERK_WAARDE", "ALG_KENMERK_KEY",
                                currentKenmerkenSQL, false,  //QueryForm, rest QueryString
                                "ALG_ONRGOED_NIVEAU", "T",
                                (ter_key<0),"ALG/T")
}
  %>

<html>
  <head>
    <%  FCLTHeader.Generate() %>
    <script>
        $(document).ready(function () {
            FcltMgr.closeDetail(window, { ter_key: <%=ter_key%>,
                                          warning: "<%=safe.jsstring(warning)%>",
                                          keepForm: <%=warning?"true":"false"%>
                                         }); } );
    </script>
  </head>
  <body>
  </body>
</html>
