/*
     $Revision$
     $Id$
*/

var changed_bits = 0;
var changed_mode = 0;

$(function ()
{   // initialisatie
    $("#inspectionmode").change(change_mode);
    $("#unit").change(change_unit);
    $("#_moment").click(SelectBits);
    $("#taskdiscipline").change(change_ctr_discipline);

    change_ctr_discipline(true); // initieel goed zetten.
});

function change_mode()
{
    var controlemode = $("#inspectionmode").val() == 0;
    if (!controlemode)
        $("#_moment").closest("tr").hide()
    else
        $("#_moment").closest("tr").show();
    changed_mode = 1;
}

function change_unit()
{
    changed_mode = 1;
}

function change_controlemode(params)
{
  $("#controlemode").closest("td").load("../Shared/loadControlemode.asp",
                                        params
                                       );

}

function callback_ctr_info(data, textStatus)
{
    if (data.ismjob)
    {
      $("#unit").val(4); // Jaarlijks.
      $("#unit").prop('disabled', true);
      $("#inspectionmode").val(1); // Interval
      $("#inspectionmode").prop('disabled', true);
    }
    else
    {
      $("#unit").prop('disabled', false);
      $("#inspectionmode").prop('disabled', false);
    }
    change_mode();
    var params = {controle_type: data.controle_type};
    console.log(data);
    if (data.init) {
      params.initKey = $("#controlemode").val();
    }
    change_controlemode(params);
}

function change_ctr_discipline(init)
{
    var ctr_disc = $("#taskdiscipline").val();
    if (ctr_disc > 0)
    {
        $.getJSON("../Shared/get_ctr_info.asp",
                  { ctr_disc: ctr_disc},
                    function (data, textStatus)
                        {
                            data.init = init;
                            callback_ctr_info(data, textStatus);
                            if (init)
                                changed_mode = 0; // Zojuist op 1 gezet, moet na initialisatie 0 zijn.
                        });
    }
}

function DateChangedEnddate()
{
  if ($("#show_enddate").is(":checkbox"))
    var isgevuld = $("#show_enddate").is(":checked")
  else
    var isgevuld = $("#show_enddate").val();
  $("input[type=checkbox][name=scf_multi_enddate]").prop("checked", isgevuld);
}

async function SelectBits()
{
    if (!await validateForm("u2", { checkOnly: ["unit"] }))
        return false;

    var proturl = "../ins/ins_bitsform.asp"
                + "?eenheid=" + $("#unit").val()
                + "&bits="    + $("#bits").val();
    parent.FcltMgr.openModalDetail(proturl, L("ins_srtcontrole_moment"),
                { callback: function (data)
                        { if (data.eenheid >= 0)
                          {
                             $("#bits").val(data.bits);
                             changed_bits = 1;
                          }
                        }
                } );
}

function pre_submit()
{
  var periode = $("#period").val();
  if (periode < 0 || periode > 999)
  {
    FcltMgr.alert("Invalid number for " + L("lcl_ins_controle_period") + " (" + periode + ")");
    return false;
  }

  var ch_mode = (changed_mode == 1);
  var ch_bits = (changed_bits == 1);
  var controlemode  = $("#inspectionmode").val() == 0;
  if (controlemode)
  {
    if (ch_mode && !ch_bits)
    {
      FcltMgr.alert(L("ins_srtcontrole_select"));
      return false;
    }
    var lunit = $("#unit").val();
    var lperiod = $("#period").val();
    if (lunit > 0 && (parseFloat(lperiod.replace(",", ".")) % 1) > 0)
    {
      FcltMgr.alert(L("ins_srtcontrole_period_int"));
      return false;
    }
  }
  else // is_interval
  {
    $("#bits").val(null);
  }

  if ($("#objecttype").val() != -1)
  {
    $("#level").val("S");
    $("#objectkey").val($("#objecttype").val());
  }
  else if ($("#objectgroup").val() != -1)
  {
    $("#level").val("G");
    $("#objectkey").val($("#objectgroup").val());
  }
  else
  {
    $("#level").val("D");
    $("#objectkey").val($("#objectdiscipline").val());
  }

  // Eenheid voor het opslaan uit de disable mode.
  $("#unit").attr('disabled', false);
  $("#inspectionmode").prop('disabled', false);

  return true;
}

var srtContKeyArray = [];
var mjbAantal;
function priceChangeCallback(json, taskdiscipline, objectdiscipline, objectgroup, objecttype)
{
  json.costs = json.costs || 0;
  json.costs2 = json.costs2 || 0;
  json.costs3 = json.costs3 || 0;
  json.material = json.material || 0;
  json.adjust_xcp = json.adjust_xcp || 0;

  if (json.costs != 0 || json.costs2 != 0 || json.costs3 != 0 || json.material != 0)
  {
    var data = { ctrdisc_key: taskdiscipline,
                 disc_key: objectdiscipline,
                 srtgroup_key : objectgroup,
                 srtdeel_key: objecttype,
                 costs: json.costs,
                 costs2: json.costs2,
                 costs3: json.costs3,
                 material: json.material,
                 adjust_xcp: json.adjust_xcp
               };

    protectRequest.dataToken(data);
    $.post("../ins/ins_srtcontrole_pc_save.asp",
           data,
           FcltCallbackRefresh,
           "json");
  }
}

function ins_pricechange(taskdiscipline, objectdiscipline, objectgroup, objecttype)
{
  var url = "../ins/ins_pricechange.asp?";
  FcltMgr.openModalDetail(url,
                          L("lcl_mjb_price_change"),
                          { callback: function (json)
                                      {
                                        priceChangeCallback(json, taskdiscipline, objectdiscipline, objectgroup, objecttype);
                                      }
                          }
                         );
}
