var gl_Error_Color = "yellow";

function f_is_Not_Valid_Number(ao_FieldObj, as_Error_String, ab_SetFocus, ls_Max_Val) {
	if ( ao_FieldObj.disabled ) {
		return false;
	}
	if (isNaN(ao_FieldObj.value) || Math.floor(ao_FieldObj.value) < 0) {
		window.alert("Invalid data in " + as_Error_String);
		if (ab_SetFocus ) {ao_FieldObj.focus();}
		ao_FieldObj.style.backgroundColor = gl_Error_Color;
		return true; 
	}
	if (ls_Max_Val > 0 && Math.floor(ao_FieldObj.value) > ls_Max_Val) {
		window.alert("Maximum allowed value of " + ls_Max_Val + " exceeded in " + as_Error_String);
		if (ab_SetFocus ) {ao_FieldObj.focus();}
		ao_FieldObj.style.backgroundColor = gl_Error_Color;
		return true; 
	}
	if (ao_FieldObj.value != "") {
		ao_FieldObj.value = Math.floor(ao_FieldObj.value);
	}
	return false;
}

function f_Set_Error (ao_FieldObj, as_Error_String, ab_SetFocus) {
	if ( ao_FieldObj.disabled ) {return false;}
	window.alert(as_Error_String);
	if (ab_SetFocus && (ao_FieldObj.disabled != true) && (ao_FieldObj.visibility != "hidden")) {ao_FieldObj.focus();}
	ao_FieldObj.style.backgroundColor = gl_Error_Color;
	return;
}
var link_color;

function f_mouseover(la_element) {
	link_color=la_element.style.color;
	la_element.style.color="green";
}
function f_mouseout(la_element) {
	la_element.style.color=link_color;
}
function getElement(elName) {    
		return document.all.item(elName,0);
}

if (document.all) 
{	document.onmouseout = offPage; }
else
{	document.addEventListener("mouseout", offPage, true); }

// Should the mouse go off the document window hide all menus
function offPage(event)
{   var l_pageY = (document.all) ? window.event.clientY : event.pageY;
	if (l_pageY < 5) hide();
}

var ls_frame;
var ls_frame_base;

var l_frames = parent.document.getElementsByTagName("frameset");
	if (l_frames.length > 0)
	{	ls_frame	   =  l_frames[0].rows;
		ls_frame_base  =  l_frames[0].rows.substring(0,l_frames[0].rows.indexOf(","));
	}

// Shows the submenu for the menu item
function showSubMenu(menuName)
{	
	// Make the background layer visible for catching the mouse as it leaves the menu
	var currentElement = document.getElementById("clearAll");
	currentElement.style.visibility = "visible";
	// Get the parent menu for this item
	var parentElementName = menuName.substr(3);
	var parentElement = document.getElementById(parentElementName);
	// Sum up all the offsets to calculate where to display this submenu
    xPos = getLeft(parentElement);
    yPos = getHeight(parentElement) + parentElement.offsetHeight ;
	// Set the location for this submenu and display it
	var currentElement = document.getElementById(menuName);
	currentElement.style.left = xPos + "px";
	currentElement.style.top  = yPos + "px";
	currentElement.style.visibility = "visible";
	
	clipSelect(currentElement);
	var li_window_height = (document.all) ? parent.document.body.scrollHeight  : window.innerHeight;
	if (document.all)
	{	var l_frames = parent.document.getElementsByTagName("frameset");
			if (l_frames.length > 0)
			{	var li_position = yPos + currentElement.clientHeight;
			   
			    li_position = ((li_position * 100) /li_window_height) + 10;
			    li_position = (li_position < 30) ? ls_frame_base : li_position + "%";
				l_frames[0].rows =  li_position +l_frames[0].rows.substr(l_frames[0].rows.indexOf(","));
			}
	}
}

function getHeight(item)
{	if (item == null) return (0);
	var li_add = getHeight(item.offsetParent);
	return (item.offsetTop + li_add);
}

function getLeft(item)
{	if (item == null) return (0);
	var li_add = getLeft(item.offsetParent);
	return (item.offsetLeft + li_add);
}

