//*****************************************************************************
// Copyright (C) 2001 Microsoft Corporation. All rights reserved.
//
// File:   /NRConfig/AccessConfig/SupportedObjectsManipulate.js
// Author: Microsoft Corporation
//
// Description:
//		JS file that manages Supported Objects (NT Domains, Active Directory
//	Containers, Site Server OU's)	
//
//*****************************************************************************


// ----------------------------------------------------------------------------
//
//		The following section defines the OriginalSupportedObject class
//		and LatestSupportedObject class.
//
// ----------------------------------------------------------------------------

/**
 *
 *	This is the constructor for the OriginalSupportedObject class
 *
 *	@param strResolutionGUID [in] - the MSCMS GUID that uniquely
 *				identifies the object currently stored in Resolution DB.
 *			
 *	@param strExternalObjectID [in] - the external object ID that uniquely
 *				identifies the object in the external repository.
 *
 */
function OriginalSupportedObject( strResolutionGUID, strExternalObjectID )
{
	this.ResolutionGUID = strResolutionGUID;
	this.ExternalObjectID = strExternalObjectID;
}


/**
 *
 *	This is the constructor for the LatestSupportedObject class
 *
 *	@param strExternalObjectID [in] - the external object ID that uniquely
 *				identifies the object in the external repository.
 *
 *	@param strDisplayName [in] - the UI display name of the Object.
 *
 *	@param bIsAvailable [in] - the boolean value to indicate whether the object
 *				is currently available through SCAPI.
 *
 *	@param bIsSelected [in] - the boolean value to indicate whether the object
 *				is currently selected (checked) by user
 *
 *	@param bIsInDB [in] - the boolean value to indicate whether the object
 *				is in the currently supported list of objects in the DB
 *
 */
function LatestSupportedObject( strExternalObjectID, strDisplayName, bIsAvailable, bIsSelected, bIsInDB )
{
	this.ExternalObjectID = strExternalObjectID;
	this.DisplayName = strDisplayName;
	this.IsAvailable = bIsAvailable;
	this.IsSelected = bIsSelected;
	this.IsInDB = bIsInDB;
}

/**
 *
 *	Clone a copy of the object. This is similar to a Copy constructor.
 *
 */
function LatestSupportedObject_Clone()
{
	return new LatestSupportedObject( this.ExternalObjectID, this.DisplayName, this.IsAvailable, this.IsSelected, this.IsInDB );
}

// hook up method to the LatestSupportedObject Class
LatestSupportedObject.prototype.Clone = LatestSupportedObject_Clone;

// ----------------------------------------------------------------------------
//
//		The following section implements the OriginalSupportedObjectsTable class
//
// ----------------------------------------------------------------------------

/**
 *
 *	This is the constructor for the OriginalSupportedObjectsTable class
 *
 */
function OriginalSupportedObjectsTable()
{
	this.ItemsCount = 0;
	this.TableArray = new Array();
}

/**
 *
 *	This method adds an OriginalSupportedObject object to the table
 *
 *	@param pOriginalSupportedObject [in] - the OriginalSupportedObject
 *				object to be added to the table
 *
 */
function OriginalSupportedObjectsTable_AddEntry( pOriginalSupportedObject )
{
	this.TableArray[this.TableArray.length] = pOriginalSupportedObject;
	this.ItemsCount = this.TableArray.length;
}

/**
 *
 *	This method retrieves an OriginalSupportedObject object from the table
 *
 *	@param nIndex [in] - the index number used to retrieve the object
 *
 */
function OriginalSupportedObjectsTable_GetEntry( nIndex )
{
	if (nIndex < this.TableArray.length)
		return this.TableArray[nIndex];
	else
		return null;
}

/**
 *
 *	This method checks for the existence of an object entry.
 *
 *	@param strExternalObjectID [in] - the external object ID that uniquely
 *				identifies the object in the external repository.
 *
 */
function OriginalSupportedObjectsTable_EntryExists( strExternalObjectID )
{
	// loop through the array to match strExternalObjectID
	for (var nIndex = 0; nIndex < this.TableArray.length; nIndex++)
	{
		if (this.TableArray[nIndex].ExternalObjectID == strExternalObjectID)
		{
			return true;
		}
	}

	// not found
	return false;
}

// hook up method to the OriginalSupportedObjectsTable Class
OriginalSupportedObjectsTable.prototype.AddEntry = OriginalSupportedObjectsTable_AddEntry;
OriginalSupportedObjectsTable.prototype.GetEntry = OriginalSupportedObjectsTable_GetEntry;
OriginalSupportedObjectsTable.prototype.EntryExists = OriginalSupportedObjectsTable_EntryExists;


