var ieV6OrLess = false;
var popupHandle;

if(navigator.appName == "Microsoft Internet Explorer")
{
	var version=0;
	if (navigator.appVersion.indexOf("MSIE")!=-1)
	{
		temp=navigator.appVersion.split("MSIE")
		temp=temp[1].split(";");
		version=parseFloat(temp[0])
	}
	if( version != 0 && version < 7 )
		ieV6OrLess = true;
	else
		ieV6OrLess = false;
}

function refreshPopup()
{
	if( popupHandle != null )
	{
		popupHandle.location.href = popupHandle.location.href;
	}
}

function closePopup()
{
	if(popupHandle != null && !popupHandle.closed) popupHandle.close();
}

/* Modal popup window code */
function mpopBig(url) { return displayMPopup(url,url.substring(0,url.indexOf('.')),500,700,1,1); }
function mpopMed(url) { return displayMPopup(url,url.substring(0,url.indexOf('.')),395,400,1,0); }
function mpopSml(url) { return displayMPopup(url,url.substring(0,url.indexOf('.')),195,400,1,0); }
function displayMPopup(url,name,height,width,resizeable,scrollbars)
{
	// Ie 6 vs 7 have different rendering styles.  For IE6 or less we add 50 pixels to
	//  the height of modal windows to account for the difference in their calculations.
	if(ieV6OrLess)
		height += 50;
	var features = "dialogHeight:"+height+"px;dialogWidth:"+width+"px;center:1;help:0;resizable:"+resizeable+";scroll:"+scrollbars+";status:0;unadorned:1;";
	return window.showModalDialog(url,name,features);
}

/* Popup window code */
function popBig(url) { return displayPopup(url,url.substring(0,url.indexOf('.')),500,700,1,1); }
function popMed(url) { return displayPopup(url,url.substring(0,url.indexOf('.')),395,400,1,0); }
function popSml(url) { return displayPopup(url,url.substring(0,url.indexOf('.')),195,400,1,0); }

function displayPopup(url,name,height,width,resizable,scrollbars)
{
	// 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 position = 2;
	
	/* encode url */
	url = encodeURI(url);
	while( url.indexOf("#") > -1 ) { url = url.replace("#", "%23"); }
	while( name.indexOf("/") > -1 ) { name = name.replace("/", "" ); }
	while( name.indexOf("\\") > -1 ) { name = name.replace("\\", "" ); }

	var properties = "toolbar=0,status=0,location=0,menubar=0,scrollbars=" + scrollbars + ",resizable=" + resizable + ",height=" + height + ",width=" + width;
	var leftprop, topprop, screenX, screenY, cursorX, cursorY, padAmt;

	if(navigator.appName == "Microsoft Internet Explorer")
	{
		screenY = document.body.offsetHeight;
		screenX = window.screen.availWidth;
	}
	else
	{
		screenY = window.outerHeight
		screenX = window.outerWidth
	}

	if(position == 1)
	{ // if POPUP not CENTER
		cursorX = screenX;
		cursorY = screenY;
		padAmtX = 10;
		padAmtY = 10;
		if( (cursorY + height + padAmtY) > screenY )
		{
			// make sizes a negative number to move left/up
			padAmtY = (-30) + (height * -1);
			// if up or to left, make 30 as padding amount
		}
		if( (cursorX + width + padAmtX) > screenX )
		{
			padAmtX = (-30) + (width * -1);	
			// if up or to left, make 30 as padding amount
		}
		if( navigator.appName == "Microsoft Internet Explorer" )
		{
			leftprop = cursorX + padAmtX;
			topprop = cursorY + padAmtY;
		}
		else
		{
			leftprop = (cursorX - pageXOffset + padAmtX);
			topprop = (cursorY - pageYOffset + padAmtY);
		}
	}
	else
	{
		leftvar = (screenX - width) / 2;
		rightvar = (screenY - height) / 2;
		if( navigator.appName == "Microsoft Internet Explorer" )
		{
			leftprop = leftvar;
			topprop = rightvar;
		}
		else
		{
			leftprop = (leftvar - pageXOffset);
			topprop = (rightvar - pageYOffset);
		}
	}
	properties = properties + ",left=" + leftprop;
	properties = properties + ",top=" + topprop;

	closePopup();
	popupHandle = document.open(url,name,properties);
	if( popupHandle == null )
		alert( "You appear to have a popup blocker in place.  Please allow popups for this site in order to continue." );
	else
		popupHandle.focus();
}
