// does some simple AJAX stuff as well as some fun DOM
// manipulation routines


// DOM stuff...


// remove all children of a given document node
function removeAllChildren(aNode) {
	/*while(aNode.hasChildNodes()) {
		aNode.removeChild(aNode.firstChild);	
	}*/
	aNode.innerHTML="";
}

// copy all children of a given document node to another
function copyAllChildren(aFrom,aTo) {
	var child=aFrom.firstChild;
	while(child) {
		aTo.addChild(child);
		child=aFrom.nextChild;	
	}
}


// AJAX stuff...


// gets an AJAX object
function startAjax() {
	var ajax;
	/*@cc_on */
	/*@if(@_jscript_version>=5)
		try {
			ajax=new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				ajax=new 
				ActiveXObject("Microsoft.XMLHTTP");
			} catch(e2) {
				ajax=false;
			}
		}
	}
	/*@end @*/
	if(!ajax && typeof XMLHttpRequest!='undefined') {
		try {
			ajax=new XMLHttpRequest();
		} catch(e) {
			alert("Error: XMLHttpRequest is busted!");
		}
	}
	if(ajax==false) {
		alert("Error: Ajax is busted!");
		// if that fails, what's the plan???
	}
	return ajax;
}


// Gets a aUrl via a given aMethod ("GET","POST", or "HEAD") and
// repaces the document aNode's children with that data.
// A) if aAsync is false, it will wait for the response and return
// whether it worked or not. 
// B) if aAsync is a function(true/false),that will be called when
// data is returned AND the ajax object is returned for reference.
// C) Finally if aAsync is null, this function will just do its thing
var addHeads=true;
function replaceNodeContents(aNode,aUrl,aMethod,aAsync) {
	var ajax;
	var success=false;
	function state() {
		if(ajax.readyState==4) { // done
			if(ajax.status==200) { // and good
				try {
					xml=ajax.responseXML;
					if(xml.getElementByType("body").length>0) {
						xml=xml.getElementByType("body")[0];
						copyAllChildren(xml,aNode);
						head=xml.getElementByType("head")[0];
						if(head) copyAllChildren(head,document.getElementsByName("head")[0]);
					} else {
						aNode.appendChild(document.createElement(xml));
					}
				} catch(e) { // if that failed, we can use plain text
					aNode.innerHTML=ajax.responseText;
				}
				success=true;
			} else {
				aNode.innerHTML=ajax.status+" Resource Not Found: "+aUrl;
			}
			if((aAsync!=null)&&(aAsync!=false))
				aAsync(ajax.status==200);
		}
	}
	removeAllChildren(aNode);
	ajax=startAjax();
	if(ajax) {
		ajax.open(aMethod,aUrl,aAsync);
		ajax.onreadystatechange=state;
		ajax.send(null);
	}
	if(aAsync==null)
		return true;
	if(aAsync!=false)
		return ajax;
	state();
	return success;
}

