/*
* ******************************************************* *
*                                                         *
* The popup function opens a popup window:                *
*                                                         *
* popup(mylink, windowname, width, height, monitor, warn) *
*                                                         *
* mylink       required - the name of the file to display *
*              in the window or 'this'.                   *
* windowname   required - name of the popup window.       *
* width        optional (def 400) - window width.         *
* height       optional (def 200) - window height.        *
* monitor      optional (def false) - if true momitor for *
*              the popup window to close and reload the   *
*              main window when this occurs.              *
* warn         optional (def false) - if true produce an  *
*              alert that tells the user he must close an *
*              active popup before opening another one.   *
*                                                         *
* A sample calling sequence is:                           *
*   <a language="JavaScript" target="_blank"              *
*   href="PhotoGallery_Help.html"                         *
*   onClick="return popup(this,'pghelp',600,300)          *
*   <img src="images/HelpNormal.gif" border="0"           *
*   alt="Help" align="middle" width="48" height="24"></a> *
*                                                         *
* ******************************************************* *
*                                                         *
* This file should be included as follows:                *
* <script type="text/javascript"                          *
*      src="JavaScripts/popup.js"></script>               *
*                                                         *
* ******************************************************* *
*/

var href;
var intID;
var WinName = new Array();

function popup(mylink, windowname, width, height, monitor, warn)
{
  if (! window.focus) return true;	// For old browsers - let them open the window
  if (width == null) width = 400;	// Default the width if none is supplied
  if (height == null) height = 200;	// Default the height if none is supplied
  if (monitor == null) monitor = false;	// Default to no monitering if none is supplied
  if (warn == null) warn = false;	// Default to no warn if none is supplied
  if (typeof(mylink) == 'string')	// Handle a string or
    href = mylink;
  else					// Handle 'this'
    href = mylink.href;

  /*
  * ***************************************************** *
  * If this is warn mode - we need to look through all of *
  * our popup windows to insure that they are all closed. *
  * ***************************************************** *
  */
  if (warn)				// Do we need to check for an open popup window?
  {
    for (windowname in WinName)		// Step through every popup window (they could be closed)
    {
      popwin = WinName[windowname];	// Fetch the window reference
      if (window.popwin && !window.popwin.closed)
      {
        alert("        You already have a maintenance window open.\n"+
              "Please close that window before selecting another function.");
        window.popwin.focus();		// Put help window on top - helpfull on re-load
        return false;			// Pretend we opened the window
      }
    }
  }

  /*
  * ********************************************* *
  * We do not have an open window with warn mode. *
  * We need to open a new pop-up window.          *
  * ********************************************* *
  */
  popwin = window.open(href, windowname, 'width='+width+', height='+height+', resizable=yes, scrollbars=yes');
  WinName[windowname] = popwin;		// Save the window reference
  window.popwin.focus();		// Put help window on top - helpfull on re-load
  if (monitor)
  {
    intID = window.setInterval("checkClosed('"+windowname+"')", 500); // Check for window close every 500 ms
  }
  return false;				// Let browser know we opened the window
}

/*
* ***************************************************** *
* This function momitors for the closure of the window. *
* It is called from an interval timer every 500 ms.     *
* ***************************************************** *
*/
function checkClosed(windowname)
{
  // If the window is closed - our work is done
  popwin = WinName[windowname];		// Fetch the window reference
  if (window.popwin.closed)
  {
    window.clearInterval(intID);	// Stop the interval timer
    window.location.reload();		// Refresh our main window
    return;
  }
}

/*
* ************************************************ *
* This function closes the specifyed popup window. *
* ************************************************ *
*/
function closePopUp(windowname)
{
  popwin = WinName[windowname];		// Fetch the window reference
  if (window.popwin && !window.popwin.closed) window.popwin.close();
  return;
}

/*
* *************************************** *
* This function closes ALL popup windows. *
* *************************************** *
*/
function closeAllPopUp()
{
  for (windowname in WinName)
  {
    popwin = WinName[windowname];	// Fetch the window reference
    if (window.popwin && !window.popwin.closed) window.popwin.close();
  }
  return;
}

/*
* ******************************************* *
* This function closes our LAST popup window. *
* ******************************************* *
*/
function closeDep()
{
  if (window.popwin && !window.popwin.closed) window.popwin.close();
  return;
}