// Logout utility JavaScript Function
// File:    logout.js
// Author:  Eric Baars/Clinton Farleigh
// WebGENCAT:  3.2
// Version: 1.3
// History:
//    11 Jul 2001  Initial creation
//    13 Aug 2001  Fix bug: going to a screen above 2 above menu (opener not menu)
//                 caused logout. (Invalid session ID)...CJF
//     9 Jan 2002  Added conditions for logout when coming from browse or detail display...CJF
//    17 Jan 2002  Call closeWindow function to close are child window of current...CJF
//     4 Feb 2002  Added gWinToClose global variable that indicates window that close operation should
//                 be performed on.  The default is top...CJF 
//    17 May 2002  Added KeepSession arguement to keep session if browser cannot find parent of detail hyperlink.
//    14 Jun 2002  Changed top.opener.typeID to aWindow.typeID so that check first opened window.
//    17 Sep 2002  Added check for closeWindow function if keepSession=true...CJF
//     8 Aug 2008  Added a bunch of messy code to fix the back button problem...Derek Lam
//    24 Aug 2010  Logout no longer redirects ... the login action already handles logout...
//                 In opera, the submit onclick location redirect prevented the form from submitting...Derek Lam
//    20 Dec 2010  Added actiaveMenu...Derek Lam

// Global window attribute for page.  
// Set to force operation on other window (eg. parent).

function activateMenu() {
  var bAlert = true;
  try {
    var current = top;
    while (current = current.opener.top) {
      if (current.typeID == "Menu") {
        current.focus();
        bAlert = false;
        break;
      }
    }
  } catch (exception) {}
  if (bAlert) {
    alert("Menu window closed");
  }
}

var gWinToClose = null ;

function closeButton(logout) {
  var aWindow;

  if (gWinToClose == null) {
    gWinToClose = top;
  }

  if (gWinToClose.opener) {
    aWindow = gWinToClose.opener;
    try {
      while (!aWindow.top.typeID && aWindow.opener) {
        aWindow = aWindow.opener;
      }
    } catch (exception) {
      aWindow = null;
    }
  }

  var sURL = top.location.toString();
  var keepSession = sURL.indexOf( "KeepSession=1") != -1;

  if (gWinToClose.opener && aWindow && aWindow.top.typeID) {
    if (gWinToClose.closeWindow) {
      gWinToClose.closeWindow(true);
    } else {
      gWinToClose.close();
    }
  } else if (gWinToClose.typeID == "Menu") {
    // Close everything associated with the menu content frame.
    gWinToClose.parent.MenuContentFrame.closeWindow(false);
    gWinToClose.parent.MenuControlFrame.closeWindow(false);
//    24 Aug 2010  Logout no longer redirects ... the login action already handles logout...
//                 In opera, the submit onclick location redirect prevented the form from submitting...Derek Lam
//    gWinToClose.location = logout;
  } else if (gWinToClose.typeID == "Login") {
    gWinToClose.location = logout;
  } else {
    if(keepSession == true) {
      if (gWinToClose.closeWindow) {
        gWinToClose.closeWindow(true);
      } else {
        gWinToClose.close();
      }
    } else {
      // Assume link came from somewhere - go back there.
      gWinToClose.closeWindow(false);
      gWinToClose.location = logout;
      if (top.browserType == "ie" && gWinToClose.frames.length != 0) {
        gWinToClose.history.go(gWinToClose.frames.length * -1);
      } else {
        gWinToClose.history.back();
      }
      if (top.browserType == "nn" && gWinToClose.history.length == 1) {
        gWinToClose.close();
      } else if(gWinToClose.opener) {
        if (top.browserType == "ie") {
          if (gWinToClose.frames.length - gWinToClose.history.length == 1) {
            gWinToClose.close();
          }
        }
        if (gWinToClose.history.length == 0) {
          gWinToClose.close();
        }
      }
    }
  }
}
