﻿// REFERENCE:  http://www.telerik.com/help/aspnet-ajax/window_programmingradwindowmethods.html


// Returns a handle of the RadWindow that this page is currently being viewed in.
function RadWindow_GetHandle() {
    var oWindow = null;
    if (window.radWindow) oWindow = window.radWindow;
    else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;
    return oWindow;
}




// Defines the valid arguments that can be provided to function OpenRadWindow().
function radWindowArgs() {
    this.url = "";

    // The windowID should be set to "null" if you never want to
    // retain the contents upon future opens of the window in the
    // same page.
    this.windowID = null;

    this.width = 400;
    this.height = 400;
    this.isModal = true;
}




// Opens a RadWindow.
function OpenRadWindow(radWindowArgs) {
    var radWin = radopen(radWindowArgs.url, radWindowArgs.windowID);
    radWin.setSize(radWindowArgs.width, radWindowArgs.height);
    radWin.set_modal(radWindowArgs.isModal);
    radWin.center();
}




// Close the RadWindow.
function RadWindow_Close(refreshParent) {
    RadWindow_GetHandle().close();

    if (refreshParent) {
        top.location.href = top.location.href;
    }
}




// Refreshes the contents of the RadWindow.
function RadWindow_Refresh() {
    RadWindow_GetHandle().reload(true);
}




// Refreshes the contents of the parent window.
// The RadWindow will automatically close.
function RadWindow_RefreshParent() {
    //window.parent.location.reload(true);
    window.parent.location.href = window.parent.location.href;
}




// This will set the parent window to the location defined.
// The RadWindow will automatically close.
function RadWindow_SetLocation(url) {
    window.parent.location.href = url;
}




// Change the title of the RadWindow.
function RadWindow_ChangeTitle(newTitle) {
    RadWindow_GetHandle().set_title(newTitle);
}




// Get the current URL viewed inside of the RadWindow.
function RadWindow_GetCurrentUrl() {
    return RadWindow_GetHandle().get_navigateUrl();
}




//the following code use radconfirm to mimic the blocking of the execution thread.
//The approach has the following limitations:
//1. It works only for elements that have *click* method, e.g. links and buttons
//2. It cannot be used in if(!confirm) checks
window.blockConfirm = function(text, mozEvent, oWidth, oHeight, callerObj, oTitle) {
    var ev = mozEvent ? mozEvent : window.event; //Moz support requires passing the event argument manually 
    //Cancel the event 
    ev.cancelBubble = true;
    ev.returnValue = false;
    if (ev.stopPropagation) ev.stopPropagation();
    if (ev.preventDefault) ev.preventDefault();

    //Determine who is the caller 
    var callerObj = ev.srcElement ? ev.srcElement : ev.target;

    //Call the original radconfirm and pass it all necessary parameters 
    if (callerObj) {
        //Show the confirm, then when it is closing, if returned value was true, automatically call the caller's click method again. 
        var callBackFn = function(arg) {
            if (arg) {
                callerObj["onclick"] = "";
                if (callerObj.click) callerObj.click(); //Works fine every time in IE, but does not work for links in Moz 
                else if (callerObj.tagName == "A") //We assume it is a link button! 
                {
                    try {
                        eval(callerObj.href)
                    }
                    catch (e) { }
                }
            }
        }

        radconfirm(text, callBackFn, oWidth, oHeight, callerObj, oTitle);
    }
    return false;
}