/*
*	AJAX Functions
*/

/* -----------------------------------------------------------------------------
*	Call this to create the XMLHTTP Object
*
*	Arguments:
*		method=GET or POST (at the moment)
*		url=url of requested file
*		func=pointer to function to process returned data
*	
*	Returns XMLHTTP object or null on failure
*/
function sendXmlHttpRequest(method, url, arg, txtFunc, xmlFunc) {
	var obj = null;
	try {
		obj = new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari, IE7+
	}
	catch (e) {
		try {
      	obj = new ActiveXObject("Msxml2.XMLHTTP");
      }
   	catch (e) {
      	obj = new ActiveXObject("Microsoft.XMLHTTP");
      }
   }
   if (obj != null) {
		obj.onreadystatechange = function() { onStateChange(obj, txtFunc, xmlFunc); };
		if (method == "POST") {
			obj.open(method, url, true);
			obj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			obj.send(arg);
		}
		if (method == "GET") {
			obj.open(method, url+'?'+arg, true);
			obj.send(null);
		}
	}
	return obj;
}

/* -----------------------------------------------------------------------------
*	Called when onreadystatechange event occurs
*
*	Argument:
*		obj = XMLHttpRequest object
*/

function onStateChange(obj, txtFunc, xmlFunc) {
	try {
		if (obj.readyState == 4) {
			if (obj.status == 200) {
				//alert(obj.getAllResponseHeaders());
				if (txtFunc && txtFunc != null) txtFunc(obj.responseText);
				if (xmlFunc && xmlFunc != null) xmlFunc(obj.responseXML);
			}
		}
	}
	catch(e) {
		alert("AJAX Error: " + e.message);
	}			
}

/*------------------------------------------------------------------------------
*	Function to recursively walk down an XML object starting at [node]
*
*	Arguments:
*		child = current child number - set to zero for top level call
*		node = node to start from - set to root for top level call
*		data = accumulated value of returned data from previous calls - set to zero length sring for top level call
*		level = level within the tree - set to zero for top level call
*
*	Returns:
*		data = string of information about
*/

function walkXml(child, node, data, level) {
	var i, n;	
	if (level>0) {
		data += "|";
		for (i=0; i<level; i++) data += "_"
	}
	n = node.childNodes.length;
	data += "level=" + level + ", ";
	data += "child=" + child + ", ";
	data += "name=" + node.nodeName + ", ";
	data += "type=" + node.nodeType + ", ";
	data += "value=" + node.nodeValue + ", ";
	data += "numChildren=" + n + "<br />\n";
	
	if (n>0)  {
		for (i=0; i<n; i++) {
			data = walkXml(i, node.childNodes.item(i), data, level+1);
		}
	}
	return(data);
}
		
