var passedColour = "#008000";
var failedColour = "#800000";
var detectableWithVB = false;

var agt = navigator.userAgent.toLowerCase();
var is_major = parseInt(navigator.appVersion);
var is_minor = parseFloat(navigator.appVersion);

var is_firefox = (agt.indexOf('firefox') != -1);
var is_firefox2 = (is_firefox && (agt.indexOf("firefox/2.") != -1 ));
var is_firefox3 = (is_firefox && (agt.indexOf("firefox/3.") != -1 ))

var is_safari = (agt.indexOf('safari') != -1);
var is_safari3_0 = (is_safari && (agt.indexOf("version/3.0") != -1 ));
var is_safari31up = ( is_safari && !is_safari3_0 && ( agt.indexOf("version/3") != -1 ) );

var is_ie = ( ( agt.indexOf("msie") != -1 ) && ( agt.indexOf("opera") == -1 ) );
var is_ie6 = ( is_ie && ( is_major == 4 ) && ( agt.indexOf("msie 6.") != -1 ) );
var is_ie7 = ( is_ie && ( is_major == 4 ) && ( agt.indexOf("msie 7.") != -1 ) );
var is_ie8 = ( is_ie && ( is_major == 4 ) && ( agt.indexOf("msie 8.") != -1 ) );

var is_win = ( (agt.indexOf("win")!=-1) || (agt.indexOf("16bit")!=-1) );
var is_win95 = ((agt.indexOf("win95")!=-1) || (agt.indexOf("windows 95")!=-1));
var is_win16 = ((agt.indexOf("win16")!=-1) ||
         (agt.indexOf("16bit")!=-1) || (agt.indexOf("windows 3.1")!=-1) ||
         (agt.indexOf("windows 16-bit")!=-1) );
var is_win31 = ((agt.indexOf("windows 3.1")!=-1) || (agt.indexOf("win16")!=-1) ||
              (agt.indexOf("windows 16-bit")!=-1));
var is_winme = ((agt.indexOf("win 9x 4.90")!=-1));
var is_win2k = ((agt.indexOf("windows nt 5.0")!=-1));
var is_win98 = ((agt.indexOf("win98")!=-1) || (agt.indexOf("windows 98")!=-1));
var is_winnt = ((agt.indexOf("winnt")!=-1) || (agt.indexOf("windows nt")!=-1));
var is_win32 = (is_win95 || is_winnt || is_win98 ||
              ((is_major >= 4) && (navigator.platform == "Win32")) ||
              (agt.indexOf("win32")!=-1) || (agt.indexOf("32bit")!=-1));
var is_win2k = is_winnt && (agt.indexOf("5.0") != -1);
var is_winxp = is_winnt && (agt.indexOf("5.1") != -1);
var is_os2 = ((agt.indexOf("os/2")!=-1) ||
              (navigator.appVersion.indexOf("OS/2")!=-1) ||
              (agt.indexOf("ibm-webexplorer")!=-1));

var is_mac = (agt.indexOf("mac")!=-1);
var is_mac68k = (is_mac && ((agt.indexOf("68k")!=-1) ||
                         (agt.indexOf("68000")!=-1)));
var is_macppc = (is_mac && ((agt.indexOf("ppc")!=-1) ||
                          (agt.indexOf("powerpc")!=-1)));
                          

// These terms are overridden
var JTHelp = "Help";

var BTDescription = "A current browser such as the latest version of Microsoft Internet Explorer is recommended to ensure full functionality of all components.";
var BTRecommendationMac = "";
var BTRecommendationWindows = "";
var BTRecommendationLinux = "";

var BTResultPass = "Your browser is sufficient.";
var BTResultFail_1 = "Your browser is not a sufficient browser, please <a href='upgrade_browser.asp?ou=";
var BTResultFail_2 = "'>upgrade</a> your browser.";
var BTName = "Browser";

