

// *********************************************************************************
// Technical check class, used by all defined technical checks.
//
// TechCheck.name
// TechCheck.passed (true or false)
// TechCheck.message (error or pass message)
// TechCheck.desc (description of check being performed)

function TechCheck(checkName, defaultState, defaultMsg, checkFunction)
{
    this.name = checkName;
    this.passed = defaultState;
    this.message = defaultMsg;

    if (checkFunction != null)
    {
        this.PerformCheck = checkFunction;
    }
}    

TechCheck.prototype.PerformCheck = StandardCheck;

// this is an empty function used by the TechCheck object if its checkFunction is null
function StandardCheck()
{

}
// *********************************************************************************


// returns the array ID of the technical check based on the name passed in
function findCheckID(checkName)
{
   for (var i = 0; i< checks.length; i++)
   {
       if (checks[i].name == checkName)
       {
            return i;
       } 
   }
   
   return null;
}

// this function is called if the port images can't be loaded,
// setting their equivalent check.passed property to false because
// the port is inaccessible.
function portImageNotLoaded(imageObj) {

    var checkID;

    checkID = findCheckID(imageObj.id);
    
    if (imageObj.id == 'port7777Check')
    {
        checks[checkID].message = 'Port 7777 is closed on your firewall.  Some learning content requires this port to be open.'
        checks[checkID].passed = false;
    }
    else if (imageObj.id == 'port7778Check')
    {
        checks[checkID].message = 'Port 7778 is closed on your firewall.  Some learning content requires this port to be open.'
        checks[checkID].passed = false;
    }
    else if (imageObj.id == 'port7780Check')
    {
        checks[checkID].message = 'Port 7780 is closed on your firewall.  Some learning content requires this port to be open.'
        checks[checkID].passed = false;
    }
    else if (imageObj.id == 'clixDomainCheck') {
        checks[checkID].message = 'The Clix Learning Management System cannot be accessed.  The URL <a href="https://e-learningforhealthcare.org.uk" title="visit Clix" target="_blank">https://e-learningforhealthcare.org.uk</a> needs to be accessible otherwise you will not be able to view any learning content.'
        checks[checkID].passed = false;
    }
}

function checkFlash()
{
    // flash version numbers
    var requiredMajorVersion = 8;
    var requiredMinorVersion = 0;
    var requiredRevision = 0;

    var hasReqestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);

    if (hasReqestedVersion) 
    {
        this.message = 'Adobe Flash player version 8 or above is installed.';
        this.passed = true;

    } 
    else 
    {  
        // flash is too old or we can't detect the plugin
        this.message = 'Some of the learning content requires Adobe Flash Player 8 or later. <a href=http://www.adobe.com/go/getflash/>Please download the latest version</a> from Adobe.';
        this.passed = false;
    }
}

