// debug #'s in use thru #5

// Must match protexpl/version.js (used to detect bad installations)
//FORMAT var shared_version = "1.982 Beta";

var shared_version = "2.80";

// January 2005 (PE 2.45):
//This file was formerly named N4C2.JS, but that is used
// not only by PE but also by FIND-NCB, and MORPHER.
//Numerous PE-specific changes made it wise to rename to BROWGET.JS
//leaving the original N4C2.JS for the other two apps.

//- - - - - - - - - - - - - - - - - - - - - - - - - - - 
/*

THE PURPOSE OF BROWGET.JS IS TO GET BROWSER TYPE, VERSION, SP, ETC.
BROWSER CHECKING IS DONE WHEN PE2.HTM CALLS FUNCTIONS
browser_ok() (defined in pe2.htm)
and
chime_version_ok() defined below.

browser_ok() CALLS NO FUNCTION HERE -- IT DEPENDS ON VARIABLES DEFINED
WHEN BROWGET.JS IS LOADED.

Browsers supported:
	isNetscape4 = Netscape 4.7x-4.8 (<5.00)
		thisBrowser = "ns4" (appName "Netscape" without userAgent "Gecko"
			or "Firefox".
			Most browsers report or may be configured to report themselves
			as "Netscape")

	isIE = IE 6 SP1 or later
		thisBrowser = "ie"

	isGecko if Mozilla, Netscape 7, FireFox.
		thisBrowser = "moz", "ns7", "ff"

Browsers specifically excluded:
	Safari
	Opera

*/
//- - - - - - - - - - - - - - - - - - - - - - - - - - - 
var pe_fatal_error = false;

NP = navigator.plugins;
var chii = -1;
var chimeVersionCookieName = "chi_ver";
var chimeVersionStr = ""; // string, e.g. "2.6 SP4"
var chimeVersionFP = 0; // floating point
var chimeVersionSP; // integer, e.g. 4, or 0 if no SP

var thisBrowser = "unknown";
var browserVersion = -1;
var isNetscape4 = false;
var isIE = false;
var isGecko = false; //mozg
var IE_SP;

// DETECT BROWSER TYPE AND VERSION
// This new block checks the correct versions of Netscape (>7.0) and
// Mozilla (>1.3) @Enrique Castro
// Eric added code for browserVersion and reworked this code.

// DETECT GECKO FAMILY
if (navigator.userAgent.toLowerCase().indexOf("gecko") != -1)
	isGecko = true;

var isSafari = false;
// DETECT Safari to avoid "fatal error, no 'rv:'" at frontdoor.
if (navigator.userAgent.toLowerCase().indexOf("safari") != -1)
	isSafari = true;

