function setVisibility(id, show){
	document.getElementById(id).style.visibility = show ? 'visible' : 'hidden';
}

function openUtility(uri){
	openUtility(uri, 800, 600);
}

function openUtility(uri, w, h){
	var util;
	try{
		util = window.open(uri, window.name + '-utility', 'width=' + w + ',hight=' + h);
	}catch(e){
		util = window.open(uri);
	}
	if(!util.opener)
		util.opener = self;
}

function toggleClass(e, hc){
	if(e.className.indexOf(hc) >= 0){
		hc = /\b + hc + \b/;
		e.className = e.className.replace(hc, '');
	}
	else
		e.className = e.className + " " + hc;
}

function changeState(id, getUri, alinkId){
	var alink = document.getElementById(alinkId);
	alink.className = alink.className.replace(/\balink\b/, 'alink-loading');
	alink.className = alink.className.replace(/\balinkopen\b/, 'alink-loading');
	var func_def = '_changeState("' + id + '", "' + getUri + '", "' + alinkId + '");';
	setTimeout(func_def, 0);
}

function _changeState(id, getUri, alinkId){
	var x = document.getElementById(id);
	if(x){
		var alink = document.getElementById(alinkId);
		if(x.style.visibility == 'hidden'){
			x.style.visibility = 'visible';
			x.style.display = '';
			alink.className = alink.className.replace(/\balink-loading\b/, 'alinkopen');
		}
		else{
			x.style.visibility = 'hidden';
			x.style.display = 'none'
			alink.className = alink.className.replace(/\balink-loading\b/, 'alink');
		}
	}
	else{
		var rd = doHttpRequest(getUri);
		if(rd && rd.documentElement){
			if(rd.documentElement.nodeName == 'error')
				alert(rd.documentElement.childNodes[0].nodeValue);
			else{
				var alink = document.getElementById(alinkId);
				var c = rd.documentElement.childNodes;
				var p = alink.parentNode;
				for(var i = c.length-1; i >= 0; --i){
					if(isIE){
						try{
							alink.insertAdjacentHTML('AfterEnd', c[i].xml);
						}catch(e){
							p.insertBefore(document._importNode(c[i], true), alink.nextSibling);
							rewritePage();
						}
					}
					else{
						p.insertBefore(document._importNode(c[i], true), alink.nextSibling);
					}
				}
				alink.className = alink.className.replace(/\balink-loading\b/, 'alinkopen');
			}
		}
		else{
			alert("Internal Server Error - sorry for the inconvenience!");
		}
	}
}

function replaceContent(uri, id, lnk){
	document.getElementById(id).className += ' loading';
	var loaderNode = document.createElement('div');
	loaderNode.setAttribute('id', 'loader');
	document.getElementById('body').appendChild(loaderNode);
	var func_def = '_replaceContent("' + uri + '", "' + id + '", "' + lnk + '");';
	setTimeout(func_def, 0);
}

function _replaceContent(uri, id, lnk){
	var xml = doHttpRequest(uri + "&replaceContent=1");
	if(xml && xml.documentElement){
		var div = document.getElementById(id);
		if(xml.documentElement.nodeName == 'error')
			alert(xml.documentElement.childNodes[0].nodeValue);
		else{
			if(lnk){
				lnk.className = 'active';
			}

			var c = getXMLElement(xml.documentElement, id);
			if(c){
				div.parentNode.replaceChild(document._importNode(c, true), div);
				__replaceUtils(xml.documentElement);
				if(isIE){
					rewritePage();
				}
			}
			else{ //replace only content
				div.innerHTML = xml.xml;
			}
		}
	}
	else
		alert("Internal Server Error - sorry for the inconvenience!");
	document.getElementById(id).className = document.getElementById(id).className.replace(/\bloading\b/, '');
	document.getElementById('body').removeChild(document.getElementById('loader'));
}

var UTILS = new Array("mainNav", "sub_turniereNav"); //XXX this has to be set by the application developer - maybe we can improve this?
function __replaceUtils(el){
	for(var i = 0; i < UTILS.length; ++i){
		var div = document.getElementById(UTILS[i]);
		if(div){
			var c = getXMLElement(el, UTILS[i]);
			if(c){
				div.parentNode.replaceChild(document._importNode(c, true), div);
			}
		}
	}
}

