/**
 * Een methode aan een onload event hangen.
 * vb: addEvent(window, 'load', startOnLoadFunction);
 *
 * @param el  Het element
 * @param ev  Naam van event (load, resize, enz...)
 * @param fn  Referentie naar aan te roepen functie.
 */
function addEvent(el, ev, fn) {

	if (!el) {
		return;
	}
	// IE
  if (window.attachEvent) {
    el.attachEvent('on' + ev, fn);
  }
  else if (el.addEventListener) {
    el.addEventListener(ev, fn, false);
  }
}


/**
 * Preload gegeven afbeelding.
 *
 * @param pImgObj  Image Object
 * @param pImgSrc  Locatie van de afbeelding
 */
function preloadImage(pImgObj, pImgSrc) {
  if (document.images) {
    eval(pImgObj + ' = new Image()');
    eval(pImgObj + ".src = '" + pImgSrc + "'");
  }
}


/**
 * Een array van foto's inladen in de cache.
 *
 * @param  De image namen.
 */
function preloadImages() { //v3.0

  if (document.images) {
    if (!document.imageArray) {
      document.imageArray = new Array();
    }
    j = document.imageArray.length;
    lArguments = preloadImages.arguments;
    for (i = 0; i < lArguments.length; i++) {
      if (lArguments[i].indexOf("#") != 0) {
        document.imageArray[j] = new Image;
        document.imageArray[j].src = lArguments[i];
        j++;
      }
    }
  }
}

/**
 * De aangeklikte preview versie afbeelden.
 * In IE6 inclusief filter effect.
 *
 * @param pImageName   De naam van het plaatje waarop de nieuwe foto afgebeeld moet worden.
 * @param pImageSrc    De src van de af te beelden foto.
 * @param pDescription De beschrijving in de titel attribute.
 */
function changeImageSrc(pImageName, pImageSrc, pDescription) {
  if (document.images) {

    var lImage = document.getElementById(pImageName);
    if (lImage) {

      // Filter toepassen?
      if (lImage.filters && browser.isIE6up) {
        var filterFunction = 'progid:DXImageTransform.Microsoft.GradientWipe(GradientSize=1,wipestyle=1,motion=forward,duration=0.5)';
        lImage.style.filter = filterFunction;
        if (lImage.filters && lImage.filters[0]) {
          lImage.filters[0].Apply();
          lImage.filters[0].Play();
        }
      }
      lImage.src = pImageSrc;
      lImage.title = pDescription;
    }
    // Controleer ook op een caption div met de beschrijving
    var lCaption = document.getElementById('fotos-caption');
    if (lCaption) {
      try {
        lCaption.innerHTML = pDescription;
      }
      catch(e) {
      }
    }
  }
}


/**
 * Print emailadres mbv JavaScript zodat deze verborgen blijft

 * voor email spiders. (tbv SPAM)
 * @param PUser      De naam van de gebruiker (het eerste deel voor de @)
 * @param PDomain    De domainnaam
 * @param PTopDomain Het laatste deel van de domeinnaam
 */
function printEmail(pUser, pDomain, pTopDomain) {
  var lAt = "@";
  var lDot = ".";
  document.write('<a href="mailto:' + pUser + lAt + pDomain + lDot + pTopDomain + '">');
  document.write(pUser + lAt + pDomain + lDot + pTopDomain);
  document.write('</a>');
}


/**
 * Een terug verwijzing afbeelden, alleen als er ook terug gebladerd kan worden.
 *
 * @param pDescription  Zichtbare titel van de link
 */
function displayGoBack(pDescription) {
  if (window.history) {
    if (window.history.length > 1) {
      document.write('<p style="margin-top:1.5em" class="backlink"><a href="Javascript:goBack()">' + pDescription + '</a></p>');
    }
  }
}


/**
 * Een terug verwijzing afbeelden, alleen als er ook terug gebladerd kan worden.
 *
 * @param pOffset       Het aantal pagina's dat teruggesprongen moet worden in de history of het form-field.
 * @param pDescription  Zichtbare titel van de link.
 */
function displayGoBackN(pOffset, pDescription) {
  if (window.history && pOffset != null && pOffset !='') {
    var lOffset = 0;
    if (pOffset.value) {
      lOffset = parseInt(pOffset.value) + 1;
    } else {
      lOffset = parseInt(pOffset) + 1;
    }
    if (window.history.length > lOffset) {
      document.write('<p style="margin-top:1.5em" class="backlink"><a href="Javascript:goBackN(' + lOffset + ')">' + pDescription + '</a></p>');
    }
  }
}


/**
 * Ga direct een stap terug. In Mozilla is de history niet leesbaar (om veiligheidsredenen)
 */
