/* Methods for creating the AJAX object and using it.  The page that includes it
 should also have:
	<!-- 
		dataValue = @@@@<%= data %>@@@@
	-->
	var dataValue = '<%= Utils.toValidJS(data) %>';

	var xmlHttp = getXmlHttp();

 	function callServer() {
		if (xmlHttp) {
			var url = [this jsp page's name];
			var toSend = [URI components];
			callAJAX(url, toSend);
		} else {
			 do traditional submit()
		}
	}

 	function updatePage() {
		if (xmlHttp.readyState == 4) { // ready to continue
			var response = xmlHttp.responseText;
			var dataValue = extractField(response, "dataValue");
			document.getElementById('data').innerHTML = dataValue;
			document.marvin.setMol(dataValue);
			...
		}
	}
*/

// create the AJAX object
function getXmlHttp() {
	/* Create a new XMLHttpRequest object to talk to the Web server
	* Code from
	* http://www-128.ibm.com/developerworks/web/library/wa-ajaxintro1.html
	*/
	xmlHttp = false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	try {
		xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
	} catch (e) {
		try {
			xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
		} catch (e2) {
			xmlHttp = false;
		}
	}
	@end @*/
	if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
		xmlHttp = new XMLHttpRequest();
	}
	return(xmlHttp);
} // getXmlHttp

// send the response to the server
function callAJAX(url, toSend) {
	xmlHttp.open('POST', url, true);
	xmlHttp.setRequestHeader('Content-Type',
			'application/x-www-form-urlencoded')
	xmlHttp.onreadystatechange = updatePage;
	xmlHttp.setRequestHeader('Accept','message/x-jl-formresult')
	xmlHttp.send(toSend);
}

// extract a field from a web page.  We assume it follows ACE conventions,
// which is to have a line (most likely in a comment) that says
// field = 'value'
function extractField(page, field) {
	page = page.substring(page.search(field + ' = @@@@'));
	page = page.substring(page.search('@@@@') + 4);
	page = page.substring(0, page.search('@@@@'));
	page = page.replace(/\\\\S\+\\\\n/g, 'S+'); // MOL needs this
	page = page.replace(/\\n/g, '\n'); // MOL needs this
	page = page.replace(/S\+/g, '\\S+\\n'); // MOL needs this
	page = page.replace(/\\"/g, '"'); // MRV needs this
	page = page.replace(/\\'/g, "'"); // text needs this
	return(page);
} // extractField()
