//This Javascript code checks the browser
//
//For those browsers that do are not supported
//
//A message will be displayed to the user informing the
//user of which browsers are supported
//
//*************************************************************
//
//Date         Version          Author          Description
//01/10/2003   1.00             KAZ             Original
//01/01/2004   1.01             KAZ             New Nestcape version check
//24/04/2004   1.02		KAZ		Made changes to check for Safari on Mac and version 1.4 or above of Java and Netscape 7.1                                             
//
//21/07/2004   1.03		NV		Altered the function getBrowserServicePack() because if an unsupported browser
//						with no SP is used, it could still possibly appear as being a valid browser, which is
//						clearly not the case.
//
//02/09/2004   1.04		NV		Altered the list of valid browsers on Win XP to include IE6 SP2. This needed to be added
//						because if the user has installed Win XP SP2 on their machine, their version of IE is upgraded
//						to IE6 SP2.	
//
//14/03/2005   1.05		KAZ		Added FireFox Browser
//						Rewording of unsupported browser Message
//
//23/05/2005   1.06		AC		Altered the list of valid browsers on Win NT to include Netscape 7.2
//
//

var isPluginChecked = false;
var isPluginInstalled;
var m_Application = "ATO digital certificate renewal process";

var operatingSystem;
var m_os98id 	= 0;
var m_osNTid 	= 1;
var m_os2Kid 	= 2;
var m_osMEid 	= 3;
var m_osXPid	= 4;
var m_osMACid	= 5;
var m_osInvalidId = 6;

var m_os98 	= "98";
var m_osNTIE = "Windows NT";
var m_osNTNS = "WinNT";
var m_os2K 	= "Windows NT 5.0";
var m_osME 	= "Win 9x";
var m_osXP	= "Windows NT 5.1";
var m_osMAC	= "Mac";
var m_osInvalid = "Invalid";

var m_IE = "Microsoft Internet Explorer";
var m_NS = "Netscape";
var m_SAF = "Safari";
var m_FireFox = "Firefox"; 

var m_SP0 = "SP0"; // If IE version does not have a service pack associated with it
var m_SP1 = "SP1";
var m_SP2 = "SP2";

var m_IE501 = "5.01";
var m_IE55 = "5.5";
var m_IE60 = "6.0";
var m_IE522 = "5.22";

var m_NS71 = "7.1";
var m_NS72 = "7.2";
var m_FF1 = "1.0";  


var m_validBrowsers = new Array(6);

function intialiseVariables()
{
	//
	//Initialise the array with valid browser combinations for operating systems
	//
	m_validBrowsers[m_os98id] =  	m_IE + "/ " + m_IE501 + "/" + m_SP2 + ";"
    					+ 	m_IE + "/ " + m_IE55 + "/" + m_SP2  + ";"
    					+ 	m_IE + "/ " + m_IE60 + "/" + m_SP1  + ";"
    					+ 	m_NS + "/"  + m_NS71 + "/" + ";"
					+ 	m_NS + "/" + m_NS72 + "/" + ";"
 					+	m_FireFox + "/" + m_FF1 + "/" + ";"

	m_validBrowsers[m_osNTid] =  	m_IE + "/ " + m_IE55 + "/" + m_SP2  + ";"
	    			+ 	 	m_IE + "/ " + m_IE60 + "/" + m_SP1  + ";"
    				+ 	 	m_NS + "/" + m_NS71 + "/" + ";"
				+		m_NS + "/" + m_NS72 + "/" + ";"

	m_validBrowsers[m_os2Kid] =  	m_IE + "/ " + m_IE501 + "/" + m_SP2  + ";"
    				+ 	 	m_IE + "/ " + m_IE55 + "/" + m_SP2   + ";"
    				+ 	 	m_IE + "/ " + m_IE60 + "/" + m_SP1   + ";"
	    			+ 	 	m_NS + "/" + m_NS71 + "/" + ";"
				+	 	m_NS + "/" + m_NS72 + "/" + ";"
 				+		m_FireFox + "/" + m_FF1 + "/" + ";"

	m_validBrowsers[m_osMEid] =  	m_IE + "/ " + m_IE55 + "/" + m_SP2   + ";"
    				+ 	 	m_IE + "/ " + m_IE60 + "/" + m_SP1   + ";"
    				+ 	 	m_IE + "/ " + m_IE60 + "/" + m_SP2   + ";"
	    			+ 	 	m_NS + "/" + m_NS71 + "/" + ";"
				+	 	m_NS + "/" + m_NS72 + "/" + ";"
 				+		m_FireFox + "/" + m_FF1 + "/" + ";"

	m_validBrowsers[m_osXPid] =  	m_IE + "/ " + m_IE60 + "/" + m_SP1   + ";"
						+	m_IE + "/ " + m_IE60 + "/" + m_SP2   + ";"
    					+ 	m_NS + "/" + m_NS71 + "/" + ";"
					+ 	m_NS + "/" + m_NS72 + "/" + ";"
 					+	m_FireFox + "/" + m_FF1 + "/" + ";"

}