function goBack() {
  if (window.history)
    window.history.go(-1);
  else
    window.back();
}

/**
 * Een of meerdere stappen terug in de geschiedenis.
 */
function goBackN(n) {
  if (window.history)
    window.history.go(n * -1);
  else
    history.go(n * -1);
}


function popup(url) {
  window.open(url, "_blank", "toolbar=no,location=no,menubar=no,scrollbars=yes,width=467,height=460,resizeable=yes,status=yes");
}

function pop(url, width, height) {
  window.open(url, "_blank", "toolbar=no,location=no,menubar=no,scrollbars=yes,width=" + width + ",height=" + height + ",resizeable=yes,status=yes");
}

function refresh() {
  parent.window.opener.location.reload();
}


/**
 * Threadsafe asynchronous XMLHTTPRequest code.
 * Source: http://www.xml.com/pub/a/2005/02/09/xml-http-request.html
 *
 * @param url       Url to open
 * @param callback  Callback function
 */
function ajaxSend(url, callback) {

  function ajaxBindCallback() {
    if (ajaxRequest.readyState == 4) {
      if (ajaxRequest.status == 200 || ajaxRequest.status == 304) {
        if (ajaxCallback) {
          ajaxCallback(ajaxRequest.responseText);
        } else {
          alert('no callback defined');
        }
      } else {
        alert("There was a problem retrieving the xml data:\n"
                + ajaxRequest.status
                + ":\t" + ajaxRequest.statusText
                + "\n" + ajaxRequest.responseText);
      }
    }
  }

  // use a local variable to hold our request and callback until the inner function is called...
  var ajaxRequest = null;
  var ajaxCallback = callback;

  // bind our callback then hit the server...
  if (window.XMLHttpRequest) {
    // moz et al
    ajaxRequest = new XMLHttpRequest();
    ajaxRequest.onreadystatechange = ajaxBindCallback;
    ajaxRequest.open("GET", url, true);
    ajaxRequest.send(null);
  } else if (window.ActiveXObject) {
    // ie
    ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
    if (ajaxRequest) {
      ajaxRequest.onreadystatechange = ajaxBindCallback;
      ajaxRequest.open("GET", url, true);
      ajaxRequest.send();
    }
  }
}


/**
 * Forceer altijd een scrollbalk in Mozilla
 */
function setScrollbarVisible() {

  // In de html moet een div aanwezig zijn:
  // <div id="mozscroll">&nbsp;</div>
  if (browser.isGecko) {
    scrolldiv = document.getElementById('mozscroll');
    if (scrolldiv) {
      scrolldiv.style.position = 'absolute';
      scrolldiv.style.top = '0px';
      scrolldiv.style.bottom = '-1px';
      scrolldiv.style.visibility = 'hidden';
    }
  }
}


/**
 * Als op de huidige pagina een form aanwezig is, focus op het eerste veld zetten.
 */
function setFocusToFirstFormField() {

  if (document.getElementsByTagName) {
    var forms = document.getElementsByTagName("form");
    if (forms && forms.length > 0) {

      // Loop alle input fields van het formulier langs.
      var fields = forms[0].getElementsByTagName("input");
      if (fields) {
        for (i = 0; i < fields.length; i++) {
          if (fields[i].type != "hidden" &&
              !fields[i].disabled &&
              !fields[i].readOnly) {
            fields[i].focus();
            break;
          }
        }
      }
    }
  }
}


