/**
 ******************************************************************************
 *
 * Confidential Property of Documentum, Inc.
 * (c) Copyright Documentum, Inc. 2001.
 * All Rights reserved.
 * May not be used without prior written agreement
 * signed by a Documentum corporate officer.
 *
 ******************************************************************************
 *
 * Project        Lister
 * File           modal.js
 * Description    the javascript responsible for modal inline dialogs
 * Created on     19th November 2001
 * Tab width      3
 *
 ******************************************************************************
 *
 * PVCS Maintained Data
 *
 * Revision       $$
 * Modified on    $$
 *
 * Log at EOF
 *
 ******************************************************************************
 */

//----------------------------------------------------------------------------
// Initialisation

// is Netscape?
var isNav = (navigator.appName == "Netscape");

// initialise modal window and frame path
if (typeof(getTopLevelWnd().modalWnd) == "undefined")
{
   getTopLevelWnd().modalWnd = null;
}

//----------------------------------------------------------------------------
// Public interface

/**
 * Enter modal execution
 */
function beginModal()
{
   if (isModal() == false)
   {
      // set modal window and frame path
      getTopLevelWnd().modalWnd = window;

      // disable all other windows
      visitFrames("disableWindowCallback", getTopLevelWnd());

      // set resize handler
      window.onresize = onModalResize;

      // ensure focus is set on modal window
      window.focus();
   }
}

/**
 * End modal execution 
 */
function endModal()
{
  if (isModal() && getTopLevelWnd().modalWnd == window)
  {
     // clear modality (but only if the window is the modal window)
     getTopLevelWnd().modalWnd = null;
  }
}


//----------------------------------------------------------------------------
// Implementation

/**
 * return whether currently in modal mode
 */
function isModal()
{
   if (getTopLevelWnd().modalWnd != null && doesWindowExist(getTopLevelWnd().modalWnd))
   {
      return true;
   }

   getTopLevelWnd().modalWnd = null;
   return false;
}

/**
 * returns whether the passed window exists (in the current frameset)
 *
 * @param wnd  Window to test for
 *
 * @return Whether window exists
 */
function doesWindowExist(wnd)
{
   return _doesWindowExist(getTopLevelWnd(), wnd);
}
function _doesWindowExist(parentWnd, wnd)
{
   for (var i = 0; i < parentWnd.frames.length; i++)
   {
      var childWnd = parentWnd.frames[i];
      if (childWnd == wnd || _doesWindowExist(childWnd, wnd) == true)
      {
         return true;
      }
   }

   return false;
}

/**
 * Visits all the frames (depth first) calling the passed function on each
 * one where the function has the following signiture:
 *
 * function strFunction()
 *
 * @param funct      Name of function to call
 * @param wnd        Window to start traversal from (null for top level)
 */
function visitFrames(funct, wnd)
{
   // call function on dispatchable windows
   if (isDispatchableWindow(wnd))
   {
      // call callback via timer function
      if ( (typeof(eval("wnd." + funct)) != "undefined") &&
           (wnd != window) && (wnd.frames.length == 0) )
      {
         wnd.setTimeout(funct + "()", 1);
      }

      // traverse children
      for (var i = 0; i < wnd.frames.length; i++)
      {
         visitFrames(funct, wnd.frames[i]);
      }
   }
}

/**
 * Called when the modal window is resized
 */
function onModalResize(event)
{
   visitFrames("refreshWindowCallback", getTopLevelWnd());
}

/**
 * Disable window callback function
 */
