/**
 * POPUP Class Developed by Paul Rojas - Made In Colombia - http://www.deseoganarmas.com
 * Based on the code of Seth Banks
 * POPUP WINDOW CODE v5.1
 * Used for displaying DHTML only popups instead of using buggy modal windows.
 *
 * By Seth Banks (webmaster at subimage dot com)
 * http://www.subimage.com/
 *
 * Contributions by Eric Angel (tab index code) and Scott (hiding/showing selects for IE users)
 *
 * This code is free for you to use anywhere, just keep this comment block.

This is a singleton object, so it is created and initialized when the library is loaded.

The variable's names that start with the underscore(_) are private varibales that only can be accessed inside of the class.

Summary:
Private Variables
  _over            : This variable indicates if the cursor is over the div 'popupTitleBar2'. This value is set in the method isOver(), which is attached to the events onmouseover and onmouseout of the div 'popupTitleBar2'.
  _mdown           : Indicates if the mouse button was pressed, it is set in the methods MouseDown() and MouseUp(), and its value is used in the method MouseMove()
  _getTitle        : It is used to know if the windows's title is obtained from the document title, or if it is defined on the method showPopup()
  _myLoadingFile   : This is the URL of the file that is loaded the first time that the popupWindow object is created.
  _myBrowser       : This is an instance of the class Browser, defined in /js/browserDetection.js 
  _gPopupMask      : This is the reference to the div 'popupMask' which creates the trasparent layer that disable the objects behind the popupWindow.
  _gPopupContainer : It is a reference to the div that contains the popupWindow.
  _gPopupFrame     : This is the iframe that loads the URL defined in the method showPopWin()
  _gReturnFunc     : This is a function that will be executed before to close the popupWindow
  _gPopupIsShown   : This is a boolean variable that indicates if the popupWindow is visible(true) or hidden(false)
  _gHideSelects    : This variable is used to hide the select objects in IE 6 and previous versions, because they remain at the top of the div
  _gTabIndexes     : This is an array that stores the tabIndex value of the objects defined in the array _gTabbableTags
  _gTabbableTags   : Pre-defined list of tags we want to disable/enable tabbing into
  _movable        : Boolean variable that indicates if the popupWindow can be dragged around the screen
  _isModal         : Boolean variable that is used to show mask behind the popupWindow object
  _intTO1          : Integer variable that stores the value returned by the function window.setTimeout() which calls the method setPopTitle(). 
  _X               : Stores the position x when the mouse button is pressed
  _Y               : Stores the position y when the mouse button is pressed
  _theBody         : Reference to the document.body object or its equivalent object depending of the browser version

Public Variables:
  bgColor          : Defines the background color used by the popupMask layer, its value by default is gray (#CCCCCC)
  
History:
04.19.07 pjrr Added the capability to hide the scrollbars when the popup window is shown
06.13.07 pjrr Modified the cursor property of the title bar
*/

