
var IDS_EMPTY_CHANNEL = "No Destination Channel has been selected.";

var g_bIsIE = false;

var IDS_FORM_CHANNEL_NAME = "NC_intxtChannelName";
var IDS_FORM_CHANNEL_GUID = "NC_intxtChannelGuid";

// ---- [Public] Body onload event handler
function WBC_onLoadHandler( bFreshTree )
{	
	g_bIsIE = (document.all != null);
	
	// only set Tree scrolling offset if it's IE and that it's not the first time the tree is being loaded
	if (g_bIsIE && !bFreshTree)
	{
		WBC_initTreeFrameScrollOffset();
	}
}

// ---- [Private] Initialize the scrolling offset attributes
function WBC_initTreeFrameScrollOffset()
{
	// write the current scrollTop value to a hidden field in the bottom frame
	var pTreeFrameDoc = window.top.frames['WBC_frameShowChannelTree'].document;
	var pBottomFrameDoc = window.top.frames['WBC_frameShowChannelName'].document;
	if (pTreeFrameDoc.body)
	{
		if (pBottomFrameDoc.forms['NC_ChannelNameForm'].WBC_hidCacheTreeFrameScrollTop)
		{
			pTreeFrameDoc.body.scrollTop = pBottomFrameDoc.forms['NC_ChannelNameForm'].WBC_hidCacheTreeFrameScrollTop.value;
		}
		if (pBottomFrameDoc.forms['NC_ChannelNameForm'].WBC_hidCacheTreeFrameScrollLeft)
		{
			pTreeFrameDoc.body.scrollLeft = pBottomFrameDoc.forms['NC_ChannelNameForm'].WBC_hidCacheTreeFrameScrollLeft.value;
		}
	}
}

// ---- [Public] Responds to TreeNodeClicked event; transfer content 
//				from the Tree to the textbox in the bottom frame
function WBC_onTreeNodeClicked(strName, strGUID)
{	
	var pChannelNameForm;

	pChannelNameForm = window.parent.frames.WBC_frameShowChannelName.document.NC_ChannelNameForm; 		
	pChannelNameForm[IDS_FORM_CHANNEL_NAME].value = unescape(strName);
	pChannelNameForm[IDS_FORM_CHANNEL_GUID].value = strGUID;
	return true;
}

// ---- [Public] Hook to intercept CNCTreeControl node expansion event
function WBC_onExpandTreeNode( strNodeID, strNodeName )
{
	if (g_bIsIE)
	{
		// write the current scrollTop value to a hidden field in the bottom frame
		var pTreeFrameDoc = window.top.frames['WBC_frameShowChannelTree'].document;
		var pBottomFrameDoc = window.top.frames['WBC_frameShowChannelName'].document;	
		pBottomFrameDoc.forms['NC_ChannelNameForm'].WBC_hidCacheTreeFrameScrollTop.value = pTreeFrameDoc.body.scrollTop;
		pBottomFrameDoc.forms['NC_ChannelNameForm'].WBC_hidCacheTreeFrameScrollLeft.value = pTreeFrameDoc.body.scrollLeft;
	}
	
	// let the event bubble up so that it can do the round-trip
	return true;
}

// ---- [Public] Hook to intercept CNCTreeControl node collapse event
function WBC_onCollapseTreeNode(  strNodeID, strNodeName )
{
	if (g_bIsIE)
	{
		// write the current scrollTop value to a hidden field in the bottom frame
		var pTreeFrameDoc = window.top.frames['WBC_frameShowChannelTree'].document;
		var pBottomFrameDoc = window.top.frames['WBC_frameShowChannelName'].document;
		pBottomFrameDoc.forms['NC_ChannelNameForm'].WBC_hidCacheTreeFrameScrollTop.value = pTreeFrameDoc.body.scrollTop;
		pBottomFrameDoc.forms['NC_ChannelNameForm'].WBC_hidCacheTreeFrameScrollLeft.value = pTreeFrameDoc.body.scrollLeft;
	}
	
	// let the event bubble up so that it can do the round-trip
	return true;
}

// keeps track of whether page is already submitting;
// used for guarding against multiple clicks on OK
var g_bSubmitted = false;

// ---- [Public] Responds to OK button event
function WBC_onOkayClicked(strURL, strFormFieldName)
{
	var pChannelNameForm;
	
	pChannelNameForm = window.parent.frames.WBC_frameShowChannelName.document.NC_ChannelNameForm; 		

	if ( pChannelNameForm[strFormFieldName].value == "" )
	{
		alert(IDS_EMPTY_CHANNEL)
		return false;
	}
	else
	{	
		// guard against multiple clicks on OK
		if (g_bSubmitted)
		{
			return false;
		}
		else
		{
			g_bSubmitted = true;

			pChannelNameForm.action = strURL;
			pChannelNameForm.target= window.parent.frames.WBC_frameShowError.name;
			pChannelNameForm.submit();
			return true;
		}
	}
}
	
// ---- [Public] Responds to Cancel button event
function WBC_onCancelClicked()
{
	window.top.close();
	return true;
}
