// ----------------------------------------------------------------------------
//
//		The following section deals with the Access Config Dialog in general
//
// ----------------------------------------------------------------------------

var IDS_UI_BROWSE_NTDOMAINS_WARNING = "The Windows NT Domain browser can take a significant amount of time to load if there are a substantial number of Domains to display.\n\nClick OK to continue.\nClick Cancel to return to the calling dialog.";
var IDS_UI_NO_NT_DOMAIN_NAME = "Please specify a Windows NT Domain name."


/**
 *
 *	[Public] Responds to page onLoad event
 *
 */
function SCA_onLoadAccessConfigDlg()
{
	// dispatch commands to initialize client-side settings
	// for all three aspects of Access config
	SCA_initNTDomainsSettings();
}


/**
 *
 *	[Public] Responds to OK clicked event on Access Config Dialog
 *
 */
function SCA_onOKAccessConfigDlg()
{
	var pForm = window.top.document.SCA_frmConfigAccess;

	
	// deal with NT Domains
	SCA_onOKForNTSettings( pForm );

	// change mouse icon to wait
	SCA_setMouseIconToWait( self );
	
	// post form content to server to update configuration
	pForm.target = "SCA_iframeHidden";
	pForm.action = pForm.SCA_hidOnOKClickedURL.value;
	pForm.submit();
}

// ----------------------------------------------------------------------------
//
//		The following section deals with NT Domains Settings
//
// ----------------------------------------------------------------------------

var g_pOriginalNTDomainsTable;
var g_pLatestNTDomainsTable;

/**
 *
 *	[Public] Initialize Javascript script objects for NT Domains
 *
 */
function SCA_initNTDomainsSettings()
{
	// initialize the two Tables keeping tracking of current supported Containers and
	// latest supported containers as configured by user
	g_pOriginalNTDomainsTable = new OriginalSupportedObjectsTable();
	g_pLatestNTDomainsTable = new LatestSupportedObjectsTable();
	
	SCA_initNTDomains( g_pOriginalNTDomainsTable, g_pLatestNTDomainsTable )
}

/**
 *
 *	[Public] Handle clicking of "Add" button for NT Domains
 *
 */
function SCA_onNTDomainAddClicked()
{
	if ( document.SCA_frmConfigAccess.SCA_txtNTDomainNameToAdd.value == "")
	{
		alert(IDS_UI_NO_NT_DOMAIN_NAME);
		return;
	}
	
	// set mouse cursor to Wait	
	SCA_setMouseIconToWait( self );
	
	var pForm = window.top.document.SCA_frmConfigAccess;
	pForm.target = "SCA_iframeHidden";
	pForm.action = pForm.SCA_hidOnNTDomainAddClickedURL.value;
	pForm.submit();
	
}

/**
 *
 *	[Public] Clear the "Add" field of NT Domains
 *
 */
function SCA_clearNTDomainToAddField()
{
	var pForm = window.top.document.SCA_frmConfigAccess;
	pForm.SCA_txtNTDomainNameToAdd.value = "";
}

/**
 *
 *	[Public] Handle clicking of "Browse" button for browsing NT Domains
 *
 */
function SCA_onNTDomainBrowseClicked()
{
	if ( confirm(IDS_UI_BROWSE_NTDOMAINS_WARNING) )
	{
		var strDlgURL = document.SCA_frmConfigAccess.SCA_hidOnNTDomainBrowseClickedURL.value;

		SCA_openWindowAndSubmit('SCA_frmConfigAccess', strDlgURL, 'BrowseNTDomain', 'width=350,height=490,toolbar=none');
	}
}

/**
 *
 *	[Public] Clone a copy of the data structure which represents 
 *			the latest SupportedNTDomains state to pass to the pop-up
 *			Browsing dialog.
 *
 */
function SCA_cloneLatestSupportedNTDomainsTable()
{
	return g_pLatestNTDomainsTable.Clone();
}

/**
 *
 *	[Public] Receives the latest SupportedNTDomains state from the pop-up
 *			Browsing dialog and also update the UI here on this dialog.
 *
 */
function SCA_updateLatestSupportedNTDomains( pObjectsSelectionTableFromPopup )
{	
	// update underlying data structure
	g_pLatestNTDomainsTable = pObjectsSelectionTableFromPopup.Clone();

	// update UI list of supported NT Domains
	
	// remove all rows in the Table first
	var pTable = document.getElementById('SupportedNTDomainsListTable');	
	while (pTable.rows.length > 0)
		pTable.deleteRow();
	
	// add objects back to UI row by row
	var pObject, bIsSelected, bIsInDB;
	var strInputHTML, pRow, pCell;
	for (var nIndex=0; nIndex < g_pLatestNTDomainsTable.ItemsCount; nIndex++)
	{
		pObject = g_pLatestNTDomainsTable.GetEntry(nIndex)
		strExternalObjectID = pObject.ExternalObjectID;
		bIsSelected = pObject.IsSelected;
		bIsInDB = pObject.IsInDB;

		// create the entry only if it's not unselected by user or
		// it is in the currently supported objects list in DB
		if (bIsSelected || bIsInDB)
		{
			// generate the smae HTML created by server side script
			// when the dialog was loaded
			strInputHTML = '<input type="checkbox" name="' + strExternalObjectID + '" ';
			if (bIsSelected)
				strInputHTML += 'checked ';		
			strInputHTML += '" onclick="SCA_onNTDomainClicked(this)">\n';

			pRow = pTable.insertRow();
			pCell = pRow.insertCell();
			pCell.width = 1;			
			pCell.innerHTML = strInputHTML;
			pCell = pRow.insertCell();
			pCell.innerHTML = '<td><nobr>' + strExternalObjectID + '</nobr></td>';
		}
	}
}

/**
 *
 *	[Public] Respond to NT Domain checkbox clicked
 *
 */
function SCA_onNTDomainClicked( pCheckBox )
{
	g_pLatestNTDomainsTable.SetObjectSelectedState( pCheckBox.name, "", pCheckBox.checked )
}

function SCA_onOKForNTSettings( pForm )
{
	// figure out what NT Domains need to be removed/added from the Supported list
	var pSupportedNTDomainsChanges = new SupportedObjectsChanges( g_pOriginalNTDomainsTable, g_pLatestNTDomainsTable );
	pForm.SCA_hidNTDomainsToAdd.value = pSupportedNTDomainsChanges.GetObjectsAddList();
	pForm.SCA_hidNTDomainsToRemove.value = pSupportedNTDomainsChanges.GetObjectsRemoveList();
}


