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


// 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('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('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;
}


// converts hierarchically all nodes into XHTML-elements by their name. supply one tree only.
function convertToXHTML(tree) {
	var xhtml = null;
	
	if(tree) {
		try {
			// make an xhtml-element out of the given element...
			if("#text" != tree.nodeName)
				xhtml = document.createElement(tree.nodeName);
			else {
				xhtml = document.createTextNode(tree.nodeValue);
			}
			
			// add the attributes
			if(tree.hasAttributes) {
				var poss = new Array('id', 'type', 'name', 'value', 'class', 'style', 'href', 'method', 'action', 'onsubmit', 'onclick', 'for', 'src', 'title', 'alt', 'size', 'checked', 'disabled');
				
				for(var i = 0; i < poss.length; i++) {
					var attr = poss[i];
					if(tree.hasAttribute(attr))
						xhtml.setAttribute(attr, tree.getAttribute(attr));
				}
			}
		}
		catch(ex) {  }
		
		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 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;
}

