
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 ();



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.substring (300, this.httpRequestObject.responseText.length));
//					document.writeln (this.httpRequestObject.responseText);

//					alert (this.httpRequestObject.responseText);
					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) {
											if (currentNode.hasChildNodes ())
												document.getElementById (currentNode.getAttribute ("domId")).innerHTML = currentNode.firstChild.data;
											else
//												document.getElementById (currentNode.getAttribute ("domId")).innerHTML = 'no data';
												document.getElementById (currentNode.getAttribute ("domId")).innerHTML = '';
//											alert (currentNode.firstChild.data);
										}
										break;
									case 'showDiv' :
										if (currentNode.getAttribute ("domId") != null) {
											if (document.all) {
												var temp = document.all[currentNode.getAttribute ("domId")];

												if (typeof (temp) != 'undefined')
													temp.style.display = 'inline';
											}
											else {
												if (document.getElementById (currentNode.getAttribute ("domId")) != null)
													document.getElementById (currentNode.getAttribute ("domId")).style.display = 'inline';
											}
//											alert (currentNode.firstChild.data);
										}
										break;
									case 'hideDiv' :
										if (currentNode.getAttribute ("domId") != null) {
											if (document.all) {
												var temp = document.all[currentNode.getAttribute ("domId")];

												if (typeof (temp) != 'undefined')
													temp.style.display = 'none';
											}
											else {
												if (document.getElementById (currentNode.getAttribute ("domId")) != null)
													document.getElementById (currentNode.getAttribute ("domId")).style.display = 'none';
											}
//											alert (currentNode.firstChild.data);
										}
										break;
									case 'eval' :
									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;
									// requires cookie.js
									case 'setCookie' :
										var value = currentNode.firstChild.data;
										var cookieName = currentNode.getAttribute ("name");
										var expirationTime = 1 * currentNode.getAttribute ("expiry"); // typecast to an integer
										var cookieDir = currentNode.getAttribute ("cookieDir");
										var cookieDomain = currentNode.getAttribute ("cookieDomain");
										var secure = currentNode.getAttribute ("secure");

										if (cookieName != null)
											set_cookie (cookieName, value, expirationTime, cookieDir, cookieDomain, secure);
										break;
									// requires cookie.js
									case 'deleteCookie' :
										var cookieName = currentNode.getAttribute ("name");
										var cookieDir = currentNode.getAttribute ("cookieDir");
										var cookieDomain = currentNode.getAttribute ("cookieDomain");

										if (cookieName != null)
											delete_cookie (cookieName, cookieDir, cookieDomain);
										break;
									case 'alert' :
										alert (currentNode.firstChild.data);
										break;
									case 'redirect' :
										document.location.href = currentNode.firstChild.data;
										break;
								}
							}
						}
					}

					// 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);
		}
	}
}