function disableWindowCallback()
{
   if (typeof(window.document) != "undefined" && 
       typeof(window.document.body) != "undefined" &&
       typeof(window.ignoreModal) == "undefined")
   {
      // hide scrollbar (IE only)
      if (isNav == false)
      {
         window.oldScroll = window.document.body.scroll;
         window.oldScrollLeft = window.document.body.scrollLeft;
         window.oldScrollTop = window.document.body.scrollTop;

         if (window.document.body.scroll != "no")
         {
            window.document.body.scroll = "no";
         }

         if (window.document.body.scrollLeft != 0 || window.document.body.scrollTop != 0)
         {
            window.scrollTo(0,0);
         }
      }

      // get the url root (https get-around)
      var urlRoot = window.location.protocol + "//" + window.location.host + g_virtualRoot;

      // create div element containing fade
      var div = window.document.createElement("DIV");
	   div.id = "fade_modal";
      
      div.innerHTML = "<table background='" + urlRoot + "/wdk/modalFade.gif' border=0 width=100% " +
                        "cellspacing=0 cellpadding=0><tr><td height=" + screen.height + "></td></tr></table>";
      with (div.style)
      {
         // position div
		   position = "absolute";
         visibility = "visible";
         zIndex = "9";
         overflow = "hidden";
		   if (isNav)
		   {
   	      left = window.pageXOffset;
	   	   top = window.pageYOffset;
            width = window.innerWidth;
            height = window.innerHeight;
         }
         else
         {
     	      left = 0;
	   	   top = 0;
            width = window.document.body.clientWidth;
            height = window.document.body.clientHeight;
         }
      }
      window.document.body.appendChild(div);

      // set focus event handler
      window.oldOnfocusEventHandler = window.onfocus;
      window.onfocus = onIgnoreFocus;
      
      // disable any enabled select controls
      if (window.document.forms.length > 0)
      {
         for (j = 0; j < window.document.forms.length; j++)
         {
            var form = window.document.forms[j];
            for (i = 0; i < form.elements.length; i++)
            {
               var element = form.elements[i];
               if (element.type.indexOf("select") == 0 && element.disabled == false)
               {
                  if (typeof(window.disabledSelectCtrls) == "undefined")
                  {
                     window.disabledSelectCtrls = [];
                  }
                  window.disabledSelectCtrls[window.disabledSelectCtrls.length] = element;
                  element.disabled = true;
               }
            }
         }
      }

      // set timer to enable window when modal window has closed
      window.setTimeout("enableWindowCallback()", 250);
   }
}

/**
 * enable window callback function
 */
function enableWindowCallback()
{
   if (typeof(window.document) != "undefined" && typeof(window.document.body) != "undefined")
   {
      // call oneself in a while if currently modal
      if (isModal())
      {
         window.setTimeout("enableWindowCallback()", 250);
         return;
      }

      // hide element if it exists
      var div = window.document.getElementById("fade_modal");
      if (div != null)
      {
         // remove fade
         window.document.body.removeChild(div);

         // reset scrollbar/position (IE only)
         if (isNav == false)
         {
            if (window.oldScroll != "no")
            {
               window.document.body.scroll = window.oldScroll;
            }

            if (window.oldScrollLeft != 0 || window.oldScrollTop != 0)
            {
               window.scrollTo(window.oldScrollLeft, window.oldScrollTop);
            }
         }
      }

      // restore onfocus(...) event handler
      window.onfocus = window.oldOnfocusEventHandler;
      
      // enable any disabled select controls
      if (typeof(window.disabledSelectCtrls) != "undefined")
      {
         for (i = 0; i < window.disabledSelectCtrls.length; i++)
         {
            var element = window.disabledSelectCtrls[i];
            element.disabled = false;
         }
         window.disabledSelectCtrls = [];
      }
   }
}

/**
 * refresh window event handler
 */
function refreshWindowCallback()
{
   if (typeof(window.document) != "undefined" && typeof(window.document.body) != "undefined")
   {
      // hide element if it exists
      var div = window.document.getElementById("fade_modal");
      if (div != null)
      {
         // resize div
         with (div.style)
         {
		      if (isNav)
		      {
   	         left = window.pageXOffset;
	   	      top = window.pageYOffset;
               width = window.innerWidth;
               height = window.innerHeight;
            }
            else
            {
     	         left = 0;
	   	      top = 0;
               width = window.document.body.clientWidth;
               height = window.document.body.clientHeight;
            }
         }
      }
   }
}

/**
 * Called when a disabled window recieves focus
 */
function onIgnoreFocus(event)
{
   // push focus back to the modal window
   if (getTopLevelWnd().modalWnd != null)
   {
      getTopLevelWnd().modalWnd.focus();
   }
}


// end