function checkJava() {
   
   var passedImgElement = "<img src='images/tick.png' alt='passed technical check'>";
   var failedImgElement = "<img src='images/cross.png' alt='passed technical check'>";
            
   
   var javaCheckID = findCheckID('javaCheck');
   var pluginCheckID = findCheckID('javaPluginCheck');
   
   if (javaCheckID == null || pluginCheckID == null)
   {
        alert('Error: Unable to run Java and Java next gen plugin check.');
        return false;
   }
   
   // handle the error if the call to the applet method 
   // fails because the applet wasn't loaded by the browser.
   try
   {
   
       // call the applet method to determine the JRE version
       var javaVersion = document.javaDetailsApplet.getJavaVersion();
   
       // check that the version is at least version 6 (1.6)
       // e.g. 1.6.0_17   (version number_update number)
       // NOTE - for iLearning a java version of at least 1.6 is required,
       //        updates 10 to 13 need the next-gen plugin to be disabled,
       //        later updates can have the plugin enabled or disabled, it doesn't matter.
       
       // split the string into it's parts and analyse the major and minor version numbers
       var versionNums = javaVersion.split(".");
       
       var majorVer = parseFloat(versionNums[0] + '.' + versionNums[1]);
       var updateVer = parseInt(versionNums[2].slice(versionNums[2].indexOf('_') +1));

       // check the version is at least 1.6
       if (majorVer >= 1.6)
       {
            checks[javaCheckID].passed = true;
            checks[javaCheckID].message = "Java is enabled (version " + javaVersion + ").";
        
            // if the java version is 1.6, check the status of the next-gen plugin
            if (majorVer == 1.6)
            {        
                // the Java next-gen plugin should be disabled for Java versions 1.6.0_10 to _13.
                // The plugin can be enabled if any other java version 6 or above is used.
                // nextGenStatus will contain a false or true value.
                var nextGenStatus = deployJava.isPlugin2();
                
                // to contain the string "enabled" or "disabled"
                var nextGenStatusStr = 'disabled';
                 
                if (nextGenStatus)
                {
                    nextGenStatusStr = 'enabled';
                } 
                
                //alert(updateVersion);
                //updateVer = 11;
                 
                if (updateVer >= 10)
                {
                    
                    // check that the next-gen plugin is enabled
                    if (nextGenStatus) {
                    
                        checks[pluginCheckID].message = 'The Java next-generation plugin is enabled.';
                        checks[pluginCheckID].passed = true;
                    }
                    else
                    {
                        checks[pluginCheckID].message = 'The Java next-generation plugin is disabled. Please <a href="http://java.com/en/download/help/new_plugin.xml" title="Switching the next generation plugin on and off">visit this web page</a> to learn how to enable the plugin.';
                        checks[pluginCheckID].passed = false;
                    }
                }
                else
                {
                    checks[pluginCheckID].passed = true;
                    checks[pluginCheckID].message = 'The next-generation plugin is not used by your version of Java.';
                }
            }
       }
       else
       {
            checks[javaCheckID].passed = false;
            checks[javaCheckID].message = "A minimum Java version of 1.6.0 is required.  Please install the <a href='http://www.java.com/en/download/index.jsp' title='the latest JRE'>latest Java Runtime Environment (JRE)</a>.";
            
       }
       
       /*
       if (javaVersion.indexOf("1.6.0") != -1 )
       {
        checks[checkID].passed = true;
        checks[checkID].message = "Java is enabled (version " + javaVersion + ").";
        
        imgElement = "<img src='images/tick.png' alt='passed technical check'>";
        
        // test for the next gen plugin
        //deployJava.isPlugin2() == true)
        
       }
       */
   }
   catch(err)
   {
    // do nothing
    // the checks.passed attribute is set to false by default.

   }
   
   // write out the result of the java check
   if (checks[javaCheckID].passed == true)
   {
    $('#fullDetail').append("<p>" + passedImgElement + "&nbsp;" + checks[javaCheckID].message + "</p>");   
   }
   else
   {
    $('#technicalChecks').html(standardFailureMsg);
    $('#fullDetail').append("<p>" + failedImgElement + "&nbsp;" + checks[javaCheckID].message + "</p>");
   }
   
   // write out the result of the java plugin check
   if (checks[pluginCheckID].passed == true)
   {
    $('#fullDetail').append("<p>" + passedImgElement + "&nbsp;" + checks[pluginCheckID].message + "</p>");   
   }
   else
   {
    $('#technicalChecks').html(standardFailureMsg);
    $('#fullDetail').append("<p>" + failedImgElement + "&nbsp;" + checks[pluginCheckID].message + "</p>");
   }
   
}

function checkBrowser() {
    
    if (BrowserDetect.browser == "Explorer" && BrowserDetect.version >= 6)
    {
        this.message = 'Microsoft Internet Explorer 6 or above is installed.';
        this.passed = true;
    }
    else
    {
        this.message = 'Your web browser is ' + BrowserDetect.browser + ' version ' + BrowserDetect.version + '. ' +
                        'We cannot guarantee that the learning content will work correctly in your browser and we recommend that you download and install <a href="http://www.microsoft.com/windows/internet-explorer/default.aspx" title="Download Internet Explorer" target="_blank">the latest version of Internet Explorer from Microsoft.</a>';
        this.passed = false;
    }

}

function checkResolution()
{
    if (screen.availHeight >= 738 && screen.availWidth >= 1024) 
    {
        this.message = 'A screen resolution of 1024 x 768 or above has been detected.';
        this.passed = true;
    }
    else {
        this.message = 'Your screen resolution (' + screen.width + ' x '+ screen.height + ') is less than the recommended 1024 x 768. You may find it difficult to view the learning content. <a href="http://www.microsoft.com/enable/training/windowsxp/screenresolution.aspx" target="_blank">Instructions are available on how to change your screen resolution.</a></p>';
        this.passed = false;
    }
}