function getOperatingSystem()
{
	//ME must be checked before 98
	if (navigator.userAgent.indexOf(m_osME)>-1)
		return m_osMEid;

	if (navigator.userAgent.indexOf(m_os98)>-1)
		return m_os98id;

	if (navigator.userAgent.indexOf(m_osXP)>-1)
		return m_osXPid;

	if (navigator.userAgent.indexOf(m_os2K)>-1)
		return m_os2Kid;

	if (navigator.userAgent.indexOf(m_osNTIE)>-1)
		return m_osNTid;

	if (navigator.userAgent.indexOf(m_osNTNS)>-1)
		return m_osNTid;

	if (navigator.userAgent.indexOf(m_osMAC)>-1)
		return m_osMACid;

	return m_osInvalidId;
}

function getBrowserName()
{
		
	if (navigator.appName.indexOf(m_IE)>-1)
		return m_IE;
	
	if (navigator.userAgent.indexOf(m_NS)>-1)
		return m_NS;
		
	if (navigator.userAgent.indexOf(m_SAF)>-1)
	{
		return m_SAF;			
	}
	if (navigator.userAgent.indexOf(m_FireFox)>-1)
	{
		return m_FireFox;			
	}			
	
}

function getBrowserServicePack(browserName)
{
	if (browserName==m_IE)
	{
		if (navigator.appMinorVersion.indexOf(m_SP1)>-1)
			return m_SP1;
	
		if (navigator.appMinorVersion.indexOf(m_SP2)>-1)
			return m_SP2;
		
		return m_SP0; 
	}

	return "";

}

function getBrowserVersion()
{
	var versionNumber = 0;

	if ((navigator.appName == "Microsoft Internet Explorer") || (navigator.userAgent.indexOf(m_NS)>-1) || (navigator.userAgent.indexOf(m_FireFox)>-1)) 
	{
		versionNumber = getBrowserVersionNumber();
	}
	else
	{
		versionNumber = 0;
	}	
	return versionNumber;
}


function getBrowserVersionNumber()
{
	var theChar;
	var versionNumber = "";
	var charIndex = 0;
	var browserVersion = "";

	browserVersion = navigator.appVersion;
	if (navigator.appName == "Microsoft Internet Explorer")
	{
		browserVersion = extractMicrosoftIEVersion();

		for(charIndex = 0; charIndex < browserVersion.length; charIndex++)
		{
			theChar = browserVersion.charAt(charIndex);
	            if (isNaN(theChar) && (theChar != '.'))
      	      {
				break;
	            }
		}

		if (charIndex >= 0)
		{
			versionNumber = browserVersion.substring(0, charIndex);
		}
		else
		{
			versionNumber = "";
		}
	}

	if (navigator.appName == "Netscape")
	{
		versionNumber = extractNetscapeVersion();
	}

	if (navigator.userAgent.indexOf(m_FireFox)>-1)
	{
		versionNumber = extractFireFoxVersion();
	}		
	return versionNumber;
}
function extractFireFoxVersion()
{
	var FirefoxVersionPrepend = m_FireFox
	var versionStartIndex = 0;
	var trimedBrowserVersion = "";
	var browserVersion = "";
	browserVersion = navigator.userAgent;

	versionStartIndex = browserVersion.indexOf(FirefoxVersionPrepend) + 1;

	if (versionStartIndex != -1)
	{
		trimedBrowserVersion = browserVersion.substring(versionStartIndex
			+ FirefoxVersionPrepend.length);
	}
	else
	{
		trimedBrowserVersion = browserVersion;
	}
	return trimedBrowserVersion;

}

