//AJAX Core
function HttpClient(method,async,xmlhttp,result) {
	this.requestType = method;
	this.isAsync = async;
	this.xmlhttp = xmlhttp;
	this.requestResult = result;
} //end HttpClient
HttpClient.prototype = {
	callback:false,
	onSend:false,
	onLoad:false,
	onError:function(error) {
		alert(error.message);
	}/*onError:function(error)*/,
	init:function() {
		try {
			this.xmlhttp = new XMLHttpRequest();
		} catch (e) {
			var XMLHTTP_IDS = new Array('MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP','Microsoft.XMLHTTP');
			var success = false;
			for (var i = 0; i < XMLHTTP_IDS.length && !success; i++) {
				try {
					this.xmlhttp = new ActiveXObject(XMLHTTP_IDS[i]);
					success = true;
				} catch (e) {}
			} //end var i = 0; i < XMLHTTP_IDS.length && !success; i++ FOR
			if (!success) {
				throw new Error('Unable to create XMLHttpRequest.');
			} //end !success IF
		} //end try/catch
	} /*end init:function()*/,
	makeRequest:function(url,payload) {
		if (!this.xmlhttp) {
			this.init();
		} //end !this.xmlhttp IF
		this.xmlhttp.open(this.requestType,url,this.isAsync);
		var self = this;
		if (this.callback != false) {
			this.xmlhttp.onreadystatechange = function() {self._readyStateChangeCallback();}
		} //end this.callback != false IF
		if (this.requestType == "POST") {
			this.xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
		} //end this.requestType == "POST" IF
		this.xmlhttp.send(payload);
		if (!this.isAsync) {
			return this.xmlhttp.responseText;
		} //end !this.isAsync IF
	} /*end makeRequest:function(url,payload)*/,
	_readyStateChangeCallback:function() {
		switch(this.xmlhttp.readyState) {
			case 2:
				if (this.onSend != false) {
					this.onSend();
				} //end this.onSend != false IF
			break;
			case 4:
				if (this.onLoad != false) {
					this.onLoad();
				} //end this.onLoad != false IF
				if (this.xmlhttp.status == 200) {
					if (this.requestResult == 'XML') {
						this.callback(this.xmlhttp.responseXML);
					} else {
						this.callback(this.xmlhttp.responseText);
					} //end this.requestResult == 'XML' IF
				} else {
					this.onError(new Error('HTTP Error Making Request: ['+this.xmlhttp.status+'] '+this.xmlhttp.statusText));
				} //end this.xmlhttp.status == 200 IF
			break;
		} //end this.xmlhttp.readyState SWITCH
	} //end _readyStateChangeCallback:function()
} //end HttpClient.prototype

//DOM Core
function emptyElement(target) {
	while (target.hasChildNodes()) {
		target.removeChild(target.firstChild);
	} //end target.hasChildNodes() WHILE
} //end emptyElement

//Navigation
function swapImage(object,replacement) {
	var target = object.getElementsByTagName("img")[0];
	if (replacement) {
		target.setAttribute("oldsrc",target.src);
		target.src = replacement;
	} else {
		target.src = target.getAttribute("oldsrc");
	} //end replacement IF
} //end swapImage

//Features
function switchFeature(to) {
	for (i in features) {
		document.getElementById("feature_"+features[i]).style.display = (to == features[i] ? "block" : "none");
		var control = document.getElementById("feature_controller_"+features[i]);
		control.style.fontWeight = (to == features[i] ? "bold" : "normal");
		control.style.textDecoration = (to == features[i] ? "none" : "underline");
	} //end i in features FOR
	document.getElementById("feature_title").innerHTML = to+".";
} //end switchFeature

//Orders
function processDemoKey(object) {
	if (object.value.length == 6) {
		var http = new HttpClient("POST",true,false,"XML");
		http.onSend = function() {
			//Update status indicator
			var target = document.getElementById("demo_status");
			emptyElement(target);
			
			var img = document.createElement("img");
			img.setAttribute("src","/ajax_progress.gif");
			target.appendChild(img);
		} //end http.onSend = function()
		http.callback = function(xmlDoc) {
			//Check status
			var success = (xmlDoc.getElementsByTagName("error").length == 0 ? true : false);
			
			//Update information
			if (success) {
				var data = xmlDoc.getElementsByTagName("data")[0];
				document.getElementById("first").value = data.getAttribute("first");
				document.getElementById("last").value = data.getAttribute("last");
				document.getElementById("email").value = data.getAttribute("email");
			} //end success IF
			
			//Update status indicator
			var target = document.getElementById("demo_status");
			emptyElement(target);
			
			var img = document.createElement("img");
			img.setAttribute("src","/ajax_"+(success ? "success" : "error")+".png");
			target.appendChild(img);
		} //end http.callback = function(xmlDoc)
		http.makeRequest("/ajax/demo_lookup.php","key="+parseInt(object.value));
	} //end object.value.length == 6 IF
} //end processDemoKey