microstrategy.errors = new mstrErrorsImpl;

mstrErrorsImpl.prototype.errorMsg = "";
mstrErrorsImpl.prototype.staticErrorMsg = "";
mstrErrorsImpl.prototype.callers = new Array();
mstrErrorsImpl.prototype.errorObj = null;

mstrErrorsImpl.prototype.log = function(err) {
    //Purpose: log a run-time script error.
    //Input: err - instance of Error object, generated by script engine.
    try {
        if (this.errorMsg.length == 0) {
            this.errorMsg = (!err.description) ? err : err.description;

            var msg1 = (microstrategy.descriptors != null) ? "*JavaScript errors were encountered on this page.*" : "JavaScript errors were encountered on this page."
            var msg2 = (microstrategy.descriptors != null) ? microstrategy.descriptors.getDescriptor("2947") : "Click here for more details."

            window.status =  msg1;
            
            if (bIsFirefox) {
                this.errorObj = err;
            } else {
	            // The new style of javascript results in much more recursion so we limit the
	            // stack to 25.
	            var stackLimit = 0;
	            var c = this.log.caller;
	            while (c != null) {
	                this.callers[this.callers.length] = String(c.toString());
	                c = c.caller;
	                stackLimit++;
	                if (stackLimit > 25) break;
	            }
            }

            if (this.staticErrorMsg.length == 0) {
                var div = document.createElement('div');
                div.className = 'mstrAlert';

                var img = document.createElement('img');
                img.src = microstrategy.FOLDER_IMAGES + 'DocError.gif';
                img.setAttribute("id", "mstrJSErrorImg");

                div.appendChild(img);
                div.appendChild(document.createTextNode(' '+ msg1 + ' '+ msg2));
                div.onclick = new Function("microstrategy.errors.show();");

                var loc = document.getElementById('mstrWeb_error');
                if (loc) {
                    loc.insertAdjacentElement('afterEnd', div);
                    microstrategy.eventManager.ondialogresize();
                }
            }
            this.staticErrorMsg = "";
            if (typeof(console) != "undefined") {
                console.log(err);
                console.trace && console.trace();
            }
        }
    } catch(e) {
        alert(err.description);
    }
   return;
}

mstrErrorsImpl.prototype.clear = function() {
    //Purpose: erase contents of error log.
    try {
        this.errorMsg = "";
        this.callers = new Array;
    } catch(e) {
        alert(e);
    }
    return;
}

mstrErrorsImpl.prototype.show = function() {
    //Purpose: display error log in a pop-window for debuggin.

    try {
         if (this.staticErrorMsg.length == 0) {
            var s =  microstrategy.descriptors.getDescriptor("2948") + "<br /><br />" + this.errorMsg + "<br/>"; //Descriptor: MicroStrategy client-side error log:
            
            if (this.errorObj) {
                var err = this.errorObj;
                if (err.fileName && err.lineNumber) {
                    s += "&nbsp;&nbsp;&nbsp;&nbsp;at " + err.fileName + ":" + err.lineNumber + "<br /><br />";
                }
                this.errorObj = null;
            } else {
	            for (var i = 0; i < this.callers.length; i++){
	                var className = methodName = "";
	                var index = this.callers[i].indexOf("@class");
	                if (index != -1){
	                    className = this.callers[i].substr(index + 7);
	                    className = className.substr(0, className.indexOf(";"));
	                }
	                index = this.callers[i].indexOf("@method");
	                if (index != -1){
	                    methodName = this.callers[i].substr(index + 8);
	                    methodName = methodName.substr(0, methodName.indexOf(";"));
	                }
	                s += '&nbsp;&nbsp;&nbsp;&nbsp;at ';
	                if (className && methodName) {
	                    s += className + '.' + methodName + '</br>';
	                } else {
	                    if (bIsIE4) {
	                        s += "unknown " + this.callers[i].substr(0, this.callers[i].indexOf("{")).replace(/\n/g, "") + "<br/>";
	                    } else {
	                        var fs = this.callers[i];
	                        var i1 = this.callers[i].indexOf("\n");
	                        var i2 = this.callers[i].indexOf("{");
	                        if (i1 > i2) {
	                            i1 = 0;
	                        } else {
	                            i1++;
	                        }
	                        s += "unknown " + this.callers[i].substr(i1, i2) + "<br/>";
	                    }
	                }
	            }
            }
            this.staticErrorMsg = s.replace(/\n/g, "<br/>");
            this.clear();
         }
         showMessage( {contents:this.staticErrorMsg, elements:microstrategy.OK_BUTTON, type:mstrMsgBoxImpl.MSG_ERROR});
    } catch(e) {
        alert(e);
    }
    return;
}

function mstrErrorsImpl() { return this; }



