/////////////////////////////////////////////////////////////////////////////////////////////
/* default lib javascript */
/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////
/*

TOC 

0.  Initialise script for all onload events
1.  IE Detect (WARNING: BROWSER CHECK SCRIPTS VIOLATE DUTCH GOVORMENT GUIDELINES FOR WEBSITES) 
2.  Jump to the next input
3.  Toggle all checkboxes [NWP]
4.  Toggle scripts
    i   Basic menu trigger function - onmouseover event
    ii  Auto pulldown loader - onload event, use init function
    iii Toggle menu - onclick event
    iv  Generic toggle
    v   Toggle all checkboxes - onclick event
5.  Classname & node handlers (WARNING: WILL BE REPLACED BY PROTOTYPE.JS)
6.  Form validation (WARNING: NOT READY, DO NOT USE)
7.  Basic popup 
8.  Cookie functions [quirksmode.org]
9.  Slideshow (IE specific)
10. Textarea character counter
11. Replace Select (WARNING: BUGGY, IE SCROLL ISSUE)


This is a list of scripts that are used but not advised 
due to poor usability.
A. Content width (re)sizer
B. Font sizing script


*/
/////////////////////////////////////////////////////////////////////////////////////////////




//------------------------------------------------------
// init

window.onload = init;
function init(){
// Trigger zappmenu for IE6 and lower
// autoRunZappMenu('navList','A')
  
// Trigger Replace Select
// (document.all && !window.print) ? null : setForm();

// Trigger Slideshow
// runSlides();

}

// End init
//------------------------------------------------------


//------------------------------------------------------
// IE Detect (Windows only!)

var detect = navigator.userAgent.toLowerCase();

// use this variable to check if browser is Internet Explorerfor Windows 
var WinIE;

if (checkIt('msie')) WinIE = true;
if (checkIt('opera')||checkIt('mac')) WinIE = false;
function checkIt(string){
	place = detect.indexOf(string) + 1;
	return place;
}
// End IE Detect
//------------------------------------------------------


//------------------------------------------------------
// Jump to the next input 'elm' after 'n' chracters

function jumpTab(elm,n){
  if (elm.value.length>=n){
    var pe=fcd((fns((fpd(elm,'DIV')),'DIV')),'INPUT');
    pe.focus();
  }
}
//------------------------------------------------------


//------------------------------------------------------
// toggle all checkboxes within element id (elID)

function checkAll(elID){
  var el=document.getElementById(elID);
  var e=fpd(el,'UL');
  var i = e.getElementsByTagName('INPUT');
  el.onclick=function(){
    this.checked = (this.checked) ? true : false;
    for(n=0;n<i.length;n++){
      if(i[n]==this)continue;
      if(!i[n].checked)i[n].checked=true;
    }
  }
  for(n=0;n<i.length;n++){
    if(i[n]==el)continue;
    i[n].onclick=function(){
      if(!this.checked)el.checked=false;
    }
  }
}
//------------------------------------------------------



//------------------------------------------------------
// Toggle scripts

// classnames to toggle
var o="open";
var c="closed";

// Zappwerk menu
// el : object to attach an event 
// t  : optional nodeName to switch classNames. Default is 'LI'
function zappMenu(el,t){
  if (!WinIE) return
  var t = ( t=="" || t==undefined || t==null ) ? 'LI' : t
  var e = fpd(el,t);
  var u = fpd(e,'UL');
  attachMenuEvent(e,u)
}


function autoRunZappMenu(startObjId,tag){
  //if (!WinIE) return ;
  var t = ( tag=="" || tag==undefined || tag==null ) ? 'A' : tag ;
  var s = document.getElementById(startObjId).getElementsByTagName(t);
  for (n=0;n<s.length;n++) {
    //if (s[n].className == "folder" ){
      var e = fpd(s[n],'LI');
      e.id=(e.id)+n;
      attachMenuEvent(e);
    //}
  }
}