var BTFullyCompatible = "FC";
var BTCurrentlyTesting = "CT";
var BTOldBrowser = "OB";
var BTFailed = "Failed";

var BTHelp = "Help";

function browsertest() {

	var pass = false;
	result_text = BTFailed;
	
	// IE
	// < 6: Not Supported
	// 6: Mainteneance
	// 7: Supported
	// 8: Investigation
	if( is_ie ) {
		if( is_ie6 || is_ie7 || is_ie8 ) {
		
			pass = true;
			
			if( is_ie7 ) {
				result_text = BTFullyCompatible;
			} else if( is_ie6 ) {
				result_text = BTOldBrowser;
			} else {
				result_text = BTCurrentlyTesting;
			}
			
		}			
	}
	
	// Firefox
	// < 2: Not Supported
	// 2: Maintenance
	// 3: Supported
	if( is_firefox ) {
		if( is_firefox2 || is_firefox3 ) {
			if( is_firefox3 ) {
				result_text = BTFullyCompatible;
			} else {
				result_text = BTOldBrowser;
			}
			pass = true;
		}
	}

	// Safari
	// < 3.1: Not Supported
	// 3: Supported
	if( is_safari ) {
					result_text = BTFullyCompatible;
			pass = true;
		//if( is_safari31up ) {
		//	result_text = BTFullyCompatible;
		//	pass = true;
		//}
	}

	drawCheck( BTName, '', pass, result_text, '' );
	
}

var CTDescription = "The use of cookies is to allow for an increased level of security and are not being used to track your movements.";
var CTResultPass = "Your cookies are enabled.";
var CTResultFail_1 = "You do not have cookies enabled.&nbsp";
var CTResultFail_2 = "Click here for instructions";
var CTResultFail = "Fail";
var CTName = "Cookies";

var CTHelp = "Help";

function cookietest() {
	var description = '';
	var pass;
	var result_text = "";
	document.cookie = "test=cookietest";
	var theCookie=""+document.cookie;
	var ind=theCookie.indexOf("test=");
	if(ind==-1) {
		pass = false;
		result_text = CTResultFail;

		result_text +="</a>";
	}
	else {
		pass = true;
		result_text = CTResultPass;
	}
	drawCheck(CTName, description, pass, result_text, CTHelp);
}

var FTDescription = "Flash is used to play back movie content.";
var FTResultPass = "Flash is enabled. Click Play on the video below. If the video does not play then you need to contact your IT department.";
var FTResultFail = "Flash is not installed. <a href='http://get.adobe.com/flashplayer/' target='_blank'>Click Here</a> to download the latest version of Flash.";
var FTName = "Flash";

function DetectFlash() {

    var result_text = "";
    var pass;

    if (FlashDetect.versionAtLeast(9, 0, 124)) {
        //FLASH SUPPORTED
        result_text = FTResultPass;
        pass = true;
    }
    else {
        //NO FLASH SUPPORT
        result_text = FTResultFail;
        pass = false;
    }
    drawCheck(FTName, '', pass, result_text, '');
}

var DTDescription = "To view our pages properly, this test ensures your computer is running the proper screen resolution and color depth.";
var DTResultPass = "You are running a sufficient screen resolution. You are also running a sufficient colour level.";
var DTResultFail_1 = "You are running a 640 X 480 screen resolution. We recommend that you run at least an 800 X 600 screen resolution.";
var DTResultFail_2 = " You are also only running at a 2-bit colour level. We recommend that you use at least a 16-bit colour level.";
var DTResultFail_3 = " You are also only running a 4-bit colour level. We recommend that you use at least a 16-bit colour level.";
var DTResultFail_4 = " You are also only running at an 8-bit colour level.  We recommend that you use at least a 16-bit colour level.";
var DTResultFail_5 = " However, you are running a sufficient colour level.";
var DTResultFail_6 = "Follow these instruction to change your display settings.";
var DTResultFail_7 = "You are running a sufficient screen resolution.";
var DTResultFail_8 = " However you are also only running a 2-bit colour level. We recommend that you use at least a 16-bit colour level.";
var DTResultFail_9 = " However, you are only running a 4-bit colour level. We recommend that you use at least a 16-bit colour level.";
var DTResultFail_10 = " However, you are only running a 4-bit colour level. We recommend that you use at least a 16-bit colour level.";
var DTResultFail_11 = "Follow these instruction to change your display settings.";


