//<![CDATA[

if( navigator.appName.indexOf( "Microsoft" ) != -1 )
	window.g_bIsIE = true;
else
	window.g_bIsIE = false;

function getObj( sName )
{
	if( document.getElementById )
		return( document.getElementById( sName ) );
	if( document.all )
		return( document.all[ sName ] );
	if( document.layers )
		return( document.layers[ sName ] );
	return( null );
}

function getEventObject( ev )
{
	return( ev ? ev : window.event );
}

function getEventElement( ev )
{
	if( ev )
		return( ev.target ? ev.target : ev.srcElement );
	return( null );
}

function getChildren( node )
{
	if( window.g_bIsIE )
		return( node.children );
	else
		return( node.childNodes );
}

function formatDates( root )
{
	if( root ) {
		if( root.id && root.id.match( /dt.*/ ) ) {
			var d = new Date();

			d.setTime( root.innerHTML );
			root.innerHTML = d.toLocaleString( );
		}

		var children = getChildren( root );

		if( children ) {
			var nChild;
			
			for( nChild = 0; nChild < children.length; ++nChild )
				formatDates( children[ nChild ] );
		}
	}
}

function setVisible( cObj, bSet )
{
	if( cObj != null ) {
		cStyle = cObj;
		if( cStyle.style )
			cStyle = cStyle.style;
		if( typeof( cStyle.display ) != 'undefined' ) {
			if( bSet == true ) {
				if( window.g_bIsIE == true )
					cStyle.display = "inline";
				else {
					if( cObj.nodeName ) {
						switch( cObj.nodeName ) {
						case "TABLE":
							cStyle.display = "table"; break;
						case "TR":
							cStyle.display = "table-row"; break;
						case "TD":
							cStyle.display = "table-cell"; break;
						default:
							cStyle.display = "inline"; break;
						}
					}
					else
						cStyle.display = "table-row";
				}
			}
			else
				cStyle.display = "none";
		}
	}
}

function isVisible( cObj )
{
	if( cObj != null ) {
		if( cObj.style )
			cObj = cObj.style;
		if( typeof( cObj.display ) != 'undefined' )
			return( cObj.display != "none" );
	}
	return( false );
}

function toggleVisible( cObj )
{
	setVisible( cObj, ! isVisible( cObj ) );
}

function setFocus( cObj )
{
	if( cObj != null )
		cObj.focus( );
}

function findParentById( h, idRegExp )
{
	if( h ) {
		if( h.id && h.id.match( idRegExp ) )
			return( h );
		return( findParentById( h.parentNode, idRegExp ) );
	}
	return( null );
}

function findParentByTAG( h, sTAG )
{
	if( h ) {
		if( h.tagName.toUpperCase( ) == sTAG.toUpperCase( ) )
			return( h );
		return( findParentByTAG( h.parentNode, sTAG ) );
	}
	return( null );
}

function findChildById( h, idRegExp )
{
	if( h ) {
		var nChild;

		if( h.id && h.id.match( idRegExp ) )
			return( h );

		var children = getChildren( h );

		if( children ) {		
			for( nChild = 0; nChild < children.length; ++nChild ) {
				var c;
				
				if( ( c = findChildById( children[ nChild ], idRegExp ) ) != null )
					return( c );
			}
		}
	}
	return( null );
}

function findChildByName( h, idRegExp )
{
	if( h ) {
		var nChild;

		if( h.name && h.name.match( idRegExp ) )
			return( h );

		var children = getChildren( h );

		if( children ) {		
			for( nChild = 0; nChild < children.length; ++nChild ) {
				var c;
				
				if( ( c = findChildByName( children[ nChild ], idRegExp ) ) != null )
					return( c );
			}
		}
	}
	return( null );
}

function findChildByTAG( h, sTag )
{
	if( h ) {
		var children = getChildren( h );

		if( children ) {		
			var nChild;
			var c;
			
			for( nChild = 0; nChild < children.length; ++nChild ) {
				if( children[ nChild ].tagName.toUpperCase( ) == sTag.toUpperCase( ) )
					return( children[ nChild ] );
			}
			for( nChild = 0; nChild < children.length; ++nChild ) {
				if( ( c = findChildByTAG( children[ nChild ], sTag ) ) != null )
					return( c );
			}
		}
	}
	return( null );
}

