<%@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 loc_key = getQParamInt("loc_key");
var dis_key = getFParamInt("districtkey");

var isNew = (loc_key < 0);

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

var fields = [];
if (this_alg.writeuse)
{
    fields.push({ dbs: "mld_dienstniveau_key",      typ: "key",     frm: "dienstniveau", track: L("lcl_mld_dienst_niveau"), foreign: "mld_dienstniveau" });
}

if (this_alg.writeman)
{
    fields.push({ dbs: "alg_locatie_code",          typ: "varchar", frm: "loc_code",     track: L("lcl_estate_locatie_man_code") },
                { dbs: "alg_locatie_omschrijving",  typ: "varchar", frm: "loc_omsch",    track: L("lcl_estate_locatie_man_descr") },
                { dbs: "alg_locatie_adres",         typ: "varchar", frm: "bez_adres",    track: L("lcl_prs_address_bezoek_adres") },
                { dbs: "alg_locatie_postcode",      typ: "varchar", frm: "bez_postc",    track: L("lcl_prs_address_bezoek_postcode") },
                { dbs: "alg_locatie_plaats",        typ: "varchar", frm: "bez_plaats",   track: L("lcl_prs_address_bezoek_plaats") },
                { dbs: "alg_locatie_land",          typ: "varchar", frm: "bez_land",     track: L("lcl_prs_address_bezoek_land") },
                { dbs: "alg_locatie_post_adres",    typ: "varchar", frm: "pst_adres",    track: L("lcl_prs_address_post_adres") },
                { dbs: "alg_locatie_post_postcode", typ: "varchar", frm: "pst_postc",    track: L("lcl_prs_address_post_postcode") },
                { dbs: "alg_locatie_post_plaats",   typ: "varchar", frm: "pst_plaats",   track: L("lcl_prs_address_post_plaats") },
                { dbs: "alg_locatie_post_land",     typ: "varchar", frm: "pst_land",     track: L("lcl_prs_address_post_land") },
                { dbs: "alg_locatie_verantw",       typ: "varchar", frm: "loc_vw",       track: L("lcl_estate_locatie_man_verantw") },
                { dbs: "alg_locatie_verantw_tel",   typ: "varchar", frm: "loc_vwtel",    track: L("lcl_estate_locatie_man_verantw_tel") },
                { dbs: "alg_locatie_x",             typ: "float",   frm: "loc_x",        track: L("lcl_geoxcoord") },
                { dbs: "alg_locatie_y",             typ: "float",   frm: "loc_y",        track: L("lcl_geoycoord") },
                { dbs: "alg_locatie_vervaldatum",   typ: "date",    frm: "vervaldatum",  track: L("lcl_alg_vervaldatum") },
                { dbs: "alg_locatie_email",         typ: "varchar", frm: "loc_mail",     track: L("lcl_noti_email") },
                { dbs: "alg_locatie_image",         typ: "varchar", frm: "loc_image",    track: L("lcl_alg_locatie_image") },
                { dbs: "alg_locatie_volgnr",        typ: "number",  frm: "loc_volgnr",   track: L("lcl_alg_locatie_volgnr") },
                { dbs: "alg_locatie_timezone",      typ: "varchar", frm: "alg_tz",       track: L("lcl_timezone") }
                );

    if (alg.canWriteDistrict(dis_key, this_alg.authparams("WEB_ALGMAN").ALGwritelevel))
        fields.push({ dbs: "alg_district_key", typ: "key", val: dis_key });
}

var warning = "";
if (loc_key > 0)
  {
    var algUpd = buildTrackingUpdate("alg_locatie", " alg_locatie_key = " + loc_key, fields);
    var err = Oracle.Execute(algUpd.sql, true);
    if (err.friendlyMsg)
        warning = err.friendlyMsg;
  }
else
  {
    fields.push({  dbs: "alg_locatie_key",         typ: "key",      seq: "alg_s_alg_locatie_key" });
    var regIns = buildInsert("alg_locatie", fields);
    var loc_key = regIns.sequences["alg_locatie_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 = 'L'"
                          + "   AND alg_onrgoed_key = " + loc_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(loc_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/L",
                          module: "ALG",
                          tracking: true,
                          moduleName: "alg_onrgoed_niveau",
                          moduleVal: "L",
                          isNew: isNew
                        });

    if ( !isNew )
    {
      if (flextrack.length)
      {
        if (algUpd && algUpd.trackarray.length)
          algUpd.trackarray = algUpd.trackarray.concat(flextrack);
        else
          algUpd.trackarray = flextrack;
      }

      if (algUpd && algUpd.trackarray.length)
          shared.trackaction("ALGLUP", loc_key, algUpd.trackarray.join("\n"));
    }
    else
      shared.trackaction("ALGLNE", loc_key);

}
%>

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