/*
 * AJAX Error Reporting - Support Class
 * version: 1.0.3
 *
 * CopyLeft (CC) 2010 Matias Perrone (Global Innovation Argentina - www.globalinnovation.com.ar)
 * Licensed under the Creative Commons License
 * http://creativecommons.org/licenses/by-sa/2.5/ar/
 * Creative Commons Reconocimiento - Compartir bajo la misma licencia 2.5 Argentina License
 * Based on a work at http://www.globalinnovation.com.ar/js/jquery.support.js
 * Permissions beyond the scope of this license may be available at Global Innovation Argentina
 * http://www.globalinnovation.com.ar/
 *
*/

function Support(sURL)
{
	this.url  = sURL;
	this.data = {};
	this.catchAll = false;
	this.showClassErrors = false;
}

Support.prototype = {
	sendError: function(oError, sFunction, sExtra)
	{
		try
		{
			this.data =	{
					error: oError,
					functionName: sFunction,
					extra: sExtra
				};

			$.ajax(
			{
					type: "post",
					url: this.url,
					data: this.data,
					dataType: "html",
					success: this.responseOK,
					error: this.responseERROR
			});
		}
		catch (oError)
		{
			if (this.showClassErrors)
			{
				alert(oError.name + ': ' + oError.message + '.\n\n'+
					  'Line: ' + (typeof(oError.lineNumber) == 'undefined' ? 0 : oError.lineNumber ) + '\n'+
					  'File: ' + (typeof(oError.fileName) == 'undefined' ? '' : oError.fileName ) +
					  ((typeof(oError.stack)      != 'undefined') ? '\nStack:\n' + oError.stack : '' ));
			}
		}
	},
	responseOK: function(sResponseText, sStatusText){},
	responseERROR: function(oHTTPRequest, sStatusText, oError){},
	onErrorFunction: function(oEvent) // onerror = function(sMessage, sURL, iLineNumber)
					{
						try
						{
							oError = {
										name: 'WindowOnError',
										message: sMessage,
										url: sURL,
										lineNumber: (typeof(iLineNumber) == 'undefined' ? 0 : iLineNumber ),
										fileName: location.pathname
									}
							this.sendError(oError, null, null)
							return this.catchAll;
						}
						catch(oError)
						{
							if (this.showClassErrors)
							{
								alert(oError.name + ': ' + oError.message + '.\n\n'+
									  'Line: ' + (typeof(oError.lineNumber) == 'undefined' ? 0 : oError.lineNumber ) + '\n'+
									  'File: ' + (typeof(oError.fileName) == 'undefined' ? '' : oError.fileName ) +
					  				  ((typeof(oError.stack)      != 'undefined') ? '\nStack:\n' + oError.stack : '' ));
							}
						}
					},
	setWindowOnError: function(){
		try
		{
			window.addEventListener("error", this.onErrorFunction, this.catchAll);
		}
		catch(oError)
		{
			if (this.showClassErrors)
			{
				alert(oError.name + ': ' + oError.message + '.\n\n'+
					  'Line: ' + (typeof(oError.lineNumber) == 'undefined' ? 0 : oError.lineNumber ) + '\n'+
					  'File: ' + (typeof(oError.fileName) == 'undefined' ? '' : oError.fileName ) +
					  ((typeof(oError.stack)      != 'undefined') ? '\nStack:\n' + oError.stack : '' ));
			}
		}
	},
	cancelWindowOnError: function(){
		try
		{
			window.removeEventListener("error", this.onErrorFunction, this.catchAll);
		}
		catch(oError)
		{
			if (this.showClassErrors)
			{
				alert(oError.name + ': ' + oError.message + '.\n\n'+
					  'Line: ' + (typeof(oError.lineNumber) == 'undefined' ? 0 : oError.lineNumber ) + '\n'+
					  'File: ' + (typeof(oError.fileName) == 'undefined' ? '' : oError.fileName ) +
					  ((typeof(oError.stack)      != 'undefined') ? '\nStack:\n' + oError.stack : '' ));
			}
		}
	}
}


