/**
 * (c) InfoProjects bv. - www.infoprojects.nl
 *
 * $Revision: 1.8 $
 * $Date: 2008/09/03 09:52:49 $
 * $Author: Jelle $
 */

function onLoad() {
  _pageContent.ProcessExternalLinks();
  _pageContent.ScrollToBookMark();
  try { initStijlset(); } catch(e) {}
  try { initPagetype(); } catch(e) {}
}

window.onload = function() {
  onLoad();
}
var _pageContent = new pageContent();

function pageContent() {
    // New window default settings
    this.newWindowDefaultWidth = '500';
    this.newWindowDefaultHeight = '200';
    this.newWindowDefaultPopupAttributes = 'status=yes,toolbar=yes,menubar=yes,location=no,scrollbars=yes,resizable=yes';
    this.newWindowLinkList = new Array();
  }

pageContent.prototype.ScrollToBookMark = function() {
    var gotoBookmarkName = document.getElementById("goto_bookmark");
    if (gotoBookmarkName != null) {
      var gotoBookmark = document.getElementById(gotoBookmarkName.value);
      if (gotoBookmark != null) {
        gotoBookmark.scrollIntoView(true);
      }
    }
  }

pageContent.prototype.FindNewWindowLink = function(linkId) {
    for (var i = 0; i < this.newWindowLinkList.length; i++) {
      if (this.newWindowLinkList[i][0] == linkId) {
        return this.newWindowLinkList[i];
      }
    }
    return null;
  }

pageContent.prototype.AddNewWindowLink = function(linkId, width, heigth) {
    this.newWindowLinkList[this.newWindowLinkList.length] = new Array(linkId, width, heigth);
    return this.newWindowLinkList.length - 1;
  }

pageContent.prototype.ProcessExternalLinks = function() {
    if (!document.getElementsByTagName) {
      return;
    }
    var anchors = document.getElementsByTagName('a');
    for(var i=0; i<anchors.length; i++) {
      if(anchors[i].className == 'externLink') {
        anchors[i].onclick = function() {
          return !window.open(this.href);
        }
      }
      if(anchors[i].className == 'print') {
        anchors[i].onclick = function() {
          print();
        }
      }
      if (anchors[i].id.indexOf('link') == 0) {
        if (this.FindNewWindowLink(anchors[i].id) != null) {
          anchors[i].onclick = function() {
            return openLinkInNewWindow(this);
          }
        }
      }
    }
  }

function openLinkInNewWindow(anchor) {
  var link = _pageContent.FindNewWindowLink(anchor.id);
  var attributes = _pageContent.newWindowDefaultPopupAttributes

  if (link[1] != '')
    attributes += ',width='+link[1];
  else
    attributes += ',width='+_pageContent.newWindowDefaultWidth;

  if (link[2] != '')
    attributes += ',height='+link[2];
  else
    attributes += ',height='+_pageContent.newWindowDefaultHeight;


  return !window.open(anchor.href, 'popup', attributes);
}


function parseDigitString(s) {
  while (s.length>1 && s.substring(0,1)=="0") {
    s=s.substring(1,s.length);
  }
  return (isNaN(parseInt(s))?0:parseInt(s));
}

function putDigitString(num,digits) {
  num = "" + num;
  while (num.length<digits) {
    num="0" + num;
  }
  return (num);
}

