/**
 * Common javascript include file for all Virtuo.CA projects
 *
 * @author Patrick Begley
 *
 * date: 04052006
 */
var collapsedRegions = new Array();

/**
 * Available functions:
 *
 *	 confirmAction( String text );
 *	 confirmUrl( String url, String text );
 *	 highlightRow( DOM.TableRow rowObject, String color );
 *	 collapseRegion( DOM.DomObject regionObject, Boolean initialState, void *callback );
 *	 getElemObject( String id );
 *
 ******************************************************************************/


/**
 * Confirms an action with specified text
 *
 * @param String text caption.
 * @return Boolean
 *
 * @author Julien DeGouffe
 */
function confirmAction( text )
{
	if( text == null || text == '' )
		return;

	return confirm( text );
}


/**
 * Confirms a url before redirection, most commonly used in onclick()
 *
 * @param String url The url to redirect to when action is confirmed.
 * @param String text The text for confirmation caption.
 *
 * @author Patrick Begley
 */
function confirmUrl( url, text )
{
	if( confirmAction( text ) )
	{
		document.location = url;
	}
}


/**
 * Highlights a row in a table when hovering over.
 *
 * @param Object rowObject The row object specified
 * @param String color The specified color in html format #000000.
 *
 * @author Patrick Begley
 */
function highlightRow( rowObject, color )
{
	if( rowObject != null && color.match( /^\#[0-9a-fA-F]{6}$/ ) != null )
	{
		for( var i = 0; i < rowObject.cells.length; i++ )
		{
			rowObject.cells[i].style.backgroundColor = color;
		}
	}
}

/**
 * Highlights the text in a object with a certain colour.
 *
 */

function highlightObj( obj, color, textOrBg )
{
	if( textOrBg != 'bg' )
		textOrBg = 'text';
	
	if( textOrBg == 'text' )
		obj.style.color = color;
	else
		obj.style.backgroundColor = color;
}



/**
 * Collapses a region with tracking and a callback function for a continous application flow.
 *
 * @param Object regionObject Region that should be collapsed, usually a Div
 * @param Boolean initialState Specifies the starting state of the object. display:hidden; == true
 * @param String callback Call back function for when the region collapses
 *
 * @author Patrick Begley
 */
function collapseRegion( regionObject, initialState, callback )
{
	if( callback )
		this.callback = callback;
	if( initialState == null )
	{
		initialState = false; // not collapsed
	}

	var collapsedStatus = false;

	if( collapsedRegions[ regionObject.id ] != null )
	{
		collapsedStatus = collapsedRegions[ regionObject.id ];
	}
	else
	{
		collapsedRegions[ regionObject.id ] = initialState;
		collapsedStatus = initialState;
	}

	if( collapsedStatus == false )
	{
		collapsedRegions[ regionObject.id ] = true;
		regionObject.style.display = 'none';
	}
	else if( collapsedStatus == true )
	{
		collapsedRegions[ regionObject.id ] = false;
		regionObject.style.display = 'block';
	}

	if( this.callback )
		this.callback( regionObject, collapsedRegions[ regionObject.id ] );
}

/**
 * Get the DOM Element object from the id
 *
 * @param String id The id of the DOM Element in html.
 * @return DOM.DomObject
 *
 * @author Patrick Begley
 */
function getElemObject( id )
{
	return ( document.all ? document.all[id] : document.getElementById ? document.getElementById( id ) : null );
}
