//  utils.js


//  Simple popup menu handlers.
//  An object that pops a menu must have an ID.  You must initialize it's
//  "onmouseover" hander to be the "showMenu" function.
//  The menu that gets popped must have the same ID prefixed with "menu-",
//  and should have been initialized to invisible by setting display:none"
//
var menupending = null;
function showMenu() {
    if ( menupending )  reallyHideMenu();
    var el = $( "menu-" + this.id );
    menupending = el;
    menupending.timeoutHandle = setTimeout( reallyHideMenu, 1500 );
    el.onmouseout = hideMenu;
    el.onmouseover = keepMenu;
    el.style.display = "block";
    menupending = el;
}
function hideMenu() {
    if ( menupending ) {
	clearTimeout( menupending.timeoutHandle );
    } else {
	menupending = this;
    }
    menupending.timeoutHandle = setTimeout( reallyHideMenu, 200 );
}
function keepMenu() {
    if ( menupending ) {
        if ( menupending == this ) {
	    clearTimeout( menupending.timeoutHandle );
	    menupending = null;
        } else {
	    reallyHideMenu();
        }
    }
}
function reallyHideMenu() {
    if ( !menupending ) return;
    clearTimeout( menupending.timeoutHandle );
    menupending.style.display = "none";
    menupending.onmouseout = null;
    menupending = null;
}

//  NOTE: if you specify "allow recursion" on an object that's
//  recursive, then you deserve what you get.  Don't do it!
function debug( message, allowRecursion ) {
    var console = document.getElementById("debug");
    if ( ! console ) {
	document.body.appendChild( document.createElement("br") );
        console = document.createElement( "textarea" );
	console.id = "debug";
	console.rows = 20;
	console.cols = 80;
	document.body.appendChild( console );
	var el = document.createElement( "button" );
	el.appendChild( document.createTextNode("Clear Debug") );
	el.onclick = clearDebug;
	document.body.appendChild( document.createElement("br") );
	document.body.appendChild( el );
    }
    if ( message instanceof Array ) {
	console.value += "array:\n";
	for ( var i=0; i<message.length; i++ ) {
	    console.value += "  ["+i+"]: ";
	    debug( message[i], allowRecursion );
        }
    } else if ( message instanceof Object ) {
	console.value += message + ":\n";
	for ( var i in message ) {
	    console.value += "  ."+i+": ";
	    debug( message[i], allowRecursion );
	}
    } else {
        if ( message[message.length-1] == "\\" ) {
	    console.value += message.substring(0,message.length-1);
	} else {
            console.value += message + "\n";
	}
    }
}
function clearDebug() {
    var console = document.getElementById("debug");
    if ( console )  console.value = "";
}
function trim( s ) {
    return ( s.replace(/^\s+/,"").replace(/\s+$/,"") );
}
//  some conversion functions to avoid NaN, undefined, etc
function stringToNumber( s ) {
    var x = parseFloat( s );
    if ( isNaN(x) ) x = 0;
    return ( x );
}
function numberToString( x, places, blankIfZero ) {
    if ( typeof x != "number" )  x = stringToNumber( x );
    if ( blankIfZero  &&  x == 0 )  return ( "" );
    if ( places )  x = x.toFixed(places)
    return ( x+"" );
}
// This function MUST ONLY BE USED in the <head> because it
// does a document.write()!!!!
function require( name, dir ) {
    if ( !dir )  dir = "Jobs"
    name = dir + "/" + name;
    document.write( "<script type='text/javascript' src='" );
    document.write( name );
    document.writeln( "'><\/script>" );
}

function safeString( s ) {
    //  zap characters we don't want in string fields!
    s = s.replace( "|","+" )
    	.replace( "\\n"," " )
	.replace( "&","&amp;" )
	.replace( "<","&lt;" );
    return( s );
}


/****
function getHHMMSS() {
    var now = new Date();
    var hh = now.getHours();
    hh = ( hh < 10 ) ? "0"+hh : ""+hh;
    var hhmmss = twoDigits( now.getHours() ) + ":"
			+ twoDigits( now.getMinutes() ) + ":"
			+ twoDigits( now.getSeconds() );
    return ( hhmmss );
}

function twoDigits( n ) {
    return ( (n<10) ? "0"+n : ""+n );
}
****/

