var _the_handles = new Array();

launchWindow = function(theFormId, openParams, errorMessageDivId)
{
	var theForm = $(theFormId);
	var winName = theForm.id;
	win = launchWindowFocus(theFormId);
	if(win == null)
	{
		win = openWindow("", winName, openParams);
		if(win != null)
		{
			theForm.submit();
			win.focus();
			
			var len = _the_handles.length;
			_the_handles[len] = new Array();
			_the_handles[len][0] = theFormId;
			_the_handles[len][1] = win;

			_w_a_t_c_h_ForClosedWindow();
		}
		else
		{
			if($(errorMessageDivId) != null)
			{
				new SimpleRequest(errorMessageDivId, "/pub/vm/popupBlockerBlocked.html", "get", "");
			}
		}
	}
		
	return win;
}

launchWindowFocus = function(theFormId)
{
	if(_the_handles != null)
	{
		for(var i=0;i<_the_handles.length;i++)
		{
			if(_the_handles[i][0] == theFormId)
			{
				win = _the_handles[i][1];
				if(win != null && !win.closed)
					win.focus();
				else
					win = null;

				return win;
			}
		}
	}
}

_w_a_t_c_h_ForClosedWindow = function()
{
	for(var i=0;i<_the_handles.length;i++)
	{
		var handle = _the_handles[i][1];
		if(handle != null && !handle.closed)
		{
			setTimeout("_w_a_t_c_h_ForClosedWindow()", 1000);
			return;
		}
		else
		{   
		    if(_the_handles[i][0] != '-1')
		    {
		      //mark the closed one
		      _the_handles[i][0] = '-1';
			  break;
			}  
			
		}
	}
	
	//delay an update - a window was closed
	setTimeout("_w_i_n_d_o_wClosed()", 1000);
}

_w_i_n_d_o_wClosed = function()
{
	if(window.taskClosed != null)
		window.taskClosed();
}

