/**
 * @file	:	functions.js
 * @desc	:	basic function definitions
 * @lmod	:	26.03.2006
 */

/**
 * @desc						:	get object (work with all browsers)
 * @param	id		:	string	:	object name
 * @param	node	:	object	:	node to search the object (def: document)
 * @return			:	object	:	needed object
 */
function getObj(id, node)
{
	if (typeof(id) == 'object')
	{
		return id;
	}
	
	if (!node)
	{
		node = document;
	}

	return node.getElementById(id);
}

/**
 * @desc					:	goto to url
 * @param	url	:	string	:	url to 'jump'
 * @param	obj	:	object	:	select object to recover id
 * @return		:	boolean	:	true ( for the sport :)
 */
function jumpUrl(url, obj)
{
	if (obj)
		var id = obj.options[obj.selectedIndex].value;
		
	window.location.href = url+id;
	return true;
}

/**
 * @desc							:	confirm some link
 * @param	confirmMsg	:	string	:	confirm message
 * @return				:	boolean	:	is confirmed ?
 */
function confirmLink(confirmMsg)
{
    var is_confirmed = confirm(confirmMsg);
    return is_confirmed;
}

/**
 * @desc						:	get elements by attribute value
 * @param	node	:	object	:	groups of elements
 * @param	tag		:	string	:	tag name
 * @param	att		:	string	:	attribute name
 * @param	val		:	string	:	attribute value
 * @return			:	object	:	elemets
 */
function getElementsByAttributeValue(node, tag, att, val) 
{
	var indexes  = node.getElementsByTagName(tag);
	var elements =[];
	
	for (i=0; i<indexes.length; i++) 
	{
		if (val)
		{
			if (indexes[i].getAttribute(att) == val) elements[elements.length]=indexes[i];
		}
		else
		{
			if (indexes[i].getAttribute(att) != null) elements[elements.length]=indexes[i];
		}
	}
	
	return (elements.length>0) ? elements : null ;
}

/**
 * @desc					:	hide object
 *
 */
function hide()
{
	for (i=0; i<arguments.length; i++)
	{
		getObj(arguments[i]).style.display = 'none';
	}
}

/**
 * @desc					:	show object
 *
 */
function show(id)
{
	for (i=0; i<arguments.length; i++)
	{
		getObj(arguments[i]).style.display = '';
	}
}

/**
 * @desc					:	show / hide object
 * @param	id	:	string	:	object id
 *
 */
function show_hide(id)
{
	var object = getObj(id)
	
	if (object.style.display == 'none')
	{
		object.style.display = 'block';
	}
	else 
	{
		object.style.display = 'none';
	}
}

/**
 * @desc							:	check or uncheck all checkboxes
 * @param	formid		:	object	:	checkboxes form
 * @param	checked		:	boolean	:	checked status
 */
function selectAll(formid, checked)
{
	var chboxes = getElementsByAttributeValue(formid, 'input', 'type', 'checkbox');
	
	if (chboxes == null) return true;
	
	for (i=0; i<chboxes.length; i++)
	{
		chboxes[i].checked = checked;
	}
	
	return true;
}

/**
 * @desc						:	replace <, >, \n
 * @param	text	:	string	:	not formated string
 * @return	text	:	string	:	formated string
 */
function htmlspecialchars(text)
{
	text = text.replace(/(<)/gi, '&lt;');
	text = text.replace(/(>)/gi, '&gt;');
	text = text.replace(/([\n])/gi, "\n <br />");
	
	return text;
}

/**
 * @desc							:	add window.onload event
 * @param	func	:	function	:	function to call
 * 
 */
function addLoadEvent(func)
{
	if (typeof window.onload == 'function')
	{
		var oldonload = window.onload;
		
		window.onload = function()
		{
			oldonload();
			func();
		}
	}
	else
	{
		window.onload = func;
	}
}

/**
 * @desc					:	include javascript and css files
 * @param	src	:	string	:	javascript file to include
 * 
 */
function include(src)
{
	if (src.match(/\.(js)$/i))
	{
		var elem = document.createElement('script');
		elem.setAttribute('type', 'text/javascript');
		elem.setAttribute('src', src);
	}
	else if (src.match(/\.(css)$/i))
	{
		var elem = document.createElement('link');
		elem.setAttribute('href', src);
		elem.setAttribute('rel', 'stylesheet');
		elem.setAttribute('type', 'text/css');
	}
	else
	{
		return;
	}
	
	document.getElementsByTagName('head')[0].appendChild(elem);
}


/**
 * @desc						:	Bind all parent class functions to 'this' of the function in context
 * @credits 					:	Courtesy of Prototype.js (http://prototype.conio.net)
 * @param	object	:	object	:
 */
Function.prototype.bind = function(object) 
{
	var __method = this;
	return function() {
		__method.apply(object, arguments);
	}
}

/**
 * @desc	:	clear array from its values
 *
 */
Array.prototype.clear = function()
{
	this.length = 0;
	return this;
}

/**
 * @desc	:	get first array element
 * @return	:	0 element of an array
 *
 */
Array.prototype.first = function()
{
	return this[0];
}


/**
 * @desc	:	get last element of an array
 * @return	:	last element
 */
Array.prototype.last = function ()
{
	return this[this.length - 1];
}


/**
 * @desc		:	return the index of given object
 * @param	obj	:	object to look for
 * @return		:	the index (if some)
 */
Array.prototype.indexOf = function(obj)
{
	for (i=0; i < this.length; i++)
	{
      if (this[i] == object) return i;
	}
	 
    return -1;
}

/**
 * @desc							:	is some element parent of another
 * @param	container	:	object	:	parent element
 * @param	containee	:	object	:	(possible) sub element
 * @return	isParent	:	boolean	:	is container parent of containee
 * @credits							:	digg.com
 */
function containsDOM (container, containee)
{
	if (document.all)
	{
		return container.contains(containee);
	}
	
	var isParent = false;
	do
	{
		if ((isParent = container == containee)) break;

		containee = containee.parentNode;
	} 
	while (containee != null);
	
	return isParent;
}


//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll()
{
	var yScroll;

	if (self.pageYOffset)
	{
		yScroll = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
	{
		// Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	}
	else if (document.body)
	{
		// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize()
{	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY)
	{	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	}
	else if (document.body.scrollHeight > document.body.offsetHeight)
	{
		// all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	}
	else
	{
		// Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight)
	{
		// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
	{
		// Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	}
	else if (document.body)
	{
		// other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight)
	{
		pageHeight = windowHeight;
	}
	else
	{ 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth)
	{	
		pageWidth = windowWidth;
	}
	else
	{
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}
