Wraps the window.external calls to js callers
************/

var diag = function () {
    this.diagObject = window.external;
    this.dxDiagText = null;
    try {
        this.settings = window.external.Settings();
    }
    catch (ex) { }
}
diag.prototype =
{
    isDebuggerInstalled: function () {
        return this.diagObject.IsDebuggerInstalled;
    },
    getEnabledAddons: function () {
        return new Array("");
    },
    getContent: function (container) {
        return this.diagObject.GetHTML(container);
    },
    collectLogs: function () {
        try {
            return this.diagObject.CollectLogs();
        }
        catch (ex) {
        }
    },
    getData: function (sDiagToolName, refresh) {
        try {
            var data = this.diagObject.GetLog(sDiagToolName, refresh);
            log.value = data;
            return log.value;
        } catch (e) {
           
        }
    },
    attachToPid: function (sPid) {
        try {
            this.diagObject.AttachDebugger(sPid)
        }
        catch (ex) {
        }
    },
    startTrace: function (sPid) {
        try {
            this.diagObject.StartTrace(sPid)
        }
        catch (ex) {
        }
    },
    stopTrace: function (sPid) {
        try {
            this.diagObject.StopTrace(sPid)
        }
        catch (ex) {
        }
    },
    launchIENoAddOns: function () {
        try {
            return this.diagObject.LaunchIEWithNoAddons()
        }
        catch (ex) {
        }
    },
    launchInSoftwareMode: function () {
        try {
            return this.diagObject.LaunchIEInSoftwareMode()
        }
        catch (ex) {
        }
    },
    exportFavorites : function(format, isRestore, path)
    {
        try {
            return this.diagObject.ExportFavorites(format, isRestore, path);
        }
        catch (ex) {
        }

    },
    launchWithEPMOff: function () {
        try {
            return this.diagObject.LaunchIEWithEPMOff()
        }
        catch (ex) {
        }
    },
    isIERunningInSWMode: function () {
        try {
            return this.diagObject.IsIERunningInSWMode();
        }
        catch (ex) {
        }
    },
    launchIE: function () {
        try {
            return this.diagObject.LaunchIE()
        }
        catch (ex) {
        }
    },
    closeIE: function (pid) {
        try {
            this.diagObject.CloseIE(pid);
        }
        catch (ex) {
        }
    },
    resetEPM: function () {
        try {
            this.diagObject.SetEPMMode(true);
        }
        catch (ex) {
        }
    },
    resetSoftwareMode: function () {
        try {
            this.diagObject.SetSoftwareMode(false);
        }
        catch (ex) {
        }
    },
    disableAddOn: function (clsid) {
        try {
            this.diagObject.DisableAddOn(clsid)
        }
        catch (ex) {
        }
    },
    enableAddOn: function (clsid) {
        try {
            this.diagObject.EnableAddOn(clsid)
        }
        catch (ex) {

        }
    },
    focus: function () {
        this.diagObject.Focus();
    },
    isRunningSilent: function () {
        return this.diagObject.IsSilentMode();
    },
    getFullLogXml: function () {
        return this.diagObject.GetFullLogXml();
    },
    /********* Data Collectors *********/
    eventLog: function () {
        return iediag.getData("Event Log Information");
    },
    dxDiag: function () {
        if (this.dxDiagText != null) {
            this.dxDiagText = iediag.getData("DxDiag", false);
        }
        return this.dxDiagText;
    },
    enabledBHOs: function (bitness) {
        return this.diagObject.GetEnabledBHOs(bitness);
    },
    submitLogs: function () {
        this.diagObject.SubmitLogs();
    },
    logTroubleShootUsage: function (toolNames, msg, success) {
        this.diagObject.LogTroubleShootUsage(toolNames, msg, success);
    },
    isDebugToolsAvailable: function () {
        return this.diagObject.isDebugToolsAvailable()
    }
}

/********* 
trouble shooter definition begins 
    - Object used to run through the trouble shooting logic
    - Uses a list of functors which map to trouble shooting steps like extoff and turning sw mode etc
************/
function troubleShooter() {
    this.currentTool = null;
    this.functionList = null;
    this.problemFound = false;
    this.addOnTS = null;
    this.functionList = new Array();
    this.functionList.push({
        "name": "noAddon",
        "fn": this.noAddon
    });
    this.functionList.push({
        "name": "useSoftwareMode",
        "fn": this.useSoftwareMode,
        "isEnabled": function () {
            return (!iediag.settings.IsSoftwareModeOn());
        },
        "reset": this.resetSWMode
    });
    this.functionList.push({
        "name": "noEPM",
        "fn": this.noEPM,
        "isEnabled": function () {
            return iediag.settings.IsEPMOn();
        },
        "reset": this.resetEPM
    });
    this.currentTool = -1;
}

