// threadsafe asynchronous XMLHTTPRequest code
// We use a javascript feature here called "inner functions".
// Using these means the local variables retain their values
// after the outer function has returned. this is useful for
// thread safety, so reassigning the onreadystatechange
// function doesn't stomp over earlier requests.
function ajaxSend(url, callback) {
	function ajaxBindCallback() {
		if (ajaxRequest.readyState == 4) {
			if (ajaxRequest.status == 200) {
				if (ajaxCallback) {
					ajaxAct(ajaxRequest.responseXML);
				} else {
					alert('No callback defined.');
				}
			} else {
				alert("There was a problem retrieving the xml data:\n" + ajaxRequest.status + ":\t" + ajaxRequest.statusText + "\n" + ajaxRequest.responseText);
			}
		}
	}
	// use a local variable to hold our request and callback
	// until the inner function is called...
	var ajaxRequest = null;
	var ajaxCallback = callback;
	// bind our callback then hit the server...
	if (window.XMLHttpRequest) {
		// moz et al
		ajaxRequest = new XMLHttpRequest();
		ajaxRequest.onreadystatechange = ajaxBindCallback;
		ajaxRequest.open("GET", url, true);
		ajaxRequest.send(null);
	} else if (window.ActiveXObject) {
		// ie
		ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
		if (ajaxRequest) {
			ajaxRequest.onreadystatechange = ajaxBindCallback;
			ajaxRequest.open("GET", url, true);
			ajaxRequest.send();
		}
	}
}

function ajaxAct(theData) {
	response = theData.documentElement;
	method = response.getElementsByTagName('method')[0].firstChild.data;
	result = response.getElementsByTagName('result')[0].firstChild.data;
	eval(method + '(result)');
}

function checkEmailExists(response) {
	Readout = document.getElementById('emailResponse');
	Readout.innerHTML = "";
	if (response == 1) {
		Readout.className = "error";
		Readout.innerHTML = "<div style=\"padding: 4px 10px; background: #001A68; color: #ffffff; font-weight: bold;\"><small>A user with this email address already exists on this site. Please use another email address or sign in using the form above. (Forgotten your password? <a href=\"/signin/?page=forgot\" style=\"color: #ffffff\">Get a reminder</a>)</small></div>";
	} else {
		Readout.className = "hidden";
		Readout.innerHTML = "";
	}
}

