<%@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 loc_key = getFParamInt("locatiekey", -1);

var isNew = (ter_key < 0);

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_terreinsector_omschrijving", typ: "varchar", frm: "ter_oms",       track: L("lcl_estate_terreinsector_man_descr") },
              { dbs: "alg_terreinsector_code",         typ: "varchar", frm: "ter_code",      track: L("lcl_estate_terreinsector_man_code") },
              { dbs: "alg_terreinsector_naam",         typ: "varchar", frm: "ter_naam",      track: L("lcl_estate_terreinsector_man_name") },
              { dbs: "alg_srtterreinsector_key",       typ: "key",     frm: "ter_srkey",     track: L("lcl_estate_terreinsector_man_sort"), foreign: "ALG_SRTTERREINSECTOR" },
              { dbs: "alg_terreinsector_oppervlak",    typ: "float",   frm: "ter_opp",       track: L("lcl_estate_terreinsector_man_oppervlak") },
              { dbs: "alg_terreinsector_omtrek",       typ: "float",   frm: "ter_omt",       track: L("lcl_estate_terreinsector_man_omtrek") },
              { dbs: "alg_terreinsector_ordernr",      typ: "varchar", frm: "ter_ordnr",     track: L("lcl_estate_terreinsector_ordernr") },
              { dbs: "alg_terreinsector_toon_telef",   typ: "number",  frm: "toon_telefoon", track: L("lcl_estate_gebouw_toon_telefoon") },
              { dbs: "alg_terreinsector_toon_email",   typ: "number",  frm: "toon_email",    track: L("lcl_estate_gebouw_toon_email") },
              { dbs: "alg_terreinsector_toon_kentk",   typ: "number",  frm: "toon_kenteken", track: L("lcl_estate_gebouw_toon_kenteken") },
              { dbs: "prs_kostenplaats_key",           typ: "key",     frm: "prs_kstn",      track: L("lcl_prs_dept_kosten"), foreign: "PRS_KOSTENPLAATS" },
              { dbs: "prs_afdeling_key",               typ: "key",     frm: "prs_afd",       track: L("lcl_dep_name_level2"), foreign: "PRS_AFDELING" },
              { dbs: "prs_perslid_key_verantw",        typ: "key",     frm: "verantw",       track: L("lcl_alg_verantw"),     foreign: "PRS_PERSLID" },
              { dbs: "prs_perslid_key_verantw2",       typ: "key",     frm: "verantw2",      track: L("lcl_alg_verantw2"),    foreign: "PRS_PERSLID" }];

if (this_alg.writeman)
{
    fields.push({ dbs: "alg_terreinsector_image",        typ: "varchar", frm: "ter_image",   track: L("lcl_alg_terreinsector_image") },
                { dbs: "alg_terreinsector_vervaldatum",  typ: "date",    frm: "vervaldatum", track: L("lcl_alg_vervaldatum") });

    if (alg.canWriteLocatie(loc_key, this_alg.authparams("WEB_TERMAN").ALGwritelevel))
        fields.push({ dbs: "alg_locatie_key",            typ: "key",     val: loc_key,       track: true /* afzonderlijke tracking, wel oldjsval */ });
}

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

    var err = Oracle.Execute(algUpd.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 != algUpd.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;
}

var flextrack = [];
if (!warning)
{
      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 = 'T'"
                          + "   AND alg_onrgoed_key = " + ter_key;

    allKenmerkenSQL     = "SELECT " + lcl.xsql('k.alg_kenmerk_omschrijving', 'k.alg_kenmerk_key') + " kenmerk_omschrijving"
                        + "     , k.alg_kenmerk_kenmerktype kenmerk_kenmerktype"
                        + "     , k.alg_kenmerk_systeem     kenmerk_systeem"
                        + "     , k.alg_kenmerk_toonbaar    kenmerk_readonly"
                        + "     , k.alg_kenmerk_uniek       kenmerk_uniek"
                        + "     , fac_kenmerkdomein_key"
                        + "  FROM alg_kenmerk k"
                        + " WHERE k.alg_kenmerk_key = "; /* wordt in saveFlexkenmerken uitgebreid */

      flextrack = saveFlexKenmerken(ter_key,
                        { kenmerkTable: "alg_onrgoedkenmerk",
                          kenmerkParentKey : "alg_onrgoed_key",
                          kenmerkWaarde: "alg_onrgoedkenmerk_waarde",
                          kenmerkKey: "alg_kenmerk_key",
                          kenmerkToonbaar: "alg_kenmerk_toonbaar",
                          currentKenmerkenSQL: currentKenmerkenSQL,
                          allKenmerkenSQL: allKenmerkenSQL,
                          requestQF: Request.Form,
                          flexPath: "ALG/T",
                          module: "ALG",
                          tracking: true,
                          moduleName: "alg_onrgoed_niveau",
                          moduleVal: "T",
                          isNew: isNew
                        });

    if (!isNew)
    {
      if (flextrack && flextrack.length)
      {
        if (algUpd && algUpd.trackarray.length)
          algUpd.trackarray = algUpd.trackarray.concat(flextrack);
        else
          algUpd.trackarray = flextrack;
      }
      if (algUpd && algUpd.trackarray.length)
          shared.trackaction("ALGTUP", ter_key, algUpd.trackarray.join("\n"));
    }
    else
      shared.trackaction("ALGTNE", ter_key);


}
%>

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