
var popupPanels = "";
var topZIndex = 100;
var gMaxIntervalCount = 5;
var gIntervalCount = 0;

//used when there are IE specific stuff is not supported by Opera
var is_ie = ( (navigator.userAgent.toLowerCase().indexOf("msie") != -1) &&
	        (navigator.userAgent.toLowerCase().indexOf("opera") == -1) );

var hasMoved = false;	        
var icSetLeftTimer;
var icIFrameTimer = setInterval ("icRefreshIFrames()", 100);

// Handle the cases where the IFrame loses it's position when the
// browser window is resized.
window.onresize = icRefreshIFrames;

function icCloseProcess()
{
  if (icIFrameTimer)
  {
    clearInterval(icIFrameTimer);
    icIFrameTimer = null;
  }
}

function icRefreshIFrames()
{
  // Due to display timing issues, the dropdown will appear above some of the IFrames,
  // so we need to perform the refresh a few times.  Then we'll cancel the timer.
  if (gIntervalCount <= gMaxIntervalCount)
  {
	  //Reset iFrame size for any datasearch popups on page
	  var allIFrames = document.getElementsByTagName("iframe");
  	
	  for (var iframeInx=0; iframeInx < allIFrames.length; iframeInx++)
	  {

	    // We only need to deal with the iframes assoc. with the popups.
	    if (allIFrames[iframeInx].id.search(/PanelIFrame/) != -1)
	    {
	      var iframeID = allIFrames[iframeInx].id;
	      var panelID = iframeID.replace(/IFrame/, '');

	      try
	      {
	        panel = document.getElementById(panelID);
	        iframe = document.getElementById(iframeID);
	        if (iframe && panel)
	        {
	          if(!hasMoved)
	          {
	            icSetLeftTimer = setInterval("icSetLeft(panel)", 100);
	          }	          
	         
	          //calculate and set the width of the panel 
	          icSetPanelWidth(panel, iframe);
	        }
	        gIntervalCount += 1;
	      }
	      catch(er)
	      {
    	    icCloseProcess();
	        gIntervalCount = 0;
	      }
	    }
	  }
	}
	else
	{
	  icCloseProcess();
	  gIntervalCount = 0;
	}  
}

//The width calculation performed in this function is essential in IE7 for popup panels, and enhances the width calculation for IE6
function icSetPanelWidth(panel, iframe)
{
    if( is_ie )
    {	     
      var newWidth = 0; 
      var scrollPanelWidth = 0;
      var collection = panel.getElementsByTagName("span");
      var reDatapanel = new RegExp("(_Datapanel)$");
      var reDatalist = new RegExp("(_Datalist)$");
     
     //set the panel to a small width so that all elements render to their smallest so we can get the smallest panel width.
     panel.style.width = '150px';  
     
      //get the width of the widest datapanel and set it to newWidth if it's wider than the existing value 
      for (var i=0; i < collection.length; i++)
      {
         var element = collection[i];
         if (reDatapanel.test(element.id) && element.offsetWidth > newWidth)
         { 
            newWidth = element.offsetWidth;                        
         }                     
      }

      //get the width of the widest datalist and set it to newWidth if it's wider than the existing value 
      collection = panel.getElementsByTagName("table");
    	             
      for (var i=0; i < collection.length; i++)
      {
         element = collection[i];
         if (reDatalist.test(element.id) && element.offsetWidth > newWidth)
         { 
            newWidth = element.offsetWidth;
           
           //if some of the datalists contain a scrollbar (which is indicated by the parent div with no id) then use get its width if it's wider than the existing value
            if(element.parentElement.tagName.indexOf('DIV') != -1 && element.parentElement.id == ""  && element.parentElement.offsetWidth > scrollPanelWidth)
            {
              scrollPanelWidth = element.parentElement.offsetWidth;
            }
         }
      }
      
      //if we found a datalist with a scrollbar use its width
      if(scrollPanelWidth != 0)
        newWidth = scrollPanelWidth;                  
    
      //need to reset the maxWidth here so that the offsetHeight is not affected. 
      panel.style.maxWidth = '';
      panel.style.width = newWidth;	 
      iframe.style.width = newWidth;
      iframe.style.height = panel.offsetHeight;
      iframe.style.top = panel.style.top;
      iframe.style.left = panel.style.left;                    
    }
    else
    {
        //needed for Opera because it draws the iframe overtop of the panel and displays
        //a white square.        
        iframe.style.width = 0;
        iframe.style.height = 0;
        iframe.style.top = 0;
        iframe.style.left = 0;
        //in case we are not in IE make sure the maxWidth is the client's width  
        panel.style.maxWidth = document.documentElement.clientWidth; 
    }
}

function icSetLeft(panel)
{
    // document.documentElement.clientWidth leaves room for the scrollbar in all browsers
    var browserWidth = document.documentElement.clientWidth;
    var browserHeight = document.documentElement.clientHeight;
    
    // Position panel at the [ Search ] link, but reposition if it is off the right side
    if(panel.offsetLeft > 0 && panel.offsetLeft + panel.offsetWidth > browserWidth)    
    { 
        panel.style.left = (browserWidth - panel.offsetWidth - 5) + 'px';            
        clearInterval(icSetLeftTimer); 
    }
     
    // Prevent panel from going off the left edge
    if(panel.offsetLeft < 0)
        panel.style.left = 0;
        
    // Prevent browser from adjusting horizontal scroll position for search panels
    if(document.documentElement.scrollLeft > 0)
        document.documentElement.scrollLeft = 0;
    
    // Stop positioning after all search panels are done
    if(icIsChildSearch(panel) == false)
        hasMoved = true;   
}
	        

