/*
 * Loads an external JavaScript file on the fly.
 */
function loadJS(filename) { 
	scriptNode = document.createElement('script');
	scriptNode.src = filename;
	scriptNode.type = 'text/javascript';
	(document.getElementsByTagName('head'))[0].appendChild(scriptNode);
}

/*
 * Checks to see if the inputStr is null or empty.
 */
function isEmpty(inputStr) {
    var isEmpty = ( inputStr == null || inputStr == undefined );
    if ( ! isEmpty ) {
    	inputStr = trim( inputStr);
        isEmpty = ( inputStr.length == 0 );
    }
    return isEmpty;
}

/*
 * Trims leading and ending white space from the string.
 */
function trim (inputStr) {
    if ( inputStr != null && inputStr != undefined &&
        inputStr.length > 0 ) {
        return inputStr.replace(/^\s*|\s*$/g,'');
    } else {
        return inputStr;
    }
}

/**
 * This function will give focus to a textbox or textarea, then select the text within it (if any).
 * @param fld This is a reference to the textbox or textarea you are targeting.
 *
 * Example usage:  focusAndSelect(document.forms[0].textboxName)
*/
function focusAndSelect(fld) {
	fld.focus();
	fld.select();
}

/**
 * Displays the DOM element with id=<toggleId> if the value of the element with
 * id=<selectorId> is equal to <val>.  Otherwise, hides that element.
 *
 * If <entryId> is specified, the DOM element with id=<entryId> will be focused
 * when toggleId is toggled on.
 * If <defaultEntryVal> is also specified, the value of entryId will be set to
 * defaultEntryVal when toggleId is toggled off.
 *
 * @param toggleId - id of the element to be toggled.
 * @param selectorId - id of the element used to determine the state of toggleId.
 * @param val - value of selectorId for which toggleId will be visible.
 * @param entryId - (OPTIONAL) If specified, id of element to be focused if the
 *              value of selectorId == val, or otherwise, to be reset if defaultEntryVal is given.
 * @param defaultEntryVal - (OPTIONAL) Value used when resetting entryId, if
 *              entryId is specified.
 *
*/
function toggleElementOnSelect( toggleId, selectorId, val, entryId, defaultEntryVal ) {
	var selector = document.getElementById(selectorId);
	var toggleElem = document.getElementById(toggleId);

	var entry = null;
	if( entryId != undefined ) {
		entry = document.getElementById(entryId);
	}

	if( selector.value == val ) {
		toggleElem.style.display = "block";
		if( entry != null ) {
			entry.focus();
		}
	}
	else {
	    // selectorID is not selected or was unselected - Reset entry if specified.
		toggleElem.style.display = "none";
		if( entry != null ) {
		    if ( defaultEntryVal != undefined )
				entry.value = defaultEntryVal;
		}
	}
}

/***************************************************************
 * The following open<xxx>Window functions are utility functions
  * used to write window changes after window is opened. Specifically,
 * openHTMLWindow is used to get around bug in IE which locks up the window
 * if there isn't a delay between opening a window and
 * writing to it.
 ***************************************************************/

// opens a small window to display HTML content. Assumes the caller passes in fully formatted HTML content.
function openHTMLWindow(htmlStr, height, width) 
{
    /***************************************************************
     * Utility to write window changes after window is opened.  
     * Used to get around bug in IE which locks up the window
     * if there isn't a delay between opening a window and
     * writing to it.  Called internally via "setTimeout(innerWriteWindowContent, 50);"
     * Note that it assume the vars newWindow and newWindowContent are set!
     ***************************************************************/
 	// we use a javascript feature here called "inner functions"
    // using these means the local variables retain their values after the outer function
    // has returned. this is useful for thread safety, so
    // reassigning the onreadystatechange function doesn't stomp over earlier requests.
    var newWindow = null;
    var newWindowContent = "";
    function innerWriteWindowContent() 
    {
	    newWindow.document.open();
	    newWindow.document.write(newWindowContent);
	    newWindow.document.close();
    }

    if ( height != null )
        hgt = height;
    else
        hgt = 300;
    if ( width != null )
        wid = width;
    else
        wid = 400;
        
    // NOTE: IE does NOT allow the windowTitle to have whitespace in it! Also - NN?FF trick - title MUST be
    // "_blank" to have window.open() open a NEW (sub) window! Otherwise it overwrites one of the same name!
	newWindow = window.open("", "_blank", "height=" + hgt + ",width=" + wid + ",resizable,scrollbars");
	                                                               //,left=0,top=100,screenX=0,screenY=100
	if (newWindow != null) 
	{
		newWindowContent += htmlStr;

        if (!newWindow.opener) 
	    {
	        newWindow.opener = self;
	    }

		// set timer to call function to write to window (need to use timer 
		// because of IE timing issue with opening window/writing to document
		// Also note that IE doesn't allow the form of passing optional function parameters as
		// arguments 3 thru n to setTimeout (ie., args after the timeout in millis param)
		// We use the inner function "trick" to get around that...
		setTimeout(innerWriteWindowContent, 50);
	}
}


//opens a small window to display HTML content. Assumes the caller passes in fully formatted HTML content.
function openHTMLWindowImmed(htmlStr, height, width) 
{
    /***************************************************************
     * Utility to write window changes after window is opened.  
     ***************************************************************/
    var newWindow = null;

    if ( height != null )
        hgt = height;
    else
        hgt = 300;
    if ( width != null )
        wid = width;
    else
        wid = 400;
        
    // NOTE: IE does NOT allow the windowTitle to have whitespace in it! Also - NN?FF trick - title MUST be
    // "_blank" to have window.open() open a NEW (sub) window! Otherwise it overwrites one of the same name!
	newWindow = window.open("", "_blank", "height=" + hgt + ",width=" + wid + ",resizable,scrollbars");
	if (newWindow != null) 
	{
        if (!newWindow.opener) 
	    {
	        newWindow.opener = self;
	    }

	    newWindow.document.open();
	    newWindow.document.write(htmlStr);
	    newWindow.document.close();
	}
}


// opens a small window to display text. Assumes caller just passes in "raw" text - this function
// will wrap in minimal HTML to make the browser happy
function openTextWindow(text, height, width) 
{
		var html = "<html><head><title>Information</title></head><body>" + text;
		    html += "<br /><br /><div style='text-align: center'><input type='button' value='Close' onclick='window.close();' /></div></body></html>";

        openHTMLWindow(html, height, width);
}

// opens a small window to display specified URL.
// NOTE: IE does NOT allow the windowTitle to have whitespace in it!
function openURLWindow(urlToOpen, height, width) {
    if ( height != null )
        hgt = height;
    else
        hgt = 300;
    if ( width != null )
        wid = width;
    else
        wid = 300;
    optStr = "height=" + hgt + ",width=" + wid + ",resizable,scrollbars";
    // NOTE: IE does NOT allow the windowTitle to have whitespace in it!
	window.open(urlToOpen, "URLWindow", optStr);
}


function forward(url)
{
	window.location=url;
}

function errorAlertBox(msg){
	if (msg.length < 1){
		return;	
	}
	else{
		window.alert(msg);
	}
}

