
/**
 * Adds an event to an object without overwriting currently assigned eventHandlers.
 * does it the correct way.
 * Function found at onlinetools.org -
 * @link http://www.onlinetools.org/articles/unobtrusivejavascript/chapter4.html
 * @param func function-object (not function name!)
 **/
function addEvent(obj, evType, fn)
{
	if (obj.addEventListener)
	{
		obj.addEventListener(evType, fn, false);
		return true;
	}
	else if (obj.attachEvent)
	{
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	}
	else
	{
		return false;
	}
}

/**
 * Adds an event to window.onload without overwriting currently assigned onload functions.
 **/
function addLoadEvent(func)
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function')
	{
		// no onload function set up until now
    	window.onload = func;
	}
	else
	{
		// onload-handler already exists, execute old one first..
		window.onload = function(){
			oldonload();
			func();
		}
	}
}



/**
 * return a nicely formatted number to print
 * @access public
 * @param t number to format
 * @param delim delimiter, defaults to ' (123'456)
 * @return String formatted number
 **/
function numberFormat(t, delim)
{
	t = t + '';
	var decpoint = t.indexOf('.');
	var decimal = '';
	if( decpoint > -1)
	{
		decimal = t.substr(decpoint);
		t = t.substr(0,decpoint);
		if(decimal.length < 3) decimal += '0';
	}

	if(!delim) delim = '\'';
	var base = t.length%3;
	var td;
	if(base == 0) td = '';
	else if(t.length < 3) td = t;
	else td = t.substr(0, base) + delim;
	for(var i=base; i < t.length; i = i+3)
	{
		td += t.substr(i, 3);
		if(i+3 < t.length && t.length > 3) td += delim;
	}
	return td + decimal;
}


/**
 * sets a cookie
 * Function found at Doc JavaScript -
 * @link http://www.webreference.com/js/column8/functions.html
 * @param name the name of the cookie
 * @param value the value of the cookie
 * @param expires the expiration date (defaults to end of session)
 * @param path path for which the cookie is supposed to be valid (defaults to current path)
 * @param domain domain for wich the cookie is supposed to be valid (defaults to current domain)
 * @param secure indicates if a secure transmission is required
 * an argument defaults when it is assigned null as a placeholder
 * a null placeholder is not required for trailing omitted arguments
 **/
function setCookie(name, value, expires, path, domain, secure)
{
	var curCookie = name + "=" + escape(value) +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
	document.cookie = curCookie;
}


/**
 * gets a cookie
 * Function found at Doc JavaScript -
 * @link http://www.webreference.com/js/column8/functions.html
 * @param name the name of the cookie to fetch
 * @return String cookie value
 **/
function getCookie(name)
{
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1)
	{
		begin = dc.indexOf(prefix);
		if (begin != 0)
			return null;
	}
	else
		begin += 2;
	var end = document.cookie.indexOf(";", begin);
	if (end == -1)
		end = dc.length;
	return unescape(dc.substring(begin + prefix.length, end));
}