// Constants
var MSJVM_COMPONENT_ID = "{08B0E5C0-4FCB-11CF-AAA5-00401C608500}";
var MS_IE_COMPONENT_ID = "{89820200-ECBD-11cf-8B85-00AA005B4383}";
var MS_MEDIAPLAYER_COMPONENT_ID = "{22d6f312-b0f6-11d0-94ab-0080c74c7e95}";
var MS_OUTLOOK_COMPONENT_ID = "{44BBA840-CC51-11CF-AAFA-00AA00B6015C}";
var DIRECT_SHOW = "{44BBA848-CC51-11CF-AAFA-00AA00B6015C}";
var MS_VBSCRIPT_COMPONENT_ID = "{4f645220-306d-11d2-995d-00c04f98bbc9}";
var FLASH_COMPONENT_ID = "{D27CDB6E-AE6D-11cf-96B8-444553540000}";

var ACROBAT_CLASS_ID = "CA8A9780-280D-11CF-A24D-444553540000";

var isMSIE = (navigator.userAgent.indexOf('MSIE') != -1);
var isWindows = (navigator.userAgent.indexOf('Win') != -1);
var isVBScriptSupported = (isMSIE && isWindows);


// Entry Function
function getSettings() {	

	//Initialise form variables
	var objForm = document.ClientSettings;

	setJavaScriptEnabled(objForm);	//JavaScript
	setJavaEnabled(objForm);		//Java
	setActiveJVMDetails(objForm);	//Active JVM	
	setCookiesEnabled(objForm);		//Cookies	
	setMSJVMDetails(objForm);		//MSJVM
	setJavaPluginDetails(objForm);	//Java Plugin (Sun)
	setJavaAppletEnabled(objForm);	//Java Applet Status
	setScreenDetails(objForm);		//Screen Width & Height
	setBrowserDetails(objForm);		//Browser Name, Version, etc.
	setAcrobatDetails(objForm);		//Acrobat Reader
	//setFlashDetails(objForm);		//Macromedia Flash
	//setVBScriptDetails(objForm);	//VBScript
	setConnectionType(objForm);		//Connection Type

	//var objJVM = new JVM(objForm, document.DetectJVMApplet, clientCaps);
	//alert(objJVM.MSJVMInstalled());

	//submit the form
	//objForm.submit();
}


////////  SET Environment Values \\\\\\\\\

function setJavaScriptEnabled(objForm) {
	objForm.JavaScriptEnabled.value = "true";
}

function setJavaEnabled(objForm) {
	var JavaEnabled;

	if(navigator.javaEnabled()) {
		JavaEnabled = "true";
	}
	else {
		JavaEnabled = "false";
	}

	objForm.JavaEnabled.value = JavaEnabled;
}

function setActiveJVMDetails(objForm) {
	objForm.JVMType.value = getJVMType();
	objForm.JVMVersion.value = getJVMVersion();
}

function setCookiesEnabled(objForm) {
	objForm.CookieEnabled.value = getCookieEnabled();
}

function setMSJVMDetails(objForm) {
	var MSJVMBuild, MSJVMInstalled;

	if (isMSIE) {
		MSJVMBuild = getMSJVMBuild();

		if (MSJVMBuild != "") {
			MSJVMInstalled = "Installed";
		}
		else {
			MSJVMBuild = "-";
			MSJVMInstalled = "Not Installed";
		}
		/*
		else {
			var objCaps = clientCaps;
			var isMSJVMInstalled = isComponentInstalled(objCaps, MSJVM_COMPONENT_ID);

			if (isMSJVMInstalled) {
				MSJVMBuild = getComponentVersion(objCaps, MSJVM_COMPONENT_ID);
				MSJVMInstalled = "Installed"
			}
			else {
				MSJVMBuild = "-";
				MSJVMInstalled = "Not Installed";
			}
		}
		*/
	}
	else {
		MSJVMBuild = "-";
		MSJVMInstalled = "Not Detectable";
	}

	objForm.MSJVMBuild.value = MSJVMBuild;
	objForm.MSJVMInstalled.value = MSJVMInstalled;
}