var DTName = "Display";
var DTResPass = "";
var DTPixDepPass = "";
var DTResFailed = "";
var DTPixDepFailed = "";
var DTMoreInfo = "";

function displaytest() {
	var passRes = true;
	var passPD = true;
	var result_text = "";
	if ( screen.height == 480 ) {
		passRes = false;

	}

	if ( screen.pixelDepth<16 || screen.colorDepth<16  ) {
		passPD = false;
	}
	
	
	document.write("<tr><td>&nbsp;</td><td colspan=2><b>" + DTName + "</b></td></tr>");
	
	if ( passRes || passPD ) {
		document.write("<tr><td>&nbsp;</td><td valign=top><img src='images/icons/system_check_checkmark.gif' width=25 height=25 alt='" + DTPass + "' title='" + DTPass + "'>");
		document.write("</td><td>");
		document.write("<font color='" + passedColour + "'><b>" + DTPassed + "</b></font><br>");
		
		if ( passRes ) {
			document.write("<br>" + DTResPass );
		}
		if ( passPD ) {
			document.write("<br>" + DTPixDepPass );		
		}
		
		document.write("</td></tr>");	

	}

	
	if(passRes == false || passPD == false ) {
	    document.write("<tr><td>&nbsp;</td><td valign=top><img src='images/icons/system_check_error.gif' width=16 height=16 alt='" + DTFailed + "' title='" + DTFailed + "'>");
		document.write("</td><td>");
		document.write("<font color='" + failedColour + "'><b>" + DTFailed + "</b></font><br>");
		
		if ( passRes == false ) {
			document.write("<br>" + DTResFailed + "<br>" );
		}
		if ( passPD == false ) {
			document.write("<br>" + DTPixDepFailed + "<br>" );		
		}
		
		document.write("<br>" + DTMoreInfo + "<br>" );				
		
		document.write("</td></tr>");	
	}
		
}

function detectPlugin() {
	var daPlugins = detectPlugin.arguments;
	var pluginFound = false;
	if (navigator.plugins && navigator.plugins.length > 0) {
		var pluginsArrayLength = navigator.plugins.length;
		for (pluginsArrayCounter=0; pluginsArrayCounter < pluginsArrayLength; pluginsArrayCounter++ ) {
			var numFound = 0;
			for(namesCounter=0; namesCounter < daPlugins.length; namesCounter++) {
				if( (navigator.plugins[pluginsArrayCounter].name.indexOf(daPlugins[namesCounter]) >= 0) ||
	    			(navigator.plugins[pluginsArrayCounter].description.indexOf(daPlugins[namesCounter]) >= 0) ) {
	    			numFound++;
				}
			}
			if(numFound == daPlugins.length) {
				pluginFound = true;
				break;
			}
		}
	}
	return pluginFound;
}

var DTPass = "Pass";
var DTPassed = "Passed";
var DTFailed = "Failed";

function drawCheck(name, description, pass, result_text, help_text) {
	document.write("<tr><td>&nbsp;</td><td colspan=2><b>" + name + "</b></td></tr><tr><td>&nbsp;</td><td valign=top>");
	if(pass) {
		document.write("<img src='images/icons/system_check_checkmark.gif' width=25 height=25 alt='" + DTPass + "' title='" + DTPass + "'>");
	}
	else {
	    document.write("<img src='images/icons/system_check_error.gif' width=16 height=16 alt='" + DTFailed + "' title='" + DTFailed + "'>");
	}
	document.write("</td><td>");
	if(pass) {
		document.write("<font color='" + passedColour + "'><b>" + DTPassed + "</b></font>");
	}
	else {
		document.write("<font color='" + failedColour + "'><b>" + DTFailed + "</b></font>");
	}
	document.write("<br>" + description + "<br>" + result_text );
		
	document.write("</td></tr>");
}

