﻿Type.registerNamespace('Ajax.Logging');

Ajax.Logging.BaseTraceListener = function(lineSeparator) {
    this._lineSeparator = '\n';

    if (lineSeparator) {
        this._lineSeparator = lineSeparator;
    }
}

Ajax.Logging.BaseTraceListener.prototype =
{
    get_lineSeparator: function() {
        if (arguments.length !== 0) throw Error.parameterCount();

        return this._lineSeparator;
    },

    dispose: function() {
    },

    publishException: function(errorCode, exception, environmentInfo) {
        throw Error.notImplemented();
    },

    formatException: function(errorCode, exception) {
        var lineSeparator = this.get_lineSeparator();
        var errorInfo = new Sys.StringBuilder();

        errorInfo.append('Error Code: ' + errorCode.toString() + lineSeparator);

        if (typeof exception.get_exceptionType != 'undefined') {
            errorInfo.append('Message: ' + exception.get_message() + lineSeparator +
                                'Type: ' + exception.get_exceptionType() + lineSeparator +
                                'StackTrace: ' + exception.get_stackTrace()
                            );
        }
        else {
            errorInfo.append('Message: ' + exception.message + lineSeparator +
                                'Type: ' + exception.name + lineSeparator +
                                'Description: ' + exception.description
                            );

            //Only Update Panel has this field
            if (typeof exception.httpStatusCode != 'undefined') {
                errorInfo.append(lineSeparator + 'HttpStatusCode: ' + exception.httpStatusCode);
            }

            //Only Unhandled error have the following two field

            if (typeof exception.lineNumber != 'undefined') {
                errorInfo.append(lineSeparator + 'LineNumber: ' + exception.lineNumber);
            }

            if (typeof exception.url != 'undefined') {
                errorInfo.append(lineSeparator + 'Url: ' + exception.url);
            }
        }

        return errorInfo.toString();
    }
}

Ajax.Logging.BaseTraceListener.registerClass('Ajax.Logging.BaseTraceListener', null, Sys.IDisposable);


Ajax.Logging.SysDebugTraceListener = function(lineSeparator) {
    Ajax.Logging.SysDebugTraceListener.initializeBase(this, [lineSeparator]);
}

Ajax.Logging.SysDebugTraceListener.prototype =
{
    dispose: function() {
        Ajax.Logging.SysDebugTraceListener.callBaseMethod(this, 'dispose');
    },

    publishException: function(errorCode, exception, environmentInfo) {
        //Calling the base class method
        var errorInfo = Ajax.Logging.SysDebugTraceListener.callBaseMethod(this, 'formatException', [errorCode, exception]);
        Sys.Debug.traceDump(errorInfo);
    }
}

Ajax.Logging.SysDebugTraceListener.registerClass('Ajax.Logging.SysDebugTraceListener', Ajax.Logging.BaseTraceListener);


Ajax.Logging.AlertTraceListener = function(lineSeparator) {
    Ajax.Logging.AlertTraceListener.initializeBase(this, [lineSeparator]);
}

Ajax.Logging.AlertTraceListener.prototype =
{
    dispose: function() {
        Ajax.Logging.AlertTraceListener.callBaseMethod(this, 'dispose');
    },

    publishException: function(errorCode, exception, environmentInfo) {
        //Calling the base class method
        var errorInfo = Ajax.Logging.AlertTraceListener.callBaseMethod(this, 'formatException', [errorCode, exception]);
        alert(errorInfo);
    }
}

Ajax.Logging.AlertTraceListener.registerClass('Ajax.Logging.AlertTraceListener', Ajax.Logging.BaseTraceListener);


Ajax.Logging.DivTraceListener = function(lineSeparator, targetElement) {
    if (typeof targetElement == 'string') {
        this._element = $get(targetElement);
    }
    else {
        this._element = targetElement;
    }

    Ajax.Logging.DivTraceListener.initializeBase(this, [lineSeparator]);
}

Ajax.Logging.DivTraceListener.prototype =
{
    dispose: function() {
        this._element = null;
        Ajax.Logging.DivTraceListener.callBaseMethod(this, 'dispose');
    },

    publishException: function(errorCode, exception, environmentInfo) {
        //Calling the base class methods
        var errorInfo = Ajax.Logging.DivTraceListener.callBaseMethod(this, 'formatException', [errorCode, exception]);
        var lineSeparator = Ajax.Logging.DivTraceListener.callBaseMethod(this, 'get_lineSeparator')

        if (this._element.innerHTML.length > 0) {
            //Put some extra breaks
            this._element.innerHTML = lineSeparator + lineSeparator + this._element.innerHTML;
        }

        this._element.innerHTML = errorInfo + this._element.innerHTML;
    }
}

Ajax.Logging.DivTraceListener.registerClass('Ajax.Logging.DivTraceListener', Ajax.Logging.BaseTraceListener);


Ajax.Logging.WebServiceTraceListener = function(lineSeparator) {
    Ajax.Logging.WebServiceTraceListener.initializeBase(this, [lineSeparator]);
}

