/*************************************************************************
Filename: 	detectMachine.js
Creator: 	Peter Ennis
Created: 	04/03/03
Edited By: 	James Speake
Edited: 	26/01/04 - Added view detection to URLs
Purpose: 	Will print to screen if any required Players are not present
			on the users browser.
Variables:	Requires String vars, describing the Players to detect....
			FLASH - detectFlash
			SHOCK - detectShockwave
			OS - detectOS
			JAVA - detect Java
			PDF - detectPDF
			AUTHOR - detectAuthorware
			REAL - detectReal
			BROWSER - detectBrowser
			* - detectAllPlayers
**************************************************************************/

var systemMaybe = false; // for when we cannot detect stuff
var theViewDS = ""; // View for all URLs.
if (getValue('view') != "") theViewDS = "?view=" + getValue('view');

var systemOK = true;
var systemOKText = "";
var osErrorText = "";
var browserErrorText = "";
var flashErrorText = "";
var flashInstallText = "";
var shockErrorText = "";
var shockInstallText = "";
var javaErrorText = "";
var pdfErrorText = "";
var authorwareErrorText = "";
var authorwareErrorTextIE = "";
var webexErrorText = "";
var realErrorText = "";
var javaImportantInfo = "";

function setTechCode(theTechURL) {
	systemOKText = "The computer you are using has all the required software it needs to run this course. <a href=\"" + theTechURL + "\">Further details about the technical requirements for this course</a>.";
	osErrorText = "<br /><strong>This course isn't supported by your operating system. <a href=\"" + theTechURL + "#browsers\">Further details</a></strong>";
	browserErrorText = "<br /><strong>This course isn't supported by your browser. <a href=\"" + theTechURL + "#browsers\">Further details</a></strong>";
	flashErrorText = "<br /><strong>Your Flash player will need upgrading before you can run this course. <a href=\"" + theTechURL + "#players\">Further details</a></strong>";
	flashInstallText = "<br /><strong>You will need to install Flash before you can run this course. <a href=\"" + theTechURL + "#players\">Further details</a></strong>";
	shockErrorText = "<br /><strong>Your Shockwave player will need upgrading before you can run this course. <a href=\"" + theTechURL + "#players\">Further details</a></strong>";
	shockInstallText = "<br /><strong>You will need to install Shockwave before you can run this course. <a href=\"" + theTechURL + "#players\">Further details</a></strong>";
	javaErrorText = "<br /><strong>Java needs enabling in your browser before you can run this course <a href=\"" + theTechURL + "#players\">Further details</a></strong>";
	pdfErrorText = "<br /><strong>You will need to install an Acrobat plugin before you can run this course. <a href=\"" + theTechURL + "#players\">Further details</a></strong>";
	authorwareErrorText = "<br /><strong>You will need to install an Authorware player before running this course. <a href=\"" + theTechURL + "#players\">Further details</a></strong>";
	authorwareErrorTextIE = "<br /><strong>You may need to install an Authorware player before running this course (We cannot automatically detect this). <a href=\"" + theTechURL +"#players\">Further details</a></strong>";
	webexErrorText = "<br /><strong>You may need to install a Webex player before running this course (We cannot automatically detect this). <a href=\"" + theTechURL +"#players\">Further details</a></strong>";
	realErrorText = "<br /><strong>You will need to install Real player before running this course. <a href=\"" + theTechURL + "#players\">Further details</a></strong>";
	javaImportantInfo = "<br /><strong>You have Java installed, but before buying this course you should <a href=\"" + theTechURL + "#java\">read our important information first.</a></strong>";
}

function detect_os(os) {
	var thisOS;
	if((navigator.userAgent.indexOf('Win') != -1) && (navigator.userAgent.indexOf('98') != -1))
	{
		//found Windows98
		thisOS = "W98";
	}
	else if(navigator.userAgent.indexOf('Win') != -1 && (navigator.userAgent.indexOf('NT 4') != -1))
	{
		//found Windows NT4 - Not Supported anymore!!
		// thisOS = "WNT";
	}
	else if(navigator.userAgent.indexOf('Win') != -1 && (navigator.userAgent.indexOf('NT 5.1') != -1))
	{
		//found Windows XP
		thisOS = "WXP";
	}
	else if(navigator.userAgent.indexOf('Win') != -1 && (navigator.userAgent.indexOf('NT 6.0') != -1))
	{
		//found Windows VISTA
		thisOS = "WVISTA";
	}
	else if(navigator.userAgent.indexOf('Mac') != -1)
	{
		//found Mac
		thisOS = "OSX";
	}

	// See if this OS matches any in the system
	var pattern = new RegExp(thisOS, "i");
	var result = os.match(pattern);
	var errorText = "";

	if (result == null) {
		systemOK = false;
		errorText = osErrorText;
	}

	os = os.replace("WVISTA", "Windows Vista", "i");
	os = os.replace("WXP", "Windows XP", "i");
	os = os.replace("W2K", "Windows 2000", "i");
	os = os.replace("OS9", "Mac OS 9", "i");
	os = os.replace("OSX", "Mac OS 10.1", "i");
	os = os.replace("W98", "Windows 98", "i");
	// Remove any reference to Win95, WinNT, WinME
	os = os.replace("WME,", "", "i");
	os = os.replace("WNT,", "", "i");
	os = os.replace("W95,", "", "i");

	return os + errorText;
}