function extractMicrosoftIEVersion()
{
	var microsoftVersionPrepend = "MSIE";
	var versionStartIndex = 0;
	var trimedBrowserVersion = "";
	var browserVersion = "";

	browserVersion = navigator.userAgent;

	versionStartIndex = browserVersion.indexOf(microsoftVersionPrepend);
	if (versionStartIndex != -1)
	{
		trimedBrowserVersion = browserVersion.substring(versionStartIndex
			+ microsoftVersionPrepend.length);
	}
	else
	{
		trimedBrowserVersion = browserVersion;
	}

	return trimedBrowserVersion;
}

function extractNetscapeVersion()
{
	var NetscapeVersionPrepend = m_NS;
	var versionStartIndex = 0;
	var trimedBrowserVersion = "";
	var browserVersion = "";

	browserVersion = navigator.userAgent;

	versionStartIndex = browserVersion.indexOf(NetscapeVersionPrepend);

	if (versionStartIndex != -1)
	{
		trimedBrowserVersion = browserVersion.substring(versionStartIndex + 1
			+ NetscapeVersionPrepend.length);
	}
	else
	{
		trimedBrowserVersion = browserVersion;
	}

	//remove leading space
	if (trimedBrowserVersion.indexOf(" ")==0)
	{
		trimedBrowserVersion=trimedBrowserVersion.substring(1,trimedBrowserVersion.length)
	}

	//remove trailing space
	if (trimedBrowserVersion.indexOf(" ")>0)
	{
		trimedBrowserVersion=trimedBrowserVersion.substring(0,trimedBrowserVersion.indexOf(" "))
	}
	return trimedBrowserVersion;
}

function browserRestrictionMsg(processName)
{
	alert("You are currently using a browser that may not work with the " + processName + ". We cannot guarantee performance of all functions. \n\nPlease check the Minimum system requirements page for compatible browsers.\n\nThese browsers can be downloaded free of charge from the Netscape(www.netscape.com) or Microsoft(www.microsoft.com) web sites.");
}

/**
* 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 isCorrectPluginTypeSSL(pluginType)
{
	if ((pluginType.indexOf("application/x-java-applet") == 0)
			&& (pluginType.indexOf("1.4.") > -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 isJavaInstalledForNetscapeSSL()
{
	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 (isCorrectPluginTypeSSL(thePlugin[mimeTypeIndex].type))
			{
				isJavaInstalled = true;
				break;
			}
			if (isJavaInstalled)
			{
				break;
			}
		}
	}
	return isJavaInstalled;
}
/**
* Checks if the java plugin has SSL (ie java 1.4) is installed. 
*/
function checkForJavaPluginSSL()
{
	isPluginChecked = true;
	if (navigator.appName == "Microsoft Internet Explorer")
	{
		isPluginInstalled = isJavaInstalledForIESSL("1.4.0");
	}
	else
	{
		isPluginInstalled = isJavaInstalledForNetscapeSSL();
	}
	if (isPluginInstalled)
	{
		return "true";
	}
	else
	{
		return "false";
	}
}

function isValidOSBrowser()
{
	var osid;
	var browserName;
	var browserVersion;
	var servicePack;

	intialiseVariables();
	osid = getOperatingSystem();	
	browserName = getBrowserName();
	browserVersion = getBrowserVersion();
	servicePack = getBrowserServicePack(browserName);
	
	if (osid == m_osInvalidId)
	{	
		browserRestrictionMsg(m_Application);
	}
	else
	{	
				
		if ( (osid == m_osMACid) && (browserName == m_SAF) )
		{
			alert("We are unable to determine if you are using the correct version of Safari. Please check to ensure your browser meets the supported browser on the minimum system requirements page.");
		} 
		else
		{
			if (m_validBrowsers[osid].indexOf(browserName + "/" + browserVersion + "/" + servicePack)==-1) 
			{		
				browserRestrictionMsg(m_Application);
			}
		}
	}
}
