/*
 * AjaxObject is a hypothetical object that encapsulates the transaction
 *     request and callback logic.
 *
 * handleSuccess( ) provides success case logic
 * handleFailure( ) provides failure case logic
 * processResult( ) displays the results of the response from both the
 * success and failure handlers
 * call( ) calling this member starts the transaction request.
 */

var SID = Math.random();

var Ajax =
{
	handleFailure:function(o)
	{
		setTimeout(function(){
			if (Ajax.t)
				Ajax.t.innerHTML = DEFAULT_ERR_MSG;
			else if (el("msg"))
				el("msg").innerHTML = DEFAULT_ERR_MSG;
			Ajax.callFcn();
		},500);
	},
	
	updateContent:function(o)
	{
		setTimeout(function(){
			if (Ajax.t) 
				Ajax.t.innerHTML = o.responseText;
			else if (Ajax.json)
				JSON = o.responseText;
			Ajax.callFcn();
		},500);
	},
	
	/*
	 *	Function:	startRequest(string[,string,function])
	 *		url:	URL
	 *		t:		DOM element id into which contents will be rendered
	 *		f:		Function to call upon success or failure of call
	 */
	startRequest:function(url,t,f,json)
	{
		this.f = false;
		if (typeof json == "undefined")
			this.json = false;
		else
			this.json = json;
		if (typeof f != "undefined")
			this.f = f;
		if (typeof t == "string")
			this.t = el(t);
		else
			this.t = t;
		var qsChar = (url.indexOf("?") == -1) ? "?" : "&";
		this.xhr = YAHOO.util.Connect.asyncRequest("GET", url + qsChar+"sid=" + SID, callback);
		if (typeof LOADING != "undefined" && this.t != false)
			el(t).innerHTML = LOADING;
		else
			el("main-content").innerHTML = LOADING;
	},
	
	callFcn: function()
	{
		if (Ajax.f[0])
			for (var i = 0; i < Ajax.f.length; i++)
				Ajax.f[i]();
		else
			Ajax.f();
	}
};

/*
 * Define the callback object for success and failure
 * handlers as well as object scope.
 */
var callback =
{
	success:Ajax.updateContent,
	failure:Ajax.handleFailure,
	timeout:10000,
	scope:Ajax
};