// Highlight element
function highlight(ic, bool, color)
{
	color = either_or(color, '#fffccc');
	
	$func(ic, function (item) {
		item.style.background = (bool) ? color : '';
	});
}

// Toggle class
function toggle_class(class_name)
{
	var items = $class(class_name);
	
	items.each(function (item) {
		Element.toggle(item);
	});
}

// Returns the really simple data (rsdata) id
function rsdata_id(elem)
{
	return elem.title;
}

// Returns the really simple data (rsdata) value
function rsdata_value(elem)
{
	return elem.innerHTML.unescapeHTML();
}

// Returns the first defined value
function either_or()
{
	for (var i=0; i<arguments.length; i++)
	{
		if (typeof arguments[i] != 'undefined')
		{	
			return arguments[i];
		}
	}
}

// Test if element is present
function isset(id)
{
	return document.getElementById(id);
}

// Get elements by class
function $class(class_name)
{
	return document.getElementsByClassName(class_name);
}

// Executes a function on elements with matching id or class
function $func(ic, func)
{
	// Remove CSS id or class identifiers
	var name = ic.slice(1);
	
	// If you are looking for element by id
	if (ic.charAt(0) == '#')
	{
		func($(name));
	}
	else
	{
		var items = $class(name);
		
		items.each(function (item) {
			func(item);
		});
	}
}

// Prints out string
function echo(str)
{
	document.write(str);
}

// Show form
function show_form(id)
{
	var form_container_id = id + '_form_container';
	var text_container_id = id + '_text_container';
	
	if (isset(form_container_id))
	{
		$(form_container_id).show();
	}
	
	if (isset(text_container_id))
	{
		$(text_container_id).hide();
	}
}

function select_all_checkboxes(e)
{
	var theForm = e.form;

	for (var i=0; i < theForm.length; i++)
	{
		if (theForm[i].type == 'checkbox' && theForm[i].name != 'select_all')
		{
			theForm[i].checked = e.checked;
		}
	}
}

// Tooltip
var tooltip = {

    timeout: {},
	
	show: function(e, ttl)
	{
        clearTimeout(this.timeout[e]);
		
        if ($(e).style.display == 'none')
		{
            this.timeout[e] = setTimeout(function(){$(e).show();}, either_or(ttl, 50));
        }
    },
	
    hide: function(e, ttl)
	{
        if ($(e).style.display == 'none')
		{
            clearTimeout(this.timeout[e]);
        }
		else
		{
            this.timeout[e] = setTimeout(function(){$(e).hide();}, either_or(ttl, 200));
        }
    }
}