<%@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", "kenmerk"],
                      js: []
                    }) %>
<%
var dis_key = getQParamInt("dis_key");
var reg_key = getFParamInt("regiokey", -1);

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

var fields = [];
if (this_alg.writeman)
{
    fields.push({ dbs: "alg_district_omschrijving", typ: "varchar",  frm: "dis_oms",  track: L("lcl_district")  });
    if (alg.canWriteRegio(reg_key, this_alg.authparams("WEB_ALGMAN").ALGwritelevel))
        fields.push({ dbs: "alg_regio_key",         typ: "key",   frm: "regiokey", track: L("lcl_region"), foreign: "alg_regio" });
}

var warning = "";
if (dis_key > 0)
{
    var algUpd = buildTrackingUpdate("alg_district", " alg_district_key = " + dis_key, fields);
    var err = Oracle.Execute(algUpd.sql, true);
    if (err.friendlyMsg)
        warning = err.friendlyMsg;
}
else
{
     fields.push({  dbs: "alg_district_key",         typ: "key",      seq: "alg_s_alg_district_key" });
     var regIns = buildInsert("alg_district", fields);
     var dis_key = regIns.sequences["alg_district_key"];

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

  if (!warning)
  {
      if (algUpd && algUpd.trackarray.length)
      { // Tracking alleen op wijzigingen
        shared.trackaction("ALGDUP", dis_key, algUpd.trackarray.join("\n"));
      }
      currentKenmerkenSQL = "SELECT k.alg_kenmerk_key"
                          + "     , ok.alg_onrgoedkenmerk_waarde"
                          + "     , k.alg_kenmerk_toonbaar"
                          + "     , k.fac_functie_key"
                          + "  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 = 'D'"
                          + "   AND alg_onrgoed_key = " + dis_key;
      saveFlexKenmerken(dis_key,
                        { kenmerkTable: "alg_onrgoedkenmerk",
                          kenmerkParentKey : "alg_onrgoed_key",
                          kenmerkWaarde: "alg_onrgoedkenmerk_waarde",
                          kenmerkKey: "alg_kenmerk_key",
                          kenmerkToonbaar: "alg_kenmerk_toonbaar",
                          currentKenmerkenSQL: currentKenmerkenSQL,
                          requestQF: Request.Form,
                          flexPath: "ALG/D",
                          module: "ALG",
                          moduleName: "alg_onrgoed_niveau",
                          moduleVal: "D",
                          isNew: dis_key < 0
                        });
}
  %>
<html>
  <head>
    <%  FCLTHeader.Generate() %>
    <script>
        $(function ()
        {
          FcltMgr.closeDetail(window, { dis_key: <%=dis_key%>,
                                        warning: "<%=safe.jsstring(warning)%>",
                                        keepForm: <%=warning?"true":"false"%>
                                      });
        });
    </script>
  </head>
  <body>
  </body>
</html>
<% ASPPAGE_END(); %>
