/**
 * File:		/scripts/generic.js
 * Author:		G. Tillman
 * Project:		HB6
 * Copyright:	2002 AGS.  All Rights Reserved.
 * Purpose:		Generic Javascripts
 * History:		0001-20020613 (got) Initial Creation
 *				0002-20020625 (got) Added setClientSize and centerWindow
 *				0003-20020722 (got) Added setChangeHandler, setButtonHandler, getElementText
 *				0004-20030113 (got) Added setBlurHandler, checkName
 */
 
/**
 * Public Scripts:
 *		centerWindow
 *		checkName
 *		forceDocumentRedraw
 *		getAllChildren
 *		getClientHeight
 *		getClientWidth
 *		getElementHeight
 *		getElementLeft
 *		getElementText
 *		getElementTop
 *		getElementWidth
 *		getWindowLeft
 *		getWindowTop
 *		setButtonHandler
 *		setChangeHandler
 *		setClientSize
 *		setElementText
 *		setResizeHandler
 */
 
/**
 * Center the window (actually, the client area) on the screen
 * @since 0002-20020625 (got)
 */

function centerWindow()
{
	window.moveTo(
		(screen.availWidth-getClientWidth())/2,
		(screen.availHeight-getClientHeight())/2
	);
}	// centerWindow

/**
 * 20030113 (got) - Check the string nm for invalid name characters
 * We accept any of the following as valid:
 * 1. letters
 * 2. numbers
 * 3. '-._
 * 4. space
 * returns true if valid, else false
 */
