/**
 * @author Anders Mattson
 */
 
MAD={};
MAD.rpc_array=[];
MAD.rpc_index=0;

MAD.Request = function(){

	this.connection;
	this.callback=null;

	try{
		this.connection = new ActiveXObject('Msxml2.XMLHTTP');
	}
	catch(e){
		try{
			this.connection = new ActiveXObject('Microsoft.XMLHTTP');
		}
		catch(e){
			this.connection = new XMLHttpRequest();
		}
	}

	this.sendRequest=function(sType, sUrl, async, cbfunc, sData){
		this.callback = cbfunc;
		this.connection.open(sType,sUrl,async);
		if(ActiveXObject)
			if (async)	this.connection.onreadystatechange=new Function('MAD.readyStateChange("'+this.rpc_index+'");');
		else
			if (async) this.connection.onload=new Function('MAD.readyStateChange("'+this.rpc_index+'");');
		if(sData && sType=='POST') this.connection.send(sData);
		else this.connection.send();
		if (async) return true;
	
		this.processResponse();
		return this.xml;
	}

	this.processResponse=function(){
		if(this.callback) return this.callback(this.connection.responseText);
		return true;
	}

	this.abort=function(){
		this.connection.abort();
	}

	this.rpc_index=MAD.rpc_index++;
	MAD.rpc_array[this.rpc_index]=this;
	return this;
}

MAD.readyStateChange=function(rpc_index){
	var rpc=MAD.rpc_array[rpc_index];

	if(ActiveXObject){
		if(rpc.connection.readyState==4){
			rpc.processResponse();
		}	
	}
	else rpc.processResponse();
}

MAD.Task = new MAD.Request();

MAD.Task2 = new MAD.Request();
