var userAgent   		= window.navigator.userAgent.toLowerCase();
var msie,netscape,netscape4,osWindows,osMacOS;
var timerId     		= null;
var DUTCH       		= 1;
var ENGLISH     		= 2;
var locale      		= DUTCH;
var DATE_SEPARATOR 	= '-';

if (userAgent.indexOf('msie') != -1)
{
  msie      = true;
  netscape  = false;
  netscape4 = false;
}
else if (userAgent.indexOf('mozilla') != -1)
{
  msie      = false;
  netscape  = true;
  netscape4 = (document.layers)?true:false;
}
else
{
  msie      = false;
  netscape  = false;
  netscape4 = false;
}
if (userAgent.indexOf('windows') != -1)
{
  osWindows = true;
  osMacOS   = false;
}
else if (userAgent.indexOf('mac') != -1)
{
  osWindows = false;
  osMacOS   = true;
}
else
{
  osWindows = false;
  osMacOS   = false;
}
function processFullForm (jForm)
{
  var pars = {};
 
  jForm.find('INPUT,SELECT,TEXTAREA').each(
    function ()
    {
      var value;
 
      if (this.type == 'checkbox')
      {
        if (this.checked)
          value = this.value;
        else
          value = '';
      }
      else
        value = $(this).val();
 
      if (typeof(pars[this.name]) != 'undefined')
      {
        if (value != '')
          pars[this.name] += ',' + value;
      }
      else
        pars[this.name] = value;
    }
  );
 
  return(pars);
}

function getAbsolutePos (o,stopAtId)
{
  if (o == window)
    return({x:0,y:0});
 
  var x = o.offsetLeft - document.body.offsetLeft;
  var y = o.offsetTop - document.body.offsetTop;
 
  while (o.offsetParent)
  {
    o = o.offsetParent;
 
    if (stopAtId && (o.id == stopAtId))
      break;
 
    x += o.offsetLeft;
    y += o.offsetTop;
  }
 
  return({x:x,y:y});
}

function getObjectRef (id)
{
  if (this.getElementById)
    return(this.getElementById(id));
  else if (this.all)
    return(this.all(id));
  else
    return(null);
}
document.getObjectRef = getObjectRef;

function getFormField (form,id)
{
  var fields  = form.elements;
  var nFields = fields.length;
  var i;

  for (i = 0;i < nFields;i++)
  {
    field = fields.item(i);

    if ((field.id == id) || (field.name == id))
      return(field);
  }

  return(null);
}

function getScrollX ()
{
  var scrollX;

  if (this.documentElement && this.documentElement.scrollLeft)
    scrollX = this.documentElement.scrollLeft;
  else if (this.body.scrollLeft)
    scrollX = this.body.scrollLeft;
  else
    scrollX = window.scrollX;

  if (!scrollX)
    scrollX = 0;

  return(scrollX);
}
document.getScrollX = getScrollX;

function getScrollY ()
{
  var scrollY;

  if (this.documentElement && this.documentElement.scrollTop)
    scrollY = this.documentElement.scrollTop;
  else if (this.body.scrollTop)
    scrollY = this.body.scrollTop;
  else
    scrollY = window.scrollY;

  if (!scrollY)
    scrollY = 0;

  return(scrollY);
}
document.getScrollY = getScrollY;

function getRelativePosX (o)
{
  if (o == window)
    return(0);
  else
    return(o.offsetLeft);
}

function getRelativePosY (o)
{
  if (o == window)
    return(0);
  else
    return(o.offsetTop);
}

function getPosX (o)
{
//  alert("p=" + o + ";o=" + o.offsetLeft);

  if (o == window)
    return(0);
  else if (o.offsetParent)
    return(getPosX(o.offsetParent) + o.offsetLeft);
  else
    return(o.offsetLeft);
}

function getPosY (o)
{
  if (o == window)
    return(0);
  else if (o.offsetParent)
    return(getPosY(o.offsetParent) + o.offsetTop);
  else
    return(o.offsetTop);
}

function getWidth (o)
{
  return(o.offsetWidth);
}