function attachMenuEvent(e,u){
  if (u!=null) {
    var l = u.getElementsByTagName('LI');
    for(n=0;n<l.length;n++){
      if (l[n].className.indexOf(c)>-1) {
        l[n].id=(u.id)+n;
        switchClassName(e,c,c);
      }
    }
  }
  var eul = e.getElementsByTagName('UL')[0];
  if (eul){
    e.onmouseover = function(){
      switchClassName(e,c,o);
      eul.style.zIndex="300";
    }
    e.onmouseout = function(){
      setTimeout("switchClassName(document.getElementById('"+e.id+"'),o,c)", 200);
      eul.style.zIndex="0";
    }
  }
}

// Zappwerk toggle
function zappToggle(el,t){
  var t = ( t=="" || t==undefined || t==null ) ? 'LI' : t
  var e = fpd(el,t);
  var u = fns(e,'UL');
  var l = u.getElementsByTagName('LI');
  for(n=0;n<l.length;n++){
    l[n].id=(u.id)+n;
    if (l[n].className.indexOf(o)>-1) {
      switchClassName(e,o,c);
    }else{
      if (l[n].className.indexOf(c)>-1) {
        switchClassName(e,c,o);
      }
    }
  }
}

// Zappwerk toggle [generic]
function listToggle(el,t){
  var t = ( t=="" || t==undefined || t==null ) ? 'LI' : t
  var e = fpd(el,t);
  if (e.className.indexOf(o)>-1) {
    switchClassName(e,o,c);
  }else if(e.className.indexOf(c)>-1) {
    switchClassName(e,c,o);
  }
}

// Zappwerk toggle all [generic]
function toggleAll(id,tag,s){
  var h = document.getElementById(id).getElementsByTagName(tag);
  for(n=0;n<h.length;n++){ 
    if (h[n].className.indexOf('makerBox')>-1){ 
      if (s.indexOf(o)>-1 && h[n].className.indexOf(o)>-1 ) {
        switchClassName(h[n],o,c);
      }else if(s.indexOf(c)>-1 && h[n].className.indexOf(c)>-1) {
        switchClassName(h[n],c,o);
    }
    }
  }
}
// End Toggle scripts
//------------------------------------------------------



//------------------------------------------------------
// Classname & node handlers

function addCSSClass(elem,cn){elem.className=(elem.className+" "+cn).trim();}
function removeCSSClass(elem,cn){elem.className=elem.className.replace(cn,"").trim();}
String.prototype.trim=function(){return this.replace( /^\s+|\s+$/, "" );}
function switchClassName(e,a,b){if(b){e.className=e.className.replace(a,"").trim();e.className=(e.className+" "+b).trim();}else{e.className=a;}}
function fpd(elem,e){var o=elem;while(o.parentNode.nodeName!=e.toUpperCase()){o=o.parentNode;}return o.parentNode;}
function fps(elem,e){var o=elem;while(o.previousSibling.nodeName!=e.toUpperCase()){o=o.previousSibling;}return o.previousSibling;}
function fns(elem,e){var o=elem;while(o.nextSibling.nodeName!=e.toUpperCase()){o=o.nextSibling;}return o.nextSibling;}
function fcd(elem,e){var o=elem;if(o.firstChild.nodeName==e.toUpperCase()){return o.firstChild;}else{while(o.nextSibling.nodeName!=e.toUpperCase()){o=o.nextSibling;}return o.nextSibling;}}
if ( typeof Array.push == 'undefined' ) Array.prototype.push = function() { var arg, i = 0; while( arg = arguments[i++] ) { this[this.length] = arg; } return this.length;}

// End Classname & node handlers
//------------------------------------------------------



//------------------------------------------------------
// Form validation

/*
var fa = new String();

var errorTypes = {
  required: new errorType(false, /^\s+|\B$/, "Laat dit veld niet leeg, \neen lege veld of een 'spatie' is niet toegestaan.")
}

function errorType( frmError, reg, errMsg ) {
  this.frmError = frmError;
  this.errMsg = errMsg;
  this.reg = reg;
}

function validateForm(e,elmId,tval){
  var i = document.getElementById(elmId);
  var f = fpd(i,'FORM')
  if(fa=="")fa = f.action;
  var t = chkInput(i.value, eval("errorTypes."+tval));
  if (t.frmError) {
    f.action = null;
    alert(t.errMsg);
    return false;
  } else {
    f.action = fa;
    //f.action();
    return true;
  }
}

function chkInput(i, t){
  if (t.reg.test(i)) {
   t.frmError = true;
  }
  return t;
}

*/
// End Form validation
//------------------------------------------------------



