﻿function GetRadWindow()
{
    var oWindow = null;
    if (window.radWindow) oWindow = window.radWindow; //Will work in Moz in all cases, including clasic dialog
    else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;//IE (and Moz az well)
    return oWindow;
}


function CloseAndConfirm()
{
    if(RadWindowClosePrompt)
    {
        return confirm("Are you sure you want to close this window? Any unsaved changes will be lost.");
    }
    else
    {
        return true;
    }
}

function CloseRadOnReload()
{
    GetRadWindow().Close();
}

function OpenRadWindow(url, title, width, height, oncloseFunction, features)
{
    RadWindowClosePrompt = false;
    //Getting rad window manager
    var oManager = GetRadWindowManager();
    //Success. Create new window
    var oNew = oManager.Open(url, null);
    //Success. Setting a size and a Url to the window using its client API before showing
    oNew.set_width(width);
    oNew.set_height(height);
    oNew.set_title(title);
    if(oncloseFunction != undefined && oncloseFunction != "")
    {
        oNew.add_close(eval(oncloseFunction));
    }

    oNew.Center();
    if(features != undefined)
    {
        var arrFeatures = features.split(",");
        var radFeatures;
        for(var i = 0; i < arrFeatures.length; i++)
        {
            if(arrFeatures[i].toLowerCase() == "prompt")
            {
                RadWindowClosePrompt = true;
            }
            else
            {
                var behaviour = eval("Telerik.Web.UI.WindowBehaviors." + arrFeatures[i].substring(0,1).toUpperCase() + arrFeatures[i].substring(1).toLowerCase());
                if(radFeatures == undefined)
                {
                    radFeatures = behaviour;
                }
                else
                {
                    radFeatures += behaviour;
                }
            }
        }
        oNew.set_behaviors(radFeatures);
    }
}

function CloseRadWindow(value)
{
    top.RadWindowClosePrompt = false;
    var oWindow = GetRadWindow();
	oWindow.close(value);
	top.RadWindowClosePrompt = true;
}

function SizeToFitHeight()
{
    window.setTimeout(
    function()
    {
        var oWnd = GetRadWindow();
        oWnd.set_height(document.body.scrollHeight + 70);
        oWnd.center();
        
    }, 0);
}
