var g_nTimerID = null;
var g_pLowerFrame = window.top.frames[1];


/**
 *
 *	Respond to page load event
 *
 */
function SCA_onLoadVisibleObjectsFrame()
{
	if (false == SCA_attemptPullData())
	{
		g_nTimerID = setTimeout("SCA_onTimer()", 500);
	}
}

/**
 *
 *	Respond to timer event to keep polling lower frame for data
 *
 */
function SCA_onTimer()
{
	if (true == SCA_attemptPullData())
	{
		clearTimeout(g_nTimerID);
	}
	else
	{
		g_nTimerID = setTimeout("SCA_onTimer()", 500);
	}
}

/**
 *
 *	Attempt to pull data from lower frame and report success/failure
 *
 */
function SCA_attemptPullData()
{
	if ( eval("g_pLowerFrame.SCA_getObjectsSelectionTable") != null )
	{
		var pObjectsSelectionTable = g_pLowerFrame.SCA_getObjectsSelectionTable();
		if (pObjectsSelectionTable != null)
		{
			var strID = "";
			for ( var i=0; i < pObjectsSelectionTable.ItemsCount; i++ )
			{
				// get the SupportedObject entry in the Table and see if its .IsSelected flag is true
				var pSupportedObject = pObjectsSelectionTable.GetEntry(i);
				if (pSupportedObject.IsSelected == true)
				{
					// look for a checkbox with the given ID and check it
					strID = pSupportedObject.ExternalObjectID;
					SCA_setCheckboxState( strID, true );
				}
			}
			// we have successfully grabbed the ObjectsSelectionTable from lower frame
			// and tried to check all checkboxes that should be checked
			return true;
		}
	}

	return false;
}

/**
 *
 *	To set the state of a checkbox in this frame
 *
 *	@param strID [in] - the ID that identifies the check box
 *
 *	@param bChecked [in] - whether the check box is checked or unchecked
 *
 */
function SCA_setCheckboxState( strID, bChecked )
{
	if (document.SCA_frmVisibleObjects.elements[strID])
	{
		document.SCA_frmVisibleObjects.elements[strID].checked = bChecked;
	}
}

/**
 *
 *	Handle user checking/unchecking checkboxes beside node in this frame
 *
 *	@param strID [in] - the ID that identifies the check box
 *
 *	@param strDisplayName [in] - the Display Name of the node
 *
 *	@param bChecked [in] - whether the check box is checked or unchecked
 *
 */
function SCA_onClickedCheckBox( strID, strDisplayName, bChecked )
{
	// synch up lower frame UI
	g_pLowerFrame.SCA_setCheckboxState( strID, bChecked );
	
	// update underlying data structure
	g_pLowerFrame.SCA_setObjectSelectedState( strID, strDisplayName, bChecked );
}

