// JavaScript Document


var req = createXMLHttpRequest();
// set a global variable to determine where the browser is AJAX-ready
var isAJAX;

function setAJAXStatus(bool) {
	isAJAX = bool;
}

function getAJAXStatus() {
	return isAJAX;
}

// create the connection object, based on browser
function createXMLHttpRequest() {
	var ua;
	
	if(window.XMLHttpRequest) {
		try {
			ua = new XMLHttpRequest();
			setAJAXStatus(true);
	    } catch(e) {
			ua = false;
	    }
	} else if(window.ActiveXObject) {
		try {
			ua = new ActiveXObject("Microsoft.XMLHTTP");
			setAJAXStatus(true);
		} catch(e) {
			ua = false;
	    }
	}else {
		// BROWSER DOES NOT SUPPORT AJAX
		// (most likely an old browser, or Opera < 8.0)
		setAJAXStatus(false);
	}
	return ua;
}

/* send request to server-side page based on parameter
	- add_doc[search]/rem_doc[search]
	- add_vol[search]/rem_vol[search]
	- add_ppl[search]/rem_ppl[search]
	- add_placesearch/rem_placesearch
	- find_rec

	Note: any page that calls this function *must* have a handleResponse function to perform
	the appropriate function (i.e. div layer change or refresh)
*/
function sendRequest(r, p) {
	var url = "datatracker.cfm?" + r;
	if (p && p.length != 0)
		url += "=" + p;
	
	var conn;
	// instantiate a new object if the main object is busy
	if (callInProgress())
		conn = createXMLHttpRequest();
	else
		conn = req;
	
	// set to asynchronous mode
	conn.open('get', url, true);
	conn.onreadystatechange = function() {
		if (conn.readyState == 4) {
			// every page that calls sentRequest must have a way to handle the response
			try {
				handleResponse(r, p);
			}catch (e) {
				window.alert(e);
			}
		}
	}
	conn.send(null);
}

/*	readyStates:
	0 - uninitialized
	1 - loading
	2 - loaded
	3 - interactive
	4 - complete
*/
// check to see if the main object is in use
function callInProgress() {
	switch ( req.readyState ) {
		case 1: case 2: case 3:
			return true;
			break;
		
		// Case 4 and 0
		default:
			return false;
			break;
	}
} 