// Here we write out the VBScript block for MSIE Windows
if ((navigator.userAgent.indexOf('MSIE') != -1) && (navigator.userAgent.indexOf('Win') != -1)) {
    document.writeln('<script language="VBscript">');

    document.writeln('detectableWithVB = False');
    document.writeln('If ScriptEngineMajorVersion >= 2 then');
    document.writeln('  detectableWithVB = True');
    document.writeln('End If');

    document.writeln('</scr' + 'ipt>');
}


JTHelp = 'VideoClassroom.org uses JavaScript to display some content, such as the number of quiz attempts or submitted files for a user, and not to create interface elements, such as advertisements.';

BTDescription = "A current browser such as the latest version of Microsoft Internet Explorer is recommended to ensure full functionality of all components.";
BTRecommendationMac ='For your web browser, it is recommended that you use Netscape 7.1 in order to ensure full functionality of all components.';
BTRecommendationWindows ='For your web browser, it is recommended that you use Microsoft Internet Explorer 7 in order to ensure full functionality of all components.';
BTRecommendationLinux ='For your web browser, it is recommended that you use Mozilla 1.6.';


BTResultPass = "Your web browser supports Javascript.";
BTResultFail_1 = "Your browser is not a sufficient browser, please <a href='upgrade_browser.asp?ou=";
BTResultFail_2 = "'>upgrade</a> your browser.";
BTName = "Browser";
BTHelp = 'VideoClassroom.org uses JavaScript to display some content, such as the number of quiz attempts or submitted files for a user, and not to create interface elements, such as advertisements.';

CTDescription = "The use of cookies is to allow for an increased level of security and are not being used to track your movements.";


CTResultPass = "Cookies are enabled.";
CTResultFail = 'Please adjust the security settings for your web browser. VideoClassroom.org requires that you enable the use of cookies.<br><br>For information on enabling cookies, search for \"cookies\" in your browser\'s Help contents.';
CTResultFail_1 = "You do not have cookies enabled.&nbsp";
CTResultFail_2 = "Click here for instructions";
CTName = "Cookies";
CTHelp = 'VideoClassroom.org uses cookies to store your login information, preferences, and session data, and not to track your movements on the internet.';

DTDescription = "In order to view our pages properly, your computer must be using a sufficiently sized screen resolution and must meet the required level of color depth.";
DTResultPass = "You are using a sufficiently sized screen resolution. You are also using a sufficient level of color depth.";
DTResultFail_1 = "You are running a 640 X 480 screen resolution. We recommend that you run at least an 800 X 600 screen resolution.";
DTResultFail_2 = " You are also only running at a 2-bit color level. We recommend that you use at least a 16-bit color level.";
DTResultFail_3 = " You are also only running a 4-bit color level. We recommend that you use at least a 16-bit color level.";
DTResultFail_4 = " You are also only running at an 8-bit color level. We recommend that you use at least a 16-bit color level.";
DTResultFail_5 = " However, you are running a sufficient color level.";
DTResultFail_6 = "Follow these instruction to change your display settings.";
DTResultFail_7 = "You are running a sufficient screen resolution.";
DTResultFail_8 = " However, you are also only running a 2-bit color level. We recommend that you use at least a 16-bit color level.";
DTResultFail_9 = " However, you are only running a 4-bit color level. We recommend that you use at least a 16-bit color level.";
DTResultFail_10 = " However, you are only running an 8-bit color level. We recommend that you use at least a 16-bit color level.";
DTResultFail_11 = "Follow these instruction to change your display settings.";
DTName = "Display";

DTPass = "Pass";
DTPassed = "Passed";
DTFailed = "Failed";