//------------------------------------------------------
// Basic popup script

// * Open a popup window to the specified uri
// * - url The location to open to popup.
// * - type The type of popupwindow(1(default) = 275 * 550, 2 = 535 * 600, 3 = 535 * 600)
// * - name The name of the window(_blank is default)

// HTML EXAMPLE:
/* <a href="popup.html" onclick="return !linkPopup(this, popupTypes.type1);" target="popup">popup link</a> */


var popupTypes = {
  type1: new PopupType(650, 480)
}

function PopupType(width, height) {
  this.width = width;
  this.height = height;
}

function openPopup(aUrl, aType, aName) {
  var aWidth, aHeight;
  if (typeof aType == 'undefined' || !(aType.width && aType.height)) {
    aType = popupTypes.type1;
  }
  aWidth = aType.width;
  aHeight = aType.height;
  var optionString = "toolbar=0, location=0, directories=0, status=0, menubar=0, scrollbars=1, resizable=0, width=" + aWidth + ", height=" + aHeight
  var aWindow = window.open(aUrl, (aName) ? aName : "_blank", optionString);
  if (aWindow) {
    aWindow.focus();
    return true;
  } else {
    return false;
  }
}

// * Opens a popup using the href and target attributes from the a tag
// * - src the ancor tag(this)
// * - aType a popup type for the width and height(default = popupTypes.type1)
function linkPopup(src, aType) {
  return openPopup(src.getAttribute('href'), aType || popupTypes.type1,(src.getAttribute('target') || '_blank'));
}

// END: Basic popup script
//------------------------------------------------------


//------------------------------------------------------
// Cookie functions [quirksmode.org]
// This function sets a cookie for the domain. Adapt for specific pages.

function setCookie(name,value,days) {
	if (days)	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	setCookie(name,"",-1);
}

// END Cookie functions
//------------------------------------------------------


//------------------------------------------------------
// slideshow 

var timer, numberOfPictures, slide;
var counter = 0
var pic = new Array()
var fadeTime = 3
var viewingTime = 4000

function getslides(containerID){
  slide = document.getElementById(containerID)
  pic = slide.getElementsByTagName('IMG');
  numberOfPictures = pic.length
  if (numberOfPictures<=0) return;
  slide.style.backgroundImage='url('+pic[0].src+')';
}

function runSlides(){
  if (numberOfPictures<=0) return;
  if (WinIE){
    slide.style.filter="blendTrans(duration=fadeTime)"
    slide.filters.blendTrans.Apply()      
  }
  slide.style.backgroundImage='url('+pic[counter].src+')';
  if (WinIE){
    slide.filters.blendTrans.Play();
  } 
  counter++;
  if (counter > (numberOfPictures-1)) counter=0; 
  timer = setTimeout('runSlides()', viewingTime);
}
// END slideshow
//------------------------------------------------------


//------------------------------------------------------
// Textarea character counter

function countChars(e,elID,n){
  var c=document.getElementById(elID);
  if(e.value.length>(n-1)) { e.value = e.value.slice(0,(n-1)); }
  c.value = (n-(e.value).length)-1;
}
function runCharCount(e){
  e.onkeypress();
}
// END Textarea character counter
//------------------------------------------------------


//------------------------------------------------------
// Replace Select
// http://easy-designs.net/articles/replaceSelect/
// EK: has a problem with scrolling 