function getXMLElement(xml, id){
	if(xml && id){
		var c = xml.childNodes;
		return getXMLElementById(c, id);
	}
	return 0;
}

function getXMLElementById(nodes, id){
	if(nodes && nodes.length >= 1){
		for(var i = 0; i < nodes.length; ++i){
			var a = nodes[i].attributes;
			if(a && a.length > 0){
				for(var j = 0; j < a.length; ++j){
					if(a[j].nodeName == 'id' && a[j].nodeValue == id){
						return nodes[i];
					}
				}
			}
			var x = getXMLElementById(nodes[i].childNodes, id);
			if(x){
				return x;
			}
		}
	}
	return 0;
}

var m_ref = -1;
function changeTableState(name, getUri, afterId, imgId){
	++m_ref;
	document.getElementById(imgId).src = '/static/img/ajax_loading.gif';
	var func_def = '_changeTableState("' + name + '", "' + getUri + '", "' + afterId + '", "' + imgId + '");';
	setTimeout(func_def, 0);
}

function _changeTableState(name, getUri, afterId, imgId){
	var x = document.getElementsByName(name);
	if(x && x.length > 0){
		var open = false;
		for(var ix = 0; ix < x.length; ++ix){
			var xx = x[ix];
			if(xx.style.visibility == 'hidden'){
				open = true;
				xx.style.visibility = 'visible';
				xx.style.display = '';
			}
			else{
				xx.style.visibility = 'hidden';
				xx.style.display = 'none'
			}
		}
		if(open)
			document.getElementById(imgId).src = '/static/img/arrow_down_on.gif';
		else
			document.getElementById(imgId).src = '/static/img/arrow_right_on.gif';
	}
	else{
		var rd = doHttpRequest(getUri);
		if(rd && rd.documentElement){
			if(rd.documentElement.nodeName == 'error'){
				alert(rd.documentElement.childNodes[0].nodeValue);
				document.getElementById(imgId).src = '/static/img/arrow_down_on.gif';
			}
			else{
				var after = document.getElementById(afterId);
				var c = rd.documentElement.childNodes;
				var p = after.parentNode;
				for(var i = c.length-1; i >= 0; --i){
					p.insertBefore(document._importNode(c[i], true), after.nextSibling);
				}
				if(isIE && m_ref == 0){
					rewritePage(); //this leads to errors! more than one request at a time is not processed :/
				}
				document.getElementById(imgId).src = '/static/img/arrow_down_on.gif';
			}
		}
		else{
			alert("Internal Server Error - sorry for the inconvenience!");
			document.getElementById(imgId).src = '/static/img/arrow_right_on.gif';
		}
	}
	--m_ref;
}

var __sortDown = new Array(); //investigate why this doesn't work in IE (6)
var __sortUp = new Array();
var __aMap = new Array();
function sortDown(uri, column, tableId, a){ //tableId may as well be bodyId
	__aMap[column] = a;
	if(isIE){
		if(document.getElementById(tableId).nodeName.toLowerCase() == 'table')
			document.getElementById(tableId).className += ' loading';
		else
			document.getElementById(tableId).parentNode.className += ' loading';
	}
	else
		document.getElementById(tableId).className += ' loading';
	var func_def = '_sortDown("' + uri + '", "' + column + '", "' + tableId + '");';
	setTimeout(func_def, 0);
}

function _sortDown(uri, column, tableId){
	_sort(uri, column, tableId, false);
}

function sortUp(uri, column, tableId, a){ //tableId may as well be bodyId
	__aMap[column] = a;
	if(isIE){
		if(document.getElementById(tableId).nodeName.toLowerCase() == 'table')
			document.getElementById(tableId).className += ' loading';
		else
			document.getElementById(tableId).parentNode.className += ' loading';
	}
	else
		document.getElementById(tableId).className += ' loading';
	var func_def = '_sortUp("' + uri + '", "' + column + '", "' + tableId + '");';
	setTimeout(func_def, 0);
}

function _sortUp(uri, column, tableId){
	_sort(uri, column, tableId, true);
}