if (isGecko && !isSafari)
{
	// This checks for Netscape 7.1 or upper
	var nin = navigator.userAgent.toLowerCase().lastIndexOf("netscape");
	if (nin != -1)
	{
		// syntax ...Netscape/7.1 .. therefore +9
		browserVersion = parseFloat(navigator.userAgent.substring(nin + 9));
//		alert("browget.js #4: Gecko Netscape browserVersion = " + browserVersion);
		if (browserVersion > 7.0)
			thisBrowser = "ns7";
	}
	// nin is used below!

	// FIREFOX?
	var fin = navigator.userAgent.toLowerCase().lastIndexOf("firefox");
	if (fin != -1)
		thisBrowser = "ff"; // version detetion below, same as for mozilla

	// if isGecko but neither netscape nor firefox, must be Mozilla
	if (thisBrowser != "ns7" && thisBrowser != "ff" && nin == -1)
		thisBrowser = "moz";

	// get browserVersion for FF, MOZ
	if (thisBrowser != "ns7" && nin == -1)
	{
		rvi = navigator.userAgent.toLowerCase().lastIndexOf("rv:");
		if (rvi == -1)
		{
			top.pe_fatal("'rv:' not found in Gecko browser navigator.userAgent");
			// top.window.close(); // can't do this
			// pe_fatal() sets top.pe_fatal_error
		}
			
		browserVersion =
			(parseFloat(navigator.userAgent.substr(rvi + 3)));
	}
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - 

// DETECT NETSCAPE 4.X
if (navigator.appName=='Netscape') // not "Navigator"!
{
	// "OS X" eliminates Safari and Netscape 7 on OSX
	// Opera has pref settings to emulate many browsers

	// Safari and Opera already excluded in pe.htm before pe2.htm
	if (!isGecko)
	{
		thisBrowser = "ns4";
	  isNetscape4 = true;
	}
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - 
// DETECT IE
if (navigator.appName=='Microsoft Internet Explorer')
{
	// Opera has an MSIE emulation mode
	// in case Safari has an MSIE emulation mode
	if (navigator.userAgent.indexOf("Opera") == -1 &&
		navigator.userAgent.indexOf("Safari") == -1)
	{
		thisBrowser = "ie";
		isIE = true;		
	}
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - 
// GET IE VERSION, SP#

function get_IE_version()
{
	var vi = navigator.appVersion.indexOf('MSIE ') + 5;
	var iev = parseFloat(navigator.appVersion.substring(vi));
//	alert("browget.js get_IE_version() returns " + iev);
	return iev;
}
function get_IE_SP() // ";SP2;Q306121;Q312461;"
{
	var isp = navigator.appMinorVersion.indexOf("SP");
	if (isp == -1)
		return null;
	var iesp = parseFloat(navigator.appMinorVersion.substring(isp + 2));
//	alert("browget.js get_IE_SP() returns " + iesp);
	return iesp;
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - 

if (isNetscape4)
	browserVersion = parseFloat(navigator.appVersion);
if (isIE)
	browserVersion = get_IE_version();
if (isIE)
	IE_SP = get_IE_SP();

//- - - - - - - - - - - - - - - - - - - - - - - - - - - 
//- - - - - - - - - - - - - - - - - - - - - - - - - - - 

function show_browser_info(caller) // alias sbi
{
alert("browget.js show_browser_info() called from\n" +
	caller + "\n" +
	"navigator.userAgent = " + navigator.userAgent + "\n" +
	"thisBrowser = " + thisBrowser + "\n" +
	"isIE = " + isIE + "\n" +
	"isNetscape4 = " + isNetscape4 + "\n" +
	"isGecko = " + isGecko + "\n" +
	"browserVersion = " + browserVersion + "\n" +
	"IE_SP = " + IE_SP + "\n" +
	"\n" +
// the chime info is passed into PE via query parameters, so Str not defined.
//	"chimeVersionStr = " + chimeVersionStr + "\n" +
	"chimeVersionFP = " + chimeVersionFP + "\n" +
	"chimeVersionSP = " + chimeVersionSP + "\n"
	);
}

/*
alert("browget.js debug#1:\n" +
	"navigator.appCodeName " + navigator.appCodeName + "\n" +
	"navigator.appName " + navigator.appName + "\n" +
	"navigator.appVersion " + navigator.appVersion + "\n" +
	"navigator.appMinorVersion " + navigator.appMinorVersion + "\n" +
	"navigator.browserLanguage " + navigator.browserLanguage + "\n" +
	"navigator.cookieEnabled " + navigator.cookieEnabled + "\n" +
	"navigator.javaEnabled() " + navigator.javaEnabled() + "\n" +
	"navigator.cpuClass " + navigator.cpuClass + "\n" +
	"navigator.language " + navigator.language + "\n" +
	"navigator.onLine " + navigator.onLine + "\n" +
	"navigator.platform " + navigator.platform + "\n" +
	"navigator.systemLanguage " + navigator.systemLanguage + "\n" +
	"navigator.userAgent " + navigator.userAgent + "\n" +
	"navigator.userLanguage " + navigator.Language + "\n" +
	"navigator.userProfile " + navigator.Profile + "\n");
*/

//- - - - - - - - - - - - - - - - - - - - - - - - - - - 
//- - - - - - - - - - - - - - - - - - - - - - - - - - - 
//- - - - - - - - - - - - - - - - - - - - - - - - - - - 
// GET CHIME INFO IF plugins EXISTS
//- - - - - - - - - - - - - - - - - - - - - - - - - - - 
// find a netscape.plugin
function findNP(match)
{
	var nplen = NP.length;
	var piname;

	for (i = 0; i < nplen; i++)
	{
		piname = NP[i].name.toLowerCase();
	
		if (piname.indexOf(match) != -1)
			return(i);
	}
	return (-1);
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - 
function any_chime()
{
	if (chii != -1)
		return(true); // any_chime has already been called
	chii = findNP("chemscape chime");
	if (chii == -1)
	{
		// Beginning Version 2.6SP4 "MDL* Chime 2.6 SP4" where * is circled R.
		var i = findNP("chime");
		var j = findNP("mdl");
		if (i == j)
			chii = i;		
	}

	if (chii == -1)
		return false;

	chime_version(); // sets chimeVersionStr, chimeVersionFP, chimeVersionSP
	return true;
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - 
function chiv2()
{
	// chii = CHIme Index in plugin array
	if (!any_chime()) // sets chimeVersionStr, chimeVersionFP, chimeVersionSP
		return false;

//	alert(NP[chii].name);

	var chok = false; // chime upgrade needed
	var maybe = false; // user must confirm
	
	// MAC
	if (navigator.appVersion.toLowerCase().indexOf("mac") != -1)
	{
		if ((chimeVersionFP >= 2.6) && (chimeVersionSP >= 3))
			chok = true;
	}
	else // Windows
	{
		if ((chimeVersionFP == 2.6) && (chimeVersionSP >= 4))
			chok = true;
	}

//@@

// There haven't been any 'betas' since 2.6
	// IF 2.X BETA, MAYBE OK
//	if (NP[chii].name.toLowerCase().indexOf("beta") != -1)
//		maybe = true;

	// IF NOT 2.X, IF >2.6, GIVE IT A TRY (V3?)
	if (!chok)
	{
		if (chimeVersionFP > 2.6)
		{
			chok = true;
			maybe = true;
		}
	}
	if (maybe)
		chime_maybe();

	return (chok);
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - 
// Returns version number of Chime (Netscape only)
function chime_version()
{
	// chii = CHIme Index in plugin array
	if (!any_chime())
		return "";

//	alert(NP[chii].name);

// <= 2.6 SP3 "Chemscape Chime N.NN"
// >= 2.6 SP4 "MDLr Chime n.n SPn"
	var cvi = NP[chii].name.toLowerCase().indexOf("chime");
	chimeVersionStr = NP[chii].name.substring(cvi + 6); // skip "chime "
	chimeVersionFP = parseFloat(chimeVersionStr);
	cvi = NP[chii].name.indexOf(" SP");
	if (cvi == -1)
		chimeVersionSP = 0;
	else
		chimeVersionSP = parseInt(NP[chii].name.substring(cvi + 3));

//	alert("browget.js #3 chimeVersionStr=|" + chimeVersionStr + "|\n" +
//		"chimeVersionSP=|" + chimeVersionSP + "|");
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - 
function chime_maybe()
{
	alert("debug #2 browget.js: chimeVersionStr = " + chimeVersionStr);

	var cvc = getCookie(chimeVersionCookieName);
	if (chimeVersionStr == cvc)
		return; // user has set cookie to stop warning.

	var m = "The Chime version with this browser,\n\"" +
		NP[chii].name +
		"\"\nis unexpected and may not work properly.\n" +
		"Cancel if you never want this warning again.";

	if (confirm(m))
		return;

	if (!confirm("You will never again be warned about this\n" +
		"version of Chime if you press OK.\n" +
		"OK only if you have verified that it works!\n" +
		"Cancel to be warned in your next PE session."))
			return;

	// Set cookie to avoid annoying user with warning.
	putCookie_year(chimeVersionCookieName, chimeVersionStr);
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - 
// chime_version_ok() is called only after a suitable browser is verified,
// and only for Netscape. Returns true/false.
function chime_version_ok()
{
	if (!chiv2())  // determines if chime version is satisfactory
		return false;
	else return true;
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - 
// for debugging:
//show_browser_info("shared/browget.js #5: show_browser_info():");
//- - - - - - - - - - - - - - - - - - - - - - - - - - - 
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function pe_fatal(errmsg)
{
	var msg = "Fatal Error in Protein Explorer:<p>" +
		errmsg + "<p>" +

"Please copy this error message and email it to emartz@microbio.umass.edu. \
Please add your platform (Windows XP? Mac OSX? etc.), your browser and \
its version (Mozilla 1.7? Internet Explorer 6 SP1?). Thanks for helping \
to make Protein Explorer better!";

	FatalWin=window.open("blank.htm", "FatalWinT",
		"scrollbars=1,menubar=1,status=1,toolbar=1,width=400,height=400,resizable=1");
	with (FatalWin.document)
	{
		open();
		writeln(msg);
		close();
	}

	window.FatalWin.focus();
	pe_fatal_error = true;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
//- - - - - - - - - - - - - - - - - - - - - - - - - - - 
//- - - - - - - - - - - - - - - - - - - - - - - - - - - 
//- - - - - - - - - - - - - - - - - - - - - - - - - - - 
//- - - - - - - - - - - - - - - - - - - - - - - - - - - 
//- - - - - - - - - - - - - - - - - - - - - - - - - - - 