// Both popUpCheckForEIntegrity and popUpCheck functions are identical except the the message properties are different.  eIntegrity's function
// provides one pdf document to the user while the standard popUpCheck function for Clix provides a different one.
function popUpCheckForEIntegrity(url) {
    this.passed = false;
    this.message = 'Pop-up windows are not enabled for the LMS.  The LMS uses pop-up windows to deliver e-Learning content.  Please allow pop-ups from the following websites: https://e-learningforhealthcare.org.uk and http://www.e-lfh.org.uk.<p><a href="http://www.e-lfh.org.uk/docs/support/How%20to%20enable%20Pop-ups%20in%20Internet%20Explorer%20and%20Mozilla%20Firefox%20June%202010.pdf" title="How to enable pop-ups" target="_blank">How to enable pop-ups in Internet Explorer and Mozilla Firefox.</a></p>';

    try {
        // open a pop-up window
        var popUpWin = window.open('', 'popUpTest', 'width=1, height=1, top=0, left=0');


        // see if the window has opened
        if (popUpWin != null) {
            this.message = 'Pop-up windows are enabled.';
            this.passed = true;

            // if so, close it.
            popUpWin.close();
        }

    }
    catch (err) {
        // do nothing, this checks.passed attribute is set to false by default
    }
}

function popUpCheck(url)
{
    this.passed = false;
    this.message = 'Pop-up windows are not enabled for the LMS.  The LMS uses pop-up windows to deliver e-Learning content.  Please allow pop-ups from the following websites: https://e-learningforhealthcare.org.uk and http://www.e-lfh.org.uk.<p><a href="http://www.e-lfh.org.uk/docs/support/How%20to%20enable%20Pop-ups%20in%20Internet%20Explorer%20and%20Mozilla%20Firefox.pdf" title="How to enable pop-ups" target="_blank">How to enable pop-ups in Internet Explorer and Mozilla Firefox.</a></p>';

    try
    {
        // open a pop-up window
       var popUpWin = window.open('', 'popUpTest', 'width=1, height=1, top=0, left=0');
        

        // see if the window has opened
        if (popUpWin != null)
        {
            this.message = 'Pop-up windows are enabled.';
            this.passed = true;

            // if so, close it.
            popUpWin.close();
        }

    }
    catch (err)
    {
      // do nothing, this checks.passed attribute is set to false by default
    }
}

function checkCookies()
{
    var tmpcookie = new Date();

    chkcookie = (tmpcookie.getTime() + '');
    		
    document.cookie = "chkcookie=" + chkcookie + "; path=/";
    		
    if (document.cookie.indexOf("chkcookie=") != -1)
    {
        this.message = 'Web browser cookies are enabled.';
        this.passed = true;	    	    
    }
    else
    {
        this.message = 'Web browser cookies are not enabled but are required to access some learning content.';
        this.passed = false;
    }
}

function checkQuickTime()
{
    var hasQuickTime = false;

    if (BrowserDetect.browser == "Explorer")
    {
        hasQuickTime =  detectActiveXPlugin("Quicktime.Quicktime");
    }
    else
    {
        hasQuickTime = hasPluginForMimeType("video/quicktime");
    }

    if (hasQuickTime)
    {
        this.message = 'Quicktime is enabled.';
        this.passed = true;
    }
    else
    {
        this.message = 'Some of the learning content requires QuickTime, which you do not have enabled. <a href="http://www.apple.com/quicktime/download/">Please download the latest version</a> from Apple.';
        this.passed = false;
    }
}

function checkAdobeReader()
{

    var hasAdobeReader = false;

    if (BrowserDetect.browser == "Explorer")
    {
        hasAdobeReader =  detectActiveXPlugin("AcroPDF.PDF");
    }
    else
    {
        hasAdobeReader = hasPluginForMimeType("application/pdf");
    }

    if (hasAdobeReader)
    {
        this.message = 'Adobe Reader is enabled.';
        this.passed = true;
    }
    else
    {
        this.message = 'Some of the learning content may include links to PDF files. You do not appear to have Adobe Reader enabled and will not be able to view such files. <a href="http://www.adobe.com/products/reader/" target="_blank">Please download the latest version</a> from Adobe.';
        this.passed = false;
    }
}



function hasPluginForMimeType(sMimeType)
{
	if (navigator.mimeTypes[sMimeType] != undefined)
	{
		return navigator.mimeTypes[sMimeType].enabledPlugin != null;
	}
	else
	{
		return false;	
	}
}

function detectActiveXPlugin(sPlugin)
{

	try
	{
		new ActiveXObject(sPlugin);
		return true;
	}
	catch (oError)
	{
		return false;
	}
}

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();
