
AsyncHTTPRequestList = {};

function AsyncHTTPRequest() {
	this.Init();	
}


AsyncHTTPRequest.prototype.Init = function(debug) {
	this.dialog = false;
	this.done = false;
	
	this.request_name = 'Async_' + Math.round(Math.random() * 10000000);
	this.request = false;

	/*@cc_on
	  @if (@_jscript_version >= 5)
	  	try
	  	{
	  		this.request = new ActiveXObject("Msxml2.XMLHTTP");
	  	}
	  	catch (e)
	  	{
	  		try
	  		{
	  			this.request = new ActiveXObject("Microsoft.XMLHTTP");
	  		}
	  		catch (E)
	  		{
	  			this.request = false;
	  		}
	  	}
	  @else
	  	this.request = false;
	  @end @*/

	if (!this.request && typeof XMLHttpRequest != 'undefined')
	{
		try
		{
			this.request = new XMLHttpRequest();
		}
		catch (e)
		{
			this.request = false;
		}
	}	
	
	AsyncHTTPRequestList[this.request_name] = this;
	this.request.onreadystatechange = new Function("AsyncHTTPRequestList['"+this.request_name +"'].OnReadyStateChange()");
	//this.request.onerror = new Function("AsyncHTTPRequestList['"+this.request_name +"'].OnError()");
}


AsyncHTTPRequest.prototype.Open = function(mode, url, post_data, content_type){
	if(this.callback_object && this.callback_delay_function_name) {
		this.callback_object[this.callback_delay_function_name]('start');
	}
	this.request.open(mode, url, true);
	if(mode == 'GET'){
		this.request.send(null);
	}
	else {
		if(!content_type) content_type = 'application/x-www-form-urlencoded';
		this.request.setRequestHeader('Content-type', content_type);
		this.request.send(post_data);
	}
}
	
AsyncHTTPRequest.prototype.SetCallback = function(target_object, target_function_name, target_delay_function_name){
	this.callback_object = target_object;
	this.callback_function_name = target_function_name;
	this.callback_delay_function_name = target_delay_function_name; //Finally, not implemented
	
}
	
AsyncHTTPRequest.prototype.OnReadyStateChange = function(){
		
	if(this.request.readyState == 4){		
		this.done = true;
		if(this.dialog) {
			this.dialog.Close();
			this.dialog = false;
		}
		
		if(this.callback_object && this.callback_delay_function_name) {
			this.callback_object[this.callback_delay_function_name]('end');
		}

		if(this.callback_object && this.callback_function_name) {
			this.callback_object[this.callback_function_name](this.request.responseXML);
		}
		
		delete(this.request);
	}
}

AsyncHTTPRequest.prototype.OnError = function(){
	if(this.callback_object && this.callback_delay_function_name) {
		this.callback_object[this.callback_delay_function_name]('error');
	}
}
