﻿//Public settings.  If necessary you can changes this on the page.
var PopupUseTop    = false;
var PopupDefWidth  = 400;
var PopupDefHeight = 300;
var PopupTimeout = 5550;

//Private variables
var popupWindows = new Array();
var popupEventAdded = false;
var popupAutoClose = new Array();
var popupWinUrls = new Array();

//Add basePath for MAC bug.  Basically, get the path from current page 
//and use it to refernce popup window html
var basePopupPath;

//First check for a ? in the path and get rid of it and everything to the right
idx = location.href.indexOf("?")
if (idx < 0) {
  basePopupPath = location.href;
} else {
  basePopupPath = location.href.substring(0,idx);
}

//Now truncate up to last slash
basePopupPath = basePopupPath.substring(0,basePopupPath.lastIndexOf("/")+1)

/* Function - popupWindow(winName,winUrl,options)

   Parameters
    
     winName    - Unique name for this popup.  Windows will be internally tracked by winName
                  and reused when the names match.
                  
     winUrl     - Url for page to load in popup.
     
     options    - A set of option for controlling the popup.  See examples below for usage.

       options.Scrollbar   - Determines if scrollbar is shown or not.  Default = true;
       options.Titlebar    - Determines if titlebar is shown or not.  Default = false;
       options.Statusbar   - Determines if statusbar is shown or not.  Default = true;
       options.Width       - Width of window.  Default = PopupDefWidth;
       options.Height      - Height of window. Default = PopupDefHeight;
       options.Left        - Left position of window. This is a pixel location, but if not set will default to centered (-1) if width is specified.
       options.Top         - Top position of window.  This is a pixel location, but if not set will default to centered (-1) if height is specified.
       options.AutoClose   - This will determine if window will close automatically when the page is unloaded.  The default is true.
       options.Resizeable  - This will determine if the window is resizable.  Default is true.
       options.BringToFront - If this option is set to true and the window already exists with the same url it will not refresh and
                              just be brought to the front.  Default is false.
       options.Toolbar      - This will display the toolbar.  Default = false
       options.Location     - This will display the location bar.  Default = false
       options.Menubar      - CA Determines presence of the menu bar. Default = false. 
       
  Returns
    
      A pointer to the popup window created or referred to by winName.
       
  Examples 
  
      popupWindow("Video",url)  //This takes all the default options
      
      popupWindow("Explore",url,{AutoClose:false,Resizable:false}) 
      
      popupWindow("XYZ",url,{Scrollbar:false,Top:0,Left:0})
   
*/

