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

    File:           prs_edit_contactpersoon.asp
    Description:    edit or add persons
    Parameters:     cps_key (required)

    Context:
    Note:

*/ %>

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

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

var cps_key = getQParamInt("cps_key", -1);
var prs_bdrkey = getQParamInt("bedrijf_key", -1);
var autfunction = 'WEB_RELUSE';
var authparams = user.checkAutorisation(autfunction);
user.auth_required_or_abort(authparams && authparams.ALGwritelevel < 9);

// Veronderstel even dat ik schrijfrechten had om hier te komen
if (cps_key > -1)
{
  sql = "SELECT c.prs_contactpersoon_key"
      + ", c.prs_bedrijf_key"
      + ", prs_bedrijf_naam"
      + ", prs_contactpersoon_aanhef"
      + ", prs_contactpersoon_voorletters"
      + ", prs_contactpersoon_naam"
      + ", prs_contactpersoon_voornaam"
      + ", prs_contactpersoon_tussenv"
      + ", prs_contactpersoon_functie"
      + ", prs_contactpersoon_titel"
      + ", prs_contactpersoon_telefoon_1"
      + ", prs_contactpersoon_telefoon_2"
      + ", prs_contactpersoon_fax"
      + ", prs_contactpersoon_email"
      + ", prs_contactpersoon_opmerking"
      + ", prs_contactpersoon_uurloon"
      + ", c.prs_perslid_key"
      + " FROM prs_contactpersoon c"
      + ", prs_bedrijf b"
      + " WHERE b.prs_bedrijf_key = c.prs_bedrijf_key"
      + " AND prs_contactpersoon_key = " + cps_key
  oRs = Oracle.Execute(sql);

  var prs_bdrkey = oRs("prs_bedrijf_key").value;
  var prs_bdrnm  = oRs("prs_bedrijf_naam").value;
  var prs_aanh   = oRs("prs_contactpersoon_aanhef").value;
  var prs_voorl  = oRs("prs_contactpersoon_voorletters").value;
  var prs_naam   = oRs("prs_contactpersoon_naam").value;
  var prs_vrnaam = oRs("prs_contactpersoon_voornaam").value;
  var prs_tsnvgs = oRs("prs_contactpersoon_tussenv").value;
  var prs_func   = oRs("prs_contactpersoon_functie").value;
  var prs_titel  = oRs("prs_contactpersoon_titel").value;
  var prs_tel1   = oRs("prs_contactpersoon_telefoon_1").value;
  var prs_tel2   = oRs("prs_contactpersoon_telefoon_2").value;
  var prs_fax    = oRs("prs_contactpersoon_fax").value;
  var prs_email  = oRs("prs_contactpersoon_email").value;
  var prs_opm    = oRs("prs_contactpersoon_opmerking").value;
  var prs_loon   = oRs("prs_contactpersoon_uurloon").value;
  var prs_prskey = oRs("prs_perslid_key").value;

  oRs.Close();
}
%>

<html>
  <head>
<%  FCLTHeader.Generate();

    //if (cps_key > -1)
    // var xfunc = user.func_enabled("*", null, null, prs_key); TODO, werken maken voor contactpersonen?
