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

     File:           prs_edit_bedrijfadres.asp
     Description:    edit or add bedrijfadres
     Parameters:     badr_key     // bedrijfadres_key
                     bedrijf_key
     Context:
     Note:
*/
%>

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<!-- #include file="../Shared/selector.inc" -->

<%
FCLTHeader.Requires({plugins:["jQuery"], js: []})

var badr_key = getQParamInt("badr_key", -1);
var prs_bdrkey = getQParamInt("bedrijf_key", -1);
var autfunction = 'WEB_PRSMGT';
var authparams = user.checkAutorisation(autfunction);
user.auth_required_or_abort(authparams && authparams.PRSwritelevel < 9);

if (badr_key == -1)
{
  sql = "SELECT prs_bedrijf_naam"
      + "  FROM prs_bedrijf"
      + " WHERE prs_bedrijf_key = " + prs_bdrkey;
  oRs = Oracle.Execute(sql);
  var prs_bdrnaam = oRs("prs_bedrijf_naam").value;
  oRs.Close();
}
else
{
  sql = "SELECT a.prs_bedrijfadres_key"
      + ", a.prs_bedrijf_key"
      + ", b.prs_bedrijf_naam"
      + ", a.prs_bedrijfadres_type"
      + ", a.mld_typeopdr_key"
      + ", a.prs_bedrijfadres_url"
      + ", a.prs_bedrijfadres_username"
      + ", a.prs_bedrijfadres_password"
      + ", a.prs_bedrijfadres_ordermode"
      + ", a.prs_bedrijfadres_certificate"
      + ", a.prs_bedrijfadres_xsl"
      + ", a.prs_bedrijfadres_ext"
      + ", a.prs_bedrijfadres_attachfile"
      + ", a.prs_bedrijfadres_flexfiles"
      + " FROM prs_bedrijfadres a"
      + ", prs_bedrijf b"
      + " WHERE b.prs_bedrijf_key = a.prs_bedrijf_key"
      + "   AND prs_bedrijfadres_key = " + badr_key
  oRs = Oracle.Execute(sql);
  if (oRs.Eof)
    shared.record_not_found();

  var prs_bdrkey    = oRs("prs_bedrijf_key").value;
  var prs_bdrnaam   = oRs("prs_bedrijf_naam").value;
  var prs_type      = oRs("prs_bedrijfadres_type").value;
  var prs_typeopdr  = oRs("mld_typeopdr_key").value;
  var prs_url       = oRs("prs_bedrijfadres_url").value;
  var username      = oRs('prs_bedrijfadres_username').value;
  var password      = oRs('prs_bedrijfadres_password').value;
  var prs_ordermode = oRs("prs_bedrijfadres_ordermode").value;
  var prs_cert      = oRs("prs_bedrijfadres_certificate").value;
  var prs_xsl       = oRs("prs_bedrijfadres_xsl").value;
  var prs_ext       = oRs("prs_bedrijfadres_ext").value;
  var bijlage       = oRs('prs_bedrijfadres_attachfile').value;
  var flexfiles     = oRs('prs_bedrijfadres_flexfiles').value;

  oRs.Close();
}
%>
<html>
  <head>
<%
    FCLTHeader.Generate();
