/*
 * Functions for detecting a version of java (specifically, the browser plugin)
 * installed on a user's machine. It first attempts to use navigator.plugins,
 * and if not available, attempts to load an applet and make calls to it.
 * @todo Make this a class?
 * Chris Lewis <development@thorpeforms.com>, 07/06/2005
 */

/* Returns true if we can use navigator.plugins. */
function canDetectPluginsDOM0()
{
	if(! navigator.plugins)
		return false;
	if(! navigator.plugins.length > 0)
		return false;
	return true;
}

/*
 * Detect java using the DOM 0 navigator.plugins collection.
 * @param arguments[0] the minimum version required
 * @param arguments[1] the maximum version required
 * @return true if a java version is found whose version is at least
 * 	the minimum and not over the maximum
 */
function jDetectDOM0()
{
	if(! canDetectPluginsDOM0())
		return false;
	minJVer = (arguments.length > 0) ? arguments[0]: '1.1';
	maxJVer = (arguments.length > 1) ? arguments[1]: '1.1';
	var foundIt = false;
	for(p = 0; p < navigator.plugins.length && foundIt == false; p++)
	{
		plugin = navigator.plugins[p];
		for(m = 0; m < plugin.length; m++)
		{
			mimeType = plugin[m].type;
			if(mimeType.indexOf('x-java') != -1
				&& mimeType.indexOf('version=') != -1)
			{
				jVer = mimeType.substr(mimeType.indexOf('version=') + 8, 3);
				//if(jVer >= minJVer && jVer <= maxJVer)
				if( j2PartVersionCompare(jVer, minJVer) > -1
					&& j2PartVersionCompare(jVer, maxJVer) < 1 )
				{
					foundIt = true;
					m = plugin.length;
				}
			}
		}
	}
	return foundIt;
}

/*
 * Detect java using method calls on a loaded applet.
 * @param arguments[0] the minimum version required
 * @param arguments[1] the maximum version required
 * @param arguments[2] the applet class name
 * @param arguments[3] the applet id
 * @return true if a java version is found whose version is at least
 * 	the minimum and not over the maximum
 */
function jDetectSniffApplet()
{
	/* Init args and params. */
	minJVer = (arguments.length > 0) ? arguments[0]: '1.1';
	maxJVer = (arguments.length > 1) ? arguments[1]: '1.1';
	appletId = (arguments.length > 3) ? arguments[2]: 'jreSniffer';
	appletName = (arguments.length > 2) ? arguments[3]: 'com.thorpe.util.JRESnifferApplet';
	appletCodeBase = (arguments.length > 2) ? arguments[4]: 'http://www.thorpeforms.com/js';
	appletArchive = (arguments.length > 2) ? arguments[5]: 'jresniffer.jar';
	var applet = null;
	/* Insert the sniffer applet. */
	document.writeln('<object id="'+appletId+'" width="1" height="1"'
		+ 'classid="clsid:8ad9c840-044e-11d1-b3e9-00805f499d93">'
		+ '<param name="type" value="application/x-java-applet;version='+minJVer+'"/>'
		+ '<param name="code" value="'+appletName+'"/>'
		+ '<param name="codebase" value="'+appletCodeBase+'"/>'
		+ '<param name="archive" value="'+appletArchive+'"/>'
		+ '<param name="scriptable" value="true"/>'
		+ '</obj'+'ect>'); /* Break up close tag so parsers dont freak. */
	/* Grab a ref to the applet. */
	if(document.getElementById)
		applet = document.getElementById(appletId);
	else if(document.all)
		applet = document.all[appletId];
	if(applet == null)
		return false;
	/* Check it and we're done. */
	jreV = applet.getSystemProperty('java.version');
	if( j2PartVersionCompare(jreV, minJVer) > -1
		&& j2PartVersionCompare(jreV, maxJVer) < 1 )
		return true;
	else
		return false;
}

/*
 * Perform a two part version compare on a typical java version. The arguments
 * are expected to be in the form of [0-9].[0-9].
 * @param ver1 the version (ex: 1.4)
 * @param ver2 the other version (ex: 1.5)
 * @return -1, 0, 1 if ver1 is less than, equal to, or greater than ver2.
 * 	-2 will be returned if either of the versions is less than 3 characters
 */
function j2PartVersionCompare(ver1, ver2)
{
	if(ver1.length < 3 || ver2.length < 3)
		return -2;
	/* Compare the first parts. */
	v1 = (ver1.substr(0,1)) * 1;
	v2 = (ver2.substr(0,1)) * 1;
	if(v1 < v2)
		return -1;
	else if(v1 > v2)
		return 1;
	/* Compare the second parts. If here than first parts are equal. */
	v1 = (ver1.substr(2,1)) * 1;
	v2 = (ver2.substr(2,1)) * 1;
	if(v1 < v2)
		return -1;
	else if(v1 > v2)
		return 1;
	else
		return 0;
}
