// RnD ajax Lib Harm@mattmo.nl	10-2006

var READYSTATE_UNINIT 		= 0;// = uninitialized
var READYSTATE_LOADING 		= 1;// = loading
var READYSTATE_LOADED		= 2;// = loaded
var READYSTATE_INTERACTIVE 	= 3;// = interactive
var READYSTATE_COMPLETE 	= 4;// = complete
var ASYNC					= true;
var SYNC					= false;
var onStateCompleted;
var onStateProgress;
var onStateError;
var HTTP_METHOD				= "post";
var currentXMLHTTPObj;
var busy					= false;
var old_ref;

function RunAJAX(ref, onCompleted, onLoading, onError) {
	if (busy) {
		if (ref == old_ref) {
			currentXMLHTTPObj.abort();
		}
	}
	if(onCompleted == null) {
		onStateCompleted = function(sender) { }; 
	} else {
		onStateCompleted = onCompleted;
	}

	if(onLoading == null) {
		onStateProgress = function(sender) { }; 
	} else {
		onStateProgress = onLoading;
	}

	if(onError == null) {
		onStateError = onError;
	} else {
		onStateError = function(code, message) { };
	}

	var xmlhttp=false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.
	 try {
	  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
	  try {
	   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	  } catch (E) {
	   xmlhttp = false;
	  }
	 }
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}
	if (!xmlhttp && window.createRequest) {
		try {
			xmlhttp = window.createRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}
	
	xmlhttp.open(HTTP_METHOD, ref, ASYNC);
	
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == READYSTATE_COMPLETE) {
			if (xmlhttp.status == 200) {
				onStateCompleted(xmlhttp);
			} else {
				onStateError(xmlhttp.status, xmlhttp.statusText);
			}

			
		} else if (xmlhttp.readyState == READYSTATE_LOADING) {
			onStateProgress(xmlhttp);
		} 

	}
	busy = true;
	old_ref = ref;
	xmlhttp.send(null);
	currentXMLHTTPObj = xmlhttp;
}



function getXMLNodes(myXpathString, xmlObj) {
	var expr = xpathParse(myXpathString);
	var nodes = expr.evaluate(new ExprContext(xmlObj));
	var result = nodes.nodeSetValue();	
	
	return result;
}