winIEpass = ((navigator.appName.indexOf("Microsoft") != -1) &&
  (navigator.appVersion.indexOf("Windows") != -1)) &&
  (parseFloat(navigator.appVersion) >= 4) ? true : false;

NNpass = ((navigator.appName == "Netscape") &&
  (navigator.userAgent.indexOf("Mozilla") != -1) &&
  (parseFloat(navigator.appVersion) >= 4) &&
  (navigator.javaEnabled())) ? true : false;

  supportedBrowser = (winIEpass || NNpass) ? true : false;

//--- Standard object Prototypes ---------------------------------------------//
  String.prototype.trim = function()
  {
    return this.replace(/^\s+|\s+$/, '');
  };

  String.prototype.toInt = function()
  {
    return Math.round(this);
  };

  String.prototype.isValidInt = function()
  {
    
  };

  String.prototype.toFloat = function()
  {
    Value = Math.abs(this.replace(',', '.'));
    Value = parseFloat(Value);
    if (isNaN(Value))
      Value = 0;
    return Value;
  };

  String.prototype.format = function()
  {
    var str = this;
    for(var i=0;i<arguments.length;i++)
    {
      var re = new RegExp('\\{' + (i) + '\\}','gm');
      str = str.replace(re, arguments[i]);
    }
    return str;
  };  // a="De waarde is {0} en de 2e waarde is {1}".format(Waarde1, Waarde2);

  String.prototype.DecodeHTML = function()
  {
//    var str = unescape(this);
    var str = this;
  // String.fromCharCode(0x0022);
    str = str.replace(/&lt;/gi, "<");
    str = str.replace(/&gt;/gi, ">");
    //str = str.replace(/&euml;/gi, "ë");
    str = str.replace(/&amp;/gi, "&");
    return str;
  };

  Array.prototype.HasValue = function(Value)
  {
    return (this.Find(Value) > -1);
  };

  Array.prototype.Find = function(Value)
  {
    Result = -1;
    for (var i = 0; i < this.length; i++)
      if (this[i] == Value)
      {
        Result = i;
        break;
      }
    return Result;
  };
  
function Text2XML(XMLText)
{
  if (window.ActiveXObject)
  {
    // code voor Internet Explorer
    var xmlSelect = new ActiveXObject("Microsoft.XMLDOM");
    xmlSelect.async = "false";
    xmlSelect.loadXML(XMLText);
  }
  else
  {
    // code voor Mozilla, Firefox, Opera, etc.
    var parser = new DOMParser();
    var xmlSelect = parser.parseFromString(XMLText, "text/xml");
  }
  return xmlSelect;
}

function LargeImage(ImageName, Width, Height)
{
  OpenDialog(ImageName, Width, Height, this);
}

function tDialogResponse(Response)
{
  this.Response = Response;
}

function LeadingZero2(Nr)
{
	return (Nr < 10) ? "0" + Nr : Nr;
}

function MySQLDate2ddmmjjjj(MySQLDate)
{
  DateArray = MySQLDate.split(' ', 2);
  if (DateArray.length < 2)
    return '';
  Date = DateArray[0];
  DateArray = Date.split('-', 3);
  if (DateArray.length < 3)
    return '';
  Dag = LeadingZero2(DateArray[2].toInt());
  Maand = LeadingZero2(DateArray[1].toInt());
  Jaar = DateArray[0];
  return Dag + '-' + Maand + '-' + Jaar;
}

function DecimalCorrect(Value, Decimals)
{
  var strValue = Value + ' '; // Make string first
  strValue = strValue.trim();
  numValue = strValue.toFloat();
  strValue = numValue.toFixed(Decimals);
  return strValue.replace('.', ',');
}

function DecimalCorrectControl(FieldName, Decimals)
{
  var EditControl = document.getElementById(FieldName);
  var strValue = EditControl.value;
  var numValue = strValue.toFloat();
  strValue = numValue.toFixed(Decimals);
  EditControl.value = strValue.replace('.', ',');
}

function ValidInteger(edtName, Message)
{
  edtName = document.getElementById(edtName);
  NrValue = edtName.value;
  StringValid = NrValue.length > 0;
  for (var i=0; i < NrValue.length; i++)
    if ((NrValue.charCodeAt(i) < 48) || (NrValue.charCodeAt(i) > 57))
    {
      StringValid = false;
      break;
    }
  if (!StringValid)
    alert(Message);
  return StringValid;
}

function ValidFloat(edtName, Message)
{
  edtName = document.getElementById(edtName);
  var NrValue = edtName.value;
  NrValue = NrValue.replace(',','.');
  StringValid = NrValue.length > 0;
  PointFound = false;
  for (var i=0; i < NrValue.length; i++)
    if ((!PointFound) && (NrValue.charCodeAt(i) == 46))
      PointFound = true;
    else
      if ((NrValue.charCodeAt(i) < 48) || (NrValue.charCodeAt(i) > 57) )
      {
        StringValid = false;
        break;
      }
  if (!StringValid)
    alert(Message);
  return StringValid;
}