function resetTroubleShootUI()
{
    $("#tsStart").css("display", "inline");
    $("#tsProgress").hide();
}

troubleShooter.prototype =
    {
        constructor: troubleShooter,
        waitForCrash: function () {
            if (iediag.IsProcessHungOrCrashed(this.processId)) {
                repro(false);
            }
        },
        ask: function (sMessage) {
            if (iediag.isRunningSilent()) {
                this.waitForCrash();
                return;
            }
            if (reproDialog == null) {
                reproDialog = $("#dialog-confirm").dialog({
                    resizable: false,
                    height: 140,
                    modal: true,
                    buttons: {
                        "Yes": function () {
                            $(this).dialog("close");
                            reproDialog.callBack.repro(true);
                        },
                        "No": function () {
                            $(this).dialog("close");
                            reproDialog.callBack.repro(false);
                        }
                    }
                });
            }
            if (sMessage) {
                $("#dialog-confirm").html(sMessage)
            }
            reproDialog.callBack = this;

            setTimeout(function () {
                iediag.focus();
                reproDialog.dialog("open");
            }, 2000)
        },
        repro: function (doesRepro) {
            this.problemFound = !doesRepro;
            var tool = this.functionList[this.currentTool];
            this.completedToolName += tool.name + ",";

            if (!this.problemFound) {
                if (typeof (tool.reset) != "undefined")
                    tool.reset();
                if (this.processId != 0) {
                    iediag.closeIE(this.processId);
                }
                this.runNextTool();
            }
            else {
                if (tool.name == "noAddon") {
                    addOnTS = new addOnsTroubleShooter();
                    addOnTS.start(addOnTS);
                }
                else {
                    $("#" + tool.name + "Solution").show();
                    this.end();
                }
            }
            if (this.processId != 0) {
                iediag.closeIE(this.processId);
            }
        },
        end: function (allToolsDone) {
            resetTroubleShootUI();
            var toolNames = "";
            var msg = ""
            if (this.problemFound) {
                toolNames = this.functionList[this.currentTool].name;
            }
            else {
                if (allToolsDone) {
                    for (var idx = 0; idx <= this.functionList.length - 1; idx++) {
                        var tool = this.functionList[idx];
                        if (tool.isEnabled) {
                            toolNames += tool.name + ","
                        }
                    }
                }
            }
            //log usage of the trouble shooting 
            iediag.logTroubleShootUsage("TroubleShootingDone", toolNames, this.problemFound);
        },
        runNextTool: function () {
            if (this.currentTool < this.functionList.length - 1) {
                var lastTool = this.functionList[this.currentTool];
                this.currentTool++;
                var ts = this;
                var tool = this.functionList[this.currentTool];
                if (typeof tool.isEnabled == "undefined" || tool.isEnabled()) {
                    $("#" + tool.name).click();
                    setTimeout(function () {
                        tool.fn(ts);
                    }, 1000)
                }
                else {
                    $("#" + lastTool.name).click();
                    this.runNextTool();
                }
            }
            else {
                var lastTool = this.functionList[this.currentTool - 1];
                $("#" + lastTool.name).click();
                this.end(true);
            }
        },
        noAddon: function (ts) {
            ts["processId"] = iediag.launchIENoAddOns();
            if (ts.processId != 0) {
                ts.ask();
            }
        },
        useSoftwareMode: function (ts) {
            ts["processId"] = iediag.launchInSoftwareMode();
            if (ts.processId != 0) {
                ts.ask();
            }
            else {
                ts.runNextTool();
            }
        },
        noEPM: function (ts) {
            ts["processId"] = iediag.launchWithEPMOff()
            if (ts.processId != 0) {
                ts.ask();
            }
            else {
                ts.runNextTool();
            }
        },
        resetEPM: function (ts) {
            iediag.settings.SetEPMMode(false);
        },
        resetSWMode: function (ts) {
            iediag.settings.SetSoftwareMode(false);
        }
    };

var reproDialog;