function setJavaPluginDetails(objForm) {	
	var javaPluginVersion = getJavaPluginVersion();
	var javaPluginInstalled;

	if (javaPluginVersion != "-") {
		javaPluginInstalled = "Installed";
	}
	else {
		javaPluginInstalled = "Not Installed";
	}

	objForm.JavaPluginVersion.value = javaPluginVersion;
	objForm.JavaPluginInstalled.value = javaPluginInstalled;
}

function setJavaAppletEnabled(objForm) {
	objForm.JavaAppletEnabled.value = getJavaAppletEnabled();
}

function setScreenDetails(objForm) {
	objForm.ScreenWidth.value = screen.width;
	objForm.ScreenHeight.value = screen.height;
	objForm.ColorDepth.value = screen.colorDepth;
}

function setBrowserDetails(objForm) {
	var browserBuild;
	var browserServicePack;

	if (isMSIE) {
		var objCaps = clientCaps;
		browserBuild = getComponentVersion(objCaps, MS_IE_COMPONENT_ID);
		browserServicePack = navigator.appMinorVersion;
	}
	else {
		browserBuild = "";
		browserServicePack = "";
	}

	objForm.BrowserName.value = navigator.appName;
	objForm.BrowserServicePack.value = browserServicePack;
	objForm.BrowserBuild.value = browserBuild;
}

function setAcrobatDetails(objForm) {
	var acrobatVersion = getAcrobatVersion(objForm);
	var acrobatInstalled;

	if (acrobatVersion != "-") {
		acrobatInstalled = "Installed";
	}
	else {
		acrobatInstalled = "Not Installed";
	}

	objForm.AcrobatVersion.value = acrobatVersion;
	objForm.AcrobatInstalled.value = acrobatInstalled;

}

function setFlashDetails(objForm) {
	var flashVersion = getFlashVersion();
	var flashInstalled;

	if (flashVersion != "-") {
		flashInstalled = "Installed";
	}
	else {
		flashInstalled = "Not Installed";
	}

	objForm.FlashVersion.value = flashVersion;
	objForm.FlashInstalled.value = flashInstalled;

}

function setVBScriptDetails(objForm) {	
	var VBScriptSupported;
	var VBScriptVersion;

	if (isVBScriptSupported) {
		VBScriptSupported = "Supported";
		VBScriptVersion = GetVBScriptVersion();
	}
	else {
		VBScriptSupported = "Unsupported";
		VBScriptVersion = "";
	}

	objForm.VBScriptVersion.value = VBScriptVersion;
	objForm.VBScriptSupported.value = VBScriptSupported;

}

function setConnectionType(objForm) {
	var objCaps = clientCaps;
	var connectionType;

	if (objCaps.connectionType != null && objCaps.connectionType != "" && objCaps.connectionType != " ") {
		connectionType = objCaps.connectionType;
	}
	else {
		connectionType = "-";
	}

	objForm.ConnectionType.value = connectionType;
}

////////  GET Environment Values \\\\\\\\\

function getJavaAppletEnabled()
{
	var applet;
	var JavaAppletEnabled;

	try {
		applet = document.DetectJVMApplet.getActiveJVMVendor();	
	}
	catch (ex) {
		applet = null;
	}

	if (applet != null) {
		JavaAppletEnabled = true;
	}
	else {
		JavaAppletEnabled = false;
	}

	return JavaAppletEnabled;
}

function getJVMType()
{
	var isAppletScriptingEnabled = getJavaAppletEnabled();
	var JVMType;

	if (isAppletScriptingEnabled) {
		try {
			JVMType = document.DetectJVMApplet.getActiveJVMVendor();
		}
		catch (ex) {
			JVMType = "Not Installed";
		}
	}
	else {
		JVMType = "Not Installed";
	}

	return JVMType;
}

function getMSJVMBuild()
{
	var isAppletScriptingEnabled = getJavaAppletEnabled();
	var MSJVMBuild;

	if (isAppletScriptingEnabled) {
		try {
			MSJVMBuild = document.DetectJVMApplet.getMSJVMBuild();
		}
		catch (ex) {
			MSJVMBuild = "";
		}
	}
	else {
		MSJVMBuild = "";
	}

	return MSJVMBuild;
}

function getJVMVersion()
{
	var isAppletScriptingEnabled = getJavaAppletEnabled();
	var JVMVersion;

	if (isAppletScriptingEnabled) {
		try {
			JVMVersion = document.DetectJVMApplet.getActiveJVMVersion();
		}
		catch (ex) {
			JVMVersion = "-";
		}
	}
	else {
		JVMVersion = "-";
	}

	return JVMVersion;
}

