/*
Copyright (c) 2004-2008 Ylab, www.ylab.nl
*/

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 hasFirebug = (window.console && console.firebug);

//trap error
if(!debugging){
	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 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() + "@vrijbergen.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;
}

Array.prototype.in_array = function(element){
	var i;

	for (i=0; i<this.length; i++){
		if (this[i] == element){
			return true;
		}
	}
	return false;
};

//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){}
	}
}

//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]);}
			}
		}
	}
}

//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ï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);
}
