/** Util.js
 * Various utility functions
 */

String.prototype.trim = function() {
	// Return string without beginning or trailing whitespace
	var x = this ;
	return x.replace(/^\s+/,'').replace(/\s+$/,'');
}

function openDialog( dlgName, dlgH, dlgW, btn ) {
	// Opens a new dialog box window
	// params:
	//   dlgUrl: URL of the document to open in the new window
	//   dlgH: height of the new window
	//   dlgW: width of the new window
	//   btn: the link or form control that called the function

	var dlgTop,dlgLeft,dlgUrl,dlgParams;

	dlgUrl = '/dialog.cfm?dialog=' + dlgName ;
	if ( btn != null ) {
		dlgTop = ',top=' + ( ( getAbsY( btn ) + dlgH > screen.height ) ? getAbsY( btn ) - dlgH : getAbsY( btn ) ) ;
		dlgLeft = ',left=' + ( ( getAbsX( btn ) + dlgW > screen.width ) ? getAbsX( btn ) - dlgW : getAbsX( btn ) ) ;
	}
	else {
		dlgTop = '' ;
		dlgLeft = '' ;
	}
	dlgParams = 'titlebar=0,menubar=0,toolbar=0,location=0,status=0,fullscreen=0,resizable=1,scrollbars=1,width=' + dlgW + ',height=' + dlgH + dlgTop + dlgLeft ;

	alert( 'btn is ' + btn + '\ndlgUrl is ' + dlgUrl + '\ndlgTop is ' + dlgTop + '\ndlgLeft is ' + dlgLeft + '\ndlgH is ' + dlgH + '\ndlgW is ' + dlgW + '\ndlgParams is ' + dlgParams ) ;
	var winDlg = window.open( dlgUrl, "DialogWindow", dlgParams );
	winDlg.focus();
}

// Get the true offset of anything on NS4, IE4/5 & NS6
function getAbsX( elt ) { return ( elt.x ) ? elt.x : getAbsPos( elt, "Left" ); }
function getAbsY( elt ) { return ( elt.y ) ? elt.y : getAbsPos( elt, "Top" ); }
function getAbsPos( elt, which ) {
	var iPos = 0;
	while ( elt != null ) {
		iPos += elt["offset" + which];
		elt = elt.offsetParent;
	}
	return iPos;
}

function addOption( selId, optVal, optText ) {
	// This function adds a new option to a SELECT form control 
	// param selId: the id of the SELECT object to which to add an OPTION
	// param optVal: the value attibute of the new OPTION
	// param optText: the text of the new OPTION
	var selObj = document.getElementById( selId );
	var optExists = false ;
	for( var i = 0; i < selObj.length; i++ ) {
		// if value entered by user already exists in the SELECT menu, select it
		if( selObj.options[i].value == optVal ) {
			optExists = true ;
			selObj.options[i].selected = true ;
			break;
		}
	}
	if( ! optExists ) {
		// create the new OPTION and add to the SELECT menu
		selObj.options[selObj.length] = new Option( optText, optVal, false, true );
	}
	selObj.focus();
}

function showKey( key, lnk ) {
// This function shows the Project List key
// param key: id of the element to show/hide
	if ( document.getElementById ) 
	{
		var id = document.getElementById( key ) ;
		mTop = parseInt( getAbsY( lnk ) ) ;
		mLeft = parseInt( getAbsX( lnk ) ) - parseInt( id.style.width ) ;
		//alert('mTop = '+mTop + '\nmLeft=' + mLeft) ;
		id.style.top = mTop + 'px' ; 
		id.style.left = mLeft + 'px' ; 
		id.style.display = 'block' ; // DISPLAY IT
	}
}

function jumpTo( sUrl ) {
 	if ( sUrl != "" ) window.top.location.href = sUrl ;
}

