/* --- JavaScript --- */
/* --- General --- */


/* --- add functions to onload event: addLoadEvent(functionName); --- */
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}


/* --- set comment values in form fields --- */

var defValues = [
	 ["trefwoord","Typ een trefwoord van een beslissing of bezwaar"]
];

function DefValObj(val) {
	this.value = val;
};

var defVal = {};

setFormFields = function() {
	for (i=0; i<defValues.length; i++) {
		var inputNode = document.getElementById(defValues[i][0]);
		if (inputNode != null) {
			inputNode.value = (inputNode.value && inputNode.value != "") ? inputNode.value : defValues[i][1];
			defVal[defValues[i][0]] = new DefValObj(defValues[i][1]);
			inputNode.onfocus = function() {
				if (this.value == defVal[this.id].value) {
					this.value = "";
				}
				addClass(this,"hasFocus");
			}
			inputNode.onblur = function() {
				removeClass(this,"hasFocus");
			}
		}
	}
}

addLoadEvent(setFormFields);


/* --- show print link if JS is supported --- */
/*
setPrintLink = function() {
	var fnxMenu = document.getElementById('functieMenu');
	for (f=0; f<fnxMenu.childNodes.length; f++) {
		var fnxItem = fnxMenu.childNodes[f];
		if (fnxItem.nodeName == "LI") {
			if (fnxItem.id == "printKnop") {
				fnxItem.className += " show";
			}
			else if (fnxItem.id == "omhoogKnop") {
				fnxItem.className = fnxItem.className.replace("eerste","");
			}
		}
	}
}

addLoadEvent(setPrintLink);	// add to onload event
*/

/* --- createElement() --- */
function createElement(element) {
	if (typeof document.createElementNS != 'undefined') {
		return document.createElementNS('http://www.w3.org/1999/xhtml', element);
	}
	if (typeof document.createElement != 'undefined') {
		return document.createElement(element);
	}
	return false;
}

/* --- setAttributes() --- */
function setAttributes(element,attr) {	// format attr: [['class','actief'],['href','http://www.test.xx']]
	if (typeof element.setAttributeNS != 'undefined') {
		for (a=0; a<attr.length; a++) {
			element.setAttributeNS('http://www.w3.org/1999/xhtml',attr[a][0],attr[a][1]);
		}
	}
	if (typeof element.setAttribute != 'undefined') {
		for (a=0; a<attr.length; a++) {
			element.setAttribute(attr[a][0],attr[a][1]);
		}
	}
	return false;
}

/* --- add/remove className --- */
function addClass(thisNode,thisClass) {
	thisNode.className += " " + thisClass;
}

function removeClass(thisNode,thisClass) {
	if (thisNode.className.length == (thisClass.length)) {
		thisNode.className = thisNode.className.replace(thisClass,"");
	}
	else {
		thisNode.className = thisNode.className.replace(" " + thisClass,"");
	}
}


/* --- add all links that require JavaScript to work --- */

addJSlinks = function() {
	serviceLinks();
	if (document.forms.length > 0) {
		formInfo();
		autoFlow();
	}
}

var oldIE;	// value is set to true if user agent is less or equal to IE6
var currentInfo = {};

function formInfo() {
	var infoFields = document.getElementById('mainForm').getElementsByTagName('dd');	// get reference to input containers (<dd>)
	if (infoFields == null) return false;
	if (oldIE) redrawOldIE(false);
	for (d=0; d<infoFields.length; d++) {
		var infoField = infoFields[d];
		if (infoField.className != "info") continue;	// find <dd>'s with class="info"
		var info = infoField.getElementsByTagName('div');
		if (info == null) continue;
		for (i=0; i<info.length; i++) {
			var infoButton = info[i];
			if (infoButton.className != "info") continue;	// find <div>'s with class="info"
			infoField.className = infoField.className.replace("info","jsInfo");	// change className into "jsInfo", so CSS knows JS is supported
			infoButton.onclick = function() {
				if (this.className.indexOf('jsToggleOn') > -1) {
					removeClass(this,'jsToggleOn');	//	hide info
					this.removeAttribute('title');
				}
				else {
					if (currentInfo.className) {
						if (currentInfo.className.indexOf('jsToggleOn') > -1) {
							removeClass(currentInfo,'jsToggleOn');	//	hide info
							currentInfo.removeAttribute('title');
						}
					}
					currentInfo = this;
					addClass(this,'jsToggleOn');	// show info
					this.title = "Klik om te sluiten";
				}
			}
			break;
		}
	}
	if (oldIE) redrawOldIE(true);
}

function redrawOldIE(redraw) {
		var container = document.getElementById('container');
		if (container != null) {
			container.style.width = (redraw) ? "auto" : "0";
		}
}

function autoFlow() {
	var flowForm = document.getElementById('flowForm');
	if (flowForm == null) return false;
	var flowChoices = flowForm.getElementsByTagName('fieldset');
	if (flowChoices == null) return false;
	for (f=0; f<flowChoices.length; f++) {
		var inputs = flowChoices[f].getElementsByTagName('input');
		if (inputs == null) continue;
		var inputLength = inputs.length;
		for (i=0; i<inputLength; i++) {
			var inputControll = inputs[i];
			if (inputControll.type == "radio") {
				inputControll.onclick = function() {
					this.selected = "selected";
					flowForm.submit();	// submit form on change
				}
			}
			else if (inputControll.type == "submit") {
				inputControll.style.display = "none";	// hide submit
			}
		}
	}
}

function serviceLinks() {
	var serviceMenu = document.getElementById('sMenu');
	if (serviceMenu != null) {
		var menu = serviceMenu.innerHTML;
		if (window.print) {
			menu += '<li class="print"><a id="print" href="#" onclick="window.print();">Print</a></li>';
		}
		if (cssSupport()) { // check CSS support
			menu += '<li class="tekstgrootte"><a class="kleiner" href="javascript:swapStyle(\'smaller\');" title="tekst verkleinen"><span>A</span></a><span>A</span><a class="groter" href="javascript:swapStyle(\'bigger\');" title="tekst vergroten"><span>A</span></a></li>';
		}
		serviceMenu.innerHTML = menu;
	}
}

function cssSupport() {
	if (!document.styleSheets) return false;	// styleSheets object is not supported
	var css = document.styleSheets;
	for (s=0; s<css.length; s++) {
		if (s == 0) {
			if (!(css[0].cssRules || css[0].rules)) return false;	// both methods (cssRules/rules) are not supported
		}
		if (!css[s].disabled) return true;	// at least one of the stylesheets is not disabled
	}
	return false;	// stylesheets are all disabled or not supported at all
}

addLoadEvent(addJSlinks);