function _sort(uri, column, tableId, up){
	var table = document.getElementById(tableId);
	var sortArray = up ? __sortUp : __sortDown;
	for(var i = 0; i < sortArray.length; ++i){
		if(sortArray[i] == uri + '@' + column){
			_sortUnload(table);
			return;
		}
	}
	var a = __aMap[column];
	var xml = doHttpRequest(uri);
	if(xml && xml.documentElement){
		if(xml.documentElement.nodeName == 'error')
			alert(xml.documentElement.childNodes[0].nodeValue);
		else{
			var c = xml.documentElement.childNodes;
			if(c && c.length >= 1){
				var it = -1;
				var ix = -1;
				for(var i = 0; i < c.length; ++i){
					if(c[i].nodeName == 'table')
						it = i;
					else if(c[i].nodeName == 'tbody') //use tbody as fallback
						ix = i;
				}
				if(it != -1)
					ix = it;
				else{ //we don't have a thead, so deactivate all sortings
					if(a){
						var as = table.parentNode.getElementsByTagName('thead')[0].getElementsByTagName('a');
						for(var ax = 0; ax < as.length; ++ax){
							if(as[ax].className == 'sortDownActive')
								as[ax].className = 'sortDown';
							else if(as[ax].className == 'sortUpActive')
								as[ax].className = 'sortUp';
						}
						a.className = up ? 'sortUpActive' : 'sortDownActive';
					}
				}

				if(isIE){
					if(table.nodeName.toLowerCase() == 'tbody')
						table.parentNode.className = table.parentNode.className.replace(/\bloading\b/, '');
				}

				table.parentNode.replaceChild(document._importNode(c[ix], true), table);
				if(isIE){
					rewritePage();
				}
				sortArray.push(uri + '@' + column);
				var osa = up ? __sortDown : __sortUp;
				for(var i = 0; i < osa.length; ++i){
					if(osa[i] == uri + '@' + column){
						osa.splice(i, 1);
						break;
					}
				}
				return;
			}
			else
				alert(xml.xml);
		}
	}
	else
		alert("Internal Server Error - sorry for the inconvenience!");

	_sortUnload(table);
}

function _sortUnload(table){
	if(isIE){
		if(table.nodeName.toLowerCase() == 'table')
			table.className = table.className.replace(/\bloading\b/, '');
		else
			table.parentNode.className = table.parentNode.className.replace(/\bloading\b/, '');
	}
	else
		table.className = table.className.replace(/\bloading\b/, '');
}

function doPaging(uri, tableId){
	document.getElementById(tableId).className += ' loading';
	var func_def = '_doPaging("' + uri + '", "' + tableId + '");';
	setTimeout(func_def, 0);
}

function _doPaging(uri, tableId){
	var xml = doHttpRequest(uri);
	if(xml && xml.documentElement){
		var table = document.getElementById(tableId);
		if(xml.documentElement.nodeName == 'error')
			alert(xml.documentElement.childNodes[0].nodeValue);
		else{
			var c = xml.documentElement.childNodes;
			if(c && c.length >= 1){
				var it = -1;
				for(var i = 0; i < c.length; ++i){
					if(c[i].nodeName == 'table')
						it = i;
				}

				table.parentNode.replaceChild(document._importNode(c[it], true), table);
				if(isIE){
					rewritePage();
				}
				return;
			}
			else
				alert(xml.xml);
		}
	}
	else
		alert("Internal Server Error - sorry for the inconvenience!");
	document.getElementById(tableId).className = document.getElementById(tableId).className.replace(/\bloading\b/, '');
}

function doHttpRequest(uri){
	document.body.style.cursor = 'wait'; //not every browser knows 'progress'
	document.body.style.cursor = 'progress';
	var ret = null;
	var http = getRequest();
	if(http){
		http.open("GET", uri, false);
		http.onreadystatechange = function(){
			if(http.readyState == 4 && http.responseText){
				ret = http.responseXML;
			}
		}
		http.send(null);
		if(!ret){
			ret = http.responseXML;
		}
	}
	document.body.style.cursor = 'auto';
	return ret;
}