function is_visible(item)
{	if (item == null) return true;
	if (item.style.visibility == "hidden") return false;
	if (is_visible(item.offsetParent)== false) return false;
	return true;
}

var g_list = new Array();

function clipSelect (currentMenu) 
{	
	if (document.all)
	{	var li_top    = currentMenu.offsetTop;
		var li_bottom = li_top + currentMenu.scrollHeight;
		var li_left   = currentMenu.offsetLeft;
		var li_right  = li_left + currentMenu.offsetWidth;
		var li_count = 0;
		l_select_boxes = document.getElementsByTagName("select");
		var li_count = l_select_boxes.length;
		for (i=0; i < li_count ; i++)
		{	var l_listbox  = l_select_boxes[i];
			var li_y = getHeight(l_listbox);
		    var li_x = getLeft(l_listbox);
		    var li_x_width = li_x + l_listbox.offsetWidth;
			if (li_y > li_top && li_y < li_bottom  && ((li_x >= li_left && li_x <= li_right) || (li_x_width > li_left && li_x_width < li_right) || (li_x < li_left && li_x_width > li_right)))
			{ if (is_visible(l_listbox)) 
			  {	g_list[g_list.length] = l_listbox.id;
			    l_listbox.style.visibility = "hidden";
			  }
			}
		}
	}

}

function unclipSelect() 
{
	if (document.all && g_list.length > 0)
	{  	for (i=0; i < g_list.length; i++)
			{	l_listbox  = document.getElementById(g_list[i]);
				l_listbox.style.visibility = "";
			}
	}
}

function hideSubMenu(evt,a_parent,a_child)
{  
    if (document.all)
    {   var my_object  = (document.all) ? window.event.toElement : evt.target;
		var l_pageY	   = getHeight(my_object) ;
		var l_pageX	   = getLeft(my_object) ;
	}
	else
	{
		var l_pageY	   = evt.pageY ;
		var l_pageX	   = evt.pageX ;
	}

    var l_cmenu    = document.getElementById(a_child);
	var li_cleft   = getLeft(l_cmenu) ;
	var li_cright  = li_cleft + l_cmenu.offsetWidth ;
	var li_ctop    = getHeight(l_cmenu);
	var li_cbottom = (document.all) ? li_ctop + l_cmenu.scrollHeight: li_ctop + l_cmenu.offsetHeight ;
	var ack = document.getElementById("AppBody");
	//ack.innerHTML = "<br> X:" + l_pageX + " Y:" + l_pageY + ack.innerHTML
	//ack.innerHTML = "<br> cX:" + li_cleft + ":" + li_cbottom + " cY:" + li_ctop + ":" + li_cbottom + ack.innerHTML
	//if we're still in the main menu item, don't hide the submenu
	if ( (l_pageX >= li_cleft && l_pageX <= li_cright) && (l_pageY >= li_ctop && l_pageY <= li_cbottom))
		{ return; }
	var l_pmenu    = document.getElementById(a_parent);
	var li_pleft   = getLeft(l_pmenu);
	// The -20 is a fudge factor to get the timing of mouse movement right
	var li_pright  = li_pleft + l_pmenu.offsetWidth - 20 ;
	var li_ptop    = getHeight(l_pmenu);
	var li_pbottom = li_ptop + l_pmenu.scrollHeight + 8;

		//if we're still below the main menu item, don't hide the submenu
//	if ( (l_pageX >= li_pleft && l_pageX <= li_pright) && (l_pageY >= li_ptop && l_pageY <= li_pbottom))
//		{	return; }
	if ( l_pageX >= li_pleft && l_pageX <= li_pright )
		{	return; }

	//Hide all the submenus
	hide();
}

function hide()
{	// Hide all the submenus
	mCount = 0;
	currentElement = document.getElementById("subMenu" + mCount);
	while (currentElement != null) {
		currentElement.style.visibility = "hidden";
		mCount += 1;
		currentElement = document.getElementById("subMenu" + mCount);
	}
	unclipSelect();
	var l_frames = parent.document.getElementsByTagName("frameset");
	if (l_frames.length > 0)
	{		l_frames[0].rows =  ls_frame;
	}
	
}