function getClient() {
  // convert all characters to lowercase to simplify testing
  var agt=navigator.userAgent.toLowerCase()
  var apv=navigator.appVersion.toLowerCase()
  this.major = parseInt(navigator.appVersion)
  this.minor = parseFloat(navigator.appVersion)
  // browserversion
  this.opera = (agt.indexOf('opera')!=-1);
  this.ns  = ((agt.indexOf('mozilla')!=-1) && ((agt.indexOf('spoofer')==-1) && (agt.indexOf('compatible') == -1)))
  this.ns4 = (this.ns && (this.minor >= 4.03) && (this.major < 5))
  this.ns6 = (this.ns && (this.major == 5) || this.opera)
  this.gecko = (this.ns && (this.major >= 5))
  this.ie   = (agt.indexOf("msie") != -1)
  this.ie4  = (this.ie && (this.major == 3))
  this.ie45 = (agt.indexOf('msie 4.5') != -1);
  this.ie5  = (this.ie && (this.major == 4))
  this.ie6  = (agt.indexOf('msie 6') != -1)
  this.ie7  = (agt.indexOf('msie 7') != -1)
  // platform
  this.mac = (apv.indexOf("macintosh")>0);
  this.win = (apv.indexOf("win")>0);
  // compatible browsers
  this.ie4comp = ((this.ie4 && !this.mac) || this.ie45 || this.ie5)
  this.ns4comp = (this.ns4);
  this.ns6comp = (this.gecko || this.ns6);
  this.comp = (this.ie4comp || this.ns4comp || this.ns6comp);
  return (this)
}

var is = new getClient();

function openWindow(url,attr) {
  popup = window.open(url,"popup",attr);
  return false;
}

function openPopup(url,name,attr) {
  popup = window.open(url,name,attr);
  return false;
}

function toggleMenu(name,n,alt) {
  if (document.all) {
    if (document.all[name]) {
      document.all[name][1-n].style.display = "none";
      document.all[name][n].style.display = "block";
    }
    else document.reload(alt);
  }
}

function toggleBlock(name,n) {
  if (document.all) {
    for (var i=0; i<document.all[name].length; i++) document.all[name][i].style.display = (n==i)?"block":"none";
  }
}

var centerTop=false;

function updateTopButton() {
  if(getElt("top_button")) {
    var topButton = getElt("top_button");

    if(getWinScrollTop() > 0) {
      setEltVisibility(topButton,'visible');
      setEltDisplay(topButton,'block');
      setEltPosition(topButton,'absolute');

      var difR = 0;

      var difC = ((getWinWidth() - 996)/2) + offsetRight;

      if(eval(centerTop) && maxTopWinWidth && ((parseInt(getWinWidth()))>maxTopWinWidth)) {
        difR = getWinWidth() - difC;
      }
      // breder dan maxwidth
      else if(maxTopWinWidth && (getWinWidth()>maxTopWinWidth)) {
        difR = maxTopWinWidth - offsetRight;
      }
      //smaller dan maxwidth
      else {
        difR = (getWinWidth() - offsetRight);
      }

      setEltLeft(topButton,difR);

      var difT = (getWinHeight() - offsetBottom) + getWinScrollTop();
      setEltTop(topButton,difT);
    } else {
      setEltVisibility(topButton,'hidden');
      setEltDisplay(topButton,'none');
    }
  }
}

// Functie om een methode 'onResize' uit te voeren
function onResize(method) {
  if(is.ns4comp || is.ns6comp) {
    return window.onresize = method;
  } else {
    document.body.onresize = method;
  }
}

// Functie om een methode 'onScroll' uit te voeren
function onScroll(method) {
  if(is.ns4comp || is.ns6comp) {
    return window.onscroll = method;
  } else {
    document.body.onscroll = method;
  }
}

// Functie om een methode 'onMouseMove' uit te voeren
function onMouseMove(method) {
  if (is.ns4comp) document.captureEvents(Event.MOUSEMOVE)
  document.onmousemove = method;
}

function getImage(img) {
  return document.images[img];
}

function swapImage(img,src) {
  document.images[img].src = src;
}

function swapImageAlt(img,alt) {
  document.images[img].alt = alt;
}

function getForm(name) {
  return document.forms[name];
}

function getElt(id) {
  return document.getElementById(id);
}

// --- POSITION ---

function setEltPosition (elt, value) {
  if (!elt || elt == null) return;
  if (is.ns4comp) elt.position = value;
  else if (is.ie4comp) elt.style.position = value;
  else if (is.ns6comp) elt.style.position = value;
}

// --- DISPLAY ---

function setEltDisplay (elt, value) {
  if (!elt || elt == null) return;
  if (is.ns4comp) elt.display = value;
  else if (is.ie4comp) elt.style.display = value;
  else if (is.ns6comp) elt.style.display = value;
}

