// SVN: $Id: ajax.js 14 2009-03-05 15:34:01Z seanh $

//For the bug in IE
var counter = 0;

/**
 * Creates a new AJAX request
 * @author	Chris Johnson
 * @date		06/23/2008
 *
 * @return	xmlhttp object
 */
function createRequest(){

	if( window.XMLHttpRequest ) {

		return new XMLHttpRequest();

	} else {

		try{

			return(new ActiveXObject("Msxml2.XMLHTTP"));

		} catch(e){ }

		try{

			return(new ActiveXObject("Microsoft.XMLHTTP"));

		} catch(e){ }

	}

	alert("Your browser doesn't support AJAX");

	return(null);
}

/**
 * Opens a connection to the url specified, and parses data according to the dataType given. It then executes the handler argument as a function, passing the parsed data as the parameter
 * An optional fourth argument is a pointer to  a function to be executed before the request opens
 * @author	Chris Johnson
 * @date		06/23/2008
 *
 * @param		string	url		The url to open
 * @param		string	dataType	json/text/xml  ... which type of data to receive (and parse accordingly)
 * @param		pointer	handler	The function to execute after the data is received and parsed, passing the parsed data as the first argument to it
 * @param		pointer	preHandler	The function to execute before the request is opened
 */
function openPage(url, dataType, handler){
	counter++;
	(url.match(/\?/)) ? sep="&" : sep="?";
	if(arguments[3]!=null && arguments[3]!='undefined'){
		preHandler = arguments[3];
		preHandler();
	}
	url = url + sep + "counter=" + counter;
	var xmlReq = createRequest();
	xmlReq.open("get", url, true);
	//Passes url at time of definition rather than execution, so even if url gets overwritten it can still be alerted
	xmlReq.onreadystatechange = (function(url, dataType){
			return(function(){
				if(xmlReq.readyState != 4){
					return;
				}
				if(xmlReq.status != 200){
					alert("The request to page \"" + url + "\" failed!");
					return;
				}
				if(dataType=="xml"){
					handlerData = xmlHandler(xmlReq.responseXML);
				}else if(dataType=="json"){
					handlerData = jsonHandler(xmlReq.responseText);
				}else{
					handlerData = xmlReq.responseText;
				}
				handler(handlerData);
			});
		})(url, dataType);
	xmlReq.send(null);
}

/**
 * Parses the JSON data returned by an AJAX call
 * @author	Chris Johnson
 * @date		06/23/2008
 *
 * @param		string	data	The data to parse
 */
function jsonHandler(data){
	if(data!=""){
		eval("responseArray = " + data + ";");
		return(responseArray);
	}else{
		return(null);
	}
}

function stripWhiteSpaceNodes(root){
	for(x = root.childNodes.length - 1; x >= 0; x--){
		node = root.childNodes[x];
		if(node.nodeType == 3 && node.nodeValue.match(/^\s*$/)){
			node.parentNode.removeChild(node);
		}else{
			stripWhiteSpaceNodes(node);
		}
	}
}
/*
Element.prototype.nodes = function(nodeName){
	return(this.getElementsByTagName(nodeName));
};

Element.prototype.getValue = function(){
	if(this.childNodes[0].nodeType==3){
		return(this.childNodes[0].nodeValue);
	}else{
		return(null);
	}
};
*/
function xmlHandler(xmlDoc){
	xmlDoc.documentElement.normalize();
	stripWhiteSpaceNodes(xmlDoc);
	return(xmlDoc);
}