function MM_openNewWindow(theURL,winName,features,targetWidth,targetHeight) 
{ 
 var targetLeft;
 var targetTop;
 targetLeft = (window.screen.width) - (targetWidth+15); //half the screen width minus half the new window width (plus 5 pixel borders).
 targetTop  = (window.screen.height/2) - (targetHeight+15); //half the screen height minus half the new window height (plus title and status bars).
 

 var win2 = window.open(theURL,winName,features +",width="+targetWidth+",height="+targetHeight+",left="+ targetLeft+ ",top=" + targetTop+ ",screenX=" + targetLeft + ",screenY=" + targetTop);
 win2.focus();
}


function MM_openNewWindowCentered(theURL,winName,features,targetWidth,targetHeight) 
{ 
 var targetLeft;
 var targetTop;
 targetLeft = (window.screen.width/2) - (targetWidth/2); 
 targetTop  = (window.screen.height/2) - (targetHeight/2);
 

 var win2 = window.open(theURL,winName,features +",width="+targetWidth+",height="+targetHeight+",left="+ targetLeft+ ",top=" + targetTop+ ",screenX=" + targetLeft + ",screenY=" + targetTop);
 win2.focus();
}



function MM_openNewWindowOffScreen(theURL,winName,features,targetWidth,targetHeight) 
{ 
 var targetLeft;
 var targetTop;
 targetLeft = window.screen.width - 1;
 targetTop  = window.screen.height - 1;

 targetWidth = 10;
 targetHeight = 10;
 

 var win2 = window.open(theURL,winName,features +",width="+targetWidth+",height="+targetHeight+",left="+ targetLeft+ ",top=" + targetTop+ ",screenX=" + targetLeft + ",screenY=" + targetTop);
 win2.focus();
}


function BEI_FindForm(formName)
{
	if( document.layers )
	{
		for( j = 0; j < document.forms.length; j++)
		{
		  if( formName == document.forms[j].name )
                  {
  		    return document.forms[j];
                  }
                }




		for( j = 0; j < document.layers.length; j++)
		{
		  for( i = 0; i < document.layers[j].document.forms.length; i++)
		  {
 		     if( formName == document.layers[j].document.forms[i].name )
			return document.layers[j].document.forms[i];
		  }
		}
	}
	else
	{
	    return document.forms[formName];
   	}
	
	return null;
}




function addCookie(theCookie)
{
// Change this when we need to.  Right now it is only used to track the my links window.
	document.cookie = theCookie;
}



function readCookie(theCookie)
{
	var allCookies = document.cookie;
	var pos = allCookies.indexOf(theCookie + "=");
	
	if( pos != -1 )
	{
		var start = pos + theCookie.length + 1;
		var end = allCookies.indexOf(";", start);
		
		if( end == -1 )
		{
			end = allCookies.length;
		}
		
		
		var value = allCookies.substring(start, end);
		value = unescape(value);
		
		return value;
	}
	else
	{
		return "";
	}
}




function openCloseDiv(a,b,c)
{
	//var source;
	//source = window.event.srcElement;
	if (a.style.display =="")
	{
		a.style.display = "none";
		//b.src="images/rarrow.gif";
		c.value = 0
	}
	else
	{
		a.style.display="";
		//b.src="images/darrow.gif";
		c.value = 1
	}
}


function BEI_swapImageByName2(lookForName, lookForSrc, replaceWith, divform) 
{
   if( divform.value == 0 )
   {
		BEI_swapImageByName(lookForName, lookForSrc, replaceWith);
	}
}







var preloadFlag = false;

function BEI_swapImageByName(lookForName, lookForSrc, replaceWith) 
{
	if (document.images && (preloadFlag == true)) 
	{
		var loc = 0;
		var img;
		
		if( img = BEI_findImageByName(lookForName) )
		{
			if( (loc = img.src.indexOf(lookForSrc)) >= 0 )
			{
				img.src = img.src.substring(0, loc) + replaceWith;
			}
		}
	}
}

/*
Use this function to swap two different image filenames.
*/
function BEI_swapImage2(lookFor,replaceWith)
{
	if (document.images && (preloadFlag == true)) 
	{
		var loc = 0;
		var img;
		
		if( img = BEI_findImage(lookFor) )
		{
			if( (loc = img.src.indexOf(lookFor)) >= 0 )
			{
				img.src = img.src.substring(0, loc) + replaceWith;
			}
		}
	}
}


/* 
  used for rollovers.  
  Name the img and then pass in the imagename, new image source.
*/

