/* AJAX functions */
function httpRequest() {
	var request;
	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		request = new XMLHttpRequest();
	} else if (window.ActiveXObject) { // IE
		try {
			request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	return request;
}
/* End AJAX functions */

/* DOM and extended JS function */
Array.prototype.inArray = function (value) {
	var i;
	for (i=0; i < this.length; i++) {
		if (this[i] == value) {
			return true;
		}
	}
	return false;
};
function selectOption(element, value) {
	target = document.getElementById(element);
	//alert(target.options.length);
	for(var i = 0; i < target.options.length; i++) {
		if (target.options[i].value == value) {
			target.selectedIndex = i;
			break;
		}
	}
}
function findAllChildren(parent, ignoreClassName, includeTagName) {
	var nodes = new Array();
	var cNodes = parent.childNodes;
	for(var i = 0; i < cNodes.length; i++) {
		//alert("class: " + cNodes[i].className + ", tag: " + cNodes[i].tagName);
		//alert("includeTagName: " + includeTagName + ", ignoreClassName: " + ignoreClassName);
		if (((cNodes[i].tagName == includeTagName) || (includeTagName == undefined)) && ((ignoreClassName == undefined) || (cNodes[i].className != ignoreClassName))) {
			nodes.push(cNodes[i]);
		}
		if (cNodes[i].hasChildNodes()) {
			nodes = nodes.concat(findAllChildren(cNodes[i], ignoreClassName, includeTagName));
		}
	}
	
	//alert("nodes length: " + nodes.length);
	return nodes;
}
function addLoadEvent(func) {
	/* 
	http://www.dustindiaz.com/top-ten-javascript
	Originally written by Simon Willison and highly adopted by many others as a simple 
	way to add events to trigger after the page has loaded. This of course attaches all 
	your events to the onload event handler which some still see as necessary, 
	nevertheless it does exactly what itŐs supposed to, and does it well.
	*/
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}

function createElementWithName(){}
(function(){
	try {
		var el=document.createElement( '<div name="foo">' );
		if( 'DIV'!=el.tagName || 'foo'!=el.name ){
			throw 'create element error';
		}
		createElementWithName = function( tag, name ){
			return document.createElement( '<'+tag+' name="'+name+'"></'+tag+'>' );
		}
	}catch( e ){
		createElementWithName = function( tag, name ){
		var el = document.createElement( tag );
		// setAttribute might be better here ?
		el.setAttribute("name",name);
		return el;
	}
}
})();
function removeAllChildren(element) {
	if (element.hasChildNodes()) {
    	while (element.childNodes.length >= 1) {
    	    element.removeChild(element.firstChild);
    	} 
	}
}
/* Modified appendChild() that first looks to 
 * see if there are any children with the same 
 * id. If so, the element is not appended.
 */
function appendNewChild(target, newElement) {
	var found = false;
	if (target.hasChildNodes()) {
		for (var i=0; i<target.childNodes.length; i++) {
			if (target.childNodes[i].id == newElement.id) {
				found = true;
				break;
			}
		}
	}
	if (found == false) {
		target.appendChild(newElement);
	}
}
function getForm() {
	var elements = document.getElementsByTagName("FORM");
	return elements[0];
}

/* End DOM and extended JS functions */

/* Animation functions */
function toggleElement(element) {
	if (typeof(element) != "undefined") {
		if (element.style.visibility == "visible") {
			element.style.visibility = "hidden";
		} else {
			element.style.visibility = "visible";
		}
	}
}
function hideElement(element) {
	if (typeof(element) != "undefined") {
		element.style.visibility = "hidden";
	}
}
function showElement(element) {
	if (typeof(element) != "undefined") {
		element.style.visibility = "visible";
	}
}
function toggleDisplay(id,style) {
	var obj = document.getElementById(id);
	if (obj.style.display == "none") {
		showContainer(id, style, false);
	} else {
		hideContainer(id, false, false);
	}
}
function showContainer(id, style, animate, center, e, ref) {
	if (!e) var e = window.event;
	
	//alert("id: "+id);
	var obj = document.getElementById(id);
	//alert("obj: "+obj);
	if ((animate == true) && (obj.style.display == "none")) {
		changeOpac(id, 0);
		obj.style.display = style;
		fadeIn(id,5,100,100);
	} else {
		try {
			obj.style.display = style;
			obj.style.visibility = "hidden";
		} catch(e) {}
	}
	if (center == true) {
		//alert("mouse y: " + e.clientY);
		//alert("obj width: " + obj.offsetWidth);
		
		var x = e.clientX - (obj.offsetWidth / 2);
		//alert("x: " + x);
		//var y = (e.clientY / 2) - (obj.offsetHeight / 2);
		obj.style.top = e.clientY + 10;
		obj.style.left = x;
	}
	obj.style.visibility = "visible";
}
function hideContainer(id, animate, removeChild) {
	//alert("id: "+id);
	if (animate == true) {
		changeOpac(id, 100);
		fadeOut(id,5,100,removeChild);
	} else {
		document.getElementById(id).style.display = "none";
	}
}
function opacity(id, opacStart, opacEnd, millisec, removeChild) { 
	//speed for each frame 
	var speed = Math.round(millisec / 100); 
	var timer = 0; 

	//determine the direction for the blending, if start and end are the same nothing happens 
	if(opacStart > opacEnd) { 
		for(i = opacStart; i >= opacEnd; i--) { 
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
			timer++; 
		}
		setTimeout("fadeEnd('" + id + "'," + removeChild + ", '')",(timer * speed));
	} else if(opacStart < opacEnd) { 
		for(i = opacStart; i <= opacEnd; i++) 
			{ 
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
			timer++; 
		}
		//setTimeout("fadeEnd('" + id + "'," + removeChild + ")",(timer * speed));
	}
}
function changeOpac(opacity, id) { 
	try {
		var object = document.getElementById(id).style;
		object.opacity = (opacity / 100);
		object.MozOpacity = (opacity / 100);
		object.KhtmlOpacity = (opacity / 100);
		object.filter = "alpha(opacity=" + opacity + ")";
	} catch (error) {}
}
function fadeEnd(objId, removeChild, alsoExecute) {
	var obj = document.getElementById(objId);
	//alert("objId: " + objId + " obj: " + obj + " removeChild: " + removeChild + " alsoExecute: " + alsoExecute);
	switch (removeChild) {
		case 2:
			// Remove element from DOM
			obj.parentNode.removeChild(obj);
			break;
		case 1:
			// Hide the element
			obj.style.display = "none";
			break;
		case 0:
			obj.style.backgroundColor = "inherit";
			break;
		case 3:
			obj.style.backgroundColor = "red";
			break;
		case 4:
			// Hide the element
			obj.style.visibility = "hidden";
			break;
	}
	eval(alsoExecute);
}
/* End Animation functions */
function popitup(url,height,width) {
	if (height==null) {height = 200}
	if (width==null) {width = 150}
	
	newwindow=window.open(url,'name','height='+height+',width='+width);
	if (window.focus) {newwindow.focus()}
	return false;
}