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

    File:               alg_edit_gebouw_multi_save.asp
    Description:        save script van alg_edit_gebouw_multi.asp
    Globalen:
    Context:
    Note:
*/
var JSON_Result = true;
%>

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

<%
protectRequest.validateToken();
var bld_key_arr = getQParamIntArray("bld_keys");
var warning = "";
var result = {};
var nr_edited = 0;
var fields = [];

if (Request.Form("prs_kstpl").Count == 1 && getFParam("check_prs_kstpl", "off") == "on")
    fields.push({ dbs: "prs_kostenplaats_key",  typ: "key",   frm: "prs_kstpl",  track: L("lcl_prs_dept_kosten"),  foreign: "prs_kostenplaats" });
if (Request.Form("verantw").Count == 1 && getFParam("check_verantw", "off") == "on")
    fields.push({ dbs: "prs_perslid_key_verantw",  typ: "key",   frm: "verantw",  track: L("lcl_alg_verantw"),  foreign: "prs_perslid" });
if (Request.Form("verantw2").Count == 1 && getFParam("check_verantw2", "off") == "on")
    fields.push({ dbs: "prs_perslid_key_verantw2",  typ: "key",   frm: "verantw2", track: L("lcl_alg_verantw2"), foreign: "prs_perslid" });
if (Request.Form("groep").Count == 1 && getFParam("check_groep", "off") == "on")
    fields.push({ dbs: "alg_gebouw_groep", typ: "varchar", frm: "groep", track: L("lcl_estate_gebouw_groepkenmerk") });
if (Request.Form("vervaldatum").Count == 1 && getFParam("check_vervaldatum", "off") == "on")
    fields.push({ dbs: "alg_gebouw_vervaldatum",  typ: "date",   frm: "vervaldatum", track: L("lcl_alg_vervaldatum") });
if (Request.Form("toon_telefoon").Count == 1 && getFParam("check_toon_telefoon", "off") == "on")
{   var toon_t = (getFParam("toon_telefoon") ? getFParam("toon_telefoon") : 0);
    fields.push({ dbs: "alg_gebouw_toon_telefoon", typ: "number", val: toon_t, track: L("lcl_estate_gebouw_toon_telefoon") });
}
if (Request.Form("toon_email").Count == 1 && getFParam("check_toon_email", "off") == "on")
{   var toon_e = (getFParam("toon_email") ? getFParam("toon_email") : 0);
    fields.push({ dbs: "alg_gebouw_toon_email", typ: "number", val: toon_e, track: L("lcl_estate_gebouw_toon_email") });
}
if (Request.Form("toon_kenteken").Count == 1 && getFParam("check_toon_kenteken", "off") == "on")
{   var toon_k = (getFParam("toon_kenteken") ? getFParam("toon_kenteken") : 0);
    fields.push({ dbs: "alg_gebouw_toon_kenteken", typ: "number", val: toon_k, track: L("lcl_estate_gebouw_toon_kenteken") });
}
if (Request.Form("dienstniveau").Count == 1 && getFParam("check_dienstniveau", "off") == "on")
    fields.push({ dbs: "mld_dienstniveau_key", typ: "key", frm: "dienstniveau", track: L("lcl_mld_dienst_niveau"), foreign: "mld_dienstniveau" });

if (Request.Form("cal_id").Count == 1 && getFParam("check_cal_id", "off") == "on")
    fields.push({ dbs: "mld_vrije_dagen_id", typ: "varchar", frm: "cal_id", track: L("lcl_calendar") });
if (Request.Form("bld_beginuur").Count == 1 && getFParam("check_bld_beginuur", "off") == "on")
    fields.push({ dbs: "alg_gebouw_beginuur",   typ: "float", frm: "bld_beginuur", track: L("lcl_estate_gebouw_beginuur") });
if (Request.Form("bld_einduur").Count == 1 && getFParam("check_bld_einduur", "off") == "on")
    fields.push({ dbs: "alg_gebouw_einduur",    typ: "float", frm: "bld_einduur", track: L("lcl_estate_gebouw_einduur") });
if (getFParam("check_bld_werkdagen", "off") == "on")
    fields.push({ dbs: "alg_gebouw_werkdagen",  typ: "check0", val: Request.Form("bld_werkdagen").Count });
if (getFParam("check_bld_bez", "off") == "on")
    fields.push({ dbs: "alg_gebouw_bez",  typ: "check", val: Request.Form("bld_bez").Count });
if (getFParam("check_bld_mld", "off") == "on")
    fields.push({ dbs: "alg_gebouw_mld",  typ: "check", val: Request.Form("bld_mld").Count });
if (getFParam("check_group", "off") == "on")
    fields.push({ dbs: "bez_actie_groep_key", typ: "key", frm: "group", track: L("bez_actie_groep"), foreign: "bez_actie_groep" });

for (var i = 0; i < bld_key_arr.length; i++)
{
    var alg_key = bld_key_arr[i];
    var this_alg = alg.func_enabled(alg_key, "G");
    if (this_alg.writeman || this_alg.writeuse)
    {
        var algUpd = buildTrackingUpdate("alg_gebouw", " alg_gebouw_key = " + alg_key, fields);
        // Submit the changed data
        var err = Oracle.Execute(algUpd.sql, true);
        if (err.friendlyMsg)
            warning = err.friendlyMsg;

        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 ok.alg_kenmerk_key = k.alg_kenmerk_key"
                            + "   AND ok.alg_onrgoedkenmerk_verwijder IS NULL"
                            + "   AND ok.alg_onrgoed_niveau = 'G'"
                            + "   AND ok.alg_onrgoed_key = " + alg_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 */

        var flextrack = saveFlexKenmerken(alg_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/G",
                            multiMode: true,
                            tracking: true,
                            module: "ALG",
                            moduleName: "alg_onrgoed_niveau",
                            moduleVal: "G",
                            isNew: false
                          });

        if ((algUpd.trackarray && algUpd.trackarray.length) || (flextrack && flextrack.length))
        {
            var update_string = L("lcl_alg_is_alglup") + "\n";
            if (algUpd.trackarray && algUpd.trackarray.length)
                update_string += algUpd.trackarray.join("\n");
            if (flextrack && flextrack.length)
                update_string += flextrack.join("\n");
            shared.trackaction("ALGGUP", alg_key, update_string);
        }
    }
}

result = { success : true, refresh: true };

Response.Write(JSON.stringify(result));

ASPPAGE_END();
%>
