

function RPCallbackGroup(successCallback, loadingCallback, failureCallback)
{
	
	this.successCallback = successCallback;
	this.loadingCallback = loadingCallback;
	this.failureCallback = failureCallback;

	this.getAsyncSuccessCallback = function()
	{		

		var instance = this;

		var asyncSuccessCallback = 
		function(o)
		{
			var responseText = o.responseText;
			var methodOutput = unescape(responseText.split("&returningObject=")[0]);
			var methodResult = eval("(" + unescape(responseText.split("&returningObject=")[1]) + ")" );
			instance.successCallback(methodOutput, methodResult);
		};

		return asyncSuccessCallback;
	}	

	this.getAsyncLoadingCallback = function()
	{
		var instance = this;
		var asyncLoadingCallback =
		function(o)
		{
			instance.loadingCallback();
		};
		
		return asyncLoadingCallback;
	}

	this.getAsyncFailureCallback = function()
	{
		var instance = this;
		var asyncFailureCallback = 
		function(o)
		{
		
			// or, some what ever. refactor this!!
			var responseText = o.responseText;
			instance.failureCallback(responseText);
		};

		return asyncFailureCallback;
	}
}



/****

@ Created : 23.06.2008
@ Last Modified : 28.06.2008
@ Author : Ali "Ximath" Çevik

RPCaller class is written as a part of RPCJS2PHP.

This class simply makes the call to the server in order to call the desired procedure from callableObj.

The callableObj must be instance of RemoteObject (defined in remoteobject.class.php)

After instantiating RPCaller with resultDiv and callableObj; the javascript object -- callableObj consists of all the functions
defined in its class file in php. Afterwise, by calling procedures of callableObj in JavaScript, simply, corresponding php procedure is
called.

****/

function RPCaller(callbackGroup, callableObj)
{											

	// constant
	this.REQUEST_URL = "./jsonrpc/rpc.php";
	
	this.callableObj = callableObj;
	this.callbackGroup = callbackGroup;

	this.cachingMechanism = null;
	
	this.setCachingMechamism = function(mec)
	{
		this.cachingMechanism = mec;
	}

	this.setCallbackGroup = function(callbackG)
	{
		this.callbackGroup = callbackG;
	}


	/** 
		Retrieve name of each procedure in callableObj and then add it as a
		Javascript procedure with appopriate parameters. 	
	**/
	this.createFunctions = function()
	{
	
		var methodArray = this.callableObj.accessors["procedureListGenerator"].accessors["generatedList"];
		
		for (var i = 0; i < methodArray.length;i++ )
		{
			var methodName = methodArray[i].accessors["name"];
			var methodParamCount = methodArray[i].accessors["paramCount"];

			var params = new Array();
			for (var p = 0;  p < methodParamCount; p++ )
			{
				params[p] = "param" + p;

			}
		
			// we need to hold a reference to appopriate instance of RPCaller
			params[params.length] = "caller";
			
			// content of each Javascript method.
			var jsMethod = new Function(params, "var normalArgs = new Array(); for (var i = 0; i < arguments.length - 1; i++){ normalArgs[i] = arguments[i];} caller.call(" + JSON.stringify(methodArray[i]) + ", normalArgs);" );
			
			// add method to callableObj [javascript object].
			this.callableObj[methodName] = jsMethod;
		}

	

	
	}
	
	/*
	  Make the call to the server side

	*/
	this.call = function(procedureJSONStr, params)
	{
	    // procedure obj
		var procedure = eval(procedureJSONStr);
		
		// instantiate a request object to make AJAX call
		var request = new AsyncRequest();

		request.setCachingMechanism(this.cachingMechanism);

		// URL of the request.
		request.setUrl(this.REQUEST_URL);
		
		// set variables to POST 
		// while escape method urlencodes the string, JSON.stringify method retrieves JSON from an obj.
		
		request.setVariables("robject=" + encodeURIComponent(JSON.stringify(this.callableObj)) + "&rprocedure=" + encodeURIComponent(JSON.stringify(procedure)) + "&params="+ encodeURIComponent(JSON.stringify(params)));
		
		request.setMethod("POST");	
		
		
		request.setSuccessCallback(this.callbackGroup.getAsyncSuccessCallback());
		request.setLoadingCallback(this.callbackGroup.getAsyncLoadingCallback());
		request.setFailureCallback(this.callbackGroup.getAsyncFailureCallback());
		

		// fire the request.
		request.makeRequest();

	}
	
	// constructor	events
	this.createFunctions();

}

