/*
 * Javascript Utilities
 */ 
 
// short function for getElementByID, but also implemented by Prototype so we use try/catch to identify if the function doesn't exist yet
try { $(''); }  catch (e) {
	function $(id)
	{
		return document.getElementById(id);
	}
}

Utils = {
	// trim string
	trim: function (str)
	{
		return str.replace(/^\s+|\s+$/,'');
	},
	
	// check if value is a number
	isInteger: function (val)
	{
		return (val.toString().search(/^-?[0-9]+$/) == 0);
	},
	
	// check if array a is the same as array b
	compareArray: function (a, b)
	{
		if (a.length != b.length)
			return true;
		if ((a.length == 0) && (b.length == 0))
			return false;
		for(i in a)
		{
			if ((typeof(a[i]) != typeof(b[i])) || (a[i] != b[i]))
				return true;
		}
		return false;
	},
	
	// check if a value is in the array
	inArray: function (needle, haystack)
	{
		for (i in haystack)
			if (haystack[i] == needle)
				return true;
		return false;
	},

	browserDetect: function()
	{
		var ua = navigator.userAgent.toLowerCase();
		var isStrict = document.compatMode == "CSS1Compat";
		 
		var isOpera = (ua.indexOf("opera") > -1);
		var isSafari = (/webkit|khtml/).test(ua);
		var isIE = (!isOpera && ua.indexOf("msie") > -1);
		var isIE7 = (!isOpera && ua.indexOf("msie 7") > -1);
		var isGecko = (!isSafari && ua.indexOf("gecko") > -1);
		
		if (isOpera) return "Opera";
		if (isSafari) return "Safari";
		if (isIE) return "IE";
		if (isIE7) return "IE7";
		if (isGecko) return "Gecko";
		return "other";
	},
	
	// if type is undefined this function toggles the display property.
	toggle: function (id, type)
	{
		var style = $(id).style;
		if (type == "visibility")
			style.visibility = style.visibility == "hidden" ? "visible" : "hidden";
		else
			style.display = style.display == "none" ? "block" : "none";
	}
}
