var minimumWidth = 1024;
var minimumHeight = 768;

var resolutionPassed = true;
var osPassed = true;
var browserPassed = true;
var javaEnabled = true;
var cookiesEnabled = true;

var width = screen.width;
var height = screen.height;

// platform and browser information
var operatingSystem;
var browserVersion;
var ieVersion;
var language = 'Could not be determined';
var languageDetermined = false;
var languageEnglish = true;
var macintosh = false;

var browserRecommend = '';
var browserText = '';
var foundVersion = false;

function isCompliant()
{
	// set width and height to zero if they are null
	if (width == null)
		width = 0;
	if (height == null)
		height = 0;
	
        // determining browser version
        if (navigator.userAgent.indexOf('Opera') != -1) {
                // determine the start of the version number
                var idx = navigator.userAgent.indexOf('Opera') + 6;
                var tmp = navigator.userAgent.substring(idx, navigator.userAgent.length);

                // determine the end of the version number
                idx = tmp.indexOf(' ');
                if (idx == -1) idx = tmp.length;
                browserVersion = tmp.substring(0, idx);
	} else if (navigator.userAgent.indexOf('Safari') != -1) {
		// determine the start of the version number
		var idx = navigator.userAgent.indexOf('Version') + 8;
		var tmp = navigator.userAgent.substring(idx, navigator.userAgent.length);

		// determine the end of the version number
		idx = tmp.indexOf('.');
		if (idx == -1) {
			idx = tmp.indexOf(' ');
			if (idx == -1) idx = tmp.length;
		}
		browserVersion = tmp.substring(0, idx);
        } else if (navigator.appName.indexOf('Internet Explorer') != -1) {
                // determine the start of the version number
                var idx = navigator.appVersion.indexOf('MSIE') + 5;
                var tmp = navigator.appVersion.substring(idx, navigator.appVersion.length);

                // determine the end of the version number
                idx = tmp.indexOf(';');
                if (idx == -1) idx = navigator.appVersion.length;
                browserVersion = tmp.substring(0, idx);
        } else if (navigator.userAgent.indexOf('Netscape/') != -1) {
                // determine the start of the version number
                var idx = navigator.userAgent.indexOf('Netscape/') + 9;
                browserVersion = navigator.userAgent.substring(idx, navigator.userAgent.length);
	} else if (navigator.userAgent.indexOf('Firefox/') != -1) {
		// determine the start of the version number
		var idx = navigator.userAgent.indexOf('Firefox/') + 8;
		browserVersion = navigator.userAgent.substring(idx, navigator.userAgent.length);
	} else if (navigator.userAgent.indexOf('Gecko') != -1) {
                // determine the start of the version number
                var idx = navigator.userAgent.indexOf('rv:') + 3;
                var tmp = navigator.userAgent.substring(idx, navigator.userAgent.length);

                // determine the end of the version number
                idx = tmp.indexOf(')');
                if (idx == -1) idx = tmp.length;
                browserVersion = tmp.substring(0, idx);
        } else {
                var idx = navigator.appVersion.indexOf(' ');
                if (idx == -1) idx = navigator.appVersion.length;
                browserVersion = navigator.appVersion.substring(0, idx);
        }

	var versionArray = new Array();
	versionArray = browserVersion.split('.');
	if (versionArray.length > 2) {
		browserVersion = versionArray[0] + '.' + versionArray[1];
	}

	// determining operating system
	// operating system is normally after the second ';' in the navigator.appVersion property
	var idx = navigator.appVersion.indexOf(';');
	if (idx != -1) {
		var tmp = navigator.appVersion.substring(idx + 1, navigator.appVersion.length);
		idx = tmp.indexOf(';');
		if (idx != -1) {
			tmp = tmp.substring(idx + 1, tmp.length);
			idx = tmp.indexOf(';');
			if (idx == -1) {
				idx = tmp.indexOf(')');
				if (idx == -1)
					idx = tmp.length;
			}

			//operatingSystem = tmp.substring(0, idx);
			operatingSystem = navigator.appVersion;

			if (operatingSystem.indexOf('NT 5.0') != -1)
				operatingSystem = 'Windows 2000';
			else if (operatingSystem.indexOf('NT 5.1') != -1)
				operatingSystem = 'Windows XP';
			else if (operatingSystem.indexOf('NT 6.0') != -1)
				operatingSystem = 'Windows Vista';
			else if (operatingSystem.indexOf('NT 6.1') != -1)
				operatingSystem = 'Windows 7';
			else if (operatingSystem.indexOf('Mac OS X') != -1)
				operatingSystem = 'Mac OS X';
			else if (operatingSystem.indexOf('Mac OSX') != -1)
				operatingSystem = 'Mac OS X';
			else if (operatingSystem.indexOf('MacOSX') != -1)
				operatingSystem = 'Mac OS X';
			else if (operatingSystem.indexOf('Macintosh') != -1)
				operatingSystem = 'Macintosh';
			else {
				operatingSystem = 'Could not be determined';
			}
		} else {
			// if the first three letters from the platform are in the first parameter
			// of the appVersion, then that is where the OS is
			idx = navigator.appVersion.indexOf(';');
			tmp = navigator.appVersion.substring(0, idx);
			idx = tmp.indexOf('(');
			if (idx == -1)
				operatingSystem = navigator.platform;
			else {
				tmp = tmp.substring(idx + 1, tmp.length);
				if (tmp.indexOf(navigator.platform.substring(0, 3)) != -1) {
					if (tmp == 'Win95') operatingSystem = 'Windows 95';
					else if (tmp == 'Win98') operatingSystem = 'Windows 98';
					else if (tmp == 'WinNT') operatingSystem = 'Windows NT';
					else operatingSystem = tmp;
				} else
					operatingSystem = navigator.platform;
			}

			// what if the OS name is just plain 'Windows' - we can do better than that
			// let's have a look at user-agent
			// OS is normally the third field
			if (operatingSystem == 'Windows') {
				idx = navigator.userAgent.indexOf(';');
				if (idx != -1) {
					tmp = navigator.userAgent.substring(idx + 1, navigator.userAgent.length);
					idx = tmp.indexOf(';');
					if (idx != -1) {
						tmp = tmp.substring(idx + 1, tmp.length);
						var end = tmp.indexOf(';');
						if (end != -1) {
							operatingSystem = trim(tmp.substring(0, end));

							// check it out
							if (operatingSystem.indexOf('Windows') == -1)
								operatingSystem = 'Windows';
							else {
								if (operatingSystem.indexOf('Windows NT') != -1) {
									if (operatingSystem == 'Windows NT 5.0')
										operatingSystem = 'Windows 2000'
									else if (operatingSystem == 'Windows NT 5.1')
										operatingSystem = 'Windows XP';
									else if (operatingSystem == 'Windows NT 6.0')
										operatingSystem = 'Windows Vista';
									else if (operatingSystem == 'Windows NT 6.1')
										operatingSystem = 'Windows 7';
									else
										operatingSystem = 'Windows NT';
								}
							}
						}
					}
				}
			}
		}
	} else
		operatingSystem = navigator.platform;

	if (navigator.userAgent.indexOf('Opera') != -1) {
		if (navigator.userAgent.indexOf('[en]') != -1) {
			languageDetermined = true;
			language = 'English';
			languageEnglish = true;
		}
	} else if (navigator.appName.indexOf('Internet Explorer') != -1) {
		if (navigator.systemLanguage != null) {
			languageDetermined = true;
			if (navigator.systemLanguage.substring(0, 2) == 'en') {
				language = 'English';
				languageEnglish = true;
			} else {
				language = 'Non-English';
				languageEnglish = false;
			}
		}
	} else if (navigator.appName.indexOf('Netscape') != -1) {
		languageDetermined = true;
		if (navigator.language.substring(0, 2) == 'en') {
			language = 'English';
			languageEnglish = true;
		} else {
			language = 'Non-English';
			languageEnglish = false;
		}
	} else {
		// language is not determinable
	}

	if (navigator.platform.toLowerCase().substring(0, 3) == 'mac')
		macintosh = true;


	// check resolutionPassed ----------------------------------------------------------
	if ((width < minimumWidth) || (height < minimumHeight))
		resolutionPassed = false;
	else
		resolutionPassed = true;

	// check osPassed ------------------------------------------------------------------
	if (operatingSystem.toLowerCase().indexOf('windows') != -1) {
		// windows
		if (operatingSystem.indexOf('95') != -1) {
			foundVersion = false;
		} else {
			if (operatingSystem.indexOf('98') != -1) foundVersion = true;
			if (operatingSystem.indexOf('2000') != -1) foundVersion = true;
			if (operatingSystem.indexOf('NT') != -1) foundVersion = true;
			if (operatingSystem.indexOf('XP') != -1) foundVersion = true;
			if  (operatingSystem.indexOf('Vista') != -1) foundVersion = true;
			if (operatingSystem.indexOf('Win32') != -1) foundVersion = true;
			if (operatingSystem.indexOf('Windows') != -1) foundVersion = true;
		}

		if (foundVersion)
			osPassed = true;
		else
			osPassed = false;
	} else if (operatingSystem.toLowerCase().indexOf('sun') != -1) {
		// sun/solaris
		if (operatingSystem.indexOf('5.') != -1)
			osPassed = true;
		else
			osPassed = false;
	} else {
		// not supported OS
		osPassed = false;
	}
	if (macintosh)
		osPassed = false;

	// check browserPassed -------------------------------------------------------------
	if (navigator.userAgent.indexOf('Opera') != -1) {
		browserRecommend = 'Opera 9.0';
		if (browserVersion < 9.0)
			browserPassed = false;
		else
			browserPassed = true;
		browserText = 'Opera ' + browserVersion;
	} else if (navigator.userAgent.indexOf('Safari') != -1) {
		browserRecommend = 'Safari 3';
		if (browserVersion < 3)
			browserPassed = false;
		else
			browserPassed = true;
		browserText = 'Safari ' + browserVersion;
	} else if (navigator.appName.indexOf('Internet Explorer') != -1) {
		if (macintosh) {
			ieVersion = 5.0;
			browserRecommend = 'Internet Explorer 5.0';
		} else {
			ieVersion = 6.0;
			browserRecommend = 'Internet Explorer 6.0';
		}
		if (browserVersion < ieVersion)
			browserPassed = false;
		else
			browserPassed = true;
		browserText = 'Internet Explorer ' + browserVersion;
	} else if (navigator.userAgent.indexOf('Firefox') != -1) { 
		browserRecommend = 'Firefox 2.0';
		if (browserVersion < 2.0) {
			browserPassed = false;
		}
		else {
			browserPassed = true;
		}
		browserText = 'Firefox ' + browserVersion;
	} else if ((navigator.userAgent.indexOf('Gecko') != -1) && (navigator.userAgent.indexOf('Netscape') == -1)) {
		browserRecommend = 'Mozilla 1.3';
		if (browserVersion < 1.3) {
			browserPassed = false;
		}
		else {
			browserPassed = true;
		}
		browserText = 'Mozilla ' + browserVersion;
	} else if (navigator.appName.indexOf('Netscape') != -1) {
		browserRecommend = 'Netscape Navigator 7.0';
		if (browserVersion < 7.0)
			browserPassed = false;
		else
			browserPassed = true;
		browserText = 'Netscape Navigator ' + browserVersion;
	} else {
		browserPassed = false;
		browserRecommend = 'Internet Explorer 6.0 or Netscape Navigator 7.0';
		browserText = navigator.appName + ' ' + navigator.appVersion;
	}

	// check javaEnabled ---------------------------------------------------------------
	if (!navigator.javaEnabled())
		javaEnabled = false;
	else
		javaEnabled = true;

	// check cookiesEnabled ------------------------------------------------------------
	if (!doCookieTest())
		cookiesEnabled = false;
	else
		cookiesEnabled = true;

	return (resolutionPassed && osPassed && browserPassed && javaEnabled && cookiesEnabled);
}