// Detect which browsers are being used
function detect_browser(browsers) {
	var detect = navigator.userAgent.toLowerCase();
	var thisBrowser,version,thestring;

	if (checkIt('konqueror')) thisBrowser = "Konqueror"
	else if (checkIt('safari')) thisBrowser = "Safari"
	else if (checkIt('omniweb')) thisBrowser = "OmniWeb"
	else if (checkIt('opera')) thisBrowser = "Opera"
	else if (checkIt('webtv')) thisBrowser = "WebTV"
	else if (checkIt('icab')) thisBrowser = "iCab"
	else if (checkIt('msie')) thisBrowser = "IE"
	else if (checkIt('firefox')) thisBrowser = "Firefox"
	else if (!checkIt('compatible'))
	{
		thisBrowser = "Netscape Navigator"
		version = detect.charAt(8);
	}
	else browser = "An unknown browser";

	if (!version) thisBrowser = thisBrowser + detect.charAt(place + thestring.length);


	function checkIt(string)
	{
		place = detect.indexOf(string) + 1;
		thestring = string;
		return place;
	}


	// See if this browser is in the list of supported ones
	var pattern = new RegExp(thisBrowser, "i");
	var result = browsers.match(pattern);
	var errorText = "";

	if (result == null && browsers.indexOf("N/A") == -1) {
		systemOK = false;
		errorText = browserErrorText;
	}

	// Format list of browsers
	browsers = browsers.replace("IE5.5", "Explorer 5.5", "i");
	browsers = browsers.replace("IE6.0", "Explorer 6.0 (PC)", "i");
	browsers = browsers.replace("IE6", "Explorer 6.0 (PC)", "i");
	browsers = browsers.replace("IE7.0", "Explorer 7.0 (PC)", "i");
	browsers = browsers.replace("IE7", "Explorer 7.0 (PC)", "i");
	browsers = browsers.replace("IE8.0", "Explorer 8.0 (PC)", "i");
	browsers = browsers.replace("IE8", "Explorer 8.0 (PC)", "i");
	
	browsers = browsers.replace("Firefox3", "Firefox 3.x (PC)", "i");

	// Remove references to Netscape - Need to change detection routine too, maybe not, seems to be broken already
	browsers = browsers.replace("N4.76,", "", "i");
	browsers = browsers.replace("N6.2,", "", "i");
	browsers = browsers.replace("N6.1,", "", "i");
	browsers = browsers.replace("N6,", "", "i");
	browsers = browsers.replace("NS6,", "", "i");

	browsers = browsers.replace("N/A,", "Not applicable to this course", "i");

	return browsers + errorText;

}


//Detect plugins
function detect_plugins(plugins) {

	var errorText = " ";
	splitString = plugins.split(", ");
		for (i=0; i<splitString.length; i++)
		{
			switch (splitString[i])
			{
			   	case "Flash" :
				  	errorText = errorText + multiDetect("Shockwave Flash", flashErrorText);
				  	break;
				case "Shockwave" :
				  	errorText = errorText + multiDetect("Shockwave Director", shockErrorText);
				  	break;
				case "Java" :
				  	errorText = errorText + detectJava();
				  	break;
				case "PDF" :
					errorText = errorText + multiDetect("Acrobat Reader", pdfErrorText);
				  	break;
			   	case "Authorware" :
				  	errorText = errorText + detectAuthor();
				  	break;
			   	case "Real" :
				 	errorText = errorText + multiDetect("RealPlayer", realErrorText);
				  	break;
				case "Webex" :
				 	errorText = errorText + detectWebex();
				  	break;
			}
		}

	// Format list of browsers
	//plugins = plugins.replace("Flash", "Flash 6", "i");
	//plugins = plugins.replace("Authorware", "Authorware 7", "i");
	//plugins = plugins.replace("PDF", "Acrobat 4.5", "i");

	if (plugins == "") {
		return "none";
	}
	else {
		return plugins + errorText;
	}
}


/* --------- Individual plugin detection routines ----------- */

