/*-----------------------------------------------------------------------*/
// JS Utilities that need not be replicated in pages.
//	usage: Place the following in the	HTML page header:
//
//		<script type='text/javascript' src='/bdetect.js'></script>
/*-----------------------------------------------------------------------*/

/* Begin Main() */
/* Instantiate 'trim' functions as methods of the string object */
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
	}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
	}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
	}

/* End of Main() */

/*-----------------------------------------------------------------------*/
function Trim(
	inString
	) {
// Trim spaces from both ends of a string.  For use use if JS ver < 1.2 and
//	situations where the target is not a string object.
/*--------------------------------^--------------------------------------*/
	var start = 0;
	while ( ( start < inString.length ) && ( inString.charAt( start ) == ' ' ) ) {
		++start;
	} /* while */

	var end = inString.length;
	while ( ( end > 0) && ( inString.charAt(end - 1 ) == ' ' ) ) {
		--end;
	} /* while */
	return inString.substring( start, end );
	} /* Trim() */

/*-----------------------------------------------------------------------*/
function BrowserDetect(
	ua
	) {
// Determine characteristics of user's browser; parses User-Agent string.
//	into useful info.
//
//	Usage: var bd = new BrowserDetect( navigator.userAgent );
//
//	Source: Originally from the Webmonkey Code Library, since heavily modified:
//					(http://www.hotwired.com/webmonkey/javascript/code_library/), with
//					Original author of this routine assumed no browser info followed OS info
//					but that is incorrect.
//
// Revision History:
//   __-___-____.BMJ.ITE: Installation on site
//   17-Mar-____.BMJ.ITE: Property added to 'bd' object to store availability and version of Javascript,
//   16-Feb-2011:BMJ.ITE: Revised to support useragent string w/indeterminate OS info location.  Added
//                        Chrome and Safari as detected browser types.
//
/*--------------------------------^--------------------------------------*/
	this.browser = 'Unknown';				// Initialize defaults
	this.platform = 'Unknown';
	this.majVer = '';
	this.minVer = '';
	this.JSVersion = 0;							// Will be filled in by mainline that called us

	/* Excise the OS info, leaving all browser info (save IE ) in ua. */
	var infoOS = '';
	i = ua.indexOf( '(' );					// OS info will be found w/in first set of parens.
	j = ua.indexOf( ')' );
	if ( i ) {
		infoOS = ua.slice( i + 1, j > 0 ? j : us.length );
		ua = ua.substr( 0, i - 1 ) + ua.substr( j + 1 );
		} /* if */

	/* First check for browser and version in infoOS - IE seems to be only stupid agent that does this, but don't
		know what the standard format is, if there is such.  Override later if we find a recognized agent signature
		further to right in the ua string. */
	this.browser = 'Mozilla';				// Default, same as 'compatible'
	var tokens = infoOS.split( ";" );
	var token = '';
	for ( i = 0; i < tokens.length; i++ ) {
		token = Trim( tokens[ i ] );
		/* compatible - might want to reset from Netscape */
		if ( token == "compatible" ) {
			/* One might want to reset browVer to a null string here, but instead, we'll assume that
				if we don't find out otherwise, then it really is Mozilla (or whatever showed up before
				the parens).	browser - try for Opera, IE, Apple Safari, or Google Chrome */
			} /* If */
		else if ( token.indexOf( 'MSIE' ) >= 0 ) {	// IE is the only one known to be found here.
			this.browser = "IE";
			browVer = token.substr( token.indexOf( ' ' ) + 1 );
			} /* else */
		/* Now check for platform - try for X11, SunOS, Win, Mac, PPC, OS/2 */
		else if ( ( token.indexOf( 'X11' ) >= 0 ) || ( token.indexOf( 'SunOS' ) >= 0 ) || ( token.indexOf( 'Linux' ) >= 0 ) )
			this.platform = "Unix";
		else if ( token.indexOf( 'Win' ) >= 0 )
			this.platform = token;
		else if ( ( token.indexOf( 'Mac' ) >= 0 ) || ( token.indexOf( 'PPC' ) >= 0 ) )
			this.platform = token;
		else if ( token.indexOf( 'OS/2' ) >= 0 )
			this.platform = token;
		} /* for */

	/* Now search the ua string for non-IE agent info (Mozilla remains the default, from above) */
	var tokens = ua.split( ' ' );
	var token = '';
	/* Go through tokens searching for a recognized signature.  Proceed from last token toward first so that the
		browsers that try to disguise themselves as more popular variantes are less successful in that endeavor. */
	for ( i = tokens.length - 1; 0, i--; ) {
		token = Trim( tokens[ i ] );	// Just in case
		if ( ( j = token.indexOf( 'Mozilla' ), j ) >= 0 ) {
			this.browser = 'Mozilla';
			browVer = token.substr( j + 8 );
			break;
			} /* else */
		else if ( ( j = token.indexOf( 'Chrome' ), j ) >= 0 ) {
			this.browser = 'Chrome';
			browVer = token.substr( j + 7 );
			break;
			} /* else */
		else if ( ( j = token.indexOf( 'Safari' ), j ) >= 0 ) {
			this.browser = 'Safari';
			browVer = token.substr( j + 7 );
			break;
			} /* else */
		else if ( ( j = token.indexOf( 'Opera' ), j ) >= 0 ) {
			this.browser = 'Opera';
			browVer = token.substr( j + 6 );
			break;
			} /* else */
		else if ( ( j = token.indexOf( 'Microsoft Internet Explorer' ), j ) >= 0 ) {
			this.browser = "IE"
			browVer = token.substr( j + 28 );
			break;
			} /* else */
		else if ( ( j = token.indexOf( 'Lynx' ), j ) >=0 ) {
			this.browser = 'Lynx';
			browVer = token.substr( j + 5 );
			break;
			} /* else */
		else if ( ( j = token.indexOf( 'Netscape' ), j ) >= 0 ) {
			this.browser = 'NS';
			browVer = token.substr( j + 9 );
			break;
			} /* else */
		} /* if */

	// Try to separate out major & minor version info
	browVer.trim;
	i = browVer.indexOf( "." );
	if ( i ) {
		this.majVer = browVer.substring( 0, i );
		this.minVer = browVer.substring( i + 1, browVer.length );
		} /* if */
	else
		this.majVer = browVer;
	} /* BrowserDetect() */

/* End bdetect.js */

