/* Window functions */
function openChild(file, windowname, width, height)
{
	child_window = open(file, windowname, 'width=' + width + ',height=' + height + ', toolbar = no, location = no, directories = no, status = no, mnubar = no, scrollbars = yes, resizable = yes');
	if (child_window == null) {
		alert('It seems that you have a popup-blocker installed. Please allow popups for this site');
	}
	if (child_window.opener == null) {
		child_window.opener = self;
	}
	return child_window;
}

function updateParent()
{
	opener.document.location.reload();
	self.close();
	return false;
}

/* Functions for setting multiple onload-statements */
var _onload_array = new Array();

function onload_push(string) {
	document.onload = onload_execute;
	_onload_array[_onload_array.length] = string;
}

function onload_execute() {
	var string = '';
	for (i = 0; i < _onload_array.length; ++i) {
		string += _onload_array[i];
	}
	eval (string);
}

/* Functions for setting multiple onclick-statements on document*/
var _onclick_array = new Array();

function onclick_push(string) {
	document.onclick = onclick_execute;
	_onclick_array[_onclick_array.length] = string;
}

function onclick_execute() {
	var string = '';
	for (i = 0; i < _onclick_array.length; ++i) {
		string += _onclick_array[i];
	}
	eval(string);
}

var _ontimer_array = new Array();
var _timer_id = 0;

function ontimer_push(string) {
	_timer_id = window.setInterval('ontimer_execute();', 50);
	_ontimer_array[_ontimer_array.length] = string;
	return _ontimer_array.length - 1;
}

function ontimer_execute() {
		var string = '';

	for (i = 0; i < _ontimer_array.length; ++i) {
		string += _ontimer_array[i];
	}
	eval(string);
}

function ontimer_removeat(id) {
	if (id >= 0 && id < _ontimer_array.length) {
		_ontimer_array[id] = '';
	}
}
function ontimer_reset() {
	window.clearInterval(_timer_id);
}

/* Function for getting the value of the selected item in a select */
function getSelectedOption(selectObj)
{

	//  if (!('options' in selectObj)) {
	//    return null;
	//  }

	for (var i = 0; i < selectObj.options.length; ++i) {
		if (selectObj.options[i].selected) return selectObj[i].value;
	}
	return null;
}

/* Functions for context-sensitive menus */
// Returns the mouse position as an array(x, y)
function get_pos(ev) {
	var pos_x = 0;
	var pos_y = 0;
	if (ev.pageX || ev.pageY) {
		pos_x = ev.pageX;
		pos_y = ev.pageY;
	} else if (ev.clientX || ev.clientY) {
		pos_x = ev.clientX + document.body.scrollLeft;
		pos_y = ev.clientY + document.body.scrollTop;
	}
	return new Array(pos_x, pos_y);
}


/* The context-sensitive menu-class */
var _MM_context_menus = new Array();

function MM_context_menu(name) {
	this.name = name;
	this.submenus = 0;
	document.write('<div id="' + this.name + '" style="visibility: hidden; position: absolute; top: 0px; left: 0px; width: 180px; background-color: #ffffff; border: 1px solid #999999;"></div>');
	this.add_to_menu = add_to_menu;
	this.add_separator = add_separator;
	this.clear_menu = clear_menu;
	this.show_menu = show_menu;
	this.hide_menu = hide_menu;
	if (!_MM_context_menus.length) {
		onclick_push('MM_context_hide_all_menus();');
	}
	_MM_context_menus.push(this);

	function add_to_menu(text, onclick)
	{
		++this.submenus;
		id = this.name + '.' + this.submenus;
		document.getElementById(this.name).innerHTML = document.getElementById(this.name).innerHTML + '<div id="' + id + '" style="width: 100%; background-color: #ffffff;" onmouseover="context_menu_mouse_over(\'' + id + '\');" onmouseout="context_menu_mouse_out(\'' + id + '\');" onclick=' + onclick + '>' + text + '</div>';
	}

	function add_separator()
	{
		document.getElementById(this.name).innerHTML = document.getElementById(this.name).innerHTML + '<hr>';
	}

	function clear_menu()
	{
		document.getElementById(this.name).innerHTML = '';
	}

	function show_menu(e)
	{
		var ev = e ? e : window.event;
		var elem = (e.target) ? e.target : e.srcElement;
		pos = get_pos(ev);
		document.getElementById(this.name).style.visibility = 'visible';
		document.getElementById(this.name).style.left = pos[0] + 'px';
		document.getElementById(this.name).style.top  = pos[1] + 'px';
		return elem;
	}


	function hide_menu()
	{
		document.getElementById(this.name).style.visibility = 'hidden';
	}
}

/* Functions for context-sensitive menus */
function context_menu_mouse_over(id) {
	document.getElementById(id).style.backgroundColor = 'rgb(31, 31, 127)';
	document.getElementById(id).style.color = 'rgb(255,255,255)';
}

function context_menu_mouse_out(id) {
	document.getElementById(id).style.backgroundColor = 'rgb(255, 255, 255)';
	document.getElementById(id).style.color = 'rgb(0, 0, 0)';
}

function MM_context_hide_all_menus()
{
	for (i in _MM_context_menus) {
		_MM_context_menus[i].hide_menu();
	}
}