function getHeight (o)
{
  return(o.offsetHeight);
}

function setPosX (o,x)
{
  var p = o;

  while (p.offsetParent)
  {
    p = p.offsetParent;
    if (p.style.position && (p.style.position == "absolute"))
      break;
  }

  if (p.style.position && (p.style.position == "absolute"))
    x -= getPosX(p);

  o.style.left = String(x) + "px";
}

function setPosY (o,y)
{
  var p = o;

  while (p.offsetParent)
  {
    p = p.offsetParent;
    if (p.style.position && (p.style.position == "absolute"))
      break;
  }

  if (p.style.position && (p.style.position == "absolute"))
    y -= getPosY(p);

  o.style.top = String(y) + "px";
}

function getMouseX (e)
{
  var x = 0;

  if (e.pageX)
  {
    x = e.pageX - document.getScrollX();
  }
  else if (e.clientX)
  {
    x = e.clientX;
  }

  return(x);
}

function getMouseY (e)
{
  var y = 0;

  if (e.pageY)
  {
    y = e.pageY - document.getScrollY();
  }
  else if (e.clientY)
  {
    y = e.clientY;
  }

  return(y);
}

function setWidth (o,w)
{
  o.style.width = w + "px";
}

function setHeight (o,h)
{
  o.style.height  = h + "px";
}

function lTrim ()
{
  return(this.replace(/^\s+/,''));
}

function rTrim()
{
  return(this.replace(/\s+$/,''));
}

function trim ()
{
  return(this.replace(/^\s+/,'').replace(/\s+$/,''));
}

