RPC=new Object();
RPC.New = function(rpcObject,rpcFunction) {
	APIRPC=new apirpc(rpcObject,rpcFunction);
	return APIRPC;
}

function apirpc(rpcObject,rpcFunction) {
	this.rpcObject=rpcObject;
	this.rpcFunction=rpcFunction;
	this.callBack=null;
	this.completed=false;
	this.controller=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 "shop/rpc.php?__object="+this.rpcObject+"&__method="+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.send(this.parseParameters());
			this.interval=window.setInterval('APIRPC.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])+"="+escape(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() {
		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();
		}
	}catch(e) {
		alert(e.message);
		this.xmlRpc = false;
	}
}


