/*
 * ajax.js
 *
 * Copyright Rubicon Systems Pty. Ltd.
 *
 * $Log: ajax.js,v $
 * Revision 1.5  2009/11/26 22:55:34  markk
 * fix for error display
 *
 * Revision 1.4  2009/11/11 06:07:41  markk
 * Fix for error handling in ajax calls
 *
 * Revision 1.3  2009/07/20 04:26:13  markk
 * Fix for Javascript libraries
 * Fix for dialog content
 * Fix for chart display
 *
 * Revision 1.2  2009/05/05 01:15:29  markk
 * Fix for displaying trends
 *
 * Revision 1.1  2008/02/04 02:48:19  timm
 * Initial revision.
 *
 */

Object.extend(Ajax,
{
	defaultErrorStr: "There has been an error, please refresh the page and try again.<br/>If this occurs frequently please report it as you may not be configured correctly in the system.",
	
	allowAjaxDueToError: true,
	
	/** Location of the Ajax Agent. */
	agent: '/AjaxAgent',
	
	/** Call the agent with the specified parameters, and place the result into 'targetDiv'.
	 * During execution, 'loadingElement' is triggered to show a loading status.
	 */
	updateDiv: function(loadingElement, targetDiv, params, errorFunc)
	{
		new Ajax.Request(this.agent,
		{
			method: 'post',
			
			parameters: params,
			
			onCreate: function()
			{
				if (loadingElement != null)
					$(loadingElement).addClassName('data-entry-ajax-on');
			},
			
			onComplete: function()
			{
				if (loadingElement != null)
					$(loadingElement).removeClassName('data-entry-ajax-on');
			},
			
			onSuccess: function(transport)
			{
				var response = null;				
				var responseStr = transport.responseText;
				
				if (responseStr != null && responseStr.length > 0)
				{
					var indexOfJSon = responseStr.indexOf("/*-secure-");					
				
					if (indexOfJSon > 0)
					{
						var responseJSonStr = responseStr.substring(indexOfJSon);						
						response = responseJSonStr.evalJSON(true);
					}
					else
					{
						response = responseStr.evalJSON(true);	
					}
				}
				
				if ((response != null) && (response.value != null))
				{
					$(targetDiv).update(response.value);
				}
				else
				{
					errorFunc();
				}
			},
			
			onFailure: function()
			{
				errorFunc();
			}
		});
	},
	
	/** Call the agent with the specified parameters, and call the specified function
	 * with the result returned (when it returns). During execution, 'loadingElement'
	 * is triggered to show a loading status. If there is an error, then 'errorFunc'
	 * is called.
	 */
	loadAndExecute: function(loadingElement, params, func, errorFunc)
	{
		if (!this.allowAjaxDueToError) {
			return;
		}
		new Ajax.Request(this.agent,
		{
			method: 'post',
			
			parameters: params,
			
			onCreate: function()
			{
				if (loadingElement != null)
					$(loadingElement).addClassName('data-entry-ajax-on');
			},
			
			onComplete: function()
			{
				if (loadingElement != null)
					$(loadingElement).removeClassName('data-entry-ajax-on');
			},
			
			onSuccess: function(transport)
			{
				var response = null;			
				var responseStr = transport.responseText;
				
				if (responseStr != null && responseStr.length > 0)
				{
					var indexOfJSon = responseStr.indexOf("/*-secure-");								
					if (indexOfJSon > 0)
					{
						var responseJSonStr = responseStr.substring(indexOfJSon);						
						response = responseJSonStr.evalJSON(true);
					}
					else if (responseStr.indexOf("ERROR: ") == 0)
					{
						var errorStr = (responseStr.length > 7 ? responseStr.substr(7) : defaultErrorStr);
						
						if (errorFunc) { 							
							errorFunc(errorStr);
						}
						else {
							try {
							defaultPageError(errorStr);
							} catch (e) {
								showErrorDialog(defaultErrorStr);
							}
						}
							
						return;
					}
					else
					{
						response = transport.responseText.evalJSON(true);	
					}
				}
				
				if (response != null)
				{
					func(response);
				}
				else
				{
					if (errorFunc) { 							
						errorFunc();
					}
					else {
						try {
						defaultPageError(defaultErrorStr);
						} catch (e) {
							showErrorDialog(defaultErrorStr);
						}
					}
				}
			},
			
			onFailure: function(data)
			{
				errorFunc(data);
			}
		});
	}
});