function getJavaPluginVersion() {	
	var javaPluginVersion;

	if (isMSIE) {
		javaPluginVersion = getJavaPluginVersionIE();
	}
	else {
		javaPluginVersion = getJavaPluginVersionNS()
	}

	return javaPluginVersion;
}

function getJavaPluginVersionIE()
{
	var VERSION_WEBSTART_INSTALLED = "1.4";
	var VERSION_JAVABEANSBRIDGE_INSTALLED = "1.3";

	var javaPluginVersion;

	if (isVBScriptSupported) {   
		if (isActiveXControlInstalled('JavaWebStart.isInstalled')) {
			javaPluginVersion = VERSION_WEBSTART_INSTALLED;
		}
		else {
			if (isActiveXControlInstalled('JavaSoft.JavaBeansBridge.1'))
				javaPluginVersion = VERSION_JAVABEANSBRIDGE_INSTALLED;
			else
				javaPluginVersion = '-';
		}
	}
	else {
		javaPluginVersion = '-';
	}

	return javaPluginVersion;
}

function getJavaPluginVersionNS()
{
	var pluginMimeType = "application/x-java-applet;jpi-version=";
	var objPlugin;
	var javaPluginVersion = "-";

	if(navigator.plugins) { 
		navigator.plugins.refresh(false);
	}

	// check for Java plugin in installed plugins
	if(navigator.mimeTypes) {
		for (var i=0; i < navigator.mimeTypes.length; i++) {

			objPlugin = navigator.mimeTypes[i];
			if( (objPlugin.type != null) && (objPlugin.type.indexOf(pluginMimeType) != -1) ) {
				var versionStartIndex = objPlugin.type.indexOf("=") + 1;
				var versionStringLength = objPlugin.type.length - versionStartIndex;

				javaPluginVersion = objPlugin.type.substr(versionStartIndex, versionStringLength);
				
				break;
			}
		}
	}

	return javaPluginVersion;
}

function getCookieEnabled() {
	var tmpcookie = new Date();
	var CookieEnabled;

	chkcookie = (tmpcookie.getTime() + '');
	document.cookie = "chkcookie=" + chkcookie + "; path=/";

	if (document.cookie.indexOf(chkcookie,0) < 0) {
		CookieEnabled = "false";
	}
	else {
		CookieEnabled = "true";
	}

	return CookieEnabled;
}

function getComponentVersion (objCaps, componentID) {
  var componentVersion = "";

  try {
	  componentVersion = objCaps.getComponentVersion(componentID, "ComponentID");
  }
  catch (e) {}

  return componentVersion;
}

function isComponentInstalled (objCaps, componentID) {
  var componentInstalled = "";

  try {
	  componentInstalled = objCaps.isComponentInstalled(componentID, "ComponentID");
  }
  catch (e) {}

  return componentInstalled;
}

function getAcrobatVersion(objForm) {
	var acrobatPluginVersion;

	if (isMSIE) {
		acrobatPluginVersion = getAcrobatVersionIE(objForm);
	}
	else {
		acrobatPluginVersion = getAcrobatVersionNS();
	}

	return acrobatPluginVersion;
}

function getAcrobatVersionIE(objForm) {
	var acrobatVersion = getAcrobatVersionFromRegistry(objForm);

	if (acrobatVersion == "") {
		acrobatVersion = getAcrobatVersionFromVBScript();

		if (acrobatVersion == "") {
			acrobatVersion = "-";
		}
	}

	return acrobatVersion;
}

function getAcrobatVersionFromRegistry(objForm) {
	var acrobatVersion;
	var acrobatVersions;

	try {
		acrobatVersions = objForm.AcrobatObj.GetVersions();

		if (acrobatVersions == "undefined" || typeof(acrobatVersions) == "undefined") {
			acrobatVersion = "";
		}
		else {
			acrobatVersion = GetHighestAcrobatVer(acrobatVersions);
		}
	}
	catch(e) {
		acrobatVersion = "";
	}

	return acrobatVersion;
}

