
/* $Id: ClientCheck.js 107789 2011-01-13 12:51:35Z v.sakhovski $
 * Copyright (C) 2006 IP Labs GmbH <http://www.iplabs.de/>
 * All rights reserved.
 */
 
 /**
 * @fileoverview
 *
 * Contains the static ClientCheck class. Depends on Utils class.
 *
 * @author Thorsten Schueller <t.schueller@iplabs.de>
 * @version $Revision: 107789 $
 */

/** External Utils @type Class @jscram.public var="Utils" */
var Utils;

/**
 * Static constructor of the ClientCheck.
 *
 * @class <p>The static ClientCheck class provides the test which 
 * client type (Java, ActiveX, XHTML) should be activated.</p>
 *
 * @constructor
 */

function ClientCheck()
{
    // Empty
}

/** The callback function which is called after the test. @type Function */
ClientCheck.callBackFunction = null;

/** Is Java installed. @type Boolean */
ClientCheck.isJava           = false;

/** Is ActiveX allowed. @type Boolean */
ClientCheck.isActiveX        = false;

/** Is user local admin. @type Number */
ClientCheck.isLocalAdmin  = 0;

/** Is Jordan ActiveX installed. @type Boolean */
ClientCheck.isActiveXJordan  = false;

/** The interval object. @type Object */  
ClientCheck.javaCheckInterval = null;

/** The interval time (in ms) for the java check id ActiveX is allowed. @type Number */
ClientCheck.intervalTime = 100;

/** internal counter for the interval. @type Number */
ClientCheck.checkIsJavaCounter = 0;

/** Number of max interval calls if activeX not supported . @type Number */
ClientCheck.maxCheckIsJava = 100;

/** Number of max interval calls if activeX is supported . @type Number */
ClientCheck.maxCheckIsJavaActiveX = 10;

/** Reference to java version applet to trigger if connection cannot be established @type Object */
ClientCheck.javaVersionAppletReference = null;

/**
 * Public success function accessible from the VersionApplet.
 */  
function javaCheckSuccess() 
{
	ClientCheck.activateJava();
}

/**
 * Public failue function accessible from the VersionApplet.
 */ 
function javaCheckFailure() 
{
	this.checkIsJavaCounter = this.maxCheckIsJava;
}
    
/**
 * Private method: (de)activates Java, depending on appletts answer.
 *
 * @param {Boolean} bool
 *			  False if java is not usable on system, true otherwise.
 */ 
ClientCheck.activateJava = function() 
{
    this.isJava = true;
};


/**
 * Checks if java applet has set the isJava var and call
 * the callBack funtion. 
 */
ClientCheck.checkIsJava = function()
{
    var abortInterval;
    
    this.checkIsJavaCounter++;

    abortInterval = this.isActiveX ? 
        this.checkIsJavaCounter >= this.maxCheckIsJavaActiveX:
        this.checkIsJavaCounter >= this.maxCheckIsJava; 

    // try to establish connection to applet again
    if (!this.isJava && this.javaVersionAppletReference)
    {
    	try
    	{
    		this.javaVersionAppletReference.connect();
    	}
    	catch (e)
    	{
    		// nothing
    	}
    }

    if (this.isJava || abortInterval)
    {
    	if (this.javaCheckInterval !== undefined) 
        {
            window.clearInterval(this.javaCheckInterval);
        }
 
        if (this.callBackFunction !== null)
        {
            this.callBackFunction(this.isJava, this.isActiveX, this.isActiveXJordan, this.isLocalAdmin);
        }
    }  
};
 
/**
 * Run the ClientCheck (ActX, Java) and prepare results.
 * 
 * @param {Function} callBackFunction
 *     The callback function. 
 */       
ClientCheck.run = function(callBackFunction, versionAppletReference) 
{
    if (callBackFunction !== undefined)
    {
        this.callBackFunction = callBackFunction;
    }

    if (versionAppletReference !== undefined)
    {
    	this.javaVersionAppletReference = versionAppletReference;
    }

    if (Utils.isIE(3))
    {
    	this.isActiveX = Utils.checkActiveXObject("Shell.UIHelper");
    	
    	if (this.isActiveX)
    	{
    		// signals activex is generally enabled, we now check for admin rights
	    	this.isLocalAdmin = Utils.checkLocalAdminRights();
    	}
    }
	else
	{
		this.isActiveX = false;
	}

    this.isActiveXJordan = this.isActiveX ?
         Utils.checkActiveXObject("JordanApplet.JordanUploader") : false;
    
    this.javaCheckInterval = window.setInterval('ClientCheck.checkIsJava()', this.intervalTime); 
};