function walk( o, f )
{
	if( o ) {
		var children = getChildren( o );

		if( ! f( o ) )
			return( false );
		if( children ) {		
			var nChild;
			
			for( nChild = 0; nChild < children.length; ++nChild )
				if( ! walk( children[ nChild ], f ) )
					return( false );
		}
	}
	return( true );
}

function getTableRowCol( tr, nCol )
{
	if( tr && tr.tagName.toUpperCase( ) == "TR" ) {
		var nChild;
	
		var children = getChildren( tr );

		if( children ) {		
			for( nChild = 0; nChild < children.length; ++nChild ) {
				if( children[ nChild ].tagName.toUpperCase( ) == "TD" )
					--nCol;
				if( nCol < 0 )
					return( children[ nChild ] );
			}
		}
	}
	return( null );
}

function getSiblingCellContent( refCell, siblingBaseId )
{
	var row, reqCell;

	row = findParentByTAG( refCell, "TR" );
	if( ! row ) {
		alert( "Houve uma falha ao tentar identificar o usu&aacute;rio selecionado. Informe o suporte." );
		return;
	}
	reqCell = findChildById( row, siblingBaseId + "[0-9]+" );
	if( ! reqCell ) {
		alert( "Houve uma falha ao tentar identificar o login do usu&aacute;rio selecionado. Informe o suporte." );
		return;
	}
	return( reqCell.innerHTML );
}

function setBG( o, c )
{
	if( o.savedBG == undefined )
		o.savedBG = o.bgColor;
	if( o.bgColor == o.savedBG )
		o.bgColor=c;
}

function restoreBG( o )
{
	if( o.savedBG != undefined && o.bgColor != o.savedBG )
		o.bgColor=o.savedBG;
}

function toggleBG( o, c )
{
	if( o.savedBG == undefined )
		o.savedBG = o.bgColor;
	if( o.bgColor == o.savedBG )
		o.bgColor=c;
	else
		o.bgColor=o.savedBG;
}

function executeWorkflow( cell, sParentRootTAG )
{
	var oRecord, oChild, nWFAction;
	
	oRecord = findParentByTAG( cell, sParentRootTAG );
	oChild = findChildById( oRecord, "Mandado_ID[0-9]?" );
	addDynamicFormParam( "ProcessID", oChild.innerHTML );

	oChild = findChildById( oRecord, "AcaoWorkflow[0-9]?" );
	nWFAction = oChild.options[ oChild.selectedIndex ].value;
	if( nWFAction == undefined || nWFAction == null || nWFAction == "" ) {
		alert( "Por favor selecione qual andamento deseja executar." );
		return;
	}
	addDynamicFormParam( "WorkflowEvent", nWFAction );

	postDynamicForm( "RouteWorkflowAction.html" );
}

function getDynamicForm( )
{
	var f, p;
	
	f = $( '__formtool' );
	if( f == null || f == undefined ) {
		var d;
		
		f = document.createElement( "form" );
		f.id = "__formtool";
		f.name = "__formtool";
		d = $( '__formtoolhider' );
		if( d == null || d == undefined ) {
			d = document.createElement( "div" );
			d.id = "__formtoolhider";
			setVisible( d, false );
			d.appendChild( f );
		}
		document.body.appendChild( d );
	}

	f.method = "post";
	return( f );
}

function addDynamicFormParam( sParamName, sParamValue )
{
	var f, p;
	
	f = getDynamicForm( );
	p = findChildByName( f, "^" + sParamName + "$" );
	if( p == null || p == undefined ) {
		p = document.createElement( "input" );
		p.type = "hidden";
		p.name = sParamName;
		p.id = sParamName;
		f.appendChild( p );
	}
	p.value = sParamValue;
}

function postDynamicForm( sAction )
{
	var f;

	f = getDynamicForm( );
	if( sAction != undefined && sAction != null && sAction.length > 0 )
		f.action = sAction;
	f.submit( );
}

function postCommand( sAction, sParamName, sParamValue )
{
	addDynamicFormParam( sParamName, sParamValue );
	postDynamicForm( sAction );
}
//]]>

