/**
* Utility Funktionen
*
* @author Dirk Tetenberg
* @copyright (c) 2009 by NEXUS COMPONENTS GmbH
* @version 1.0
*/

/**
 * Ermittelt ob es sich bei dem Browser um den Internetexplorer handelt
 *
 * @return bool
 */
function isIE()
{
	return navigator.appName.indexOf("Microsoft") >= 0;
}

/**
 * Ermittelt die Version des Browsers (nur Hauptversion)
 *
 * @return int
 */
function browserVersion()
{
	return parseInt(navigator.appVersion.substring(0, 1));
}

function callUserFunction(func)
{
	if (typeof func == "function") return func();
	else if (typeof func == "string") return eval(func);
}

function formatString()
{
	var result = new String("");
	var args = formatString.arguments;

	if (args.length == 0) return result;

	var result = new String(args[0]);
	for (var i = 1; i < args.length; i++) {
		var pattern = "{" + i + "}";
		while(result.indexOf(pattern) >= 0) {
			var replace = args[i];
			result = result.replace(pattern, replace);
		}
	}

	return result;
}

function strpad(padside, str, minlen, padchar)
{
	var result = new String(str);
	if (typeof padchar != "string") padchar = " ";
	while(result.length < minlen) {
		if (padside == 0) result += padchar;
		else result = padchar + result;
	}

	return result;
}

function strpadleft(str, minlen, padchar)
{
	return strpad(1, str, minlen, padchar);
}

function strpadright(str, minlen, padchar)
{
	return strpad(0, str, minlen, padchar);
}

/**
 * Adds a function to the onload handler chain
 *
 * @param function handler Load handler function
 */
function addLoadHandler(handler)
{
	var oldhandler = window.onload;
	if (typeof oldhandler != "function") {
		window.onload = handler;
	} else {
		window.onload = function() {
			if (oldhandler) oldhandler();
			callUserFunction(handler);
		}
	}
}

function changeLanguage(newlanguage)
{
	var href = self.location.protocol + "//" + self.location.hostname + self.location.pathname + "?";
	var search = self.location.search;
	search = search.substring(1, search.length- 1);
	search = search.split("&");
	search.push(formatString("language={1}", newlanguage));
	for (var i = 0; i < search.length; i++)
	{
		if (search[i] != "")
		{
			item = search[i].split("=");
			href = href + "&" + formatString("{1}={2}", item[0], item[1]);
		}
	}
	self.location.href = href;
}

// #############################################################################
// function to get the absolute top position of a node relative to the window
function getOffsetTop(elm)
{
	var mOffsetParent = elm.offsetParent;
	var mOffsetTop = elm.offsetTop;

	while(mOffsetParent)
	{
		mOffsetTop += mOffsetParent.offsetTop;
		mOffsetParent = mOffsetParent.offsetParent;
	}

	return mOffsetTop + (MSIE4 ? (parseInt(fetch_object("controlbar").currentStyle.borderLeftWidth) + 2) : 0);
}

// #############################################################################
// function to get the absolute left position of a node relative to the window
function getOffsetLeft(elm)
{
	var mOffsetLeft = elm.offsetLeft;
	var mOffsetParent = elm.offsetParent;

	while(mOffsetParent)
	{
		mOffsetLeft += mOffsetParent.offsetLeft;
		mOffsetParent = mOffsetParent.offsetParent;
	}

	return mOffsetLeft + (MSIE4 ? (parseInt(fetch_object("controlbar").currentStyle.borderLeftWidth) + parseInt(fetch_object("controlbar").currentStyle.paddingLeft)) : 0);
}