//This Javascript has code that checks the browser and java versions 
//installed on a clients machine.
//
//
//*************************************************************
// Date         Version          Author          Description
// ??????????   1.00             KAZ             Original
//
// 14/03/2005   1.0.1		 KAZ		 Applet tag for other browsers whichwill include FireFox Browser
//
// 12/12/2005   1.0.2            KAZ             Added Vunerability check
//
// 14/08/2007	1.0.3			NV				Added CSI plugin checks and applet rendering via the CSI plugin if it is present
//12/06/2008 1.11 PL Added Skip's mod for _csi_isJavaInstalledForNetscape. OS compatibility.						

// Client Javascript functions to support BAF CSI.

// This version uses an indirect method of calling an applet that 
// is supported by both Microsoft and Netscape.

// The indirect method works by having javascript modify certain
// variables that are monitored by the the applet. Once the last
// key variable is set, the applet processes these variables and
// then runs a javascript call back function.


//======================================================================
// Constants for integrating with the CSI Applet.
// THESE VALUES MUST NOT BE CHANGED.


// The following constants are used by scripts that cannot do javascript to java communication.
var _csi_codeBase;


// Constants used for CSI installation.
// Note: The ^ character is replaced by new line characters in the javascript and java message boxes
var _csi_INSTALL_REDIRECTION_MESSAGE = "The CSI is not installed on this computer or is out of date.^";
	_csi_INSTALL_REDIRECTION_MESSAGE += "You will now be re-directed to install the latest version of this software.^";
	_csi_INSTALL_REDIRECTION_MESSAGE += "You can use our program when you have this installed.";


var _csi_CSIHTTP_INSTALL_PAGE = "./CDInstallJavaCSI.htm";
var _csi_CSICD_INSTALL_PAGE = "./CDInstallJavaCSI.htm";

var _csi_CSI_INSTALL_PAGE = "./CDInstallJavaCSI.htm"
var _csiCIA_NetscapeJavaVersion = "";

//Valid values are DEV TEST PROD
//:01 Refers to the Pilot messages displayed by the Applet
//:02 Refers to the Post Pilot messages displayed by the Applet
var _csiCIA_Env = "PROD:02";
var _installationMessagedisplayed = false;

// Constants for checking/using the CSI Plugin
var _csi_CSI_PLUGIN_MIME_TYPE_MOZILLA 	= "application/x-AuGovCsiPlugin"; 		// Mozilla MIME type for the CSI Plugin
var _csi_CSI_PLUGIN_PLID_MOZILLA	= "@csi.business.gov.au/CsiPlugin";		// The PLID for Mozilla CSI Plugin
var _csi_CSI_PLUGIN_CLASSID 		= "clsid:FEAD952B-CEED-4b17-A8D2-2DCD0DF96271"; // Class ID for IE CSI Plugin
var _csi_CSI_PLUGIN_INSTALLED_IE 	= "CsiPlugin.isInstalled"; 			// Class ID name for checking if IE CSI Plugin installed


function _csi_isCsiPluginInstalledForIE()
{
    // Immediately return false if the Browser isn't IE.
    if (!_csi_isIe())
  	    return false;

	var csiInstalled = false;

	// try/catch is available in IE5+, Mozilla 1.0, and Netscape 6.
	try
	{
		var tempObject = new ActiveXObject(_csi_CSI_PLUGIN_INSTALLED_IE);
		delete (tempObject);
		tempObject = null;
		csiInstalled = true;
	}
	catch(err)
	{
		// No need to do anything
	}

	return csiInstalled;
}

