/*
browser.js 8/1/2003
Author: Dale Swain

Steps to use:
	1. Create new instance: 
		- example: var bd = new browser_detect();
	2. Override any defauts if desired:
		- example: bd.set_min_version('IE', 6.0);
	3. Check browser credentials and alert as needed
		- example: bd.check_browsers();
	4. get help
		- example: bd.help();
*/

// Global vars
var AOL, IE, IEMAC, KONQ, LNX, MOZ, NS, OPERA, SAFARI, bd;

////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Main function, instantiates browser detection variables and functions
function browser_detect()
{
	// default values
	this.debug = false;
	this.browsarr = Array();
	this.display_error_text = '';
	this.appversion = '';
	this.platform = '';
	this.useragent = '';
	this.version = 0;
	
	this.add_browser = add_browser;
	this.check_browsers = check_browsers;
	this.create_redirect_page = create_redirect_page;
	this.get_min_version = get_min_version;
	this.set_min_version = set_min_version;
	this.help = help;
	
	this.add_browser( "AOL", 6.0, "America Online", "http://www.aol.com/downloads" );
	this.add_browser( "IE", 5.1, "Microsoft Internet Explorer", 'http://www.microsoft.com/windows/ie/default.asp' );
	this.add_browser( "IEMAC", 5.1, "Microsoft Internet Explorer for Mac", 'http://www.microsoft.com/mac/products/internetexplorer/internetexplorer.aspx' );
	this.add_browser( "KONQ", 2.1, "Konquerer Web Browser", "http://www.kde.org/download" );
	this.add_browser( "MOZ", 0.9, "Mozilla Web Browser", 'http://www.mozilla.org/' );
	this.add_browser( "LNX", -1, "Lynx Web Browser", 'http://www.lynx.org/' );
	this.add_browser( "NS", 6.1, "Netscape", 'http://channels.netscape.com/ns/browsers/default.jsp' );
	this.add_browser( "OPERA", 7.0, "Opera Web Browser", "http://www.opera.com/download" );
	this.add_browser( "SAFARI", 85, "Safari", 'http://www.apple.com/safari/' ); // Mac markets as 'version 85' for release 1.0
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Adds a new browser to the array of known browsers
function add_browser( browser, version, description, download )
{
	for( var i = 0; i < this.browsarr.length; i++ ) if( this.browsarr[i][0] == browser ) return alert( 'There is already a browser named ' + browser );
	this.browsarr[this.browsarr.length] = Array( browser, version, description, download );
	return true;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Returns the minimum allowed version for specified browser
function get_min_version( browser ) 
{
	for( var i = 0; i < this.browsarr.length; i++ ) if( this.browsarr[i][0] == browser ) return this.browsarr[i][1];
	alert( 'There is no valid browser for variable ' + browser );
	return false;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Sets the minimum allowed version for specified browser
function set_min_version( browser, version ) 
{
	for( var i = 0; i < this.browsarr.length; i++ ) if( this.browsarr[i][0] == browser ) return this.browsarr[i][1] = version;
	alert( 'There is no valid browser for variable ' + browser );
	return false;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Executes the check to see if the minimum browser requirements have been met.
function check_browsers()
{
	if( window.onerror ) window.onerror = this.create_redirect_page(); // fail safe
	var appver = window.navigator.appVersion.toLowerCase();
	var plat = window.navigator.platform.toLowerCase();
	var ua = window.navigator.userAgent.toLowerCase();
	var ver = '';

	// these vars get set globally for all to use. the first 2 are to help clear things up....
	UN_IE = ( ua.indexOf("aol") != -1 || ua.indexOf("opera") != -1 ) ? true : false;
	UN_MOZ = ( ua.indexOf("konqueror") != -1 || ua.indexOf("msie") != -1 || ua.indexOf("netscape") != -1 || ua.indexOf("safari") != -1 ) ? true : false;

	AOL = ( ua.indexOf("aol") != -1 ) ? true : false;
	IE = ( ua.indexOf("msie") != -1 && plat == 'win32' && !UN_IE ) ? true : false;
	IEMAC = ( ua.indexOf("msie") != -1 && plat == 'macppc' && !UN_IE ) ? true : false;
	KONQ = ( ua.indexOf("konqueror") != -1 ) ? true : false;
	LNX = ( ua.indexOf("lynx") != -1 ) ? true : false;
	MOZ = ( ua.indexOf("mozilla") != -1 && !UN_MOZ ) ? true : false;
	NS = ( ua.indexOf("netscape") != -1 ) ? true : false;
	OPERA = ( ua.indexOf("opera") != -1 ) ? true : false;
	SAFARI = ( ua.indexOf("safari") != -1 ) ? true : false;
	
	// determine the version. gets tricky cuz some browsers meet more than one spec.
	if( AOL ) ver = ua.substring( ua.indexOf("aol ")+4, ua.indexOf(";", ua.indexOf("aol ")) );
	else if( IE || IEMAC ) ver = ua.substring( ua.indexOf("msie ")+5, ua.indexOf(";", ua.indexOf("msie ")) );
	else if( KONQ ) ver = ua.substring( ua.indexOf("konqueror/")+10, ua.indexOf(";", ua.indexOf("konqueror/")) );
	else if( MOZ ) ver = ua.substring( ua.indexOf("rv:")+3, ua.indexOf(")", ua.indexOf("rv:")) );
	else if( NS && ua.indexOf("ppc") != -1 ) ver = ua.substring( ua.indexOf("netscape/")+9, ua.length );
	else if( NS ) ver = ua.substring( ua.indexOf("netscape/")+9, ua.indexOf(" (", ua.indexOf("netscape/")) );
	else if( OPERA ) ver = ua.substring( ua.indexOf("opera ")+6, ua.indexOf(" [", ua.indexOf("opera ")) );
	else if( SAFARI ) ver = ua.substring( ua.indexOf("safari/")+7, ua.length );
	this.version = parseFloat( ver );

	if( this.debug ) alert( 'appver = ' + appver + '\nplat = ' + plat + '\nua = ' + ua + '\nver = ' + ver );
	for( var i = 0; i < this.browsarr.length; i++ )
	{
		if( eval(this.browsarr[i][0]) )
		{
			if( this.debug ) alert( this.browsarr[i][2] + ' ' + this.version );
			if( this.version < this.browsarr[i][1] || this.browsarr[i][1] == -1 ) return this.create_redirect_page();
		}
	}
	return;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Will display an error message with the current minimum requirements for the application
function create_redirect_page()
{
	var title = 'Incompatible Browser';
	var acolor = '#004466';
	var ahcolor = '#000000';
	var fontsz = '9pt';
	var fontfam = 'tahoma,arial,helvetica';
	var supported = '\n<p>\n<b><u>Supported Browsers (minimum):</u></b>\n'
	var unsupported = '\n<p>\n<b><u>Unsupported Browsers:</u></b>\n'

	var text = '<html>\n\n<head>\n<title>' + title + '</title>\n<script type="text/javascript">\n<!--\n//--' + '>\n<' + '/script>\n';
	text += '<style>\nbody { font-family:' + fontfam + '; font-size:' + fontsz + '; }\na { color:' + acolor + '; text-decoration:none; }\n';
	text += 'a:hover { color:' + ahcolor + '; text-decoration:underline; }\n</style>\n</head>\n\n<body>\n<div style="width:650px;">\n';
	text += '<table width="100%" border="0">\n\t<tr>\n\t\t<td width="62px"><img src="/common/js/error.gif" style="';
	text += 'display:inline;"></td>\n\t\t<td style="color:#ee0000;font-size:18pt;">This web browser does not meet the<br>minimum ';
	text += 'requirements for this application.</td>\n\t</tr>\n</table>\n<hr style="width:100%; text-align:left;" align="left">\n';
	for( var i = 0; i < this.browsarr.length; i++ )
	{
		if( this.browsarr[i][1] == -1 ) // -1 means NOT SUPPORTED
			unsupported += '<li>' + this.browsarr[i][2] + '</li>\n';
		else
		{
			var sb_version = this.browsarr[i][1].toString();
			var is_version = this.version.toString();
			if( this.browsarr[i][1] > 84 ) sb_version = '1.0'; // this fixes the stupid SAFARI issue
			if( this.version > 84 ) is_version = '1.0';
			if( sb_version.indexOf('.') == -1 ) sb_version += '.0';
			if( is_version.indexOf('.') == -1 ) is_version += '.0';
			var desc = this.browsarr[i][2] + ' ' + sb_version;

			supported += ( this.browsarr[i][3] != '' ) ? '<li><a href="' + this.browsarr[i][3] + '">' + desc + '</a>' : '<li>' + desc;
			if( eval(this.browsarr[i][0]) ) supported += ' <font color="#999999">(current version: ' + is_version + ')</font>';
			supported += '</li>\n';
		}
	}
	text += supported + '</p>\n' + unsupported + '</p>\n\nIf you have one of the supported browsers on your computer, launch it now ';
	text += 'to access this web software.<br>Otherwise follow one of the links above to download the browser of your choice.\n';
	text += '</body>\n\n</html>\n';

	this.display_error_text = text;
	setTimeout( 'show_redirect()', 500 );
	return;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////
// This actually displays the redirection page.
function show_redirect()
{
	document.write( bd.display_error_text );
	document.close();
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////
// displays help for the browser_detect object
function help()
{
	var text = 'browser_detect Help:\n';
	text += '\tCreate new instance: var bd = new browser_detect()\n';
	text += '\tOverride defaults if needed: bd.set_min_version(\'IE\',6.0)\n';
	text += '\tExecute the check for requirements: bd.check_browsers()\n\n';
	text += 'Method List:\n';
	text += '\thelp() :: Displays this help\n';
	text += '\tcheck_browsers() :: Executes check for minimum requirements\n';
	text += '\tget_min_version(BROWSER) :: \n\t  - Get the current required minimum version for BROWSER\n';
	text += '\t  - Where BROWSER is one of the default browser variables.\n\t  - Example: bd.get_min_version(\'IE\');\n';
	text += '\tset_min_version(BROWSER,VERSION) :: \n\t  - Set the current required minimum VERSION for BROWSER\n';
	text += '\t  - Where BROWSER is one of the default browser variables.\n\t  - Example: bd.set_min_version(\'IE\',6.0);\n';
	text += '\tadd_browser(BROWSER,VERSION,DESCRIPTION,URL) :: \n\t  - Adds a new supported browser to the list.\n';
	text += '\t  - BROWSER is one of the default browser variables. (i.e. \'IE\')\n';
	text += '\t  - VERSION is the minimum allowed version. (i.e. 6.0)\n';
	text += '\t  - DESCRIPTION is the display name. (i.e. \'Microsoft Internet Explorer\')\n';
	text += '\t  - URL is download url for getting the browser if needed.\n\n';
	text += 'Default browser variables: \n';
	for( var i = 0; i < this.browsarr.length; i++ ) 
	{
		text += '\t' + this.browsarr[i][0] + ' = ' + this.browsarr[i][2] + ' ' + this.browsarr[i][1];
		text += ( eval(this.browsarr[i][0]) ) ? ' (This Browser)\n' : '\n';
	}
	alert( text );
	return;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////
// This enforces the default settings and updates the js environment with vars ( IE, NS, etc...).
bd = new browser_detect();
bd.check_browsers();