%>
    <script  type="text/javascript" >
      function cps_submit()
      {
        document.activeElement.blur(); // trigger laatste onChanges
        if (!validateForm("u2"))
          return false;

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

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

      function process_perslid_info(prs_info)
      {
        $("#prs_naam").val(prs_info.prs_perslid_achternaam? prs_info.prs_perslid_achternaam : "");
        $("#prs_tsnvgs").val(prs_info.prs_perslid_tussenvoegsel? prs_info.prs_perslid_tussenvoegsel: "");
        $("#prs_voorl").val(prs_info.prs_perslid_voorletters? prs_info.prs_perslid_voorletters : "");
        $("#prs_vrnaam").val(prs_info.prs_perslid_voornaam? prs_info.prs_perslid_voornaam : "");
        $("#prs_titel").val(prs_info.prs_perslid_titel? prs_info.prs_perslid_titel : "");
        $("#prs_tel1").val(prs_info.prs_perslid_telefoon? prs_info.prs_perslid_telefoon : "");
        $("#prs_tel2").val(prs_info.prs_perslid_mobiel? prs_info.prs_perslid_mobiel : "");
        $("#prs_email").val(prs_info.prs_perslid_email ? prs_info.prs_perslid_email : "");
        $("#prs_func").val(prs_info.prs_srtperslid? prs_info.prs_srtperslid : "");
        // Aanhef is niet aanwezig in prs_perslid tabel
        // Fax is een flexkenmerk en is niet altijd aanwezig
      }

      function onChangeName(p_key)
      {
        if (p_key > 0)
          $.getJSON("../prs/get_prs_info_ajax.asp?req_info=prs_perslid&prs_perslid_key=" + p_key, process_perslid_info);
      }
    </script>
  </head>

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

    IFRAMER_HEADER(L("lcl_prs_contactpersoon_frame"), buttons);
%>
    <div id="edit">
      <form name="u2"
            action="prs_edit_contactpersoon_save.asp?cps_key=<%=cps_key%>"
            method="post"
            onSubmit="cps_submit();">
<%
        BLOCK_START("prsPerslid", L("lcl_prs_basisblok"));

        sql = "SELECT prs_bedrijf_key"
            + ", prs_bedrijf_naam"
            + " FROM prs_v_aanwezigbedrijf"
            + " WHERE prs_bedrijf_intern IS NULL"
            + " ORDER BY prs_bedrijf_naam_upper";
        FCLTselector("prs_bdrkey", sql,
                    { label: L("lcl_prs_companies_name"),
                      initKey: prs_bdrkey,
                      emptyOption: "",
                      readonly: (prs_bdrkey != -1),
                      required: true
                   });
        // Een readonly selector heeft geen name, en wordt dus niet gesubmit. Bij toevoegen heb ik die wel nodig.
%>
        <input type="hidden" name="prs_bdrkey" value="<%=prs_bdrkey%>">
<%      FCLTpersoonselector("prs_prskey",
                            "sgPerson",
                            { label: L("lcl_person"),
                              perslidKey: prs_prskey,
                              filtercode: "ORDBO2",
                               onChange: "onChangeName"
                            });
        RWFIELDTR("prs_naam", "fld", L("lcl_prs_companies_contact_naam"), prs_naam, {required: true, maxlength: 30});
        RWFIELDTR("prs_tsnvgs", "fldshort", L("lcl_prs_companies_contact_tussenv"), prs_tsnvgs, {maxlength: 15});
        RWFIELDTR("prs_voorl", "fldshort", L("lcl_prs_companies_contact_voorl"), prs_voorl, {maxlength: 10});
        RWFIELDTR("prs_vrnaam", "fld", L("lcl_prs_companies_contact_voorn"), prs_vrnaam, {maxlength: 30});
        RWFIELDTR("prs_aanh", "fld", L("lcl_prs_companies_contact_aanhef"), prs_aanh, {maxlength: 30});
        RWFIELDTR("prs_titel", "fldshort", L("lcl_prs_companies_contact_titel"), prs_titel, {maxlength: 10});
        RWFIELDTR("prs_func", "fld", L("lcl_prs_companies_contact_functie"), prs_func, {maxlength: 30});
        RWFIELDTR("prs_tel1", "fld", L("lcl_prs_companies_contact_tel1"), prs_tel1, {maxlength: 30});
        RWFIELDTR("prs_tel2", "fld", L("lcl_prs_companies_contact_tel2"), prs_tel2, {maxlength: 30});
        RWFIELDTR("prs_fax", "fld", L("lcl_prs_companies_contact_fax1"), prs_fax, {maxlength: 30});
        RWFIELDTR("prs_email", "fld", L("lcl_prs_companies_contact_email"), prs_email, {maxlength: 50});
        RWTEXTAREATR("prs_opm", "fldtxt", L("lcl_prs_companies_opmerking"), prs_opm);
        RWFIELDTR("prs_loon", "fldshort", L("lcl_prs_companies_uurloon"), prs_loon, {maxlength: 8, datatype: "float"});
        BLOCK_END();

        BLOCK_START("prsFlex"+(S("prs_flexcolumns")!=1?"2":""), L("lcl_prs_flexblok"));
        generateFlexKenmerkCode ({link_key       : cps_key,
                                  niveau         : "C",
                                  reado          : false,
                                  flexcolumns    : S("prs_flexcolumns")
                                 });
        BLOCK_END();
IFACE.FORM_END();
%>
      </form>
    </div>
  </body>
</html>
