/********************************************************************************************************/
/************************************ GLOBAL VAR ********************************************************/
/********************************************************************************************************/
// -------------------------- DECLARE ------------------------- //
// It's the name of the installer main class.
var nameInstallerMainClass;

// It's the object that allows to wait the applet creation.
var g_thWaiter, g_thWaiterJavaDetector, g_thWaiterCheckJava;
// Indicates if launch is currently run.
var g_runLauncher;
// Indicates if application must be launch manually (value is true or false).
var g_modeLaunchManual;

// It's the object that stores all parameters to set to applet.
var g_objectParameter;
// ------------------------------------------------------------ //

// --------------------------- INIT --------------------------- //
// Initializes the installer's name.
nameInstallerMainClass = "ct.installer.CTInstaller";
g_runLauncher = false;
g_modeLaunchManual = false;
// ------------------------------------------------------------ //

/********************************************************************************************************/
/************************************** CREATE **********************************************************/
/********************************************************************************************************/
/**
 * Destroys a waiter.
 */
function destroyWaiter()
{
    // --------------------------- DELETE ------------------------- //
   
    if (!g_thWaiter)
    {
        return;
    }
   
    clearInterval(g_thWaiter);
}

/**
 * Creates a waiter.
 */
function createWaiter()
{
    // --------------------------- DELETE ------------------------- //
    
    // Destroys old one before new creation.
    destroyWaiter();
      
    // Creates a new waiter.
    g_thWaiter = setInterval("waitCreationForStart()", 50);
}

/**
 * This function allows to make a dynamic applet creation
 */
function createApplet()
{
    // --------------------------- DELETE ------------------------- //
    
    // Removes old applet if this one exist.
    removeApplet();
      
    // Starts wait.
    startWaitAppletCreation();
        
    // Applet creation.
    addApplet();
}

/**
 * Allows to wait applet creation.
 */
function startWaitAppletCreation()
{
    var javaDetect;
    // --------------------------- INIT --------------------------- //
    
    javaDetect = document.getElementById("javaDetector");
    javaDetect.startWaitAppletCreation();
}

/**
 * This function allows to add a dynamic applet
 */
function addApplet()
{
    var vDivApplet,
    vApplet,
    vDivParent;
    var vParam;
    var count,
    index;
    // --------------------------- INIT --------------------------- //
    // Create a dynamic <DIV>
    vDivApplet = document.createElement("div");
    vDivApplet.id = "divApplet";
    vDivApplet.name = "divApplet";
    
    vApplet = document.createElement("applet");
    vApplet.setAttribute("code", nameInstallerMainClass);
    vApplet.setAttribute("codebase", "./APPLET/JAVA/");
    vApplet.setAttribute("archive", "CIInstaller.jar");
    vApplet.setAttribute("name", "CIApplet");
    vApplet.setAttribute("id", "CIApplet");
    vApplet.setAttribute("width", "1");
    vApplet.setAttribute("height", "1");
    vApplet.setAttribute("modeLaunchManual", g_modeLaunchManual);
    
    // Adds parameters.
    vParam = document.createElement("param");
    vParam.setAttribute("name", "base");
    vParam.setAttribute("value", nameBase);
    vApplet.appendChild(vParam);
    
    //Add applet to the <DIV> for applet
    vDivApplet.appendChild(vApplet);
    
    // Add the <DIV> that is associated to the applet in the main div
    vDivParent = document.getElementById("divAppletCedreo");
    vDivParent.appendChild(vDivApplet);
}

/**
 * Remove an applet.
 */
function removeApplet()
{
    var vDivApplet,
    vApplet,
    vDivParent;
    // --------------------------- INIT --------------------------- //
    
    // Test if the "div" that contains the applet does already exist.
    vDivApplet = document.getElementById("divApplet");
    if (vDivApplet)
    {
        // If exist then delete we must remove the applet div from the page and its child
        vApplet = document.getElementById("CIApplet");
        vDivApplet.removeChild(vApplet);
        
        vDivParent = document.getElementById("divAppletCedreo");
        vDivParent.removeChild(vDivApplet);
    }
}

/**
 * Allows to wait the applet creation.
 */
function waitCreationForStart()
{
    var vApplet;
    // -------------------------- DECLARE --------------------------//
    
    vApplet = document.getElementById("CIApplet");
    try
    {
        if (vApplet == null || vApplet.isActive() == false || g_runLauncher == true)
        {
            return;
        }
    }
    catch (error)
    {
        // Waits that applet is visible. It's a way to manage FireFox issue.
        return;
    }
    
    if (vApplet.isCreated() == true)
    {
        // Indicates that launcher running.
        g_runLauncher = true;
        
        // Stops the waiter because applet is now created.
        destroyWaiter();
        
        // Calls applet.
        vApplet.clearArrParameterSpecific();
        addArrParameterSpecificToApplet(vApplet, g_objectParameter);
        vApplet.callLaunch();
        
        // Indicates that launcher is terminated.
        g_runLauncher = false;
    }
}