/***************************************************************************
 *
 * Determines whether the CSI Plugin is installed on this machine. The plugin
 * is available in CSI v3.1 onwards and replaces the Java plugin. This
 * function is specific for Mozilla-based browsers.
 *
 * @return  true if CSI Plugin is installed and enabled for this Mozilla
 *          based browser, false otherwise.
***************************************************************************/
function _csi_isCsiPluginInstalledForMozilla()
{
    // Immediately return false if this is IE.
    // todo: Check what happens for IE on Mac.
    if (_csi_isIe())
  	    return false;

    // Refresh plugins state. 'false' is so we don't reload or activate them
  	navigator.plugins.refresh(false);

  	var installedMimeTypes = window.navigator.mimeTypes;
  	var csiInstalled = false;

	// sanity checks
	if (!installedMimeTypes || !installedMimeTypes.length || installedMimeTypes.length <= 0)
		return false;

    // See if the browser handles this MIME type
    var csiMimePlugin = installedMimeTypes[_csi_CSI_PLUGIN_MIME_TYPE_MOZILLA];

  	// See if there is a plugin associated for the MIME type
	if (csiMimePlugin && csiMimePlugin.enabledPlugin)
	{
		// todo: Additional checks can be performed in here by querying
		// the plugin object. For now we assume that the existance of a
		// plugin for our MIME type is enough.
		csiInstalled = true;
	}

	return csiInstalled;
}


/***************************************************************************
 *
 * Returns true if the browser is internet explorer, false otherwise.
 ***************************************************************************/
function _csi_isInternetExplorer()
{
	return (navigator.appName.toLowerCase().indexOf("internet explorer") != -1);
}


/***************************************************************************
 *
 * Returns true if the browser is netscape 4, false otherwise.
 ***************************************************************************/
function _csi_isNetscape4()
{
	if (navigator.appName.toLowerCase().indexOf("netscape") != -1)
	{
		if (navigator.appVersion.charAt(0) == '4')
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		return false;
	}
}

/***************************************************************************
 *
 * Returns true if the browser is netscape 7, false otherwise.
 ***************************************************************************/
function _csi_isNetscape7()
{
	if (navigator.userAgent.indexOf("Netscape") != -1)
	{
		return true;
	}
	else
	{
		return false;
	}
}
/***************************************************************************
 *
 * Checks if the java plugin is install on Netscape browser.
 *
 * @return	true if the plugin is installed, false otherwise.
 ***************************************************************************/
function _csi_isJavaInstalledForNetscape()
{
	var isJavaInstalled = false;

	for (var pluginIndex = 0; pluginIndex < navigator.plugins.length; pluginIndex++)
	{
		var thePlugin = navigator.plugins[pluginIndex];
		for (var mimeTypeIndex = 0; mimeTypeIndex < thePlugin.length; mimeTypeIndex++)
		{
			if (_csi_isCorrectPluginType(thePlugin[mimeTypeIndex].type))
			{
				isJavaInstalled = true;
				break;
			}

			if (isJavaInstalled)
			{
				break;
			}
		}
	}

	return isJavaInstalled;
}

/**************************************************************************
 *
 * Checks if the plugin mime type is the version of the java plugin that we are
 * after.
 *
 * @return true if the plugin is the correct type.
 **************************************************************************/
function _csi_isCorrectPluginType(pluginType)
{
	if ((pluginType.indexOf("application/x-java-applet") == 0)
			&& (pluginType.indexOf("1.3.1") > -1))
	{
		return true;
	}
	else
	{
		return false;
	}
}


/***************************************************************************
 *
 * Checks if the java plugin is install on Internet Explorer browser.
 *
 * @return	true if the plugin is installed, false otherwise.
 ***************************************************************************/
var _csi_isIeJavaInstalled;
function _csi_isJavaInstalledForIE()
{	

	try{
		document.applets[0].isAppletRunning();
	}
	catch (Exception)
	{
		return false;
	}

	return true;	

}


/***************************************************************************
 *
 * Checks if the current operating system is an Apple Mac
 ***************************************************************************/
function _csi_isMac()
{
	return (navigator.userAgent.indexOf("Mac") > -1);
}


/***************************************************************************
 *
 * Checks if the current browser is Internet Explorer
 ***************************************************************************/
function _csi_isIe()
{
	return (navigator.appName.indexOf("Microsoft") > -1);
}


