
function NewDialog(url, action, init,name) {
	if (typeof init == "undefined") {
		init = window;	// pass this window object by default
	}
	NewDialog._geckoOpenModal(url, action, init,name);
};

NewDialog._parentEvent = function(ev) {
	if (NewDialog._modal && !NewDialog._modal.closed) {
		NewDialog._modal.focus();
		
	}
};

// should be a function, the return handler of the currently opened dialog.
NewDialog._return = null;

// constant, the currently opened dialog
NewDialog._modal = null;
// detect browser
NewDialog._isIE = navigator.appName =="Microsoft Internet Explorer"?true:false;
// the dialog will read it's args from this variable
NewDialog._arguments = null;

NewDialog._geckoOpenModal = function(url, action, init,name) {
	var dlg;
	if (name!=null)
	dlg = window.open(url, name,
			      "toolbar=no,menubar=no,personalbar=no,width=10,height=10," +
			      "scrollbars=no,resizable=yes");
	else
	dlg = window.open(url, "hadialog",
			      "toolbar=no,menubar=no,personalbar=no,width=10,height=10," +
			      "scrollbars=no,resizable=yes");
	NewDialog._modal = dlg;
	NewDialog._arguments = init;

	// capture some window's events
	function capwin(w) {
		NewDialog._addEvent(w, "click", NewDialog._parentEvent);
		NewDialog._addEvent(w, "mousedown", NewDialog._parentEvent);
		NewDialog._addEvent(w, "focus", NewDialog._parentEvent);
	};
	// release the captured events
	function relwin(w) {
		NewDialog._removeEvent(w, "click", NewDialog._parentEvent);
		NewDialog._removeEvent(w, "mousedown", NewDialog._parentEvent);
		NewDialog._removeEvent(w, "focus", NewDialog._parentEvent);
	};
	capwin(window);
	// capture other frames
	for (var i = 0; i < window.frames.length; capwin(window.frames[i++]));
	// make up a function to be called when the Dialog ends.
	NewDialog._return = function (val) {
		if (val && action) {
			action(val);
		}
		relwin(window);
		// capture other frames
		for (var i = 0; i < window.frames.length; relwin(window.frames[i++]));
		NewDialog._modal = null;
	};
};
NewDialog._addEvent = function(el, evname, func) {
	if (NewDialog._isIE) {
		el.attachEvent("on" + evname, func);
	} else {
		el.addEventListener(evname, func, true);
	}
};
NewDialog._removeEvent = function(el, evname, func) {
	if (NewDialog._isIE) {
		el.detachEvent("on" + evname, func);
	} else {
		el.removeEventListener(evname, func, true);
	}
};

