
var cls_modalDialogWindow = function() {

	this.debugCounter = 0;
	this.msc_returnFunction;
	this.msc_returnFunctionArgument;
	this.msc_returnValue;
	this.obj_win
	this.int_closeTimeOut;

	this.focusWindow = function() {
		if ((typeof this.win == "object") && (!this.win.closed)) {
			this.win.focus();
		}
	}


	this.disableFocus = function() {
		return false;
	}

	this.enableFocus = function() {
		return false;
	}



	this.enableOpenerLayer = function(str_objectName) {

		var str_openerLayer = 'modalDiv';

		if (document.getElementById(str_openerLayer)) {
			obj_openerLayer = document.getElementById(str_openerLayer);
			document.getElementById(str_openerLayer).style.height	= document.body.clientHeight+ "px";
			obj_openerLayer.style.display = 'block';
			// for ie WCH.Discard(str_openerLayer);
			obj_openerLayer.onclick = function() {
				eval(str_objectName+'.focusWindow();');
				//window.setTimeout(str_objectName+'.focusWindow()', 10);
			}
		}
	}


	this.disableOpenerLayer = function() {

		var str_openerLayer = 'modalDiv';

		if (document.getElementById(str_openerLayer)) {
			obj_openerLayer = document.getElementById(str_openerLayer);
			obj_openerLayer.style.display = 'none';
			document.getElementById(str_openerLayer).style.height	= "0px";
			obj_openerLayer.onclick = null;
		}
	}


	this.installCloseChecker = function(str_objectName) {
		this.int_closeTimeout = window.setInterval(str_objectName + ".closeChecker()", 100);
		return true
	}


	this.uninstallCloseChecker = function() {
		if (this.int_closeTimeout) {
			window.clearInterval(this.int_closeTimeout);
		}
		return true;
	}


	this.closeChecker = function() {
		//this.debugCounter++;
		//window.status = this.debugCounter;
		if ((!this.win) || (this.win.closed)) {
			this.uninstallCloseChecker();
			this.disableOpenerLayer();
			this.callReturnFunction();
		}
		return true;
	}


	this.closeWindow = function() {
		this.uninstallCloseChecker();
		this.disableOpenerLayer();
		this.callReturnFunction();
		this.win.close();
	}

	this.callReturnFunction = function() {

		str_function = String(obj_modalDialogWindow.msc_returnFunction);

		if (str_function.length > 0) {
			//eval(obj_modalDialogWindow.msc_returnFunction+'('+obj_modalDialogWindow.msc_returnFunctionArgument+')');
			eval(str_function+'('+obj_modalDialogWindow.msc_returnFunctionArgument+')');
		}

		return true;
	}

}



var obj_modalDialogWindow 			= new cls_modalDialogWindow();
var boo_modalDialogWindowUnload = false;
var obj_timeout;



// Generate a modal dialog.
// str_url 										-- 	str_url of the page/frameset to be loaded into dialog
// int_width 									-- 	pixel int_width of the dialog window
// int_height 								-- 	pixel int_height of the dialog window
// msc_returnFunction 				-- 	reference to the function (on this page) that is to act on the data returned from the dialog
// msc_returnFucntionArgument	--	argument for the return function

function openModalDialog(str_url, int_width, int_height, msc_returnFunction, msc_returnFunctionArgument) {

	//check for an active RTE and close the instance if any exist, function is in richtext.js
	if(typeof fct_richtext_checkActiveRte != 'undefined') {
		if(isFunction(fct_richtext_checkActiveRte) && (fct_richtext_checkActiveRte() == true)) {
			fct_richtext_cleanShutdownRte();
		}
	}

	if ((typeof obj_modalDialogWindow.win != "object") || (obj_modalDialogWindow.win.closed)) {

		// Initialize properties of the modal dialog object.
		obj_modalDialogWindow.msc_returnFunction					= msc_returnFunction;
		obj_modalDialogWindow.msc_returnFunctionArgument	= msc_returnFunctionArgument;
		obj_modalDialogWindow.msc_returnValue							= false;
		obj_modalDialogWindow.str_url 										= str_url;
		obj_modalDialogWindow.int_width 									= int_width;
		obj_modalDialogWindow.int_height 									= int_height;

		// Keep str_name unique so Navigator doesn't overwrite an existing dialog.
		obj_modalDialogWindow.str_name 										= (new Date()).getSeconds().toString();

		// The best we can do is center in screen.
		obj_modalDialogWindow.left 	= (screen.width - obj_modalDialogWindow.int_width) / 2;
		obj_modalDialogWindow.top 	= (screen.height - obj_modalDialogWindow.int_height) / 2;
		var str_attr = 'left=' + obj_modalDialogWindow.left + ',top=' + obj_modalDialogWindow.top + ',resizable=no,width=' + obj_modalDialogWindow.int_width + ',height=' + obj_modalDialogWindow.int_height + ',dependent=yes,scrollbars=yes';

		// Generate the dialog and make sure it has focus.
		if (str_file = obj_modalDialogWindow.str_url.match(/^(.*)\.php/)) {

			if (typeof str_file != 'string') {
				str_file = str_file[0];
			}

			if (!str_file.match(/^(https?:|ftp:)/)) {
				obj_modalDialogWindow.str_url = document.location.protocol + '//' + document.location.hostname + '/' + str_file + obj_modalDialogWindow.str_url.match(/\?(.*)$/);
			}
		}

		obj_modalDialogWindow.win = window.open(obj_modalDialogWindow.str_url, obj_modalDialogWindow.str_name, str_attr);
		obj_modalDialogWindow.focusWindow();
		obj_modalDialogWindow.enableOpenerLayer("obj_modalDialogWindow");
		obj_modalDialogWindow.installCloseChecker("obj_modalDialogWindow");
	} else {
		obj_modalDialogWindow.focusWindow();
	}
}