function popupWindow(winName,winUrl,options) {
  

  //Set up defaults
  //CED : Add toolbar and location
  var scrollbar  = true;
  var toolbar    = false;
  var location   = false;
  var titlebar   = false;
  var statusbar  = false;  
  var width      = PopupDefWidth;
  var height     = 0;
  var left       = -1;
  var top        = -1;
  var autoclose  = true;
  var resizeable = true;
  var autoHeight = false;
  var bringToFront = false;
  var menubar    = false;
  
  var win;
  
  //CED : Add toolbar and location
  if (typeof(options) != "undefined") {
    if (typeof(options.Toolbar) != "undefined") toolbar = options.Toolbar;
    if (typeof(options.Location) != "undefined") location = options.Location;
    if (typeof(options.Scrollbar) != "undefined") scrollbar = options.Scrollbar;
    if (typeof(options.Titlebar)  != "undefined") titlebar = options.Titlebar;    
    if (typeof(options.Statusbar) != "undefined") statusbar = options.Statusbar;        
    if (typeof(options.Width)     != "undefined") width =  options.Width;
    if (typeof(options.Height)    != "undefined") height = options.Height;
    if (typeof(options.Left)      != "undefined") left = options.Left;
    if (typeof(options.Top)       != "undefined") top = options.Top;
    if (typeof(options.AutoClose) != "undefined") autoclose = options.AutoClose;
    if (typeof(options.Resizable) != "undefined") resizeable = options.Resizeable;
    if (typeof (options.BringToFront) != "undefined") bringToFront = options.BringToFront;
    if (typeof (options.Menubar) != "undefined") menubar = options.Menubar;
  }
  
  if (height == 0) {
    autoHeight = true;
    height = PopupDefHeight;
  }

  //alert('screenheight: '+screen.height+' screenavailw: '+screen.availHeight+' height: '+height);
  //alert('screenwidth: '+screen.width+' screenavailw: '+screen.availWidth+' width: '+width);

  var HeightBuffer = 35;
  var WidthBuffer = 10;

  // Comment 6994
  // Betsy's PC is not including her taskbar height and then covering the taskbar
  // I could only repeat this by setting taskbar to not auto-hide and not always on top  
  // Only do this nudge when task bar is not accounted for in availHeight
  if (!autoHeight && ( (screen.availHeight == screen.height) || (screen.availHeight-height<HeightBuffer) ))
  {
    if (screen.availHeight<height)
    {
      top = 0;
      height = screen.availHeight - (HeightBuffer*3);
    }
    else if (screen.availHeight-height<HeightBuffer)
    {
      top = 0;
      height -= (HeightBuffer*2);
    }
  }

  //if (!autoHeight && (screen.availWidth-width<WidthBuffer))
  //In an attempt to fix comment 8118 we decided to limit the width
  //of all popups to the availWidth.  This code was already here 
  //excpet it was not doing it if autoHeight was true.  I'm removing that
  //since I'm not sure why it's there in the first place.  MAD 11/21/06
  if ((screen.availWidth-width)<WidthBuffer)
  {
    left = 0;
    width = screen.availWidth - WidthBuffer;
    /*else if (screen.availWidth-width<WidthBuffer)
    {
      left = 0;
      width -= (WidthBuffer*3);
    }*/
  }
  
  //Build options string using an array  
  //CED : change yes and no to 1 and 0 for firefox
  //CED : Add toolbar and location
var opts = new Array();
  if (scrollbar) opts.push("scrollbars=1");
  if (toolbar) opts.push("toolbar=1");
  if (location)
      opts.push("location=1");
  else
      opts.push("location=0");
  if (statusbar) 
     opts.push("status=1");  
  else
     opts.push("status=0");
  if (titlebar) opts.push("titlebar=1");
  if (menubar) opts.push("menubar=1");
  if (resizeable) opts.push("resizable=1");
  opts.push("height="+height);
  opts.push("width="+width);
  if (left < 0) left = (screen.availWidth - width)/2;
  if (top  < 0) top  = (screen.availHeight -height)/2;
  opts.push("left="+left);
  opts.push("top="+top);
  var optsstr = opts.join(",");
  
  //Popup window and bring it to the front.
  var winMain;
  
  if (PopupUseTop) {
    winMain = top.window
  } else {
    winMain = window;
  }

  var isAbsoluteUrl = false;
  if (winUrl.indexOf('http') == 0)
      isAbsoluteUrl = true;
  
  //This doesn't seem right and is messing up absolute URLs.
  //if ((winUrl.charAt(0) != "/") && (winUrl.search(/http%3A/) != -1))
  if ((winUrl.charAt(0) != "/") && ! isAbsoluteUrl) {
    winUrl = basePopupPath + winUrl;
}

//I moved these lines of code that manipulate winUrl, this way when we compare again cached version, it will be the same.
if (!isAbsoluteUrl && autoHeight) {
    winUrl = popups_replaceParam(winUrl, "autoh", "yes");
} else {
    winUrl = popups_removeParam(winUrl, "autoh");
}

if (!isAbsoluteUrl && (left >= 0 || top >= 0)) {
    winUrl = popups_replaceParam(winUrl, "centerwin", "yes");
} else {
    winUrl = popups_removeParam(winUrl, "centerwin");
}


  if (bringToFront) {
    win = getPopup(winName);
    if (win) {
      var wUrl = popupWinUrls[winName];
      if (wUrl && wUrl == winUrl) {
        win.focus();
        return win;
      }
    }
  }
  
  //Close PopupWindow if it already exists
  closePopup(winName);

  win = winMain.open(winUrl,winName,optsstr);

  //setTimeout(function () { _hasPopupBlocker(win) }, PopupTimeout);

  /** This code had timing issues on some machines I switched it to use parameters  
  try {
      win.AutoHeight = autoHeight;
      if (left >= 0 || top >= 0) {
        win.CenterWindow = true;
      } else {
        win.CenterWindow = false;
      }
  } catch(ex)
  {
  }
  **/
  
  //Add to global list
  popupWindows[winName] = win;
  popupAutoClose[winName] = autoclose;
  popupWinUrls[winName] = winUrl;

  //Bring to front.  
  win.focus();
  
  if (autoclose && ! popupEventAdded) {
    if(winMain.attachEvent) { 
      winMain.attachEvent("onunload",PopupMainWindowUnloaded); 
    } else if(winMain.addEventListener) { 
      winMain.addEventListener("unload",PopupMainWindowUnloaded, false); 
    }
  }
  
  return win;

}