function selectReplacement(obj) {
  obj.className += ' replaced';
  var ul = document.createElement('ul');
  ul.className = 'selectReplacement';
  var opts = obj.options;
  for (var i=0; i<opts.length; i++) {
    var selectedOpt;
    if (opts[i].selected) {
      selectedOpt = i;
      break;
    } else {
      selectedOpt = 0;
    }
  }
  for (var i=0; i<opts.length; i++) {
    var li = document.createElement('li');
    var txt = document.createTextNode(opts[i].text);
    li.appendChild(txt);
    li.selIndex = opts[i].index;
    li.selectID = obj.id;
    li.onclick = function() {
      selectMe(this);
    }
    if (i == selectedOpt) {
      li.className = 'selected';
      li.onclick = function() {
        this.parentNode.className += ' selectOpen';
        this.onclick = function() {
          selectMe(this);
        }
      }
    }
    if (window.attachEvent) {
      li.onmouseover = function() {
        this.className += ' hover';
      }
      li.onmouseout = function() {
        this.className =  this.className.replace(new RegExp(" hover\\b"), '');
      }
    }
    ul.appendChild(li);
  }
  obj.parentNode.insertBefore(ul,obj);
}
function selectMe(obj) {
  var selChange = fpd(obj,'FORM');
  var lis = obj.parentNode.getElementsByTagName('li');
  for (var i=0; i<lis.length; i++) {
    if (lis[i] != obj) {
      lis[i].className='';
      lis[i].onclick = function() {
        selectMe(this);
      }
    } else {
      setVal(obj.selectID, obj.selIndex);
      obj.className='selected';
      obj.parentNode.className = obj.parentNode.className.replace(new RegExp(" selectOpen\\b"), '');
      obj.onclick = function() {
        obj.parentNode.className += ' selectOpen';
        this.onclick = function() {
          selectMe(this);
        }
      }
      selChange.submit()
    }
  }
}
function setVal(objID, selIndex) {
  var obj = document.getElementById(objID);
  obj.selectedIndex = selIndex;
}
function setForm() {
  var s = document.getElementsByTagName('select');
  for (var i=0; i<s.length; i++) {
    selectReplacement(s[i]);
  }
}

// END: Replace Select
//------------------------------------------------------




/////////////////////////////////////////////////////////////////////////////////////////////
/* CAUTION! SCRIPTS THAT REPRESENT BAD PRACTICE - BUT WE USE THEM ANYWAY */
/////////////////////////////////////////////////////////////////////////////////////////////


//------------------------------------------------------
// Content width (re)sizer

/* EK: this system has one major flaw. 
   The HTML page needs to set the content width before the page loads. 
   If the content width needs to change after the pageload user wil see teh page 'switch'. */

// # setting the wrapper width by setting it's ID name (use the onload itit to trigger the function)
// # sw = minimun width (trigger)
// # ws = className for small window size
// # wl = className for larger window size
// # we = element to set ID for

function sPW(sw,ws,wl,we){
  var s = new gMS();
  we.id = ( s.w > sw ) ? wl : ws;
}

// returns web client window width and height [crossbrowser]

function gMS(w,h){
  if( typeof( window.innerWidth ) == 'number' ) {
    this.w = window.innerWidth; 
    this.h = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    this.w = document.documentElement.clientWidth; 
    this.h = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    this.w = document.body.clientWidth; 
    this.h = document.body.clientHeight;
  }
}
// END: Content width (re)sizer
//------------------------------------------------------


//------------------------------------------------------
// Font sizing script

if (getCookie('basesize')=='' || getCookie('basesize')=='NaN'){setCookie('basesize', 75, 30)}
var bn=parseInt(getCookie('basesize'));
function sizing(n){
  if ((getCookie('basesize'))=='') { alert('Mededeling:\nHet schalen van lettertypes maakt gebruik van cookies.\nDeze browser kan momenteel daar geen gebruik van maken.\nControleer je privacy instellingen in de browser opties.'); bn=75; return; }
  bn=parseInt(bn+n);
  if (parseInt(bn)>=135&&n==10) { bn = 135; return; }
  if (parseInt(bn)<=55&&n==-10) { bn = 55; return; }
  setCookie('basesize', parseInt(bn), 30);
  window.location.reload();
}

function writeSize(){
  var setSize ="";
  setSize += "<style type='text/css' rel='alternative'>body{font-size:"+bn+"%;}<\/style>"
  document.write(setSize)
}
// End Font sizing script
//------------------------------------------------------



//  EOF

