// LMS Check JavaScripts, copyright (c) 2009, eLearning Corner, LLC
// This product is copyrighted and licensed for use at a rate so friendly you won't want to develop it yourself.


function fnLmsCheck () {
	$('div#jshelp').hide(); // Hide the enable JavaScript text, may work for some browsers not covered in the inline code of the .htm file

	function validParm (a, b) { // Check for existence of both parameters
		if (typeof a == 'undefined' || typeof b == 'undefined') {
			alert('Undefined parameter. ');	
			return true; //  Return true if any undefined parameters found.
		}
		return false; 
	}
	
	function setRequired (strTestName, strhtml) {
		if (validParm(strTestName, strhtml) ) {return;} // Don't die if the configuration file has a bad parameter
		var strSelector = 'tr#' + strTestName + ' td.r';
		$(strSelector).html(strhtml);
	}
	
	function setWorkstation (strTestName, strhtml) {
		if (validParm(strTestName, strhtml) ) {return;} // Don't die if the configuration file has a bad parameter
		var strSelector = 'tr#' + strTestName + ' td.w';
		$(strSelector).html(strhtml);
	}
	
	function setPassFail (strTestName, bolPassFail) {
		if (validParm(strTestName, bolPassFail) ) {return;} // Don't die if the configuration file has a bad parameter
		var strSelector = 'tr#' + strTestName + ' td.s img';
		$(strSelector).attr(bolPassFail ? { src: 'checkgreen.gif', alt: 'Green check' } : { src: 'crossred.gif', alt: 'Red Cross' });
	}
	
	function setRow (strTestName, strHtmlR, strHtmlW, bolPassFail) { // Set all 3 cells with a single call
		setRequired(strTestName, strHtmlR);
		setWorkstation(strTestName, strHtmlW);
		setPassFail(strTestName, bolPassFail);
	}
	
	function bolCommaNotation (strV1, strV2) { // compare that V2 is .GT. V1 (version strings in format N.N.N.N)
		var arrV1 = strV1.split(','); 
		var arrV2 = strV2.split(',');  // Convert dot-notation strings to arrays of dot-less strings
		
		var intV1 = (((100 * parseInt(arrV1[0], 10)) + parseInt(arrV1[1], 10)) * 100 + parseInt(arrV1[2], 10)) * 100 + parseInt(arrV1[3], 10);
		var intV2 = (((100 * parseInt(arrV2[0], 10)) + parseInt(arrV2[1], 10)) * 100 + parseInt(arrV2[2], 10)) * 100 + parseInt(arrV2[3], 10);

		return intV2 >= intV1;
	}


	// Flag whether JavaScript is enabled or not.  This doesn't require any specific external library.
	setRow('js', 'Enabled', 'Enabled', true);
	
	// Remove any tests which are not enabled in configuration.js
	for (var obj in lmsCheck) {  // If enabled=false for this object, remove the row from the table.
		if ( lmsCheck[obj].disabled) {  $('tr#'+obj).remove();	} // Remove any disabled row
	}

	// Stripe the table rows for good looks
	$('#lmscheck tr:even').addClass('lightgray'); // Alternate row shading for visibility after specified rows are deleted above.
	$('#lmscheck tr:first').removeClass('lightgray').addClass('darkgray'); // Header row is dark gray
	$('#lmscheck .s').addClass('white'); // Last column is white for green/red pass/fail
	
	// Detect the Java Version on the machine.  Java has a decimal version notation N.M
	// Uses plugindetect.js
	if ( !lmsCheck.java.disabled) {
		var arrPluginDetect = (PluginDetect.getVersion('Java', 'A.class') || '0,0,0,0').split(','); // i,j,k,l: i=1 (Java), j=version, l=update
		var strVersion = (arrPluginDetect[0] == '1') ? ( 'Java ' + arrPluginDetect[1] + ' Update ' + arrPluginDetect[3]) : 'No Java Detected';
		setRow('java', lmsCheck.java.requiredText, strVersion, true);
	}
	
	// Detect the Browser Version.  Browsers use a decimal version notation N.M
	// Uses browserdetect.js from Quirksmode
	if ( !lmsCheck.browser.disabled) {
		BrowserDetect.init();
		var strB = BrowserDetect.browser, intV = BrowserDetect.version, intMinV = lmsCheck.browser['min'+strB];
		var boolOK = intMinV ? (intMinV <= intV) : false;
		var strMsg = boolOK ? '' : (intMinV ? ('<br />Minimum version is ' + intMinV ) : ('<br />' + strB + ' is not supported.') );
		setRow('browser', lmsCheck.browser.requiredText, strB + ' ' + intV + strMsg, boolOK);
	}
	
	// Detect pop-up blocking.  Uses no external library.
	if ( !lmsCheck.popup.disabled) {
		var objPopupWin = window.open('','','width=1,height=1,left=0,top=0,scrollbars=no');
		var bolPopupEnabled = objPopupWin ?  true : false;
		if(bolPopupEnabled) { objPopupWin.close();}
		setRow('popup', lmsCheck.popup.required ? 'Enabled' : 'Disabled', bolPopupEnabled ? 'Enabled' : 'Disabled', lmsCheck.popup.required ? bolPopupEnabled : true);
	}
	
	// Check for session cookies
	// Uses no external library.
	if ( !lmsCheck.cookies.disabled) {
		// Create a test session cookie
		document.cookie = 'testcookie=none;path=/'; // Set a test cookie
		var bolGoodCookie = document.cookie.indexOf('testcookie=none') >= 0; //  True if session cookies are enabled
		setRow('cookies', 
			    lmsCheck.cookies.required ? 'Enabled' : 'Disabled', 
			    bolGoodCookie ? 'Enabled' : 'Disabled', 
			    lmsCheck.cookies.required ? bolGoodCookie : true);
	}
	
	// Check display resolution.  Uses no external library.
	if ( !lmsCheck.resolution.disabled) {
		setRequired('resolution', lmsCheck.resolution.width.toString() + ' X ' + lmsCheck.resolution.height + ' pixels');
		setWorkstation('resolution', screen.width.toString() + ' X ' + screen.height + ' pixels'); // Update the report
		setPassFail('resolution', screen.width >= lmsCheck.resolution.width && screen.height >= lmsCheck.resolution.height);
	}
	
	// Check bandwidth
	// Uses jquery.js
	if ( !lmsCheck.bandwidth.disabled) {
		var intTime1 = new Date().getTime(); // start time in milliseconds
		$.ajax({
			url: 'payloadtest.html',
			cache: false,
			success: function(html){
				var intDeltaT = new Date().getTime() - intTime1; // finish time in milliseconds
				var intBandwidth = (100000*8)/(intDeltaT/1000)/1000; // Calculate bandwidth in kbps
				intBandwidth = parseInt(intBandwidth.toString(), 10); // Integer portion of kbps
				setRow('bandwidth', 
				       lmsCheck.bandwidth.minBandwidth.toString() + 'kb/s', 
				       intBandwidth.toString() + 'kb/s',
				       intBandwidth >= lmsCheck.bandwidth.minBandwidth);
			}
		});
	}
	
	// Check for flash.  Flash version is in comma-notation A,B,C,D
	// Uses plugindetect.js
	if ( !lmsCheck.flash.disabled) {
		var bolFlashInstalled = PluginDetect.isMinVersion('Flash', '0'); // 
		var strFlashVersion = PluginDetect.getVersion('Flash') || '0,0,0,0'; // Flash version number
		setRow('flash', lmsCheck.flash.minVersion, strFlashVersion, bolFlashInstalled ? bolCommaNotation(lmsCheck.flash.minVersion, strFlashVersion) : false );
	}

}