// ----------------------------------------------------------------------------
//
//		The following section implements the LatestSupportedObjectsTable class
//
// ----------------------------------------------------------------------------

/**
 *
 *	This is the constructor for the LatestSupportedObjectsTable class
 *
 */
function LatestSupportedObjectsTable()
{
	this.ItemsCount = 0;
	this.TableArray = new Array();
}

/**
 *
 *	This method adds an LatestSupportedObject object to the table
 *
 *	@param pLatestSupportedObject [in] - the LatestSupportedObject
 *				object to be added to the table
 *
 */
function LatestSupportedObjectsTable_AddEntry( pLatestSupportedObject )
{
	this.TableArray[this.TableArray.length] = pLatestSupportedObject;
	this.ItemsCount = this.TableArray.length;
}

/**
 *
 *	This method retrieves an LatestSupportedObject object from the table
 *
 *	@param nIndex [in] - the index number used to retrieve the object
 *
 */
function LatestSupportedObjectsTable_GetEntry( nIndex )
{
	if (nIndex < this.TableArray.length)
		return this.TableArray[nIndex];
	else
		return null;
}

/**
 *
 *	This method sets the IsSelected flag of an object.
 *
 *	@param strExternalObjectID [in] - the ID that identifies the object in the 
 *				external repository
 *
 *	@param strDisplayName [in] - the display name of the object
 *
 *	@param bSelected [in] - the boolean value to set the IsSelected field to.
 *
 */
function LatestSupportedObjectsTable_SetObjectSelectedState( strExternalObjectID, strDisplayName, bSelected )
{
	// loop through the array to match strExternalObjectID and then
	// modify IsSelected property with bSelected
	for (var nIndex = 0; nIndex < this.TableArray.length; nIndex++)
	{
		if (this.TableArray[nIndex].ExternalObjectID == strExternalObjectID)
		{
			this.TableArray[nIndex].IsSelected = bSelected;
			return;
		}
	}
	
	// if not found in existing table, that must mean it is selecting an object
	// not in the Table yet. ASSERT that bSelected must be true.
	if (bSelected != true)
	{
		alert("DEBUG ASSERT: Logical error in LatestSupportedObjectsTable_SetObjectSelectedState()");
		return;
	}
	
	// Add an entry to the Table, 
	//	bIsAvailable parameter: 'true' which indicates that this object is currentyy visible
	//	bIsInDB parameter: 'false' which indicates that  this object was not in the 
	//						Currently Supported Objects list from the DB.
	var pNewSupportedObject = new LatestSupportedObject( strExternalObjectID, strDisplayName, true, bSelected, false );
	this.AddEntry( pNewSupportedObject )
}

/**
 *
 *	This method returns if the Entry with the a given LDAP GUID is currently
 *	selected
 *
 *	@param strExternalObjectID [in] - the LDAP GUID which identifies the Container
 *
 */
function LatestSupportedObjectsTable_EntrySelected( strExternalObjectID )
{
	// loop through the array to match strExternalObjectID and then
	// return its IsSelected value
	for (var nIndex = 0; nIndex < this.TableArray.length; nIndex++)
	{
		if (this.TableArray[nIndex].ExternalObjectID == strExternalObjectID)
		{
			return this.TableArray[nIndex].IsSelected;
		}
	}
	
	// not found
	return false;
}

/**
 *
 *	Clone a copy of the object. This is similar to a Copy constructor.
 *
 */
function LatestSupportedObjectsTable_Clone()
{
	var pCloneTable = new LatestSupportedObjectsTable();
	var pCloneSupportedObject = null;
	
	// loop through the Table and clone each entry
	for (var nIndex = 0; nIndex < this.TableArray.length; nIndex++)
	{
		pCloneSupportedObject = this.TableArray[nIndex].Clone();
		pCloneTable.AddEntry( pCloneSupportedObject );
	}

	return pCloneTable;	
}

// hook up method to the LatestSupportedObjectsTable Class
LatestSupportedObjectsTable.prototype.AddEntry = LatestSupportedObjectsTable_AddEntry;
LatestSupportedObjectsTable.prototype.GetEntry = LatestSupportedObjectsTable_GetEntry;
LatestSupportedObjectsTable.prototype.SetObjectSelectedState = LatestSupportedObjectsTable_SetObjectSelectedState;
LatestSupportedObjectsTable.prototype.EntrySelected = LatestSupportedObjectsTable_EntrySelected;
LatestSupportedObjectsTable.prototype.Clone = LatestSupportedObjectsTable_Clone;

