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

    File:           bez/bez_make_contact.asp
    Description:    Maak handig nieuw bedrijf(+contactpersoon aan)
    Parameters:

    Context:        Bezoekers aanmelden (ontvangstbalie)
    Note:

*/ %>
<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<!-- #include file="../Shared/save2db.inc" -->
<!-- #include file="../Shared/json2.js" -->
<!-- #include file="./bez.inc" -->

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

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

var this_bez = bez.func_enabled_afspraak(-1);
user.auth_required_or_abort(this_bez.canContact);

%>

<html>
  <head>
<%  FCLTHeader.Generate();
    var submitting = getQParamInt("submit", 0) == 1;
    if (submitting)
    {
      // eerst het bedrijf aanmaken
      var fields =  [ { dbs: "prs_bedrijf_key",  typ: "key",     seq: "prs_s_prs_alluitvoerende_keys" },
                      { dbs: "prs_bedrijf_naam", typ: "varchar", frm: "bdr_naam" }
                    ];
      var regIns = buildInsert("prs_bedrijf", fields);
      var bedrijf_key = regIns.sequences["prs_bedrijf_key"];
      sql = regIns.sql;
      var err = Oracle.Execute(sql, true);
      if (err.friendlyMsg)
        abort_with_warning(err.friendlyMsg);

      // nu de contactpersoon aanmaken
      var fields =  [ { dbs: "prs_bedrijf_key",                 typ: "key",     val: bedrijf_key },
                      { dbs: "prs_contactpersoon_key",          typ: "key",     seq: "prs_s_prs_contactpersoon_key" },
                      { dbs: "prs_contactpersoon_naam",         typ: "varchar", frm: "prs_naam" },
                      { dbs: "prs_contactpersoon_voornaam",     typ: "varchar", frm: "prs_vrnaam" },
                      { dbs: "prs_contactpersoon_voorletters",  typ: "varchar", frm: "prs_voorl" },
                      { dbs: "prs_contactpersoon_tussenv",      typ: "varchar", frm: "prs_tsnvgs" }];

      var regIns = buildInsert("prs_contactpersoon", fields);
      var cont_key = regIns.sequences["prs_contactpersoon_key"];
      var err = Oracle.Execute(regIns.sql, true);
      if (err.friendlyMsg)
        abort_with_warning(err.friendlyMsg);

      var prs_string = getFParam("prs_naam")
                     + (getFParam("prs_voorl")? ", " + getFParam("prs_voorl") : "")
                     + (getFParam("prs_tsnvgs")? " " + getFParam("prs_tsnvgs") : "")
                     + (getFParam("prs_vrnaam")? " (" + getFParam("prs_vrnaam") + ")" : "");
%>    <script>
        FcltMgr.closeDetail(window, { success: true,
                                      cont_key: <%=cont_key%>,
                                      naam: "<%=safe.jsstring(prs_string)%>",
                                      bedrijf: "<%=safe.jsstring(getFParam("bdr_naam"))%>"
                                    } );
      </script><%
      Response.End;
    }
%>
    <script type="text/javascript">
      function bez_submit()
      {
          document.activeElement.blur(); // trigger laatste onChanges
          if (!validateForm("u2"))
              return false;
          document.forms.u2.submit();
      }
      function bez_cancel()
      {
          FcltMgr.closeDetail(window);
      }
      jQuery(document).ready(function()
           {
             FcltMgr.fixActiveelementError();
             $("#bdr_naam").focus();
           });

    </script>
  </head>

  <body class="modal" id="mod_edit_prs_modal" style="width:400px">
    <form name="u2"
          action="bez_make_contact.asp?submit=1"
          method="post"
          target="hidFrameSubmit"
          onSubmit="bez_submit();">
<%
      BLOCK_START("bezBezoekers", L("lcl_prs_basisblok"));
      RWFIELDTR("bdr_naam",   "fld",      L("lcl_prs_companies_name"),            "", {required: true, maxlength: 60});
      RWFIELDTR("prs_naam",   "fld",      L("lcl_prs_companies_contact_naam"),    "", {required: true, maxlength: 30});
      RWFIELDTR("prs_vrnaam", "fld",      L("lcl_prs_companies_contact_voorn"),   "", {maxlength: 30});
      RWFIELDTR("prs_tsnvgs", "fldshort", L("lcl_prs_companies_contact_tussenv"), "", {maxlength: 15});
      RWFIELDTR("prs_voorl",  "fldshort", L("lcl_prs_companies_contact_voorl"),   "", {maxlength: 10});
      BLOCK_END();

      CreateButtons([{ title: L("lcl_submit"), action: "bez_submit()"},
                     { title: L("lcl_cancel"), action: "bez_cancel()"}]);
IFACE.FORM_END();
%>
    </form>
     <iframe src="../Shared/empty.html" name="hidFrameSubmit" style="display:none"></iframe>
  </body>
</html>