/**
 * Waits creation of java detector.
 */
function waitJavaDetector()
{
    var vApplet;
    // -------------------------- DECLARE --------------------------//
    
    vApplet = document.getElementById("javaDetector");
    try
    {
        if (vApplet.isActive() == false || vApplet.startWaitAppletCreation())
        {
            return;
        }
    }
    catch (error)
    {
        // Waits that applet is visible. It's a way to manage FireFox issue.
        return;
    }
    
    // Stops the waiter because applet is now created.
    clearInterval(g_thWaiterJavaDetector);
    
    // Calls applet.
    createApplet();
    
    // Allows to be sure that applet is created before calling room construction.
    createWaiter();
}

/**
 * Waits java checking.
 */
function waitCheckJava()
{
    // -------------------------- DECLARE --------------------------//
    
    if (g_checkJava == false)
    {
        return;
    }
    
    // Desactivates waiter.
    clearInterval(g_thWaiterCheckJava);
    
    // Checks Java state.
    if (checkJava() == false)
    {
        return;
    }
    
    // Controls java detection before others actions.
    g_thWaiterJavaDetector = setInterval("waitJavaDetector()", 50);
}

/**
 * Manages uninstalled Java case.
 */
function manageJavaUninstalled()
{
    // -------------------------- DECLARE --------------------------//
    
    // Indicates that java isn't installed.
    alert(messageJavaUninstalled);
    
    // Does to url that allows to download java.
    document.location = strUrlJavaInstal;
}

/**
 * Manages disabled Java case.
 */
function manageJavaDisabled()
{
    // -------------------------- DECLARE --------------------------//
    
    // Indicates that java is disabled.
    alert(messageJavaDisabled);
}

/**
 * Checks java state.
 * @return Returns true if Java is ok.
 */
function checkJavaChrome()
{
    // -------------------------- DECLARE --------------------------//
    
    if (g_javaInstalled == true && g_javaIsActive == false)
    {
        return true;
    }
    
    return g_javaInstalled;
}

/**
 * Checks java state.
 * @return Returns true if Java is ok.
 */
function checkJavaOpera()
{
    // -------------------------- DECLARE --------------------------//
    
    if (g_javaInstalled == true && g_javaIsActive == false)
    {
        return false;
    }
    
    return g_javaInstalled;
}

/**
 * Checks java state.
 * @return Returns true if Java is ok.
 */
function checkJava()
{
    var res;
    // -------------------------- DECLARE --------------------------//
    
    if (BrowserDetect.browser == "Chrome")
    {
        res = checkJavaChrome();
    }
    else if (BrowserDetect.browser == "Opera")
    {
        res = checkJavaOpera();
    }
    else
    {
        if (g_javaIsActive == true)
        {
            res = true;
        }
        else
        {
            res = g_javaInstalled;
        }
    }
    
    if (res == false)
    {
        manageJavaUninstalled();
    }
    
    if (res == true)
    {
        if (g_javaEnabled == false)
        {
            manageJavaDisabled();
        }
    }
    
    return res;
}

/**
 * Starts applet with a configuration file.
 * @param p_objectParamter is object that stores all parameters.
 */
function startAppletWithArrParameterSpecific(p_objectParamter)
{
    var vApplet;
    // -------------------------- DECLARE --------------------------//
    
    // Indicates manual launch.
    g_modeLaunchManual = true;
        
    // Stores the url to send in order to manage the case when applet is created.
    g_objectParameter = p_objectParamter;
    
    // Checks Java state.
    if (checkJava() == false)
    {
        return;
    }
    
    // Controls java detection before others actions.
    g_thWaiterJavaDetector = setInterval("waitJavaDetector()", 50);
}

/**
 * Starts applet with a configuration file.
 * @param p_objectParamter is object that stores all parameters.
 */
function startAppletWithArrParameterSpecificDirect(p_objectParamter)
{
    // -------------------------- DECLARE --------------------------//
    
    // Indicates automatic launch.
    g_modeLaunchManual = false;
    
    // Stores the url to send in order to manage the case when applet is created.
    g_objectParameter = p_objectParamter;
    
    // Waits end of java checking.
    g_thWaiterCheckJava = setInterval("waitCheckJava()", 50);
}
