<% /*
     $Revision$
     $Id$

    File:           scaffolding_m_edit.asp
    Description:    show detailed information of a model

    Parameters:

    Context:
    Note:

*/ %>
<%
function scaffolding_m_edit(model, scf_params)
{
    FCLTHeader.Requires({ plugins: ["jQuery"],
                               js: ["jquery-ui.js", "jquery.timepicker-table.js"],
                              css: ["timePicker-table.css"]})

    if ("requires" in scf_params.edit)
        FCLTHeader.Requires(scf_params.edit.requires);

    var transit = scf_transit2url(scf_params);

    var key = getQParamInt("id", -1);
    if (key > 0)
    {
        user.auth_required_or_abort(model.REST_PUT);
        if (!model.data) // Zelf nog ophalen
        {
            var xxx_params = { filter: { id: key } };
            var xxx_array = model.REST_GET(xxx_params);

            if (!xxx_array.length)
                shared.record_not_found();
             model.data = xxx_array[0];
        }
    }
    else
    {
        user.auth_required_or_abort(model.REST_POST);
        model.data = {};
    }
    if ("hook_pre_edit" in model)
        model.hook_pre_edit(model.data, model.fields);

    function makeButtons()
    {
        CONTROLGROUP_START({ horizontal: true, id: "mybuttons" })
            for (var i = 0; i < model.edit.buttons.length; i++)
            {
                var btn = model.edit.buttons[i];
                BUTTON(btn.label, btn.params);
            }
        CONTROLGROUP_END()
    }
    %>

    <html>
      <head>
    <%  FCLTMHeader.Generate(); %>
        <script>
          function scf_submit()
          {
            if (!validateForm("u2"))
              return false;
            $.post($("form[name=u2]")[0].getAttribute("action"), $("[name=u2]").serialize(), FcltCallbackClose, "json");
            return true; // disable button
          }

          function scf_cancel()
          {
            FcltMgr.closeDetail(window, { cancel: true } );
          }
          var key = <%=key%>;
        </script>
        <style>
            geenhekjeaanbeginregel,#mybuttons .ui-btn { height: 26px; width: 40px; font-size: 18px; }
        </style>

      </head>
      <body>
        <div>
    <%
        PAGE_START();
        HEADER({title: model.record_title, back: true, home: true });
        CONTENT_START();

        makeButtons();
    %>
          <form name="u2" method="post" action="<%=scf_params.this_fullpath%>?mode=save&id=<%=key%><%=transit%>" onsubmit="return false;">
    <%
            var editfields = model.edit.fields || [];
            if (!editfields.length)
            {
                for (var fld in model.fields)
                    editfields.push(fld);
            }
            for (var i = 0; i < editfields.length; i++)
            {
                var fld = editfields[i];
                if (fld instanceof Function)
                {
                    fld();
                }
                else
                {
                    var field = model.fields[fld];
                    if (field.readonly || fld == "id")
                        scf_ROFIELD(model, fld, model.data[fld]);
                    else if (field.insertonly && key > 0)
                        scf_ROFIELD(model, fld, model.data[fld]);
                    else
                        scf_RWFIELD(field, fld, model.data[fld]);
                }
            }
            IFACE.FORM_END();
    %>
          </form>
<%
      CONTENT_END();
      FOOTER();
      PAGE_END();
%>
        </div>
      </body>
    </html>
<%
}
%>