<%@ LANGUAGE = JavaScript %>
<% /*
     $Revision$
     $Id$

     File:               fac_edit_widget_page_save.asp
     Description:        Meldt specifieke opdrachten financieel klaar
     Parameters:
         opdr_key        Array van opdracht keys van opdrachten die moeten worden afgerond (kostenklaar maken)
     Context:
     Note:

*/ %>

<%
var JSON_Result = true;
%>
<!-- #include file="../Shared/common.inc" -->
<!-- #include file="fac_widget.inc" -->
<%

function BytesToStr(bytes)
{
    var stream = Server.CreateObject("ADODB.STREAM");
    stream.type = 1;
    stream.open;
    stream.write(bytes);
    stream.position = 0;
    stream.type = 2; // Text
    stream.charset = "utf-8";
    stream.position = 0;

    var ReadBytes = 131072;
    var sOut = "";
    while (!stream.EOS)
    {
        sOut = sOut + stream.ReadText(ReadBytes);
    }
    stream.close;

    return sOut;
}

function RequestJSON()
{
    var raw_request;
    if (Request.TotalBytes > 0)
    {
        var lngBytesCount = Request.TotalBytes;
        raw_request = BytesToStr(Request.BinaryRead(lngBytesCount));
    }
    try
    {
        var parsed_request = JSON.parse(raw_request);
    }
    catch (e)
    {
        __DoLog("JSON.parse faalt met: {0}<br>{1}".format(e.description, raw_request), "#f00");
        return {};
    }
    return parsed_request;
}

var data = RequestJSON();

protectRequest.validateToken(data.RVT_token);

var wgt_page_key = parseInt(data.wgt_page_key, 10);

var this_wgt_page = wgt.func_enabled_wgt_page(wgt_page_key);
user.auth_required_or_abort(this_wgt_page.canWrite);

var mode = data.mode;
var result = { success: false };

if (mode == "home")
{
    if (user.has("WEB_PRSSYS"))
    {
        var sql = "UPDATE fac_setting"
                + "   SET fac_setting_pvalue = " + wgt_page_key
                + " WHERE fac_setting_name = 'widgetportal'";
        var err = Oracle.Execute(sql, true);
        if (err.friendlyMsg)
            result.warning = err.friendlyMsg;
        else
        {
            result.toaster = L("fac_widget_homepage_saved");
            settings.loadSET(true); // Forceer caching opnieuw
        }
    }
}
else if (mode == "page")
{
    var widgets = data.widgets;

    // Per widget een rechten-check, klopt er 1 niet, dan klappen we er uit want dan is er iets fout (bug) of wordt er getamperd.
    var widgets = [];
    var widget = {};
    var error = false;
    for (w in data.widgets)
    {
        widget = data.widgets[w];
        widget.height = widget.height || null; // Null indien undefined, want !isNaN(null) == true

        // Verifiëer data;
        if (!isNaN(widget.key)      && widget.key === parseInt(widget.key, 10) &&
            !isNaN(widget.order)    && widget.order === parseInt(widget.order, 10) &&
            !isNaN(widget.width)    && widget.width === parseInt(widget.width, 10) &&
            !isNaN(widget.height)   && ((widget.height === parseInt(widget.height, 10)) || widget.height === null))
        {
            widgets.push(widget);
        }
        else
        {
            error = true;
            break;
        }
    }

    wgt.saveWidgetPage(wgt_page_key, widgets);
    result.success = !error;
}

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