// Begin filterNodes
// filterNodes(o.node, {p: [], br: [], a: ['href']}); Laat allen P, BR en A nodes in element staan
function filterNodes(element, allow)
{
  // Recurse into child elements 
  Array.fromList(element.childNodes).forEach(
    function(child)
    { 
      if (child.nodeType===1)
      { 
        filterNodes(child, allow); 
    
        var tag= child.tagName.toLowerCase(); 
        if (tag in allow)
        { 
          // Remove unwanted attributes 
          Array.fromList(child.attributes).forEach(
            function(attr)
            { 
              if (allow[tag].indexOf(attr.name.toLowerCase())===-1) 
                child.removeAttributeNode(attr); 
            }); 
        }
        else
        { 
          // Replace unwanted elements with their contents 
          while (child.firstChild) 
            element.insertBefore(child.firstChild, child); 
          element.removeChild(child); 
        } 
      } 
    }); 
} 
 
// ECMAScript Fifth Edition (and JavaScript 1.6) array methods used by `filterNodes`. 
// Because not all browsers have these natively yet, bodge in support if missing. 
// 
if (!('indexOf' in Array.prototype))
{ 
  Array.prototype.indexOf= function(find, ix /*opt*/)
  { 
    for (var i= ix || 0, n= this.length; i<n; i++) 
      if (i in this && this[i]===find) 
        return i; 
    return -1; 
  }; 
}
 
if (!('forEach' in Array.prototype))
{ 
  Array.prototype.forEach= function(action, that /*opt*/)
  { 
    for (var i= 0, n= this.length; i<n; i++) 
      if (i in this) 
        action.call(that, this[i], i, this); 
  }; 
} 
 
// Utility function used by filterNodes. This is really just `Array.prototype.slice()` 
// except that the ECMAScript standard doesn't guarantee we're allowed to call that on 
// a host object like a DOM NodeList, boo. 
// 
Array.fromList= function(list)
{ 
  var array= new Array(list.length); 
  for (var i= 0, n= list.length; i<n; i++) 
    array[i]= list[i]; 
  return array; 
};
// END filterNodes


function swapNodes(item1,item2)
{
  // Omdat "HTMLElement.swapNode(HTMLElement2)" niet werkt onder Firefox
  var itemtmp = item1.cloneNode(1); // We need a clone of the node we want to swap
  var parent = item1.parentNode;// We also need the parentNode of the items we are going to swap.

  item2 = parent.replaceChild(itemtmp,item2); // First replace the second node with the copy of the first node which returns a the new node
  parent.replaceChild(item2,item1); //Then we need to replace the first node with the new second node
  
  // And finally replace the first item with it's copy so that we
  // still use the old nodes but in the new order. This is the reason
  // we don't need to update our Behaviours since we still have 
  // the same nodes.
  parent.replaceChild(item1,itemtmp);
  itemtmp = null; // Free up some memory, we don't want unused nodes in our document.
}

function RandomKey(Length)
{
  var Number ='';
  for(var i = 0; i < Length; i++)
    Number += Math.ceil(Math.random() * 10);
  return Number;
}

function flash_Get_PageVar(VarName)
{
  return document.getElementById('edt_' + VarName).value;
//  return PageVars[VarName];
}

function ShowImgTitle(e)
{
  if (window.event)
    e = window.event;
  var srcEl = e.srcElement ? e.srcElement : e.target;  // srcEl now can be used x-browser.
  alert(srcEl.title);
}

function SelectTable_MouseEvent(e)
{
  if (window.event)
    e = window.event;
  var srcEl = e.srcElement ? e.srcElement : e.target;  // srcEl now can be used x-browser.
  var obj_Row = srcEl;

  if (obj_Row.tagName != 'TR')
    obj_Row = obj_Row.parentNode;
  if (obj_Row.tagName != 'TR')
    obj_Row = obj_Row.parentNode;
  if (obj_Row.tagName != 'TR')
    obj_Row = obj_Row.parentNode;
  if (obj_Row.tagName != 'TR')
    obj_Row = obj_Row.parentNode;
  if (obj_Row.tagName == 'TR')
  {
    if (e.type == 'mouseover')
    {
      obj_Row.style.background = '#4444C9';
    }

    if (e.type == 'mouseout')
    {
      obj_Row.style.background = '#796eff';
    }

  }
}

// dHTMLWindow class -----------------------------------------------------------
function TdHTMLWindow(divName, iframeName)
{
  this.Div = document.getElementById(divName);
  this.Frame = document.getElementById(iframeName);
  this.Control_1 = '';
  this.Control_2 = '';
  this.Control_3 = '';
  this.Control_4 = '';
  this.Value_1 = '';
  this.Value_2 = '';
  this.Done = false;
  this.WindowName = '';
  this.Focus = function ()
  {
  }

  this.IsVisible = function ()
  {
    return (this.Div.style.visibility != 'hidden');
  }
  this.KeyEvent = function () {};
  this.Show = function()
  {
    this.Div.style.visibility = '';
  }

  this.Hide = function()
  {
    this.Div.style.visibility = 'hidden';
  }

  this.Load = function(URL, Left, Top, Width, Height)
  {
    this.Done = false;
    this.KeyEvent = function () {};
    this.Div.style.left = Left;
    this.Div.style.top = Top;
    this.Div.style.width = Width;
    this.Div.style.height = Height;
    this.Frame.style.width = Width;
    this.Frame.style.height = Height;
    this.Frame.src = URL;
  }
  this.Frame.ControlObject = this;
  return this;
}

