//  ------------------------------------------------------------
//  PopupManager.js
//  ------------------------------------------------------------
//  VCS INFO:
//  
//  $Revision:   1.14  $
//  $Date:   20 Dec 1999 14:17:38  $
//  $Author:   shsieh  $
//  $Workfile:   PopupManager.js  $
//   ------------------------------------------------------------
//   INCLUDE DEPEDENCY:
//   WSCAPI/InitPage.js
//  ------------------------------------------------------------
//  All Rights Reserved.  Copyright (c) 1988-1999 FileNET Corp.
//  ------------------------------------------------------------

function IDMWSC_PopupManager_findPopupsArray()
{
	var popupsArray = null;
	
	if (top.IDMWSC_PopupManager_popups != null)
	{
		popupsArray = top.IDMWSC_PopupManager_popups;
	}
	else
	{
		// Go up the chain of window hierarchy, looking for the topmost
		// window containing the array of popups.
		var w = top;
		while (1)
		{
			if ((typeof(w.topOpener) != "undefined") && !(w.topOpener.closed))
				w = w.topOpener;
			else
				break;

			if (typeof(w.IDMWSC_PopupManager_popups) == "unknown")
				break;
			else if (w.IDMWSC_PopupManager_popups != null)
				{
					popupsArray = w.IDMWSC_PopupManager_popups;
					break;
				}
		}
	}

	return popupsArray;
}

function IDMWSC_PopupManager_findPopup(popupsArray, popupName)
{
	// Use the array to find the popup window with the given name.
	var curPopup = null;
	var i;
	var curIndex;
			
	for (i=0; i<popupsArray.length; i++)
	{
		if (popupsArray[i])
		{
			if (popupsArray[i].closed)
				//popupsArray.splice(i, 1);  only works for NS4.x, not IE4.0?
				popupsArray[i] = null;
			else if (popupsArray[i].name == popupName)
			{
				curPopup = popupsArray[i];
				break;  // found existing window
			}
		}
	}
	return curPopup;
}

// This function is used to work around a problem in Windows 95/98
// where the window.closed check may cause an error to occur in
// IDMWSC_PopupManager_findPopup, when it is called.  This function
// can be called to null out the array element identifying the same
// popup window without checking for the .closed property.
// It is currently used by VersionList.asp and CopyMove.asp when
// these pages are unloading.  This is a workaround for FR 15078/CR 10590.
function IDMWSC_PopupManager_unloadPopup(popupWin)
{
	var popupsArray = IDMWSC_PopupManager_findPopupsArray();
	if (popupsArray != null)
	{
		// Find the popup window in the array that is the same
		// as popupWin and just set that array element to null.
		var i;
		for (i=0; i<popupsArray.length; i++)
		{
			if (popupsArray[i] == popupWin)
			{
				//alert("found matching popup, setting array elem to null");
				popupsArray[i] = null;
				break;  // found matching window, done.
			}
		}
	}
}

