/* 
*  JSpecView Utility functions
*  Version 1.0, Copyright(c) 2002, Dept of Chemistry, University of the West Indies, Mona
*/

// Browser's User Agent String
var agentStr = navigator.userAgent; 
// Message that is displayed if browser cannot display the applet
var noAppletMsg = "If you don't see an applet here then either: <br>" +
				  "1. You are using an old version of your browser or <br>" +
				  "2. Your browser is not supported <br>";
 

/*
	Returns the name of the browser
	one of: 'Microsoft Internet Explorer', 'Netscape', 'Opera', 'Mozilla Compatible'
	or the empty string if browser is unknown
*/
function getBrowserName(){	 
	var browser = ""; 
	var index;	 
	var name = navigator.appName; 
	 
	index = agentStr.indexOf("Opera");	 
	if(index > 0){ 
		browser = "Opera"; 
	} 
	else{ 
		index = agentStr.indexOf("MSIE"); 
		if(index > 0){ 
			browser = name;		 
		} 
		else{ 
			index = agentStr.indexOf("Mozilla/"); 
			if(index == 0){ 
				if(agentStr.indexOf("Netscape") > 0) 
					browser = "Netscape"; 
				else {					 
					var str = agentStr.substring(8, agentStr.length); 
					if(str.indexOf("4.7") == 0) 
						browser = "Netscape"; 
					else{ 
						browser = "Mozilla or Compatible"; 
					}					 
				} 
			} 
			else{ 
				browser = name; 
			} 
		} 
	} 
 
	return browser; 
} 
 

/*
	Gets the version string of the browser that was identified.
	The parameter 'browser' should be one of the strings returned by getBrowserName();
	if browser in unknown (ie. the browser = "") then an empty string is returned
*/
function getBrowserVersionString(browser){ 
	var version = ""; 
	var index; 
 
	if(browser == "") 
		return version; 
 
	if(browser == "Opera"){ 
		index = agentStr.indexOf("Opera"); 
		if(index == 0){ 
			version = agentStr.substring(6, agentStr.indexOf(" ")); 
		} 
		else{ 
			version = agentStr.substring((index + 6), (agentStr.indexOf("[") - 1)); 
		} 
	} 
	else if(browser == "Microsoft Internet Explorer"){ 
		index = agentStr.indexOf("MSIE");	 
		var tmp = agentStr.substring((index + 5), agentStr.length);	 
		version = tmp.substring(0, tmp.indexOf(";")); 
	} 
	else if(browser == "Netscape"){ 
		if(agentStr.indexOf("Netscape") > 0){ 
			version = agentStr.substring((agentStr.lastIndexOf("/") + 1), agentStr.length); 
		} 
		else{ 
			version = agentStr.substring(8, (agentStr.indexOf("[") - 1)); 
		} 
	} 
	else if(browser == "Mozilla or Compatible"){ 
		version = agentStr.substring(8, (agentStr.indexOf(" "))); 		
	} 
	/* 
	else if(browser == "Mozilla"){ 
		index = agentStr.indexOf("rv"); 
		version = agentStr.substring((index + 3), agentStr.indexOf(")")); 
	} 
	*/ 
 
	return version; 
} 
 
/*
	Inserts an applet using the <applet> tag
*/
function insertAppletTag(_name, _code, _codebase, _archive, _width, _height,  
	                     _hspace, _vspace, _align, _paramNames, _paramValues){ 
	var appletStr; 
 
	appletStr = "<applet";	 
	appletStr += "	id		= '" + _name + "'";	 
	appletStr += "	code		= '" + _code + "'" + "\n"; 
	appletStr += "	codebase	= '" + _codebase + "'" + "\n"; 
	if(_archive != "") 
		appletStr += "	archive	= '" + _archive + "'";	 
	appletStr += "	width		= '" + _width + "'" + "\n"; 
	appletStr += "	height	= '" + _height + "'" + "\n"; 
	appletStr += "	hspace	= '" + _hspace + "'" + "\n"; 
	appletStr += "	vspace	= '" + _vspace + "'" + "\n"; 
	appletStr += "	align	= '" + _align + "'" + "\n"; 
	appletStr += "mayscript";
	appletStr += ">\n"; 
	if(_paramNames != null){
		for(var i = 0; i < _paramNames.length; i++){ 
			appletStr += "<param name= '" + _paramNames[i] + "' value = '" + _paramValues[i] + "'>" + "\n"; 
		} 
	}
	appletStr += noAppletMsg;
	appletStr += "</applet>"; 
 
	document.writeln(appletStr); 
} 
 
/*
	Inserts an applet using the <object> tag
classid = "clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
codebase = "http://java.sun.com/products/plugin/autodl/jinstall-1_4_1_02-windows-i586.cab#Version=1,4,1,20"
appletStr += "	classid	=clsid:CAFEEFAC-0014-0001-0002-ABCDEFFEDCBA\n"; 
appletStr += "	codebase	= 'http://java.sun.com/products/plugin/autodl/jinstall-1_4_1_02-windows-i586.cab#Version=1,4,1,20'>\n";

*/