function displayInfo()
{
	//
	// begin display
	//
	document.write('<font face="Arial,Helvetica" size="2">');

	document.write('<table border="0">');
	document.write('<tr><td>');
	document.write('<td><h3><b>QTAC Online Services Test Page</b>');
	document.write('</table>');
	document.write('<br>');

	//
	// now time to display results
	//

	document.write('<table border="2" cellpadding="2" cellspacing="0" bordercolor="#C0C0C0">');
	document.write('<tr bgcolor="#FFE1E1">');
	document.write('<th>Attribute</th>');
	document.write('<th>Recommended</th>');
	document.write('<th>Your configuration</th>');
	document.write('<th>Passed</th>');

	//
	// screen resolution
	//

	writeHeading('Screen resolution', '1024 by 768 pixels (minimum)');
	writePassed(resolutionPassed, false, width + ' by ' + height + ' pixels', 'resolution');

	//
	// operating system
	//

	writeHeading('Operating System',
		'Microsoft Windows 98/NT/2000/XP/Vista/7<br>Mac OS X');

	if (macintosh) {
		writePassed(false, true, operatingSystem, 'macos');
	} else
		writePassed(osPassed, false, operatingSystem, 'os');

	//
	// browser
	//

	writeHeading('Browser', browserRecommend);
	writePassed(browserPassed, false, browserText, 'browser');

	//
	// browser language
	//

	writeHeading('Browser Language', 'English');
	writePassed(languageEnglish, false, language, 'language');

	//
	// Java enabled
	//

	writeHeading('Java', 'Enabled');
	writePassed(javaEnabled, false, (javaEnabled ? 'Enabled': 'Disabled'), 'java');

	//
	// cookies enabled
	//

	writeHeading('Cookies', 'Enabled');
	writePassed(cookiesEnabled, false, (cookiesEnabled ? 'Enabled' : 'Disabled'), 'cookies');

	document.write('</table>');

	//
	// display general information about requirements
	//

	document.write('<p>Mandatory / recommended system and browser requirements which cannot be tested:');
	document.write('<ul>');
	document.write('<li>Pentium or equivalent processor computer (recommended)');
	document.write('<li>English language operating system (recommended)');
	document.write('<li>SSL (mandatory)');
	document.write('</ul></p>');


	//
	// display a summary of the results
	//

	if (macintosh) {
		document.write('<p>You are using a computer running a Macintosh operating system. ');
		document.write('At the current point in time, QTAC\'s online services are only certified ');
		document.write('to work on Mac OS X. This is due to the Java VM (Virtual Machine) which is ');
		document.write('used on versions of the Macintosh OS prior to Mac OS X.  It does not support ');
		document.write('all of the operations which QTAC\'s online services require.</p>');

		document.write('<p>If you are using a version of the Macintosh OS other than Mac OS X, and ');
		document.write('you choose to continue past this point, and any part of QTAC\'s online services ');
		document.write('should not work, please use a different machine (preferably one running ');
		document.write('Microsoft Windows (not 95) or Mac OS X using Internet Explorer). ');
	}

	if (resolutionPassed && osPassed && browserPassed && languageEnglish && javaEnabled && cookiesEnabled) {
		// document.write('<p>You have passed all of the tests, continue...');

		// cannot test SSL yet
		//sslTest();
	} else {
		if (!resolutionPassed) {
			document.write('<p><b>Resolution</b>');
			document.write('<p>Online services require a screen no smaller than 1024 by 768 pixels.</p>');
		}

		if (!osPassed) {
			document.write('<p><b>Operating System</b>');
			document.write('<p>Recommended to use Windows (not 95) or Mac OS X.</p>');
		}

		if (!browserPassed) {
			document.write('<p><b>Browser</b>');
			document.write('<p>Recommended to use:');
			document.write('<ul>');
			document.write('<li>Microsoft Internet Explorer (Version 6.0 or higher);');
			document.write('<li>Firefox (Version 2.0 or higher); or');
			document.write('<li>Netscape Navigator (Version 7.0 or higher);');
			document.write('<li>Safari (Version 3.0 or higher).');
			document.write('<li>Opera (Version 9.0 or higher).');
			document.write('</ul></p>');
		}

		if (!languageEnglish) {
			if (languageDetermined) {
				document.write('<p><b>Browser Language</b>');
				document.write('<p>Recommended to use an English language browser.</p>');
			} else {
				document.write('<p>Could not determine the language of your browser.</p>');
			}
		}

		if (!javaEnabled) {
			document.write('<p><b>Java</b>');
			document.write('<p>Java is required to use QTAC\'s online services.</p>');
		}

		if (!cookiesEnabled) {
			document.write('<p><b>Cookies</b>');
			document.write('<p>Cookies are required to use QTAC\'s online services.</p>');
		}

		if (!resolutionPassed || !javaEnabled || !cookiesEnabled) {
			document.write('<p>You have failed one or more tests that will prevent you ');
			document.write('from using QTAC\'s online services');

			document.write('<p>It is not recommended that you continue to use QTAC\'s ');
			document.write('online services until you ');
			document.write('have addressed the above issue(s).</p>');
		} else {
			document.write('<p>You have not passed one or more tests which are recommended only ');
			document.write('which means that QTAC\'s online services may work, although they have ');
			document.write('not been specifically tested with your configuration.');

			document.write('<p>If QTAC\'s online services should not work with your configuration, ');
			document.write('please try to firstly upgrade your configuration to that which ');
			document.write('is recommended.');

			// cannot test SSL yet
			//sslTest();
		}
	}

	document.write('<p><b>Notes on compliance to use QTAC\'s Online Services:</b></p>');
	document.write('<p>In order to use QTAC\'s online services, there are a series of ');
	document.write('system and browser requirements which are necessary. These ');
	document.write('requirements can be recommended or mandatory. If your system ');
	document.write('should fail a mandatory requirement, you will be given help on ');
	document.write('how to resolve the problem, but you will not be able to use ');
	document.write('QTAC\'s online services. However, if you should fail a recommended ');
	document.write('requirement, you will be given help on how to resolve the problem, ');
	document.write('but you will be given the chance to proceed and try to use the ');
	document.write('online services.</p>');

	document.write('<p>Some things cannot be tested by this page (as listed above). ');
	document.write('It is your responsibility to ensure that your ');
	document.write('computer meets these requirements.</p>');
}
