/*************************************************
 * Created by Hedde Bosman aka Tex-nd			*
 * Free to copy under the GPL license			*
 * ( http://www.gnu.org/copyleft/gpl.html )	  *
 *************************************************/

var ns4 = document.layers;
var msie = (navigator.appName=="Microsoft Internet Explorer");
var opera = (navigator.appName=="Opera" || navigator.userAgent.toLowerCase().indexOf("opera") >= 0);

function getElementStyle(target) {
	var s;
	var elem = document.getElementById(target)
	if (elem)
		s = (ns4) ? elem : elem.style;
	else return null;
	return s;
}

function setVisibility(target, show) {
	var s = getElementStyle(target);
	if (s == null || s == undefined) alert("could not find "+target);
	s.visibility = show ? "visible" : "hidden";
	if (show) s.display = "block";
}

function setContent(target, content) {
	var elem = document.getElementById(target);
	if (elem) elem.innerHTML = content;
}

function htmlspecialchars(str) {
	str = ""+str;
	str = str.replace(/&/g,"&amp;");
	str = str.replace(/\"/g,"&quot;"); //"
	str = str.replace(/</g,"&lt;");
	str = str.replace(/>/g,"&gt;");
	return str;
}

function uEnc(str) {
	str = encodeURI(str);
	str = str.replace(/\+/, "%2B");
	str = str.replace("/\\/", "\\\\");
	str = str.replace(/#/, "%23");
	str = str.replace(/&/g,"%26");
	return str;
}

function fEnc(str) { // encode string so that it will work in an input field.
	str = str.replace(/"/, "&#34;"); //"
	return str;
}

function getWindowSize() {
	var windowWidth = 0;
	var windowHeight = 0;
	
	windowWidth = window.innerWidth ? window.innerWidth : 0;
	if (windowWidth == 0) windowWidth = document.documentElement ? document.documentElement.clientWidth : 0;
	if (windowWidth == 0) windowWidth = document.body ? document.body.clientWidth : 0;

	windowHeight = window.innerHeight ? window.innerHeight : 0;
	if (windowHeight == 0) windowHeight = document.documentElement ? document.documentElement.clientHeight : 0;
	if (windowHeight == 0) windowHeight = document.body ? document.body.clientHeight : 0;
	
	return new Array(windowWidth, windowHeight);
}

function getPageSize() {
	var pageWidth=0;
	var pageHeight=0;

	if(document.body && (document.body.scrollWidth || document.body.scrollHeight)) {
		pageWidth  = Math.max(pageWidth, document.body.scrollWidth);
		pageHeight = Math.max(pageHeight, document.body.scrollHeight);
	}
	if(document.body.offsetWidth) {
		pageWidth = Math.max(pageWidth, document.body.offsetWidth);
		pageHeight = Math.max(pageHeight, document.body.offsetHeight);
	}
	if (window.innerWidth && window.innerHeight) {
		pageWidth = Math.max(pageWidth, window.innerWidth);
		pageHeight = Math.max(pageHeight, window.innerHeight);
	}
	if (self.innerWidth && self.innerHeight) {
		pageWidth = Math.max(pageWidth, self.innerWidth);
		pageHeight = Math.max(pageHeight, self.innerHeight);
	}
	var de = document.documentElement;
	if (de) {
		pageWidth = Math.max(pageWidth, de.clientWidth);
		pageHeight = Math.max(pageHeight, de.clientHeight);
	}
	return new Array(pageWidth, pageHeight);
}

function darkScreenResize() {
	s = getElementStyle("darkscreen");

	var dim = getPageSize();
	if (dim[0] == 0) dim[0] = '100%';
	else dim[0] += 'px';
	if (dim[1] == 0) dim[1] = '100%';
	else dim[1] = (dim[1]+20)+'px';
	
	s.width  = dim[0];
	s.height = dim[1];
}

function darkScreen(show) {
	var d = document.getElementById("darkscreen");
	var s = getElementStyle("darkscreen");
	if (show) { // stupid internet explorer
		darkScreenResize();
		s.display = "block";
		darkScreenResize();
		window.onresize = darkScreenResize;
	} else {
		s.display = "none";
		window.onresize = null;
	}
}

function hasRights(t) {
	if (t == "admin") return (userRights=="admin");
	else if (t == "supervisor") return (userRights=="admin"||userRights=="supervisor");
	else return true;
}

/********************************************************
 * Event handling
 */
function myAddEventListener(instance, eventName, listener) {
	var listenerFn = listener;
	if (instance.addEventListener) {
		instance.addEventListener(eventName, listenerFn, false);
	} else if (instance.attachEvent) {
		listenerFn = function() {
			listener(window.event);
		}
		instance.attachEvent("on" + eventName, listenerFn);
	} else {
		throw new Error("Event registration not supported");
	}
	return {
		instance: instance,
		name: eventName,
		listener: listenerFn
	};
}

function myRemoveEventListener(event) {
	var instance = event.instance;
	if (instance.removeEventListener) {
		instance.removeEventListener(event.name, event.listener, false);
	} else if (instance.detachEvent) {
		instance.detachEvent("on" + event.name, event.listener);
	}
}
function myStopEvent(e) {
	if (!e) e = window.event;
	if (e.stopPropagation) {
		e.stopPropagation();
	} else {
		e.cancelBubble = true;
	}
}
/********************************************************
 * DOM modifiers
 */


function getNodeValue(node) {
	if (node) {
		while (node.childNodes.length != 0) {
			node = node.firstChild;
		}
		if (node.nodeValue)
			return node.nodeValue;
		return "";
	}
	return "";
}
function getNamedNode(xml, nodename, index) {
	return xml.getElementsByTagName(nodename)[index];
}
function getNamedNodeValue(xml, nodename, index) {
	node = getNamedNode(xml, nodename, index);
	return getNodeValue(node);
}

/********************************************************
 * DOM table modifiers
 */
// clear a table with id 'id'
function tableClear(id, leave) {
	if (leave == undefined || leave == null) leave = 0;
	var table = document.getElementById(id);
	var start = table.rows.length-1-leave;
	if (start >=0)
		for (i = start; i >= 0; i--)
			table.deleteRow(i);
}

// add a row to table 'id' with the 'content' in an array of strings at 'pos'
function tableAddRow(id, content, pos, thOpt, cellColSpan) {
	var table = document.getElementById(id);
	var p = table.rows.length + pos;
	if (pos >= 0) {
		p = pos;
	}
	// thOpt: 0 = nothing, 1 = th first column, 2 = th All
	if (thOpt == undefined || thOpt == null) thOpt = 0;

	var col = new Array();
	var row = table.insertRow(p);
	for (var i = 0; i < content.length; i++) {
		col[i] = document.createElement( ((thOpt==1&&i==0)||thOpt==2) ? 'th' : 'td' );
		col[i].innerHTML = content[i];
		col[i].className = (thOpt==2?"":(p%2==0?"tr0":"tr1"));
		if (cellColSpan != null && cellColSpan != undefined) {
			col[i].colSpan = cellColSpan[i];
		}
		row.appendChild(col[i]);
	}
	return row;
}
function tableGetRow(id, node) {
	var row = null;
	if ((typeof node == "object") && (typeof node.parentNode != "undefined")) {
		if (typeof node.parentNode.parentNode.rowIndex != "undefined") { // cell
			row = node.parentNode.parentNode.rowIndex;
		} else if (typeof node.parentNode != "undefined") { // row
			row = node.parentNode.rowIndex;
		}
	} else if (node instanceof Number) {
		row = node;
	} else if ((typeof node == 'number') && isFinite(node)) {
		row = node;
	}
	return row;
}
// delete a row from a table 'id' at the position of node
function tableDelRow(id, node) {
	var row = tableGetRow(id, node);
	if (row == null) return;
	document.getElementById(id).deleteRow(row);
}

