oRPC=new Object();
API.RPC=oRPC;

oRPC.length=0;
oRPC.New = function(rpcObject,rpcFunction) {
	API.RPC[this.length]=new api_rpc(this.length,rpcObject,rpcFunction);
	this.length++;
	return API.RPC[this.length-1];

}
oRPC.Get=function(id) {
	if(API.RPC[id])
		return API.RPC[id];
	return false;
}

function api_rpc(rpcId,rpcObject,rpcFunction) {
	this.id=rpcId;
	this.rpcObject=rpcObject;
	this.rpcFunction = rpcFunction
	this.callBack=null;
	this.completed=false;

	this.parameter_names=Array();
	this.parameter_values=Array();
	this.parameters=0;
	this.status=null;
	this.responseText=null;
	this.responseXML=null;

	this.interval=null;

	this.customHref=null;
	this.requestMethod="POST";

	this.getHref=function() {
		if(this.customHref===null)
			return "rpc.php?__object="+this.rpcObject+"&__function="+this.rpcFunction;
		else
			return this.customHref;
	}

	this.setHref=function(href) {
		this.customHref=href;
	}

	this.setRequestMethod=function(method) {
		this.requestMethod=method;
	}

	this.getRequestMethod=function() {
		return this.requestMethod;
	}

	this.execute = function() {
		if(this.xmlRpc) {
			this.completed=false;
			this.xmlRpc.open(this.getRequestMethod(), this.getHref(),true);
			this.xmlRpc.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			this.xmlRpc.setRequestHeader("Cache-Control", "no-cache");
			//this.xmlRpc.setRequestHeader("Connection", "close");
			this.xmlRpc.send(this.parseParameters());
			this.interval=window.setInterval('API.RPC['+this.id+'].change();',100);
			return true;
		}
		else
			return false;

	}
	this.setParameter=function(name,value) {
		for(i=0;i<this.parameters;i++) {
			if(this.parameter_names[i]==name) {
				this.parameter_values[i]=value;
				return;
			}
		}
		this.parameter_names[this.parameters]=name;
		this.parameter_values[this.parameters]=value;
		this.parameters++;
	}
	this.getParameter=function(name) {
		for(i=0;i<this.parameters;i++) {
			if(this.parameter_names[i]==name) {
				return this.parameter_values[i];
			}
		}
		return false;
	}
	this.parseParameters=function() {
		ret='';
		for(i=0;i<this.parameters;i++) {
			if(ret!='')
				ret=ret+"&";
			ret=ret+escape(this.parameter_names[i])+"="+encodeURIComponent(this.parameter_values[i]);
		}
		return ret;
	}
	this.collectForm=function(form) {
		for(i=0;i<form.elements.length;i++) {
			if(form.elements[i].name)
				this.setParameter(form.elements[i].name,form.elements[i].value);
			else if(form.elements[i].id)
				this.setParameter(form.elements[i].id,form.elements[i].value);
		}
	}
	/*
	this.change = function() {
		// Should detect carrier and xmlRpc here
		API.RPC[this.id]._change();

		for(i=0;i<rpc_directory.length;i++) {
			try {
				if(rpc_directory[i])
					rpc_directory[i]._change();
			}
			catch(e) {
				alert(e.message);
			}
		}

	}
	*/


	this.change = function() {
		if(this.xmlRpc.readyState==4 && this.completed==false) {
			this.status=this.xmlRpc.status;
			this.responseText=this.xmlRpc.responseText;
			if(this.xmlRpc.responseXML) {
				this.responseXML=new ActiveXObject("Microsoft.XMLDOM");
  				this.responseXML.async="false";
				this.responseXML.loadXML(this.responseText);
			}
			else {
				parser=new DOMParser();
				this.responseXML = parser.parseFromString(this.responseText,"text/xml");
			}
			if(this.callBack)
				this.callBack(this.status,this.responseXML.documentElement,this.responseText);
			this.completed=true;
			if(this.interval) {
				window.clearInterval(this.interval);
			}
		}
	}
	// Create rpc
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.
	try{
		this.xmlRpc = new ActiveXObject("Msxml2.XMLHTTP");
	} catch(e){
		try{
			this.xmlRpc = new ActiveXObject("Microsoft.XMLHTTP");
		}catch(e){
			this.xmlRpc = false;
		}
	}
	@end @*/
	try{
		if (!this.xmlRpc  && typeof XMLHttpRequest!='undefined') {
			this.xmlRpc  = new XMLHttpRequest();
		}
		//this.change=this.xmlRpc.onreadystatechange=this.change();

	}
	catch(e) {
		alert(e.message);
		this.xmlRpc = false;
	}



}
