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

<%
var DOCTYPE_Disable = 1;
var FCLTEXPIRES = 8*60; // geen database interactie tenslotte
%>

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

<%
Response.ContentType = "text/javascript";
%>

/*
i volgnr
ktype 'D', 'C', 'N' etc.
klen lengte
kdec decimalen
kmin kmax
*/
function checkRequiredGroup(group)
{
  var requiredgroup = "required" + group;
  var groupisrequired = false;
  var rgroup = $("." + requiredgroup);

  for (var i = 0; i < rgroup.length; i++)
  {
    var rgvalue   = rgroup[i].value;
    var tagName   = rgroup[i].tagName;
    var tagType   = rgroup[i].type;
    var isbijlage = rgroup[i].getAttribute("isBijlage")==1;
    var bijlage   = rgroup[i].getAttribute("nBijlagen");
    var ischecked = rgroup[i].checked;

    var hasInput    = false;
    var hasSelect   = (tagName == "SELECT" && rgvalue > 0);
    var hasBijlage  = (tagName == "INPUT" && isbijlage && bijlage > 0);
    var hasCheckbox = false;
    if (tagType == "checkbox")
      hasCheckbox = (tagName == "INPUT" && tagType == "checkbox" && ischecked);
    else
      hasInput = (((tagName == "INPUT" || tagName == "TEXTAREA") && rgvalue != "") && !isbijlage);

    if (hasInput || hasSelect || hasBijlage || hasCheckbox)
    {
      groupisrequired = true;
      break;
    }
  }

  if (groupisrequired)
  {
    $("." + requiredgroup).addClass("required")
                          .attr("required", "required");
    for (var i = 0; i < rgroup.length; i++)
    { // Als checkbox dan toch verplicht is, vink maar aan.
      if (rgroup[i].type == "checkbox")
        rgroup[i].checked = true;
    }
  }
  else
  {
    $("." + requiredgroup).removeClass("required")
                          .removeAttr("required");
  }
}

function checkKenmerk(field, show, ktype, klen, kdec, kmin, kmax)
{
  var valid = true;
  var value = field.value;
  var err = "<%=L("lcl_mld_invalid_kenmerk_format")%>";

  if (value != '')
  {
    if (ktype == 'D')
    {
      $(field).change(cal_show_onChange);
      // Test of er nu een geldig datumformaat staat.
      var arr = /^(\d{1,2})[-](\d{1,2})[-](\d{1,4})$/.exec(value);
      if (arr)
      {
        var dt = new Date( arr[3], arr[2] -1, arr[1] );
        if ((arr[1] != dt.getDate()) ||
            (arr[2] != dt.getMonth() + 1) ||
            (arr[3] != dt.getYear() && arr[3] != dt.getFullYear()))
        {
          valid = false;
        }
      }
      else valid = false;
    }
    else if (ktype == 'C' || ktype == 'L' || ktype == 'Q')
    { // newlines verdubbelen naar %0D%0A dus aantal regels bijtellen
      if (value.length + (value.split("\n").length-1) > klen) valid = false;
    }
    else if (ktype == 'N')
    {
      if (kdec)
      {
        var l1 = klen-kdec;
        var l2 = kdec;
        // Als een punt én een komma in een getal behouden we alleen de laatste
        if (value.lastIndexOf(".") > value.lastIndexOf(","))
        {
            value = value.replace(/\,/ig, "");
            field.value = value;
        }
        if (value.lastIndexOf(",") > value.lastIndexOf("."))
        {
            value = value.replace(/\./ig, "");
            field.value = value;
        }
        var s = '^\\-?(\\d{1,'+l1+'}[.,]?|\\d{0,'+l1+'}[.,]\\d{1,'+l2+'})$';
        valid = value.match(new RegExp(s)) && valid;
        if (valid) { var t = parseFloat(value.replace(',','.'));}
      }
      else
      {
        if (klen == 1 && kmin == 0 && kmax == 1)
        {
          // Checkbox; waarde in checked is altijd true of false, dus altijd valid.
          valid = true;
          t = (field.checked?1:0);
        }
        else
        {
          var s = '^\\-?\\d{1,'+klen+'}[.,]?$';
          valid = value.match(new RegExp(s)) && valid;
          if (valid) { var t = parseInt(value);}
        }
      }
      if (valid)
      {
        if ((kmin !== null) && (t < kmin)) { valid = false; err = "<%=L("lcl_mld_kenmerk_out_range")%>"; }
        if ((kmax !== null) && (t > kmax)) { valid = false; err = "<%=L("lcl_mld_kenmerk_out_range")%>"; }
      }
    }
  }
  if (show && !valid)
  {
    alert(err);
    field.focus();
  }
  else if (!show && valid && field.className.match(/required\d*S\d*/g) && field.className.match(/required\d*S\d*/g).length > 0)
  { // Er is een required groep aanwezig.
    var requiredgroup = field.className.match(/required\d*S\d*/g)[0];
    var group = requiredgroup.substr(8);
    checkRequiredGroup(group);
  }

  return valid;
}

