﻿Cwo.RegisterNamespace("Cwo.Error");

Cwo.Error.Popup = function (message, detail) {
    // Load up a modal using the forms framework.
    var popup;
    popup = new Cwo.Form(null, 16, false, "", "Heading:" + message + ", Detail:" + detail, null, null, true); // TODO : Refactor this, doesn't need to load from server.
};

// Iterate through formfields (Cwo.Collection) and generates html as necessary
Cwo.Error.Modal = function (info) {
    var message = info.GetDataByKey("message"),
        detail = info.GetDataByKey("messagedetail"),
        fields = info.GetArrayByKey("formfieldname"),
        fieldList = "",
        i = 0;

    if (message === undefined || message === null) {
        message = "";
    } else {
        message = "<h3>" + message + "</h3>";
    }

    if (detail === undefined || detail === null) {
        detail = "";
    } else {
        detail = "<p>" + detail + "</p>";
    }

    if (fields === undefined || fields === null || fields.length === 0) {
        fieldList = "";
    } else {
        fieldList = "<ul>";
        for (i = 0; i < fields.length; i += 1) {
            fieldList += "<li>" + fields[i] + "</li>";
        }
        fieldList += "</ul>";
    }
    $("div.Error").html(message + detail + fieldList).show();
};

// Log JavaScript errors to the Event log
Cwo.Error.Log = function (message, url, line, stack) {
    var serviceUrl = "/webservices/ExceptionInterface.asmx",
        ex = {},
        ajaxCall;

    ex.Error = (typeof (message) !== "undefined") ? message : "";
    ex.Url = (typeof (url) !== "undefined") ? url : "";
    ex.Line = (typeof (line) !== "undefined") ? line : "";
    ex.StackTrace = (typeof (stack) !== "undefined") ? stack.toString() : "";
    ex.AppName = window.navigator.appName;
    ex.AppVersion = window.navigator.appVersion;
    ex.UserAgent = window.navigator.userAgent;

    ajaxCall = new Cwo.AjaxCall(serviceUrl + "/Exception", JSON.stringify(ex), function () { }, null, null);

    // 'return true' to prevent the error icon / popup in the status bar
    return true;
};

// Error tracking function ex: a string or an Error object
Cwo.Error.TrackError = function (ex) {
    var err = this,
        i;

    for (i in ex) {
        if (ex.hasOwnProperty(i)) {
            if (i === "message") {
                err.message = ex[i];
            }
            if (i === "fileName" || i === "sourceURL") {
                err.url = ex[i];
            }
            if (i === "lineNumber" || i === "line") {
                err.line = ex[i];
            }
            if (i === "stack") {
                err.stack = ex[i].toString();
            }
        }
    }

    Cwo.Error.Log(err.message, err.url, err.line, err.stack);
};

// Override jQuery.fn.bind to wrap every provided function in try/catch
// TODO : Commented out as conflicts with events bound in jquery.ui.slider
//var jQueryBind = jQuery.fn.bind;
//jQuery.fn.bind = function(type, data, fn) {
//    if (!fn && data && typeof data === 'function') {
//        fn = data;
//        data = null;
//    }
//    if (fn) {
//        var origFn = fn;
//        var wrappedFn = function() {
//            try {
//                origFn.apply(this, arguments);
//            }
//            catch (ex) {
//                Cwo.Error.TrackError(ex);
//            }
//        };
//        fn = wrappedFn;
//    }
//    return jQueryBind.call(this, type, data, fn);
//};

// window.onerror = Cwo.Error.Log;

