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

     SUBMIT-form
*/ %>
<%
var JSON_Result = true;
%>

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../API2/api2.inc" -->
<!-- #include file="../API2/model_regions.inc" -->
<!-- #include file="alg.inc" -->
<%
protectRequest.validateToken();
var regio_key = getQParamInt("regio_key");
var action = getQParam("action", "");

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

var formfields = [];
if (this_alg.writeman)
    formfields.push({ name: "name", frm: "reg_oms" });
var params = { filter: { "id": regio_key }};
var model = new model_regions();
var jsondata = api2.form2JSONdata(model, params, formfields);

switch (action)
{
    case "I":
        var regio_array = model.REST_POST(params, jsondata);
        regio_key = regio_array.key;
        break;
    case "U":
        if (regio_key > 0)
            var regio_array = model.REST_PUT(params, jsondata, params.filter.id);
        break;
    case "D":
        if (regio_key > 0)
            var regio_array = model.REST_DELETE(params, params.filter.id);
        break;
    default :
        var regio_array = {warning: L("lcl_cnt_Del_Fails_Auth")};
}
var warning = regio_array.warning;

var result = { regio_key: regio_key,
               warning: warning,
               keepForm: !!warning,
               success: true };

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 = 'E'"
                        + "   AND alg_onrgoed_key = " + regio_key;
    saveFlexKenmerken(regio_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/E",
                          module: "ALG",
                          moduleName: "alg_onrgoed_niveau",
                          moduleVal: "E",
                          isNew: regio_key < 0
                        });
}

Response.Write(JSON.stringify(result));
%><% ASPPAGE_END(); %>