/***************************************************************************
 *
 * Creates the string used by javascript to open a new window.
 * @param	windowHeight	The height of the new window.
 * @param	windowWidth		The width of the new window.
 *
 * @return	The string used by javascript to open a new window.
 ***************************************************************************/
function _csi_createWindowParams(windowHeight, windowWidth)
{
	var windowTop = _csi_getWindowCentreTop(windowHeight);
	var windowLeft = _csi_getWindowCentreLeft(windowWidth);
	var windowParams = "height=" + windowHeight + ",width=" + windowWidth 
			+ ",screenX=" + windowLeft + ",screenY=" + windowTop 
			+ ",left=" + windowLeft + ",top=" + windowTop 
			+ ",status=no,directories=no,toolbar=no,menubar=no,location=no";

	return windowParams;
}


/***************************************************************************
 *
 * Finds to position that the top of the window must be for the window to be 
 * in the middle of the screen.
 * @param	windowHeight	The height of the new window.
 *
 * @return	Top position of the new window.
 ***************************************************************************/
function _csi_getWindowCentreTop(windowHeight)
{
	return (screen.height / 2) - (windowHeight / 2);
}


/***************************************************************************
 *
 * Finds to position that the left of the window must be for the window to be 
 * in the middle of the screen.
 * @param	windowWidth		The width of the new window.
 *
 * @return	Left position of the new window.
 ***************************************************************************/
function _csi_getWindowCentreLeft(windowWidth)
{
	return (screen.width / 2) - (windowWidth / 2);
}


/***************************************************************************
 * if the url protocal is file then we are running the CD version
 ***************************************************************************/
function _csi_setupCSIInstallation()
{
	if (window.location.protocol == "file:")
	{
		_csi_CSI_INSTALL_PAGE = _csi_CSICD_INSTALL_PAGE;
	}
	else
	{
		_csi_CSI_INSTALL_PAGE = _csi_CSIHTTP_INSTALL_PAGE;
	}
	return true;
}


///////////////////////////////////////////////////////////////////////////////////
/////			The following functions are the only functions 			/////
/////			to be called by the application developer				/////
///////////////////////////////////////////////////////////////////////////////////


/***************************************************************************
 *
 * Checks if the java plugin is installed. If it is not then the user
 * will be redirected to the CSI install page.
 *
 * NB: Edited by James Newell 30/03/2007 v3.2.0
 * This function now checks if the CSI Plugin is installed and will 'pass'
 * if either the Java or CSI Plugin is installed.
 ***************************************************************************/
function checkForJavaPlugin()
{
	var isJavaInstalled = false;

	if (_csi_isMac())
	{
		// Do nothing java is always installed on Mac osX (which is the only version of Mac we support).
		// v3.2.0: Don't check for CSI Plugin either as no support for this on Mac.
		return;
	}

	if (_csi_isIe())
	{
		// N.B.: Check for the CSI Plugin before the Java Plugin because we prefer the CSI Plugin
		isJavaInstalled = _csi_isCsiPluginInstalledForIE() || _csi_isJavaInstalledForIE();
	}
	else
	{
		// N.B.: Check for the CSI Plugin before the Java Plugin because we prefer the CSI Plugin
		isJavaInstalled = _csi_isCsiPluginInstalledForMozilla() || _csi_isJavaInstalledForNetscape();
	}

	if (isJavaInstalled == false)
	{
		var message;

		// replace the ^ character with new line characters.
		message = _csi_INSTALL_REDIRECTION_MESSAGE.replace(/\^/g, '\n');
		alert(message);
		window.location.href = _csi_CSI_INSTALL_PAGE;
	}
}

function displayInstallationPage()
{
	var message;

	// replace the ^ character with new line characters.
	message = _csi_INSTALL_REDIRECTION_MESSAGE.replace(/\^/g, '\n');
	_installationMessagedisplayed  = true;
	alert(message);
	window.open(_csi_CSI_INSTALL_PAGE, "CSI_INSTALL_WINDOW");
//	window.location.href = "./DummyPage.htm";			
//	window.close();
}

