var request_count = 0;	// Keeps track of the current number of asynchronous requests

/* Sets up an asynchronous http request, and acts upon receiving it.
 * <b>Parameters</b>:
 * url: the server side script to send the data to and get a reply from
 * data: the data to pass, in the form "para1:value1&para2:value2&...paraN:valueN"
 * onReceive: the event handler to call on receipt of a successful reply
 * onFinish: the event handler to call when the http request finishes, regardless of whether or not it succeeds
 *
 * <b>Notes about onFinish</b>
 * It is suggested that in most cases the 'onFinish' handler will not be needed. The null method 'dull' is
 * provided as an empty handler to pass in these cases. onFinish was designed for situations where an interface element
 * should show a busy state whilst the request is in progress, and needs to be returned to its default state regardless
 * of whether or not the request was successful.
 *
 * <b>Non-successful http requests</b>
 * If the server script returns an error code (other than 200) then this method pops up a default notification window
 * that tells the user that the request failed.
 *
 * In addition, sendData supports a particular error syntax, to allow server scripts (for example) to alert users when
 * they enter invalid data. This is done by the server reply beginning with the text '@Error:' (no quotes). Everything
 * following the colon is displayed in a notification box to the user.
 * 
 * In both these situations, onFinish is called, but *not* onReceive. onReceive only runs on a successful http request.
 */
function sendData(url, data, onReceive, onFinish) {
	var http_request;
	if (window.XMLHttpRequest) { // Non-IE browsers
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType("text/html");
		}
	} else if (Window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
	            try {
		       	http_request = new ActiveXObject("Microsoft.XMLHTTP");
		    } catch (e) {}
    		}
  	}
  	if (!http_request) {
  	  	alert("Error: Failed to create XMLHttpRequest");
    		return false;
    	}
   		
	try {
		http_request.open('POST', url, true);
	} catch (e) {
		alert("Error: Unable to open connection to " + url);
		// Quite possibly due to lack of security permissions (eg. accessing things outside of site's domain).
		return;
	}
	addRequest();
	
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
 	http_request.setRequestHeader("Content-length", data.length);
  	http_request.setRequestHeader("Connection", "close");
    	http_request.onreadystatechange = function() {
    		var result;
    		if (http_request.readyState == 4) {
			removeRequest();
    			onFinish(result);
			if (http_request.status == 200) {
				result = http_request.responseText;
				if (result.substr(0,8) == "@Error: ") {
					alert(result.substring(8));
				} else {
					onReceive(result);
				}
			} else {
				alert("There was a problem with the request.");
			}
			http_request.onreadystate = null;
			http_request = null;
		}
    	} 
  	http_request.send(data);    			
}

// Takes the data and sends it off to the specified url, with splat as the event handler
function scrape(url, poststr) {
	sendData(url, poststr, splat, dull);
}

// Receives a reply in the form of: ID: data...
// Updates the element with the specified ID and replaces its innerHTML with the rest of the reply
function splat(result) {
	id = result.substring(0,result.indexOf(':'));
	element = document.getElementById(id);
	element.innerHTML = result.substring(result.indexOf(':')+1);
}	

// A null function to be passed when no event handling is needed
function dull(result) {return;}	

function triggerEvent(event, args) {
	var divs = document.getElementsByTagName("div");
	var i;
	var script;
	var pos;
	for (i = 0; i < divs.length; i++) {
		if ((script = divs[i].getAttribute(event)) != null) {
			if ((pos = script.indexOf('-e-')) > -1) {
				script = script.substring(0,pos) + args + script.substring(pos+3);
			}
			eval(script);
		}
	}
}








