﻿function displayPopup(position,url,name,height,width,evnt) 
{
// position=1 POPUP: makes screen display up and/or left, down and/or right 
// depending on where cursor falls and size of window to open
// position=2 CENTER: makes screen fall in center

	var popupHandle;

	var properties = "toolbar=0,location=0,height=" + height;
	properties = properties + ",width=" + width;
	var leftprop, topprop, screenX, screenY, cursorX, cursorY, padAmt;
	
	screenX = window.screen.availWidth;
	screenY = window.screen.availHeight;
	
	if (evnt == null)
		position = 2; //force center if a click event is not available
	
	if(position == 1)	// if POPUP not CENTER
	{ 
		cursorX = getX(evnt);	
		cursorY = getY(evnt);
							
		leftprop = cursorX + 10;
		topprop = cursorY + 10;		
				
		//see if the popup exist the screen when displayed at the mouse coordinates
		var offsetX = screenX - 10 - leftprop - width;
		var offsetY = screenY - 10 - topprop - height;
		
		if(offsetX < 0)	//if the popup exceeds the screen to the right, move
		{
			leftprop += offsetX - 30;
		}		
		
		if(offsetY < 0) //if the popup exceeds the screen to the bottom, move
		{
			topprop += offsetY - 30;
		}
	}
	else
	{
		leftprop = (screenX - width) / 2;
		topprop = (screenY - height) / 2;
	}

	if(evnt != null) 
	{
		properties = properties + ",left=" + leftprop;
		properties = properties + ",top=" + topprop;
	}

	popupHandle = open(url,name,properties);
	
	function getX(evt) {
		 if (document.all) { return (evt.clientX); }
		 return (evt.layerX);
	}
	function getY(evt) {
		 if (document.all) { return (evt.clientY); }
		 return (evt.layerY);
	}	
}