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

    File:           job_edit_job.asp
    Description:    show detailed information of a job

    Parameters:     job_key

    Context:
    Note:

*/ %>
<!-- #include file="../Shared/common.inc"-->
<!-- #include file="../Shared/iface.inc"-->
<!-- #include file="../Shared/persoonselector.inc" -->
<!-- #include file="../Shared/calendar.inc" -->

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

  authparams = user.checkAutorisation("WEB_MLDBOF", true);

  var MLDBOF_read  = authparams && (authparams.PRSreadlevel < 9 && authparams.ALGreadlevel < 9);
  var MLDBOF_write = authparams && (authparams.PRSwritelevel < 9 && authparams.ALGwritelevel < 9);

  authparams = user.checkAutorisation("WEB_MLDFOF", true);

  var MLDFOF_read  = authparams && (authparams.PRSreadlevel < 9 && authparams.ALGreadlevel < 9);
  var MLDFOF_write = authparams && (authparams.PRSwritelevel < 9 && authparams.ALGwritelevel < 9);

  authparams = user.checkAutorisation("WEB_BESFOF", true);

  var BESFOF_read  = authparams && (authparams.PRSreadlevel < 9 && authparams.ALGreadlevel < 9);
  var BESFOF_write = authparams && (authparams.PRSwritelevel < 9 && authparams.ALGwritelevel < 9);

if (!(MLDFOF_read || MLDFOF_write || BESFOF_read || BESFOF_write || MLDBOF_read || MLDBOF_write)) {
  shared.simpel_page(L("lcl_no_auth"));
}

var job_key = getQParamInt("job_key");
var rtable = getQParamInt("rtable", -1);
var actiontxt = "";
var objfirstdate = new Date();
var objenddate = new Date();
var action = 2; // default for add mode
var status = 2; // default for add mode
var rkey = -1;

var person = user_key;

if (job_key > -1)
{
var sql =
  "SELECT act.fac_activiteit_key "
  +"      ,act.fac_activiteit_interval/24 interval"
  +"      ,act.fac_activiteit_aantal "
  +"      ,act.prs_perslid_key "
  +"      ,fac_activiteit_eerste "
  +"      ,fac_activiteit_einddatum "
  +"      ,act.fac_activiteit_status_key "
  +"      ,act.fac_activiteit_aantal "
  +",DECODE(fac_activiteit_volgende,NULL,0,1) reado "
  +","+     S("prs_pers_string")+" naam "
  +"      ,fac_activiteit_laatste "
  +"      ,fac_activiteit_omschrijving "
  +"      ,fac_activiteit_xmlnode "
  +" FROM   FAC_ACTIVITEIT act, prs_perslid p "
  +" WHERE  act.prs_perslid_key = p.prs_perslid_key "
  +" AND	  fac_activiteit_key = "+job_key;

oRs=Oracle.Execute(sql);

    var interval  = oRs("interval").value;
    var person    = oRs("prs_perslid_key").value;
    var firstdate = oRs("fac_activiteit_eerste").value;
    var enddate   = oRs("fac_activiteit_einddatum").value;
    var status    = oRs("fac_activiteit_status_key").value;
    var act_descr = oRs("fac_activiteit_omschrijving").value;
    var aantal    = oRs("fac_activiteit_aantal").value;
    var type      = oRs("fac_activiteit_xmlnode").value;
      if (type == "bestelling")
        rtable = 1;
      if (type == "melding")
        rtable = 2;
      if (type == "opdracht")
        rtable = 3;

      if (firstdate == null)
        var firstEmpty = "1";
      else
        objfirstdate = new Date(firstdate);

      if (enddate == null)
        var endEmpty = "1"
      else
        objenddate = new Date(enddate);

      naam      = oRs("naam").value;
      lastdate  = oRs("fac_activiteit_laatste").value;
      if (interval == 0) {
        interval = '1';
      }

      if (type == "bestelling")
      {
        sql2 ="SELECT bes_bestelling_key "
             + "  FROM bes_bestelling "
             + " WHERE fac_activiteit_key = "+ job_key;

        oRs2 = Oracle.Execute (sql2);
        if (!oRs2.eof)
          rkey = oRs2("bes_bestelling_key").value;
        oRs2.Close();
      }

      if (type == "melding")
      {
        sql2 = "SELECT mld_melding_key "
             + "  FROM mld_melding "
             + " WHERE fac_activiteit_key = " + job_key;

        oRs2 = Oracle.Execute (sql2);
        if (!oRs2.eof)
          rkey = oRs2("mld_melding_key").value;
        oRs2.Close();
      }

      if (type == "opdracht")
      {
        sql2 = "SELECT mld_opdr_key "
             + "  FROM mld_opdr "
             + " WHERE fac_activiteit_key = "+ job_key;

        oRs2 = Oracle.Execute (sql2);
        if (!oRs2.eof)
          rkey = oRs2("mld_opdr_key").value;
        oRs2.Close();
      }

      // 1-malig
      if ((aantal == 1) && (firstdate != '')) {
        action = 1;
        actiontxt = L("lcl_job_action_1");
      } else {
        // Tot einddatum
        if (aantal == '' || aantal == null) {
          action = 2;
          actiontxt = L("lcl_job_action_2");
        } else {
          if (aantal != '' && aantal != null) {
              // Aantal keer
              action = 3;
              actiontxt = L("lcl_job_action_3");
          }
        }
      }
    oRs.Close();
}
%>

<html>
<head>
<%
   FCLTHeader.Generate();