// Browser Detect  v2.1.6
// documentation: http://www.dithered.com/javascript/browser_detect/index.html
// license: http://creativecommons.org/licenses/by/1.0/
// code by Chris Nott (chris[at]dithered[dot]com)
function BrowserDetect() {
  var ua = navigator.userAgent.toLowerCase();

  // browser engine name
  this.isGecko = (ua.indexOf('gecko') != -1 && ua.indexOf('safari') == -1);
  this.isAppleWebKit = (ua.indexOf('applewebkit') != -1);

  // browser name
  this.isKonqueror = (ua.indexOf('konqueror') != -1);
  this.isSafari = (ua.indexOf('safari') != - 1);
  this.isOmniweb = (ua.indexOf('omniweb') != - 1);
  this.isOpera = (ua.indexOf('opera') != -1);
  this.isIcab = (ua.indexOf('icab') != -1);
  this.isAol = (ua.indexOf('aol') != -1);
  this.isIE = (ua.indexOf('msie') != -1 && !this.isOpera && (ua.indexOf('webtv') == -1) );
  this.isMozilla = (this.isGecko && ua.indexOf('gecko/') + 14 == ua.length);
  this.isFirebird = (ua.indexOf('firebird/') != -1);
  this.isNS = ( (this.isGecko) ? (ua.indexOf('netscape') != -1) : ( (ua.indexOf('mozilla') != -1) && !this.isOpera && !this.isSafari && (ua.indexOf('spoofer') == -1) && (ua.indexOf('compatible') == -1) && (ua.indexOf('webtv') == -1) && (ua.indexOf('hotjava') == -1) ) );

  // spoofing and compatible browsers
  this.isIECompatible = ( (ua.indexOf('msie') != -1) && !this.isIE);
  this.isNSCompatible = ( (ua.indexOf('mozilla') != -1) && !this.isNS && !this.isMozilla);

  // rendering engine versions
  this.geckoVersion = ( (this.isGecko) ? ua.substring((ua.lastIndexOf('gecko/') + 6), (ua.lastIndexOf('gecko/') + 14)) : -1 );
  this.equivalentMozilla = ( (this.isGecko) ? parseFloat(ua.substring(ua.indexOf('rv:') + 3)) : -1 );
  this.appleWebKitVersion = ( (this.isAppleWebKit) ? parseFloat(ua.substring(ua.indexOf('applewebkit/') + 12)) : -1 );

  // browser version
  this.versionMinor = parseFloat(navigator.appVersion);

  // correct version number
  if (this.isGecko && !this.isMozilla) {
    this.versionMinor = parseFloat(ua.substring(ua.indexOf('/', ua.indexOf('gecko/') + 6) + 1));
  }
  else if (this.isMozilla) {
    this.versionMinor = parseFloat(ua.substring(ua.indexOf('rv:') + 3));
  }
  else if (this.isIE && this.versionMinor >= 4) {
    this.versionMinor = parseFloat(ua.substring(ua.indexOf('msie ') + 5));
  }
  else if (this.isKonqueror) {
    this.versionMinor = parseFloat(ua.substring(ua.indexOf('konqueror/') + 10));
  }
  else if (this.isSafari) {
    this.versionMinor = parseFloat(ua.substring(ua.lastIndexOf('safari/') + 7));
  }
  else if (this.isOmniweb) {
    this.versionMinor = parseFloat(ua.substring(ua.lastIndexOf('omniweb/') + 8));
  }
  else if (this.isOpera) {
    this.versionMinor = parseFloat(ua.substring(ua.indexOf('opera') + 6));
  }
  else if (this.isIcab) {
    this.versionMinor = parseFloat(ua.substring(ua.indexOf('icab') + 5));
  }

  this.versionMajor = parseInt(this.versionMinor);

  // dom support
  this.isDOM1 = (document.getElementById);
  this.isDOM2Event = (document.addEventListener && document.removeEventListener);

  // css compatibility mode
  this.mode = document.compatMode ? document.compatMode : 'BackCompat';

  // platform
  this.isWin = (ua.indexOf('win') != -1);
  this.isWin32 = (this.isWin && ( ua.indexOf('95') != -1 || ua.indexOf('98') != -1 || ua.indexOf('nt') != -1 || ua.indexOf('win32') != -1 || ua.indexOf('32bit') != -1 || ua.indexOf('xp') != -1) );
  this.isMac = (ua.indexOf('mac') != -1);
  this.isUnix = (ua.indexOf('unix') != -1 || ua.indexOf('sunos') != -1 || ua.indexOf('bsd') != -1 || ua.indexOf('x11') != -1)
  this.isLinux = (ua.indexOf('linux') != -1);

  // specific browser shortcuts
  this.isNS4x = (this.isNS && this.versionMajor == 4);
  this.isNS40x = (this.isNS4x && this.versionMinor < 4.5);
  this.isNS47x = (this.isNS4x && this.versionMinor >= 4.7);
  this.isNS4up = (this.isNS && this.versionMinor >= 4);
  this.isNS6x = (this.isNS && this.versionMajor == 6);
  this.isNS6up = (this.isNS && this.versionMajor >= 6);
  this.isNS7x = (this.isNS && this.versionMajor == 7);
  this.isNS7up = (this.isNS && this.versionMajor >= 7);

  this.isIE4x = (this.isIE && this.versionMajor == 4);
  this.isIE4up = (this.isIE && this.versionMajor >= 4);
  this.isIE5x = (this.isIE && this.versionMajor == 5);
  this.isIE55 = (this.isIE && this.versionMinor == 5.5);
  this.isIE5up = (this.isIE && this.versionMajor >= 5);
  this.isIE6x = (this.isIE && this.versionMajor == 6);
  this.isIE6up = (this.isIE && this.versionMajor >= 6);

  this.isIE4xMac = (this.isIE4x && this.isMac);
}
var browser = new BrowserDetect();

