var mb_iframe = null;
var modalbox = null;
var ActiveContainer = null;
var ActiveContainerFrame = null;

function BOX_show(cid, w, h) // Display BOX_overlay    		
{ 
  modalbox = document.getElementById(cid);
  if( modalbox == null) { alert('Container not found.'); return;}

  if(w != null && typeof(w) != 'object') modalbox.style.width = w;
  if(h != null && typeof(h) != 'object') modalbox.style.height = h;
  
  if(document.getElementById('BOX_overlay') == null)
  {
    var bo = document.createElement('div');
    bo.id = 'BOX_overlay';
    bo.className = 'BOX_overlay';
    bo.innerHTML = '<iframe src="" style="position:absolute;left:0;top:0;width:100%;height:100%;z-index:100;" scrolling="no" frameBorder="0" allowTransparency="true" id="popupFrameMask" name="popupFrameMask" ></iframe>';
    document.body.appendChild(bo);
    
    mb_iframe = document.getElementById('popupFrameMask');
    
    addEvent(mb_iframe,'load', MaskFrame);
    mb_iframe.onload = MaskFrame;
  }
  
  BOX_layout();
  
 }
addEvent(window, "resize", BOX_layout);
addEvent(window, "scroll", BOX_layout);
window.onscroll = BOX_layout;

function MaskFrame()
{    
    if(mb_iframe != null)
        mb_iframe.contentWindow.document.write('<div style="background-color:#FCFCFC;width:100%;height:100%;"></div>');
}

function BOX_remove(cid) //Remove BOX_overlay
{
  if(cid != null && modalbox != null)
  {
  	if(modalbox.id != cid) return;
  }
  
  window.onscroll = null;
  window.onresize = null;

  var bo = document.getElementById('BOX_overlay')
  if (bo != null) bo.style.display="none";
  
  if (modalbox != null) {
  	 modalbox.style.display="none";
  	 modalbox = null;
  }
  
  if (mb_iframe != null) mb_iframe.style.display="none";
}  

function BOX_layout()
{
  if( modalbox == null) {return;}    
 	 	 	
	try
    {
        var theBody = document.getElementsByTagName("BODY")[0];
        var scrollLeft = parseInt(getScrollLeft(),10);
        var scrollTop = parseInt(getScrollTop(),10);        
    	
        var clientWidth = getViewportWidth();
        var clientHeight = getViewportHeight();	
        
        // Determine what's bigger, scrollHeight or fullHeight / width
	      if (clientHeight > theBody.scrollHeight) {
		    	popHeight = clientHeight;
	    	}
	    	else {
		    	popHeight = theBody.scrollHeight;
	    	}
    	
	    	if (clientWidth > theBody.scrollWidth) {
		    	popWidth = clientWidth;
	    	} 
	    	else {
		    	popWidth = theBody.scrollWidth;
	    	}
	    	    
        // Resize transparent object
        var bo = document.getElementById('BOX_overlay');
        if (bo != null)
        {
            bo.style.width = popWidth +'px';
            bo.style.height = popHeight +'px';
            bo.style.display = "block";
                                    
	        if(mb_iframe != null) mb_iframe.style.display = "block";
        }

        //Popup position
        //modalbox.style.position = 'relative';
        //modalbox.style.zIndex = 120;
        modalbox.style.display = "block";
        
        if (clientWidth >= modalbox.offsetWidth)
            modalbox.style.left = (scrollLeft+((clientWidth-modalbox.offsetWidth)/2)) + 'px';
        else if ((scrollLeft+modalbox.offsetWidth) < theBody.scrollWidth)
            modalbox.style.left =  scrollLeft + 'px';
        
        if (clientHeight >= modalbox.offsetHeight)
            modalbox.style.top = (scrollTop+((clientHeight-modalbox.offsetHeight)/2)) + 'px';
        else if ((scrollTop+modalbox.offsetHeight) < theBody.scrollHeight)
	        modalbox.style.top = scrollTop + 'px';
	    				
    }
    catch(ex)
	{
		window.status =	'Error: ' + ex.number + '; ' + ex.description;
    }
}