// Determines if the datasearch panel in question is a child of another datasearch.
// This will help us determine when all of the panels have been positioned.
function icIsChildSearch(panel)
{
    var obj = panel;
    
    while(obj.tagName != 'BODY')
    {
        obj = obj.parentNode; 
        
        if(obj.className.indexOf('DataSearch') > -1)
            return true;
    }
    
    return false;
}


function icGetAbsoluteLeft(oNode)
{
   var oCurrentNode=oNode;
   var iLeft=0;
   
   while(oCurrentNode.tagName!="BODY")
   {
      iLeft+=oCurrentNode.offsetLeft;
      oCurrentNode=oCurrentNode.offsetParent;
   }

   /* dont go beyond the width of the browser */
   var right = iLeft + oNode.nextSibling.offsetWidth;

   if (right > document.body.clientWidth)
      iLeft -= (right - document.body.clientWidth + 5);

   if (iLeft < 0)
      iLeft = 0;
        
   return iLeft;
}

function icGetAbsoluteTop(oNode)
{
   var oCurrentNode=oNode;
   var iTop=0;
   while(oCurrentNode.tagName!="BODY")
   {
      iTop+=oCurrentNode.offsetTop;
      oCurrentNode=oCurrentNode.offsetParent;
   }

   return iTop;
}



function icSetCapture(oPoint, e)
{   
	oPoint.style.position = "absolute";
	//only IE supports setCapture so make sure only IE executes it
	if (typeof oPoint.firstChild.setCapture != 'undefined')
	{
	    oPoint.firstChild.setCapture(true);
	}
	
    //Firefox and Opera do no support global events, so an event e is passed in for those two
    //browsers and event is used for IE
    var e = e || event;    
	oPoint.myLeft = e.clientX - oPoint.offsetLeft;
	oPoint.myTop = e.clientY - oPoint.offsetTop;
	oPoint.style.posLeft = oPoint.offsetLeft;
	oPoint.style.zIndex = topZIndex += 2;

    // Need to move IFrame in unison with the Panel in order to hide z-order agnostic dropdown
    var iframe = document.getElementById(oPoint.id+"IFrame");
	if (iframe && is_ie)
	{
	    //so IE can reposition the iframe
	    iframe.myLeft = oPoint.myLeft;
	    iframe.myTop = oPoint.myTop;
	    iframe.style.posLeft = oPoint.style.posLeft;
	    iframe.style.zIndex = oPoint.style.zIndex-1; 
	    iframe.style.width = oPoint.style.width;
	    iframe.style.height = oPoint.style.height;
	}
		
    oPoint.isMoving = hasMoved = true;    
}

function icMove(oPoint, e)
{
	if(oPoint.isMoving+"" == "true")
	{
	    //Firefox and Opera do no support global events, so an event e is passed in for those two
        //browsers and event is used for IE
	    var e = e || event;
	    
		//move the object with the mouse pointer by setting it's
		//coordinates as the original x,y to the mouse's x,y
		 oPoint.style.top = e.clientY - oPoint.myTop +"px";
		 oPoint.style.left = e.clientX - oPoint.myLeft +"px";
		//Don't let user drag outside the window.		
		if ( oPoint.offsetLeft < 0 ) oPoint.style.left = 0;
		if ( oPoint.offsetTop < 0 ) oPoint.style.top = 0;
		

        // Need to move IFrame in unison with the Panel in order to hide z-order agnostic dropdown
        var iframe = document.getElementById(oPoint.id+"IFrame");
	    if (iframe && is_ie)
	    {
	        //so IE can reposition the iframe
	        iframe.style.posTop = oPoint.style.posTop;
	        iframe.style.posLeft = oPoint.style.posLeft;
  	        iframe.style.width = oPoint.offsetWidth;
	        iframe.style.height = oPoint.offsetHeight;
	    }
	}
}

function icResize(oPanel)
{
  var iframe = document.getElementById(oPanel.id + 'IFrame');
  
	if (iframe && is_ie )
	{
	  //so IE can reposition the iframe
	  iframe.style.posTop = oPanel.offsetTop;
	  iframe.style.posLeft = oPanel.offsetLeft;
  	  iframe.style.width = oPanel.offsetWidth;
	  iframe.style.height = oPanel.offsetHeight;
	}
}

function icBringToTop(oPanel)
{
    // Make sure we're the same size.
    icResize(oPanel);
  
    var iframe = document.getElementById(oPanel.id + 'IFrame');
  
	if (iframe)
	{
  	  oPanel.style.zIndex = topZIndex += 2;
	  iframe.style.zIndex = oPanel.style.zIndex-1;
	}
}

function icReleaseCapture(oPoint)
{
    //only IE supports setCapture so make sure only IE executes it
	if (typeof document.releaseCapture != 'undefined')
	{
	    document.releaseCapture();
	}
	oPoint.isMoving=false;
}


function ic_browserType()
{
	var agt=navigator.userAgent.toLowerCase();

	if (agt.indexOf('netscape') != -1) 
	return 'ns'
	if (agt.indexOf('msie') != -1)
	return 'ie';
	
	return 'mo';  
}

//Used to prevent the DataSearch panel from opening
//when there are errors on the page.
function dSValid()
{
	if (typeof(Page_ClientValidate) == 'function') 
	{
		if (!Page_ClientValidate()) 
		{
			alert('You must correct all the errors on this page before you can access this search panel.');
			return false;
		}
	}
	
	return true;
		
}

