/*******************************************************************************\
|	Name:		Footsteps2														|
|	Filename:	rpc.js															|
|	Version:	2.0.0															|
|	Author:		Eric van Blokland												|
|	Project:	Shared library													|
|	Summary:	General rpc javascript functions								|
|																				|
\*******************************************************************************/

var rpc_directory=Array();

function rpc(rpcObject,rpcFunction) {
	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.execute = function() {
		if(this.xmlRpc) {
			this.completed=false;
			this.xmlRpc.open("POST", "rpc.php?__object="+this.rpcObject+"&__function="+this.rpcFunction,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());
			return true;
		}
		else
			return false;

	}
	this.setParameter=function(name,value) {
		for(i=0;i<this.parameters;i++) {
			if(this.parameter_names[i]==name) {
				this.paramter_names[i]=name;
				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() {
		// Should detect carrier and xmlRpc here
		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;
		}
	}
	// 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;
	}

	// Placing ourselve in the rpc directory;
	rpc_directory.push(this);


}