// Multiway detection routine
function multiDetect(playerName, errorTextString)
{
	var agt = navigator.userAgent.toLowerCase();
	var ie  = (agt.indexOf("msie") != -1);
	var ns  = (navigator.appName.indexOf("Netscape") != -1);
	var win = ((agt.indexOf("win")!=-1) || (agt.indexOf("32bit")!=-1));
	var mac = (agt.indexOf("mac")!=-1);

	// Detect for IE & Windows
	if (ie && win) {
		pluginlist = detectIE("Adobe.SVGCtl","SVG Viewer") + detectIE("SWCtl.SWCtl.8.5","Shockwave Director") + detectIE("ShockwaveFlash.ShockwaveFlash.5","Shockwave Flash") + detectIE("rmocx.RealPlayer G2 Control.1","RealPlayer") + detectIE("QuickTimeCheckObject.QuickTimeCheck.1","QuickTime") + detectIE("MediaPlayer.MediaPlayer.1","Windows Media Player") + detectIE("PDF.PdfCtrl.5","Acrobat Reader") + detectIE("Authorware.Authorware.1","Authorware");
	}

	// Detect for Netscape and non windows machines
	if (ns || !win) {
			nse = ""; for (var i=0;i<navigator.mimeTypes.length;i++) nse += navigator.mimeTypes[i].type.toLowerCase();
			pluginlist = detectNS("image/svg-xml","SVG Viewer") + detectNS("application/x-director","Shockwave Director") + detectNS("application/x-shockwave-flash","Shockwave Flash") + detectNS("audio/x-pn-realaudio-plugin","RealPlayer") + detectNS("video/quicktime","QuickTime") + detectNS("application/x-mplayer2","Windows Media Player") + detectNS("application/pdf","Acrobat Reader") + detectNS("application/x-authorware-map","Authorware");
	}

	// IE Detection routine
	function detectIE(ClassID,name) {
		result = false;
		//document.write('<SCRIPT LANGUAGE=VBScript>\n on error resume next \n result = IsObject(CreateObject("' + ClassID + '"))</SCRIPT>\n');
		//if (result) {
		//	return name+',';
		//}
		//else {
			return '';
		//}
	}

	// Netscape Detection Routine
	function detectNS(ClassID,name) {
		n = "";
		if (nse.indexOf(ClassID) != -1) {
			if (navigator.mimeTypes[ClassID].enabledPlugin != null) {
				n = name+",";
			}
		}
		return n;
	}

	pluginlist += navigator.javaEnabled() ? "Java," : "";
	if (pluginlist.length > 0) pluginlist = pluginlist.substring(0,pluginlist.length-1);

	// Test against player name passed to function & return error text if not found
	if (pluginlist.indexOf(playerName)!=-1){
		return "";
	}
	else {
		systemOK = false;
		return errorTextString;
	}
}


// Detect Authorware
// Currently can't find any way of detecting in IE/Windows without trying to install the plugin

function detectAuthor() {
	var agt = navigator.userAgent.toLowerCase();
	var ie  = (agt.indexOf("msie") != -1);
	var win = ((agt.indexOf("win")!=-1) || (agt.indexOf("32bit")!=-1));
	if (navigator.mimeTypes && navigator.mimeTypes["application/x-authorware-map"]) {
		return "";
	}
	else if (ie && win) {
		systemMaybe = true;
		return authorwareErrorTextIE;
	}
	else {
		systemOK = false;
		return authorwareErrorText;
	}
}

// Detect the webex player - Can't currently do this though
function detectWebex() {
	systemMaybe = true;
	return webexErrorText;
}


//Detect Java
function detectJava() {

	if (navigator.javaEnabled()) {
		systemMaybe = true;
		return javaImportantInfo;
	}
	else {
		systemOK = false;
		return javaErrorText;
	}
}


// Get values from the Querystring (used for getting the view)
function getValue(value) {
    var pairs = location.search.substring(1).split("&");
	var eachPair;
	var returnedValue = "";
	for (var i=0; i<pairs.length; i++) {
		 eachPair = pairs[i].split(value + "=");
		 if (eachPair.length > 1) returnedValue = eachPair[1];
	}
	return returnedValue;
}

// Display the technical information
function displayTechInfo(browser, OS, thePlugins, modem, courseCode, divID) {
	var theTechURL = "/help/techspecs/" + theViewDS;
	//theViewDS != "" ? theTechURL += "&" : theTechURL += "?";
	//theTechURL += "techcode=" + courseCode;
	setTechCode(theTechURL);
	var outputHtml = '';
	var args = displayTechInfo.arguments;
	var showAll = false;
	if (args.length == 5) showAll = args[4];
	if (!modem) outputHtml += "<p>This course is more suitable for users on a broadband connection. Users on modems may experience slight delays.</p>";

	// Put systemMaybe stuff in here
	var failText = "<p class=\"clearfix\">";
	if (!showAll) failText += "<img src=\"/resources/images/general/technical_alert.gif\" width=\"40\" height=\"45\" alt=\"alert symbol\" />";
	failText += "This course has been tested to work using the following technical specification:</p>";
	failText += "<ul";
	failText += "><li><a href=\"" + theTechURL + "#browsers\">Browsers</a>: " + detect_browser(browser) + "</li";
	failText += "><li><a href=\"" + theTechURL + "#os\">Operating Systems</a>: " + detect_os(OS) + "</li";
	failText += "><li><a href=\"" + theTechURL + "#players\">Plugins</a>: " + detect_plugins(thePlugins) + "</li";
	failText += "></ul>";

	if (systemOK && !showAll && !systemMaybe) {
		outputHtml += "<p><img src=\"/resources/images/general/technical_tick.gif\" width=\"40\" height=\"45\" alt=\"tick symbol\" />" + systemOKText + "</p>";
	} else {
		outputHtml += failText;
	}
	jQuery('#' + divID).html(outputHtml);
}