function getHeight(poppedWindow) {
    var myHeight = 0;
    if (typeof (poppedWindow.innerWidth) == 'number') {
        //Non-IE
        myHeight = poppedWindow.innerHeight;
    } else if (poppedWindow.document.documentElement && (poppedWindow.document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        myHeight = poppedWindow.document.documentElement.clientHeight;
    } else if (poppedWindow.document.body && (poppedWindow.document.body.clientHeight)) {
        //IE 4 compatible
        myHeight = poppedWindow.document.body.clientHeight;
    }
    return myHeight;
}

function _hasPopupBlocker(poppedWindow) {
    var result = false;
    try {
        if (typeof poppedWindow == 'undefined') {
            // Safari with popup blocker... leaves the popup window handle undefined
            result = true;
        }
        else if (poppedWindow && poppedWindow.closed) {
            // This happens if the user opens and closes the client window...
            // Confusing because the handle is still available, but it's in a "closed" state.
            // We're not saying that the window is not being blocked, we're just saying
            // that the window has been closed before the test could be run.
            result = false;
        }
        else if (poppedWindow && getHeight(poppedWindow) > 0) {
            // This is the actual test. The client window should be fine.
            result = false;
        }
        else {
            // Else we'll assume the window is not OK
            result = true;
        }

    } catch (err) {
        //if (console) {
        //    console.warn("Could not access popup window", err);
        //}
        result = true;
    }

    if (result) {
        alert("You seem to have a popup blocker installed.  Please disable it for this site.");
    }
}

function PopupMainWindowUnloaded() {
  for (var winName in popupWindows) {
    var win = popupWindows[winName];
    if (win && typeof(win) == "object" && ! win.closed && popupAutoClose[winName]) {
      popupWindows[winName] = null;
      popupAutoClose[winName] = false;
      popupWinUrls[winName] = null;
      win.close();
    }
  }
}

function getPopup(winVar) {
  if (typeof(winVar) == "string") {
    winVar = popupWindows[winVar];
  }
  if (winVar && ! winVar.closed) {
   return winVar;
  } else {
    return null;
  }
}

//closePopup can be called with a name of window reference.
function closePopup(winVar)
{
  if (typeof(winVar) == "string") {
    winVar = popupWindows[winVar];
  }
  if (winVar && ! winVar.closed)
   winVar.close()
}

function focusPopup(winVar) {
  if (typeof(winVar) == "string") {
    winVar = popupWindows[winVar];
  }
  if (winVar && ! winVar.closed)
   winVar.focus();
}


// Remove parameter from reqstr
function popups_removeParam(reqstr,param)
{
  var evstr, dest;
  dest = new String(reqstr); 

  // strip out existing parameter
  //FOUND A BUG MAD 10/25/00
  //Routine did not handle "param=" at end of string
  //evstr = "dest = dest.replace(/[\\?|\\&]"+param+"=[^\\&]+/ig,\"\");"

  //FOUND A BUG AAL 03/27/03
  //Routine didn't handle removing empty param
  //evstr = "dest = dest.replace(/[\\?|\\&]"+param+"=(([^\\&]+)|\s*$)/ig,\"\");"  
  
  evstr = "dest = dest.replace(/[\\?|\\&]"+param+"=(([^\\&]*)|\s*$)/ig,\"\");"
  eval(evstr);

  // if we stripped out the first param, make the following param have the ampersand
  if (dest.indexOf("?")==-1 && dest.indexOf("&")!=-1)
    dest = dest.replace(/\&/,"?");

  return dest;
}


// If parameter exists in reqstr, change its value
// If not, add it
function popups_replaceParam(reqstr,param,value)
{
  var evstr, dest;
  dest = new String(reqstr); 

  // strip out existing parameter
  dest = popups_removeParam(dest,param)

  if (dest.indexOf("?")>=0)
    dest=dest+"&"+param+"="+value;
  else
    dest=dest+"?"+param+"="+value;

  return dest;
}

// CA Obtains the collection of popup windows (e.g., for name or popup traversal)
function getPopupWindows() {
    return popupWindows;
}


function openerHasFunction(functionName) {
    try {
        if (window.opener && eval("window.opener." + functionName))
            return true;
    } catch (e) {
    }
    return false;
}


