/** Collection of global js functions */

function hexStr2dec(hexstr)
{
	var num = parseInt(hexstr + '', 16);
	
	return num;
}

function dec2hexStr(num, width)
{
	if(!width || width < 1)
		width = 1;
	var hexstr = '';
	
	// actual conversion
	hexstr+= num.toString(16);
	hexstr = hexstr.toUpperCase();
	
	while(hexstr.length < width)
		hexstr = '0' + hexstr;
	return hexstr;
}

/** Get all parentElements with a given className
 * 
 * @param DOMElement	element		Base element: all of its parents (with the given className) will be matched
 * @param string		className	Classname to search for
 * 
 * @reutn array			parents		Array with all parents wit the given className
 */
function getParentElementsByClassName(element, name)
{
	var parents = [];
	while(element.parentNode)
	{
		if(element == element.parentNode)
			break;
		element = element.parentNode;
		if(element.className)
		{
			if(element.className.indexOf(name) > -1)
				parents.push(element);
		}
	}
	return parents;
}

/** Get all parentElements with a given nodeName
 * 
 * @param DOMElement	element		Base element: all of its parents (with the given nodeName) will be matched
 * @param string		className	Classname to search for
 * 
 * @reutn array			parents		Array with all parents wit the given nodeName
 */
function getParentElementsByTagName(element, name)
{
	var parents = [];
	name = name.toUpperCase();
	while(element.parentNode)
	{
		if(element == element.parentNode)
			break;
		element = element.parentNode;
		if(element.nodeName)
		{
			if(element.nodeName.toUpperCase() == name)
				parents.push(element);
		}
	}
	return parents;
}


/* Merge two objects.
The first specified object gets updated with the values of obj2
*/
function mergeObject(obj1, obj2)
{
	if(obj1 == undefined)
		obj1 = {};
	for (var p in obj2)
	{
		if (obj2[p].constructor==Object) 					// If the property is an object, recurse
		{
			obj1[p] = mergeObject(obj1[p], obj2[p]);

		}
		else											// Change the property to the value of obj2
		{
			obj1[p] = obj2[p];
		}
	}

  return obj1;
}

/* Access an object by numeric index
*/
function getObjectItemByIndex(object, index)
{
	var name = getObjectItemNameByIndex(object, index);
	if(name !== null) return object[name];
	return null;
}

function setObjectItemByIndex(object, index, value)
{
	var name = getObjectItemNameByIndex(object, index);
	if(name !== null)
	{
		object[name] = value;
		return object;
	}
	return null;
}

function getObjectItemNameByIndex(object, index)
{
	var i=0;
	for(var itm in object)
	{
		if(i == index) return itm;
		i++;
	}
	return null;
}

/* Test if an object is empty (~ == {})
*/
function isEmptyObject(o)
{
	for(var p in o)
	{
		if(o.hasOwnProperty(p))
			return false;
	}
	return true;
}
/* Remove item(s) from an array
 * 
 * @param array		The array that will have items removed
 * @param start		Start index: negative means offset from end
 * @param end		End index: negative means offset from end
 */
function array_remove(array, start, end)
{
	var r = array.slice((start || end) + 1 || array.length);
	array.length = start < 0 ? array.length + start : start;
	return array.push.apply(array, r);
};

/* Refresh the current page
 * 
 * @return void
 */
function refreshPage()
{
	try
	{
		history.go(0);
	}
	catch(e)
	{
		try
		{
			window.location.reload();
		}
		catch(e)
		{
			window.location.href=window.location.href;
		}
	}
}


function isArray(obj)
{
    return Object.prototype.toString.call(obj) === '[object Array]';
}


/** Array.unique() : remove double entries
 * 
 * @return (Array)		Array without double entries
 */
Array.prototype.unique = function()
{
	var r = new Array();
	o:for(var i = 0, n = this.length; i < n; i++)
	{
		for(var x = 0, y = r.length; x < y; x++)
		{
			if(r[x]==this[i])
				continue o;
		}
		r[r.length] = this[i];
	}
	return r;
};

/* IE hacks & fixes */

// array.indexOf
[].indexOf || (Array.prototype.indexOf = function(v,n)
	{
		n = (n==null)?0:n; var m = this.length;
		for(var i = n; i < m; i++)
			if(this[i] == v)
				return i;
		return -1;
	});

String.trim || (String.prototype.trim = function() 
{
 return this.replace(/^\s+|\s+$/g, "");
});
