
/**
 * Create an async Ajax call
 * @param string Endpoint (http://wwww.myexample.org/service)
 * @param string Service name (GeoService)
 * @param string Invoked method (GetCity)
 * @param Object Parameters send to service
 * @param Object Method that is called upon succes
 */
function AjaxRequest(uri, service, method, params, callbackMethod)
{
	// This is the default connection format
	uri += '?service=' + service + '&method=' + method;
	
	_AJAX_REQUESTS[_AJAX_REQUESTS.length] = new Ajax.Request(uri, 
	{
		method: 'post',
		sanitizeJSON: true,
		evalJS: false,
		parameters: params,
		requestHeaders: 
		{
			Accept: 'application/json'
		},
		onSuccess: callbackMethod,
		onFailure: function()
		{
			// Could not reach the endpoint?
			alert("The service is unreachable");
		}
	});
};

/**
 * Caching of JS objects, arrays etc
 */
var Cache = Class.create(
{
	Collection: {},
	Add: function(key, value, subject)
	{
		// Simple adding
		if (!subject) 
		{
			this.Collection[key] = value;
			return;
		}
		
		// Sub colection
		if (!this.Collection[subject]) 
		{
			this.Collection[subject] = {};
		}
		
		this.Collection[subject][key] = value;
	},
	
	Get: function(key, subject)
	{
		// Simple retrieving
		if (!subject) 
		{
			return this.Collection[key];
		}
		
		// Retrieving sub collection
		return this.Collection[subject][key];
	},
	
	Contains: function(key, subject)
	{
		// Simple retrieving
		if (!subject) 
		{
			if (this.Collection[key]) 
			{
				return true;
			}
			
			return false;
		}
		
		// Retrieving sub collection
		if (!this.Collection[subject]) 
			return false;
		
		if (this.Collection[subject][key]) 
		{
			return true;
		}
		return false;
	}
	
});

// Both these Parameters are used by various
// widgets and JS classes!
var _AJAX_DEFAULT_ENDPOINT = '/WebServices/proxy.php';
var _AJAX_REQUESTS = [];
var _CACHE = new Cache();
var _UNIQUEKEY = 0;