function getAcrobatVersionFromVBScript() {
	var acrobatVersion;
	var PDF_OBJECT_NAME = "PDF.PdfCtrl.";

	if (isVBScriptSupported) {

		//Check for the new Acrobat 7 version first
		if (isActiveXControlInstalled("AcroPDF.PDF.1")) {
			//"AcroPDF.PDF.1" is a new object created for Adobe Acrobat 7
			acrobatVersion = "7";
		}

		//If version 7 could not be found, check for older versions, i.e. 6-
		if (acrobatVersion == "") {
			for (var i=1; i<=7; i++)
			{
				if (isActiveXControlInstalled(PDF_OBJECT_NAME + i)) {
					acrobatVersion = i;
				}
			}					
		}
	}
	else {
		acrobatVersion = "";
	}
	
	return acrobatVersion;
}

function GetHighestAcrobatVer(acrobatVersions) {
	var acrobatVersion;
	var currentAcrobatVersion;
	
	var numericAcrobatVersion = 0;
	var numericCurrentAcrobatVersion = 0;

	numericValue = 0;
	numericHighest = 0;

	//e.g. acrobatVersions: AccCheck=5.0.0,AcroForm=5.0.0,ADBC=5.0.0, etc.
	var arrAcrobatVersionsMap = acrobatVersions.split(",");

	for (var i = 0; i < arrAcrobatVersionsMap.length; i++) {
		if (arrAcrobatVersionsMap[i] != null && arrAcrobatVersionsMap[i] != "") {
			try {
				//e.g. acrobatVersionsMap[i]: AccCheck=5.0.0
				var arrAcrobatVersionMap = arrAcrobatVersionsMap[i].split("=");
				var arrAcrobatVersion = arrAcrobatVersionMap[1].split(".");

				if (arrAcrobatVersion.length > 1) {
					//currentAcrobatVersion = arrAcrobatVersion[0] + "." + arrAcrobatVersion[1] + "." + arrAcrobatVersion[2];
					currentAcrobatVersion = arrAcrobatVersion[0] + "." + arrAcrobatVersion[1];
					numericCurrentAcrobatVersion = parseInt(arrAcrobatVersion[0] * 1000000 + arrAcrobatVersion[1] * 1000 + arrAcrobatVersion[2]);
				}
			}
			catch(e) {}
		}

		if (numericCurrentAcrobatVersion > numericAcrobatVersion) {
			numericAcrobatVersion = numericCurrentAcrobatVersion;
			acrobatVersion = currentAcrobatVersion;
		}
	}

	return acrobatVersion;
}

function getAcrobatVersionNS()
{
	var pluginName = "adobe acrobat";
	var objPlugin;
	var acrobatPluginVersion = "-";

	if(navigator.plugins) { 
		navigator.plugins.refresh(false);
	}

	// check for Acrobat plugin in installed plugins
	if(navigator.mimeTypes) {
		for (var i=0; i < navigator.plugins.length; i++) {

			objPlugin = navigator.plugins[i];
			if (objPlugin.name.toLowerCase().indexOf(pluginName) >= 0) {
				var versionStartIndex = objPlugin.description.toLowerCase().lastIndexOf("version ") + 8;
				var versionEndIndex = objPlugin.description.toLowerCase().lastIndexOf(" for");

				acrobatPluginVersion = objPlugin.description.substring(versionStartIndex, versionEndIndex);

				break;
			}
		}
	}

	return acrobatPluginVersion;
}

function getFlashVersion() {
	var flashPluginVersion;

	if (isMSIE) {
		flashPluginVersion = getFlashVersionIE();
	}
	else {
		flashPluginVersion = getFlashVersionNS();
	}

	return flashPluginVersion;
}

function getFlashVersionIE() {
	var flashVersion;
	var FLASH_OBJECT_NAME = "ShockwaveFlash.ShockwaveFlash.";

	if (isVBScriptSupported) {
		for (var i=2; i<=6; i++)
		{
			if (isActiveXControlInstalled(FLASH_OBJECT_NAME + i)) {
				flashVersion = i;
			}
		}
	}
	else {
		flashVersion = "-";
	}
	
	return flashVersion;
}