function insertObjectTag(_name, _code, _codebase, _archive, _width, _height,  
	                     _hspace, _vspace, _align, _paramNames, _paramValues, _javaVersion){ 	
	var appletStr; 
	appletStr = "<object \n"; 
	appletStr += "	classid	=clsid:8AD9C840-044E-11D1-B3E9-00805F499D93\n"; 
	appletStr += "	width		= '" + _width + "'" + "\n"; 
	appletStr += "	height	= '" + _height + "'" + "\n"; 
	appletStr += "	hspace	= '" + _hspace + "'" + "\n"; 
	appletStr += "	vspace	= '" + _vspace + "'" + "\n"; 
	appletStr += "	align		= '" + _align + "'" + "\n"; 
	appletStr += "	id		= '" + _name + "'\n"; 	
	appletStr += "	codebase	= 'http://java.sun.com/products/plugin/autodl/jinstall-1_4_1_02-windows-i586.cab#Version=1,4,1,20'>\n";
	appletStr += "  <param name = 'id' value = '"+ _name + "'>\n";
	appletStr += "  <param name = 'mayscript' value = 'true'>\n"; 
	appletStr += "  <param name = 'code' value = '" + _code + "'>\n";  
	appletStr += "  <param name = 'codebase' value = '" + _codebase + "'>\n"; 
	if(_archive != "") 
		appletStr += "  <param name = 'archive' value = '" + _archive + "'>\n";	 
	appletStr += "  <param name = 'type' value = 'application/x-java-applet;version="+ _javaVersion +"'>\n"; 
	if(_paramNames != null){
		for(var i = 0; i < _paramNames.length; i++){ 
			appletStr += "<param name= '" + _paramNames[i] + "' value = '" + _paramValues[i] + "'>" + "\n"; 
		} 
	}
	appletStr += noAppletMsg;
	appletStr += "</object>";	 
 
	document.writeln(appletStr); 
} 
 

/*
	Inserts an applet using the <embed> tag
*/
function insertEmbedTag(_name, _code, _codebase, _archive, _width, _height,  
	                    _hspace, _vspace, _align, _paramNames, _paramValues, _javaVersion){ 
	var appletStr; 
 
	appletStr = "<embed \n"; 
	appletStr += "	type 		= 'application/x-java-applet;version="+ _javaVersion +"'\n"; 
	appletStr += "	name		= '" + _name + "'\n"; 
	appletStr += "	code		= '" + _code + "'" + "\n";	 
    appletStr += "	codebase	= '" + _codebase + "'" + "\n"; 
	if(_archive != "") 
	appletStr += "	archive	= '" + _archive + "'" + "\n"; 
	appletStr += "	width		= '" + _width + "'" + "\n"; 
	appletStr += "	height	= '" + _height + "'" + "\n"; 
	appletStr += "	hspace	= '" + _hspace + "'" + "\n"; 
	appletStr += "	vspace	= '" + _vspace + "'" + "\n"; 
	appletStr += "	align		= '" + _align + "'" + "\n";	
	appletStr += "	mayscript	= 'true'\n";
	if(_paramNames != null){
		for(var i = 0; i < _paramNames.length; i++){ 
			appletStr += _paramNames[i] + " = '" + _paramValues[i] + "'\n"; 
		}
	}
	appletStr += "pluginspage='http://java.sun.com/products/plugin/index.html#download'>\n"; 
	appletStr += "<noembed>\n" + noAppletMsg + "\n</noembed>\n"; 
	appletStr += "</embed>";	 
 
	document.writeln(appletStr); 
} 
 
 
/*
	Inserts an applet using a tag that is determined by the browser type and version
	returned by getBrowserName() and getBrowserVersionString(browser);
*/
function insertApplet(_name, _code, _codebase, _archive, _width, _height,  
	                  _hspace, _vspace, _align, _paramNames, _paramValues, _javaVersion){ 
	var browserName = getBrowserName();	 
	var browserVersion = getBrowserVersionString(browserName); 
		 
	if(browserName == "Netscape"){ 
		if(browserVersion >= "6") 
			insertAppletTag(_name, _code, _codebase, _archive, _width, _height,  
	                        _hspace, _vspace, _align, _paramNames, _paramValues); 
		else if(browserVersion > 4){ 
				insertEmbedTag(_name, _code, _codebase, _archive, _width, _height,  
	                           _hspace, _vspace, _align, _paramNames, _paramValues, _javaVersion);			 
		} 
		else{ 
			document.writeln("You are using an old version of Netscape.<br />"); 
			document.writeln("You need to upgrade before you can view the applet on this page.<br />"); 
		} 
	} 
	else if(browserName == "Microsoft Internet Explorer"){ 
		if(browserVersion >= 5) 
			insertObjectTag(_name, _code, _codebase, _archive, _width, _height,  
	                        _hspace, _vspace, _align, _paramNames, _paramValues, _javaVersion); 
		else{ 
			document.writeln("You are using an old version of Internet Explorer.<br />"); 
			document.writeln("You need to upgrade before you can view the applet on this page.<br />"); 
		} 
	} 
	else if(browserName == "Opera"){ 
		if(browserVersion >= 6) 
			insertObjectTag(_name, _code, _codebase, _archive, _width, _height,  
	                        _hspace, _vspace, _align, _paramNames, _paramValues, _javaVersion); 
		else{ 
			document.writeln("You are using an old version of Opera.<br />"); 
			document.writeln("You need to upgrade before you can view the applet on this page.<br />"); 
		} 
	}		 
	else if(browserName == "Mozilla or Compatible"){ 
		if(browserVersion >= 5.0) 
			insertEmbedTag(_name, _code, _codebase, _archive, _width, _height,  
	                        _hspace, _vspace, _align, _paramNames, _paramValues, _javaVersion);		 
		else{ 
			document.writeln("You are using an old version of your browser.<br />"); 
			document.writeln("You need to upgrade before you can view the applet on this page.<br />"); 
		} 
	}	 
	else{ 
		document.writeln(noAppletMsg);
		 
	} 
} 
 

/*
	Convenience function that inserts an applet by calling insertApplet()
	with values from variables defined in the html or in the params.js file
	before this function is called. 
	Note: the variable names should be the same as those in the params.js
	      (or variable names below).
*/
function insertSingleApplet(){ 
	insertApplet(name, code, codebase, archive, width, height,  
	            hspace, vspace, align, paramNames, paramValues, javaVersion) 
} 
 

