/*
 ***************************
 *    Utility functions    *
 *    © Pascal Pfiffner    *
 ***************************
 */

var is_IE = (navigator.appName.indexOf("Explorer") > -1);



// launches the function 'callMeOnLoad' if it is defined
var callMeOnLoad = null;
function checkOnLoad() {
	if(null != callMeOnLoad) {
		callMeOnLoad();
	}
}


// returns the index of an item in an array, -1 if it is not there
function in_array(key, arr) {
	if(arr) {
		for(var i = 0; i < arr.length; i++) {
			if(arr[i] == key)
				return i;
		}
	}
	return -1;
}


// converts hierarchically all nodes into XHTML-elements by their name. supply one tree only.
function convertToXHTML(tree) {
	var xhtml = null;
	
	if(tree) {
		
		// make the top node an xhtml-element
		try {
			if("#text" != tree.nodeName)
				xhtml = document.createElement(tree.nodeName);
			else {
				xhtml = document.createTextNode(tree.nodeValue);
			}
			
			// add the attributes
			if(tree.attributes.length > 0) {
				var poss = new Array('id', 'type', 'name', 'value', 'class', 'style', 'href', 'rel', 'method', 'action', 'for', 'src', 'title', 'alt', 'size', 'checked', 'disabled');
				var handler_poss = new Array('onsubmit', 'onclick', 'onfocus', 'onblur', 'onmousedown', 'onmouseup');
				
				var attr_value = new Array();
				for(var i = 0; i < tree.attributes.length; i++) {
					var attr = tree.attributes[i].name;
					
					// 'normal' attribute
					if(in_array(attr, poss) >= 0) {
						xhtml.setAttribute(((is_IE && ('class' == attr)) ? 'className' : attr), tree.getAttribute(attr));
					}
					
					// event handlers
					else if(in_array(attr, handler_poss) >= 0) {
						if(is_IE) {
							attr_value[attr] = tree.getAttribute(attr) ? tree.getAttribute(attr).replace(/return /ig, "ret_val = ") : null;
							try {
								xhtml[attr] = function() { var ret_val = true; eval(attr_value[attr]); return ret_val; };
								/*if('onsubmit' == attr)
									xhtml.onsubmit = function() { var ret_val = true; eval(attr_value['onsubmit']); return ret_val; };
								else if('onclick' == attr)
									xhtml.onclick = function() { var ret_val = true; eval(attr_value['onclick']); return ret_val; };
								else if('onfocus' == attr)
									xhtml.onfocus = function() { var ret_val = true; eval(attr_value['onfocus']); return ret_val; };
								else if('onblur' == attr)
									xhtml.onblur = function() { var ret_val = true; eval(attr_value['onblur']); return ret_val; };
								else if('onmousedown' == attr)
									xhtml.onmousedown = function() { var ret_val = true; eval(attr_value['onmousedown']); return ret_val; };
								else if('onmouseup' == attr)
									xhtml.onmouseup = function() { var ret_val = true; eval(attr_value['onmouseup']); return ret_val; };*/
							}
							catch(exc) {  }
						}
						else {
							xhtml.setAttribute(attr, tree.getAttribute(attr));
						}
					}
				}
			}
		}
		catch(ex) {  }
		
		
		// do the same to all childNodes
		if(tree.hasChildNodes) {
			var child = null;
			var bar = tree.childNodes;
			for(var i = 0; i < bar.length; i++) {
				child = convertToXHTML(bar[i]);
				if(child)
					xhtml.appendChild(child);
			}
		}
	}
	
	return xhtml;
}


// returns a generic element
function createGenericElement(type, id, cls, inner) {
	var node;
	try {
		node = document.createElement(type);
	}
	catch (exc) {
		return null;
	}
	
	if(id)
		node.setAttribute('id', id);
	if(cls)
		node.setAttribute(is_IE ? 'className' : 'class', cls);
	if(inner)
		node.innerHTML = inner;
	
	return node;
}


// returns a <a href> -element
function createLink(id, cls, hrf, inner) {
	var node;
	try {
		node = document.createElement('a');
	}
	catch (exc) {
		return null;
	}
	
	if(id)
		node.setAttribute('id', id);
	if(cls)
		node.setAttribute(is_IE ? 'className' : 'class', cls);
	if(hrf)
		node.setAttribute('href', hrf);
	if(inner)
		node.innerHTML = inner;
	node.setAttribute('onclick', 'if(linkClicked(this)){return false;}');
	
	return node;
}


// returns an option-element
function createOptionElement(value, text, selected) {
	var node = document.createElement("option");
	node.value = value;
	node.text = text;
	
	return node;
}


// returns the object, whether you supply an id (string) or an object itself
function getObj(id) {
	var obj = null;
	if(typeof(id) == "string") {
		obj = getById(id) ? getById(id) : null;
	}
	else if(typeof(id) == "object") {
		obj = id;
	}
	
	return obj;
}


// returns the object with the correspondent ID
function getById(id) {
	if(document.getElementById)
		return document.getElementById(id);
	else if(document.all)
		return document.all[id];
	else
		return false;
}


// returns the child-nodes of an element (stripping all #text - nodes)
function getElementNodes(obj) {
	var childs = obj.childNodes;
	var elem = new Array();
	for(var i = 0; i < childs.length; i++) {
		if("#text" != childs[i].nodeName)
			elem.push(childs[i]);
	}
	return elem;
}


// cleans a node from its childs
function cleanNode(node) {
	if(typeof(node) != "object") {
		node = getObj(node);
		if(typeof(node) != "object")
			return false;
	}
	
	while(node.firstChild != null) {
		node.removeChild(node.firstChild);
	}
}


// returns a string which has escaped characters (from XML)
function fromXML(string) {
	if(!string)
		return '';
	
	var conv = string.replace(/&lt;/, "<");
	conv = conv.replace(/&gt;/, ">");
	
	return conv;
}