function getFlashVersionNS()
{
	var pluginName = "shockwave flash";
	var objPlugin;
	var flashVersion = "-";

	if(navigator.plugins) { 
		navigator.plugins.refresh(false);
	}

	// check for Shockwave plugin in installed plugins
	if(navigator.mimeTypes) {
		for (var i=0; i < navigator.plugins.length; i++) {

			objPlugin = navigator.plugins[i];
			if (objPlugin.name.toLowerCase().indexOf(pluginName) >= 0) {
				var versionStartIndex = objPlugin.description.toLowerCase().lastIndexOf("flash ") + 6;
				var versionEndIndex = objPlugin.description.length;

				flashVersion = objPlugin.description.substring(versionStartIndex, versionEndIndex);

				break;
			}
		}
	}

	return flashVersion;
}


////////  CREATE Environment Fields \\\\\\\\\

function createFields() {

	document.write('<input type="hidden" name="JavaScriptEnabled" value="Disabled">');
	document.write('<input type="hidden" name="JavaEnabled" value="Disabled">');
	document.write('<input type="hidden" name="JVMType">');
	document.write('<input type="hidden" name="JVMVersion">');
	document.write('<input type="hidden" name="CookieEnabled" value="Disabled">');
	document.write('<input type="hidden" name="MSJVMBuild">');
	document.write('<input type="hidden" name="MSJVMInstalled">');
	document.write('<input type="hidden" name="JavaPluginVersion">');
	document.write('<input type="hidden" name="JavaPluginInstalled">');
	document.write('<input type="hidden" name="JavaAppletEnabled" value="Disabled">');
	document.write('<input type="hidden" name="ScreenWidth">');
	document.write('<input type="hidden" name="ScreenHeight">');
	document.write('<input type="hidden" name="ColorDepth">');
	document.write('<input type="hidden" name="BrowserName">');
	document.write('<input type="hidden" name="BrowserBuild">');
	document.write('<input type="hidden" name="BrowserServicePack">');
	document.write('<input type="hidden" name="AcrobatVersion">');
	document.write('<input type="hidden" name="AcrobatInstalled">');
	document.write('<input type="hidden" name="FlashVersion">');
	document.write('<input type="hidden" name="FlashInstalled">');
	document.write('<input type="hidden" name="VBScriptSupported">');
	document.write('<input type="hidden" name="VBScriptVersion">');
	document.write('<input type="hidden" name="ConnectionType">');
	document.write('<input type="hidden" name="SSLSupport" value="false">');

	createAcrobatObject();
	createVBFunction();
	setSSLSupport();

}

function createAcrobatObject() {
	document.write("<object id='AcrobatObj' style='display: none' classid='clsid:" + ACROBAT_CLASS_ID + "'></object>");
}

function createVBFunction() {
	//Create a VB Script function is VB Script is supported (i.e. platform is windows and browser is IE)
	if (isVBScriptSupported) {
		document.writeln('<script language="VBscript">');

		document.writeln('\'do a one-time test for a version of VBScript that can handle this code');
		document.writeln('detectableWithVB = False');
		document.writeln('If ScriptEngineMajorVersion >= 2 then');
		document.writeln('  detectableWithVB = True');
		document.writeln('End If');

		document.writeln('Function GetVBScriptVersion');
		document.writeln('  Dim VBScriptVersion');
		document.writeln('  VBScriptVersion = ScriptEngineMajorVersion & "." & ScriptEngineMinorVersion & "." & ScriptEngineBuildVersion');
		document.writeln('  GetVBScriptVersion = VBScriptVersion');
		document.writeln('End Function');
		
		document.writeln('Function isActiveXControlInstalled(activeXControlName)');
		document.writeln('  on error resume next');
		document.writeln('  Dim activeXControlInstalled');
		document.writeln('  If detectableWithVB Then');
		document.writeln('     activeXControlInstalled = IsObject(CreateObject(activeXControlName))');
		document.writeln('  Else');
		document.writeln('     activeXControlInstalled = false');
		document.writeln('  End If');
		document.writeln('  isActiveXControlInstalled = activeXControlInstalled');
		document.writeln('End Function');

		document.writeln('</scr' + 'ipt>');
	}
}

function setSSLSupport() {
	window.setTimeout("document.all('SSLImage').src=''", 6000);
	document.write('<img src="https://ecomm.comsec.com.au/ecommcorporate/images/commbank_logo_sml.gif" border=0 width=1 height=1 ID=SSLImage onerror="if (typeof(SSLSupport)==\'undefined\') document.all(\'SSLSupport\').value=false" onload="document.all(\'SSLSupport\').value=true">');
}