function javaString ()
{
  return(this.replace(/\'/g,"\\\'").replace(/\\/g,"\\\\"));
}

function urlEncode ()
{
  return(escape(this).replace(/\+/g,"%2B"));
}

function htmlTextEncode ()
{
  var chars   = new Array("\"");
  var nChars  = chars.length;
  var codes   = new Array("&quot;");
  var s       = "";
  var i,
      k;
  var c;

  for (i = 0;i < this.length;i++)
  {
    c = this.charAt(i);

    for (k = 0;k < nChars;k++)
    {
      if (c == chars[k])
      {
        s += codes[k];
        break;
      }
    }

    if (k == nChars)
      s += c;
  }

  return(s);
}

function htmlEncode ()
{
  var chars   = new Array("\"","<",">","&");
  var nChars  = chars.length;
  var codes   = new Array("&quot;","&lt;","&gt;","&amp;");
  var s       = "";
  var i,
      k;
  var c;

  for (i = 0;i < this.length;i++)
  {
    c = this.charAt(i);

    for (k = 0;k < nChars;k++)
    {
      if (c == chars[k])
      {
        s += codes[k];
        break;
      }
    }

    if (k == nChars)
      s += c;
  }

  return(s);
}

function appendPar (par)
{
  if (this.indexOf("?") == -1)
    return(this + "?" + par);
  else
    return(this + "&" + par);
}

function javaURL ()
{
  return(this.urlEncode().javaString());
}

function unformatCurrency ()
{
  if (locale == DUTCH)
    return(Number(this.replace(/\./g,"").replace(",",".")));
  else
    return(Number(this.replace(/\,/g,"")));
}

function intValue ()
{
  return(parseInt(this));
}

function doubleValue ()
{
  return(parseFloat(this));
}

String.prototype.lTrim            = lTrim;
String.prototype.rTrim            = rTrim;
String.prototype.trim             = trim;
String.prototype.javaString       = javaString;
String.prototype.urlEncode        = urlEncode;
String.prototype.htmlTextEncode   = htmlTextEncode;
String.prototype.htmlEncode       = htmlEncode;
String.prototype.appendPar        = appendPar;
String.prototype.javaURL          = javaURL;
String.prototype.unformatCurrency = unformatCurrency;
String.prototype.intValue         = intValue;
String.prototype.doubleValue      = doubleValue;

function formatCurrency ()
{
  var i = Math.floor(this);
  var f = Math.round((this - i) * 100);

  if (f == 100)
  {
    i++;
    f = "00";
  }
  else if (f < 10)
    f = "0" + f;

  if (locale == DUTCH)
    return(i + "," + f);
  else
    return(i + "." + f);
}

Number.prototype.formatCurrency = formatCurrency;

function popupRect (width,height)
{
  var left  = 50;
  var top   = 50;

  if (screen)
  {
    left  = (screen.availWidth - width) / 2;
    top   = (screen.availHeight - height) / 2;
  }

  return('left=' + left + ',top=' + top + ',width=' + width + ',height=' + height);
}

function nop ()
{
}

function isLeapYear (year)
{
  if ((year % 4) == 0)
  {
    if ((year % 100) == 0)
      return((year % 400) == 0);
    else
      return(true);
  }
  else
    return(false);
}

function popupWindow (url,w,h)
{
  if (w == null)
    w = 400;
  if (h == null)
    h = 300;

  window.open(url,"_blank",popupRect(w,h) +
    ",directories=0,fullscreen=0,location=0,menubar=0,resizable=1,scrollbars=1,toolbar=0",false);
}

function insertFlash (directory,id,fileName,width,height,version,pars)
{
  document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"\n' +
    '  codebase="https://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' + version + ',0,0,0"\n' +
    '  width="' + width + '"\n' +
    '  height="' + height + '"\n' +
    '  id="' + id + '"\n' +
    '  align="">\n' +
    '  <param name="movie" value="/' + directory + '/' + fileName + '.swf">\n' +
    '  <param name="flashVars" value="' + pars + '">\n' +
    '  <param name="quality" value="high">\n' +
    '  <param name="scale" value="stretch">\n' +
    '  <param name="bgcolor" value="#ffffff">\n' +
    '  <param name="wmode" value="transparent">\n' +
    '  <param name="allowScriptAccess" value="always">\n' +
    '  <param name="swLiveConnect" value="true">\n' +
    '  <embed src="/' + directory + '/' + fileName + '.swf?' + pars + '"\n' +
    '    flashVars="' + pars + '"\n' +
    '    quality="high"\n' +
    '    scale="stretch"\n' +
    '    bgcolor="#ffffff"\n' +
    '    width="' + width + '"\n' +
    '    height="' + height + '"\n' +
    '    name="' + id + '"\n' +
    '    align=""\n' +
    '    wmode="transparent"\n' +
    '    allowScriptAccess="always"\n' +
    '    swLiveConnect="true"\n' +
    '    type="application/x-shockwave-flash"\n' +
    '    pluginspage="http://www.macromedia.com/go/getflashplayer">\n' +
    '</object>');
}

function validEMail (eMail)
{
  if (eMail == "")
    return(false);

  return(/^\S+@\S+\.[a-zA-Z]{2,}$/.test(eMail));
}

function showDIV (id)
{
  var oDIV  = this.getObjectRef(id);

  oDIV.style.display    = "block";
  oDIV.style.visibility = "visible";
}
document.showDIV  = showDIV;

function showDIVAt (id,x,y)
{
  var oDIV  = this.getObjectRef(id);

  oDIV.style.display  = "block";

  setPosX(oDIV,x);
  setPosY(oDIV,y);

  oDIV.style.visibility = "visible";
}
document.showDIVAt  = showDIVAt;

function showDIVNextTo (id,oRef)
{
  var oDIV        = this.getObjectRef(id);
//  alert("trying to locatie DIV: " + id + " -> " +oDIV);
  var bodyScrollX = this.getScrollX();
  var bodyScrollY = this.getScrollY();

  var bodyWidth   = top.clientWidth- 30;
  var bodyHeight  = top.clientHeight - 30;

  oDIV.style.display  = "block";

  x = getPosX(oRef) + getWidth(oRef) + 2;
  y = getPosY(oRef) - 1;
  w = getWidth(oDIV);
  h = getHeight(oDIV);

  if ((x + w - bodyScrollX) > bodyWidth)
  {
    x = getPosX(oRef) - w + 1;
    if (x < bodyScrollX)
      x = bodyScrollX + bodyWidth - w;
  }
  if ((y + h - bodyScrollY) > bodyHeight)
    y = bodyScrollY + bodyHeight - h;

  setPosX(oDIV,x);
  setPosY(oDIV,y);

  oDIV.style.visibility = "visible";
}
document.showDIVNextTo  = showDIVNextTo;

function showDIVNextToLT (id,oRef)
{
  var oDIV        = this.getObjectRef(id);
  var bodyScrollX = this.getScrollX();
  var bodyScrollY = this.getScrollY();
  var bodyWidth   = this.body.contentWidth;
  var bodyHeight  = this.body.contentHeight;

  oDIV.style.display  = "block";

  w = getWidth(oDIV);
  h = getHeight(oDIV);
  x = getPosX(oRef) - w - 1;
  y = getPosY(oRef) + getHeight(oRef) - h - 1;

  if (x < bodyScrollX)
    x = bodyScrollX;
  if (y < bodyScrollY)
    y = bodyScrollY;

  setPosX(oDIV,x);
  setPosY(oDIV,y);

  oDIV.style.visibility = "visible";
}
document.showDIVNextToLT  = showDIVNextToLT;

function showDIVBelow (id,oRef)
{
  var oDIV        = this.getObjectRef(id);
  var bodyScrollX = this.getScrollX();
  var bodyScrollY = this.getScrollY();
  var bodyWidth   = this.body.contentWidth;
  var bodyHeight  = this.body.contentHeight;

  oDIV.style.display  = "block";

  w = getWidth(oDIV);
  h = getHeight(oDIV);
  refW = getWidth(oRef);
  
  x = getPosX(oRef) - w + refW;
  y = getPosY(oRef) + getHeight(oRef) + 1;

  if (x < bodyScrollX)
    x = bodyScrollX;
  if (y < bodyScrollY)
    y = bodyScrollY;

  setPosX(oDIV,x);
  setPosY(oDIV,y);

  oDIV.style.visibility = "visible";
}
document.showDIVBelow = showDIVBelow;

function showDIVAboveCenter (id,oRef)
{
  var oDIV        = this.getObjectRef(id);
  var bodyScrollX = this.getScrollX();
  var bodyScrollY = this.getScrollY();
  var bodyWidth   = this.body.contentWidth;
  var bodyHeight  = this.body.contentHeight;

  oDIV.style.display  = "block";

  w = getWidth(oDIV);
  h = getHeight(oDIV);
  x = getPosX(oRef) + ((getWidth(oRef) - getWidth(oDIV)) / 2);
  y = getPosY(oRef) - getHeight(oDIV) - 1;

  if (x < bodyScrollX)
    x = bodyScrollX;
  if (y < bodyScrollY)
    y = bodyScrollY;

  setPosX(oDIV,x);
  setPosY(oDIV,y);

  oDIV.style.visibility = "visible";
}
document.showDIVAboveCenter = showDIVAboveCenter;

function hideDIV (id)
{
  var oDIV  = this.getObjectRef(id);

  oDIV.style.display    = "none";
  oDIV.style.visibility = "hidden";
}
document.hideDIV  = hideDIV;

function getDoc (frameId)
{
  var doc = null;

  try
  {
    if (msie)
      doc = top.frames(frameId).document;
    else
      doc = top.frames[frameId].document;
  }
  catch (e)
  {
  }

  return(doc);
}

function typeOf (value)
{
  var s = typeof value;

  if (s === "object")
  {
    if (value)
    {
      if ((typeof value.length === "number") &&
          !(value.propertyIsEnumerable("length")) &&
          (typeof value.splice === "function"))
      {
        s = "array";
      }
    }
    else
    {
      s = "null";
    }
  }

  return(s);
}

function openModal (jObject)
{
  if (browserIE6)
    $('select').css({visibility:'hidden'});
  jObject.modal(modalPars);
 
  return(jObject);
}
 
function closeModal ()
{
  $.modal.close();
  if (browserIE6)
    $('select').css({visibility:'visible'});
}


var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();
