//0 -- Not Installed
//1 -- Installed
//2 -- Cannot Detect

var browser = 4;
var os = 3;
var clientInstalled = 2;
var clientVersion = "";

var appletLoaded = false;
var activexLoaded = false;

var appletinit = false;
var activexinit = false;

var installinprogress = false;

var TuxClientSystemObj;

var installquestion;
var installnotice;
var msiUrl;
var tryautoinstall = true;

//0 -- Not Installed
//2 -- ActiveX Disabled 
//Other -- Client version
function is2XClientInstalled()
{
    try
    {
        TuxClientSystemObj = new ActiveXObject("TuxClientSystem.ClientSystem");
    }
    catch (err)
    {
        try
        {
            XMLDOMObj = new ActiveXObject("Microsoft.XMLDOM");
        }
        catch (err)
        {
            return 2;
        }
	   
	    return 0;
    }
    
    return TuxClientSystemObj.Version;
}

function forwardPage(clientInstalled)
{
    var returnURL = window.location.href;  
    window.location = "CheckTuxClient.aspx?os="+os+"&browser="+browser+"&installed="+clientInstalled+"&version="+clientVersion + "&url=" + returnURL + "&javaavail=" + appletinit;
}

function appendApplet()
{
    var code =   "<applet code='TuxClientDetection'  NAME='checkApplet' HEIGHT=0 WIDTH=0 MAYSCRIPT Archive='Clients/TuxClientDetection.jar'></applet>";    
    document.getElementById("code").innerHTML = code;
    appletLoaded = true; 
}

function appendActiveX()
{   
    var code = "<object classid='clsid:444B0796-91CD-4CA5-BFFF-289BF93D356D' codebase='Clients/TuxClientSystem.cab' onReadyStateChange='notifyStateChanged(this)' />";
    document.getElementById("code").innerHTML = code;
    activexLoaded = true; 
}

function notifyStateChanged(doc)
{
    if (doc.readyState != 4)
        return;
        
    try
    {
        TuxClientSystemObj = new ActiveXObject("TuxClientSystem.ClientSystem");
        activexinit = true;
    }
    catch(err) 
    {
        return;
    }
}

function installClient(bSilent)
{
    if (installinprogress)
    {
        clientVersion = TuxClientSystemObj.Version;
        if (clientVersion != '')
            return 1;

        return -1;
    }
 
    detectBrowser();
	        
    // if internet explorer is detected we use active x, otherwise java is used
    if (browser == 0 && !activexLoaded)
	       {
        appendActiveX();
    }            
    else if (navigator.javaEnabled() && !appletLoaded && !activexLoaded)
    {   
        appendApplet();	
    }
    else if (!appletLoaded && !activexLoaded)
	            {
        return 0;
    }
        
    if (appletLoaded && !appletinit)
        return -1;

    if (activexLoaded && !activexinit)
        return -1;
        
    if (bSilent || installnotice == '' || !confirm(installquestion))
        return 0;
        
	                spanElement = document.getElementById('LabelDetecting');
	                if (spanElement)
	                {
	                    txtElement = spanElement.firstChild;
	                    txtElement.nodeValue = installnotice;
	                }
    
	                clientUrlElement = document.getElementById('PanelClientUrl');
	                if (clientUrlElement)
	                {
	                    clientUrlElement.innerHTML = "<span id='Label1' class='noclient_msg'>" + installnotice +"</span>";
	                }
	                
	                clientInfoElement = document.getElementById('ClientLinkBar');
	                if (clientInfoElement)
	                {
	                    var lnk = document.getElementById('ClientUrlLink');                        
	                    clientInfoElement.removeChild(lnk);
	                    clientInfoElement.innerHTML = "<span id='ClientUrlLink'>" + installnotice +"</span>";
	                }
                
    // install using java
    if (appletinit)
	            {
        // java installation is blocking so we don't need to set installInProgress flag
        if (!document.checkApplet.InstallClient(msiUrl))
            return 0;
        
	                clientVersion = document.checkApplet.Get2xClientVersion();
	                return 1;
	            }
     
    // install using active-x
    if (activexinit)
	    {	        
        TuxClientSystemObj.StartClientDownload(msiUrl);
        installinprogress = true;
		    return -1;
		}
    
    return -1;
}

