﻿//get the current RadWindow
function getRadWindow()
{
	var targetWindow = null;
	if(window.radWindow)
	{
		targetWindow = window.radWindow;
	}
	else
	{
		if(window.frameElement && window.frameElement.radWindow)
		{
			targetWindow = window.frameElement.radWindow;
		}
	}
	
	return targetWindow;
} 


//close the current window indicating
//if it was closed by cancelling or after
//actions were completed
function closeRadWindow(isCancelled)
{
	if(isCancelled == null || typeof(isCancelled) == "undefined")
	{
		isCancelled = false;
	}
	var targetWindow = getRadWindow();
	targetWindow.returnValue = isCancelled;
	targetWindow.Close(isCancelled);
	
	//return false to prevent a postback
	//if called from a button that usually
	//triggers a postback
	return false;
}

//default handler to run when a window is opened
function radWindowOpen(radWindow)
{
	//often rad windows don't center properly
	//so this method is the default open handler
	//to center once loading is complete
	radWindow.Center();
}

function radWindowClose(radWindow)
{
	if(radWindow.returnValue == false)
	{
		//the window was not closed
		if(typeof(updateCurrentPage) == "function")
		{
			updateCurrentPage();
		}
	}
}


//prepare a rad window and return ready to open
//not opening in this method because the calling
//method may want to perform further customisations
function createRadWindow(windowName, pageUrl, windowHeight, windowWidth, windowTitle, openHandler, closeHandler, callbackHandler)
{
	//set defaults for missing arguments
	if(windowName == null)
	{
		windowName = "RadWindow";
	}
	
	if(windowHeight == null)
	{
		windowHeight = 400;
	}
	
	if(windowWidth == null)
	{
		windowWidth = 400;
	}

	if(windowTitle == null)
	{
		windowTitle = "Loading...";
	}

	
	//create the new window always using
	//the window manager of the main window
	var targetWindow = top.radopen(windowName);
	
	//congifure the new window
	if(targetWindow != null)
	{
		with(targetWindow)
		{
			SetUrl(pageUrl);
			SetSize(windowWidth, windowHeight);
			SetTitle(windowTitle);
			SetModal(true);
			Center();
			if (typeof(Telerik.Web.UI.WindowBehaviors) != "undefined")
			{
				set_behaviors(Telerik.Web.UI.WindowBehaviors.Close + Telerik.Web.UI.WindowBehaviors.Reload); 
			}
			//default to using the open handler
			//in this file if no other specified
			if(openHandler == null)
			{
				openHandler = radWindowOpen;
			}
			
			targetWindow.OnClientPageLoad = openHandler;
			
			if(closeHandler == null)
			{				
				closeHandler = radWindowClose;
			}
			
			targetWindow.OnClientClose = closeHandler;
			
			if(callbackHandler != null)
			{
				targetWindow.ClientCallBackFunction = callbackHandler;
			}
		}
	}
	
	//return the window read for the calling function to show
	return targetWindow;
}

//prepare a rad window and return ready to open
//not opening in this method because the calling
//method may want to perform further customisations
function createRadPopup(windowName, pageUrl, windowTop, windowLeft, windowHeight, windowWidth, windowTitle)
{
	//set defaults for missing arguments
	if(windowName == null)
	{
		windowName = "RadPopup";
	}
	
	if(windowHeight == null)
	{
		windowHeight = 175;
	}
	
	if(windowWidth == null)
	{
		windowWidth = 350;
	}

	if(windowTitle == null)
	{
		windowTitle = "Loading...";
	}
	
	if(windowTop == null)
	{
		windowTop = (document.body.offsetHeight - (windowHeight + 50));
	}
	
	if(windowLeft == null)
	{
		windowLeft = (document.body.offsetWidth - (windowWidth + 50));
	}

	//create the new window always using
	//the window manager of the main window	
	var targetWindow = top.radopen(windowName);
	
	//congifure the new window
	if(targetWindow != null)
	{
		with(targetWindow)
		{
			SetUrl(pageUrl);
			MoveTo(windowLeft, windowTop);
			SetSize(windowWidth, windowHeight);
			SetTitle(windowTitle);
			SetModal(false);
			if(typeof(Telerik) != "undefined")
			{
				set_behaviors(Telerik.Web.UI.WindowBehaviors.Close);
			}
		}
	}
	
	//return the window read for the calling function to show
	return targetWindow;
}
