/********************************************************************************************************/ 
/************************************ GLOBAL VAR ********************************************************/
/********************************************************************************************************/
// -------------------------- DECLARE ------------------------- //
// Applet variables
//Window size
var widthWindow;
var heightWindow;	

// It's an array that contains all applet <PARAM>
var arrParams;

// It's message for java uninstalled error.
var messageJavaUninstalled;
// It's url of page that provides the way to instal java.
var strUrlJavaInstal;

// It's the name of the installer main class.
var nameInstallerMainClass;
var nameBase;
// ------------------------------------------------------------ //

// --------------------------- INIT --------------------------- //
// Get the parent <DIV>
arrParams = new Array();

//Initialize
widthWindow = computeWidthWindow();
heightWindow = computeHeightWindow();

// Don't put brakets because we replace the default function of "window.onresize" with "computeWindowSize".
window.onresize = computeWindowSize;

// Initializes java variable.
messageJavaUninstalled="Java isn't installed";
strUrlJavaInstal="http://www.cedreo.com";

// Initializes the installer's name.
nameInstallerMainClass = "ct.installer.CTInstaller";
nameBase = "HOMESPIRIT";
if (nameInstallerMainClass == "Empty")
{
	alert("Installer main class is empty.");
}
// ------------------------------------------------------------ //

/********************************************************************************************************/ 
/*************************************** INFO ***********************************************************/
/********************************************************************************************************/
// Test if it's internet explorer
var InternetExplorer = navigator.appName.indexOf("Microsoft") != -1;

// Test if java is activated on the browser
if (navigator.javaEnabled() == false)
{
	alert("This page requires Java to be enabled");
}

//Add java detector
document.writeln('<div style="position:absolute; left:-1px; top:-1px;" id="divJavaDetector" name="divJavaDetector" >'); 
document.writeln('<APPLET code="ci.detector.java.CIJavaDetector" codebase="./APPLET/JAVA/" archive="CIJavaDetector.jar" width="1" height="1" id="javaDetector" name="javaDetector" mayscript>');
document.writeln('</APPLET>');
document.writeln('</div>');

// Add applet cedreo.
document.writeln('<div  style="position:absolute; left:0px; top:0px;" width="1" height="1" id="divAppletCedreo" name="divAppletCedreo">');
document.writeln('</div>');

/********************************************************************************************************/ 
/************************************** CREATE **********************************************************/
/********************************************************************************************************/ 
/**
 * This function allows to make a dynamic applet creation
 */
function createApplet()
{
	// --------------------------- DELETE ------------------------- //
	removeApplet();
	// ------------------------------------------------------------ //
	
	// Java detection
	if (javaDetection()==true)
	{
		// Starts wait.
		startWaitAppletCreation();
	
		// Applet creation.	
		addApplet();
	}
	else
	{
		// Indicates that java isn't installed.
		alert(messageJavaUninstalled);
		
		// Does to url that allows to download java.
		document.location = strUrlJavaInstal;
	}
}

/**
 * Allows to wait applet creation.
 */
function startWaitAppletCreation()
{
	var javaDetect;
	// --------------------------- INIT --------------------------- //
	
	javaDetect = document.getElementById("javaDetector");
	javaDetect.startWaitAppletCreation();
}

/**
 * This function allows to detect if java exists.
 */
function javaDetection()
{
	var javaDetect, javaVersion;
	var javaExists;
	// --------------------------- INIT --------------------------- //
	
	javaDetect = document.getElementById("javaDetector");
	
	javaExists = true;
	try
	{
		javaVersion = javaDetect.getVersion();
	}
	catch(error)
	{
		javaExists = false;
	}
	
	return true;
}

/**
 * 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");

        // 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);
	}
}
