var _d = document;
var _w = window;

_w.onerror = function() { return false; }

var menu_objs = new Array();
var menu_timers = new Array();
var menu_hide_timer = 200;
var menu_instant_switch = true;
var onload_funcs = new Array();
var xml_req;

// alias for document.getElementsByTagName
function dom_by_tag(tag)
{
	return _d.getElementsByTagName(tag);
}

// alias for document.getElementById
function dom_id(id)
{
	return _d.getElementById(id);
}

// alias for document.createElement
function dom_create(tag)
{
	return _d.createElement(tag);
}

//alias for document.createTextNode
function dom_createText(text)
{
	return _d.createTextNode(text);
}

// search array for value and return position if found or -1 if not
function array_pos(val, array, field)
{
	for (var k = 0; k < array.length; k ++) {
		if (field && field.length > 0) {
			if (array[k][field] == val) {
				return k;
			}
		}
		else {
			if (array[k] == val) {
				return k;
			}
		}
	}
	return -1;
}

// returns the elements position on the document
function get_position(obj, type)
{
	var retval = 0;

	if (type == "Left" && obj.x) {
		return obj.x;
	}
	else if (obj.y) {
		return obj.y;
	}
	else {
		while (obj.offsetParent) {
			//if (obj && obj.style && obj.style.position) obj.style.position = "relative";
			retval += eval("obj.offset" + type);
			obj = obj.offsetParent;
		}
		return retval;
	}
}

// tr
function ntr(t,id)
{
	return t.insertRow(id);
}

// td
function ntd(tr,id)
{
	return tr.insertCell(id);
}

// check whether a value is 'set'
function isset(arg)
{
	return (typeof(arg) != "undefined") ? true : false;
}

// adds menus to menu arrays
function get_menus()
{
	var divs = document.getElementsByTagName("div");
	var d = 0;
	while (divs[d]) {
		if (divs[d].id && divs[d].id.substring(0, 7) == "submenu") {
			divs[d].style.position = "absolute";
			divs[d].style.zIndex = 1;
			divs[d].onmouseover = new Function("cancel_hide_timer('" + divs[d].id + "')");
			divs[d].onmouseout = new Function("start_hide_timer('" + divs[d].id + "')");
			menu_objs[menu_objs.length] = divs[d];
		}
		d ++;
	}
}

// starts the hide timer (time enough to allow users to mouseover submenus)
function start_hide_timer(menu)
{
	menu_timers[menu] = setTimeout("hide_menu('" + menu + "')", menu_hide_timer);
}

// cancels the hide timer
function cancel_hide_timer(menu)
{
	if (menu_timers[menu]) {
		clearTimeout(menu_timers[menu]);
	}
}

// hides all menus
function hide_all_menus()
{
	var k = 0;
	while (menu_objs[k]) {
		menu_objs[k].style.display = "none";
		k ++;
	}
}

// hides the menu
function hide_menu(menu)
{
	if (document.getElementById(menu)) {
		document.getElementById(menu).style.display = "none";
	}
}

// shows the menu
function show_menu(menu, obj)
{
	me = document.getElementById(menu);
	if (menu_instant_switch) {
		hide_all_menus();
	}
	if (me) {
		me.style.left = (get_position(obj, "Left") - 1)+'px';
		me.style.top = (get_position(obj, "Top") + obj.offsetHeight)+'px';;
		me.style.zIndex = 3;
		me.style.display = "";
	}
}

function send_search()
{
	var f = document.forms['search_customers'];
	if (f.elements['search_firstname'].value.length == 0 && f.elements['search_lastname'].value.length == 0) {
		alert("Please specify a value for firstname or surname to perform a valid search");
	}
	else {
		f.submit();
	}	
}


// adds to onload
function add_to_onload(func)
{
	func = func.replace(/;$/i, "");
	onload_funcs[onload_funcs.length] = func;
	window.onload = new Function(onload_funcs.join(";"));
}


add_to_onload("get_menus()");