/*
	_Utils.js : Utilidades

	Autor: 
*/

// ------------------------------------
// Datos
var m_PopWnd       = null;
var m_CurTab       = "";
var m_CurTabIFrame = "";

var m_FitTop = false;

window.onload   = _OnLoad;
window.onunload = _OnUnload;

// ------------------------------------
// _OnLoad
function _OnLoad()
{
	if( m_FitTop == true )
		GetTopWnd().SetMinHeight( document.body.offsetHeight );
		
	if ( typeof OnInit == "function" )
		OnInit();
}

// ------------------------------------
// _OnUnload
function _OnUnload()
{
	if( m_FitTop == true )
		GetTopWnd().RestoreMinHeight();
		
	if ( typeof OnEnd == "function" )
		OnEnd();
}

// --------------------------------------------------------
// Funciones de Ventana

// ------------------------------------
// CreatePopupWnd
function CreatePopupWnd( nLeft, nTop, nWidth, nHeigth )
{
	try
	{
		var bCenter = false;
		
		if( nLeft == -1 || nTop == -1 )
			bCenter = true;
			
		if ( bCenter )
		{	
			nLeft = (screen.width-nWidth)/2;
			nTop  = (screen.height-nHeigth)/2;
		}
		
		var Params = "";
		
		Params += "left="   + nLeft   + ",";
		Params += "top="    + nTop    + ",";
		Params += "width="  + nWidth  + ",";
		Params += "height=" + nHeigth + ",";
		Params += "status=yes,resizable=yes,scrollbars=yes";
	
		m_PopWnd = window.open( "about:blank", "wndPop", Params );
		m_PopWnd.focus();
	}
	catch(e) {}
	
	return m_PopWnd;
}

// ------------------------------------
// Pop_Load
function Pop_Load( sURL )
{
	try
	{
		var sExec = "m_PopWnd.location ='" + sURL + "'"; 
		window.setTimeout( sExec, 100 );
	}
	catch(e) {}
}	

// ------------------------------------
// Tab_SetTab
function Tab_SetTab( sID )
{
	try
	{
		if( m_CurTab != sID )
		{
			Ds_SetClass( m_CurTab, "tabItem0" );
			m_CurTab = sID;
			Ds_SetClass( m_CurTab, "tabItem1" );
		}
		
		var fn = "fn" + sID + "()"; 
		eval( fn );
	}
	catch(e) {}
}	


// ------------------------------------
// GetTopWnd
function GetTopWnd( pFrame )
{
	try
	{
		var pThis = ( (arguments.length > 0) ? pFrame : this );
		
		// --- Comprobar esta ventana
		if( pThis.ITopWnd )
			return pThis;
			
		// --- Jerarquía hacia arriba
		while( pThis.parent != pThis )
		{
			pThis = pThis.parent;
			
			if( pThis.ITopWnd )
				return pThis;
		}
			
		if( pThis.opener )
			return GetTopWnd( pThis.opener );
	}
	catch(e) {}
	
	return null;
}	

// ------------------------------------
// GetWnd
function GetWnd( sWnd, pFrame )
{
	var pWnd = null;

	try
	{
		var n       = 0;
		var pThis   = ( (arguments.length > 1) ? pFrame : GetTopWnd() );
		
		// --- Buscar primero en esta ventana
		pWnd = pThis.frames[sWnd];
		if( pWnd != null )
			return pWnd;

		// --- Jerarquía hacia abajo
		for ( n = 0; n < pThis.frames.length; n++ )
		{
			pWnd = GetWnd( sWnd, pThis.frames[n] );

			if( pWnd != null )
				return pWnd;
		}
	}
	catch(e) {}
	
	return pWnd;
}

// ------------------------------------
// WndLoad
function WndLoad( sWnd, sURL, bReplace )
{
	try
	{
		var sExec = "GetWnd('" + sWnd + "')";
		
		if( (arguments.length > 2) && (true == bReplace) )
			sExec += ".location.replace('" + sURL + "')";
		else
			sExec += ".location = '" + sURL + "'";

		window.setTimeout( sExec, 100 );
	}
	catch(e) {}
}