function getEltDisplay(elt) {
  if(!elt || elt == null) {
    return;
  }
  if(is.ns4comp) {
    return (elt.display);
  } else if(is.ie4comp) {
    return (elt.style.display);
  } else if(is.ns6comp) {
    return (elt.style.display);
  }
}

// --- VISIBILITY ---

function setEltVisibility (elt, value) {
  if (!elt || elt == null) return;
  if (is.ns4comp) elt.visibility = value;
  else if (is.ie4comp) elt.style.visibility = value;
  else if (is.ns6comp) elt.style.visibility = value;
}

// --- POSITION ---

function setEltLeft (elt, x) {
  if (!elt || elt == null) return;
  if (is.ns4comp)      elt.left=x;
  else if (is.ie4comp) elt.style.pixelLeft=x;
  else if (is.ns6comp) elt.style.left = (x + "px");
}

function setEltTop (elt, y) {
  if (!elt || elt == null) return;
  if (is.ns4comp)      elt.top=y;
  else if (is.ie4comp) elt.style.pixelTop=y;
  else if (is.ns6comp) elt.style.top= (y + "px");
}

function setEltHeight (elt, y) {
  if (!elt || elt == null) return;
  if (is.ns4comp)      elt.height=y;
  else if (is.ie4comp) elt.style.pixelHeight=y;
  else if (is.ns6comp) elt.style.height= (y + "px");
}

function getEltLeft (elt) {
  if (!elt || elt == null) return 0;
  if (is.ns4comp)      return (elt.left);
  else if (is.ie4comp) return (elt.offsetLeft);
  else if (is.ns6comp) return (elt.offsetLeft);
}

function getEltTop (elt) {
  if (!elt || elt == null) return 0;
  if (is.ns4comp)      return (elt.top);
  else if (is.ie4comp) return (elt.offsetTop);
  else if (is.ns6comp) return (elt.offsetTop);
}

function getEltWidth (elt) {
  if (!elt || elt == null) return 0;
  if (is.ns4comp)      return(elt.document.width);
  else if (is.ie4comp) return (elt.offsetWidth);
  else if (is.ns6comp) return (elt.offsetWidth);
}

function getEltHeight (elt){
  if (!elt || elt == null) return 0;
  if (is.ns4comp)      return(elt.document.height);
  else if (is.ie4comp) return (elt.clientHeight);
  else if (is.ns6comp) return (elt.offsetHeight);
}

// --- CLIPPING ---

function setEltClip (elt, cliptop, clipright, clipbottom, clipleft)
{ if (is.ns4comp) {
    elt.clip.left   = clipleft;
    elt.clip.top    = cliptop;
    elt.clip.right  = clipright;
    elt.clip.bottom = clipbottom;
  }
  else if (is.ie4comp)  elt.style.clip = 'rect(' + cliptop + ' ' +
       clipright + ' ' + clipbottom + ' ' + clipleft +')';
  else if (is.ns6comp)  elt.style.clip = 'rect(' + cliptop + ' ' +
       clipright + ' ' + clipbottom + ' ' + clipleft +')';
}

// --- WINDOW PROPERTIES ---

function getWinWidth()
{ if (is.ns4comp) return(window.innerWidth);
  else if (is.ie4comp) return(document.body.clientWidth);
  else if (is.ns6comp) return(window.innerWidth);
}

function getWinHeight()
{ if (is.ns4comp) return(window.innerHeight);
  else if (is.ie4comp) return(document.body.clientHeight);
  else if (is.ns6comp) return(window.innerHeight);
}

function getWinScrollLeft()
{ if (is.ns4comp) return(window.pageXOffset);
  else if (is.ie4comp) return(document.body.scrollLeft);
  else if (is.ns6comp) return(window.pageXOffset);
}

function getWinScrollTop()
{ if (is.ns4comp) return(window.pageYOffset);
  else if (is.ie4comp) return(document.body.scrollTop);
  else if (is.ns6comp) return(window.pageYOffset);
}