function BEI_swapImage() 
{
	if (document.images && (preloadFlag == true)) 
	{
		for (var i=0; i<BEI_swapImage.arguments.length; i+=2) 
		{
			document[BEI_swapImage.arguments[i]].src = BEI_swapImage.arguments[i+1];
		}
	}
}

function BEI_searchImageArray(images, n) 
{
	if( images )
	{
		for( j = 0; j < images.length; j++)
		{
			if( (loc = images[j].src.indexOf(n)) >= 0 )
			{
				return images[j];
			}
		}
	}

	return;
}


function BEI_searchImageArrayByName(images, n) 
{
	if( images )
	{
		for( j = 0; j < images.length; j++)
		{
			if( (loc = images[j].name.indexOf(n)) >= 0 )
			{
				return images[j];
			}
		}
	}

	return;
}



function BEI_findImage(n, d) 
{
	var p,i,j,x;  

	if(!d) 
	{
		d=document;

		if( (p=n.indexOf("?"))>0&&parent.frames.length) 
		{
			d=parent.frames[n.substring(p+1)].document; 
			n=n.substring(0,p);
		}

		for( i = 0; i < parent.frames.length; i++)
		{
			d=parent.frames[i].document; 
    
			x = BEI_searchImageArray(d.images, n);
			if( x ) return x;
		}
		x = BEI_searchImageArray(d.images, n);
		if( x ) return x;
	}
	else
	{
		x = BEI_searchImageArray(d.images, n);
		if( x ) return x;
	}

  
	if( d.forms )
	{
		for( j = 0; j < d.forms.length; i++)
		{
			x = BEI_searchImageArray(d.forms[j].images, n);
			if( x ) return x;
		}
	}

  
	if( d.layers )
	{
		for( j = 0; j < d.layers.length; j++)
		{
			x=BEI_findImage(n,d.layers[j].document); 
			if( x ) return x;
		}
	}

	x = BEI_searchImageArray(d.images, n);
	return x;
}		



function BEI_findImageByName(n, d) 
{
	var p,i,j,x;  

	if(!d) 
	{
		d=document;

		if( (p=n.indexOf("?"))>0&&parent.frames.length) 
		{
			d=parent.frames[n.substring(p+1)].document; 
			n=n.substring(0,p);
		}

		for( i = 0; i < parent.frames.length; i++)
		{
			d=parent.frames[i].document; 
    
			x = BEI_searchImageArrayByName(d.images, n);
			if( x ) return x;
		}
		x = BEI_searchImageArrayByName(d.images, n);
		if( x ) return x;
	}
	else
	{
		x = BEI_searchImageArrayByName(d.images, n);
		if( x ) return x;
	}

  
	if( d.forms )
	{
		for( j = 0; j < d.forms.length; i++)
		{
			x = BEI_searchImageArrayByName(d.forms[j].images, n);
			if( x ) return x;
		}
	}

  
	if( d.layers )
	{
		for( j = 0; j < d.layers.length; j++)
		{
			x=BEI_findImage(n,d.layers[j].document); 
			if( x ) return x;
		}
	}

	x = BEI_searchImageArrayByName(d.images, n);
	return x;
}		





