var coverDiv;
var childWindow;
var checkInterval;

function OpenCmsDialog(url, dialogGuid, clientArgs, title, height, width, menubar, scrollbars, toolbar, status, resizable)
{
	if (dialogGuid)
	{
		try
		{
			coverDiv = window.top.document.createElement("div");
			coverDiv.style.background = "url(../CORE/Images/spacer.gif) no-repeat center";
			coverDiv.style.position = "absolute";
			coverDiv.style.left = 0;
			coverDiv.style.top = 0;
			coverDiv.style.zIndex = 2000;
			coverDiv.onfocus = function() {if (childWindow && !childWindow.closed) {childWindow.focus();}};
			coverDiv.onmouseover = function() {if (childWindow && !childWindow.closed) {	childWindow.focus();} };
			coverDiv.onclick = function() {if (childWindow && !childWindow.closed) {	childWindow.focus();} };
			
			window.top.document.body.appendChild(coverDiv);
			
			window.onunload = checkDialog;
			
			window.onresize = function(){
				if(coverDiv){
				var w;
				var h;
					if( typeof( window.innerWidth ) == 'number' ) {
						//Non-IE
						w = window.innerWidth;
						h = window.innerHeight;
					} else {
						w = window.top.document.body.clientWidth;
						h = window.top.document.body.clientHeight;
					}

					coverDiv.style.width = w;
					coverDiv.style.height = h;
				}
			}
			
			var left = Math.floor((screen.availWidth - width) / 2);
			var top = Math.floor((screen.availHeight - height) / 2);
			var delimiter = url.indexOf('?') == -1 ? '?' : '&';
			
			childWindow = window.open(url+ delimiter+"dialogGuid=" + dialogGuid + "&clientArgs=" + clientArgs, "",  "width=" + width + ", height=" + height + ", top=100, left=100, menubar=" + menubar + ", scrollbars=" + scrollbars + ", toolbar=" + toolbar + ", status=" + status + ", resizable=" + resizable + ", left = "+left+", top = "+top );
			
			while(childWindow.document.body==null){};
			function NoContextMenu(){
				childWindow.event.cancelBubble = true;
				return false;
			}
			
			childWindow.document.oncontextmenu = NoContextMenu;
			
			
			checkInterval = window.setInterval("checkDialog()",200);
			
			window.onfocus = function() {
				if (childWindow && !childWindow.closed)
					childWindow.focus();
			}  
			childWindow.document.title.innerText = title;
		}
		catch (e) {}
	}
}

function checkDialog(){
	if (childWindow ==null || childWindow.closed){
		if(coverDiv){
			coverDiv.style.display='none'; 
			coverDiv = null;
		}
		if(checkInterval) window.clearInterval(checkInterval);
	}
}




function ExecuteAfterCmsDialogClose(dialogGuid, result, ClientResponseHandler, UniqueID)
{
	// execute client event
	try
	{
		if (ClientResponseHandler && eval (ClientResponseHandler)) {
			// alert("ExecuteAfterCmsDialogClose u Dialog.js->" + ClientResponseHandler + "(\"" + result + "\")") 
			result = result.replace(/"/gi, '\\"');
			var strEval = ClientResponseHandler + "(\"" + result + "\")";
			eval (strEval);
		}
	}
	catch (e) 
	{ 
		HandleErr(e); 
	}
	
	// execute server event
	try
	{
		if (DialogServerEventCall)	
			__doPostBack(UniqueID, "ExecuteServerEventCall");
	}
	catch (e) 
	{ 
		HandleErr(e); 
	}
}


function HandleErr(e)
{
	var r = '';
	for (var p in e)
		r += p + ': ' + e[p] + '\n';
    alert(r);
}