// Op een generieke manier documenten openen
// DWF bestanden worden eventueel met de SLNKDWF viewer geopend
// Poweruser IE feature: Ctrl ingedrukt opent DWF bestanden toch 'gewoon'
function openDoc(filepath)
{
  var fwnd;
<%if (S("view_dwf_with_slnkdwf"))
  { %>
    if (!(window.event && event.ctrlKey) && filepath.toUpperCase().match('.DWF'))
      fwnd = window.open("../SLNKDwf/ShowDWF.asp?relfile=" + filepath,'facDoc',"");
    else
<%} %>
      fwnd = window.open(filepath, 'facDoc', "");
  try { fwnd.focus(); } catch (e) { };
}

//Data komt terug van de closeModal
function bijlagen_callback(data, orgParams)
{
  if (orgParams.params.objButton)
  {
    if (orgParams.params.multi) // ktype == "M"
    {
      orgParams.params.objButton.value = L("lcl_appendixes_teller").format(data.cnt);
      orgParams.params.objButton.setAttribute("nBijlagen", data.cnt);
    }
    else
    {
      orgParams.params.objButton.nextSibling.setAttribute("nBijlagen", data.cnt);
      orgParams.params.objButton.value = data.fileName||"";
      $(orgParams.params.objButton).toggle(data.fileName != "");
      $(orgParams.params.objButton).next().toggle(data.fileName == ""); // de 'Add' knop
      if (data.fullPath) // handtekening
        $(orgParams.params.objButton).nextAll("img").show()[0].src = data.fullPath;
      else
        $(orgParams.params.objButton).nextAll("img").hide();
      FcltMgr.resized();
    }
    if (orgParams.params.objButton.onchange)
      orgParams.params.objButton.onchange(); // Omdat input veld readonly is wordt de onchange niet automatisch uitgevoerd.
  }

  if (orgParams.params.saveUrl)
  {
      $.post(orgParams.params.saveUrl, { flexVal: data.fileName }); // DIRECT opslaan in de database
  }
}
// encrypt geeft aan of bestand encrypted moet worden.
function onBijlagen(formurl, // protected
                    saveUrl, // protected
                    multi,
                    objButton)
{
  var params = { multi: multi, objButton: objButton };
  // Voor de zekerheid slaan we wijziging van de bestandsnaam direct op.
  // op het filesysteem is het namelijk ook al 'gecommit'

  params.saveUrl = saveUrl;
  FcltMgr.openModalDetail(formurl, L("lcl_appendixes"), {params: params, callback: bijlagen_callback, XXXXalwaysCallback: true } );
}

function xmlNodeDetails(key, xmlnode)
{
  var url = "";
  switch (xmlnode.toLowerCase())
  {
    case "afdeling"     :   url = "appl/prs/prs_afdeling.asp?key=" + key; break;
    case "afspraak"     :   url = "appl/bez/bez_afspraak.asp?afspr_key=" + key + "&urole=fo"; break;
    case "bedrijf"      :   url = "appl/prs/prs_bedrijf.asp?bedrijf_key=" + key; break;
    case "bestelling"   :   url = "appl/bes/bes_bestelling.asp?bes_key=" + key + "&urole=fo"; break;
    case "bestelopdr"   :   url = "appl/bes/bes_opdr.asp?ordernr=" + key + "&urole=fo"; break;
    case "contract"     :   url = "appl/cnt/cnt_contract.asp?cnt_key=" + key + "&urole=bo"; break;
    case "deel"         :   url = "appl/ins/ins_deel.asp?ins_key=" + key + "&urole=fe"; break;
    case "factuur"      :   url = "appl/fin/fin_factuur.asp?fin_key=" + key; break;
    case "gebouw"       :   url = "appl/alg/alg_gebouw.asp?key=" + key; break;
    case "locatie"      :   url = "appl/alg/alg_locatie.asp?key=" + key; break;
    case "melding"      :   url = "appl/mld/mld_melding.asp?mld_key=" + key + "&urole=fo"; break;
    case "message"      :   url = "appl/msg/msg_message.asp?message_key=" + key + "&urole=bo"; break;
    case "opdracht"     :   url = "appl/bez/mld_opdr.asp?opdr_key=" + key + "&urole=bo"; break;
    case "perslid"      :   url = "appl/prs/prs_perslid.asp?prs_key=" + key; break;
    case "reservering"  :   url = "appl/res/res_reservering.asp?rsv_ruimte_key=" + key + "&urole=fo"; break;
    case "xreservering" :   url = "appl/res/res_reservering.asp?reservering_key=" + key + "&urole=fo"; break;
    case "ruimte"       :   url = "appl/alg/alg_ruimte.asp?key=" + key; break;
    case "scenario"     :   url = "appl/prj/prj_scenario.asp?prj_key=" + key; break;
    case "verdieping"   :   url = "appl/alg/alg_verdieping.asp?key=" + key; break;
    default             :   break;
  }
  if (url != "")
    FcltMgr.openDetail(url, "<%=L("lcl_bes_srtprod")%>", {callback: FcltMgr.reload});
}