DTResPass = "Your computer meets the minimum screen resolution requirements.";
DTPixDepPass = "Your computer meets the minimum color depth requirements.";
DTResFailed = "VideoClassroom.org requires a minimum screen resolution of 800x600 pixels. We recommend that you set your resolution to 1024x768 or higher.";
DTPixDepFailed = "VideoClassroom.org requires a minimum color depth of 16-bit to run.";
DTMoreInfo = 'For more information on changing your display settings, search for \"screen resolution\" or \"color\" in your operating systems Help.';

BTFullyCompatible = 'Your web browser is fully compatible.';
BTCurrentlyTesting = 'You are using a web browser that we are currently investigating. Your browser should be compatible, but if you encounter problems please switch to a recent version of <a href=\"http://www.microsoft.com/windows/products/winfamily/ie/default.mspx\">Internet Explorer</a>, <a href=\"http://www.mozilla.com/en-US/firefox/personal.html\">Firefox</a>, or <a href=\"http://www.apple.com/safari/\">Safari</a>.';
BTOldBrowser = 'You are using an older web browser. Although it should be compatible, we recommend that you switch to a newer version of <a href=\"http://www.microsoft.com/windows/products/winfamily/ie/default.mspx\">Internet Explorer</a>, <a href=\"http://www.mozilla.com/en-US/firefox/personal.html\">Firefox</a>, or <a href=\"http://www.apple.com/safari/\">Safari</a>.';
BTFailed = 'Your browser is not supported. We recommend that you switch to a newer version of <a href=\"http://www.microsoft.com/windows/products/winfamily/ie/default.mspx\">Internet Explorer</a>, <a href=\"http://www.mozilla.com/en-US/firefox/personal.html\">Firefox</a>, or <a href=\"http://www.apple.com/safari/\">Safari</a>.';