// ----------------------------------------------------------------------------
//
//		The following section implements the SupportedObjectsChanges class
//
// ----------------------------------------------------------------------------

var IDS_XML_TAG_OBJECTS = "Objects";
var IDS_XML_TAG_OBJECT = "Object";


/**
 *
 *	This is the constructor for the SupportedObjectsChanges class
 *
 *	@param pOriginalSupportedObjectsTable [in] - the OriginalSupportedObjectsTable object
 *				which contains the list of Supported Containers originally
 *			
 *	@param pLatestSupportedObjectsTable [in] - the LatestSupportedObjectsTable object which
 *				contains the list of Supported Containers that has been
 *				modified by user manipulation through the UI
 *
 */
function SupportedObjectsChanges( pOriginalSupportedObjectsTable, pLatestSupportedObjectsTable )
{
	this.OriginalSupportedObjectsTable = pOriginalSupportedObjectsTable;
	this.LatestSupportedObjectsTable = pLatestSupportedObjectsTable;
}

/**
 *
 *	This method generates the list of LDAP Containers to be added. For each 
 *	of the Container in the LatestSupportedObjectsTable with IsSelected being true,
 *	it checks for its existence in the OriginalSupportedObjectsTable and if it 
 *	does not exist, the Container should be in the "Add" list.
 *
 */
function SupportedObjectsChanges_GetObjectsAddList()
{
	var strList = "";
	var strExternalObjectID = "";
	
	for (var nIndex = 0; nIndex < this.LatestSupportedObjectsTable.ItemsCount; nIndex++ )
	{
		if (this.LatestSupportedObjectsTable.GetEntry(nIndex).IsSelected == true)
		{
			strExternalObjectID = this.LatestSupportedObjectsTable.GetEntry(nIndex).ExternalObjectID;
			if (this.OriginalSupportedObjectsTable.EntryExists( strExternalObjectID ) == false)
			{
				// guard against duplicates, just in case
				if ( strList.indexOf(strExternalObjectID) < 0 )
				{
					// wrap each LDAP DistinguishedName within "<Object>...</Object>"
					var strDisplayName = this.LatestSupportedObjectsTable.GetEntry(nIndex).DisplayName;
					strList = strList + SCA_wrapXMLTag( IDS_XML_TAG_OBJECT, strDisplayName, false);
				}
			}		
		}
	}

	// finally wrap the whole XML string within "<Objects>...</Objects>"
	return SCA_wrapXMLTag( IDS_XML_TAG_OBJECTS, strList, false );
}

/**
 *
 *	This method generates the list of LDAP Containers to be removed. For each 
 *	of the Container in the OriginalSupportedObjectsTable, it checks its IsSelected 
 *	flag in the LatestSupportedObjectsTable and if it is false, that means the 
 *	Container should be in the "Remove" list.
 *
 */
function SupportedObjectsChanges_GetObjectsRemoveList()
{
	var strList = "";
	var strExternalObjectID = "";
	var strResGUID = "";
	
	for (var nIndex = 0; nIndex < this.OriginalSupportedObjectsTable.ItemsCount; nIndex++ )
	{
		strExternalObjectID = this.OriginalSupportedObjectsTable.GetEntry(nIndex).ExternalObjectID;
		strResGUID = this.OriginalSupportedObjectsTable.GetEntry(nIndex).ResolutionGUID;
		if (this.LatestSupportedObjectsTable.EntrySelected( strExternalObjectID ) == false)
		{
			// guard against duplicates, just in case
			if ( strList.indexOf(strExternalObjectID) < 0 )
			{
				// wrap each MSCMS GUID within "<Object>...</Object>"
				strList = strList + SCA_wrapXMLTag( IDS_XML_TAG_OBJECT, strResGUID, false);
			}
		}
	}
	
	// finally wrap the whole XML string within "<Objects>...</Objects>"
	return SCA_wrapXMLTag( IDS_XML_TAG_OBJECTS, strList, false );
}

// hook up method to the SupportedObjectsChanges Class
SupportedObjectsChanges.prototype.GetObjectsAddList = SupportedObjectsChanges_GetObjectsAddList;
SupportedObjectsChanges.prototype.GetObjectsRemoveList = SupportedObjectsChanges_GetObjectsRemoveList;