/***************************************************************************
 *
 * Called by inline script in the form as it is rendered to dynamically
 * bound the applet. The only real reason for doing this is to keep this
 * code out of the framework and have it here in this central location.
 * @codeBase	The directory where the applet jar can be found.
 ***************************************************************************/
function renderCsiApplet(codeBase)
{
	_csi_setupCSIInstallation();

	_csi_codeBase = codeBase;

	var appletHtml = "";

	if (_csi_isMac())
	{
		appletHtml += '<APPLET 	width="0"																';
		appletHtml += '			height="0"																';
		appletHtml += '			align="baseline"															';
		appletHtml += '			codebase="' + codeBase + '"													';
    	      appletHtml += '			mayscript="true"																	';
		appletHtml += '			code="au.gov.ato.csicra.main.StartApplet"					';
		appletHtml += '			archive="csiCRA.jar">												';
		appletHtml += '	<PARAM NAME="archive"  VALUE="csiCRA.jar">									';
		appletHtml += '	<PARAM NAME="code"     VALUE="au.gov.ato.csicra.main.StartApplet">	';
		appletHtml += '	<PARAM NAME="type"     VALUE="application/x-java-applet;version=1.4.1_02">				';
		appletHtml += '	<PARAM NAME="install_redirection_message" VALUE="' + _csi_INSTALL_REDIRECTION_MESSAGE +'">';
		appletHtml += '	<PARAM NAME="csi_install_page" 		VALUE="' + _csi_CSI_INSTALL_PAGE + '">			';
		appletHtml += '	<PARAM NAME="ENV" 		VALUE="' + _csiCIA_Env  + '">			';
		appletHtml += '	<PARAM NAME="BROWSER" 		VALUE="MAC">			';
		appletHtml += '	<PARAM NAME="VUNERABILITY" 	VALUE="TRUE">			';
		appletHtml += '</APPLET>																			';

	}
	else if (_csi_isIe())
	{
		if (_csi_isCsiPluginInstalledForIE())
		    appletHtml += '<OBJECT	classid="' + _csi_CSI_PLUGIN_CLASSID + '"								';
		else
		    appletHtml += '<OBJECT	classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"';
		appletHtml += '			width="0"										';
		appletHtml += '			height="0"										';
		appletHtml += '			align="bottom"										';
		appletHtml += '			name="csiCRA_applet"									';
		appletHtml += '			mayscript="true">									';
	 	appletHtml += '	<PARAM NAME="codebase" VALUE="' + codeBase + '">							';
		appletHtml += '	<PARAM NAME="archive"  VALUE="csiCRA.jar">								';
		appletHtml += '	<PARAM NAME="code"     VALUE="au.gov.ato.csicra.main.StartApplet">					';
		appletHtml += '	<PARAM NAME="type"     VALUE="application/x-java-applet;version=1.4.1_02">				';
		appletHtml += '	<PARAM NAME="install_redirection_message" VALUE="' + _csi_INSTALL_REDIRECTION_MESSAGE +'">';
		appletHtml += '	<PARAM NAME="csi_install_page" 		VALUE="' + _csi_CSI_INSTALL_PAGE + '">			';
		appletHtml += '	<PARAM NAME="ENV" 		VALUE="' + _csiCIA_Env  + '">			';
		appletHtml += '	<PARAM NAME="BROWSER" 		VALUE="IE">			';
		appletHtml += '	<PARAM NAME="VUNERABILITY" 	VALUE="TRUE">			';
		appletHtml += '</OBJECT>																			';
	}
	else if (_csi_isNetscape7())
	{

			appletHtml += '<APPLET	width="0"																';
			appletHtml += '			height="0"																';
			appletHtml += '			align="baseline"															';
			appletHtml += '			name="csi_applet"															';
			appletHtml += '			mayscript="true"															';
			appletHtml += '			codebase="' + codeBase + '"													';
			appletHtml += '			archive=="csiCRA.jar"												';
			appletHtml += '			code="au.gov.ato.csicra.main.StartApplet">					';
			appletHtml += '	<PARAM NAME="archive"  VALUE="csiCRA.jar">								';
			appletHtml += '	<PARAM NAME="code"     VALUE="au.gov.ato.csicra.main.StartApplet">					';
			appletHtml += '	<PARAM NAME="type"     VALUE="application/x-java-applet;version=1.4.1_02">				';			
			appletHtml += '	<PARAM NAME="install_redirection_message" VALUE="' + _csi_INSTALL_REDIRECTION_MESSAGE +'">';
			appletHtml += '	<PARAM NAME="csi_install_page" 		VALUE="' + _csi_CSI_INSTALL_PAGE + '">			';
			appletHtml += '	<PARAM NAME="ENV" 		VALUE="' + _csiCIA_Env  + '">			';
			appletHtml += '	<PARAM NAME="BROWSER" 		VALUE="Netscape">			';
			appletHtml += '	<PARAM NAME="VUNERABILITY" 	VALUE="TRUE">			';
			appletHtml += '</APPLET>																			';
	}
	else
	{
			if(_csi_isCsiPluginInstalledForMozilla())
    	    {
    	        appletHtml += '<embed   type="' 			+ _csi_CSI_PLUGIN_MIME_TYPE_MOZILLA + '"                                                     ';
    	        appletHtml += '		height=				"0"																			';
		        appletHtml += '		width=				"0"																			';
    	        appletHtml += '		align=				"baseline"																	';
    	        appletHtml += '		name=				"csi_applet"																	';
    	        appletHtml += '		mayscript=				"true"																	';
    	        appletHtml += '		codebase="' 			+ codeBase + '"													';
    	        
    	        appletHtml += '		archive=			"csiCRA.jar"																';
    	       // appletHtml += '		archive=			"../../eclipse-SDK-3.2-win32.zip"																';

    	        appletHtml += '		code=				"au.gov.ato.csicra.main.StartApplet"											';
    	        appletHtml += '	        install_redirection_message="'  + _csi_INSTALL_REDIRECTION_MESSAGE + '"           	';
    	        appletHtml += '	        csi_install_page="'             + _csi_CSI_INSTALL_PAGE + '"	    				';
    	        appletHtml += '	        ENV="'             + _csiCIA_Env + '"	    				';
	            appletHtml += '	        BROWSER="Other"	    				';
	            appletHtml += '	        VUNERABILITY="TRUE"	    				';
    	        appletHtml += '>                                                                                            ';
    	        appletHtml += '</embed>                                                                                     ';
    	    }
    	    else // Mozilla but no CSI Plugin
    	    {
    	        appletHtml += '<APPLET 	width="0"																			';
    	        appletHtml += '			height="0"																			';
    	        appletHtml += '			align="baseline"																	';
    	        appletHtml += '			name="csi_applet"																	';
    	        appletHtml += '			mayscript="true"																	';
    	        appletHtml += '			codebase="' + codeBase + '"													';
    	        appletHtml += '			archive="csiCRA.jar"																';
    	        appletHtml += '			code="au.gov.ato.csicra.main.StartApplet">											';
    	        appletHtml += '	<PARAM NAME="install_redirection_message" VALUE="' + _csi_INSTALL_REDIRECTION_MESSAGE + '">	';
    	        appletHtml += '	<PARAM NAME="csi_install_page" 		VALUE="' + _csi_CSI_INSTALL_PAGE + '">					';					
	        	appletHtml += '	<PARAM NAME="ENV" 		VALUE="' + _csiCIA_Env  + '">			';
				appletHtml += '	<PARAM NAME="BROWSER" 		VALUE="Other">			';
				appletHtml += '	<PARAM NAME="VUNERABILITY" 	VALUE="TRUE">			';
    	        appletHtml += '</APPLET>';
    	    }																
	}

	document.writeln(appletHtml);
}