function XMLhttp() {}

XMLhttp.create = function ()
{
  try
  {
    if (window.XMLHttpRequest)
      return new XMLHttpRequest();
    if(window.ActiveXObject)
      return new ActiveXObject(getXMLParser());
  }
  catch (ex) {};
  throw new Error("XMLHttp is not supported on this browser");
};

//--- Ajax request --------------------------------------------------------//
function GetAjax()
{
  var xhr; 
  try
  {
    xhr = new ActiveXObject('Msxml2.XMLHTTP');
  }
  catch (e) 
  {
    try 
    {
      xhr = new ActiveXObject('Microsoft.XMLHTTP');
    }
    catch (e2) 
		{
      try
      {
        xhr = new XMLHttpRequest();
      }
      catch (e3)
      {
        xhr = false;
      }
    }
  }
  return xhr;
}

// Dialogs ---------------------------------------------------------------------
function OpenDialog(FilePath, Width, Height, ParentObject)
{
  //var sFeatures= "dialogHeight: " + Height + "px; dialogWidth: " + Width + "px; edge: raised; " +
  //  "center: 1; help: 0; resizable: 0; status: 1;";
  //window.showModalDialog(FilePath, ParentObject, sFeatures);
  window.open(FilePath, "_blank", "width=" + Width + "px,height=" + Height + "px");
}

function OpenKalender(DatumVeld, KalenderKnop, Root)
{
  Left = document.getElementById(KalenderKnop).offsetLeft + 20;
  Top = document.getElementById(KalenderKnop).offsetTop;
//  Left = document.getElementById(KalenderKnop).offsetLeft - 160;
//  Top = document.getElementById(KalenderKnop).offsetTop + 20;

  if (oMain = document.getElementById('contentDiv'))
  {
    Left = Left + oMain.offsetLeft;
    Top = Top + oMain.offsetTop;
  }

  MydHTML.Control_1 = document.getElementById(DatumVeld);
  MydHTML.Control_2 = document.getElementById(KalenderKnop);
  MydHTML.Load(Root + '/_include/Kalender.php', Left, Top, 180, '');
  MydHTML.Show();
}

function DatumOnKeyPress(e)
{
  if (window.event)
    e = window.event;
  var KeyCode = e.keyCode ? e.keyCode : e.which;
  if ((KeyCode < 58) && (KeyCode > 47))
  {}  //  0-9
  else
    if (KeyCode != 45)
      if (e.keyCode)
        window.event.keyCode = 0;
      else
        e.which = 0;

  // TODO: Deze check zou ook tijdens invoer een correct datum formaat kunnen
  //   forceren. Dan zou ook CTRL-V en rechter muis click gefilterd moeten worden.
}

function IntOnKeyPress(e)
{
  var Keys = new Array(48,49,50,51,52,53,54,55,56,57);
  if (window.event)
    e = window.event;
  var KeyCode = e.keyCode ? e.keyCode : e.which;
  var NewKey = 0;
  for (var i=0; i < Keys.length; i++)
    if (KeyCode == Keys[i])
      NewKey = KeyCode;

  if (e.keyCode)
    window.event.keyCode = NewKey;
  else
    e.which = NewKey;
}

function FloatOnKeyPress(e)
{
  var Keys = new Array(48,49,50,51,52,53,54,55,56,57,46,44);
  if (window.event)
    e = window.event;
  var KeyCode = e.keyCode ? e.keyCode : e.which;
  var NewKey = 0;
  for (var i=0; i < Keys.length; i++)
    if (KeyCode == Keys[i])
      NewKey = KeyCode;

  if (NewKey == 46)
    NewKey = 44;
  
  if (e.keyCode)
    window.event.keyCode = NewKey;
  else
    e.which = NewKey;
}

function Datum_Ok(DatumTekst, EmptyOk)
{
  if (DatumTekst != '')
  {
    Result = false;
    try
    {
      DateArray = DatumTekst.split('-', 3);
      Dag = DateArray[0].toInt();
      Maand = DateArray[1].toInt();
      Jaar = DateArray[2].toInt();

  //  Controleer of datum geldig is minder dan 2209032000000 ms (70 jaar) voor
  //  of 4102488000000 ms (130 jaar) na 01-01-1970 (Unix timestamp) ligt.
      var MyDate = new Date();
      //alert('Date: ' + MyDate);
      MyDate.setFullYear(Jaar, Maand - 1, Dag);
      if (!isNaN(MyDate))
        if ( (MyDate.getTime() > - 2209032000000) && (MyDate.getTime() < 4102488000000) )
          if ( (MyDate.getFullYear() == Jaar) && (MyDate.getDate() == Dag) && (MyDate.getMonth() == (Maand - 1)) )
            Result = true;
    }
    catch(err) { }
    return Result;
  }
  else
    return EmptyOk;
}
