var m_lHighResWidth = 800, m_lHighResHeight = 600;

var m_lSpacing = 100;

var m_lWindowWidth, m_lWindowHeight;

var m_lOldWindowWidth, m_lOldWindowHeight;

var m_lScreenWidth, m_lScreenHeight;

var m_lTopX, m_lTopY;

var m_sOptions;

var m_bWindowSizeChanged = false;



function getWindowWidthAndHeight() {

	var aOptionsList = m_sOptions.split(",");

	var lArrayCount = 0;



	do

	{

		if (aOptionsList[lArrayCount].indexOf("width") >= 0)

		{

			m_lWindowWidth = aOptionsList[lArrayCount];

			m_lWindowWidth = m_lWindowWidth.replace(" ", "");

			m_lWindowWidth = m_lWindowWidth.replace("width=", "");

			m_lWindowWidth = Number(m_lWindowWidth);

		}



		if (aOptionsList[lArrayCount].indexOf("height") >= 0)

		{

			m_lWindowHeight = aOptionsList[lArrayCount];

			m_lWindowHeight = m_lWindowHeight.replace(" ", "");

			m_lWindowHeight = m_lWindowHeight.replace("height=", "");

			m_lWindowHeight = Number(m_lWindowHeight);

		}



		lArrayCount ++

	} while (lArrayCount < aOptionsList.length)

}



function getScreenWidthAndHeight() {

	m_lScreenWidth = screen.availWidth;

	m_lScreenHeight = screen.availHeight;

}



function calculateXYPosition(bIsLowRes) {

	var bLeftIsOffScreen, bRightIsOffScreen;

	var bTopIsOffScreen, bBottomIsOffScreen;



	if (!bIsLowRes)

	{

		m_lTopX = ((m_lScreenHeight  / 2) - (m_lWindowHeight / 2));

		m_lTopY = ((m_lScreenWidth  / 2) - (m_lWindowWidth / 2));

	}

	else

	{

		m_lTopX = 0;

		m_lTopY = 0;

	}

}



function calculateWindowSize(bIsLowRes)

{

	if (bIsLowRes)

	{

		var lMarginSize = 130;



		if (m_lWindowHeight >= m_lScreenHeight)

		{

			m_lOldWindowHeight = m_lWindowHeight;

			m_lWindowHeight = (m_lScreenHeight - lMarginSize);

			m_bWindowSizeChanged = true;

		}



		if (m_lWindowWidth >= m_lScreenWidth)

		{

			m_lOldWindowWidth = m_lWindowWidth;

			m_lWindowWidth = (m_lScreenWidth - lMarginSize);

			m_bWindowSizeChanged = true;

		}

	}

}



function createNewOptionsString() {

	

	if (m_bWindowSizeChanged)

	{

		var sOldWidthString, sOldHeightString;

		var sNewWidthString, sNewHeightString;

		

		sOldWidthString = "width=" + m_lOldWindowWidth;

		sOldHeightString = "height=" + m_lOldWindowHeight;

		sNewWidthString = "width=" + m_lWindowWidth;

		sNewHeightString = "height=" + m_lWindowHeight;



		m_sOptions = m_sOptions.replace(sOldWidthString, sNewWidthString);

		m_sOptions = m_sOptions.replace(sOldHeightString, sNewHeightString);

	}



	m_sOptions += ",left=" + m_lTopY + ",top=" + m_lTopX;

}



function openWindow(sUrl, sTitle, sOptions, bReplace) {

	var bIsLowRes;



	bIsLowRes = (m_lScreenWidth <= m_lHighResWidth && m_lScreenHeight <= m_lHighResHeight);

	m_sOptions = sOptions;



	getWindowWidthAndHeight();

	getScreenWidthAndHeight();

	calculateWindowSize(bIsLowRes);

	calculateXYPosition(bIsLowRes);

	createNewOptionsString();

	viewer=window.open("", sTitle, m_sOptions, bReplace);

	// viewer.moveTo(m_lTopX, m_lTopY);

	viewer.location=sUrl;

	viewer.focus();

}