function newImage(arg) 
{
	if (document.images) 
	{
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}




function BEI_moveLayer(popupLayerName, moveX, moveY)
{
	var l;
   
	l = BEI_findLayer(popupLayerName);
	
   if( !l )
     return null;
   
	if( document.layers )
	{
		l.left += moveX;
		l.top += moveY;
	}
	else
	{
		if( l.style.left )
		   l.style.left = moveX + parseInt(l.style.left);
		else
		   l.style.left = moveX;
		
		if( l.style.top )
		   l.style.top = moveY + parseInt(l.style.top);
		else
		   l.style.top = moveY;
	}
}


function BEI_setLayer(popupLayer, moveX, moveY)
{
	var l;

	l = BEI_findLayer(popupLayer);
	
   if( !l )
     return null;

	if( moveX < 0 )
            moveX = 0;


	if( moveY < 0 )
            moveY = 0;
   
	if( document.layers )
	{
		if( moveX > 0 )
			l.left = moveX;

		if( moveY > 0 )
			l.top = moveY;
	}
	else
	{
		if( moveX > 0 )
		       l.style.left = moveX;

		if( moveY > 0 )
		       l.style.top = moveY;
	}
}




function BEI_findLayer(popupLayerName)
{
	var W3C = document.getElementById? true : false;
	var NN4 = document.layers? true : false;
	var IE4 = document.all? true : false;

	if( W3C )
	{
		return document.getElementById(popupLayerName);
	}
	else if( NN4 )
	{

		var i = 0;
		for(i = 0; i < document.layers.length; i++)
		{

			if( document.layers[i].name == popupLayerName )
			{
				return document.layers[i];
			}
	 	}
	}
	else if( IE4 )
	{
		return document.all(popupLayerName);	
	}
	else
	{

	}


	return null;
}




function BEI_showLayer(popupLayerName, show)
{
   var l;
   
   l = BEI_findLayer(popupLayerName);


	var W3C = document.getElementById? true : false;
	var NN4 = document.layers? true : false;
	var IE4 = document.all? true : false;
	
	if( W3C )
	{
		if(show)
		{
//alert("Show: "+popupLayerName);		
//alert(document.getElementById(popupLayerName).style.visibility);

			document.getElementById(popupLayerName).style.visibility = "visible";

//alert(document.getElementById(popupLayerName).style.visibility);
			
		}
		else
		{
//alert("Hide: "+popupLayerName);		
//alert(document.getElementById(popupLayerName).style.visibility);

			document.getElementById(popupLayerName).style.visibility = "hidden";
		}
		
		
		if( IE4 || !NN4) //ds:8-4-05
		{
		   if( show )
			   l.style.display="";
		   else
			   l.style.display="none";
		}
		
		
	}
	else
	{
		l = BEI_findLayer(popupLayerName);
	
		if( NN4 )
		{
		  l.hidden = !show;
		}
		else if( IE4 )
		{
		   if( show )
			   l.style.display="";
		   else
			   l.style.display="none";
		}
	}
}

function BEI_showPopup(popupLayerName, width, height)
{
	var l;
   
	l = BEI_findLayer(popupLayerName);
 
   if( !l )
     return null;
     
	l.BEI_mouseTrackerMove = BEI_mouseTrackerMove;
	l.BEI_mouseTrackerUp   = BEI_mouseTrackerUp;





	BEI_setMouseTracker(l, popupLayerName, width, height);
	BEI_showLayer(popupLayerName, true);
}



function BEI_hidePopup(popupName)
{
	BEI_showLayer(popupName, false);
	BEI_freeMouseTracker();
}



function BEI_freeMouseTracker() 
{
    if (document.captureEvents) 
    { 
        document.releaseEvents(Event.MOUSEMOVE | Event.MOUSEUP);
    }

    window.BEI_startX = null;
    window.BEI_startY = null;
    window.BEI_pageX = null;
    window.BEI_pageY = null;
    
    document.onmousemove = null;
    document.onmouseup   = null;
    document.BEI_activePopup = null;
    document.BEI_activePopupName = null;
    document.BEI_activePopupWidth = null;
    document.BEI_activePopupHeight = null;
}

function BEI_setMouseTracker(popupLayer, popupLayerName, width, height) 
{
    document.onmousemove = this.BEI_mouseTrackerMove;
    document.onmouseup   = this.BEI_mouseTrackerUp;

    document.BEI_activePopup = this;
    document.BEI_activePopupName = popupLayerName;   
    document.BEI_activePopupWidth = width;
    document.BEI_activePopupHeight = height;

//    alert("a: " + document.Event);

    if (document.captureEvents) 
    { 
        document.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);
    }
}



function BEI_mouseTrackerUp(e) 
{
   BEI_hidePopup(document.BEI_activePopupName);
}


function BEI_mouseTrackerMove(e) 
{
    e = e || window.Event || window.event;
    window.BEI_pageX = e.pageX || e.clientX;
    window.BEI_pageY = e.pageY || e.clientY;

    if( !window.BEI_startY )
    {
        window.BEI_startX = Math.max(  (document.BEI_activePopupWidth / 2),  window.BEI_pageX );
        window.BEI_startY = Math.max(  (document.BEI_activePopupHeight / 2), window.BEI_pageY );

//        window.BEI_startX = window.BEI_pageX;
//        window.BEI_startY = window.BEI_pageY;

        BEI_setLayer(document.BEI_activePopupName,  window.BEI_startX - (document.BEI_activePopupWidth / 2),  window.BEI_startY - (document.BEI_activePopupHeight / 2) );

    }
    
//	status = window.pageX + "   " + window.pageY + "  " + window.startX + "  " + window.startY;    
	
	if( Math.abs(window.BEI_startX- window.BEI_pageX) > (document.BEI_activePopupWidth / 2) || Math.abs( window.BEI_startY - window.BEI_pageY ) > (document.BEI_activePopupHeight / 2) )
	{
		BEI_hidePopup(document.BEI_activePopupName);
	}
}
