// JavaScript Document
function HttpClient() { }

HttpClient.prototype =  {
	requestType: 'GET',
	isAsync: true,
	xmlhttp:false,
	callback:false,
	onSend: function() {
	//	var status = document.getElementById('HttpClientStatus'); 
	//	status.style.display = "block";
	},
	onload:function() {
	//	var status = document.getElementById('HttpClientStatus'); 
	//	status.style.display = "none";
	},
	onError:function(error) {
		alert(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) {
				
				}
			}
			if(!success) {
				throw new Error("Unable to create HttpRequest.");
			}
		}
	},
	makeRequest: function(url,payload) {
		if(!this.xmlhttp) {
			this.init();
		}
		
		this.xmlhttp.open(this.requestType,url,this.isAsync);
		
		if(this.requestType=="POST") {
			this.xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded;carset=UTF-8');
		}
		
		var self = this;
		this.xmlhttp.onreadystatechange = function() {
			self._readyStateChangeCallback();
		}
		
		this.xmlhttp.send(payload);
		if(!this.isAsync) {
			return this.xmlhttp.responseText;
		}
	},
	_readyStateChangeCallback:function() {
		switch(this.xmlhttp.readyState) {
			case 2:
				this.onSend();
				break;
			case 4:
				this.onload();
				if(this.xmlhttp.status==200) {	
					this.callback(this.xmlhttp.responseText);				
				} else {
					this.onError('HTTP Error Making Request: ['+this.xmlhttp.status+']'+this.xmlhttp.statusText);
				}
				break;
		}
	}
}
