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

    File:           ins_xcp.asp
    Description:    Interval gegeven opgeven/aanpassen
    Parameters:     ins_keys of ins_key (geen verschil)

    Context:        Actie ins_inspect_list
    Note:           Resultaat van een submit is een json object

*/
%>
<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<!-- #include file="../Shared/kenmerk_common.inc" -->
<!-- #include file="../Shared/calendar.inc" -->
<!-- #include file="../Shared/save2db.inc" -->
<!-- #include file="../Shared/selector.inc" -->
<!-- #include file="../Shared/kostenplaatsselector.inc" -->
<!-- #include file="ins_flexkenmerksrtcontrole.inc" -->
<!-- #include file="ins.inc" -->

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

// Eigenlijk tot nu toe geen multi ins_key waarden. Slechts één ins_key waarden wordt gesubmit.
var ins_key = getQParamInt("ins_key");

var srtcont_key = getQParamInt("srtcont_key", -1); // voorgekozen inspectiesoort vanuit MJOB of Graphics thema.
var scen_key = getQParamInt("scen_key", 1); // Scenario.
var incbtw = getQParamInt("incbtw", 0); // Lijst inclusief BTW weergeven.
var deelsrtcont_key = getQParamInt("deelsrtcont_key", -1); // bestaande inspectie
var transitParam = buildTransitParam(["srtcont_key", "deelsrtcont_key"]);
var add = getQParamInt("add", 0) == 1; // Controle inspectie (ins_srtcontrole) waarvan de periode op 0 staat actief maken.
var ins_xcp_active_cost = S("ins_xcp_active_cost");

// Mag ik de exceptions (xcp) waarden van de taak wijzigen?
var this_ins = ins.func_enabled_deel(ins_key,
                                     { srtcont_key: srtcont_key,
                                       scen_key: scen_key
                                     });
user.anything_todo_or_abort((srtcont_key > 0 && this_ins.canShowXcp) || (srtcont_key < 0 && this_ins.hasAnyReadXcp));

