// @(#)pogoSolution.js	1.8 07/04/10

var deployJava = {
    debug: null,

    myInterval: null,
    preInstallJREList: null,
    returnPage: 'http://java.com',
    brand: null,
    locale: 'en',
    
    redirect_to_GetJava: 1,
    click_to_install: 2,
    IEInstallOption: null,
    
    redirect_to_XPI: 2,
    FFInstallOption: null,

    getJavaURL: 'http://java.sun.com/webapps/getjava/BrowserRedirect?host=java.com',

    // XPI file for the JRE bundle
    win32_pluginspage: 'http://javadl.sun.com/webapps/download/AutoDL?BundleId=18712',
    
    
    runApplet: function(attributes, parameters, family)
    {
        if (family == null)
        {
            family = '1.0';
        }

        if ((family != '1.0') && (family != '1.4.2') && (family != '1.5.0') && (family != '1.6.0'))
        {
            if (deployJava.debug)
            {
                alert('Bad setting of "family" variable.\nSetting it to "1.0"');
            }
    
            family = '1.0';
        }
    
        var JREs = deployJava.getJREs();
    
        var found = false;
        for (var i = 0; i < JREs.length; ++i)
        {
            if (deployJava.compareVersions(JREs[i], family))
            {                         
                found = true;
            }
        }
    
        if (found)
        {
            deployJava.emitAppletTag(attributes, parameters);
        }
        else
        {
            deployJava.installJRE();
        }
    },
    
  
    getJREs: function()
    {
        var list = new Array();

        var browser = deployJava.getBrowser();
        
        if (browser == 'MSIE')
        {   
            if (deployJava.testUsingActiveX('1.6.0'))
            {
                list[0] = '1.6.0';
            }
            else if (deployJava.testUsingActiveX('1.5.0'))
            {
                list[0] = '1.5.0';
            }
            else if (deployJava.testUsingActiveX('1.4.2'))
            {
                list[0] = '1.4.2';
            }
            else if (deployJava.testForMSVM())
            {
                list[0] = '1.1';
            }
        }
        else if (browser == 'Netscape Family')
        {
            if (deployJava.testUsingMimeTypes('1.6'))
            {
                list[0] = '1.6.0';
            }
            else if (deployJava.testUsingMimeTypes('1.5'))
            {
                list[0] = '1.5.0';
            }
            else if (deployJava.testUsingMimeTypes('1.4.2'))
            {
                list[0] = '1.4.2';
            }
        }
        else if (browser == 'Safari')
        {
            if (deployJava.testUsingPluginsArray('1.6.0'))
            {
                list[0] = '1.6.0';
            }
            else if (deployJava.testUsingPluginsArray('1.5.0'))
            {
                list[0] = '1.5.0';
            }
            else if (deployJava.testUsingPluginsArray('1.4.2'))
            {
                list[0] = '1.4.2';
            }
        }
		else
		{
            if (deployJava.testUsingMimeTypes('1.6'))
            {
                list[0] = '1.6.0';
            }
            else if (deployJava.testUsingMimeTypes('1.5'))
            {
                list[0] = '1.5.0';
            }
            else if (deployJava.testUsingMimeTypes('1.4.2'))
            {
                list[0] = '1.4.2';
            }
		}
            
        if (deployJava.debug)
        {
            for (var i = 0; i < list.length; ++i)
            {
                alert('We claim to have detected Java SE ' + list[i]);
            }
        }
    
        return list;
    },
    
    
    getBrowser: function()
    {
        var browser = navigator.userAgent.toLowerCase();
    
        if (deployJava.debug)
        {
            alert('userAgent -> ' + browser);
        }
    
        if (browser.indexOf(' chrome/') != -1)
        {
            if (deployJava.debug)
            {
                alert('We claim to have detected "Chrome".');
            }
    
            return 'Chrome';
        }
        else if ((navigator.vendor) && (navigator.vendor.toLowerCase().indexOf('apple') != -1) &&
            (browser.indexOf('safari') != -1))
        {
            if (deployJava.debug)
            {
                alert('We claim to have detected "Safari".');
            }
    
            return 'Safari';
        }
        else if (browser.indexOf('msie') != -1)
        {
            if (deployJava.debug)
            {
                alert('We claim to have detected "IE".');
            }
    
            return 'MSIE';
        }
        else if ((browser.indexOf('mozilla') != -1) || (browser.indexOf('firefox') != -1))
        {
            if (deployJava.debug)
            {
                alert('We claim to have detected a Netscape family browser.');
            }
    
            return 'Netscape Family';
        }
        else
        {
            if (deployJava.debug)
            {
                alert('We claim to have failed to detect a browser.');
            }
    
            return '?';
        }
    },
    
    
    testUsingActiveX: function(version)
    {
        var objectName = 'JavaWebStart.isInstalled.' + version + '.0';
    
        if (!ActiveXObject) 
        {
            if (deployJava.debug)
            {
                alert ('Browser claims to be IE, but no ActiveXObject object??');
            }
    
            return false;
        }
    
        try 
        {
            return (new ActiveXObject(objectName) != null);
        } 
        catch (exception) 
        {
            return false;
        }
    },
    

    testForMSVM: function()
    {
        var clsid = '{08B0E5C0-4FCB-11CF-AAA5-00401C608500}';

        if (typeof oClientCaps != 'undefined')
        {
            var v = oClientCaps.getComponentVersion(clsid, "ComponentID");
            if ((v == '') || (v == '5,0,5000,0'))
            {
                return false;
            } 
            else 
            {
                return true;
            } 
        }
        else
        {
            return false;
        }
    },

      
    testUsingMimeTypes: function(version)
    {
        if ((!navigator.mimeTypes) || (!navigator.mimeTypes.length))
        {
            if (deployJava.debug)
            {
                alert ('Browser claims to be Netscape family, but no mimeTypes[] array??');
            }
    
            return false;
        }

        navigator.plugins.refresh(false);

        for (var i = 0; i < navigator.mimeTypes.length; ++i)
        {
            var s = navigator.mimeTypes[i].type; 
            var m = s.match(/^application\/x-java-applet\x3Bversion=(1\.6|1\.5|1\.4\.2)$/);
            if (m != null)
            {
                if (deployJava.compareVersions(m[1], version)) return true;   
            }
        }
    
        return false;
    },
    
    
    testUsingPluginsArray: function(version)
    {
        if ((!navigator.plugins) || (!navigator.plugins.length))
        {
            if (deployJava.debug)
            {
                alert ('Browser claims to be Safari, but no plugins[] array??');
            }
    
            return false;
        }
    
        for (var i = 0; i < navigator.plugins.length; ++i)
        {
            var s = navigator.plugins[i].description;
    
            if (s.search(/^Java Switchable Plug-in/) != -1)
            {
                return true;
            }
    
            m = s.match(/^Java (1\.4\.2|1\.5).* Plug-in/);
            if (m != null)
            {
                if (deployJava.compareVersions(m[1], version)) return true; 
            }
        }
    
        return false;
    },
    
    
    emitAppletTag: function(attributes, parameters)
    {
        if ((attributes == null) || (typeof attributes != 'object'))
        {
            alert ("Missing or incorrect first argument to runApplet().");
            return;
        }
    
        if (attributes["code"])
        {
            var s = '<' + 'applet code=' + attributes["code"];
            for (var attribute in attributes)
            {
                if (attribute == 'code')
                {
                    continue;
                }
    
                s += (' ' + attribute + '="' + attributes[attribute] + '"');
            }
    
            s += '>';
    
            document.write(s);
    
            if ((parameters != null) && (typeof parameters == 'object'))
            {
                for (var parameter in parameters)
                {
                    s = '<' + 'param name="' + parameter + '" value="' + parameters[parameter] + '">';
                    document.write(s);
                }
            }

            document.write('<' + '/' + 'applet' + '>');
        }
        else
        {
            if (deployJava.debug)
            {
                alert('Sorry, I need a code attribute to emit an applet tag.');
            }
        }
    },
    
    
    installJRE: function()
    {
        var browser = deployJava.getBrowser();
        var platform = navigator.platform.toLowerCase();
    
        if (browser == 'MSIE')
        {
            deployJava.IEInstall();
        }
        else if ((browser == 'Netscape Family') && (platform.indexOf('win32') != -1))
        {
            deployJava.FFInstall();
        }
        else
        { 
            location.href = deployJava.getJavaURL + '&returnPage=' + deployJava.returnPage
                                                  + '&locale=' + deployJava.locale +
            ((deployJava.brand != null) ? ('&brand=' + deployJava.brand) : '');
        }
    },
    
    
    IEInstall: function()
    {
        if (deployJava.IEInstallOption == null)
        {
            deployJava.IEInstallOption = deployJava.redirect_to_GetJava;
        }
    
        if (deployJava.IEInstallOption == deployJava.redirect_to_GetJava)
        {
            location.href = deployJava.getJavaURL + '&returnPage=' + deployJava.returnPage
                                                  + '&locale=' + deployJava.locale +
            ((deployJava.brand != null) ? ('&brand=' + deployJava.brand) : '');
        }
        else if (deployJava.IEInstallOption == deployJava.click_to_install)
        {
            deployJava.preInstallJREList = deployJava.getJREs();

            deployJava.myInterval = setInterval("deployJava.poll()", 12000);
        }
    },
    
    done: function (name, result)
    {
    },
    
    FFInstall: function()
    {
        if (deployJava.FFInstallOption == null)
        {
            deployJava.FFInstallOption = deployJava.redirect_to_GetJava;
        }
    
        if (deployJava.FFInstallOption == deployJava.redirect_to_GetJava)            
        {
            location.href = deployJava.getJavaURL + '&returnPage=' + deployJava.returnPage
                                                  + '&locale=' + deployJava.locale +
            ((deployJava.brand != null) ? ('&brand=' + deployJava.brand) : '');
        }
        else if (deployJava.FFInstallOption == deployJava.redirect_to_XPI)
        {

            deployJava.preInstallJREList = deployJava.getJREs();

            if (deployJava.preInstallJREList.length == 0)
            {
                deployJava.myInterval = setInterval("deployJava.poll()", 12000);
            }
        }
    },
    
    
    setIEInstallOption: function(value)
    {
        if ((value != deployJava.redirect_to_GetJava) && (value != deployJava.click_to_install))
        {
            if (deployJava.debug)
            {
                alert('Bad value for IEInstallOption, using "redirect_to_GetJava".');
            }
    
            deployJava.IEInstallOption = deployJava.redirect_to_GetJava;
        }
        else
        {
            deployJava.IEInstallOption = value;
        }
    },
    
    
    setFFInstallOption: function(value)
    {
        if ((value != deployJava.redirect_to_GetJava) && (value != deployJava.redirect_to_XPI))
        {
            if (deployJava.debug)
            {
                alert('Bad value for FFInstallOption, using "redirect_to_GetJava".');
            }
    
            deployJava.FFInstallOption = deployJava.redirect_to_GetJava;
        }
        else
        {
            deployJava.FFInstallOption = value;
        }
    },
    
    
    // return true if 'installed' (considered as a JRE version string) is
    // greater than or equal to 'required' (again, a JRE version string).
    compareVersions: function(installed, required)
    {
        var a = installed.split('.');
        var b = required.split('.');
    
        for (var i = 0; i < a.length; ++i) a[i] = Number(a[i]);
        for (var i = 0; i < b.length; ++i) b[i] = Number(b[i]);
    
        if (a.length == 2) a[2] = 0;      
    
        if (a[0] > b[0]) return true;
        if (a[0] < b[0]) return false;
    
        if (a[1] > b[1]) return true;
        if (a[1] < b[1]) return false;
    
        if (a[2] > b[2]) return true;
        if (a[2] < b[2]) return false;
    
        return true;
    },
    
    
    enableAlerts: function()
    {
        deployJava.debug = true;
    },

    poll: function()
    {

        var postInstallJREList = deployJava.getJREs();           

        if ((deployJava.preInstallJREList.length == 0) && (postInstallJREList.length == 1))
        {
            clearInterval(deployJava.myInterval);
            location.href = deployJava.returnPage;
        }

        if ((deployJava.preInstallJREList.length == 1) && (postInstallJREList.length == 1) &&
            (deployJava.preInstallJREList[0] != postInstallJREList[0]))
        {
            clearInterval(deployJava.myInterval);
            location.href = deployJava.returnPage;
        }

    }
};

