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

     File:         prs_edit_bedrijfadres_save.asp
     Description:  SUBMIT-form
*/
%>
<%
var JSON_Result = true;
%>

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../shared/save2db.inc" -->
<!-- #include file="../Shared/json2.js" -->

<%
var bedrijf_key = getQParamInt("bedrijf_key");
var badr_key = getQParamInt("badr_key", -1);
var kanaal = getFParam("prs_type") // type is verplicht
var autfunction = 'WEB_PRSMGT';
var authparams = user.checkAutorisation(autfunction);
user.auth_required_or_abort(authparams && authparams.PRSwritelevel < 9);

var isDeleted = false;
var warning = "";

if ((badr_key > 0) && (kanaal == "X"))
{
    sql = "DELETE FROM prs_bedrijfadres"
        + " WHERE prs_bedrijf_key = " + bedrijf_key
        + "   AND prs_bedrijfadres_key = " + badr_key;
    isDeleted = true;

    var err = Oracle.Execute(sql, true);
    if (err.friendlyMsg)
        warning = err.friendlyMsg;
}
else
{
    var typeopdr = -1;
    if (kanaal == "O")
        typeopdr = getFParamInt("prs_typeopdr", -1);

    var isNew = (badr_key < 0);

    var fso = new ActiveXObject("Scripting.FileSystemObject");
    var xsl = getFParam("prs_xsl", "");
    if (xsl && !fso.FileExists(Server.MapPath(custpath + "/" + xsl))) // Zo doet puo_xmltools/ SafeLoadFileXSL dat
        abort_with_warning(xsl + " not found");

    var protocol = getFParam("protocol");
    var url = getFParam("prs_url");

    var fields =  [ { dbs: "prs_bedrijfadres_type",        typ: "varchar",  frm: "prs_type" }
                  , { dbs: "mld_typeopdr_key",             typ: "key",      val: typeopdr }
                  , { dbs: "prs_bedrijfadres_url",         typ: "varchar",  val: protocol + url}
                  , { dbs: "prs_bedrijfadres_username",    typ: "varchar",  frm: "prs_username" }
                  , { dbs: "prs_bedrijfadres_password",    typ: "varchar",  frm: "prs_password" }
                  , { dbs: "prs_bedrijfadres_ordermode",   typ: "number",   frm: "prs_ordermode" }
                  , { dbs: "prs_bedrijfadres_certificate", typ: "varchar",  frm: "prs_cert" }
                  , { dbs: "prs_bedrijfadres_xsl",         typ: "varchar",  frm: "prs_xsl" }
                  , { dbs: "prs_bedrijfadres_ext",         typ: "varchar",  frm: "prs_ext" }
                  , { dbs: "prs_bedrijfadres_attachfile",  typ: "varchar",  frm: "bijlage" }
                  , { dbs: "prs_bedrijfadres_flexfiles" ,  typ: "number",   frm: "flexfiles" }
                  ];

    var isDeleted = false;
    if (badr_key > 0)
    {
        sql = buildUpdate("prs_bedrijfadres", fields)
            + "     prs_bedrijfadres_key = " + badr_key;

        var err = Oracle.Execute(sql, true);
        if (err.friendlyMsg)
            warning = err.friendlyMsg;
    }
    else
    {
        fields.push( { dbs: "prs_bedrijf_key", typ: "key", val: bedrijf_key } );
        fields.push( { dbs: "prs_bedrijfadres_key", typ: "key", seq: "prs_s_prs_bedrijfadres_key" });
        var regIns = buildInsert("prs_bedrijfadres", fields);
        var badr_key = regIns.sequences["prs_bedrijfadres_key"];
        sql = regIns.sql;
        var err = Oracle.Execute(sql, true);
        if (err.friendlyMsg)
            warning = err.friendlyMsg;
    }
}

result = { success: true
         , deleted: isDeleted
         , bedrijf_key: bedrijf_key
         , badr_key: badr_key
         , warning: warning
         , keepForm: (warning?true:false)
         , refresh: true
         };
Response.Write(JSON.stringify(result));
%>