function checkName( nm )
{
	var rc = ( nm.search(/^[a-zA-Z0-9' ._\-]+$/) > -1 );
	
	return rc;
}	// checkName

/**
 * Forces the document to refresh itself.  This is required
 * strictly as an IE bug workaround!
 */
function forceDocumentRedraw()
{
	var saveColor = document.body.style.borderColor;
	if( saveColor == "red")
		document.body.style.borderColor = "green";
	else
		document.body.style.borderColor = "red";
	document.body.style.borderColor = saveColor;
}	// forceDocumentRedraw


/**
 * Recursively look down through all children of parentNode and add them to
 * childArr (an Array).  Only  nodes that are type nodeType; e.g., "img"
 * are returned.
 * Parameters:
 *	parentNode - starting place
 *	childArr - holds the results. NOTE - Be sure and instantiate your
 *		array BEFORE passing it to this function!
 *	nodeName - e.g. "img"
 * Notes:
 *  1.	This is a recursive function
 *	2.	childArr is NOT cleared out before starting
 */
function getAllChildren( parentNode, childArr, nodeName )
{	
	if( typeof parentNode.childNodes != "undefined" && parentNode.childNodes != null )
	{	// ok to proceed
		var nodeNameLC = nodeName.toLowerCase();
		for( var i = 0; i < parentNode.childNodes.length; ++i )
		{
			if( parentNode.childNodes[i].nodeName.toLowerCase() == nodeNameLC )
				childArr[childArr.length] = parentNode.childNodes[i];
			getAllChildren( parentNode.childNodes[i], childArr, nodeName );
		}	// for i								
	}	// ok to proceed
}	// getAllChildren

/**
 * Get the height of the browser's client area
 * @since 0001-20020613 (got)
 */
function getClientHeight()
{
	var cH = 0;

	if( typeof window.innerHeight != "undefined" )
	{	// NN (this test must come before the IE test)
		cH = window.innerHeight;
	}	// NN
	else if( typeof document.body.clientHeight != "undefined" )
	{	// IE
		cH = document.body.clientHeight;
	}	// IE
	else alert( "getClientHeight: unsupported browser" );
	
	return cH;
}	// getClientHeight
 
/**
 * Get the width of the browser's client area
 * NOTE: on IE cannot use this until after document loads
 * @since 0001-20020613 (got)
 */
function getClientWidth()
{
	var cW = 0;

	if( typeof window.innerWidth != "undefined" )
	{	// NN (this test must come before the IE test)
		cW = window.innerWidth;
	}	// NN
	else if( typeof document.body.clientWidth != "undefined" )
	{	// IE
		cW = document.body.clientWidth;
	}	// IE
	else alert( "getClientWidth: unsupported browser" );
	
	return cW;
}	// getClientWidth

/**
 * Get the height of the element (in pixels)
 */
function getElementHeight( theElem )
{
	return theElem.offsetHeight;
}	// getElementHeight

/**
 * Get the left edge of the element (in pixels)
 */
 function getElementLeft( theElem )
 {
 	return theElem.offsetLeft;
 }	// getElementLeft
 
/**
 * Return the (plain) text contents of the element whose id is
 * elemID or "" if it cannot find any
 * See also: setElementText
 */
function getElementText( elemID )
{
	var theElem = document.getElementById(elemID);
	if( theElem != null )
	{
		if( typeof theElem.innerText != "undefined" )
			return theElem.innerText;		// IE
		else if( typeof theElem.firstChild != "undefined" )
			return theElem.firstChild.data;	// NN
		else return "";
	}	// theElem != null

}	// getElementText
 
 /**
  * Get the top edge of the element (in pixels)
  */
function getElementTop( theElem )
{
	return theElem.offsetTop;
}	// getElementTop

/**
 * Get the width of the element (in pixels)
 */
function getElementWidth( theElem )
{
	return theElem.offsetWidth;
}	// getElementWidth

/**
 * Get the window's left coordinate
 */
function getWindowLeft( win )
{
	if ( typeof win == "undefined" || win == null )
		return 0;
	if ( typeof win.screenX != "undefined" )
		return win.screenX;
	if ( typeof win.screenLeft != "undefined" )
		return win.screenLeft;
	return 0;
}	// getElementWidth

/**
 * Get the window's top coordinate
 */
function getWindowTop( win )
{
	if ( typeof win == "undefined" || win == null )
		return 0;
	if ( typeof win.screenY != "undefined" )
		return win.screenY;
	if ( typeof win.screenTop != "undefined" )
		return win.screenTop;
	return 0;
}	// getElementWidth

/**
 * Assign a on-blur handler function to the browser window.
 * Parameters:
 * 	theHandler - a pointer to the handler function.  Should have the 
 *		following prototype:
 *		handler_func( evt )
 *			evt will be null for IE (use global event) object
 */
function setBlurHandler( theHandler )
{
	if( typeof window.addEventListener != "undefined" )
	{	// NN6
		window.addEventListener( "blur", theHandler, false );
	}
	else if( typeof window.onblur != "undefined" )
	{	// IE
		window.onblur = theHandler;
	}
}	// setBlurHandler

/**
 * Given a button element, theElem, set it's onclick handler to theHandler
 */
function setButtonHandler( theElem, theHandler )
{
	theElem.onclick=theHandler;
}	// setButtonHandler

/**
 * If theElem supports the "onchange" event handler, set it to theHandler
 */
function setChangeHandler( theElem, theHandler )
{
	if( typeof theElem.addEventListener != "undefined" )
	{	// NN/Mozilla
		theElem.addEventListener( "change", theHandler, false );
	}
	else if( typeof theElem.onchange != "undefined" )
	{
		theElem.onchange = theHandler;
	}
}	// setChangeHandler

/**
 * Resize the current window  such that its client area is
 * exactly cWidth and  cHeight
 * Parameters:
 *		cWidth - desired client width, in pixels
 *		cHeight - desired client height, in pixels
 * @since 0002-20020625 (got)
 */
function setClientSize( cWidth, cHeight )
{
	window.resizeTo( cWidth, cHeight );
	var innerW = getClientWidth();
	var innerH = getClientHeight();
	var deltaW = cWidth - innerW;
	var deltaH = cHeight - innerH;
	window.resizeTo( cWidth + deltaW, cHeight + deltaH );
}	// setClientSize

/**
 * Set the (plain) text of the given element.  Works for p tags.
 * TEST IT before using on other tags.
 * Parameters:
 *		elemID - the ID of the parent tag whose text you wish to set.  
 *		theText - the text to use
 */
function setElementText( elemID, theText )
{
	var theElem = document.getElementById(elemID);
	if( theElem != null )
	{
		if( typeof theElem.innerText != "undefined" )
			theElem.innerText = theText;		// IE
		else if( typeof theElem.firstChild != "undefined" )
			theElem.firstChild.data = theText;	// NN
	}	// theElem != null
}	// setElementText

/**
 * Assign a resize handler function to the browser window.
 * Parameters:
 * 	theHandler - a pointer to the handler function.  Should have the 
 *		following prototype:
 *		handler_func( evt )
 *			evt will be null for IE (use global event) object
 */
function setResizeHandler( theHandler )
{
	if( typeof window.addEventListener != "undefined" )
	{	// NN6
		window.addEventListener( "resize", theHandler, false );
	}
	else if( typeof window.onresize != "undefined" )
	{	// IE
		window.onresize = theHandler;
	}
}	// setResizeHandler