function IDMWSC_PopupManager_open(popupType, popupName, popupURL, popupFeatures, reloadPopupURL)
{
	// If popup type is "Viewer" and browser type is IE, then open new
	// popup window WITHOUT caching it in any popup collection.  Due to
	// a security restriction set on IE browsers, after the IE Viewer
	// popup (running the IDMView app in-place) is opened, we can not
	// refer to this popup window object again.  Otherwise, an 'access denied'
	// scripting error will be reported.
	if (popupType == "Viewer")
	{
		if (IDMWSC_IEBrowser)
		{
			curPopup = open(popupURL, popupName, popupFeatures);
			//curPopup.focus();
			return curPopup;
		}
	}

	var curPopup = null;
	var popupsArray = IDMWSC_PopupManager_findPopupsArray();

	if (popupsArray == null)
	{
		// No popup collection is available.  Open new popup window WITHOUT
		// caching it in any popup collection.
		curPopup = open(popupURL, popupName, popupFeatures);
		// Set focus in case a popup with the same name already exists.
		curPopup.focus();
	}
	else
	{
		// Assign the window already opened for this popupName, if any
		curPopup = IDMWSC_PopupManager_findPopup(popupsArray, popupName);

		if (curPopup)
		{
			curPopup.focus();
			if (reloadPopupURL)
				curPopup.document.location.href = popupURL;
		}
		else
		{
			// open a new popup
			curPopup = open(popupURL, popupName, popupFeatures);
			curPopup.topOpener = curPopup.opener.top;

			// Find next available slot in array to save curPopup.
			// If none found, then append to end of array.
			var bFound = false;
			for (i=0; i<popupsArray.length; i++)
				if (popupsArray[i] == null)
				{
					popupsArray[i] = curPopup;
					bFound = true;

					// WORK AROUND FOR FR 9147: this is here to work around problems in Win98/IE 5.  If we don't reference
					// the window ref here, we get script errors if we access .closed when the window was closed
					popupsArray[i];

					break;  // found it
				}
			if (!bFound) { // append to array
				popupsArray[popupsArray.length] = curPopup;

				// WORK AROUND FOR FR 9147: this is here to work around problems in Win98/IE 5.  If we don't reference
				// the window ref here, we get script errors if we access .closed when the window was closed
				popupsArray[popupsArray.length-1];

			}
		}
	}

	return curPopup;
}

function IDMWSC_PopupManager_generatePopupNamePrefix(libraryName)
{
	// Form the popup window prefix.  It has the form of IDMWSC_PopupManager_[<libraryName>_],
	// where the separators in the library name, if specified, are replaced by "_".
	if (libraryName.length == 0)
		return "IDMWSC_PopupManager_";
	else
		return "IDMWSC_PopupManager_" + IDMWSC_replaceSeparatorsWithUnderscores(libraryName) + "_";
}

function IDMWSC_PopupManager_closeLibraryPopups(libraryName)
{
	// Closes all popups belonging to the specified libraryName.
	// If libraryName is empty, all popups will be closed.

	// Retrieve global array of popups
	var popupsArray = null;
	if (top.IDMWSC_PopupManager_popups != null)
		popupsArray = top.IDMWSC_PopupManager_popups;
	else
	{
		// Traverse up the chain of opener windows to find array
		var topWindow = top.opener;
		while (topWindow != null)
		{
			if (topWindow.IDMWSC_PopupManager_popups != null)
			{
				popupsArray = topWindow.IDMWSC_PopupManager_popups;
				break;
			}
			topWindow = topWindow.opener;
		}
	}

	if (popupsArray != null)  // array found
	{
		// Replace separators in libraryName with "_"'s, if necessary
		var convertedLibName = "";
		if (libraryName.length > 0)
			convertedLibName = IDMWSC_replaceSeparatorsWithUnderscores(libraryName);

		// Loop through array to close necessary popups
		var i;
		var popupWin;
		for (i=0; i<popupsArray.length; i++)
		{
			popupWin = popupsArray[i];
			if (popupWin)
			{
				if (popupWin.closed)
				{
					//popupsArray[i].splice(i, 1);  only works for NS4.x, not IE4.0?
					popupsArray[i] = null;
				}
				else if (-1 != popupWin.name.indexOf(convertedLibName))
				{
					popupWin.close();
					popupsArray[i] = null;
				}
			}
		}
	}
	// else there is no popup collection.  Nothing to close.
}

function IDMWSC_PopupManager_closeAll()
{
	IDMWSC_PopupManager_closeLibraryPopups("");
}

function IDMWSC_replaceSeparatorsWithUnderscores(srcString)
{
	// This function replaces all non-alphanumeric and non "_" characters
	// in the srcString to '_'.
	var newStr = srcString;	
	newStr = newStr.replace(/\W/g, "_");
	return newStr;
}
