var menu_timer = new Array();
var menu_cell = "";
var menu_a = ""
var highlight = '';
var menu_status = new Array();
var browser;
var a_highlight = '#808080';
var hiddenSelects = new Array();
var selectBehaviour = 'clip'; // use 'hide' or 'clip'

function browser(){
	if (document.all){
		browser = 'ie'
	}else if (document.getElementById){
		browser = 'moz'
	}else if (document.layers){
		browser = 'ns4'
	}
	return browser
}
browser = browser()

function persistent_highlight(cell,a_tag){
	close_all_subs()
	menu_cell = cell
  	menu_a = a_tag
}

function mouseon(cell,submenu,a_tag,position,x,y){
	if (browser != 'ns4'){
		cell.style.backgroundColor = highlight;
    if (a_tag) menu_div(a_tag).style.color=a_highlight;
	}
	if (submenu){
		if (position){
			if (position == 'relative'){
				pos = return_positions(cell);
				x = parseInt(x);
				y = parseInt(y);			
				menu_div(submenu).style.top = (pos['y']+y) + "px";
				menu_div(submenu).style.left = (pos['x']+x) + "px";
			}else{ // position must be absolute
				menu_div(submenu).style.top = y + "px";
				menu_div(submenu).style.left = x + "px";
			}
		}
		submenu_on(submenu)
	} else {
		close_all_subs()
	}
}

function mouseout(cell,submenu,a_tag){
	
	if (browser != 'ns4' && menu_cell != cell){
		cell.style.backgroundColor = ""
		if 	(menu_div(a_tag))
			menu_div(a_tag).style.color="";
	}
	if (submenu){
		submenu_off(submenu)
	}
}


function menu_div(div_name){
	if (browser == 'ie'){
		construct = document.all[div_name];
	}else if (browser == 'moz'){
		construct = document.getElementById(div_name)
	}else if (browser == 'ns4'){
		construct = document.layers[div_name]
	}
	return construct
}
function subClick(href){
	document.location.href = href
}


var r;
function submenu_on(menu){
	var parWidth = new Array();
	var parHeight = new Array();
	var parNode = new Array();
	var clipLeft;
	var clipRight

	menu_status[menu] = 0;
	close_all_subs();
	clear_timer(menu);
	menu_status[menu] = 1;

	// get a list of selects on the page

	selects = document.getElementsByTagName("SELECT");
	for (i=0;i < selects.length; i++){
		if (document.all){
			var divBox = menu_div(menu).getBoundingClientRect();
			var selBox = selects[i].getBoundingClientRect();

			// check for overlap
			if (divBox.bottom > selBox.top && divBox.right > selBox.left &&
					divBox.left < selBox.right && divBox.top < selBox.bottom){
				if (selectBehaviour == 'hide')
					selects[i].style.visibility = 'hidden';
				else if (selectBehaviour == 'clip'){
					// check for partial overlap
					if (divBox.right > selBox.right){
						if (divBox.left < selBox.left)
							clipLeft = selBox.right - selBox.left;
						else
							clipLeft = 0;
					}
					else
						clipLeft = divBox.right - selBox.left;
					clipRight = (selBox.right - selBox.left) - (selBox.right - divBox.left);

					if (clipLeft < 0) clipLeft = 'auto';
					if (clipRight < 0) clipRight = 'auto';

					// make sure the parent height and width is fixed (stops elements on the page
					// moving when select position changes to absolute
					parNode = selects[i].parentNode;
					parBox = parNode.getBoundingClientRect();
					if (!parNode.style.width)
						parNode.style.width = parBox.right - parBox.left;
					if (!parNode.style.height)
						parNode.style.height = parBox.bottom - parBox.top;
					
					

					// Change position to absolute and set clipping
					selects[i].style.position = "absolute";
					selects[i].style.left = selBox.left-2;
					selects[i].style.top = selBox.top-2;
					selects[i].style.clip = 'rect(auto '+clipRight+' auto '+clipLeft+')';
				}
			}
		}
		hiddenSelects[selects[i].sourceIndex] = selects[i];
	}
	menu_div(menu).style.visibility = 'visible';
}

function submenu_off(menu){
	set_timer(menu)
}

function close_menu(menu){
		menu_div(menu).style.visibility = 'hidden';
		menu_status[menu] = 0;
		if (!document.layers) {
      	if (menu_cell) menu_cell.style.backgroundColor = "";
      	if (menu_a) menu_div(menu_a).style.color = "";
		for (i in hiddenSelects){
			if (selectBehaviour == 'hide'){
				hiddenSelects[i].style.visibility = 'visible';
			}else if(selectBehaviour == 'clip') {
				hiddenSelects[i].style.top = 0;
				hiddenSelects[i].style.left = 0;
				hiddenSelects[i].style.position = 'relative';
				hiddenSelects[i].style.clip = 'rect(auto auto auto auto)'
			}
		}
    }
}

function set_timer(menu){
	menu_timer[menu] = setTimeout("close_menu('"+menu+"')",500)
}
function clear_timer(menu){
	clearTimeout(menu_timer[menu]);
}
function close_all_subs(){
	for (a in menu_status){
		if (menu_status[a] == 1) {
			close_menu(a);
			clear_timer(a);
		}
	}
}

// -->

<!--


function getRealLeft(el) { 
	xPos = el.offsetLeft; tempEl = el.offsetParent; 
	while (tempEl != null) { xPos += tempEl.offsetLeft; 
		tempEl = tempEl.offsetParent; 
	} 
	return xPos; 
} 

function getRealTop(el) { 
	yPos = el.offsetTop; tempEl = el.offsetParent; 
	while (tempEl != null) { yPos += tempEl.offsetTop; 
		tempEl = tempEl.offsetParent; 
	} 
	return yPos; 
} 

//Assign the returned values to variables in this way: 

function return_positions(el) {
	//el = document.getElementById(el);
	trueX = getRealLeft(el); 
	trueY = getRealTop(el); 
	pos = new Array();
	pos['x'] = trueX;
	pos['y'] = trueY;
	return pos;
}

//-->

// Highlighting mouseover rows
function highlight_on(tr,colour){
	if (!colour) colour =  '#E1EAF7';
	tr.style.backgroundColor = colour;
	tr.style.cursor = "pointer"
}
function highlight_off(tr,colour){
	if (!colour) colour = '#FFFFFF';
	tr.style.backgroundColor = colour;
}
var open_success;
function show_success(el_clicked, el_show, x_add, y_add){
	if (!y_add)
		y_add = 0;
	if (!x_add)
		x_add = 0;
	el= document.getElementById(el_show);
	if (open_success)
		close_success(open_success);
	open_success = el;
	el.style.left = x_add + "px";
	el.style.top = y_add + "px";
	el.style.visibility = "visible";
}
function close_success(element_close){
	element_close.style.visibility = "hidden"
}

