// Menu variables
var timerID = null;
var timer2_ID = null;
var timerOn = false;
var timecount = 300;
var left_offset = 10;
var menu_2_offset = 22;
var i_num_menus = 40;

// for tool tips
var tooltip_obj;

function imgON(img)
{
    img.src = "images/" + img.id + "_on.jpg"
}
function imgOFF(img)
{
    img.src = "images/" + img.id + ".jpg"
}

function pop_menu(name)
{
    // Clear the timeouts
    if (timerID != null)
    {
	clearTimeout(timerID);
	timerID = null;
    }

    if (timer2_ID != null)
    {
	clearTimeout(timer2_ID);
	timer2_ID = null;
    }
    
    var offset = 0;
    
    // Only names with id under 20 uses left_offset
    if (name < 20)
    {
	offset = left_offset;
    }
    
    var menu = get_object("menu_" + name);
    var pop = get_object("pop_" + name);
    var top = top_pos(menu);// + menu.offsetHeight;
    var left = left_pos(menu) + menu.offsetWidth + offset;

    pop.style.left = left + "px";
    pop.style.top = top + "px";
    pop.style.visibility = "";

    //alert("left: " + pop.style.left + "  top: " + pop.style.top + " offsetwidth: " + menu.offsetWidth);
}

function close(name)
{
    var obj = get_object("pop_" + name);

    if (obj)
    {
	obj.style.visibility = "hidden";
    }
}

function close_pop_menu(name)
{  
    if (timer2_ID != null)
    {
	clearTimeout(timer2_ID);
	timer2_ID = null;
    }
    
    timer2_ID = setTimeout("close(" + name + ")", timecount);
    timerOn = true;
}

function clear_menus()
{
    for (var i = 0; i <= i_num_menus; i++)
    {
	close(i);	
    }
}

// Starts time for timerID
function start_time()
{
    if (timerID != null)
    {
	clearTimeout(timerID);
	timerID = null;
    }

    timerID = setTimeout("clear_menus()", timecount);
    timerOn = true;
}

// Stops time for timerID
function stop_time()
{
    clearTimeout(timerID);
    timerID = null;
    timerOn = false;
}

// Stops time for timer2_ID
function stop_time2()
{
    clearTimeout(timer2_ID);
    timer2_ID = null;
    timerOn = false;
}

function handle_over(num)
{
    clear_menus();
    pop_menu(num);
    stop_time();
}

function handle_sub_over(num)
{
    pop_menu(num);
    stop_time();
}

function handle_out()
{
    start_time();
}  

function top_pos(el) 
{
    return doPosLoop(el, "Top");
}

function left_pos(el) 
{
    return doPosLoop(el, "Left");
}

function doPosLoop(el, val) 
{
    var temp = el;
    var x = temp["offset" + val];
    while (temp.tagName != "BODY") 
    {
	temp = temp.offsetParent;

	if (temp)
	{
	    x += temp["offset" + val];
	}
	else
	{
	    break;
	}
    }
    return x;
}

function get_object(obj)
{
    if (document.getElementById)
    {
	return document.getElementById(obj);
    }
    else if (document.layers)
    {
	return document.layers[obj];
    }
    else if (document.all)
    {
	return document.all[obj];
    }
}

function open_window(url_i, name_i, width_i, height_i)
{
    window.open(url_i, name_i, 'width=' + width_i + ',height=' + height_i + ',toolbar=yes, location=yes, directories=no, status=no, menubar=yes, scrollbars=yes, resizable=no');
}

// /////////////////////////////////////////////////////////////////////
// This function limits the input to numbers
// Input: the field, an event, a decimal
// Output: restricts the field to numbers
// Created by: Feryl A. Dec. 20, 2004
// /////////////////////////////////////////////////////////////////////
function numbersonly(myfield, e, dec)
{
    var key;
    var keychar;

    // Grab the key events
    if (window.event)
    {
	key = window.event.keyCode;
    }
    else if (e)
    {
	key = e.which;
    }
    else
    {
	return true;
    }

    // Convert the character code to string
    keychar = String.fromCharCode(key);

    // control keys
    if ((key == null) || (key == 0) || (key == 8) || (key == 9) || (key == 13) || (key == 27))
    {
	return true;
    }
    // numbers
    else if ( ( ("0123456789.-").indexOf(keychar) > -1) )
    {
	return true;
    }
    // decimal point jump
    else if ( dec && (keychar == ".") )
    {
	myfield.form.elements[dec].focus();
	return false;
    }
    else
    {
	return false;
    }
}

function delete_confirm()
{
    return confirm("Are you sure you want to delete?");
}

function confirm_dbdelete(is_checked)
{
    if (is_checked)
	return confirm("Item will be removed from the database.\nAre you sure you want to delete?");
}

function display_editor(bl_checked)
{
    var editor = get_object("editor");
    if (bl_checked)
    {
	editor.style.display = "none";
    }
    else
    {
	bl_checked = false;
	editor.style.display = "block";
	return bl_checked;  
	}
}
	
function pop_tooltip(event_i, item_i)
{
    tooltip_obj = get_object(item_i + "_tip");
    
    if (tooltip_obj)
    {
	tooltip_obj.style.left = get_mouse_x(event_i) - 125;
	tooltip_obj.style.top = get_mouse_y(event_i) + 10;
	//tip.style.left = 100;
	//tip.style.top = 100;
	tooltip_obj.style.visibility = "";

	if (document.captureEvents)
	{
	    document.captureEvents(Event.MOUSEMOVE);
	}
	document.onmousemove = move_tooltip;
    }
}

function close_tooltip(event_i)
{
    //var tip = getObject(item_i + "_tip");
    if (tooltip_obj)
    {
	tooltip_obj.style.visibility = "hidden";
	tooltip_obj = null;

	if (document.releaseEvents)
	{
	    document.releaseEvents(Event.MOUSEMOVE);
	}
	document.onmousemove = null;
    }
}

function move_tooltip(event_i)
{
    if (tooltip_obj)
    {
	tooltip_obj.style.left = get_mouse_x(event_i) - 125;
	tooltip_obj.style.top = get_mouse_y(event_i) + 10;
    }
}

function get_mouse_y(event_i)
{
    if (!event_i)
    {
	if (window.event)
	{
	    event_i = window.event;
	}
	else
	{
	    return;
	}
    }
    
    if (event_i.pageY)
    {
	return event_i.pageY;
    }
    else if (event_i.clientY)
    {
	return event_i.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
    }
    else
    {
	return null;
    }
}

function get_mouse_x(event_i)
{
    if (!event_i)
    {
	if (window.event)
	{
	    event_i = window.event;
	}
	else
	{
	    return;
	}
    }

    if (event_i.pageX)
    {
	return event_i.pageX;
    }
    else if (event_i.clientX)
    {
	return event_i.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
    }
    else
    {
	return null;
    }
}