//found somewhere on the net...
function getRequest(){
	var xmlhttp = false;
	if(typeof XMLHttpRequest != 'undefined'){
		try{
			xmlhttp = new XMLHttpRequest();
		}catch(e){
			xmlhttp = false;
		}
	}
	if(!xmlhttp && window.createRequest){
		try{
			xmlhttp = window.createRequest();
		}catch(e){
			xmlhttp = false;
		}
	}
	if (!xmlhttp) {
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
				xmlhttp = false;
			}
		}
	}
	return xmlhttp;
}

function rewritePage(){
	var x = document.documentElement.outerHTML;
	var px = window.pageXOffset;
	var py = window.pageYOffset;
	if(document.documentElement.scrollLeft || document.documentElement.scrollTop){
		px = document.documentElement.scrollLeft;
		py = document.documentElement.scrollTop;
	}
	else if(document.body.scrollLeft || document.body.scrollTop){
		px = document.body.scrollLeft;
		py = document.body.scrollTop;
	}
	document.open();
	document.write('<?xml version="1.0" encoding="utf-8"?>');
	document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">');
	document.write(x);
	document.close();
	window.scrollTo(px, py);
	rewritePageX(document.documentElement.childNodes);
}

function rewritePageX(nodes){ //set the name attribute to the id attribute because IE6 looses it
	if(nodes && nodes.length){
		for(var x = 0; x < nodes.length; ++x){
			var node = nodes[x];
			if(node.attributes && node.getAttribute('id')){
				node.setAttribute('name', node.getAttribute('id'));
			}
			rewritePageX(node.childNodes);
		}
	}
}

/* written/inspired by Anthony Holdener
(http://www.alistapart.com/articles/crossbrowserscripting) */
if(!document.ELEMENT_NODE){
	document.ELEMENT_NODE = 1;
	document.ATTRIBUTE_NODE = 2;
	document.TEXT_NODE = 3;
	document.CDATA_SECTION_NODE = 4;
	document.ENTITY_REFERENCE_NODE = 5;
	document.ENTITY_NODE = 6;
	document.PROCESSING_INSTRUCTION_NODE = 7;
	document.COMMENT_NODE = 8;
	document.DOCUMENT_NODE = 9;
	document.DOCUMENT_TYPE_NODE = 10;
	document.DOCUMENT_FRAGMENT_NODE = 11;
	document.NOTATION_NODE = 12;
}

document._importNode = function(node, allChildren){
	switch(node.nodeType){
		case document.ELEMENT_NODE:
			var newNode = document.createElement(node.nodeName);
			/* does the node have any attributes to add? */
			if(node.attributes && node.attributes.length > 0)
				for(var i = 0; i < node.attributes.length; ++i)
					newNode.setAttribute(node.attributes[i].nodeName, node.getAttribute(node.attributes[i].nodeName));
			/* are we going after children too, and does the node have any? */
			if(allChildren && node.childNodes)
				for(var i = 0; i < node.childNodes.length; ++i){
					var nn = document._importNode(node.childNodes[i], true);
					if(nn)
						newNode.appendChild(nn);
				}
			return newNode;
		case document.TEXT_NODE:
		case document.CDATA_SECTION_NODE:
				return document.createTextNode(node.nodeValue);
		case document.ENTITY_REFERENCE_NODE:
				if(isIE){ //workaround broken browser - &nbsp; is ignored...
					if(node.nodeName == 'euro')
						return document.createTextNode(String.fromCharCode(8364));
					else if(node.nodeName == 'uuml')
						return document.createTextNode(String.fromCharCode(252));
					else if(node.nodeName == 'auml')
						return document.createTextNode(String.fromCharCode(228));
					else if(node.nodeName == 'ouml')
						return document.createTextNode(String.fromCharCode(246));
					else if(node.nodeName == 'Uuml')
						return document.createTextNode(String.fromCharCode(220));
					else if(node.nodeName == 'Auml')
						return document.createTextNode(String.fromCharCode(196));
					else if(node.nodeName == 'Ouml')
						return document.createTextNode(String.fromCharCode(214));
					else if(node.nodeName == 'szlig')
						return document.createTextNode(String.fromCharCode(223));
					else
						return document.createTextNode(String.fromCharCode(160));
				}
	}
	return null;
};

var isIE = false;
try{
	if(document.all && navigator.appVersion.indexOf("MSIE") >= 0 && navigator.appVersion.indexOf("6") >= 0){
		isIE = true;
	}
}catch(e){}