// --- MOUSE EVENT PROPERTIES ---

var curMouseY = 0;
function getMouseWinTop(e) {
  var posy = 0;
  if (!e) var e = window.event;
  if (e.pageY)
  {
    posy = e.pageY;
  }
  else if (e.clientY)
  {
    posy = e.clientY + document.body.scrollTop;
  }
  curMouseY = posy;
  return posy;
}

function updateXY(e) {
  getMouseWinLeft(e);
  getMouseWinTop(e);
}

var curMouseX = 0;
function getMouseWinLeft(e) {
  var posx = 0;
  if (!e) var e = window.event;
  if (e.pageX)
  {
    posx = e.pageX;
  }
  else if (e.clientX)
  {
    posx = e.clientX + document.body.scrollLeft;
  }
  curMouseX = posx;
  return posx;
}

// --- CLASS ---
function getEltClassName(elt) {
  return elt.className;
}

function setEltClassName(elt,val) {
  elt.className = val;
}

// --- BACKGROUND ---

function setEltBg(elt,val) {
  val = (val.indexOf("url(") != -1)?val:("url('"+val+"')");
  elt.style.backgroundImage = val;
}

// -- Datum veld check --
function fieldDateCheck(input, nextEltId, length) {
  try {
    removeCharsFromDateField(input);

    if (input.value.length == length) {
      var elt = document.getElementById(nextEltId);
      if(elt) {
        elt.focus();
      }
    }
  }
  catch (e)
  { }
}

// -- Datum veld opschoner, verwijderd alle niet cijfers --
function removeCharsFromDateField(input) {
  try {
    var pos = getCaretPosition(input);
    var regex = new RegExp("[^0-9]", "g");
    input.value = input.value.replace(regex, "");
    setCaretPosition(input, input.value.length <= pos ? pos : input.value.length);
  }
  catch (e)
  { }
}

function hideSelectBoxes(bool) {
  // prototype js needed!!!
  // fix for IE6 or lower
  var selectBoxes = $$('select');
  for(var i = 0; i < selectBoxes.length; i++) {
    if(bool) {
      selectBoxes[i].hide();
    } else {
      selectBoxes[i].show();
    }
  }
}

function showCalendar(curDate, fieldId) {
  // prototype js needed!!!
  hideSelectBoxes(true);
  var url = iproxGet + '?xdl=/views/global/xdl/calendar&fieldName=' + fieldId + '&calendar_date=' + curDate;
  new Ajax.Updater( { success: (fieldId + '_calendar') }, url);
}

function selectCurrentDate(date, fieldId) {
  // prototype js needed!!!
  $(fieldId + '_d').value = date.substring(6,8);
  $(fieldId + '_m').value = date.substring(4,6);
  $(fieldId + '_y').value = date.substring(0,4);
  closeCalendar(fieldId);
}

function selectCurrentDay(id) {
  // prototype js needed!!!
  $(id).toggleClassName('selected');
}

function closeCalendar(fieldId) {
  // prototype js needed!!!
  hideSelectBoxes(false);
  $(fieldId + '_calendar').innerHTML = '';
}

function getCaretPosition (ctrl) {
  var CaretPos = 0;
  // IE Support
  if (document.selection) {
    ctrl.focus ();
    var Sel = document.selection.createRange ();
    Sel.moveStart ('character', -ctrl.value.length);
    CaretPos = Sel.text.length;
  }
  // Firefox support
  else if (ctrl.selectionStart || ctrl.selectionStart == '0') {
    CaretPos = ctrl.selectionStart;
    return (CaretPos);
  }
}

function setCaretPosition(ctrl, pos) {
  if (ctrl.setSelectionRange) {
    ctrl.focus();
    ctrl.setSelectionRange(pos,pos);
  }
  else if (ctrl.createTextRange) {
    var range = ctrl.createTextRange();
    range.collapse(true);
    range.moveEnd('character', pos);
    range.moveStart('character', pos);
    range.select();
  }
}