Ajax.Logging.WebServiceTraceListener.prototype =
{
    dispose: function() {
        Ajax.Logging.WebServiceTraceListener.callBaseMethod(this, 'dispose');
    },

    publishException: function(errorCode, exception, environmentInfo) {
        //Calling the base class method
        var errorInfo = Ajax.Logging.WebServiceTraceListener.callBaseMethod(this, 'formatException', [errorCode, exception]);

        //Invoking the WebService
        EBiz.Services.Globals.Logger.Publish(PAGE_IDENTIFIER, errorCode, exception, environmentInfo.url, environmentInfo.referrer, environmentInfo.scripts, this.onSuccess, this.onError, this);
    }, 
    onSuccess: function(result, context, method) 
    {
        return;
    },
    onError: function(result, context, method) 
    {
        return;
    }
}

Ajax.Logging.WebServiceTraceListener.registerClass('Ajax.Logging.WebServiceTraceListener', Ajax.Logging.BaseTraceListener);


Ajax.Logging.ExceptionManager = function() {
    this._listeners = new Array();
    Ajax.Logging.ExceptionManager.initializeBase(this);
}

Ajax.Logging.ExceptionManager.prototype =
{
    initialize: function() {
        Ajax.Logging.ExceptionManager.callBaseMethod(this, 'initialize');
    },

    dispose: function() {
        if (this._listeners.length > 0) {
            for (var i = 0; i < this._listeners.length; i++) {
                this._listeners[i].dispose();
            }
        }

        delete this._listeners;

        Ajax.Logging.ExceptionManager.callBaseMethod(this, 'dispose');
    },

    addListener: function(listener) {
        var e = Function._validateParams(arguments, [{ name: 'listener', type: Ajax.Logging.BaseTraceListener}]);
        if (e) throw e;
        if (!Array.contains(this._listeners, listener))
        {
            Array.add(this._listeners, listener);
        }
    },

    removeListener: function(listener) {
        var e = Function._validateParams(arguments, [{ name: 'listener', type: Ajax.Logging.BaseTraceListener}]);
        if (e) throw e;

        listener.dispose();

        Array.remove(this._listeners, listener);
    },

    publishException: function(errorCode, exception) {
        var e1 = Function._validateParams(arguments, [{ name: 'errorCode', type: Number }, { name: 'exception', type: Error}]);
        var e2 = Function._validateParams(arguments, [{ name: 'errorCode', type: Number }, { name: 'exception', type: Sys.Net.WebServiceError}]);

        if ((e1) && (e2)) {
            throw e1;
        }

        if (this._listeners.length > 0) {
            var environmentInfo = this._getEnvironmentInfo();

            for (var i = 0; i < this._listeners.length; i++) {
                this._listeners[i].publishException(errorCode, exception, environmentInfo);
            }
        }
    },

    _getEnvironmentInfo: function() {
        var scriptTags = document.getElementsByTagName('script');
        var scripts = new Array();

        if (scriptTags) {
            for (var i = 0; i < scriptTags.length; i++) {
                var scriptTag = scriptTags[i];

                if (typeof scriptTag.src != 'undefined') {
                    if (scriptTag.src.length > 0) {
                        Array.add(scripts, scriptTag.src + ' : ' + scriptTag.readyState);
                    }
                }
            }
        }

        var url = window.location.href;

        var referrer = '';

        if (document.referrer) {
            if (document.referrer.length > 0) {
                referrer = document.referrer;
            }
        }

        return { url: url, referrer: referrer, scripts: scripts };
    }
}

Ajax.Logging.ExceptionManager.registerClass('Ajax.Logging.ExceptionManager', Sys.Component);

Ajax.Logging.ExceptionManager._staticInstance = $create(Ajax.Logging.ExceptionManager, { 'id': 'exceptionManager' });

Ajax.Logging.ExceptionManager.getInstance = function() {
    return Ajax.Logging.ExceptionManager._staticInstance;
}

if (typeof (Sys) != 'undefined') {
    Sys.Application.notifyScriptLoaded();
}

var ERROR_CODE_UNHANDLED = 1;
var ERROR_CODE_WEB_SERVICE = 2;

Sys.Application.add_init(initializeLogger);

function initializeLogger() {
    // nmalovic, 11/19/2008 - Turning off capturing of unhandled jscript exception in order to decrease number of web service calls
    // TODO: ConfigChannel this

//    window.onerror = function(message, url, lineNumber) {
//        if (typeof(PAGELOAD_MODAL_POPUP)=="string")
//        {
//            $get(PAGELOAD_MODAL_POPUP).style.display = 'none';
//        }
//        var e = Error.create(message, { description: message, name: 'UnhandledError', lineNumber: lineNumber, url: url });
//        var mgr = Ajax.Logging.ExceptionManager.getInstance();
//        mgr.addListener(new Ajax.Logging.WebServiceTraceListener());
//        ////    mgr.addListener(new Ajax.Logging.AlertTraceListener());
//        ////    mgr.addListener(new Ajax.Logging.SysDebugTraceListener());
//        ////    mgr.addListener(new Ajax.Logging.DivTraceListener('<br/>', 'divException'));
//        mgr.publishException(ERROR_CODE_UNHANDLED, e);
//        return true;
//    }
}

function publishException(result) {
    Ajax.Logging.ExceptionManager.getInstance().publishException(ERROR_CODE_WEB_SERVICE, result);
}
