	/**
	 * Displays an error as a JQuery UI block
	 *
	 */
	function displayError(arg1,arg2,arg3)
	{
		jQuery.blockUI(arg1.responseText + '<br>',{ width: '80%',top:'10%', left:'20%', color:'#000', backgroundColor:'#ffbfbf'});
	}
	
	/**
	 * Cleans up the UI
	 *
	 */
	function _cleanUpDisplay()
	{
		// Stop all spinners
		stopAllSpinableButtons();	
		
		// Clean up flash display.
		cleanupFlashDiv(true);
		
	}
	
	/**
	 * Display an Error Message
	 * Accessor function to handleError
	 *
	 */
	function _displayMessageOnError(message)
	{
		// Clean up
		_cleanUpDisplay();
		
		// Add the Error
		addError(message);
		
		// Display the Error
		displayFlashDiv();
				
	}
	
	/**
	 * Handles Complete
	 *
	 */
	function handleComplete(XMLHttpRequest, textStatus)
	{
		
	}
	
	/**
	 * Handles if the Ajax is successful. 
	 * A successful response, may contain an invalid JSON response. 
	 *
	 */
	function handleSuccess(json, statusText)
	{

		// Validate JSON 
		if ( validateJSON( json ) )
		{
			// JSON response is valid, parse the response
			parseResponse(json, statusText)  
		
		} 
		else
		{
			// JSON response is not valid
			handleErrorJSON();
		}
	
	}
	
	/**
	 * Validates the JSON  response
	 *
	 * @return Returns True if the JSON response contains no error(s), False otherwise.
	 */
	function validateJSON( json ) 
	{
		// Ensure the JSON response is an object.
		// If not an object it is a string, which is not proper JSON.
		if ( json.length > 0 )
		{
			return false;
		}
			
		return true;
	
	}
	
	/**
	 * Handles a JSON related error(s)
	 *
	 */
	function handleErrorJSON()
	{
		// Clean up
		_cleanUpDisplay();
		
		_displayMessageOnError("An error has occurred while processing your request, please try again. (JSONP100)");
	
	}
	
	
	/**
	 * Handles a HTTP error(s)
	 *
	 */
	function handleErrorHTTP(XMLHttpRequest, textStatus)
	{
			
			if ( XMLHttpRequest.status )
			{
				// Alerts for debugging
				//alert("XMLHttpRequest Ready Status: " + XMLHttpRequest.readyState);
				//alert("XMLHttpRequest Status: " + XMLHttpRequest.status);
				
				// Specific Cases for each HTTP status range.
				
				if ( XMLHttpRequest.status < 200 )
				{
					// HTTP Status Code is in the 1XX range (Information).
					
					// Handle common HTTP Status Codes, and display default Message
					switch ( XMLHttpRequest.status )
					{
						default:
							_displayMessageOnError("An error has occurred while processing your request, please try again. (JSONH"+XMLHttpRequest.status+")");
							break;
					
					}
					
				}
				else if ( XMLHttpRequest.status >= 200 && XMLHttpRequest.status < 300 )
				{
					// HTTP Status Code is in the 2XX range (Success).
					
					// Handle common HTTP Status Codes, and display default Message
					
					switch ( XMLHttpRequest.status )
					{
						default:
							_displayMessageOnError("An error has occurred while processing your request, please try again. (JSONH"+XMLHttpRequest.status+")");
							break;
					
					}
					
				
				}
				else if ( XMLHttpRequest.status >= 300 && XMLHttpRequest.status < 400 )
				{
					// HTTP Status Code is in the 3XX range (Redirection).
					
					// Handle common HTTP Status Codes, and display default Message
					switch ( XMLHttpRequest.status )
					{
						default:
							_displayMessageOnError("An error has occurred while processing your request, please try again. (JSONH"+XMLHttpRequest.status+")");
							break;
					
					}
					
					
				}
				else if ( XMLHttpRequest.status >= 400 && XMLHttpRequest.status < 500 )
				{
					// HTTP Status Code is in the 4XX range (Client Error).
					
					// Handle common HTTP Status Codes, and display default Message
					switch ( XMLHttpRequest.status )
					{
						case ( 404 ):		
							_displayMessageOnError("An error has occurred while processing your request, please try again. (JSONH"+XMLHttpRequest.status+")");
							break;
							
						default:
							_displayMessageOnError("An error has occurred while processing your request, please try again. (JSONH"+XMLHttpRequest.status+")");
							break;
					
					}

				
				}
				else if ( XMLHttpRequest.status >= 500 )
				{
					// HTTP Status Code is in the 5XX range (Server Error).
					
					// Handle common HTTP Status Codes, and display default Message
					switch ( XMLHttpRequest.status )
					{
						default:
							_displayMessageOnError("An error has occurred while processing your request, please try again. (JSONH"+XMLHttpRequest.status+")");
							break;
					
					}

				
				}
				
			}
	
	}
	
	
	
	/**
	 * Pases the JSON response
	 *
	 */
	function parseResponse(json, statusText)  
	{     
		// Clean up flash display.
		if ( json.cleanupFlashDiv == undefined ) 
			cleanupFlashDiv(true);
		else
			cleanupFlashDiv(json.cleanupFlashDiv);
		
		//clean up all old validation errors
		jQuery('div.error').each(function(){_doResetValidationError(jQuery(this));});
	
		//Check to see if there are any errors
		if (json.validationErrors)
		{
			for(i = 0;i < json.validationErrors.length; i++) 
			{		
				flagValidationError(json.validationErrors[i].elementName,json.validationErrors[i].message);						
			}
		}
	
		if (json.flashError)
		{
			for(i = 0; i < json.flashError.length; i++) 
			{
				addError(json.flashError[i]);
			}						
			
			displayFlashDiv();
		}
		else if (json.flashWarning)
		{
			for(i = 0; i < json.flashWarning.length; i++) 
			{
				addWarning(json.flashWarning[i]);
			}	
			
			displayFlashDiv();
		}
		else if (json.flashNotice)
		{
			for(i = 0; i < json.flashNotice.length; i++) 
			{
				addNotice(json.flashNotice[i]);
			}	
			
			displayFlashDiv(true);
		}
		else if (json.redirectUrl)
		{
			window.location = json.redirectUrl;
		}	
		
		// stopAllSpinableButtons is called before parsing the JSON request. 
		// The reason for this is that if the JSON request is invalid/corrupt, the button will continue to spin.
		if (json.stopSpiningButtons)
		{
			stopAllSpinableButtons();
		}

	}  	
 	
	function resetValidationError(callingObject)
	{
		_resetValidationError(callingObject.id);
	}
	
	function _resetValidationError(objectId)
	{
		entryRow = jQuery('#row_for_' + objectId);			
		if (entryRow)
		{
			_doResetValidationError(entryRow);
		}			
		else
		{
			alert ('Error for ' + objectId + ' missing row_for_' + objectId + ' div');
		}			
	}
	
	function _doResetValidationError(entryRow)
	{
		entryRow.removeClass('error');
						
		errorMessageDiv = jQuery('p.errorHint', entryRow);			
		errorMessageDivContent = jQuery('span.errorCopy',errorMessageDiv);
			
		if (errorMessageDiv && errorMessageDivContent)
		{		
			errorMessageDiv.css("display",'none');				
			errorMessageDivContent.html('');
		}
		else
		{
			alert ('Error for ' + entryRow.id + ' missing errorHint P tag');
		}				
	}
	
	function flagValidationError(objectId,message)
	{
		entryRow = jQuery('#row_for_' + objectId);			
		if (entryRow)
		{
			entryRow.addClass('error');
			
			//errorMessageDiv = jQuery('#error_for_' + objectId )			
			errorMessageDiv = jQuery('#error_for_' + objectId )			
			errorMessageDivContent = jQuery('#error_for_' + objectId + ' .errorCopy')			
			if (errorMessageDiv && errorMessageDivContent)
			{		
				/*
				errorMessageDiv.html('<strong>' + message + '</strong>');	
				*/
				
//				errorMessageDivContent.html(message);
				errorMessageDivContent.html('<img alt="' + message + '" title="' + message + '" src="/images/icons/exclamation.png" style="vertical-align:text-bottom;">');
				errorMessageDiv.css("display",'inline');		
				
			}
			else
			{
				alert ('Error for ' + objectId + ' missing error_for_' + objectId + ' div');
			}		
		}
		else
		{
			alert ('Error for ' + objectId + ' missing row_for_' + objectId + ' div');
		}
	}

