/***********************************************************************
*
* OpenPopup			-		opens a new window and sets the focus on it
*
* Input: 	sURL		-	the URL for the newly created window
* 			sWindowName	-	string identifier of the window
* 			nWidth		-	width of the window
*			nHeight		-	height of the window
*
* Returns:			-		window object
************************************************************************/

function OpenPopup(sURL, sWindowName, nWidth, nHeight)
	{
	var oPopupWindow = window.open(sURL, sWindowName, 'location=no, menubar=no, directories=no, toolbar=no, width=' + nWidth + ', height=' + nHeight);
	oPopupWindow.focus();
	return oPopupWindow;
	}

/***********************************************************************
*
* AddQuotes			-		convert quotes in text
*
* Input: 	sText		-	the text to convert
*
* Returns:			-		replaced text
************************************************************************/

function AddQuotes(sText)
	{
	var re = /(['"])/g;
	return sText.replace(re, function ($0, $1) {
		switch ($1)
			{
			case '"':
				return "&quot;";
			case '&':
				return "&amp;";
			default:
				return $1;
			}
		});
	}

/***********************************************************************
*
* PopupImage		-		opens an image in a new window
*
* Input: 	sImageURL	-	the image URL
* 			sWindowName	-	string identifier of the window
* 			sLabel		-	title of the window
* 			nWidth		-	width of the window
*			nHeight		-	height of the window
*
* Returns:			-		window object
************************************************************************/

function PopupImage(sImageURL, sWindowName, sLabel, nWidth, nHeight)
	{
	var oWindow = OpenPopup('', sWindowName, nWidth, nHeight);
	var oDocument = oWindow.document;
	var sLabel = (sLabel.length > 0) ? AddQuotes(sLabel) : '';
	oDocument.open('text/html', 'replace');
	oDocument.write('<html><head><title>', sLabel,
		'</title>',
		'<script>',
		'function initOnLoad() {',
			'var i = 0;',
			'if (window.document.images[0] != null){',
				'window.resizeTo(window.document.images[0].width + 30, window.document.images[0].height + 80);',
			'}}</script>',
		'</head><body marginheight="0" marginwidth="0" leftmargin="0" topmargin="0" onload="initOnLoad()">',
		'<a href="#" onclick="self.close();" onblur="self.close()">',
		'<img border="0" alt="', sLabel, '" title="Click here to close" src="', AddQuotes(sImageURL), '"></a></body></html>');
	oDocument.close();
	oWindow.focus();
	}
