/*
 * Ajax toolkit
 * Author: [Ben Griffiths, 20/03/2006]
 */

var httpRequest = false;
var stateHandler = new stateChangeHandler();

function makeAjaxRequest(url, parameters, onSuccess, onFail) {
	httpRequest = false;
	if (window.XMLHttpRequest) {	/* DOM Compliant */
		httpRequest = new XMLHttpRequest();
		if (httpRequest.overrideMimeType) {
			httpRequest.overrideMimeType('text/xml');	/* returns valid xml */
		}
	} else if (window.ActiveXObject) {	/* IE 5/6 */
		try {
			httpRequest = new ActiveXObject('Msxml2.XMLHTTP');
		} catch (e) {
			try {
				httpRequest = new ActiveXObject('Microsoft.XMLHTTP');
			} catch (e) {
				onFail();
				return false();
			}
		}
	}
	if (!httpRequest) {
		onFail();
		return false();
	}

	
	stateHandler.setOnSuccess(onSuccess);
	stateHandler.setOnFail(onFail);
	httpRequest.onreadystatechange = stateHandler.onStateChange;
	httpRequest.open('POST', url + parameters, true);
	httpRequest.send("");
}

function stateChangeHandler() {}
stateChangeHandler.prototype.onStateChange = function () {
	if (httpRequest.readyState==4) { 
		if (httpRequest.status == 200) {
			stateHandler.onSuccess();
		} else {
			stateHandler.onFail();
		}
	}
}
stateChangeHandler.prototype.onSuccess;
stateChangeHandler.prototype.setOnSuccess = function (func) {this.onSuccess = func;}
stateChangeHandler.prototype.onFail;
stateChangeHandler.prototype.setOnFail = function (func) {this.onFail = func;}

function XmlDom() {
	if (window.ActiveXObject) {
		var msActiveXSigs = ["MSXML2.DOMDocument.5.0", "MSXML2.DOMDocument.4.0", "MSXML2.DOMDocument.3.0", "MSXML2.DOMDocument", "Microsoft.XmlDom"];
		for (var i=0; i<msActiveXSigs.length; i++) {
			try {
				var xmlDom = new ActiveXObject(msActiveXSigs[i]);
				return xmlDom;
			} catch (e) {}
		}
		throw new Error ("XML DOM object missing, unable to load vote");
	} else if (document.implementation && document.implementation.createDocument) {
		function FakeDocument() {
			this.documentElement = null;
			this.loadXML = function(str) { 
				var e = document.createElement("dummy");
				e.innerHTML = str.replace(/((<|\&lt;)(\/?))option/g, "$1specialoption")
						.replace(/(((?:<|\&lt;)!\[\CDATA\[)|(\]\](?:>|\&gt;))|((?:<|\&lt;)\?xml.*\?(?:>|\&gt;)))/gi, "");
				this.documentElement = e.getElementsByTagName("*")[0];
				this.documentElement.selectNodes = function() {
					if (arguments.length > 0)
						arguments[0] = arguments[0].replace(/option/g, "specialoption");
					return this.constructor.prototype.selectNodes.apply(this, arguments);
				}
				this.getElementsByTagName = function() {
					return this.documentElement
							.getElementsByTagName.apply(this.documentElement, arguments);
				}
			}
		}
		return new FakeDocument();
	} else {
		throw new Error("Your browser does not support the XML Dom object, unable to load vote");
	}
	
}

/*
 * These functions are to mirror functionality from IE in FF such that 
 * 
 */
if (document.implementation && document.implementation.createDocument) {
	/* add functionality to Document to mirror loadXML in IE */
	Document.prototype.loadXML = function (xml) {
		var parser = new DOMParser();
		var xmlDom = parser.parseFromString(xml, "text/xml");
		
		/* ensure the Document is empty */
		while (this.firstChild) {
			this.removeChild(this.firstChild);
		}
		
		/* populate with new data */
		for (var i=0; i<xmlDom.childNodes.length; i++) {
			var newNode = this.importNode(xmlDom.childNodes[i], true);
			this.appendChild(newNode);
		}
		
	}
	/* add functionality to Document to mirror Document.xml in IE */
	Document.prototype.__defineGetter__("xml", function () {
		var serializer = new XMLSerializer();
		return serializer.serializeToString(this, "text/xml");
	});
	/* add select nodes functionality, from IE, to Element */
	Element.prototype.selectNodes = function (xPath) {
		var evaluator = new XPathEvaluator();
		var result = evaluator.evaluate(xPath, this, null, XPathResult.ORDERED_NONE_ITERATOR_TYPE, null);
		var nodes = new Array();
		if (result != null) {
			var element = result.iterateNext();
			while (element) {
				nodes.push(element);
				element = result.iterateNext();
			}
		}
		return nodes;
	}
	Element.prototype.__defineGetter__("xml", function () {
		var serializer = new XMLSerializer();
		return serializer.serializeToString(this, "text/xml");
	});
}

