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

     File:           fac_api.asp
     Description:
     Parameters:     api_key
*/
%>

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<!-- #include file="../Shared/rapportselector.inc" -->
<!-- #include file="fac.inc" -->
<!-- #include file="../Shared/json2.js" -->
<%
FCLTHeader.Requires({ plugins:["jQuery"] })

var api_key = getQParamInt("api_key", -1);

var hasFACFAC = user.checkAutorisation("WEB_FACFAC");

var api_name;
var api_omschrijving;
var api_apikey;
var api_filepath;
var api_loglevel;
var usrrap_key;
var api_viewmapping_json;
var api_stylesheet;
var import_app_key;
var api_options_json;
var api_xmlnode;

function prettyJson(j)
{
  try
  {
    var xx = JSON.stringify(eval("(" + j + ")"), null, 2);
    if (xx == "null")
        return "";
    return xx;
  }
  catch (e)
  {
    return j;
  };
};

if (api_key > 0)
{
  var sql = "SELECT * FROM fac_api a"
          + " WHERE fac_api_key =" + api_key;
  var oRs = Oracle.Execute(sql);
  var api_name             = oRs("fac_api_name").Value;
  var api_omschrijving     = oRs("fac_api_omschrijving").Value;
  var api_filepath         = oRs("fac_api_filepath").Value;
  var api_loglevel         = oRs("fac_api_loglevel").Value;
  var usrrap_key           = oRs("fac_usrrap_key").Value;
  var api_viewmapping_json = prettyJson(oRs("fac_api_viewmapping_json").Value);
  var api_stylesheet       = oRs("fac_api_stylesheet").Value;
  var import_app_key       = oRs("fac_import_app_key").Value;
  var api_options_json     = prettyJson(oRs("fac_api_options_json").Value);
  oRs.Close();
}
%>

<html>
  <head>
<%  FCLTHeader.Generate(); %>
    <script type="text/javascript" >

      jQuery(document).ready(function()
                              {
                                $('textarea').resize(function () { FcltMgr.resized(window) } );
                                $('textarea').autogrow();
                              });

      function api_submit_callback(json)
      {
          window.navigate("fac_show_api.asp?api_key=" + json.api_key);
      }

      function doSubmit()
      {
        document.activeElement.blur(); // trigger laatste onChanges

        // Standaard checks op verplichte velden, datum formaat, numeriek en float formaat, currency formaat.
        if (!validateForm("u2"))
          return false;

        $.post($("form[name=u2]")[0].action, $("[name=u2]").serialize(), FcltCallbackAndThen(api_submit_callback), "json");
      }

      function doCancel()
      {
        FcltMgr.closeDetail(window, { cancel: true } );
      }

    </script>
  </head>

  <body id="editbody">
<%
  var buttons = []
  buttons.push ({title: L("lcl_submit"), icon: "opslaan.png", action: "doSubmit()" });
  buttons.push( {title: L("lcl_cancel"), icon: "undo.png", action: "doCancel()" } );
    IFRAMER_HEADER("Setting", buttons);
%>
    <div id="edit">
      <form name=u2
            method="post"
            action="fac_edit_api_save.asp?api_key=<%=api_key%>"
            onSubmit="doSubmit();">
<%
        BLOCK_START("apiSample", L("lcl_fac_api"));
        RWFIELDTR("fac_api_name",                "fld required", L("lcl_fac_api_name")        , api_name,             {maxlength: 32});
        RWFIELDTR("fac_api_omschrijving",        "fld",          L("lcl_fac_api_omschrijving"), api_omschrijving,     {maxlength: 60});
        RWFIELDTR("fac_api_filepath",            "fld required", L("lcl_fac_api_filepath")    , api_filepath,         {maxlength: 64});
        RWFIELDTR("fac_api_loglevel",            "fld",          L("lcl_fac_api_loglevel")    , api_loglevel);
        FCLTrapportselector("fac_usrrap_key"
                           ,"sgRapport"
                           ,{ label: L("lcl_fac_api_usrrap")
                            , rapportKey: usrrap_key
                            , readonly: false
                            });
        RWTEXTAREATR("fac_api_viewmapping_json", "fldtxt",       L("lcl_fac_api_viewmapping") , api_viewmapping_json, {maxlength: 4000});
        RWFIELDTR("fac_api_stylesheet",          "fld",          L("lcl_fac_api_stylesheet")  , api_stylesheet,       {maxlength: 64});
        RWFIELDTR("fac_import_app_key",          "fld",          L("lcl_fac_import_app")      , import_app_key);
        RWTEXTAREATR("fac_api_options_json",     "fldtxt",       L("lcl_fac_api_options")     , api_options_json,     {maxlength: 4000});
        BLOCK_END();
IFACE.FORM_END();
%>
      </form>
    </div>
  </body>
</html>
