/**
 * This script contains utilities for a good scripting.
 */

//----------------------------------------------------------------------------------------->

/**
 * The browser object
 */
function Browser()
{
	/**
	 * The navigator
	 */
	this.navigator = navigator;
	
	//-----------------------------
	// Wrap navigator properties
	//-----------------------------

	function _Engine()
	{
		/**
		 * The engine code
		 * (Mozilla, Opera, ...) 
		 */
		this.code = navigator.appCodeName;
			
		/**
		 * The engine version : version + platform + ...
		 */
		this.versionString	= navigator.appVersion;
		this.version 		= parseFloat(this.versionString);

		/**
		 * The name of the client that implements this engine.
		 */
		this.clientName	= navigator.appName;
	}
	
	/**
	 * The engine.
	 */
	this.Engine = new _Engine();
	
	/**
	 * The platform of this browser.
	 */
	this.platform	= navigator.platform;

	/**
	 * The user Agent header that sent is sent by the client to the server.
	 */
	this.userAgent	= navigator.userAgent;

	/**
	 * List of known platforms
	 */
	this.isWin32	= (this.platform.indexOf("Win32") >= 0);
	this.isLinux	= (this.platform.indexOf("Linux") >= 0);
	this.isMac		= (this.platform.indexOf("Mac") >= 0);

	/**
	 * List of known client browsers
	 */
	this.isGecko 	= (this.userAgent.indexOf("Gecko/") >= 0);
	this.isFirefox 	= (this.userAgent.indexOf("Firefox/") >= 0);
	this.isNetscape = (this.userAgent.indexOf("Netscape/") >= 0);
	this.isMSIE		= (this.userAgent.indexOf("MSIE") >= 0);
	this.isSafari	= (this.userAgent.indexOf("Safari/") >= 0);
	this.isOpera	= (this.userAgent.indexOf("Opera") >= 0);
	
	/**
	 * The code of the client browser
	 */
	this.code = this.Engine.code;
	if ( this.isGecko )		this.code = "Gecko";
	if ( this.isFirefox ) 	this.code = "Firefox";
	if ( this.isNetscape ) 	this.code = "Netscape";
	if ( this.isMSIE )		this.code = "MSIE";
	if ( this.isSafari )	this.code = "Safari";
	if ( this.isOpera )		this.code = "Opera";
	
	/**
	 * The name of the client brower
	 */
	this.name = this.Engine.clientName;
	if ( this.isFirefox ) 	this.name = "Mozilla Firefox";
	if ( this.isSafari ) 	this.name = "Safari"
	if ( this.isOpera ) 	this.name = "Opera"

	/**
	 * The version of the browser
	 */
	this.majorVersion 	= 0;
	this.minorVersion 	= 0;
	this.minorVersion2 	= 0;
	this.version		= "" + this.Engine.version;

	//------------------------------------
	// Detect the version of the browser 
	//------------------------------------
	
	var v 	= this.version;
	var ua 	= this.userAgent;
	var k 	= this.code;
	
	if ( this.isGecko || this.isFirefox || this.isNetscape || this.isSafari )
	{
		k += "/";
	}
	
	if ( k != null )
	{
		// extract the version
		v = ua.substring( ua.indexOf(k) + k.length );
		
		// removes inutilse spaces
		v = trim(v);
		this.version = v;

		if ( v.length > 0 )
		{
			var i;
			
			// search the first space of the first ';'
			i = v.indexOf(' ');
			if ( i > 0 )
			{
				v = v.substring(0, i);
			}
			i = v.indexOf(';');
			if ( i > 0 )
			{
				v = v.substring(0, i);
			}
			
			this.version = v;
			
			i = v.indexOf(".");
			if ( i < 0 )
			{
				this.majorVersion = parseInt(v);
			}
			else
			{
				this.majorVersion = parseInt(v.substring(0, i));
				v = v.substring(i + 1);
				
				i = v.indexOf(".");
				if ( i < 0 )
				{
					this.minorVersion = parseInt(v);
				}
				else
				{
					this.minorVersion = parseInt(v.substring(0, i));
					v = v.substring(i + 1);
					this.minorVersion2 = parseInt(v);
				}
			}
		}
	}
}

/**
 * The unique instance of the Browser object.
 */
Browser.Instance = new Browser();

//----------------------------------------------------------------------------------------->

/**
 * Determines whether the given char is a white space : space, tabulation, newline
 */ 
function isWhiteSpace(c)
{
	return (c == ' ' || c == '\r' || c == '\n' || c == '\t');
}

/**
 * Trims the given string : remove white spaces at begin and end of the string.
 */
function trim(s)
{
	if ( typeof s != "string" )
	{
		return s;	
	} 
	
	while ( s.length > 0 && isWhiteSpace(s.charAt(0)) )
	{
		s = s.substring(1);	
	}

	while ( s.length > 0 && isWhiteSpace(s.charAt(s.length - 1)) )
	{
		s = s.substring(0, s.length - 1);
	}

	return s;
}

/**
 * Returns the index of any chars include in the toSearch string into the s string
 */
function indexOfAny(s, toSearch, index) 
{
	if ( typeof index == "undefined" || index == null )
	{
		index = 0;
	}
	
	for ( var i = index; i < s.length ; i++ )
	{
		if( toSearch.indexOf(s.charAt(i)) >= 0 )
		{
			return i;
		}
	}
	
	return -1;
}

//----------------------------------------------------------------------------------------->

/**
 * Returns the string representation of the given array.
 */
function arrayToString(arr, sep, prefix)
{
	if ( typeof arr == "undefined" )
	{
		return;
	}
	
	if ( typeof sep != "undefined" )
	{
		if ( typeof prefix == "undefined" )
		{
			prefix = '';
		}
		
		var s = '';
		for ( var i = 0; i < arr.length; i++ )
		{
			s += (i > 0 ? sep : '') + prefix + arr[i];
		}

		return s;
	}
	else
	{
		return arr;
	}
}

/**
 * Returns the string representation of the given integer 
 * with the given number of digit.
 */
function intToString(v, nbDigit)
{
	var s 	= '' + v;
	if ( v < 0 )
	{
		return s;
	}
	
	var l = nbDigit - s.length;
	if ( l <= 0 )
	{
		return s;
	}
	
	// insert '0'
	for ( var i = 0; i < l; i++, s = '0' + s );

	// return the formated string
	return s;
}

