// JavaScript Document
function getXHRObject() {
	var xo = null;
	if ( window.XMLHttpRequest ){
		xo = new XMLHttpRequest();
	} else if( window.ActiveXObject ){
		xo = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return(xo);
}

function runAjaxCall(path,postFunction,postData,noticeID) {
	
	// Generic ajax call will run an XMLHTTPRequest call on the passed path.
	// If postData is passed, call will be set as a post, else it is a get using params
	//	passed on the path value.  postData should be in urlencoded param=value[&param=value]
	//	format (ex. userID=1&userName=This%20User).
	// postFunction is the function called when the contents are returned.
	// The function should accept one parameter: the returned string data.
	var thisXHR = getXHRObject();
	if ( thisXHR == null ) { return(null); }
	else {
		xhrType = "GET";
		// Check to see if this is a post
		if ( postData == null ) { postData = ""; }
		else {
			xhrType = "POST";
		}
		// If there's a notice ID, get that object
		if ( noticeID != null ) {
			var noticelabel = document.getElementById(noticeID);
		}
		
		// Run the call
		thisXHR.open(xhrType,path,true);
		if ( xhrType == "POST" ) {
			// Set post headers
			thisXHR.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			thisXHR.setRequestHeader("Content-length", postData.length);
			thisXHR.setRequestHeader("Connection", "close");	
		}
		thisXHR.onreadystatechange=function() {
			if ( thisXHR.readyState == 2 ) {
				if ( noticelabel != null ) { noticelabel.innerText = 'Request sent'; }
			}
			if ( thisXHR.readyState == 3 ) {
				if ( noticelabel != null ) { noticelabel.innerText = 'Processing...'; }
			}
			if ( thisXHR.readyState == 4 ) {
				// Call the callback function passing the responseText as a parameter
				if ( noticelabel != null ) { noticelabel.innerText = ''; }
				// NOTE: THIS USES EVAL, so make sure functionname is not going to run amok
				respVal = thisXHR.responseText;
				eval(postFunction + "('" + respVal + "')");
			}
		}
		thisXHR.send(postData);
	}
}