/*
     $Revision$
     $Id$

     File:      deelwerk_edit.js
*/

function updateStopPlace(pautfunction, pstopplace)
{
  var lplace_key = $('#place').val();
  $("#stopplace").load("../deelwerk/load_stopplace.asp",
                       { autfunction: pautfunction,
                         place_key: lplace_key,
                         emptyOption: "",
                         stopplace: (pstopplace? pstopplace : "")}, // srtgroupChanged
                       function(){});
}

// Onchange urenveldje
function updateDag(veld)
{
  if (!sanitizeHours(veld))
  {
      veld.focus();
      veld.select();
      return false;
  }
  anyChange = true;
  var tr = $(veld).closest("tr")[0]
  var som = 0;
  $("input", tr).each(function()
                      {
                        if (this.id == "fldmldurentotal") // som-veld
                        {
                          this.value = decimalToHour(som);
                          return false; // stop de loop
                        }
                        else
                          som += hourToDecimal(this.value);
                      });

  var mld_opdr_key = -1;
  var mld_impropdr_key = -1;
  if (tr.id.substr(0,1) == "O")
    mld_opdr_key = parseInt(tr.id.substr(1));
  else
    mld_impropdr_key = parseInt(tr.id.substr(1));

  var uren = hourToDecimal(veld.value);
  // Kleur/stijl aanpassen bij afwijkingen
  if ($("#fldmldurentotal", tr).val() == $("#fldmldurenplan", tr).val())
    $("#fldmldurentotal", tr).removeClass("afwijk");
  else
    $("#fldmldurentotal", tr).addClass("afwijk");
}

function onChangeWeekdatum()
{
  var week_date = parseInt($("#deelwerkdate").val(), 10);
  var weekdate = new Date(week_date);
  var weeknr = getWeeknumber(weekdate);
  var jaar = weekdate.getYear();
  $("#weeknummer").val(jaar + "-" + weeknr);
  if (todayweek - weeknr > mld_deelwerk_wijzig)
  {
    // hide deeluren (visibility i.p.v. display i.v.m. FcltMgr.resized() en het niet meer zichtbaar zijn van de gehele kalender)
    $("#deeluren")[0].style.visibility = "hidden";
    $("#deelmat")[0].style.display = "none";
    $("#btnAddWeeklist")[0].style.visibility = "hidden";
  }
  else
  {
    // visible deeluren (visibility i.p.v. display i.v.m. FcltMgr.resized() en het niet meer zichtbaar zijn van de gehele kalender)
    $("#deeluren")[0].style.visibility = "visible";
    $("#deelmat")[0].style.display = "block";
    $("#btnAddWeeklist")[0].style.visibility = "visible";
  }

  var firstDowNew = YWDtoDate(jaar, weeknr, 1); // Get first day of week

  // Verander alle gegevens indien de week is veranderd.
  if (firstDow.getTime() != firstDowNew.getTime())
  {
    firstDow = firstDowNew;
    reloadSPlaceActlist(firstDow);
  }

  FcltMgr.resized();
}

function makeMaterialKeyString(matArray)
{
  var matkeystr = "";
  if (matArray)
  {
    for (i = 0; i < matArray.length; i++)
    {
      if (matArray[i].amount > 0)
        matkeystr += (matkeystr == ""? "" : ",") + matArray[i].key;
    }
  }
  return matkeystr;
}

function deelwerk_object_callback(data)
{
  // Lijst met materialen weergeven
  $("#materialen").html(data.matHtml);
  // String met keys in hidden input opslaan
  // String met keys in hidden input opslaan
  var matkeystr = makeMaterialKeyString(data.matExistArray);
  if (matkeystr == "")
  {
    matkeystr = -1;
  }
  $("#matkeystr").val(matkeystr);
  FcltMgr.resized();
}

function deelwerk_object()
{
  var url = "deelwerk_material.asp";

  var matkeystr = $("#matkeystr").val();
  var matamountstr = "";
  if (matkeystr == "")
  {
    matkeystr = -1;
    matamountstr = -1;
  }
  else
  {
    var materialkeystr = matkeystr.split(",");
    for (var i = 0; i < materialkeystr.length; i++)
    {
      matamountstr += (matamountstr != ""? "," : "") + $("#amount" + materialkeystr[i]).val();
    }
  }
  url += "?matkeystr=" + matkeystr;
  url += "&matamountstr=" + matamountstr;
  if (url.length > 2083)
  { // maximum lengte van url is 2083 karakters (maximum path lengte is 2048 karakters).
    alert(L("lcl_mld_max_material"));
    return;
  }

  FcltMgr.openModalDetail(url, L("lcl_mld_materialen"), {width: 700, obj_html: matkeystr, callback: deelwerk_object_callback } );
}

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

function onChangeAmount()
{
  var valid = true;
  var field = window.event.srcElement;
  var value = field.value;
  if (value == "")
  {
    field.value = "0";
    return true;
  }
  var err = "Ongeldig formaat ";
  if (value != "")
  {
    valid = checkExpr(value);
  }
  if (!valid)
    alert(err);
  //else
  field.focus();
  return valid;
}

function checkKey()
{
  kc = window.event.keyCode;
  if (kc < 48 || kc > 57)
    event.returnValue = false;
}

function checkExpr(value)
{
  var valid = true;
  var arr = /^(\d{1,4})$/.exec(value);
  if (!(arr))
    valid = false;
  return(valid);
}

function checkInputNumbers() {
  var valid = true;
  var field = window.event.srcElement;
  var value = field.value;
  if (value == '') {
    if (field == document.u2.week) {
      d = new Date(); //today's date
      field.value = getWeeknumber(d);
    }
    else if ((field == document.u2.hours) || (field == document.u2.minutes)) // hours and minutes
      field.value = '00';
    else // km
      field.value = '0';
    return true;
  }
  var err = "Ongeldig formaat ";
  if( value != '' )
  {
    valid = checkExpr(value);
  }
  if (!valid) alert(err);
  if (field == document.u2.km) field.value = parseInt(field.value, 10);  // voorloopnullen eventueel verwijderen.
  return valid;
}

function onChangePerson() {
  reloadSPlaceActlist(firstDow);
}

function reloadSPlaceActlist(firstDow)
{
  var prs_key = $("#person").val();
  parent.window.frames.weekletterListFrame.location.href = "deelwerk_list.asp?urole=" + urole + "&prs_key=" + prs_key + "&startday=" + firstDow.getTime();
}

function deelwerk_submit()
{
  document.activeElement.blur(); // trigger laatste onChanges

  // Standaard checks op verplichte velden, datum formaat, numeriek en float formaat, currency formaat.
  if (!validateForm("u2"))
    return false;

  document.forms.u2.submit();
  return true;
}

