popupDialog._return 	= null;
popupDialog._modal 		= null;
popupDialog._arguments 	= null;
popupDialog.agt 		= navigator.userAgent.toLowerCase();
popupDialog.is_ie	   	= ((popupDialog.agt.indexOf("msie") != -1) && (popupDialog.agt.indexOf("opera") == -1));

function popupDialog(url, action, init, w, h) 
{
	if (typeof init == "undefined") init = window
	popupDialog._geckoOpenModal(url, action, init, w, h);
};

popupDialog._stopEvent = function(ev) 
{
	if (popupDialog.is_ie) 
	{
		ev.cancelBubble = true;
		ev.returnValue = false;
	} 
	else 
	{
		ev.preventDefault();
		ev.stopPropagation();
	}
};

popupDialog._addEvent = function(el, evname, func) 
{
	if (popupDialog.is_ie) 
	{
		el.attachEvent("on" + evname, func);
	} 
	else 
	{
		el.addEventListener(evname, func, true);
	}
};

popupDialog._removeEvent = function(el, evname, func) 
{
	if (popupDialog.is_ie) 
	{
		el.detachEvent("on" + evname, func);
	} 
	else 
	{
		el.removeEventListener(evname, func, true);
	}
};

popupDialog._parentEvent = function(ev) 
{
	setTimeout( function() 
		{ 
			if (popupDialog._modal && !popupDialog._modal.closed) 
			{ 
				popupDialog._modal.focus() 
			} 
		}
		, 50);

	if (popupDialog._modal && !popupDialog._modal.closed) 
	{
		popupDialog._stopEvent(ev);
	}
};

popupDialog._geckoOpenModal = function(url, action, init, w, h) 
{
	var l = Math.round((screen.width-w)/2);
  	var t = Math.round((screen.height-h)/2);
	var dlg = window.open(url, "hadialog"+Math.round(Math.random()*1000),
			      "toolbar=no,menubar=no,personalbar=no," +
			      "scrollbars=no,resizable=no,modal=yes,dependable=yes,"+
			      "width="+w+",height="+h+",top="+t+",left="+l);

	popupDialog._modal = dlg;
	popupDialog._arguments = init;

	function capwin(w) 
	{
		popupDialog._addEvent(w, "click", popupDialog._parentEvent);
		popupDialog._addEvent(w, "mousedown", popupDialog._parentEvent);
		popupDialog._addEvent(w, "focus", popupDialog._parentEvent);
	};
	function relwin(w) 
	{
		popupDialog._removeEvent(w, "click", popupDialog._parentEvent);
		popupDialog._removeEvent(w, "mousedown", popupDialog._parentEvent);
		popupDialog._removeEvent(w, "focus", popupDialog._parentEvent);
	};

	capwin(window);
	for (var i = 0; i < window.frames.length; capwin(window.frames[i++]));
	
	popupDialog._return = function (val) 
	{
		if (action) 
		{
			action(val);
		}
		relwin(window);
		for (var i = 0; i < window.frames.length; relwin(window.frames[i++]));
		popupDialog._modal = null;
    };
};

/**********************************************************************/

function __popupDialog_close(val) 
{
	opener.popupDialog._return(val);
	window.close();
};

/**********************************************************************/
var popup_name;
var popup_name2;
var i = 0;

function popup(src, wwid, wheigh) 
{
  	var winl = (screen.width-wwid)/2;
  	var wint = (screen.height-wheigh)/2;
  	var option = "toolbar=no,scrollbars=no,location=no,directories=no,menubar=no,resizable=yes,status=no,modal=yes,dependable=yes,width="+wwid+",height="+wheigh+",top="+wint+",left="+winl;
  	if (popup_name) popup_name.close();
  	popup_name = window.open(src, 'popup',option); 
}

function popup2(src, wwid, wheigh) 
{
  	var winl = (screen.width-wwid)/2;
  	var wint = (screen.height-wheigh)/2;
  	var option = "toolbar=no,scrollbars=no,location=no,directories=no,menubar=no,resizable=no,status=no,modal=yes,dependable=yes,width="+wwid+",height="+wheigh+",top="+wint+",left="+winl;
  	if (popup_name2) popup_name2.close();
  	popup_name2 = window.open(src, 'popup2',option); 
}

function popup_scroll(src, wwid, wheigh) 
{
  	var winl = (screen.width-wwid)/2;
  	var wint = (screen.height-wheigh)/2;
  	var option = "toolbar=no,scrollbars=yes,location=no,directories=no,menubar=no,resizable=yes,status=no,modal=yes,dependable=yes,width="+wwid+",height="+wheigh+",top="+wint+",left="+winl;
  	if (popup_name) popup_name.close();
  	popup_name = window.open(src, 'popup',option); 
}

function popup_open(src, wwid, wheigh) 
{
		i++;
  	var winl = (screen.width-wwid)/2;
  	var wint = (screen.height-wheigh)/2;
  	var option = "toolbar=no,scrollbars=no,location=no,directories=no,menubar=no,resizable=yes,status=no,modal=yes,dependable=yes,width="+wwid+",height="+wheigh+",top="+wint+",left="+winl;
  	//if (popup_name) popup_name.close();
  	//popup_name = window.open(src, 'popup',option); 
  	window.open(src, 'popup'+i,option); 
}