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

     File:         job_edit_job_save.asp
     Description:  SUBMIT-form
*/ %>

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

<%
FCLTHeader.Requires({ plugins:["jQuery"] });
var job_key = getQParamInt("job_key", -1);

var rtable = getQParamInt("rtable", -1);
var interval  = getFParamInt("interval", 0);
var rkey = getQParamInt("rkey", -1);
var action = getFParamInt("action", -1);
var mkey = -1;

var rtablename= '';
if (rtable == 1) rtablename = 'bestelling';
if (rtable == 2) rtablename = 'melding';
if (rtable == 3) rtablename = 'opdracht';

var fields =  [ { dbs: "prs_perslid_key", typ: "key",  frm: "person" },
                { dbs: "fac_activiteit_xmlnode", typ: "varchar",  val: rtablename },
                { dbs: "fac_activiteit_status_key", typ: "key",  frm: "status" },
                { dbs: "fac_activiteit_omschrijving", typ: "varchar", frm: "act_descr" },
                { dbs: "fac_activiteit_eerste", typ: "date",  frm: "firstdate" }]

if (action == 1){ // eenmalig
    fields.push({  dbs: "fac_activiteit_interval",    typ: "number",      val: "" });
    fields.push({  dbs: "fac_activiteit_einddatum",    typ: "date",       val: "" });
    fields.push({  dbs: "fac_activiteit_aantal",    typ: "number",       val: "1" });
}
else if (action == 2) { // Tot einddatum
    fields.push({  dbs: "fac_activiteit_interval",    typ: "number",      val: interval * 24 });
    fields.push({  dbs: "fac_activiteit_einddatum",    typ: "date",       frm: "enddate" });
    fields.push({  dbs: "fac_activiteit_aantal",    typ: "number",       val: "" });
}
else if (action == 3)  { // Aantal keer
    fields.push({  dbs: "fac_activiteit_interval",    typ: "number",      val: interval * 24 });
    fields.push({  dbs: "fac_activiteit_einddatum",    typ: "date",       val: "" });
    fields.push({  dbs: "fac_activiteit_aantal",    typ: "number",       frm: "aantal" });
}

var warning = "";
if (job_key > 0)
  {
    sql = buildUpdate("fac_activiteit", fields)
        + "     fac_activiteit_key = " + job_key;

    var err = Oracle.Execute(sql, true);
    if (err.friendlyMsg)
        warning = err.friendlyMsg;
  }
else
  {
  var melding_key_valid = false;
  if (rtable == 3)  {
  var mkey = getFParamInt("job_mldnr", -1)
    sql = " select  'x' FROM mld_melding "
        + " WHERE   mld_melding_status in (2,3,4,7) and fac_activiteit_key is null and mld_melding_key = "+mkey;
    oRs = Oracle.Execute( sql );
    if (!oRs.eof)
      melding_key_valid = true;
    else
      warning = L("lcl_job_call_number_set");
    oRs.Close();
  }
  if (rtable != 3 || melding_key_valid == true)  {
    fields.push({  dbs: "fac_activiteit_key",         typ: "key",      seq: "fac_s_fac_activiteit_key" });
    fields.push({  dbs: "fac_activiteit_user_key",    typ: "key",      val: user_key });
    var regIns = buildInsert("fac_activiteit", fields);
    var job_key = regIns.sequences["fac_activiteit_key"];
    sql = regIns.sql;
    var err = Oracle.Execute(sql, true);
    if (err.friendlyMsg)
        warning = err.friendlyMsg;
    }
  }
%>
<html>
  <head>
    <%  FCLTHeader.Generate() %>
    <script>
        $(document).ready(function () {
            FcltMgr.closeDetail(window, { job_key: <%=job_key%>,
                                          rtable: <%=rtable%>,
                                          rkey: <%=rkey%>,
                                          mkey: <%=mkey%>,
                                          warning: "<%=safe.jsstring(warning)%>",
                                          keepForm: <%=warning?"true":"false"%>
                                         });
         } );
    </script>
  </head>
  <body>
  </body>
</html>
