// browsers.js - browser-specific stuff
// copyright © 2006 WebAware Pty Ltd
//---------------------------------------------------------------------
// members:
// user_agent		same as navigator.user_agent
// browser			name of browser (Netscape, Firefox, MSIE)
// version			major version of browser (4, 5, 6, 7, ...)
// revision			revision of browser, 
//---------------------------------------------------------------------
// functions:
// GetBrowserVersion	detect browser, version, revision
// DebugPrint			dump details of the object to output
// IsMSIE				returns true for Microsoft Internet Explorer
// IsNetscape			returns true for Netscape
// IsFirefox			returns true for Firefox
// IsSafari				returns true for Safari
// IsMozilla			returns true for Netscape, Firefox, Safari
// LinkStylesheet		create a style sheet link to a given url
//---------------------------------------------------------------------

// constructor - make a new browser tools object
function BrowserTools()
{
	// add member data
	this.user_agent = navigator.userAgent;
	this.browser = "Unknown";
	this.version = 0;
	this.revision = 0;
	
	//---------------------------------------------------------------------
	// GetBrowserVersion()
	// find type and version of browser 
	//---------------------------------------------------------------------
	// Internet Expl:	Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)
	// Netscape to 4.x:	Mozilla/4.61 [en] (WinNT; I)
	// Netscape 6.x:	Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:0.9.4.1) Gecko/20020508 Netscape6/6.2.3 ...
	// Netscape 7.x:	Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.0.1) Gecko/20020823 Netscape/7.0 ...
	// Firefox 1.5:		Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1
	//----------------------------------------------------------------------
	this.GetBrowserVersion = function()
	{
		var i, j, s;
		
		this.browser = "Unknown";
		this.version = 0;
		this.revision = 0;
		
		// check for Netscape (or Mozilla, seen as Netscape)
		if (this.user_agent.indexOf("Mozilla") >= 0 && this.user_agent.indexOf("compatible") == -1)
		{
			if (this.user_agent.indexOf("Firefox") == -1)
				this.browser = "Netscape";
			else
				this.browser = "Firefox";
	
			i = this.user_agent.indexOf(this.browser);
			if (i >= 0)
			{
				this.version = 6;
				i = this.user_agent.indexOf("/", i);
				j = this.user_agent.indexOf(".", i);
				if (i >= 0 && j > i)
				{
					this.version = parseInt(this.user_agent.substring(i + 1, j));
					i = j + 1;
					j = this.user_agent.indexOf(" ", i);
					if (j <= 0)
							j = this.user_agent.length - 1;
					if (this.browser == "Netscape")
						this.revision = parseInt(parseFloat(this.user_agent.substring(i, j + 1)) * 10);
					else
					{
						s = this.user_agent.substring(i, j + 1).split(".");
						this.revision = parseInt(s[0]) * 1000 + parseInt(s[1]) * 100 + parseInt(s[2]);
					}
				}
			}
			else
			{
				i = this.user_agent.indexOf("/");
				j = this.user_agent.indexOf(".", i);
				if (i >= 0 && j > i)
				{
					this.version = parseInt(this.user_agent.substring(i + 1, j - i));
					i = j + 1;
					j = this.user_agent.indexOf(" ", i);
					if (j >= 0)
						this.revision = parseInt(this.user_agent.substring(i, j - i));
				}
			}
		}
		else if (this.user_agent.indexOf("MSIE") >= 0)
		{
			this.browser = "MSIE";
			i = this.user_agent.indexOf("MSIE");
			j = this.user_agent.indexOf(".", i);
			if (i >= 0 && j > i)
			{
				this.version = parseInt(this.user_agent.substring(i + 5, j));
				i = j + 1;
				j = this.user_agent.indexOf(";", i);
				if (j >= 0)
					this.revision = parseInt(this.user_agent.substring(i, j));
			}
		}
	}

	//---------------------------------------------------------------------
	// DebugPrint()
	// dump basic browser facts to the current page
	//---------------------------------------------------------------------
	this.DebugPrint = function()
	{
		document.write("<p>user_agent = " + this.user_agent + "</p>\n");
		document.write("<p>browser = " + this.browser + "</p>\n");
		document.write("<p>version = " + this.version + "</p>\n");
		document.write("<p>revision = " + this.revision + "</p>\n");
	}

	//---------------------------------------------------------------------
	// LinkStylesheet()
	// create a style sheet link to the given url
	//---------------------------------------------------------------------
	this.LinkStylesheet = function(url)
	{
		document.write("<link rel='stylesheet' type='text/css' href='" + url + "'>\n");
	}

	//---------------------------------------------------------------------
	// IsMSIE()
	// returns true if a Microsoft Internet Explorer browser, else false
	//---------------------------------------------------------------------
	this.IsMSIE = function()
	{
		return this.user_agent.indexOf("MSIE") >= 0;
	}

	//---------------------------------------------------------------------
	// IsNetscape()
	// returns true if a Netscape browser, else false
	//---------------------------------------------------------------------
	this.IsNetscape = function()
	{
		return this.user_agent.indexOf("Netscape") >= 0;
	}

	//---------------------------------------------------------------------
	// IsFirefox()
	// returns true if a Firefox browser, else false
	//---------------------------------------------------------------------
	this.IsFirefox = function()
	{
		return this.user_agent.indexOf("Firefox") >= 0;
	}

	//---------------------------------------------------------------------
	// IsSafari()
	// returns true if a Safari browser, else false
	//---------------------------------------------------------------------
	this.IsSafari = function()
	{
		return this.user_agent.indexOf("Safari") >= 0;
	}

	//---------------------------------------------------------------------
	// IsMozilla()
	// returns true if a Mozilla browser, else false
	//---------------------------------------------------------------------
	this.IsMozilla = function()
	{
		return this.IsNetscape() || this.IsFirefox() || this.IsSafari();
	}
	
	// detect browser version
	this.GetBrowserVersion();
}
