/////////////////////////////////////////////////////////////////
//Modal dialog functions

//Open new dialog window
//parameters: 
//				evt - event object (required)
//				wnd - window object
//				url - url string
//				width,height - size of window
//for creating new dialog window all parameters should be present, wnd should be set to parent window
//for activating existing dialog url should be null, wnd - window for activating, other parameters are not important
var modalDlgWindow = null;
var inputControlToFocus = null;
function ShowModalDialog(evt, wnd, url, width, height)
{
	if (null == url)
	{
		if (wnd && !wnd.closed)
			wnd.focus();
		return wnd;
	}
	var isIE = (document.all)? true: false;
	if (!isIE || navigator.appName.indexOf("Explorer")==-1)		//Netscape or Safari
	{
		if ((null == wnd) || wnd.closed)
		{
			var top = Math.floor((window.screen.height-height)/2);
			var left = Math.floor((window.screen.width-width)/2);
			var sFeatures = "height=" + height + ",width=" + width + ",status=no,toolbar=no,menubar=no,modal=yes,resizable=yes,location=no,left=" + left + ",top=" + top;
			wnd = window.open(url,"_blank",sFeatures);
		}
		else
			wnd.focus();
		if (null != evt)
			evt.preventDefault();
		modalDlgWindow = wnd;
		window.onfocus = function() { return FocusDialog(); }
		return wnd;
	}
	else		//IE
	{
		if(window.event != null)
			window.event.returnValue = false;

		// < IE 7.0 -> enlarge height.
		if (navigator.appName && navigator.appVersion && navigator.appName == "Microsoft Internet Explorer")
		{
			var index = navigator.appVersion.indexOf("MSIE");
			var version;
			if (index != -1)
				version = parseFloat(navigator.appVersion.substring(index+5));
			if (version < 7.0)
			{
				height += 55;
				width += 8;
			}
		}
			
		var sFeatures = "dialogHeight:" + height + "px; dialogWidth:" + width + "px; edge: Raised; center: Yes; help: No; resizable: Yes; status: No";
		var url = window.showModalDialog(url, window ,sFeatures);
		if (url)
			RedirectWindow(url);			
		return null;
	}
}

function ShowModalDialogWithNoResize(evt, wnd, url, width, height)
{
	if (null == url)
	{
		if (wnd && !wnd.closed)
			wnd.focus();
		return wnd;
	}
	var isIE = (document.all)? true: false;
	if (!isIE || navigator.appName.indexOf("Explorer")==-1)		//Netscape or Safari
	{
		if ((null == wnd) || wnd.closed)
		{
			var top = Math.floor((window.screen.height-height)/2);
			var left = Math.floor((window.screen.width-width)/2);
			var sFeatures = "height=" + height + ",width=" + width + ",status=no,toolbar=no,menubar=no,modal=yes,resizable=no,location=no,left=" + left + ",top=" + top;
			wnd = window.open(url,"_blank",sFeatures);
		}
		else
			wnd.focus();
		if (null != evt)
			evt.preventDefault();
		modalDlgWindow = wnd;
		window.onfocus = function() { return FocusDialog(); }
		return wnd;
	}
	else		//IE
	{
		if(window.event != null)
			window.event.returnValue = false;

		// < IE 7.0 -> enlarge height.
		if (navigator.appName && navigator.appVersion && navigator.appName == "Microsoft Internet Explorer")
		{
			var index = navigator.appVersion.indexOf("MSIE");
			var version;
			if (index != -1)
				version = parseFloat(navigator.appVersion.substring(index+5));
			if (version < 7.0)
			{
				height += 55;
				width += 8;
			}
		}
			
		var sFeatures = "dialogHeight:" + height + "px; dialogWidth:" + width + "px; edge: Raised; center: Yes; help: No; resizable: No; status: No";
		var url = window.showModalDialog(url, window ,sFeatures);
		if (url)
			RedirectWindow(url);			
		return null;
	}
}

function ShowParamDialog(evt, wnd, url, width, height, callbackFunc)
{
	url = url + "&fun=" + callbackFunc;

	ShowModalDialog(evt, null, url, width, height);
}

//Sets input focus to the dialog window // Netscape or Safari
function FocusDialog()
{
	if (modalDlgWindow && !modalDlgWindow.closed)
	{
		modalDlgWindow.focus();
	}
	return false;
}
/////////////////////////////////////////////////////////////////

//close dialog; params: dlg - dialog window object
function CloseDialog(dlg)
{
	if ((dlg != null) && !dlg.closed)
		dlg.close();
}

/*redirect functions*/

function RedirectWindow(url)
{
	if (url)
	{
		if ("self" == url)
			window.location.href = window.location.href;
		else
			window.location.href = url;
	}
}

function RedirectParentWindow(url)
{
	if (url)
	{
		if (this.opener)
			this.opener.RedirectWindow(url)
		else
			this.returnValue = url;
		this.close();
	}
}

function ShowWindow(url, width, height)
{
	var posX = Math.round((window.screen.width - width)/2);
	var posY = Math.round((window.screen.height - height)/2);
	var sFutures = "directories=no, resizable=yes, height=" + height + ", width=" + width + ", top=" + posY + ", left=" + posX;
	window.open(url,"_blank",sFutures);
}

function ShowFakeModalDialog(url, width, height, isCentered)
{
	var sFeatures = "height=" + height + ",width=" + width + ",status=no,toolbar=no,menubar=no,modal=yes,resizable=Yes,location=no";
	if (isCentered)
	{
		var posX = Math.round((window.screen.width - width)/2);
		var posY = Math.round((window.screen.height - height)/2);
		sFeatures += ", top=" + posY + ", left=" + posX;
	}

	var fakeModalWindow = window.open(url,"_blank",sFeatures);
	
	// set timer to IE only
	if( window.showModelessDialog )
	{
		window.onfocus = function()
			{
				try
				{
					window.focused = true;
					if(fakeModalWindow != null && !fakeModalWindow.closed)
						fakeModalWindow.focus();
				}
				catch (e) {}
			}
		window.onblur = function()
			{
				window.focused = false;
			}
		fakeModalWindow.onfocus = function()
			{
				fakeModalWindow.focused = true;
			}
		fakeModalWindow.onblur = function()
			{
				try
				{
					if( fakeModalWindow != null && fakeModalWindow.parent && fakeModalWindow.parent.focused )
						fakeModalWindow.focus();
				}
				catch (e) {}
			}
	}
	
	return fakeModalWindow;
}

function ShowUploadStatus1()
{
	var width = 250;
	var height = 50;
	var url = "Dialog.aspx?page=uploadstatus";

	var posX = Math.round((window.screen.width - width)/2);
	var posY = Math.round((window.screen.height - height)/2);
	var sFutures = "directories=no, resizable=no, height=" + height + ", width=" + width + ", top=" + posY + ", left=" + posX;
	var upload = window.open(url,"upload",sFutures);

	window.onunload = function (e)
	{
		CloseUploadStatus(upload);
	}
}

function CloseUploadStatus(wnd)
{
	wnd.close();
}

function CloseAndRefreshParent()
{
	if (window.opener)
	{
		if( opener.RefreshMemberPage )
			opener.RefreshMemberPage();
		else
			opener.RefreshPage();
	}
	else if (window.dialogArguments)
	{
		if(dialogArguments.RefreshMemberPage)
			dialogArguments.RefreshMemberPage();
		else
			dialogArguments.RefreshPage();
	}
	
	window.close();
}