var isMJOB = false;
var aantal = 0;
var srtdeel_eenheid;
var xcp_periode;
var xcp_eenheid;
var xcp_kp_key;
var xcp_bits;
var xcp_uren;
var xcp_materiaal;;
var xcp_kosten;
var xcp_kosten2;
var xcp_kosten3;
var xcp_perc;
var xcp_groep;
var xcp_startdatum = null;
var xcp_vervaldatum = null;
var xcp_opmerk;
var xcp_aantal;
var xcp_aanteh;
var xcp_key=-1;
var periodereartext = "";
var opmerk;
if (srtcont_key > 0)
{
  var sql = "SELECT dix.ctr_discipline_key"
          + "     , dix.ctr_ismjob"
          + "     , dix.ins_srtcontrole_eenheid"
          + "     , dix.ins_deel_aantal"
          + "     , isd.ins_srtdeel_eenheid"
          + "     , (SELECT " + S("prs_kpn_string")
          + "          FROM prs_kostenplaats k"
          + "         WHERE k.prs_kostenplaats_key = dix.prs_kostenplaats_key) kostenplaats"
          + "     , fbtw.fin_btwtabelwaarde_perc"
          + "  FROM ins_v_defined_inspect_xcp dix"
          + "     , ins_srtdeel isd"
          + "     , fin_btwtabelwaarde fbtw"
          + " WHERE dix.ins_srtdeel_key = isd.ins_srtdeel_key"
          + "   AND dix.fin_btwtabelwaarde_key = fbtw.fin_btwtabelwaarde_key(+)"
          + "   AND dix.ins_deel_key = " + ins_key
          + "   AND dix.ins_srtcontrole_key = " + srtcont_key
          + "   AND dix.ins_scenario_key = " + scen_key;
  var oRs = Oracle.Execute(sql);
  isMJOB = oRs("ctr_ismjob").Value == 1;
  xcp_eenheid = oRs("ins_srtcontrole_eenheid").Value;
  lcl.set_dialect(oRs("ctr_discipline_key").Value, "CTR_DISCIPLINE_KEY");

  var aantal = oRs("ins_deel_aantal").Value;
  var srtdeel_eenheid = oRs("ins_srtdeel_eenheid").Value;
  var kostenpl = oRs("kostenplaats").Value;
  var btw_perc = oRs("fin_btwtabelwaarde_perc").Value || 0;
  oRs.Close();

  switch (xcp_eenheid)
  {
    case -1: periodereartext = "";
             break;
    case 0:  periodereartext = L("lcl_ins_controle_period_rearhour");
             break;
    case 1:  periodereartext = L("lcl_ins_controle_period_rearday");
             break;
    case 2:  periodereartext = L("lcl_ins_controle_period_rearweek");
             break;
    case 3:  periodereartext = L("lcl_ins_controle_period_rearmonth");
             break;
    case 4:  periodereartext = L("lcl_ins_controle_period_rearyear");
             break;
  }

  var sql = "SELECT xcp.ins_srtcontroledl_xcp_periode"
          + "     , xcp.ins_srtcontroledl_xcp_eenheid"
          + "     , xcp.prs_kostenplaats_key"
          + "     , xcp.ins_srtcontroledl_xcp_bits"
          + "     , xcp.ins_srtcontroledl_xcp_uren"
          + "     , xcp.ins_srtcontroledl_xcp_materia"
          + "     , xcp.ins_srtcontroledl_xcp_kosten"
          + "     , xcp.ins_srtcontroledl_xcp_kosten2"
          + "     , xcp.ins_srtcontroledl_xcp_kosten3"
          + "     , xcp.ins_srtcontroledl_xcp_perc"
          + "     , xcp.ins_srtcontroledl_xcp_groep"
          + "     , xcp.ins_srtcontroledl_xcp_startdat"
          + "     , xcp.ins_srtcontroledl_xcp_eind"
          + "     , xcp.ins_srtcontroledl_xcp_opmerk"
          + "     , xcp.ins_srtcontroledl_xcp_aantal"
          + "     , xcp.ins_srtcontroledl_xcp_aanteh"
          + "     , xcp.ins_srtcontroledl_xcp_key"
          + "     , xcp.ins_scenario_key"
          + "     , isc.ins_srtcontrole_opmerking"
          + "  FROM ins_srtcontroledl_xcp xcp"
          + "     , ins_srtcontrole isc"
          + " WHERE xcp.ins_deel_key = " + ins_key
          + "   AND xcp.ins_srtcontrole_key = " + srtcont_key
          + "   AND xcp.ins_srtcontrole_key = isc.ins_srtcontrole_key"
          + "   AND xcp.ins_scenario_key = " + scen_key;
  var oRs = Oracle.Execute(sql);
  if (!oRs.eof)
  {
    xcp_periode = oRs("ins_srtcontroledl_xcp_periode").Value;
    xcp_eenheid = oRs("ins_srtcontroledl_xcp_eenheid").Value;
    xcp_kp_key = oRs("prs_kostenplaats_key").Value;
    xcp_bits = oRs("ins_srtcontroledl_xcp_bits").Value;
    xcp_uren = oRs("ins_srtcontroledl_xcp_uren").Value;
    xcp_materiaal = oRs("ins_srtcontroledl_xcp_materia").Value;
    xcp_kosten = oRs("ins_srtcontroledl_xcp_kosten").Value;
    xcp_kosten2 = oRs("ins_srtcontroledl_xcp_kosten2").Value;
    xcp_kosten3 = oRs("ins_srtcontroledl_xcp_kosten3").Value;
    xcp_perc = oRs("ins_srtcontroledl_xcp_perc").Value;
    xcp_groep = oRs("ins_srtcontroledl_xcp_groep").Value;
    xcp_startdatum = oRs("ins_srtcontroledl_xcp_startdat").Value != null? new Date(oRs("ins_srtcontroledl_xcp_startdat").Value) : null;
    xcp_vervaldatum = oRs("ins_srtcontroledl_xcp_eind").Value != null? new Date(oRs("ins_srtcontroledl_xcp_eind").Value) : null;
    xcp_opmerk = oRs("ins_srtcontroledl_xcp_opmerk").Value;
    xcp_aantal = oRs("ins_srtcontroledl_xcp_aantal").Value;
    xcp_aanteh = oRs("ins_srtcontroledl_xcp_aanteh").Value;
    xcp_key = oRs("ins_srtcontroledl_xcp_key").Value;
    opmerk = oRs("ins_srtcontrole_opmerking").Value;
  }
  oRs.Close();
}
%>

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

    <script type="text/javascript">
      var isMJOB = <%=isMJOB? 1 : 0 %> == 1;
      var ins_xcp_active_cost = <%=ins_xcp_active_cost%>;
      var incbtw = <%=incbtw%>;

      async function ins_submit()
      {
        if (!await validateForm("u2"))
          return false;

        // Als alle xcp waarden overeenkomen met de std waarden en groep en de datum zijn leeg dan kan het xcp record worden verwijderd.
        var periode_std = $("#sel_srtcontrole option:selected")[0].getAttribute("periode_std");
        var eenheid_std = $("#sel_srtcontrole option:selected")[0].getAttribute("eenheid_std");
        var kp_key_std = $("#sel_srtcontrole option:selected")[0].getAttribute("kp_key_std");
        var bits_std = $("#sel_srtcontrole option:selected")[0].getAttribute("bits_std");
        var kosten1_std = $("#sel_srtcontrole option:selected")[0].getAttribute("kosten1_std");
        var kosten2_std = $("#sel_srtcontrole option:selected")[0].getAttribute("kosten2_std");
        var kosten3_std = $("#sel_srtcontrole option:selected")[0].getAttribute("kosten3_std");
        var materiaal_std = $("#sel_srtcontrole option:selected")[0].getAttribute("materiaal_std");
        var uren_std = $("#sel_srtcontrole option:selected")[0].getAttribute("uren_std");
        var perc_std = $("#sel_srtcontrole option:selected")[0].getAttribute("perc_std");
        var groep_std = $("#sel_srtcontrole option:selected")[0].getAttribute("groep_std");
        var vervaldatum_std = $("#sel_srtcontrole option:selected")[0].getAttribute("vervaldatum_std");

        if (periode_std == $("#periode").val() &&
            eenheid_std == $("#eenheid").val() &&
            kp_key_std == $("#account").val() &&
            bits_std == $("#bits").val() &&
            kosten1_std == $("#kosten1").val() &&
            kosten2_std == $("#kosten2").val() &&
            kosten3_std == $("#kosten3").val() &&
            uren_std == $("#uren").val() &&
            materiaal_std == $("#materiaal").val() &&
            perc_std == $("#perc").val() &&
            groep_std == $("#groep").val() &&
            vervaldatum_std == $("#vervaldatum").val())
        {
          ins_delete();
          return true;
        }

        if (isMJOB)
        {
          // De disabled van de eenheid afhalen, anders wordt de waarde niet gesubmit.
          $("#eenheid").prop("disabled", false);
          $("#sel_srtcontrole").prop("disabled", false);
        }

        var data = $("[name=u2]").serialize();
        $.post($("form[name=u2]")[0].action,
               data,
               FcltCallbackClose,
               "json");
        return true; // disable submit.
      }

      function ins_delete()
      {
        var srtcont_key = $("#sel_srtcontrole").val();
        if (srtcont_key < 0)
        {
          FcltMgr.alert(L("lcl_ins_controle_srt_verplicht"));
          $("#sel_srtcontrole").addClass("missing");
          return false;
        }
        var data = { ins_key: <%=ins_key%>,
                     srtcont_key: srtcont_key };
        protectRequest.dataToken(data);
        $.post("ins_delete_xcp.asp",
               data,
               FcltCallbackClose,
               "json");
      }

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

      function onClickCbDay(pday)
      { // Minimaal 1 checkbox moet aangevinkt zijn. De laatste uit vinken kan niet.
        if (!document.getElementById("insInspZo").checked &&
            !document.getElementById("insInspMa").checked &&
            !document.getElementById("insInspDi").checked &&
            !document.getElementById("insInspWo").checked &&
            !document.getElementById("insInspDo").checked &&
            !document.getElementById("insInspVr").checked &&
            !document.getElementById("insInspZa").checked)
          $("#insInsp" + pday).prop('checked', true);
      }

      function onClickCbMnth(pmonth)
      { // Minimaal 1 checkbox moet aangevinkt zijn. De laatste uit vinken kan niet.
        if (!document.getElementById("insInspJan").checked &&
            !document.getElementById("insInspFeb").checked &&
            !document.getElementById("insInspMaa").checked &&
            !document.getElementById("insInspApr").checked &&
            !document.getElementById("insInspMei").checked &&
            !document.getElementById("insInspJun").checked &&
            !document.getElementById("insInspJul").checked &&
            !document.getElementById("insInspAug").checked &&
            !document.getElementById("insInspSep").checked &&
            !document.getElementById("insInspOkt").checked &&
            !document.getElementById("insInspNov").checked &&
            !document.getElementById("insInspDec").checked)
          $("#insInsp" + pmonth).prop('checked', true);
      }

      function onChangeEenheid()
      {
        var mode = $("#sel_srtcontrole option:selected")[0].getAttribute("modus");
        $("#mode").val(mode);

        var stdeenheid = parseInt(getSrtControleValue("eenheid_std"), 10);
        var xcpeenheid = parseInt(getSrtControleValue("eenheid_xcp"), 10);
        var eenheid = parseInt($("#eenheid").val(), 10);
        if (eenheid < 0)
          eenheid = stdeenheid;
        if (xcpeenheid < 0)
          xcpeenheid = stdeenheid;
        var bits = 0;
        if (eenheid == 1 || eenheid == 2 || eenheid == 3 || eenheid == 4)
        {
          var bits_std = $("#sel_srtcontrole option:selected")[0].getAttribute("bits_std");
          var bits_xcp = $("#sel_srtcontrole option:selected")[0].getAttribute("bits_xcp");
          if (eenheid == 1)
            bits = (bits_xcp >= 0? bits_xcp : bits_std);
          else
            bits = (bits_xcp > 0? bits_xcp : bits_std);
        }

        var eenheidChanged = false;
        if(xcpeenheid != eenheid)
          eenheidChanged = true;

        if ($("#sel_srtcontrole").val() > 0)
        {
          if (mode == 1)
          { // Interval modus (1)
            // Bits niet zichtbaar
            $(".trbits2").hide();
            $(".trbits3").hide();
            $(".trbits4").hide();
            //if (eenheid == -1)
            //{
            //  //$(".trperiode").hide();
            //  //$("#periode").removeClass("required");
            //}
            //else
            //{
            //  //$(".trperiode").show();
            //  //$("#periode").addClass("required");
            //}

            switch (eenheid)
            {
              case -1: $("#periodereartext").text("");
                       break;
              case 0:  $("#periodereartext").text(L("lcl_ins_controle_period_rearhour"));
                       break;
              case 1:  $("#periodereartext").text(L("lcl_ins_controle_period_rearday"));
                       break;
              case 2:  $("#periodereartext").text(L("lcl_ins_controle_period_rearweek"));
                       break;
              case 3:  $("#periodereartext").text(L("lcl_ins_controle_period_rearmonth"));
                       break;
              case 4:  $("#periodereartext").text(L("lcl_ins_controle_period_rearyear"));
                       break;
            }
          }
          else
          { // Moment modus (0)
            switch (eenheid)
            {
              case -1: //$(".trperiode").hide();
                       $(".trbits2").hide();
                       $(".trbits3").hide();
                       $(".trbits4").hide();
                       $(".trnotholiday").hide();
                       $("#periodereartext").text("");
                       break;
              case 0:  //$(".trperiode").show();
                       $(".trbits2").hide();
                       $(".trbits3").hide();
                       $(".trbits4").hide();
                       $(".trnotholiday").show();
                       $("#periodereartext").text(L("lcl_ins_controle_period_rearhour"));
                       $("#insInspNoHoli").prop('checked', (bits & 128) == 128);
                       break;
              case 1:  //$(".trperiode").show();
                       $(".trbits2").hide();
                       $(".trbits3").hide();
                       $(".trbits4").hide();
                       $(".trnotholiday").show();
                       $("#periodereartext").text(L("lcl_ins_controle_period_rearday"));
                       $("#insInspNoHoli").prop('checked', (bits & 128) == 128);
                       break;
              case 2:  //$(".trperiode").show();
                       $(".trbits2").show();
                       $(".trbits3").hide();
                       $(".trbits4").hide();
                       $(".trnotholiday").show();
                       $("#periodereartext").text(L("lcl_ins_controle_period_rearweekon"));
                       // Alle checkboxen de juiste waarde geven.
                       var dag_bits = (bits & 1) + (bits & 2) + (bits & 4) + (bits & 8) + (bits & 16) + (bits & 32) + (bits & 64);
                       if (dag_bits == 0 || eenheidChanged)
                       { // Er is geen dag gekozen. Dan maar vandaag kiezen.
                         var todaydow = new Date().getDay();
                         bits = Math.pow(2, todaydow - 1);
                       }
                       $("#insInspZo").prop('checked', (bits & 1) == 1);
                       $("#insInspMa").prop('checked', (bits & 2) == 2);
                       $("#insInspDi").prop('checked', (bits & 4) == 4);
                       $("#insInspWo").prop('checked', (bits & 8) == 8);
                       $("#insInspDo").prop('checked', (bits & 16) == 16);
                       $("#insInspVr").prop('checked', (bits & 32) == 32);
                       $("#insInspZa").prop('checked', (bits & 64) == 64);
                       $("#insInspNoHoli").prop('checked', (bits & 128) == 128);
                       break;
              case 3:  //$(".trperiode").show();
                       $(".trbits2").hide();
                       $(".trbits3").show();
                       $(".trbits4").hide();
                       $(".trnotholiday").show();
                       $("#periodereartext").text(L("lcl_ins_controle_period_rearmonthon"));
                       // De bits kan nog een waarde van weekelijks hebben als je eenheid van wekelijks naar maandelijks zet.
                       // Dan eerste dag aangeven.
                       var dag_bits = (bits & 1) || (bits & 2) || (bits & 4) || (bits & 8) || (bits & 16) || (bits & 32) || (bits & 64);
                       if (dag_bits == 0 || eenheidChanged)
                       { // Er is geen dag gekozen. Dan maar zondag kiezen.
                         var todaydow = new Date().getDay();
                         dag_bits = 1;
                       }
                       var week_bits = (bits & 256) / 256 * 1 + (bits & 512) / 512 * 2 + (bits & 1024) / 1024 * 4;
                       if ((week_bits < 1 || week_bits > 7) || eenheidChanged)
                       { // Er is geen week gekozen, dan maar de eerste kiezen.
                         var todaydate = new Date().getDate();
                         week_bits = 1;
                       }
                       week_bits = (week_bits & 1) * 256 + (week_bits & 2) * 256  + (week_bits & 4) * 256;
                       $("#dagbits").val(dag_bits);
                       $("#weekbits").val(week_bits);
                       $("#insInspNoHoli").prop('checked', (bits & 128) == 128);
                       break;
              case 4:  //$(".trperiode").show();
                       $(".trbits2").hide();
                       $(".trbits3").hide();
                       $(".trbits4").show();
                       $(".trnotholiday").hide();
                       // Als bits geen maand waarde heeft dan selecteer eerste maand.
                       var maandbits = bits;
                       if ((maandbits & 4095) == 0 || eenheidChanged) maandbits = 1;

                       $("#insInspJan").prop('checked', (maandbits & 1) == 1);
                       $("#insInspFeb").prop('checked', (maandbits & 2) == 2);
                       $("#insInspMaa").prop('checked', (maandbits & 4) == 4);
                       $("#insInspApr").prop('checked', (maandbits & 8) == 8);
                       $("#insInspMei").prop('checked', (maandbits & 16) == 16);
                       $("#insInspJun").prop('checked', (maandbits & 32) == 32);
                       $("#insInspJul").prop('checked', (maandbits & 64) == 64);
                       $("#insInspAug").prop('checked', (maandbits & 128) == 128);
                       $("#insInspSep").prop('checked', (maandbits & 256) == 256);
                       $("#insInspOkt").prop('checked', (maandbits & 512) == 512);
                       $("#insInspNov").prop('checked', (maandbits & 1024) == 1024);
                       $("#insInspDec").prop('checked', (maandbits & 2048) == 2048);

                       $("#periodereartext").text(L("lcl_ins_controle_period_rearyearin"));
                       break;
            }
          }
        }
        else
        {
          //$(".trperiode").hide();
          $(".trbits2").hide();
          $(".trbits3").hide();
          $(".trbits4").hide();
          $(".trnotholiday").hide();
        }

        FcltMgr.resized(window);
      }

      function getSrtControleValue(pval)
      {
        return $("#sel_srtcontrole option:selected")[0].getAttribute(pval);
      }

      function getSrtControleAmountValue(pval)
      {
        var lval = getSrtControleValue(pval);
        return lval === ""? "" : num2currEditable(parseFloat(lval.replace(',', '.')))
      }

      function getSrtControleEenheid(pval)
      {
        var lval = parseInt(getSrtControleValue(pval), 10);
        var rval = "";
        switch (lval)
        {
          case 0:  rval = L("lcl_ins_controle_uurlijks");
                   break;
          case 1:  rval = L("lcl_ins_controle_dagelijks");
                   break;
          case 2:  rval = L("lcl_ins_controle_wekelijks");
                   break;
          case 3:  rval = L("lcl_ins_controle_maandelijks");
                   break;
          case 4:  rval = L("lcl_ins_controle_jaarlijks");
                   break;
        }
        return rval;
      }

      function onChangeSrtControle()
      {
        isRunning = getSrtControleValue("lastactivectrstatus") == "2" || getSrtControleValue("lastactivectrstatus") == "3" || getSrtControleValue("lastactivectrstatus") == "5";
        var periodereartext = "";
        if (isRunning)
        {
          var eenheid_xcp = parseInt(getSrtControleValue("eenheid_xcp"), 10);
          var eenheid_std = parseInt(getSrtControleValue("eenheid_std"), 10);
          switch (eenheid_xcp >= 0? eenheid_xcp : eenheid_std)
          {
            case 0:  periodereartext = L("lcl_ins_controle_period_rearhour");
                     break;
            case 1:  periodereartext = L("lcl_ins_controle_period_rearday");
                     break;
            case 2:  periodereartext = L("lcl_ins_controle_period_rearweek");
                     break;
            case 3:  periodereartext = L("lcl_ins_controle_period_rearmonth");
                     break;
            case 4:  periodereartext = L("lcl_ins_controle_period_rearyear");
                     break;
          }
        }

        $("#periode").val(getSrtControleValue("periode_xcp"));
        $("#periode").attr("placeholder", getSrtControleValue("periode_std"));
        $("#periode").prop("title", getSrtControleValue("periode_std"));
        $("#stdperiode").text( getSrtControleValue("periode_std")? " [" + getSrtControleValue("periode_std") + (isRunning? " " + periodereartext : "") + "]" : "");
        if (isRunning)
        {
          $("#periode").prop("disabled", true);
          if (getSrtControleValue("periode_xcp") != "")
            $("li[id='btn_ins_delete']").hide(); // Verwijder "Verwijderen" button.
        }
        else
        {
          $("#periode").prop("disabled", false);
          $("li[id='btn_ins_delete']").show(); // Toon "Verwijderen" button.
        }

        $("#aantal").val(getSrtControleValue("aantal_xcp"));
        $("#aantal").attr("placeholder", getSrtControleValue("aantal_std"));
        $("#stdaantal").text( getSrtControleValue("aantal_std")? " [" + getSrtControleValue("aantal_std") + "]" : "");
        $("#aanteh").val(getSrtControleValue("aanteh_xcp"));
        $("#aanteh").attr("placeholder", getSrtControleValue("aanteh_std"));
        $("#stdaanteh").text( getSrtControleValue("aanteh_std")? " [" + getSrtControleValue("aanteh_std") + "]" : "");

        isMJOB = getSrtControleValue("ismjob") == 1;
        if (!isMJOB && !isRunning)
        {
          $("#eenheid").prop("disabled", false);
          $("#eenheid").closest("tr").show();
          $("#eenheid").val(getSrtControleValue("eenheid_xcp"));
          $("#stdeenheid").text( getSrtControleEenheid("eenheid_std")? " [" + getSrtControleEenheid("eenheid_std") + "]" : "");
        }
        else
        {
          $("#eenheid").val(-1); // Voor MJOB geldt dat de xcp eenheid niet aangepast is en daarom moet de lege waarde van de selectbox geselecteerd zijn met key "-1".
          $("#eenheid").prop("disabled", true);
          $("#eenheid").closest("tr").hide();
        }

        onChangeEenheid();

        $("#account").attr("placeholder", getSrtControleValue("kp_key_std"));
        $("#stdkp_key").text( getSrtControleValue("kp_oms_std")? " [" + getSrtControleValue("kp_oms_std") + "]" : "");
        if (getSrtControleAmountValue("kp_key_xcp"))
          sgAccount.setValue(getSrtControleAmountValue("kp_key_xcp"), getSrtControleValue("kp_oms_xcp"));

        $("#kosten1").val(getSrtControleAmountValue("kosten_xcp1"));
        $("#kosten1").attr("placeholder", getSrtControleValue("kosten_std1"));
        $("#stdkosten1").text( getSrtControleValue("kosten_std1")? " [" + getSrtControleValue("kosten_std1") + "]" : "");

        $("#kosten2").val(getSrtControleAmountValue("kosten_xcp2"));
        $("#kosten2").attr("placeholder", getSrtControleValue("kosten_std2"));
        $("#stdkosten2").text( getSrtControleValue("kosten_std2")? " [" + getSrtControleValue("kosten_std2") + "]" : "");

        $("#kosten3").val(getSrtControleAmountValue("kosten_xcp3"));
        $("#kosten3").attr("placeholder", getSrtControleValue("kosten_std3"));
        $("#stdkosten3").text( getSrtControleValue("kosten_std3")? " [" + getSrtControleValue("kosten_std3") + "]" : "");

        $("#uren").val(getSrtControleAmountValue("uren_xcp"));
        $("#uren").attr("placeholder", getSrtControleValue("uren_std"));
        $("#stduren").text( getSrtControleValue("uren_std")? " [" + getSrtControleValue("uren_std") + "]" : "");

        $("#materiaal").val(getSrtControleAmountValue("materiaal_xcp"));
        $("#materiaal").attr("placeholder", getSrtControleValue("materiaal_std"));
        $("#stdmateriaal").text( getSrtControleValue("materiaal_std")? " [" + getSrtControleValue("materiaal_std") + "]" : "");

        $("#perc").val(getSrtControleValue("perc_xcp"));
        $("#perc").attr("placeholder", getSrtControleValue("perc_std"));
        $("#stdperc").text( getSrtControleValue("perc_std")? " [" + getSrtControleValue("perc_std") + "]" : "");

        $("#groep").val(getSrtControleValue("groep_xcp"));
        $("#groep").attr("placeholder", getSrtControleValue("groep_std"));
        $("#stdgroep").text( getSrtControleValue("groep_std")? " [" + getSrtControleValue("groep_std") + "]" : "");

        var vervaldatum_std_dag = getSrtControleValue("eind_std_dag");
        var vervaldatum_std_maand = getSrtControleValue("eind_std_maand");
        var vervaldatum_std_jaar = getSrtControleValue("eind_std_jaar");
        var vervaldatum_xcp_dag = getSrtControleValue("eind_xcp_dag");
        var vervaldatum_xcp_maand = getSrtControleValue("eind_xcp_maand");
        var vervaldatum_xcp_jaar = getSrtControleValue("eind_xcp_jaar");
        var vervaldatum_std = (vervaldatum_std_dag === ""? "" : new Date(vervaldatum_std_jaar, (vervaldatum_std_maand - 1), vervaldatum_std_dag));
        var vervaldatum_xcp = (vervaldatum_xcp_dag === ""? "" : new Date(vervaldatum_xcp_jaar, (vervaldatum_xcp_maand - 1), vervaldatum_xcp_dag));
        // Zet de vervaldatum
        $("#show_vervaldatum").datepicker("setDate", (vervaldatum_xcp === ""? "" : vervaldatum_xcp));
        $("#stdvervaldatum").text(vervaldatum_std === ""? "" : " [" + vervaldatum_std.toDateString() + "]");
        $("#show_vervaldatum").attr("placeholder", vervaldatum_std === ""? "" : vervaldatum_std.toDateString());

        $("#opmerk").val(getSrtControleValue("opmerk_xcp"));
        // De standaard opmerking kan lang zijn en daarom niet tonen na het label.
        // Indien er een standaard opmerking aanwezig is, dan een * tonen met een title/popup op de *.
        $("#stdopmerk").text( getSrtControleValue("opmerk_std")? " [" + "*" + "]" : "");
        $("#stdopmerk").prop("title", getSrtControleValue("opmerk_std"));

        $("#xcp_key").val(getSrtControleValue("xcp_key"));

        // Zet de startdatum
        var startdatum_xcp_dag = getSrtControleValue("start_xcp_dag");
        var startdatum_xcp_maand = getSrtControleValue("start_xcp_maand");
        var startdatum_xcp_jaar = getSrtControleValue("start_xcp_jaar");
        var startdatum_xcp = (startdatum_xcp_dag === ""? "" : new Date(startdatum_xcp_jaar, (startdatum_xcp_maand - 1), startdatum_xcp_dag));
        // Waarde van de startdatum moet kleiner zijn als de eigenlijke nextdatum (1 dag eerder).
        var nextdate_dag = getSrtControleValue("nextdate_dag");
        var nextdate_maand = getSrtControleValue("nextdate_maand");
        var nextdate_jaar = getSrtControleValue("nextdate_jaar");
        var nextdate = new Date(nextdate_jaar, (nextdate_maand - 1), nextdate_dag - 1); // (1 dag eerder).
        // Als er al inspecties zijn geweest dan is de startdatum niet meer van belang. Dan de startdatum readonly maken/weergeven.
        var hascontroles = getSrtControleValue("hascontroles");
        var startdatum_ro = (hascontroles == 1) || (<%=(!this_ins.canChangeXcp && !this_ins.hasAnyWriteXcp)? 1 : 0%> == 1);
        var startdatumparams = { cal_id: "startdatum",
                                 datum: (startdatum_xcp === ""? "" : startdatum_xcp.getTime()),
                                 initEmpty: startdatum_xcp === ""? 1 : 0,
                                 readonly: startdatum_ro? 1 : 0};
        // Alleen als er een controle is geweest, moet er een maxDate meegegeven worden.
        if (!hascontroles)
          startdatumparams.maxDate = nextdate.getTime();
        // Deze functie onChangeSrtControle waarin we bevinden, wordt altijd in het begin aangeroepen dus deze parameters van de startdatum overrulen altijd de initiële waarden.
        $("#show_startdatum").closest("td").load("../Shared/loadCalendar.asp"
                                                + "?cal_id=" + startdatumparams.cal_id
                                                + "&datum=" + startdatumparams.datum
                                                + "&initEmpty=" + startdatumparams.initEmpty
                                                + "&readonly=" + startdatumparams.readonly
                                                + "&saveValue=" + (startdatumparams.readonly && startdatumparams.datum != null? 1 : 0) // Wel submitten als er een readonly waarde wordt getoond.
                                                );
        $("#account").text(getSrtControleValue("kostenplaats")? getSrtControleValue("kostenplaats") : "");
      }

      function calcKosten(number)
      {
        if (!$("#kosten" + number).length) // geen rechten of niet geconfigureerd
            return;

        var kosten = parseFloat($("#kosten" + number).val().replace(',', '.'));
        if ($("#kosten" + number).val() != '' && !isNaN(kosten))
        {
          $("#kosten" + number).val(num2currEditable(kosten));
          var paantal_std = <%=safe.jsfloat(aantal)%>;
          var paantal_xcp = parseFloat($("#aantal").val().replace(',', '.'));
          var paantal = paantal_xcp? paantal_xcp : paantal_std;
          if (paantal > 1)
          {
            var txt = "* " +  num2currEditable(paantal) + " = " + num2currEditable(paantal * kosten)
                    + (incbtw
                      ? " (" + num2currEditable(paantal * kosten * (1 + <%=safe.jsfloat(btw_perc)%>/100)) + " " + L('lcl_mjb_inclbtw') +")"
                      : "");
            $("#totkosten" + number).text(txt);
          }
        }
      }

      function calcUren()
      {
        if (!$("#uren").length) // geen rechten of niet geconfigureerd
            return;

        var uren = parseFloat($("#uren").val().replace(',', '.'));
        if ($("#uren").val() != '' && !isNaN(uren))
          $("#uren").val(num2currEditable(uren));
      }

      function calcMateriaal()
      {
        if (!$("#materiaal").length) // geen rechten of niet geconfigureerd
            return;

        var materiaal = parseFloat($("#materiaal").val().replace(',', '.'));
        if ($("#materiaal").val() != '' && !isNaN(materiaal))
        {
          $("#materiaal").val(num2currEditable(materiaal));
          <% if (aantal > 1) { %>
            var paantal_std = <%=safe.jsfloat(aantal)%>;
            var paantal_xcp = parseFloat($("#aantal").val().replace(',', '.'));
            var paantal = paantal_xcp? paantal_xcp : paantal_std;
            var txt = "* " +  num2currEditable(paantal) + " = " + num2currEditable(paantal * materiaal)
                    + (incbtw
                      ? " (" + num2currEditable(paantal * materiaal * (1 + <%=safe.jsfloat(btw_perc)%>/100)) + " " + L('lcl_mjb_inclbtw') +")"
                      : "");
            $("#totmatateriaal").text(txt);
          <% } %>
        }
        else
          $("#totmat").text("");
      }
      $(function () {
        calcMateriaal(); // initieel vullen
        calcKosten(1);
        calcKosten(2);
        calcKosten(3);
      });
    </script>
  </head>


  <body class="fclt-modal" id="mod_insxcp">
    <% MODAL_START();%>
    <form name="u2" action="ins_xcp_save.asp?ins_key=<%=ins_key%><%=transitParam%>" method="post">
    <% MODAL_BLOCK_START(); %>
      <input type="hidden" id="mode" name="mode" value="1">
      <input type="hidden" id="scen_key" name="scen_key" value="<%=scen_key%>">
      <input type="hidden" id="xcp_key" name="xcp_key" value="<%=xcp_key%>">