/**
 * X-browser event handler attachment and detachment
 * TH: Switched first true to false per http://www.onlinetools.org/articles/unobtrusivejavascript/chapter4.html
 *
 * @argument obj - the object to attach event to
 * @argument evType - name of the event - DONT ADD "on", pass only "mouseover", etc
 * @argument fn - function to call
 */
function addEvent(obj, evType, fn)
{
 if (obj.addEventListener){
    if(navigator.userAgent.indexOf("Firefox")>0 && evType=='load') {
 	    // document.addEventListener('DOMContentLoaded',fn, false); 	    
 	    obj.onload=fn;
 	}
    else
        obj.addEventListener(evType, fn, false);
    return true;
 } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
 } else {
    return false;
 }
}
function removeEvent(obj, evType, fn, useCapture)
{
  if (obj.removeEventListener){
    obj.removeEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.detachEvent){
    var r = obj.detachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
}

/**
 * Code below taken from - http://www.evolt.org/article/document_body_doctype_switching_and_more/17/30655/
 *
 * Modified 4/22/04 to work with Opera/Moz (by webmaster at subimage dot com)
 *
 * Gets the full width/height because it's different for most browsers.
 */
function getViewportHeight() 
{
	if (window.innerHeight!=window.undefined) return window.innerHeight;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	if (document.body) return document.body.clientHeight; 

	return window.undefined; 
}
function getViewportWidth() 
{
	var offset = 17;
	var width = null;
	if (window.innerWidth!=window.undefined) return window.innerWidth; 
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 
	if (document.body) return document.body.clientWidth; 
}

/**
 * Gets the real scroll top
 */
function getScrollTop() 
{
	if (self.pageYOffset) // all except Explorer
	{
		return self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		return document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		return document.body.scrollTop;
	}
}
function getScrollLeft() 
{
	if (self.pageXOffset) // all except Explorer
	{
		return self.pageXOffset;
	}
	else if (document.documentElement && document.documentElement.scrollLeft)
		// Explorer 6 Strict
	{
		return document.documentElement.scrollLeft;
	}
	else if (document.body) // all other Explorers
	{
		return document.body.scrollLeft;
	}
}
function autoIframe(frameId) 
{
    try 
    {
        frame = document.getElementById(frameId);        
        if (frame && !window.opera) {
          if (frame.contentDocument && frame.contentDocument.body.offsetHeight) {                            
                height = frame.contentDocument.body.offsetHeight + 16;
                
          } else if(frame.Document && frame.Document.body.scrollHeight) {                
                height = frame.Document.body.scrollHeight + 30;
          }
        }
        
        frame.style.height = height + 'px';
        document.getElementById(ActiveContainer).style.height = 25 + height + 'px';
        
    }
    catch(ex) {
        window.status = ex.message;
    }
}

/**************** ModalBox Display Methods *****************/
function ShowPopup (cid) 
{
    BOX_show(cid); 
}
function HidePopup (cid) 
{
    BOX_remove(cid); 
}
function ShowPopupFrame (ctrl, _url, w, h)
{
    ActiveContainer = ctrl + '.PopupContainer';
    ActiveContainerFrame = ctrl + '.PopupFrame1';
     
    BOX_show(ActiveContainer, w, h);
    var _iframe1 = document.getElementById(ActiveContainerFrame);
    if( _iframe1 != null) {
        _iframe1.src= _url;
        addEvent(_iframe1,'load',PopupFrameAutoFit);
    }
}
function PopupFrameAutoFit()
{
    if (window.parent && window.parent.autoIframe) {window.parent.autoIframe(ActiveContainerFrame);} 
}
function HidePopupFrame () 
{
    var tb = parent.document.getElementsByName('TB_closeWindowButton');     
  
    for(var i=0; i<tb.length; i++) {
        if (document.all) tb[i].click();
        else {
            var evt = document.createEvent('MouseEvents');
            evt.initEvent('click', true, false);
            tb[i].dispatchEvent(evt);
        }
   }
}
function ShowPopupDlg (ctrl, cid, w, h) 
{
    ActiveContainer = ctrl + '.PopupDlgContainer';
    
    BOX_show(ActiveContainer, w, h);
    var _div1 = document.getElementById(cid);
    if( _div1 != null) {
        document.getElementById('PopupContent').innerHTML = _div1.innerHTML;
    }
}
function HidePopupDlg () 
{
    BOX_remove(ActiveContainer); 
}