//////////////////////////////////////////////
// Data Controller Helper
//////////////////////////////////////////////
// 
// The data controller is a small helper  
// object that listens for requests from other  
// classes and performs an AJAX call.  This  
// class is most likely not needed, and should  
// be replaced with the Prototype JS  
// AJAX.Request object, which is actually  
// used in this class anyway.
//
//////////////////////////////////////////////

var DataController = Class.create();
DataController.prototype = {
	
ajaxRequest: null,
callbackobject: null,
//
// Constructor
initialize: function()
	{
		document.observe('DataController:Request',				this.Request.bindAsEventListener(this));
		document.observe('DataController:Cancel',					this.Cancel.bindAsEventListener(this));
	},
	
	
//AJAX Request
Request: function(e)
	{
		var params = e.memo;
		
		if (this.ajaxRequest!=null)
		{
			//cancel previous ajax call.
			//perhaps this should be pooled in some way.
			try
			{
				this.callbackobject = null;
				this.ajaxRequest.transport.abort();
			}
			catch (e)
			{
			}
		}		
		
		var thisObj = this;
		
		var requestUrl = params.url;
		var callBackObject = params.sender;
		this.callbackobject = params.sender;
		var callBackID = params.id;
		var requestType = params.type || "GET";
		var postString = params.postvars || '';
		
		var returnParameter = null;
		if (params.returnParameter)
		{
			returnParameter = params.returnParameter
		}
		
		
		
		this.ajaxRequest = new Ajax.Request(requestUrl, {
		  requestType: 'get',
		  parameters: postString,
		  onSuccess: function(transport) 
		  {			
		  		thisObj.ajaxRequest = null;
				if (thisObj.callbackobject!=null)
				{
			  		thisObj.callbackobject.HandleResponse(callBackID, transport.responseText, returnParameter);
					//thisObj.callbackobject = null;
				}
		  },
		  onFailure: function(transport) {
		  	// Try to tell the object that the operation failed
		  	try
		  	{
		  		//thisObj.ajaxRequest = null;
				  //thisObj.callbackobject = null;
				  //var transport2 = transport;	
				  
			  	thisObj.callbackobject.HandleError(callBackID, transport.status);
		  	}
		  	catch(e) {}
			  		  
		  }
		});												
		
		
		
	},
	
//Cancel the AJAX Request
Cancel: function(e)
	{
		var sender = e.memo;
		if (this.ajaxRequest!=null)
		{
			try
			{
				this.callbackobject = null;
				this.ajaxRequest.transport.abort();
			}
			catch (e)
			{
				alert(e);
			}
		}
	}
		
	
};