<%    sql = "SELECT ins_scenario_omschrijving"
          + "  FROM ins_scenario"
          + " WHERE ins_scenario_key = " + scen_key;
      var oRs = Oracle.Execute(sql); %>
      <tr><td><div class="<%=scen_key == 1? "fcltactsit" : "fcltscenario"%>"><%=(scen_key == 1? L("lcl_mjb_act_situation"): L("lcl_mjb_scenario") + " " + oRs("ins_scenario_omschrijving").Value) %></div></td></tr>
<%    oRs.Close();
      MODAL_BLOCK_END();
      MODAL_BLOCK_START("insXcp", L("lcl_ins_interval_kenmerken"), { icon: "fa-cog" })
        // mogelijke inspecties op dit object/ deze objecten
        var sql = "SELECT COUNT (DISTINCT ins_deel_key) aantal"
                + "  FROM ins_deel"
                + " WHERE ins_deel_key = " + ins_key;
        var oRs = Oracle.Execute(sql);
        var cntdeel = oRs("aantal").Value;
        oRs.Close();

        var sql = "SELECT *"
                + "  FROM ("
                // De view ins_v_defined_inspect_xcp levert de volgende regels op
                // Alle Active Situatie (AS) soort controle's die geen uitzondering (XCP) record hebben maar wel een scenario.
                // en
                // Alle soort controles van de scenario's.
                // en
                // Alle Active Situatie (AS) soort controle's die:
                //   a) geen uitzondering (XCP) record hebben en geen scenario.
                //   b) een uitzondering (XCP) record hebben.
                + "        SELECT di.ins_srtcontrole_key"
                + "             , " + lcl.xsqla('di.ins_srtcontrole_omschrijving', 'di.ins_srtcontrole_key')
                + "             , di.ins_srtcontrole_mode modus"
                + "             , di.ins_deel_aantal aantal_std"
                + "             , xcp.ins_srtcontroledl_xcp_aantal aantal_xcp"
                + "             , di.ins_srtdeel_eenheid aanteh_std"
                + "             , xcp.ins_srtcontroledl_xcp_aanteh aanteh_xcp"
                + "             , di.ins_srtcontrole_eenheid eenheid_std"
                + "             , COALESCE(xcp.ins_srtcontroledl_xcp_eenheid, -1) eenheid_xcp"
                + "             , di.ins_srtcontrole_periode periode_std"
                + "             , xcp.ins_srtcontroledl_xcp_periode periode_xcp"
                + "             , di.prs_kostenplaats_key kp_key_std"
                + "             , xcp.ins_srtcontroledl_xcp_kp_key kp_key_xcp"
                + "             , (SELECT " + S("prs_kpn_string")
                + "                  FROM prs_kostenplaats k"
                + "                 WHERE k.prs_kostenplaats_key = di.prs_kostenplaats_key) kp_oms_std"
                + "             , (SELECT " + S("prs_kpn_string")
                + "                  FROM prs_kostenplaats k"
                + "                 WHERE k.prs_kostenplaats_key = xcp.ins_srtcontroledl_xcp_kp_key) kp_oms_xcp"
                + "             , di.ins_srtcontrole_bits bits_std"
                + "             , xcp.ins_srtcontroledl_xcp_bits bits_xcp"
                + "             , di.ins_srtcontrole_groep groep_std"
                + "             , xcp.ins_srtcontroledl_xcp_groep groep_xcp"
                + "             , di.ins_srtcontrole_kosten kosten_std1"
                + "             , xcp.ins_srtcontroledl_xcp_kosten kosten_xcp1"
                + "             , di.ins_srtcontrole_kosten2 kosten_std2"
                + "             , xcp.ins_srtcontroledl_xcp_kosten2 kosten_xcp2"
                + "             , di.ins_srtcontrole_kosten3 kosten_std3"
                + "             , xcp.ins_srtcontroledl_xcp_kosten3 kosten_xcp3"
                + "             , di.ins_srtcontrole_uren uren_std"
                + "             , xcp.ins_srtcontroledl_xcp_uren uren_xcp"
                + "             , di.ins_srtcontrole_materiaal materiaal_std"
                + "             , xcp.ins_srtcontroledl_xcp_materia materiaal_xcp"
                + "             , di.ins_srtcontrole_percentage perc_std"
                + "             , xcp.ins_srtcontroledl_xcp_perc perc_xcp"
                + "             , di.ins_srtcontrole_opmerking opmerk_std"
                + "             , xcp.ins_srtcontroledl_xcp_opmerk opmerk_xcp"
                + "             , COALESCE(xcp.ins_srtcontroledl_xcp_key, -1) xcp_key"
                + "             , xcp.ins_srtcontroledl_xcp_startdat startdate_xcp"
                + "             , TO_CHAR(xcp.ins_srtcontroledl_xcp_startdat, 'DD') start_xcp_dag"
                + "             , TO_CHAR(xcp.ins_srtcontroledl_xcp_startdat, 'MM') start_xcp_maand"
                + "             , TO_CHAR(xcp.ins_srtcontroledl_xcp_startdat, 'YYYY') start_xcp_jaar"
                + "             , di.ins_srtcontrole_eind eind_std"
                + "             , TO_CHAR(di.ins_srtcontrole_eind, 'DD') eind_std_dag"
                + "             , TO_CHAR(di.ins_srtcontrole_eind, 'MM') eind_std_maand"
                + "             , TO_CHAR(di.ins_srtcontrole_eind, 'YYYY') eind_std_jaar"
                + "             , xcp.ins_srtcontroledl_xcp_eind eind_xcp"
                + "             , TO_CHAR(xcp.ins_srtcontroledl_xcp_eind, 'DD') eind_xcp_dag"
                + "             , TO_CHAR(xcp.ins_srtcontroledl_xcp_eind, 'MM') eind_xcp_maand"
                + "             , TO_CHAR(xcp.ins_srtcontroledl_xcp_eind, 'YYYY') eind_xcp_jaar"
                + "             , TO_CHAR(ins.nextcyclusdate (di.ins_deel_key, di.ins_srtcontrole_key, " + scen_key + ", 1), 'DD') nextdate_dag"
                + "             , TO_CHAR(ins.nextcyclusdate (di.ins_deel_key, di.ins_srtcontrole_key, " + scen_key + ", 1), 'MM') nextdate_maand"
                + "             , TO_CHAR(ins.nextcyclusdate (di.ins_deel_key, di.ins_srtcontrole_key, " + scen_key + ", 1), 'YYYY') nextdate_jaar"
                + "             , xcp.ctr_ismjob ismjob"
                + "             , CASE"
                + "               WHEN (SELECT COUNT(sdc.ins_deelsrtcontrole_key)"
                + "                       FROM ins_deelsrtcontrole sdc"
                + "                      WHERE sdc.ins_deel_key = di.ins_deel_key"
                + "                        AND sdc.ins_srtcontrole_key = di.ins_srtcontrole_key"
                + "                        AND sdc.ins_deelsrtcontrole_status = 6"
                + "                        AND sdc.ins_scenario_key = 1) > 0"
                + "               THEN 1"
                + "               ELSE 0"
                + "               END hascontroles"
                + "             , (SELECT " + S("prs_kpn_string")
                + "                  FROM prs_kostenplaats k"
                + "                 WHERE k.prs_kostenplaats_key = xcp.prs_kostenplaats_key) kostenplaats"
                + "             , COALESCE((SELECT MAX(id2.ins_deelsrtcontrole_status)"
                + "                           FROM ins_deelsrtcontrole id2"
                + "                          WHERE id2.ins_deel_key = di.ins_deel_key"
                + "                            AND id2.ins_srtcontrole_key = di.ins_srtcontrole_key"
                + "                            AND id2.ins_scenario_key = 1"
                + "                            AND id2.ins_deelsrtcontrole_status IN (2, 3, 5))"
                + "                      , (SELECT id.ins_deelsrtcontrole_status"
                + "                           FROM ins_deelsrtcontrole id"
                + "                          WHERE id.ins_deelsrtcontrole_key = (SELECT MAX(id2.ins_deelsrtcontrole_key)"
                + "                                                                FROM ins_deelsrtcontrole id2"
                + "                                                               WHERE id2.ins_deel_key = di.ins_deel_key"
                + "                                                                 AND id2.ins_srtcontrole_key = di.ins_srtcontrole_key"
                + "                                                                 AND id2.ins_scenario_key = 1))) lastactivectrstatus"
                + "          FROM ins_v_defined_inspect_xcp xcp"
                + "             , ins_v_defined_inspect di"
                + "         WHERE xcp.ins_srtcontrole_key = di.ins_srtcontrole_key"
                + "           AND xcp.ins_deel_key = di.ins_deel_key"
                + "           AND xcp.ins_deel_key = " + ins_key
                // De xcp_periode overruled de standaard periode. De xcp_periode kan/mag NULL zijn maar de standaard periode niet.
                // Mogelijke waarden van de standaard periode en xcp periode:
                // ins_srtcontrole_periode     ins_srtcontroledl_xcp_periode    uitleg
                //         0                                NULL                een inactieve soort controle die ook geldt voor het object
                //         1..n                             NULL                een actieve soort controle geldt ook voor het object.
                //         0                                1                   een standaard niet actieve controle is voor het object actief gemaakt.
                //         1..n                             0                   een standaard actieve controle is voor het object inactief gemaakt.
                + (add
                  ? "         AND COALESCE (xcp.ins_srtcontroledl_xcp_periode, di.ins_srtcontrole_periode) <= 0" // Inactieve soort controles tonen.
                    + "       AND xcp.ins_srtcontroledl_xcp_periode IS NULL"                                     // Soort controles die niet overruled zijn tonen.
                  : "         AND (COALESCE (xcp.ins_srtcontroledl_xcp_periode, di.ins_srtcontrole_periode) > 0" // Actieve soort controles tonen.
                    + "            OR ins_srtcontroledl_xcp_periode = 0)")                                       // Soort controles die inactief zijn gemaakt tonen.
                + (srtcont_key > 0
                  ? "         AND di.ins_srtcontrole_key = " + srtcont_key
                  : "")
                + "           AND di.ctr_discipline_key IN (SELECT w.ins_discipline_key"
                + "                                           FROM fac_v_webgebruiker w"
                + "                                              , fac_functie f"
                + "                                          WHERE w.fac_functie_key = f.fac_functie_key "
                + "                                            AND f.fac_functie_code = " + (scen_key == 1? "'WEB_CTRUSE'" : "'WEB_CTRSEN'")
                + "                                            AND w.fac_gebruiker_prs_level_write < 9"
                + "                                            AND w.fac_gebruiker_alg_level_write < 9"
                + "                                            AND w.prs_perslid_key = " + user_key + ")"
                + "         AND xcp.ins_scenario_key = " + scen_key
                + "        GROUP BY di.ins_srtcontrole_key"
                + "               , di.ins_srtcontrole_omschrijving"
                + "               , di.ins_srtcontrole_mode"
                + "               , di.ins_deel_aantal"
                + "               , xcp.ins_srtcontroledl_xcp_aantal"
                + "               , di.ins_srtdeel_eenheid"
                + "               , xcp.ins_srtcontroledl_xcp_aanteh"
                + "               , di.ins_srtcontrole_periode"
                + "               , di.ins_srtcontrole_eenheid"
                + "               , di.prs_kostenplaats_key"
                + "               , di.ins_srtcontrole_bits"
                + "               , di.ins_srtcontrole_groep"
                + "               , di.ins_srtcontrole_kosten"
                + "               , di.ins_srtcontrole_kosten2"
                + "               , di.ins_srtcontrole_kosten3"
                + "               , di.ins_srtcontrole_uren"
                + "               , di.ins_srtcontrole_materiaal"
                + "               , di.ins_srtcontrole_percentage"
                + "               , di.ins_srtcontrole_opmerking"
                + "               , di.ins_srtcontrole_eind"
                + "               , di.ins_deel_key"
                + "               , xcp.ins_srtcontroledl_xcp_key"
                + "               , xcp.ins_srtcontroledl_xcp_periode"
                + "               , xcp.ins_srtcontroledl_xcp_eenheid"
                + "               , xcp.ins_srtcontroledl_xcp_kp_key"
                + "               , xcp.ins_srtcontroledl_xcp_bits"
                + "               , xcp.ins_srtcontroledl_xcp_groep"
                + "               , xcp.ins_srtcontroledl_xcp_kosten"
                + "               , xcp.ins_srtcontroledl_xcp_kosten2"
                + "               , xcp.ins_srtcontroledl_xcp_kosten3"
                + "               , xcp.ins_srtcontroledl_xcp_uren"
                + "               , xcp.ins_srtcontroledl_xcp_materia"
                + "               , xcp.ins_srtcontroledl_xcp_perc"
                + "               , xcp.ins_srtcontroledl_xcp_opmerk"
                + "               , xcp.ins_srtcontroledl_xcp_startdat"
                + "               , xcp.ins_srtcontroledl_xcp_eind"
                + "               , xcp.ctr_ismjob"
                + "               , xcp.prs_kostenplaats_key"
                + "        HAVING COUNT(*) = " + cntdeel + ")" // Als multi dan alleen de inspecties die voor *alle* objecten gelden
                + " ORDER BY ins_srtcontrole_omschrijving";
        FCLTselector("sel_srtcontrole",
                     sql,
                     { label: L("lcl_ins_controle"),
                       initKey: srtcont_key,
                       emptyOption: L("lcl_select_srtcontrole"),
                       readonly: !this_ins.canChangeXcp && !this_ins.hasAnyWriteXcp,
                       disable: srtcont_key > 0,
                       suppressEmpty: !this_ins.canChangeXcp && !this_ins.hasAnyWriteXcp,
                       required: true,
                       onChange: "onChangeSrtControle()",
                       trclass: "mod_xcp",
                       extraParamValue: "modus, aantal_std, aantal_xcp, aanteh_std, aanteh_xcp, eenheid_std, eenheid_xcp"
                                      + ", periode_std, periode_xcp, kp_key_std, kp_key_xcp, kp_oms_std, kp_oms_xcp, bits_std, bits_xcp"
                                      + ", kosten_std1, kosten_xcp1, kosten_std2, kosten_xcp2, kosten_std3, kosten_xcp3"
                                      + ", uren_std, uren_xcp, materiaal_std, materiaal_xcp, perc_std, perc_xcp"
                                      + ", opmerk_std, opmerk_xcp, xcp_key, groep_std, groep_xcp"
                                      + ", start_xcp_dag, start_xcp_maand, start_xcp_jaar"
                                      + ", eind_std_dag, eind_xcp_dag, eind_std_maand, eind_xcp_maand, eind_std_jaar, eind_xcp_jaar"
                                      + ", nextdate_dag, nextdate_maand, nextdate_jaar"
                                      + ", ismjob, hascontroles, kostenplaats, lastactivectrstatus",
                       extraParamValueInit: "1, , , , , -1, -1"
                                          + ", , , , , , '', '', "
                                          + ", , , , , , "
                                          + ", , , , , , "
                                          + ", , , , , "
                                          + ", , , "
                                          + ", , , , , , "
                                          + ", , , "
                                          + ", , , , "
                     });

        sql = "SELECT 0, " + safe.quoted_sql(L("lcl_ins_controle_uurlijks")) + " FROM DUAL"
           + " UNION SELECT 1, " + safe.quoted_sql(L("lcl_ins_controle_dagelijks")) + " FROM DUAL"
           + " UNION SELECT 2, " + safe.quoted_sql(L("lcl_ins_controle_wekelijks")) + " FROM DUAL"
           + " UNION SELECT 3, " + safe.quoted_sql(L("lcl_ins_controle_maandelijks")) + " FROM DUAL"
           + " UNION SELECT 4, " + safe.quoted_sql(L("lcl_ins_controle_jaarlijks")) + " FROM DUAL";
        FCLTselector("eenheid",
                     sql,
                     { label: L("lcl_ins_controle_eenheid")  + " " + L("lcl_ins_controle").toLowerCase() + "<span class='stdmjb' id='stdeenheid'></span>",
                       emptyOption: isMJOB? null : "",
                       initKey: isMJOB? 4 : xcp_eenheid,
                       required: false,
                       readonly: !this_ins.canChangeXcp && !this_ins.hasAnyWriteXcp,
                       suppressEmpty: !this_ins.canChangeXcp && !this_ins.hasAnyWriteXcp,
                       onChange: "onChangeEenheid()",
                       trclass: "mod_xcp"
                     });
        RWFIELDTR("periode",
                  "fldshort",
                  L("lcl_ins_controle_period") + "<span class='stdmjb' id='stdperiode'></span>",
                  xcp_periode,
                  { datatype: "number",
                    //required: true,
                    readonly: !this_ins.canChangeXcp && !this_ins.hasAnyWriteXcp,
                    suppressEmpty: !this_ins.canChangeXcp && !this_ins.hasAnyWriteXcp,
                    fronttext: L("lcl_ins_controle_period_fronttext"),
                    reartext: "<span id='periodereartext'>" + periodereartext + "</span>",
                    trclass: "trperiode",
                    maxlength: 3
                  });
        if (!isMJOB)
        {
%>
          <tr class="trbits2">
            <td class="label">
            </td>
            <td>
              <input type="checkbox" class="fldcheck" name="insInspZo" id="insInspZo" onClick="onClickCbDay('Zo')" value="1" <%=this_ins.canChangeXcp || this_ins.hasAnyWriteXcp? "" : " readonly"%>><label for="insInspZo"><%=safe.html(calendar_names.days[0])%></label>
              <br>
              <input type="checkbox" class="fldcheck" name="insInspMa" id="insInspMa" onClick="onClickCbDay('Ma')" value="1" <%=this_ins.canChangeXcp || this_ins.hasAnyWriteXcp? "" : " readonly"%>><label for="insInspMa"><%=safe.html(calendar_names.days[1])%></label>
              <br>
              <input type="checkbox" class="fldcheck" name="insInspDi" id="insInspDi" onClick="onClickCbDay('Di')" value="1" <%=this_ins.canChangeXcp || this_ins.hasAnyWriteXcp? "" : " readonly"%>><label for="insInspDi"><%=safe.html(calendar_names.days[2])%></label>
              <br>
              <input type="checkbox" class="fldcheck" name="insInspWo" id="insInspWo" onClick="onClickCbDay('Wo')" value="1" <%=this_ins.canChangeXcp || this_ins.hasAnyWriteXcp? "" : " readonly"%>><label for="insInspWo"><%=safe.html(calendar_names.days[3])%></label>
              <br>
              <input type="checkbox" class="fldcheck" name="insInspDo" id="insInspDo" onClick="onClickCbDay('Do')" value="1" <%=this_ins.canChangeXcp || this_ins.hasAnyWriteXcp? "" : " readonly"%>><label for="insInspDo"><%=safe.html(calendar_names.days[4])%></label>
              <br>
              <input type="checkbox" class="fldcheck" name="insInspVr" id="insInspVr" onClick="onClickCbDay('Vr')" value="1" <%=this_ins.canChangeXcp || this_ins.hasAnyWriteXcp? "" : " readonly"%>><label for="insInspVr"><%=safe.html(calendar_names.days[5])%></label>
              <br>
              <input type="checkbox" class="fldcheck" name="insInspZa" id="insInspZa" onClick="onClickCbDay('Za')" value="1" <%=this_ins.canChangeXcp || this_ins.hasAnyWriteXcp? "" : " readonly"%>><label for="insInspZa"><%=safe.html(calendar_names.days[6])%></label>
            </td>
          </tr>
          <tr class="trbits3">
            <td>
            </td>
            <td>
<%
            sql = "SELECT 256, " + safe.quoted_sql(L("lcl_ins_controle_eerste")) + ", 1 FROM DUAL"
                + " UNION SELECT 512, " + safe.quoted_sql(L("lcl_ins_controle_tweede")) + ", 2 FROM DUAL"
                + " UNION SELECT 768, " + safe.quoted_sql(L("lcl_ins_controle_derde")) + ", 3 FROM DUAL"
                + " UNION SELECT 1024, " + safe.quoted_sql(L("lcl_ins_controle_vierde")) + ", 4 FROM DUAL"
                + " UNION SELECT 1280, " + safe.quoted_sql(L("lcl_ins_controle_vijfde")) + ", 5 FROM DUAL"
                + " UNION SELECT 1536, " + safe.quoted_sql(L("lcl_ins_controle_eennalaatste")) + ", 6 FROM DUAL"
                + " UNION SELECT 1792, " + safe.quoted_sql(L("lcl_ins_controle_laatste")) + ", 7 FROM DUAL"
                + " ORDER BY 1";
            FCLTselector("weekbits",
                         sql,
                         { initKey: 1,
                           required: false,
                           readonly: !this_ins.canChangeXcp && !this_ins.hasAnyWriteXcp,
                           suppressEmpty: !this_ins.canChangeXcp && !this_ins.hasAnyWriteXcp,
                           html: " width='100px'"
                         });
            sql = "";
            for (var i = 0; i <= 6; i++)
            {
              sql += (i > 0? " UNION " : "") + "SELECT " + Math.pow(2, (i )) + ", " + safe.quoted_sql(calendar_names.days[i]) + " FROM DUAL"
            }
            sql += " ORDER BY 1";
            FCLTselector("dagbits",
                         sql,
                         { initKey: 1,
                           required: false,
                           readonly: !this_ins.canChangeXcp && !this_ins.hasAnyWriteXcp,
                           suppressEmpty: !this_ins.canChangeXcp && !this_ins.hasAnyWriteXcp
                         });
%>
            <%=L("lcl_ins_controle_period_ofthemonth")%></td>
          </tr>
          <tr class="trbits4">
            <td>
            </td>
            <td>
              <input type="checkbox" class="fldcheck" name="insInspJan" id="insInspJan" onClick="onClickCbMnth('Jan')" value="1" <%=(xcp_bits & 1) == 1? "CHECKED" : ""%>><label for="insInspJan"><%=safe.html(calendar_names.months[0])%></label>
              <br>
              <input type="checkbox" class="fldcheck" name="insInspFeb" id="insInspFeb" onClick="onClickCbMnth('Feb')" value="1" <%=(xcp_bits & 2) == 2? "CHECKED" : ""%>><label for="insInspFeb"><%=safe.html(calendar_names.months[1])%></label>
              <br>
              <input type="checkbox" class="fldcheck" name="insInspMaa" id="insInspMaa" onClick="onClickCbMnth('Maa')" value="1" <%=(xcp_bits & 4) == 4? "CHECKED" : ""%>><label for="insInspMaa"><%=safe.html(calendar_names.months[2])%></label>
              <br>
              <input type="checkbox" class="fldcheck" name="insInspApr" id="insInspApr" onClick="onClickCbMnth('Apr')" value="1" <%=(xcp_bits & 8) == 8? "CHECKED" : ""%>><label for="insInspApr"><%=safe.html(calendar_names.months[3])%></label>
              <br>
              <input type="checkbox" class="fldcheck" name="insInspMei" id="insInspMei" onClick="onClickCbMnth('Mei')" value="1" <%=(xcp_bits & 16) == 16? "CHECKED" : ""%>><label for="insInspMei"><%=safe.html(calendar_names.months[4])%></label>
              <br>
              <input type="checkbox" class="fldcheck" name="insInspJun" id="insInspJun" onClick="onClickCbMnth('Jun')" value="1" <%=(xcp_bits & 32) == 32? "CHECKED" : ""%>><label for="insInspJun"><%=safe.html(calendar_names.months[5])%></label>
              <br>
              <input type="checkbox" class="fldcheck" name="insInspJul" id="insInspJul" onClick="onClickCbMnth('Jul')" value="1" <%=(xcp_bits & 64) == 64? "CHECKED" : ""%>><label for="insInspJul"><%=safe.html(calendar_names.months[6])%></label>
              <br>
              <input type="checkbox" class="fldcheck" name="insInspAug" id="insInspAug" onClick="onClickCbMnth('Aug')" value="1" <%=(xcp_bits & 128) == 128? "CHECKED" : ""%>><label for="insInspAug"><%=safe.html(calendar_names.months[7])%></label>
              <br>
              <input type="checkbox" class="fldcheck" name="insInspSep" id="insInspSep" onClick="onClickCbMnth('Sep')" value="1" <%=(xcp_bits & 256) == 256? "CHECKED" : ""%>><label for="insInspSep"><%=safe.html(calendar_names.months[8])%></label>
              <br>
              <input type="checkbox" class="fldcheck" name="insInspOkt" id="insInspOkt" onClick="onClickCbMnth('Okt')" value="1" <%=(xcp_bits & 512) == 512? "CHECKED" : ""%>><label for="insInspOkt"><%=safe.html(calendar_names.months[9])%></label>
              <br>
              <input type="checkbox" class="fldcheck" name="insInspNov" id="insInspNov" onClick="onClickCbMnth('Nov')" value="1" <%=(xcp_bits & 1024) == 1024? "CHECKED" : ""%>><label for="insInspNov"><%=safe.html(calendar_names.months[10])%></label>
              <br>
              <input type="checkbox" class="fldcheck" name="insInspDec" id="insInspDec" onClick="onClickCbMnth('Dec')" value="1" <%=(xcp_bits & 2048) == 2048? "CHECKED" : ""%>><label for="insInspDec"><%=safe.html(calendar_names.months[11])%></label>
              <br>
            </td>
          </tr>
          <tr class="trnotholiday">
            <td class="label">
            </td>
            <td>
              <input type="checkbox" class="fldcheck" name="insInspNoHoli" id="insInspNoHoli" value="1" <%=this_ins.canChangeXcp || this_ins.hasAnyWriteXcp? "" : " readonly"%>><label for="insInspNoHoli"><%=L("lcl_ins_controle_notholiday")%></label>
            </td>
          </tr>
<%      } %>
<%
        RWFIELDTR("aantal",
                  "fldshort",
                  L("lcl_amount") + "<span class='stdmjb' id='stdaantal'></span>",
                  xcp_aantal,
                  { datatype: "currency",
                    html: "onBlur='calcKosten(1);calcKosten(2);calcKosten(3);'",
                    readonly: !this_ins.canChangeXcp && !this_ins.hasAnyWriteXcp,
                    suppressEmpty: !this_ins.canChangeXcp && !this_ins.hasAnyWriteXcp
                  });

        RWFIELDTR("aanteh",
                  "fldshort",
                  L("ins_srtcontrole_eenheid") + " " + L("lcl_amount").toLowerCase() + "<span class='stdmjb' id='stdaanteh'></span>",
                  xcp_aanteh,
                  { datatype: "text",
                    readonly: !this_ins.canChangeXcp && !this_ins.hasAnyWriteXcp,
                    suppressEmpty: !this_ins.canChangeXcp && !this_ins.hasAnyWriteXcp
                  });

        FCLTkostenplaatsselector("account",
                                 "sgAccount",
                                 user_key,
                                 { kostenplaatsKey: xcp_kp_key,
                                   label: L("lcl_prs_kosten") + "<span class='stdmjb' id='stdkp_key'>123</span>",
                                   showCustomLabel: true,
                                   filtercode: "A",
                                   whenEmpty: ""  //liefst kp_oms_std maar dat vind ik te moeilijk
                                 });

        if (ins_xcp_active_cost & 1)
          RWFIELDTR("kosten1",
                    "fldshort",
                    L("ins_srtcontrole_kosten") + "&nbsp;(" + (S("currency_pref")).replace(/^\s+|\s+$/gm,'') + ")<span class='stdmjb' id='stdkosten1'></span>",
                    xcp_kosten,
                    { datatype: "currency",
                      html: "onBlur='calcKosten(1)'",
                      readonly: !this_ins.canChangeXcp && !this_ins.hasAnyWriteXcp,
                      suppressEmpty: !this_ins.canChangeXcp && !this_ins.hasAnyWriteXcp,
                      reartext: "&nbsp;<span id='totkosten1'></span>"
                    });
        if (ins_xcp_active_cost & 2)
          RWFIELDTR("kosten2",
                    "fldshort",
                    L("ins_srtcontrole_kosten2") + "&nbsp;(" + S("currency_pref").replace(/^\s+|\s+$/gm,'') + ")<span class='stdmjb' id='stdkosten2'></span>",
                    xcp_kosten2,
                    { datatype: "currency",
                      html: "onBlur='calcKosten(2)'",
                      readonly: !this_ins.canChangeXcp && !this_ins.hasAnyWriteXcp,
                      suppressEmpty: !this_ins.canChangeXcp && !this_ins.hasAnyWriteXcp,
                      reartext: "&nbsp;<span id='totkosten2'></span>"
                    });
        if (ins_xcp_active_cost & 4)
          RWFIELDTR("kosten3",
                    "fldshort",
                    L("ins_srtcontrole_kosten3") + "&nbsp;(" + S("currency_pref").replace(/^\s+|\s+$/gm,'') + ")<span class='stdmjb' id='stdkosten3'></span>",
                    xcp_kosten3,
                    { datatype: "currency",
                      html: "onBlur='calcKosten(3)'",
                      readonly: !this_ins.canChangeXcp && !this_ins.hasAnyWriteXcp,
                      suppressEmpty: !this_ins.canChangeXcp && !this_ins.hasAnyWriteXcp,
                      reartext: "&nbsp;<span id='totkosten3'></span>"
                    });
        if (ins_xcp_active_cost & 8)
          RWFIELDTR("materiaal",
                    "fldshort",
                    L("ins_srtcontrole_materiaal") + "&nbsp;(" + S("currency_pref").replace(/^\s+|\s+$/gm,'') + ")<span class='stdmjb' id='stdmateriaal'></span>",
                    xcp_materiaal,
                    { datatype: "currency",
                      html: "onBlur='calcMateriaal()'",
                      readonly: !this_ins.canChangeXcp && !this_ins.hasAnyWriteXcp,
                      suppressEmpty: !this_ins.canChangeXcp && !this_ins.hasAnyWriteXcp,
                      reartext: "&nbsp;<span id='totmatateriaal'></span>"
                    });
        RWFIELDTR("uren",
                  "fldshort",
                  L("ins_srtcontrole_uren") + "<span class='stdmjb' id='stduren'></span>",
                  xcp_uren,
                  { datatype: "currency",
                    html: "onBlur='calcUren()'",
                    readonly: !this_ins.canChangeXcp && !this_ins.hasAnyWriteXcp,
                    suppressEmpty: !this_ins.canChangeXcp && !this_ins.hasAnyWriteXcp
                  });
        RWFIELDTR("perc",
                  "fldshort",
                  L("ins_srtcontrole_percentage") + "<span class='stdmjb' id='stdperc'></span>",
                  xcp_perc,
                  { datatype: "number",
                    readonly: !this_ins.canChangeXcp && !this_ins.hasAnyWriteXcp,
                    suppressEmpty: !this_ins.canChangeXcp && !this_ins.hasAnyWriteXcp
                  });
        //RWFIELDTR("bits", "fldshort trbits3", L("lcl_ins_controle_bits"), "", {trclass: "trbits", datatype: "number" });
        RWFIELDTR("groep",
                  "fld",
                  L("lcl_ins_controle_groep") + "<span class='stdmjb' id='stdgroep'></span>",
                  xcp_groep,
                  { datatype: "text",
                    readonly: !this_ins.canChangeXcp && !this_ins.hasAnyWriteXcp,
                    suppressEmpty: !this_ins.canChangeXcp && !this_ins.hasAnyWriteXcp
                  });
        FCLTcalendar("startdatum",
                     { datum: xcp_startdatum,
                       label    : L("lcl_ins_startdatum"),
                       initEmpty: xcp_startdatum == null,
                       readonly: !this_ins.canChangeXcp && !this_ins.hasAnyWriteXcp,
                       suppressEmpty: !this_ins.canChangeXcp && !this_ins.hasAnyWriteXcp
                     });
        FCLTcalendar("vervaldatum",
                     { datum: xcp_vervaldatum,
                       label    : L("lcl_ins_vervaldatum") + "<span class='stdmjb' id='stdvervaldatum'></span>",
                       initEmpty: xcp_vervaldatum == null,
                       readonly: !this_ins.canChangeXcp && !this_ins.hasAnyWriteXcp,
                       suppressEmpty: !this_ins.canChangeXcp && !this_ins.hasAnyWriteXcp
                     });
        RWTEXTAREATR("opmerk",
                     "fldtxt",
                     L("ins_srtcontrole_opmerking") + "<span class='stdmjb' id='stdopmerk' title='" + safe.htmlattr(opmerk) + "'></span>",
                     xcp_opmerk,
                     { readonly: !this_ins.canChangeXcp && !this_ins.hasAnyWriteXcp,
                       suppressEmpty: !this_ins.canChangeXcp && !this_ins.hasAnyWriteXcp
                     });
      MODAL_BLOCK_END();

      var buttons =  [];
      if (this_ins.canChangeXcp || this_ins.hasAnyWriteXcp)
      {
        buttons.push({id:"btn_ins_submit", title: L("lcl_submit"), action:"ins_submit()", importance: 1 , icon: "fa-fclt-save"});
        buttons.push({id:"btn_ins_delete", title: L("lcl_delete"), action:"ins_delete()", importance: 1, icon: "fa-trash-alt"});
      }
      buttons.push({id:"btn_ins_cancel", title: L("lcl_cancel"),  action:"ins_cancel()", importance: 3 , icon: "fa-fclt-cancel"});
      SIMPLE_BLOCK_START();
        CreateButtons(buttons);
      SIMPLE_BLOCK_END();
      IFACE.FORM_END();
%>
        <script type="text/javascript">
          $(function()
          {
<%          if (this_ins.canChangeXcp || this_ins.hasAnyWriteXcp)
            { %>
              onChangeSrtControle();
<%          } %>

            //$(function() { var tooltips = $("[title]").tooltip({ position: { my: "left top",
            //                                                                 at: "right+5 top-5",
            //                                                                 collision: "none"}
            //                                                   });
            //             });
          });
        </script>
    </form>
    <% MODAL_END(); %>
  </body>
</html>
<% ASPPAGE_END(); %>
