function initAJAX() {}
addOnloadEvent(initAJAX);

function makeAJAXRequest(url, eventHandler, responseType, eventArgs) {
	var result = true;
	var httpRequest = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		try {
			httpRequest = new XMLHttpRequest();
			if (httpRequest.overrideMimeType) {
				httpRequest.overrideMimeType('text/xml'); //Deals with Firefox bug
			}
		} catch (e) {}
	} else if (window.ActiveXObject) { // IE
		try {
			httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	
	if (!httpRequest) {
		result = false;
	} else {
		try {
			if (eventHandler != null) {
				httpRequest.onreadystatechange = function() { alertContents(httpRequest, eventHandler, responseType, eventArgs); };
			}
			httpRequest.open('GET', url, true);
			httpRequest.send(null);
		} catch (e) { result = false; }
	}
	
	return result;
}

function alertContents(httpRequest, eventHandler, responseType, eventArgs) {
	if (httpRequest.readyState == 4) {
		if (httpRequest.status == 200) { //HTTP Code 200 == OK
			//Deal with normal event handler
			var alertFunction = eventHandler + "(httpRequest." + responseType + eventArgs + ");"
			eval(alertFunction);
		} else {
			alert('There was a problem with the request.');
		}
	}
}

//Searches through all the children of a DOM node, looking for a child with a matching ID
function getChildById(DOMNode, ID) {
	result = null;
	
	//First get elements
	var children = getChildElements(DOMNode);
	
	try {
		var cont = true;
		for (var i = 0; i < children.length; i++) {
			var tempChild = children[i];
			var tempID = tempChild.getAttribute("id");
			if (tempID == ID) {
				cont = false;
				result = tempChild;
			}
		}
	} catch (e) {}
	
	return result;
}

function getChildElements(DOMNode) {
	var result = new Array();
	var n = 0;
	for (var i = 0; i < DOMNode.childNodes.length; i++) {
		tempNode = DOMNode.childNodes[i];
		if (tempNode.nodeType == 1) { //Node.ELEMENT_NODE
			result[n] = tempNode;
			n++;
		}
	}
	return result;
}

function copyNode(DOMNode) {
	var IEVersion = getInternetExplorerVersion();
	
	//Create new node itself
	var result;
	if (DOMNode.nodeType == 1) { //Node.ELEMENT_NODE
		if (IEVersion < 0) { //Non-IE browser
			result = document.createElement(DOMNode.tagName);
		} else {
			result = document.createElement(DOMNode.xml);
		}
	} else if (DOMNode.nodeType == 2) { //Node.ATTRIBUTE_NODE
	} else if (DOMNode.nodeType == 3) { //Node.TEXT_NODE
		result = document.createTextNode(DOMNode.nodeValue);
	}
	
	//Copy attributes in case they weren't already copied using DOMNode.xml
	if (IEVersion < 0) {
		var attributeList = DOMNode.attributes;
		try { //Some types of nodes have no attributes
			for (var i = 0; i < attributeList.length; i++) {
				var currentAttribute = attributeList[i];
				var attributeName = currentAttribute.nodeName.toLowerCase();
				var actionText = currentAttribute.nodeValue;
				
				//Test to see if we're talking about an event listener
				if (attributeName.substring(0, 2) == "on") {
					try {
						addEvent(result, attributeName.substring(2), function() { eval(actionText); });
					} catch (e) { alert("Error copying HTML data: " + e); }
				} else { //Not an event
					var newAttribute = document.createAttribute(attributeName);
					newAttribute.value = attributeList[i].nodeValue;
					result.setAttributeNode(newAttribute);
				}
			}
		} catch (e) {}
	}
	
	//Recursively copy children
	var childrenList = DOMNode.childNodes;
	for (var i = 0; i < childrenList.length; i++) {
		result.appendChild(copyNode(childrenList[i]));
	}
	
	return result;
}

//Recursively searches upwards in the DOM twoards the root for a node matching a particular string
function matchParentNode(DOMNode, ID) {
	var result = null;
	try {
		if (DOMNode.id == ID) {
			result = DOMNode;
		} else {
			result = matchParentNode(DOMNode.parentNode, ID);
		}
	} catch (e) {} //Probably at root node
	return result;
}

//Restripes a join table with background colors
function restripeTable(DOMNode) {
	var currentColorClass = colorList[0];
	var rowDivCollection = getChildElements(DOMNode);
	for (var i = 1; i < rowDivCollection.length; i++) {
		var cellCollection = getChildElements(rowDivCollection[i]);
		for (var j = 0; j < cellCollection.length; j++) {
			var currentCell = cellCollection[j];
			if (currentCell.nodeName.toLowerCase() == "div") { //We don't want to change the class of anything but div tags
				cellCollection[j].setAttribute(getElementClassAttributeName(), currentColorClass);
			}
		}
		currentColorClass = getNextColorClass(currentColorClass);
	}
}

function getChildIndex(DOMNode) {
	var parentNode = DOMNode.parentNode;
	var childList = getChildElements(parentNode);
	var cont = true;
	var result = -1;
	for (var i = 0; cont && i < childList.length; i++) {
		if (childList[i] == DOMNode) {
			result = i;
			cont = false;
		}
	}
	return result;
}