function getAppletResult()
{
    if(navigator.javaEnabled())
    {
        if(!appletLoaded)
            appendApplet();	
	
	    if (appletinit)
	    {
	        clientVersion = document.checkApplet.Get2xClientVersion();
	        return (clientVersion == "")?0:1;
	    }		    
	    else
	    {
		    return -1;
		}
    }
    else
    {
        return 2;
    }
}

function detectOS()
{
    if(navigator.platform.toUpperCase().indexOf('WIN') != -1)
        os = 0;
    else if(navigator.platform.toUpperCase().indexOf('LINUX') != -1)
        os = 1;
    else if(navigator.platform.toUpperCase().indexOf('MAC') != -1)
        os = 2;
    else
        os = 3;
}

function detectBrowser()
{
    if((navigator.userAgent.indexOf("Win")  != -1) && (navigator.userAgent.indexOf("MSIE") != -1) && (parseInt(navigator.appVersion) >= 4 ))
        browser = 0;
    else if (navigator.userAgent.indexOf("Firefox")  != -1)
        browser = 1;
    else if ((navigator.userAgent.indexOf("Safari")  != -1))
        browser = 2;
    else if ((navigator.userAgent.indexOf("Opera")  != -1))
        browser = 3;
    else 
        browser = 4;
}

function IsTuxClientInstalled() 
{
    detectBrowser();
    detectOS();
    
    if (os != 0 && os != 2) //Win and MAC supported only
        return 2; // Unknown
    
    if(browser == 1) //ex firefox
    {  
        var tuxMime = navigator.mimeTypes["application/x-2xa"];
        if(typeof(tuxMime) == "object") 
            return 1; //Installed
       
            return 0; // Not Installed
    } 
    else if(browser == 0) //IE
    {       
        var res = is2XClientInstalled();    
        if(res == 2) // if unknown use Java
        {
            return getAppletResult();
        }
        else if (res == 0) // Not Installed
        {
            return 0;
        } 
       
            clientVersion = res; // Installed
            return 1;
        }
    else
    {
        return getAppletResult();
    }
    
    return 2; // Unknown
}

// Client installation code

function tryToInstallCode(bSilent)
{
    returnval = installClient(bSilent);
    
    if (returnval == '-1')
    {
        return -1;
    }        
    else if ( returnval == '1')
    {
        forwardPage(1);
        return 1;
    }
    else
    {
        forwardPage(0);
        return 0;
    }       
}
    
var tryToInstallCount = 10;
function delayedtryToInstall()
{
    tryToInstallCode(false);
    
    if (tryToInstallCount > 0)
    {
        tryToInstallCount = tryToInstallCount - 1;
        setTimeout("delayedtryToInstall()",1000);
    }
    else
    {   
        forwardPage(0);                
    }
}
    
function tryToInstall()
{
    if (tryToInstallCode(false) == -1)
    {
	    setTimeout("delayedtryToInstall()",1000);
    }
    return returnval;
}
    
function delayedDownloadClient()
{   
    iret = tryToInstallCode(true);
    if (iret != 1)
    {
        window.location = msiUrl;
        return;
    }
    IsTuxClientInstalled(); 
    forwardPage(1);        
}
    
function DownloadClient()
{
    iret = tryToInstallCode(true);
    if (iret == -1)
    {
        setTimeout("delayedDownloadClient()",1000);
        return;
    }
    else if (iret != 1)
    {
        window.location = msiUrl;
        return;
    }
    
    IsTuxClientInstalled(); 
    forwardPage(1);
}

function GetClientEnv()
{
    detectBrowser();
    detectOS();
    
    var returnURL = window.location.href;      
    window.location = "CheckTuxClient.aspx?os="+os+"&browser="+browser+"&url=" + returnURL;
}