%>
<script type="text/javascript">
    function job_submit()
    {
        document.activeElement.blur(); // trigger laatste onChanges
        if (!validateForm("u2"))
            return false;

        if (checkInput())
          document.forms.u2.submit();
    }

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

  if (document.getElementById("action")) {

    if (document.getElementById("action").value == '1') {
      $('#aantal').val("1");
      $('#enddate').val("");
      $('#interval').val("");


      $('#aantal').attr("disabled", true);
      $('#show_firstdate').removeAttr("disabled"); // TODO: knopje ook uit/in schakkelen gaat nu fout als er op wordt gedrukt.
      $('#show_enddate').attr("disabled", true);
      $('#interval').attr("disabled", true);
    }

    if (document.getElementById("action").value == '2') {
      $('#aantal').removeAttr("disabled");
      $('#aantal').val("");

      $('#aantal').attr("disabled", true);
      $('#show_firstdate').removeAttr("disabled");
      $('#show_enddate').removeAttr("disabled");
      $('#interval').removeAttr("disabled");
    }

    if (document.getElementById("action").value == '3') {
      $('#show_enddate').val("");

      $('#aantal').removeAttr("disabled");
      $('#show_firstdate').removeAttr("disabled");
      $('#show_enddate').attr("disabled", true);
      $('#interval').removeAttr("disabled");
    }
  }
}

function checkInput() {

    if (sgPerson.getValue()==-1)
      {
        alert(L("lcl_job_requestor_empty"));
        return false;
      }

      if (document.getElementById("show_firstdate").value == '') {
            alert(L("lcl_job_firstdate_empty"));
            return false;
      }

     if ((document.getElementById("show_enddate").value == '') && (document.getElementById("action").value == '2')) {
          alert(L("lcl_job_enddate_empty"));
          return false;
        }

    if (document.getElementById("interval")) {
      interval_value = document.getElementById("interval").value;
      if (interval_value != '') {
        if (!isGoodInteger(interval_value) || (interval_value < 0)) {
          valid = false;
          if( ! valid ) {
            alert(L("lcl_job_period_error"));
            return false;
          }
        }
      }
    }

    if (document.getElementById("aantal")) {
      aantal_value = document.getElementById("aantal").value;
      if ((aantal_value != '') || (document.getElementById("action").value == '3')) {
        if (!isGoodInteger(aantal_value) || (aantal_value<=0)) {
          valid = false;
          if( ! valid ) {
            alert(L("lcl_job_n_times_error"));
            return false;
          }
        }
      }
    }

    return true;
  }

function isGoodInteger(str)
  {
    var anum=/(^\d+$)/
    return anum.test(str);
  }
</script>

</head>

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

  IFRAMER_HEADER(L("lcl_job_frame_algemeen"), buttons);
%>
<div id="edit">
<form name="u2"
      action="job_edit_job_save.asp?job_key=<%=job_key%>&rtable=<%=rtable%>"
      method="post"
      target="hidFrameSubmit"
      onSubmit="job_submit();">
<%
BLOCK_START("jobJob", L("lcl_job_algemeen"));
if (job_key > -1)   {
  ROFIELDTR("fldshort", L("lcl_job_nr"), job_key);
}
  RWFIELDTR("act_descr",  "fld",          L("lcl_descr"),        act_descr, {maxlength: 30});

  %>
  <tr>
    <td class="label"><label><%=L("lcl_job_status")%></label></td>
    <td>
      <select type='text' id="status" name="status" onChange=''>
        <option value='1'><%=L("lcl_job_stat_1_inactief")%></option>
        <option value='2'><%=L("lcl_job_stat_2_actief")%></option>
        <option value='3'><%=L("lcl_job_stat_3_1xoverslaan")%></option>
      </select>
    </td>
  </tr>
  <%

  FCLTpersoonselector(
              "Person",
              "sgPerson",
            { perslidKey: person,
              label: L("lcl_requestor"),
              fieldNameKey: "person",
              required: true,
              readonly: false
            });
  %>
  <tr>
  <td class="label"><label><%=L("lcl_job_action")%></label></td>
  <td>
    <select type='text' id="action" name="action" onChange="changeAction();">
      <option value='1'><%=L("lcl_job_action_1")%></option>
      <option value='2'><%=L("lcl_job_action_2")%></option>
      <option value='3'><%=L("lcl_job_action_3")%></option>
    </select>
  </td>
  </tr>
  <%

  FCLTcalendar( "firstdate",
              { label : L("lcl_job_firstdate"),
                datum : objfirstdate,
                initEmpty: firstEmpty,
                volgnr: 1
               });

  RWFIELDTR("interval",  "fldshort",          L("lcl_job_period"),        interval, {maxlength: 10});

  FCLTcalendar( "enddate",
              { label : L("lcl_job_enddate"),
                datum : objenddate,
                initEmpty: endEmpty,
                volgnr: 2
               });

  RWFIELDTR("aantal",  "fldshort",          L("lcl_job_n_times"),        aantal, {maxlength: 10});

  if (job_key > -1)   {
    ROFIELDTR("fld", L("lcl_job_lastdate"), toDateTimeString(lastdate));
  }
  BLOCK_END();

if (rtable == 3  && job_key < 0)
{
BLOCK_START("jobMld", L("lcl_job_mld"));
  RWFIELDTR("job_mldnr",  "fldshort",          L("lcl_job_call_number"),   "", {required: true, datatype: "number", maxlength: 10});
BLOCK_END();
}
IFACE.FORM_END();
%>
</form>
<iframe src="../Shared/empty.html" name="hidFrameSubmit" style="display:none"></iframe>
</div>
</body>
</html>
<script>
  if (document.getElementById("status")) {
    document.getElementById("status").value = '<%=status%>';
  }

  if (document.getElementById("action")) {
    document.getElementById("action").value = '<%=action%>';
    changeAction();
  }
</script>
