/**
 * @copyright (c) DomainTools, LLC
 * The following code may NOT be copied or distributed and is for reference only.
 */

var Ajax = {
	/* Make the ajax call
	 * @param Object payload */
	callAjax: function (payload)
	{	
		jQuery.ajax({
			
			// Defaults
			async: 		(typeof payload.async !== 'undefined') ? payload.async : false,
			cache: 		(typeof payload.cache !== 'undefined') ? payload.cache : false,
			dataType: 	(typeof payload.dataType !== 'undefined') ? payload.dataType : 'json',
			type: 		(typeof payload.type !== 'undefined') ? payload.type : 'POST',
			data:		(typeof payload.data !== 'undefined') ? payload.data : '',
			url: 		payload.url,
			
			beforeSend: function ()
			{
				// Execute user defined method
				if (typeof payload.beforeSend === 'function')
				{
					payload.beforeSend();
				}
			},
			
			success: function (res)
			{
				// Check status of response
				if (typeof res.type !== 'undefined' || typeof res.status !== 'undefined')
				{
					// Execute USER DEFINED method on FAIL
					if (res.type == 'failure' || res.type == 'error')
					{
						if (typeof payload.fail === 'function')
							payload.fail(res);
					}
					
					// Execute USER DEFINED method on SUCCESS
					else if (res.type == 'success')
					{
						if (typeof payload.success === 'function')
							payload.success(res);
					}
				}
			},
			
			// We went boom.
			error: function (XMLHttpRequest, textStatus)
			{
			}
			
		});
	}
}