var dialogueWindows = new Object();

onunload = closeAllDialogues;

/**
 * Open a popup window
 */ 
function openDialogue( windowName, windowUrl, windowWidth, windowHeight, scrollbars ) 
{	
	var dialogue = dialogueWindows[ windowName ];
	var windowTop = ( screen.height / 2 ) - ( windowHeight / 2 );
	var windowLeft = ( screen.width / 2 ) - ( windowWidth / 2 );
	var features = 'height=' + windowHeight + ',width=' + windowWidth + ',top=' + windowTop + ',left=' + windowLeft + ',scrollbars=' + ( scrollbars ? "yes" : "no" ) + ',resizable=yes,status=no';
	
	if( dialogue && ! dialogue.closed )
	{
		dialogue.close();
	}	
	
	dialogueWindows[ windowName ] = window.open( windowUrl, windowName, features );
	
}

function refresh()
{
    location.reload();
}

/**
 * Close all windows that were opened by openDialogue
 * Handy to clean up popups when leaving the page
 */
function closeAllDialogues()
{
	for( windowName in dialogueWindows )
	{	
		var dialogue = dialogueWindows[ windowName ];
		
		if( dialogue && ! dialogue.closed )
		{
			dialogue.close();
		}
	}
}

/**
 * Step back to previous page
 *
 * @param string defaultUrl
 */
function stepBack( defaultUrl )
{
	// This uses referrer because a simple history.back() will not force a reload of the page
	
	if( document.referrer.indexOf( window.location.host ) != -1 )
	{
		window.location = document.referrer;
		return;
	}
	
	window.location = defaultUrl;
}

