/*
	getBswBrowserInstance() returns the BswBrowser singleton used for all references to it under the same window object.
	This ensures that we are dealing with only one browser object for each unique browser window request.
	The window property associates this singleton to the current window object 
	any subsequent calls to retrieve the singleton will use the existing instance.
*/
function getBswBrowserInstance()
{	
	// check if previous existence exists to return, otherwise create instance to return 
	// (NOTE: use the === operator because the == operator treats the undefined value as equal to null)
	if (window.BswBrowser === undefined) 
	{	
		window.BswBrowser = new Object();
		
		window.BswBrowser.userAgent = navigator.userAgent.toLowerCase();
		window.BswBrowser.majorVersion = parseInt(navigator.appVersion);
		window.BswBrowser.minorVersion = parseFloat(navigator.appVersion);

		/*
			Returns the width of the browser in pixels
		*/
		window.BswBrowser.getBaseX = function()
		{
			var toReturn = null;
			
			if(document.all)
			{
				toReturn = document.body.clientWidth;
			} 
			else 
			{
				toReturn = window.innerWidth;
			}	
			
			return toReturn;
		}

		/*
			Returns the height of the browser in pixels
		*/
		window.BswBrowser.getBaseY = function ()
		{
			var toReturn;
			
			if(document.all)
			{
				toReturn = document.body.clientHeight;
			} 
			else 
			{
				toReturn = window.innerHeight;
			}	
			
			return toReturn;
		}
		
		/*
			Attempts to retrieve the cookie matching the passed in cookie name
		*/
		window.BswBrowser.getCookie = function(name)
		{
			var toReturn = null;
			var ca = document.cookie.split(';');
			var nameEQ = name + "=";
			
			for(var i=0; i < ca.length; i++) 
			{
				var c = ca[i];
				while (c.charAt(0)==' ') c = c.substring(1, c.length); //delete spaces
				if (c.indexOf(nameEQ) == 0) toReturn = c.substring(nameEQ.length, c.length);
			}
			
			return toReturn;
		}	

		/*	
			Returns the value of the passed in query string name
		*/
		window.BswBrowser.getQueryStringValue = function(qsName)
		{
			toReturn = null;
			
			var url = window.location.toString();
			var qsBeginingIndex = url.indexOf("?");
			var qsNames = url.substring(qsBeginingIndex + 1,url.length).split('&');
			
			for(val in qsNames){
				
				var valueBeginningIndex = qsNames[val].indexOf("=");
				var name = qsNames[val].substring(0,valueBeginningIndex);
				var value = qsNames[val].substring(valueBeginningIndex + 1,qsNames[val].length);
				
				if(qsName == name)
				{
					toReturn = value;
				}
			}

			return toReturn;
		}
		
		/*
			This function returns the string value of the XML DOM object supported by the current browser 
		*/
		window.BswBrowser.getXMLRequestObjectType = function() 
		{
			var toReturn = null;
			
			if (window.XMLHttpRequest)
			{
				toReturn = "XMLHttpRequest"; // Mozilla/Safari
			}
			else if (document.implementation && document.implementation.createDocument)
			{
				toReturn = "XmlDocument"; // Mozilla/Safari
			}
			else if (typeof ActiveXObject != "undefined")
			{	
				try
				{
					var msXml = new ActiveXObject('MSXML2.DOMDocument.3.0'); // newer IE
					toReturn = "ActiveX DOMDocument";
				}
				catch (e)
				{	
					var msXml = new ActiveXObject('Msxml.DOMDocument'); // older IE
					toReturn = "ActiveX DOMDocument";
				}
			}
			
			return toReturn;
		}

		/*
			This function returns the string value of the XSL Processor object supported by the current browser 
		*/
		window.BswBrowser.getXSLTProcessorType = function() 
		{
			var toReturn = null;
			
			if (window.ActiveXObject)
			{
				toReturn = "ActiveX XSLTemplate";
			}
			else
			{
				try
				{
					var mozzilaXSLT = new XSLTProcessor();
					toReturn = "XSLTProcessor";
				}
				catch (e)
				{	
					toReturn = "Unsupported";
				}
			}
			
			return toReturn;
		}

		/*
			Creates a cookie with the passed in values
		*/
		window.BswBrowser.setCookie = function(name,value,expires)
		{		
			if (navigator.cookieEnabled) 
			{
				document.cookie = name + "=" + value + expires + "; path=/";
			} 
			else 
			{
				alert("Cookies is currently not enabled. Please enable cookies for your browser before proceeding.");
			}
		}			
		
		/*
			Hides default (Object.prototype.toString()) method with a more useful implemetnation - returns user agent string.
		*/
		window.BswBrowser.toString = function()
		{
			return BswBrowser.userAgent;
		}
	}
	
	return window.BswBrowser;
}