// Popup code
var popupWindow=new function(){
  var _over=false, _mdown=false, _getTitle=true;
  var _myLoadingFile='', _myBrowser=null, _gPopupControls=null;
  var _gPopupMask=null, _gPopupContainer=null, _gPopupFrame=null, _gPopupInner=null, _gReturnFunc;
  var _gPopupIsShown=false;var _gHideSelects=false;var _gTabIndexes=new Array();
  // Pre-defined list of tags we want to disable/enable tabbing into
  var _gTabbableTags=new Array("A","BUTTON","TEXTAREA","INPUT","IFRAME");
  var _movable=false;var _isModal=true;var _intTO1, _X, _Y;
  this.bgColor='CCCCCC'; var _theBody;
	var _selfRef=this, _titleBarHeight=18, _isIFrameMovable=null;
	this.theBody=null; 

  this.initPopUp=function(pBrowser){
    _myBrowser=new Browser();
    //alert(_myBrowser.AppBrowser);
    _gPopupMask=document.getElementById("popupMask2");
    _gPopupContainer=document.getElementById("popupContainer");
    _gPopupControls=document.getElementById("popupControls"); 
    _gPopupFrame=document.getElementById("popupFrame");
    _gPopupInner=document.getElementById("popupInner");

    if (_myBrowser.AppBrowserVer <= 6 && _myBrowser.AppBrowser=='IE') {
      _gHideSelects=true;
    }
  }

  this.showPopWin=function (url, width, height, returnFunc, movable, isModal, getTitle, windowCaption) {
    if (!_gPopupIsShown){
      _gPopupMask.backgroundColor=this.bgColor;
      _gPopupIsShown=true;
      _movable=movable;
      _isModal=isModal;
      _isIFrameMovable=null;
      if (arguments[7]) _getTitle=getTitle;
      if (_myBrowser.AppBrowser=='Mozilla' || _myBrowser.AppBrowser=='Netscape' || _myBrowser.AppBrowser=='Firefox') {
        document.onkeydown=keyDownHandler;
      }else{
        disableTabIndexes();
      }
      _gPopupMask.style.display=(_isModal ? "block" : "none");
      _gPopupContainer.style.display="block";
      if (document.documentElement && document.documentElement.scrollTop)
        _theBody=document.documentElement;
      else if (document.body)
        _theBody=document.body;

      this.theBody=_theBody;
      _theBody.style.overflow='hidden';
      centerPopWin(width, height);

      _titleBarHeight=parseInt(document.getElementById("popupTitleBar2").offsetHeight, 10);
      _gPopupContainer.style.width=width + "px";
      _gPopupContainer.style.height=(height+_titleBarHeight) + "px";
      _gPopupFrame.style.width=parseInt(document.getElementById("popupTitleBar2").offsetWidth, 10) + "px";
      _gPopupFrame.style.height=height + "px";

      if (_isModal) setOpacity(_gPopupMask, 40);
      if (url.trim() != ''){_gPopupFrame.src=url;}
      _gReturnFunc=returnFunc;
      // for IE
      if (_gHideSelects==true){
        hideSelectBoxes();
      }

      if (_getTitle)
        _intTO1=window.setTimeout("popupWindow.setPopTitle();", 600);
      else
        document.getElementById("popupTitle").innerHTML=windowCaption;

      if (_myBrowser.AppBrowser=='Opera') document.getElementById("popupTitleBar2").style.height='32px';

      addEvent(window, "resize", centerPopWin, false);
      addEvent(window, "scroll", centerPopWin, false);
      if (_movable){
        document.getElementById("popupTitleBar2").style.cursor='move';
        addEvent(document, "mousedown", MouseDown, false);
        setTimeout(checkIFrame, 2000);
      }
    }
  }

  function checkIFrame(){
    try{
      _gPopupFrame.contentWindow.document.onmousemove=_selfRef.MouseMove;
      _isIFrameMovable=true;
    }catch(err){_isIFrameMovable=false;}
  }

  function centerPopWin(width, height){
    if (_gPopupIsShown && !_over){
      if (width==null || isNaN(width)){
        width=_gPopupContainer.offsetWidth;
      }
      if (height==null || isNaN(height)){
        height=_gPopupContainer.offsetHeight;
      }

      var fullHeight=getViewportHeight();
      var fullWidth=getViewportWidth();

      var scTop=parseInt(_theBody.scrollTop,10);
      var scLeft=parseInt(_theBody.scrollLeft,10);

      var htmlheight=_theBody.scrollHeight;
      var htmlwidth=_theBody.scrollWidth;
      if (htmlheight < fullHeight) htmlheight=fullHeight;
      if (htmlwidth < fullWidth) htmlwidth=fullWidth;

      if (_isModal){
        _gPopupMask.style.height=htmlheight + "px";
        _gPopupMask.style.width=htmlwidth + "px";
        _gPopupMask.style.top="0px";
        _gPopupMask.style.left="0px";
      }else{
        _gPopupMask.style.height="0px";
        _gPopupMask.style.width="0px";
        _gPopupMask.style.top="0px";
        _gPopupMask.style.left="0px";
      }

      var _titleBarHeight=parseInt(document.getElementById("popupTitleBar2").offsetHeight, 10);

      _gPopupContainer.style.top=(scTop + ((fullHeight - (height+_titleBarHeight)) / 2)) + "px";
      _gPopupContainer.style.left= (scLeft + ((fullWidth - width) / 2)) + "px";
    }
  }

  /**
   * @argument callReturnFunc - bool - determines if we call the return function specified
   * @argument returnVal - anything - return value 
   */
  this.hidePopWin=function(callReturnFunc){
    var tmp=false;
    if (window.showConfirmWindow) tmp=showConfirmWindow();

    if (!tmp && _gPopupIsShown){
      _gPopupIsShown=false;
      restoreTabIndexes();
      if (_gPopupMask==null) return;
      
       if (_isModal){
        _gPopupMask.style.height="0px";
        _gPopupMask.style.width="0px";
        _gPopupMask.style.display="none";
      }
      _gPopupContainer.style.display="none";
      if (callReturnFunc==true && _gReturnFunc != null) _gReturnFunc(window.frames["popupFrame"].returnVal);
   
      _gPopupFrame.src=_myLoadingFile;
  
      if (_gHideSelects==true) displaySelectBoxes();
   
      window.clearTimeout(_intTO1);
      removeEvent(window, "resize", centerPopWin, false);
      removeEvent(window, "scroll", centerPopWin, false);
      if (_movable){
        removeEvent(document, "mousedown", MouseDown, false);
        removeEvent(document, "mouseup", MouseUp, false);
      }
      document.getElementById("popupTitleBar2").style.cursor='default';
      _theBody.style.overflow='scroll';
    }
  }
  
  this.setPopTitle=function(){
    if (_getTitle){
      if (window.frames["popupFrame"].document.title==null){
        _intTO1=window.setTimeout("popupWindow.setPopTitle();", 10);
      }else{
        document.getElementById("popupTitle").innerHTML=window.frames["popupFrame"].document.title;
      }
    }
  }
  
  function keyDownHandler(e){
    if (_gPopupIsShown && e.keyCode==9) return false;
  }
  
  function disableTabIndexes(){
    var i=0;
    for (var j=0; j < _gTabbableTags.length; j++) {
      var tagElements=document.getElementsByTagName(_gTabbableTags[j]);
      for (var k=0 ; k < tagElements.length; k++) {
        if (_myBrowser.AppBrowser=='IE' || (_myBrowser.AppBrowser=='Firefox' && _myBrowser.AppBrowserVer >= '1.5')) {
          _gTabIndexes[i]=tagElements[k].tabIndex;
          tagElements[k].tabIndex="-1";
        }else
          tagElements[k].disabled=true;

        i++;
      }
    }
  }
  
  function restoreTabIndexes(){
    var i=0;
    for (var j=0; j < _gTabbableTags.length; j++) {
      var tagElements=document.getElementsByTagName(_gTabbableTags[j]);
      for (var k=0 ; k < tagElements.length; k++) {
        if (_myBrowser.AppBrowser=='IE' || (_myBrowser.AppBrowser=='Firefox' && _myBrowser.AppBrowserVer >= '1.5')) {
          tagElements[k].tabIndex=_gTabIndexes[i];
          tagElements[k].tabEnabled=true;
        }else
          tagElements[k].disabled=false;

        i++;
      }
    }
  }
  
  /**
  * Hides all drop down form select boxes on the screen so they do not appear above the mask layer.
  * IE has a problem with wanted select form tags to always be the topmost z-index or layer
  * Thanks for the code Scott!
  */
  function hideSelectBoxes(){
    for(var i=0; i < document.forms.length; i++) {
      for(var e=0; e < document.forms[i].length; e++){
        if(document.forms[i].elements[e].tagName=="SELECT") {
          document.forms[i].elements[e].style.visibility="hidden";
        }
      }
    }
  }
  
  /**
  * Makes all drop down form select boxes on the screen visible so they do not reappear after the dialog is closed.
  * IE has a problem with wanted select form tags to always be the topmost z-index or layer
  */
  function displaySelectBoxes(){
    for(var i=0; i < document.forms.length; i++) {
      for(var e=0; e < document.forms[i].length; e++){
        if(document.forms[i].elements[e].tagName=="SELECT") {
          document.forms[i].elements[e].style.visibility="visible";
        }
      }
    }
  }

  this.isOver=function(over){
    _over=over;
  }

  MouseDown=function(e){
    if (getEventSource(e)=='popupControls' || getEventSource(e)=='imgClose' || _isIFrameMovable==null) return false;
    if (_over || getEventSource(e)=='popupTitleBar2') {
      _mdown=true;
      var posX;var posY;
      if(document.all){
        posY=window.event.clientY+_theBody.scrollTop;
        posX=window.event.clientX+_theBody.scrollLeft;
      }else{
        posY=e.clientY+window.scrollY;
        posX=e.clientX+window.scrollX;
      }
      var st=_gPopupContainer.style;
      _X=posX-parseInt(st.left);
      _Y=posY-parseInt(st.top);
      if (!_isIFrameMovable){
        _gPopupFrame.style.visibility='hidden';
        _gPopupInner.style.border='2px dotted silver';
      }
      addEvent(document,"mousemove",_selfRef.MouseMove);
      addEvent(document,"mouseup",MouseUp);
    }
  }

  this.MouseMove=function(e){
    if (_gPopupIsShown && _mdown){
      var posX;var posY;
      if(document.all){
        try{
          posY=window.event.clientY+_theBody.scrollTop;
          posX=window.event.clientX+_theBody.scrollLeft;
        }catch(err){
          posY=_gPopupFrame.contentWindow.event.clientY+_selfRef.theBody.scrollTop;
          posX=_gPopupFrame.contentWindow.event.clientX+_selfRef.theBody.scrollLeft;
				}
      }else{
        posX=e.pageX;
        posY=e.pageY;
      }
      tmpSource=getEventSource(e);
      var st=_gPopupContainer.style;
      if (tmpSource==''){
        posX+=parseInt(st.left);
        posY+=parseInt(st.top)+_titleBarHeight+5;
      }

      st.left=(posX-_X)+"px";
      st.top=(posY-_Y)+"px";
      return stopEvent(e);
    }
  }

  MouseUp=function(e) {
    removeEvent(document, "mousemove", _selfRef.MouseMove, false);
    removeEvent(document, "mouseup", MouseUp, false);
    _mdown=false;
    _over=false; 
    if (!_isIFrameMovable){
      _gPopupFrame.style.visibility='visible';
      _gPopupInner.style.border='2px solid black';
    }
  }

  this.hideControls=function(bool) {
    _gPopupControls.style.visibility=(bool?'hidden':'visible');
  }

  stopEvent=function(ev){
    try{
      ev||(ev=window.event);
      if(document.all){
        ev.cancelBubble=true;
        ev.returnValue=false;
      }else{
        ev.preventDefault();
        ev.stopPropagation();
      }
    }catch(err){}
    return false;
  }

  this.writeControls=function (loadingFile) {
    _myLoadingFile=loadingFile;
    var s='<div id="popupMask2"></div><div id="popupContainer"><div id="popupInner">';
    s+='<div id="popupTitleBar2"  ondblclick="void(0);" onmouseover="javascript: popupWindow.isOver(true);"  onmouseout="javascript: popupWindow.isOver(false);" style="cursor:'+(_movable?'move':'default')+';height:'+_titleBarHeight+'px;">';
    s+='<div id="popupTitle" ></div>';
    s+='<div id="popupControls"></div></div>';
    s+='<iframe src="'+_myLoadingFile+'" style="width:100%;height:100%;background-color:transparent;" scrolling="auto" frameborder="0" allowtransparency="true" id="popupFrame" name="popupFrame" width="100%" height="100%"></iframe></div></div>';
    document.write(s);
  }
}

function setOpacity(object, opacity) {
  var b=new Browser();

  if (b.AppBrowser=="Opera") {
    object.style.backgroundImage= 'url("/images/maskBG.png")'; // For Opera, etc.
    object.style.backgroundRepeat='repeat';
  }else{
    object.style.opacity=(opacity / 100);
    object.style.MozOpacity=(opacity / 100);
    object.style.KhtmlOpacity=(opacity / 100);
    object.style.filter="alpha(opacity=" + opacity + ")";
  }
}