/*
Copyright (c) 2004 Ylab, http://www.ylab.nl
*/

//trap error
/*
window.onerror = function(msg, url, line){
	window.status = "Er is een fout opgetreden. Meld dit a.u.b. aan de webmaster. " + line + ": " + msg;
	return true;
};*/

var debugging = true;
var isOpera = (navigator.userAgent.indexOf("Opera") > -1);
var isNav = ((!isOpera) && (navigator.appName == "Netscape"));
var isIE  = ((!isOpera) && (navigator.appName.indexOf("Explorer") > -1 ));

var loadFunctions = [];
function addLoadFunction(f)  {loadFunctions[loadFunctions.length] = new Function(f);}
window.onload   = function() {for (var i=0; i<loadFunctions.length; i++){loadFunctions[i]();}};

//display mailto link
function printMail(username, linktext){
	username = username.toLowerCase() + "@nscu.nl";
	if (!linktext) {linktext = username;}
	document.write(linktext.link("mailto:" + username));
}

//Convert id into object
function id2object(el){
	if (typeof(el)=="string"){el = document.getElementById(el);}
	return el;
}

/* quick getElement reference */
function $$() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = id2object(arguments[i]);
		if (arguments.length == 1){
			return element;
		}
		elements.push(element);
	}
	return elements;
}

function callMethodsForEvent(obj, handler){
	if (!obj.methods || !obj.methods[handler]){return;}
	for (var i=0; i<obj.methods[handler].length; i++){obj.methods[handler][i].call(obj);}
}

function addEvent(obj, handler, f){
	if (!obj){return;}
	//prepare method storage
	if (!obj.methods){obj.methods = [];}
	if (!obj.methods[handler]){obj.methods[handler] = [];}
	//the generic method wich will call the actual methods
	var parentMethod = function(){callMethodsForEvent(this, handler);};
	if (!obj[handler]){
		obj[handler] = parentMethod;
	}
	else if (obj[handler].toString() != parentMethod.toString()){
		//store existing method
		obj.methods[handler].push(obj[handler]);
		obj[handler] = parentMethod;
	}
	//store new method
	if (typeof(f) != "function"){f = new Function(f);}
	obj.methods[handler].push(f);
}

//Set focus on first input element
function setFirstFieldFocus(container){
	var list;
	if(container.tagName == "FORM"){
		list = container.elements;
	}
	else{
		list = container.getElementsByTagName("input");
	}
	for (var i=0; i< list.length; i++){
		try {
			if (list[i].type == "button"){continue;}
			list[i].focus();
			if(list[i].select){list[i].select();}
			break;
		}
		catch (exception){}
	}
}

//add class
function addClass(obj, strClass){
	if(!obj){return;}
	if(!obj.className){
		obj.className = strClass;
	}
	else if(!obj.className.match(strClass)){
		obj.className += ' ' + strClass;
	}
}

//remove class
function removeClass(obj, strClass){
	if(!obj || !obj.className){return;}
	 obj.className = obj.className.replace(strClass, '');
}

//enable or disable elements and their children
function setEnabled(on){
	for (var i=1; i<arguments.length; i++){
		var obj = id2object(arguments[i]);
		if (!obj){continue;}
		obj.disabled = !on;
		if(on){
			removeClass(obj, 'disabled');
		}
		else{
			addClass(obj, 'disabled');
		}
		if (obj.hasChildNodes()){
			for (var j=0; j<obj.childNodes.length; j++){
				if (obj.childNodes[j].nodeType == 1){setEnabled(on, obj.childNodes[j]);}
			}
		}
	}
}

//set a style property or fail gracefully
function setStyle(objectId, prop, value){
	var obj = document.getElementById(objectId);
	if ((obj) && (obj.style[prop] != value)){
		obj.style[prop] = value;
	}
}

function Viewport(){
	this.x = 0;
	this.y = 0;
	if (self.innerHeight) {
		// all except Explorer
		this.x = self.innerWidth;
		this.y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight){
		// Explorer 6 Strict Mode
		this.x = document.documentElement.clientWidth;
		this.y = document.documentElement.clientHeight;
	}
	else if (document.body){
		// other Explorers
		this.x = document.body.clientWidth;
		this.y = document.body.clientHeight;
	}
}


//COOKIES
function getCookie(name){
	//Function to return the value of the cookie specified by "name".
	//returns: String object containing the cookie value, or null if the cookie does not exist.

	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	var j = 0;

	function getCookieVal(offset){
		//Internal function to return the decoded value of a cookie
		var endstr = document.cookie.indexOf (";", offset);

		if (endstr == -1){
			endstr = document.cookie.length;
		}
		return unescape(document.cookie.substring(offset, endstr));
	}

	while (i < clen) {
		j = i + alen;
		if (document.cookie.substring(i, j) == arg){
			return getCookieVal (j);
		}
		i = document.cookie.indexOf(" ", i) + 1;
		if (i === 0){
			break;
		}
	}
	return null;
}

function setCookie(name, value){
	//Function to create or update a cookie for calling document.
	document.cookie = name + "=" + escape (value);
}

//DEBUGGING
function devAlert(){
	if (!debugging){return;}
	var code = "Deze functie is nog niet ge"+String.fromCharCode(239)+"mplementeerd.\n";
	for(var i=0; i < arguments.length; i++) {
		code += arguments[i] + "\n";
	}
	alert(code);
}

function debugAlert(){
	if(!debugging){return;}
	var code = "";
	for(var i=0; i < arguments.length; i++){
		code += arguments[i] + "\n";
	}
	code += '\nKlik op Annuleren om verdere meldingen te onderdrukken.';
	debugging = confirm(code);
}

function debugConfirm(){
	if(!debugging){return;}
	var code = "";
	for(var i=0; i < arguments.length; i++){
		code += arguments[i] + "\n";
	}
	return confirm(code);
}