var FlashDetect = new function() {
    var self = this;
    self.installed = false;
    self.raw = "";
    self.major = -1;
    self.minor = -1;
    self.revision = -1;
    self.revisionStr = "";
    var activeXDetectRules = [
        {
            "name": "ShockwaveFlash.ShockwaveFlash.7",
            "version": function(obj) {
                return getActiveXVersion(obj);
            }
        },
        {
            "name": "ShockwaveFlash.ShockwaveFlash.6",
            "version": function(obj) {
                var version = "6,0,21";
                try {
                    obj.AllowScriptAccess = "always";
                    version = getActiveXVersion(obj);
                } catch (err) { }
                return version;
            }
        },
        {
            "name": "ShockwaveFlash.ShockwaveFlash",
            "version": function(obj) {
                return getActiveXVersion(obj);
            }
        }
    ];
    /**
    * Extract the ActiveX version of the plugin.
    * 
    * @param {Object} The flash ActiveX object.
    * @type String
    */
    var getActiveXVersion = function(activeXObj) {
        var version = -1;
        try {
            version = activeXObj.GetVariable("$version");
        } catch (err) { }
        return version;
    };
    /**
    * Try and retrieve an ActiveX object having a specified name.
    * 
    * @param {String} name The ActiveX object name lookup.
    * @return One of ActiveX object or a simple object having an attribute of activeXError with a value of true.
    * @type Object
    */
    var getActiveXObject = function(name) {
        var obj = -1;
        try {
            obj = new ActiveXObject(name);
        } catch (err) {
            obj = { activeXError: true };
        }
        return obj;
    };
    /**
    * Parse an ActiveX $version string into an object.
    * 
    * @param {String} str The ActiveX Object GetVariable($version) return value. 
    * @return An object having raw, major, minor, revision and revisionStr attributes.
    * @type Object
    */
    var parseActiveXVersion = function(str) {
        var versionArray = str.split(","); //replace with regex
        return {
            "raw": str,
            "major": parseInt(versionArray[0].split(" ")[1], 10),
            "minor": parseInt(versionArray[1], 10),
            "revision": parseInt(versionArray[2], 10),
            "revisionStr": versionArray[2]
        };
    };
    /**
    * Parse a standard enabledPlugin.description into an object.
    * 
    * @param {String} str The enabledPlugin.description value.
    * @return An object having raw, major, minor, revision and revisionStr attributes.
    * @type Object
    */
    var parseStandardVersion = function(str) {
        var descParts = str.split(/ +/);
        var majorMinor = descParts[2].split(/\./);
        var revisionStr = descParts[3];
        return {
            "raw": str,
            "major": parseInt(majorMinor[0], 10),
            "minor": parseInt(majorMinor[1], 10),
            "revisionStr": revisionStr,
            "revision": parseRevisionStrToInt(revisionStr)
        };
    };
    /**
    * Parse the plugin revision string into an integer.
    * 
    * @param {String} The revision in string format.
    * @type Number
    */
    var parseRevisionStrToInt = function(str) {
        return parseInt(str.replace(/[a-zA-Z]/g, ""), 10) || self.revision;
    };
    /**
    * Is the major version greater than or equal to a specified version.
    * 
    * @param {Number} version The minimum required major version.
    * @type Boolean
    */
    self.majorAtLeast = function(version) {
        return self.major >= version;
    };
    /**
    * Is the minor version greater than or equal to a specified version.
    * 
    * @param {Number} version The minimum required minor version.
    * @type Boolean
    */
    self.minorAtLeast = function(version) {
        return self.minor >= version;
    };
    /**
    * Is the revision version greater than or equal to a specified version.
    * 
    * @param {Number} version The minimum required revision version.
    * @type Boolean
    */
    self.revisionAtLeast = function(version) {
        return self.revision >= version;
    };
    /**
    * Is the version greater than or equal to a specified major, minor and revision.
    * 
    * @param {Number} major The minimum required major version.
    * @param {Number} (Optional) minor The minimum required minor version.
    * @param {Number} (Optional) revision The minimum required revision version.
    * @type Boolean
    */
    self.versionAtLeast = function(major) {
        var properties = [self.major, self.minor, self.revision];
        var len = Math.min(properties.length, arguments.length);
        for (i = 0; i < len; i++) {
            if (properties[i] >= arguments[i]) {
                if (i + 1 < len && properties[i] == arguments[i]) {
                    continue;
                } else {
                    return true;
                }
            } else {
                return false;
            }
        }
    };
    /**
    * Constructor, sets raw, major, minor, revisionStr, revision and installed public properties.
    */
    self.FlashDetect = function() {
        if (navigator.plugins && navigator.plugins.length > 0) {
            var type = 'application/x-shockwave-flash';
            var mimeTypes = navigator.mimeTypes;
            if (mimeTypes && mimeTypes[type] && mimeTypes[type].enabledPlugin && mimeTypes[type].enabledPlugin.description) {
                var version = mimeTypes[type].enabledPlugin.description;
                var versionObj = parseStandardVersion(version);
                self.raw = versionObj.raw;
                self.major = versionObj.major;
                self.minor = versionObj.minor;
                self.revisionStr = versionObj.revisionStr;
                self.revision = versionObj.revision;
                self.installed = true;
            }
        } else if (navigator.appVersion.indexOf("Mac") == -1 && window.execScript) {
            var version = -1;
            for (var i = 0; i < activeXDetectRules.length && version == -1; i++) {
                var obj = getActiveXObject(activeXDetectRules[i].name);
                if (!obj.activeXError) {
                    self.installed = true;
                    version = activeXDetectRules[i].version(obj);
                    if (version != -1) {
                        var versionObj = parseActiveXVersion(version);
                        self.raw = versionObj.raw;
                        self.major = versionObj.major;
                        self.minor = versionObj.minor;
                        self.revision = versionObj.revision;
                        self.revisionStr = versionObj.revisionStr;
                    }
                }
            }
        }
    } ();
};
