
function ajaxManager () {

	this.ajaxHandler = null;
	this.ajaxHandlers = new Array ();	// a storage array for the ajaxHandler objects (so the onreadystatechange function can reference itself)

	// specify an ajax handler to use
	this.use_ajax_handler = function (ajaxHandlerName) {

		// if the user has specified a name for this ajax handler
		if (typeof (ajaxHandlerName) == 'string') {

			if (typeof (this.ajaxHandlers[ajaxHandlerName]) == 'object') {
//				alert ('using existing ajax handler');
				this.ajaxHandler = this.ajaxHandlers[ajaxHandlerName];
			}
			else {
//				alert ('creating new ajax handler');
				this.ajaxHandler = new ajaxHandler (ajaxHandlerName);
			}
		}
		// or if they havn't just create a new ajax handler with a random name
		// (used for set and forget http requests)
		else {
//			alert ('creating anonymous ajax handler');
			this.ajaxHandler = new ajaxHandler (false);
		}
	}
}
ajaxManager = new ajaxManager ();
/**
ajaxManager.use_ajax_handler ('bob');
alert ('ahoy ' + ajaxManager.ajaxHandler.myName);
ajaxManager.use_ajax_handler ('bob2');
alert ('ahoy2 ' + ajaxManager.ajaxHandler.myName);
ajaxManager.use_ajax_handler (false);
alert ('ahoy2 ' + ajaxManager.ajaxHandler.myName);
ajaxManager.use_ajax_handler ('bob');
alert ('ahoy3 ' + ajaxManager.ajaxHandler.myName);
/**/
function ajaxHandler (myName) {

	this.myName = myName;

	// if the caller code 
	if (typeof (myName) == 'boolean')
		// generate a random name for this object's httpRequestObject,  stored in the global httpRequestObjects array
		this.myName = Math.random ();

	// store the instance of this object for reference later
	ajaxManager.ajaxHandlers[this.myName] = this;

	// specify a time in ms for the http request to be repeated
	// 0 is off
	this.timeoutDelay = 0;
	this.timoutId = 0;

	// 
	this.uri = '';
	this.requestMethod = 'GET';





	// httpRequestObject
/**	if (navigator.appName == "Microsoft Internet Explorer")
		httpRequestObjects[myName] = new ActiveXObject ("Microsoft.XMLHTTP");
	else
		httpRequestObjects[myName] = new XMLHttpRequest ();
/**
	if (window.XMLHttpRequest)
		httpRequestObjects[myName] = new XMLHttpRequest ();
	else
		httpRequestObjects[myName] = new ActiveXObject ("Microsoft.XMLHTTP");
/**/

	//	http://developer.mozilla.org/en/docs/AJAX:Getting_Started
	var httpRequestObject = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		this.httpRequestObject = new XMLHttpRequest ();
		if (this.httpRequestObject.overrideMimeType) {
			this.httpRequestObject.overrideMimeType ('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			this.httpRequestObject = new ActiveXObject ("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				this.httpRequestObject = new ActiveXObject ("Microsoft.XMLHTTP");
			}
			catch (e) {}
		}
	}



	// find out the myName of this ajaxHandler object
	this.get_name = function () {
		return this.myName;
	}
	// set how long to wait before the next automatic request should be made
	this.set_timeout = function (timeoutDelay) {
		this.timeoutDelay = timeoutDelay;
	}
	// stop the next timeout http requet
	this.stop_timeout = function () {
		this.timeoutDelay = 0;
	}

	// make an http request
	this.make_http_request = function (uri, method) {
		this.internal_make_http_request (false, uri, method);
	}
	// 
	this.internal_make_http_request = function (requestedByTimeout, uri, requestMethod) {
		// if the timeout has been cancelled,  don't make a new http request
		if ((requestedByTimeout) && (this.timeoutDelay == 0)) {
			if (typeof (this.timeoutId) != 'undefined')
				clearTimeout (this.timeoutId);
			return false;
		}

		if (typeof (uri) != 'undefined')
			this.uri = uri;
		if (typeof (requestMethod) != 'undefined')
			this.requestMethod = requestMethod.toUpperCase ();

		// if there's a request already occuring,  stop it before starting a new one
		// Uninitialised or Completed
		if ((this.httpRequestObject.readyState != 0) && (this.httpRequestObject.readyState != 4))
			this.httpRequestObject.abort ();

//		this.httpRequestObject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		this.httpRequestObject.open (this.requestMethod, this.uri, true);
		eval ("this.httpRequestObject.onreadystatechange = function () { ajaxManager.ajaxHandlers[\'" + this.myName + "\'].process_http_response (); };");
		this.httpRequestObject.send (null);
	}

	// process the response of an http request
	this.process_http_response = function () {

		try {
			// completed download
			if (this.httpRequestObject.readyState == 4) {
				// http status 200
				if (this.httpRequestObject.status == 200) {
//					alert (this.httpRequestObject.responseText);

					this.httpRequestObject.responseXml;
					var xmldoc = this.httpRequestObject.responseXML;

					// development mode only:
					// display the ajax reply if it wasn't valid xml
					if ((typeof (websiteMode) != 'undefined') && (websiteMode == 'development')) {
						if (xmldoc.childNodes.length == 0)
							document.writeln ('<strong>Warning: invalid xml ajax response:</strong><br />\n<a href="' + this.uri + '">' + this.uri + '</a><br />\n' + this.httpRequestObject.responseText);
					}

					// find the root node
					var count;
					for (count = 0; count < xmldoc.childNodes.length; count++) {
						if (xmldoc.childNodes[count].nodeName == "root") {
							var rootNode = xmldoc.childNodes[count];

							// loop through the nodes and carry out their instructions
							var count2;
							for (count2 = 0; count2 < rootNode.childNodes.length; count2++) {
								var currentNode = rootNode.childNodes[count2];

								switch (currentNode.nodeName) {
									case 'updateDiv' :
										if (currentNode.getAttribute ("domId") != null)
											document.getElementById (currentNode.getAttribute ("domId")).innerHTML = currentNode.firstChild.data;
										break;
									case 'evalJS' :
										eval (currentNode.firstChild.data);
										break;
									case 'enableField' :
										var temp = get_object (currentNode.firstChild.data);
										if (typeof (temp) != 'undefined')
											temp.disabled = false;
										break;
									case 'disableField' :
										var temp = get_object (currentNode.firstChild.data);
										if (typeof (temp) != 'undefined')
											temp.disabled = true;
										break;
								}
							}
						}
					}
/**
					count = 0;
					var currentNode = nodeList.firstChild;
					do {
						alert (currentNode.firstChild.data);
						var currentNode = nodeList.nextSibling;
					}
					while ((currentNode !== null) || (count++ < 10));
/*
					// update div contents
					nodeList = xmldoc.getElementsByTagName ("updateDiv");
					for (count = 1; nodeList.length; count++) {
						currentNode = nodeList.nextNode ();
						if (currentNode.getAttribute ("domId") != null)
							document.getElementById (currentNode.getAttribute ('domId')).innerHTML = currentNode.firstChild.data;
					}

					// update div contents
					nodeList = xmldoc.getElementsByTagName ("evalJS");
					for (count = 1; nodeList.length; count++) {
						currentNode = nodeList.nextNode ();
						alert (currentNode.firstChild.data);
					}
/**
					count = 0;
					do {
						var currentNode = xmldoc.getElementsByTagName ('updateDiv').item (count);
						if (currentNode != null) {
							if (currentNode.getAttribute ('domId') != null)
								document.getElementById (currentNode.getAttribute ('domId')).innerHTML = currentNode.firstChild.data;

						}
//						alert ('"' + currentNode.firstChild.data + '"');
						count++;
					}
					while (currentNode !== null);
/**/
					// set a timeout so the object tries again?
					if (typeof (this.timeoutId) != 'undefined')
						clearTimeout (this.timeoutId);
					if (this.timeoutDelay > 0)
						this.timeoutId = setTimeout ("ajaxManager.ajaxHandlers[" + this.myName + "].internal_make_http_request (true);", this.timeoutDelay);
				}
			}
		}
		catch( e ) {
//			alert('Caught Exception: ' + e.description);
		}
	}
}


