
//<script> // script tag to force VS to format the file as JS not ASPX

/*****************************************************************************************************************************
 * JavaScript to standardise the handling of exceptions client side
 */
var isDebug = false;
var enableDebug = false;
 
function LogVerbosity()
{
	this.Audit = -1;
	this.NotSet = 0;
	this.Fatal = 1;
	this.Serious = 2;
	this.Warning = 3;
	this.Information = 4;
	this.SuperVerbose = 5;

	this.getVerbosityName = function(verbosity) {
		var verbosityWord = 'NotSet';
		switch(verbosity)
		{
			case -1:
				verbosityWord='Audit';
				break;
			case 0:
				verbosityWord='NotSet';
				break;
			case 1:
				verbosityWord='Fatal';
				break;
			case 2:
				verbosityWord='Serious';
				break;
			case 3:
				verbosityWord='Warning';
				break;
			case 4:
				verbosityWord='Information';
				break;
			case 5:
				verbosityWord='SuperVerbose';
				break;
		}
		return verbosityWord;
		}
}
var logVerbosity = new LogVerbosity();

// Overloads (the types are also listed for ease of reading):
//
// Log(Exception ex, int verbosity) - 2 args
// Log(String information, int verbosity); - 2 args

// Log(Exception ex, Array args, int verbosity) - 3 args
// Log(Exception ex, String information, int verbosity) - 3 args

// Log(Exception ex, String information, Array args, int verbosity) - 4 args
// Log(Exception ex, String information, Array parameters, int verbosity) - 4 args

// Log(Exception ex, String information, Array parameters, Array args, int verbosity) - 5 args

// ex is the exception
// args is the arguments of the function which caused it (as an associative array)
// information is free text
// parameters is merge data to be added to information (as an associative array)
// verbosity is a verbosity level.
function Log(ex, arg2, arg3, arg4, arg5)
{    
    // Create our object of argument pairs
    var argPairs = new Object();
    
	// Allow overloading of this function
	if(typeof(arg2) == 'number' && typeof(arg3) == 'undefined' && typeof(arg4) == 'undefined' && typeof(arg5) == 'undefined')
	{
	    // Log(ex, verbosity)
	    argPairs['verbosity'] = arg2;
	}
	else if(typeof(arg2) == 'object' && typeof(arg3) == 'number' && typeof(arg4) == 'undefined' && typeof(arg5) == 'undefined')
	{
	    // Log(ex, args, verbosity)
	    argPairs['args'] = '';
	    for(var key in arg2)
	        argPairs['args'] += '(' + key + ':' + arg2[key] + ')';
	    argPairs['verbosity'] = arg3;
	}
	else if(typeof(arg2) == 'string' && typeof(arg3) == 'object' && typeof(arg4) == 'number' && typeof(arg5) == 'undefined')
	{
	    // Log(ex, information, args, verbosity)
	    argPairs['information'] = arg2;
	    argPairs['args'] = '';
	    for(var key in arg3)
	        argPairs['args'] += '(' + key + ':' + arg3[key] + ')';
	    argPairs['verbosity'] = arg4;
	}
	else if(typeof(arg2) == 'string' && typeof(arg3) == 'object' && typeof(arg4) == 'object' && typeof(arg5) == 'number')
	{
	    // Log(ex, information, parameters, args, verbosity)
	    argPairs['information'] = arg2;
	    argPairs['params'] = '';
	    for(var key in arg3)
	        argPairs['params'] += '(' + key + ':' + arg3[key] + ')';
	    argPairs['args'] = '';
	    for(var key in arg4)
	        argPairs['args'] += '(' + key + ':' + arg4[key] + ')';
	    argPairs['verbosity'] = arg5;
	}
	else if(typeof(arg2) == 'string' && typeof(arg3) == 'object' && typeof(arg4) == 'number' && typeof(arg5) == 'undefined')
	{
	    // Log(ex, information, parameters, verbosity)
	    argPairs['information'] = arg2;
	    argPairs['params'] = '';
	    for(var key in arg3)
	        argPairs['params'] += '(' + key + ':' + arg3[key] + ')';
	    argPairs['verbosity'] = arg4;
	}
	else if(typeof(arg2) == 'string' && typeof(arg3) == 'number' && typeof(arg4) == 'undefined' && typeof(arg5) == 'undefined')
	{
	    // Log(ex, information, verbosity)
	    argPairs['information'] = arg2;
	    argPairs['verbosity'] = arg3;
	}
	
	var d = new Date();
	var t = d.getTime();
    
    // stuff common to all types of error
	argPairs['browser'] = navigator.appName;
	argPairs['browserversion'] = navigator.appVersion;
	argPairs['booth'] = '[NONE]';
	argPairs['timestamp'] = t;
	
	// add standard exception stuff
	if(typeof(ex) == 'object')
	{
        argPairs['name'] = ex.name;
        argPairs['number'] = ex.number;
        argPairs['message'] = ex.message;
        argPairs['description'] = ex.description;
    }
    else
    {
        argPairs['message'] = ex;
    }
    
    // These don't work in IE!!
    argPairs['file'] = ex.fileName;
    argPairs['line'] = ex.lineNumber;
    
    // Fix for IE - can only get the function this one was called from, but its better than nothing!
    if(ex.stack === undefined)
    {
        if((argPairs.verbosity >= 5 && isDebug) || argPairs.verbosity < 5)
        {
            var fnCode = "" + Log.caller;
            fnCode = fnCode.replace(/function (.*\(.*\)).*/g, "$1");
            var fnLines = fnCode.split('\n');
            var fnName = fnLines[0];
            argPairs['stack'] = 'First line of caller function was "' + fnName + '"';
        }
        else
        {
            var fnCode = "" + Log.caller;
            argPairs['stack'] = 'Caller function was "' + fnCode + '"';
        }
    }
    else
    {
        // Firefox maintains a full call stack, so just use that
        argPairs['stack'] = ex.stack;
    }
    
	// make the argument pairs into a formatted URL
	var errorDisplayBaseURL = '/errorDisplayPage.aspx?';
	var errorDisplayURL = '';
	for(var argName in argPairs)
	{
	    errorDisplayURL += '&' + argName + '=' + escape(argPairs[argName]);
	}
	errorDisplayURL = errorDisplayBaseURL + errorDisplayURL.substring(1,errorDisplayURL.length);
	
	// Case 49640 - Only show the alert if built as debug
	if(isDebug) {
	    window.open(errorDisplayURL,'errormessage' + t,'scrollbars=yes,resizable=yes,width=800,height=220,modal=yes');
	}
	else 
	{
        // MJA 07Jul08 case 62282 - prevent blowup on IE6.
	    try
	    {
	        EMR.Ajax.get(errorDisplayURL);
	    }
	    catch(ex)
	    {
	        alert('An error has occurred, probably your session has expired. Please logon again. If you see this error again, please send a screenshot to smartFOCUS DIGITAL support.: ' + errorDisplayURL ); // TODO: Decide if we want to do this or not.
	    }
	}
	
	if(enableDebug) {
	    debugger;
	}
}

//</script>