/**
 * JS-Library
 * Tested with jQuery 1.2.6 & jQuery 1.3.2 ...
 * 
 * Last updated 7/2/2009
 * 
 * @author David Sullivan
 * @version 1.0
 */

/**
 * Creates dummy object for browsers without console.
 */
if (!console || console == null || console == undefined)
{
	function console(){}
	console.log = function(){};
}

/**
 * Decides if Internet Explorer or not.
 * @param {String} versionNum optional version number
 * @return true or false
 */
function isIE(versionNum)
{
	return isBrowser("MSIE", versionNum);
}

/**
 * Decides if Mozilla Firefox or not.
 * @param {String} versionNum optional version number
 * @return true or false
 */
function isFx(versionNum)
{
	return isBrowser("Firefox", versionNum);	
}

/**
 * Decides if user agent (navigator.userAgent) and version matches the given strings.
 * @param {String} idStr identification string, e.g. 'MSIE' or 'Firefox'
 * @param {String} versionNum version number e.g. '3.0'
 */
function isBrowser(idStr, versionNum)
{	
	if (versionNum)
	{
		var separators = new Array();
		separators["Firefox"] = "/";
		separators["MSIE"] = " ";
		
		idStr += (separators[idStr] ? separators[idStr] : "") + versionNum;
	}
		
	if (navigator.userAgent.search(idStr) != -1)	
		return true;
	else
		return false;
		
}

/**
 * Gets the suffix of a filename (or path) without dot.
 * @param {String} filename filename or path
 * @return the suffix string
 */
function getSuffix(filename)
{
	try
	{
		filename = new String(filename);
		return filename.split(".").pop();
	}
	catch (e)
	{
		return "";
	}
}

/**
 * Checks if item is in array.
 * @param {Object} item needle
 * @param {Array} array haystack
 * @return true or false
 */
function inArray(item, array)
{
	try
	{
		for (i = 0; i < array.length; i++)
			if (array[i] == item)
				return true;
				
		return false;
	}
	catch (e)
	{
		return false;
	}
}

/**
 * Checks if the given string matches the email pattern.
 * @param {String} string the potential email string
 * @return true or false
 * @see the PHP version of isCorrectEmail()
 */
function isCorrectEmail(string)
{
	try
	{
		regex = /^[\+a-z0-9]+([_\.-][\+a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\.[a-z]{2,}$/; // regex from PHP isCorrectMail()
		if (string.search(regex) != -1)
			return true;
		else
			return false;
	}
	catch (e)
	{
		return false;
	}
}

/**
 * Removes leading and trailing spaces.
 * @param {String} string the string with spaces
 * @return string without spaces
 */
function trim(string)
{
	try
	{
		string = string.replace(/^ */, "");
		string = string.replace(/ *$/, "");
		return string;
	}
	catch (e)
	{
		return "";
	}
}

function randInt (min, max)
{
	try
	{
		return min + parseInt(Math.random() * max);
	}
	catch (e)
	{
		return 0;
	}
}

function randStr(len, chars, nums){
	try {
		// standard values
		if (len == null || len == undefined) 
			len = 10;
		if (chars == null || chars == undefined) 
			chars = true;
		if (nums == null | nums == undefined) 
			nums = true;
		
		if (!chars && !nums) 
			throw "One of 'chars' or 'nums' must be true";
		if (typeof(len) != "number") 
			throw "'len' must be a number"; // integer actually!
		var str = "";
		if (chars) 
			str += "abcdefghijklmnoprstuvwxyz";
		if (nums) 
			str += "0123456789";
		
		var ret = "";
		for (i = 0; i < len; i++) {
			ret += str.substr(randInt(0, str.length - 1), 1);
		}
		
		return ret;
	} 
	catch (e) {
		return "";
	}
}
