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

    File:               alg_edit_terreinsector_multi_save.asp
    Description:        save script van alg_edit_terreinsector_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 ter_key_arr = getQParamIntArray("ter_keys");
var warning = "";
var result = {};
var nr_edited = 0;
var fields = [];

if (Request.Form("ter_srt").Count == 1 && getFParam("check_ter_srt", "off") == "on")
    fields.push({ dbs: "alg_srtterreinsector_key", typ: "key", frm: "ter_srt", track: L("lcl_estate_terreinsector_man_sort"), foreign: "ALG_SRTTERREINSECTOR" });
if (Request.Form("vervaldatum").Count == 1 && getFParam("check_vervaldatum", "off") == "on")
    fields.push({ dbs: "alg_terreinsector_vervaldatum", typ: "date", frm: "vervaldatum", track: L("lcl_alg_vervaldatum") });
if (Request.Form("prs_kstn").Count == 1 && getFParam("check_prs_kstn", "off") == "on")
    fields.push({ dbs: "prs_kostenplaats_key", typ: "key", frm: "prs_kstn", 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("prs_afd").Count == 1 && getFParam("check_prs_afd", "off") == "on")
    fields.push({ dbs: "prs_afdeling_key",  typ: "key",  frm: "prs_afd", track: L("lcl_dep_name_level2"), foreign: "PRS_AFDELING"});
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_terreinsector_toon_telef",   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_terreinsector_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_terreinsector_toon_kentk",   typ: "number", val: toon_k, track: L("lcl_estate_gebouw_toon_kenteken")});
}


for (var i = 0; i < ter_key_arr.length; i++)
{
    var alg_key = ter_key_arr[i];
    var this_alg = alg.func_enabled(alg_key, "T");
    if (this_alg.writeman || this_alg.writeuse)
    {
        var algUpd = buildTrackingUpdate("alg_terreinsector", " alg_terreinsector_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 = 'T'"
                            + "   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/T",
                                            multiMode: true,
                                            tracking: true,
                                            module: "ALG",
                                            moduleName: "alg_onrgoed_niveau",
                                            moduleVal: "T"
                                          });

        if (flextrack && flextrack.length)
        {
            var update_string = L("lcl_alg_is_algrup") + "\n" + flextrack.join("\n");
            shared.trackaction("ALGTUP", alg_key, update_string);
            nr_edited++;
        }
    }
}

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

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

ASPPAGE_END();
%>
