/*
11/17/2009	Darin Niday - Viatech Consulting, Inc
	
This file is used by One Click Fix pages to proxy calls to the various methods on the ActiveX control and log activity.

- UPDATE 1/22/10: Added check to see whether or not the object was properly instantiated and gives a better message to the user
*/

//used to wrap calls to the One Click Fix ActiveX control
function OCFProxy() {
    //set default values for server name and protocol
    this.ServerName = "www.comcastsupport.com"; //name of server domain (e.g. chat-staging.comcast.com)
    this.Protocol = "http://"; //pre-fix to the server name, defines the protocol used when referencing CAB files

    var axObj = null; //object that will reference to the ActiveX

    //helper function to calculate the domain, same as 'request.ServerVariables("SERVER_NAME")' in vbscript
    this.deriveServerName = function(url, protocol) {
        var urlNoProtocol = url.split(protocol)[1]; //get url without the protocol		
        return urlNoProtocol.split("/")[0]; //return everything before the first '/'
    }

    this.Enabled = true;

    //initialize
    this.init = function() {
        if (null == axObj) {
            try {
                //initialize active x
                axObj = new ActiveXObject("ComcastOcf.OneClickFixes");

                //set environment variables
                this.Protocol = location.protocol + "//";
                this.ServerName = this.deriveServerName(location.href, this.Protocol);
            }
            catch (e) {
                if (-2146827859 == e.number)
                    alert("You must install the ActiveX control to use this feature");

                this.Enabled = false;
            }
        }
    }


    /* IE */

    //fires ActiveX to set the home page in IE and logs the hit
    this.setIeHomePage = function() {
        this.init();

        if (this.Enabled) {
            axObj.SetHomePage();
            this.sendHit("SetIEHomePage");
        }
    }

    //fires ActiveX to display IE version information and logs the hit
    this.displayVersionInformation = function() {
        this.init();

        if (this.Enabled) {
            axObj.DisplayVersionInformation();
            this.sendHit("DisplayVersionInformation");
        }
    }

    //fires ActiveX to set the number of days to keep history in IE and logs the hit
    this.daysToKeepHistory = function() {
        this.init();

        if (this.Enabled) {
            axObj.DaysToKeepHistory();
            this.sendHit("DaysToKeepHistory");
        }
    }

    //fires ActiveX to toggle if IE should check to be the default browser upon load and logs the hit
    this.checkForDefaultBrowser = function() {
        this.init();

        if (this.Enabled) {
            axObj.CheckForDefaultBrowser();
            this.sendHit("CheckForDefaultBrowser");
        }
    }

    //fires ActiveX to clear the page history in IE and logs the hit
    this.clearHistory = function() {
        this.init();

        if (this.Enabled) {
            axObj.ClearHistory();
            this.sendHit("ClearIEHistory");
        }
    }

    //fires ActiveX to set the cache in IE and logs the hit
    this.clearCache = function() {
        this.init();

        if (this.Enabled) {
            axObj.ClearCache();
            this.sendHit("ClearIECache");
        }
    }

    //fires ActiveX to set custom security options for IE and logs the hit
    this.setCustomSecurity = function() {
        this.init();

        if (this.Enabled) {
            axObj.SetCustomSecurity();
            this.sendHit("SetCustomSecurity");
        }
    }


    /* Performance */

    //fires ActiveX to clear windows temporary files and logs the hit
    this.clearTempFiles = function() {
        this.init();

        if (this.Enabled) {
            axObj.ClearTempFiles();
            this.sendHit("ClearTempFiles");
        }
    }


    /* Connection */

    //fires ActiveX to check network connectivity and logs the hit
    this.checkNetworkConnectivity = function() {
        this.init();

        if (this.Enabled) {
            axObj.CheckNetworkConnectivity();
            this.sendHit("CheckNetworkConnectivity");
        }
    }

    //fires ActiveX to run the ping utility and logs the hit
    this.pingUtility = function() {
        this.init();

        if (this.Enabled) {
            axObj.PingUtility();
            this.sendHit("PingUtility");
        }
    }


    /* Outlook Express */

    //fires ActiveX to create a news account for outlook express and logs the hit
    // 	*Uses 'runScript' to manually run packaged VBS script to perform the functionality
    this.createNewsAccount = function() {
        this.runScript("CreateOutlookExpressNewsAccount.cab", true);
    }

    //fires ActiveX to configure outlook express with comcast mail and logs the hit
    // 	*Uses 'runScript' to manually run packaged VBS script to perform the functionality
    this.configureComcastMail = function() {
        this.runScript("CreateOutlookExpressProfilePort587.cab", true);
    }

    //fires ActiveX to remember the password for outlook express and logs the hit
    // 	*Uses 'runScript' to manually run packaged VBS script to perform the functionality
    this.rememberOEPassword = function() {
        this.runScript("VC_RememberPasswordForMail.cab", true);
    }

    //fires ActiveX to set the user's default mail client and logs the hit
    // 	*Uses 'runScript' to manually run packaged VBS script to perform the functionality
    this.setDefaultMailClient = function() {
        this.runScript("DefaultMailClient.cab", true);
    }

    //fires ActiveX to create a news account for outlook express and logs the hit
    // 	*Uses 'runScript' to manually run packaged VBS script to perform the functionality
    this.createGroupMailingList = function() {
        this.runScript("OECreateGroupMailingList.cab", true);
    }

    //fires ActiveX to check comcast mail servers for connectivity and logs the hit
    // 	*Uses 'runScript' to manually run packaged VBS script to perform the functionality
    this.checkComcastOEServers = function() {
        this.runScript("PingOutlookExpressServer.cab", false);
    }

    //fires ActiveX to configure outlook express for alternate ports and logs the hit
    // 	*Uses 'runScript' to manually run packaged VBS script to perform the functionality
    this.configureComcastMailAlternatePorts = function() {
        this.runScript("VC_OEBlockPort25.cab", true);
    }

    //fires ActiveX to configure suscom settings and logs the hit
    // 	*Uses 'runScript' to manually run packaged VBS script to perform the functionality
    this.configureSusCom = function() {
        this.runScript("suscom_to_comcast_combined.cab", true);
    }

    //fires ActiveX to uninstall the security manager and logs the hit
    // 	*Uses 'runScript' to manually run packaged VBS script to perform the functionality
    this.unistallSecurityManager = function() {
        this.runScript("UninstallSecurityManager.cab", true);
    }

    //used to run a script file through the ActiveX control
    this.runScript = function(script, wait) {
        this.init();

        if (this.Enabled) {
            axObj.RunScript(this.Protocol + this.ServerName + "/sdcxuser/oneclickfix/scripts/" + script,
						script,
						script.replace(".cab", ".vbs"), wait);

            this.sendHit(script);
        }
    }


    /* Reporting */

    var xhr; //global variable (performance) to hold the XmlHttpRequest object 

    //uses XHR to post a hit on the selected One Click Fix (is recorded in the db)
    this.sendHit = function(script) {
        if (!xhr)
            xhr = new ActiveXObject("Microsoft.XMLHTTP");

        xhr.open('POST', 'record_ocf.asp', false);
        xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

        xhr.send("id=" + script);
    }
}

//initialize OCF javascript wrapper  
var OCF = new OCFProxy();