
function UTIL_LIB()
{
	this.CURRENT_BROWSER = false;
	this.CURRENT_PLATFORM = false;

	// Get the current mouse position; return integer
	this.getMouseLeft = function(oEvent)
	{
		oEvent = this.getEvent(oEvent);

		var left = 0;
		
		if(oEvent.pageX)
		{
			left = oEvent.pageX;
		}
		else
		{
			left = oEvent.clientX;
			
			if(document.documentElement.scrollLeft)
			{
				left += document.documentElement.scrollLeft;
			}
			else if(document.body.scrollLeft)
			{
				left += document.body.scrollLeft;
			}
		}
			
		return left;
	}

	// Get the current mouse position; return integer
	this.getMouseTop = function(oEvent)
	{
		oEvent = this.getEvent(oEvent);

		var top = 0;
		
		if(oEvent.pageY)
		{
			top = oEvent.pageY;
		}
		else
		{
			top = oEvent.clientY;
			
			if(document.documentElement.scrollTop)
			{
				top += document.documentElement.scrollTop;
			}
			else if(document.body.scrollTop)
			{
				top += document.body.scrollTop;
			}
		}
			
		return top;
	}

	// Get the element; return object or null
	this.getElement = function(sElementId)
	{
		return document.getElementById(sElementId);
	}

	this.getEvent = function(oEvent)
	{
		if(window.event) // IE
		{
			return window.event;
		}

		return oEvent;
	}

	// Get absolute element left; return integer
	this.getAbsoluteElementLeft = function(oElement)
	{
		var left = 0;

		if(oElement.offsetParent)
		{
			while(oElement.offsetParent) 
			{
				left += oElement.offsetLeft;
				oElement = oElement.offsetParent;
			}
		}
		else if(oElement.x)
		{
			left += oElement.x;
		}

		return left;
	}

	// Get absolute element top; return integer
	this.getAbsoluteElementTop = function(oElement)
	{
		var top = 0;

		if(oElement.offsetParent)
		{
			while(oElement.offsetParent)
			{
				top += oElement.offsetTop;
				oElement = oElement.offsetParent;
			}
		}
		else if(oElement.y)
		{
			top += oElement.y;
		}

		return top;
	}

	// Get element left; return integer
	this.getElementLeft = function(oElement)
	{
		return oElement.offsetLeft;
	}

	// Get absolute element top; return integer
	this.getElementTop = function(oElement)
	{
		return oElement.offsetTop;
	}

	// Get element width; return integer
	this.getElementWidth = function(oElement)
	{
		return oElement.offsetWidth;
	}

	// Get element height; return integer
	this.getElementHeight = function(oElement)
	{
		return oElement.offsetHeight;
	}

	// Get screen resolution width; return integer
	this.getScreenWidth = function()
	{
		return 0;
	}

	// Get screen resolution height; return integer
	this.getScreenHeight = function()
	{
		return 0;
	}

	// Get browser body width; return integer
	this.getBodyWidth = function()
	{
		var iWidth = 0;

		if(window.innerWidth)
		{
			iWidth = window.innerWidth;
		}
		else if(document.documentElement && document.documentElement.clientWidth)
		{
			iWidth = document.documentElement.clientWidth;
		}
		else if(document.body && document.body.clientWidth) 
		{
			iWidth = document.body.clientWidth;
		}

		return iWidth;
	}

	// Get browser body height; return integer
	this.getBodyHeight = function()
	{
		var iHeight = 0;

		if(window.innerHeight)
		{
			iHeight = window.innerHeight;
		}
		else if(document.documentElement && document.documentElement.clientHeight)
		{
			iHeight = document.documentElement.clientHeight;
		}
		else if(document.body && document.body.clientHeight)
		{
			iHeight = document.body.clientHeight;
		}

		return iHeight;
	}

	// Set the status text in the browser
	this.setStatusText = function(sText)
	{
		window.status = sText;
	}

	// Submit a form by NAME or ID
	this.submitForm = function(sFormName)
	{
		var aElements = document.getElementsByTagName('form');

		for(var i = 0; i < aElements.length; i++)
		{
			if((aElements[i].id == sFormName) || (aElements[i].name == sFormName))
			{
				aElements[i].submit();
			}
		}
	}

	// Redirect
	this.redirect = function(sUrl)
	{
		document.location.href = sUrl;
	}

	// Returns IE, FF, OP, NN, SF or false
	this.getBrowser = function()
	{
		if(this.CURRENT_BROWSER)
		{
			return this.CURRENT_BROWSER;
		}
		else
		{
			var browser = 'Unknown browser';

			if(navigator.userAgent.indexOf('OmniWeb') != -1)
			{
				browser = 'OmniWeb';
			}
			else if(navigator.vendor.indexOf('Apple') != -1)
			{
				browser = 'Safari';
			}
			else if(navigator.opera)
			{
				browser = 'Opera';
			}
			else if(navigator.vendor.indexOf('iCab') != -1)
			{
				browser = 'iCab';
			}
			else if(navigator.vendor.indexOf('KDE') != -1)
			{
				browser = 'Konqueror';
			}
			else if(navigator.userAgent.indexOf('Firefox') != -1)
			{
				browser = 'Firefox';
			}
			else if(navigator.vendor.indexOf('Camino') != -1)
			{
				browser = 'Camino';
			}
			else if(navigator.userAgent.indexOf('Netscape') != -1)
			{
				browser = 'Netscape Navigator';
			}
			else if(navigator.userAgent.indexOf('MSIE') != -1)
			{
				browser = 'Internet Explorer';
			}
			else if(navigator.userAgent.indexOf('Gecko') != -1)
			{
				browser = 'Mozilla';
			}
			else if(navigator.userAgent.indexOf('Mozilla') != -1)
			{
				browser = 'Netscape Navigator';
			}

			this.CURRENT_BROWSER = browser;
		}
	}

	this.getPlatform = function()
	{
		if(this.CURRENT_PLATFORM)
		{
			return this.CURRENT_PLATFORM;
		}
		else
		{
			var platform = 'Unknown platform';

			if(navigator.platform.indexOf('Win') != -1)
			{
				platform = 'Windows';
			}
			else if(navigator.platform.indexOf('Mac') != -1)
			{
				platform = 'Mac';
			}
			else if(navigator.platform.indexOf('Linux') != -1)
			{
				platform = 'Linux';
			}

			this.CURRENT_PLATFORM = platform;
		}
	}

	// Get properties of array or object
	this.getProperties = function(oVar)
	{
		var properties = new Array();

		for(p in oVar)
		{
			properties[properties.length] = p;
		}

		return properties.sort();
	}

	this.popup = function(f_url, f_width, f_height, f_name)
	{
		if(this.popup.arguments.length < 4)
		{
			f_name = this.randomCode(64, false, true);
		}

		var oWindow = window.open(f_url, f_name, 'directories=no,height=' + f_height + ',location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no,width=' + f_width);
		oWindow.focus();

		return oWindow;
	}


	// Create a random code with N digits.
	this.randomCode = function(f_length, f_userfriendly, f_hex)
	{
		if(this.randomCode.arguments.length < 3)
		{
			f_hex = false;
		}

		if(this.randomCode.arguments.length < 2)
		{
			f_userfriendly = false;
		}

		if(this.randomCode.arguments.length < 1)
		{
			f_length = 64;
		}

		if(f_hex)
		{
			var chars = new Array('A', 'B', 'C', 'D', 'E', 'F', '2', '3', '4', '5', '6', '7', '8', '9');
		}
		else if(f_userfriendly)
		{
			/*
				Some fonts use (almost) the same symbols for 'i', 'l' and 'one', same goes for 'o' and 'zero'.
				That's why we removed these characters from the characterlist (UPPERCASE + lowercase).
			*/
			var chars = new Array('a', 'b', 'c', 'd', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '2', '3', '4', '5', '6', '7', '8', '9');
		}
		else
		{
			var chars = new Array('a', 'b', 'c', 'd', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
		}

		var s = '';

		for(var i = 0; i < f_length; i++)
		{
			s += chars[Math.round(Math.random() * (chars.length - 1))];
		}

		return s;
	}

	// Preload given images, submit like: "'url', 'url', new array('url', [image_width], [image_height]), 'url', .."
	this.preloadImages = function()
	{
		var __preloadImages = new Array();

		if(document.images)
		{
			for(var i = 0; i < this.preloadImages.arguments.length; i++)
			{
				var x = this.preloadImages.arguments[i];

				if(typeof(x) == 'string')
				{
					__preloadImages[i] = new Image();
					__preloadImages[i].src = this.preloadImages.arguments[i];
				}
				else if(x instanceof Array)
				{
					__preloadImages[i] = new Image(x[1], x[2]);
					__preloadImages[i].src = x[0];
				}
				else
				{
					// Ignore
				}
			}
		}
	}


	this.showRows = function()
	{
		for(var i = 0; i < this.showRows.arguments.length; i++)
		{
			try
			{
				document.getElementById(this.showRows.arguments[i]).style.display = 'table-row';
			}
			catch(e1)
			{
				try
				{
					document.getElementById(this.showRows.arguments[i]).style.display = 'block';
				}
				catch(e2)
				{
					// alert('Cannot show row using id: ' + this.showRows.arguments[i] + '.');
				}
			}
		}
	}

	this.hideRows = function()
	{
		for(var i = 0; i < this.hideRows.arguments.length; i++)
		{
			try
			{
				document.getElementById(this.hideRows.arguments[i]).style.display = 'none';
			}
			catch(e1)
			{
				// alert('Cannot hide row using id: ' + this.hideRows.arguments[i] + '.');
			}
		}
	}
}

var UTIL = new UTIL_LIB();