%>
<script type="text/javascript" >
    function badr_submit()
    {
      if (!validateForm("u2"))
        return false;

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

    function badr_cancel()
    {
      FcltMgr.closeDetail(window, { cancel: true } );
    }
    function typeChange()
    {
      var noTypeopdr = $("#prs_type").val() != "O";
      $("#prs_typeopdr").prop("disabled", noTypeopdr);
    }

    function editBijlageCallback(data, orgData)
    {
      $("#bijlage").val(data.filenaam);
    }

    function edit_bijlage()
    {
      var url = "<%=protectQS.create("../../appl/shared/BijlagenForm.asp?module=BDRADR&multi=1")%>";
      FcltMgr.openModalDetail(url, L("lcl_mrk_upload_foto"),
                  { callback: editBijlageCallback } );
    }
    $(function () {
        $("#selxsl").change(function () {
             $("#prs_xsl").val(this[this.selectedIndex].text);
        });
    });

</script>
  </head>

  <body id="editbody">
<%
    var buttons =  [ {title: L("lcl_submit"), action:"badr_submit()", icon: "opslaan.png" }
                   , {title: L("lcl_cancel"), action:"badr_cancel()", icon: "undo.png" }
                   ];

    IFRAMER_HEADER(L("lcl_prs_bedrijfadres_frame"), buttons);
%>
    <div id="edit">
      <form name="u2"
            action="prs_edit_bedrijfadres_save.asp?bedrijf_key=<%=prs_bdrkey%>&badr_key=<%=badr_key%>"
            method="post">
<%
      BLOCK_START("prsBedrijfadres", L("lcl_prs_basisblok"));
        ROFIELDTR("fld", L("lcl_prs_companies_name"), prs_bdrnaam);
        // type
        sql = "SELECT 'B' prs_type"
            + ", " + safe.quoted_sql(L("lcl_prs_bedrijfadres_type_b"))+" prs_type_oms"
            + "  FROM DUAL"
            + " UNION SELECT 'O', " + safe.quoted_sql(L("lcl_prs_bedrijfadres_type_o"))+" FROM DUAL"
            + " UNION SELECT 'C', " + safe.quoted_sql(L("lcl_prs_bedrijfadres_type_c"))+" FROM DUAL"
        FCLTselector("prs_type"
                    , sql
                    , { initKey: prs_type
                      , label: L("lcl_prs_bedrijfadres_type")
                      , onChange: "typeChange()"
                      , emptyOption : prs_type?null:""
                      , required: !prs_type
                      }
                    );

        // Opdrachttype
        sql = "SELECT mld_typeopdr_key"
            + ", "+ lcl.xsqla('t.mld_typeopdr_omschrijving', 't.mld_typeopdr_key')
            + " FROM mld_typeopdr t"
            + " ORDER BY 2";
        FCLTselector("prs_typeopdr"
                    , sql
                    , { initKey: prs_typeopdr
                      , label: L("lcl_mld_typeopdr")
                      , emptyOption : L("lcl_prs_bedrijfadres_type_o_other")

                      , initEmpty: true // Altijd lege waarde tonen, ook als er maar 1 waarde is.
                      , disable: (prs_type!="O") // Alleen voor bestelopdrachten te gebruiken.
                      }
                    );

        var protocol = "";
        var arr = (prs_url||"").split(":");
        if (arr.length > 1)
        {
            protocol = arr[0].toLowerCase() + ":";
            if (protocol != "mailto:")
                protocol += "//";
            prs_url = prs_url.substr(protocol.length);
        }
        sql = "SELECT         'mailto:', 'mailto:' FROM DUAL"
            + " UNION SELECT  'https://', 'https://' FROM DUAL"
            + " UNION SELECT  'http://', 'http://' FROM DUAL"
            + " UNION SELECT  'ftp://', 'ftp://' FROM DUAL"
            + " ORDER BY 1";
        FCLTselector('protocol', sql,
                          { label: L("lcl_prs_bedrijfadres_protocol"),
                            initKey: protocol,
                            initEmpty: !protocol,
                            required: !protocol
                          }
                       );
        RWFIELDTR("prs_url",       "fld", L("lcl_prs_bedrijfadres_url"),            prs_url,       {maxlength: 512, required: true});
        RWFIELDTR("prs_username",  "fld", L("lcl_prs_bedrijfadres_username"),       username,      {maxlength: 32});
        RWFIELDTR("prs_password",  "fld", L("lcl_prs_bedrijfadres_wachtwoord"),     password,      {maxlength: 32});
        RWFIELDTR("prs_ordermode", "fld", L("lcl_prs_companies_order_mode"),        prs_ordermode, {maxlength: 3});
        RWFIELDTR("prs_cert",      "fld", L("lcl_prs_companies_order_certificate"), prs_cert,      {maxlength: 255});
        var att_f = Server.MapPath(custpath + "/xsl");
        var objFso = Server.CreateObject("Scripting.FileSystemObject");
        var selfilehtml = "";
        if (objFso.FolderExists(att_f))
        {
            var selfilehtml = "<select id='selxsl'><option>" + L("lcl_prs_companies_xsl_choose") + "</option>";
            var objFiles = objFso.GetFolder(att_f);
            var allFiles = new Enumerator(objFiles.files);
            for (; !allFiles.atEnd(); allFiles.moveNext())
            {
                var attFile = allFiles.item();
                var name = attFile.name;
                if (name.match(/_20......_......\./)) // De backups: melding_20150122_154913.xsl
                    continue;                         // hoeven we niet
                var vFileDate = attFile.DateLastModified
                var vPath = attFile.Path.substr(Server.MapPath(custpath).length+1);
                selfilehtml += "<option>" + safe.html(vPath) + "</option>";
            }
            selfilehtml += "</select>";
        }
        RWFIELDTR("prs_xsl",       "fld", L("lcl_prs_companies_xsl"),               prs_xsl,       {maxlength: 256, reartext: selfilehtml});
        RWFIELDTR("prs_ext",       "fld", L("lcl_prs_bedrijfadres_ext"),            prs_ext,       {maxlength: 10});
        var def = " (" + (S("puo_order_flexfiles")?L("lcl_Yes"):L("lcl_No")) + ")";
        sql = "SELECT         0, " + safe.quoted_sql(L("lcl_default") + def) + " FROM DUAL"
            + " UNION SELECT  1, " + safe.quoted_sql(L("lcl_Yes")) + " FROM DUAL"
            + " UNION SELECT  2, " + safe.quoted_sql(L("lcl_No")) + " FROM DUAL"
            + " ORDER BY 1";
        FCLTselector('flexfiles', sql,
                          { label:  L("lcl_puo_order_flexfiles"),
                            initKey: flexfiles||0
                          }
                       );
%>
        <tr valign="top">
          <td class="label ">
            <label for="k1val"><span title=""><%=L("lcl_prs_bedrijfadres_bijlage")%>:</span></label>
          </td>
          <td>
            <input type="text" id="bijlage" name="bijlage" class="fld" value="<%=safe.htmlattr(bijlage)%>" readonly>
            &nbsp;
            <input type="button" class="mbutton" onClick="edit_bijlage()">
          </td>
        </tr>
<%
      BLOCK_END();

    IFACE.FORM_END();
%>
      </form>
    </div>
  </body>
</html>
