 // function to prevent closing of telerik dropdown control
    function stopPropagation(e) {
        e.cancelBubble = true;
        if (e.stopPropagation) {
            e.stopPropagation();
        }
    }

    //Get the current telerik RadWindow
    function GetRadWindow() {
        var oWindow = null;
        if (window.radWindow) oWindow = window.radWindow;
        else if (window.frameElement && window.frameElement.radWindow) oWindow = window.frameElement.radWindow;
        return oWindow;
    }

    function CloseRadWindow() {
        var wnd = GetRadWindow();
        wnd.close();
    }

    function CloseRadWindowWithArgument(arg) {
        var wnd = GetRadWindow();
        if (wnd != null) 
        {
            wnd.close(arg);
        }
    }

    function CloseRadWindowWithReload(toReload) {
        var wnd = GetRadWindow();
        if (wnd != null) {
            var obj = new Object();
            obj.reload = toReload;
            wnd.close(obj);
        }
    }

    function CloseRadWindowWithFullReload() {
        var oWnd = GetRadWindow();
        var parentWnd = oWnd.BrowserWindow;
        oWnd.close();
        if (parentWnd != null) {
            parentWnd.location.href = parentWnd.location.href;
        }
    }

    function reloadParentDragWindowAndClose(toReloadVar, addVariableToUrl) {
        var oWnd = GetRadWindow();
        var obj = new Object();
        obj.reload = toReloadVar;
        var parentWnd = oWnd.BrowserWindow;
        if (parentWnd != null) {
            var parentRadWindowManager = parentWnd.GetRadWindowManager();

            var allWindows = parentRadWindowManager.GetWindows();
            if (allWindows != null && allWindows.length > 0) {

                for (var i = 0; i < allWindows.length; i++) {
                    if (allWindows[i].GetUrl() && (allWindows[i].GetUrl().indexOf('Apply') > 0 || allWindows[i].GetUrl().indexOf('Manage') > 0 || allWindows[i].GetUrl().indexOf('RequestPoolDetail') > 0)) {
                        if (addVariableToUrl) {
                            allWindows[i].SetUrl(allWindows[i].GetUrl() + "&ReloadParent=true");
                        }
                        else {
                            allWindows[i].reload();
                        }
                    }
                }
            }
        }
        oWnd.close(obj);
    }

    function closeParentDragWithReload() {
        reloadParentDragWindowAndClose(true, true);
    }

    function SetReloadArgument(arg) {
        var wnd = GetRadWindow();
        if (wnd != null) {
            var obj = new Object();
            obj.reload = arg;
            wnd.Argument = obj;
        }
    }

    function OpenRadWindow(mngrId, wndId, url) {
        var mngr = $find(mngrId);
        if (mngr != null) {
            var wnd = mngr.GetWindowByName(wndId);
            if (wnd != null) {
                wnd.setUrl(url);
                wnd.show();
                return wnd;
                //wnd.maximize();
            }
        }
        return null;
    }


    //String trim functions
    String.prototype.trim = function() {
        return this.replace(/^\s+|\s+$/g, "");
    }
    String.prototype.ltrim = function() {
        return this.replace(/^\s+/, "");
    }
    String.prototype.rtrim = function() {
        return this.replace(/\s+$/, "");
    }
    
    //Check if a value is numeric
    function isNumber(n) {
        return !isNaN(parseFloat(n)) && isFinite(n);
    }

    function showDayOfWeekName(containerId, dayOfWeekIndex) {

        var containerObj = document.getElementById(containerId);
        if (containerObj != null) {
            containerObj.innerHTML = DaysOfWeekArray[dayOfWeekIndex];
        }
   }

    /***********************************************
    * Disable Text Selection script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
    * This notice MUST stay intact for legal use
    * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
    ***********************************************/

    function disableSelection(target) {
        if (typeof target.onselectstart != "undefined") //IE route
            target.onselectstart = function() { return false }
        else if (typeof target.style.MozUserSelect != "undefined") //Firefox route
            target.style.MozUserSelect = "none"
        else //All other route (ie: Opera)
            target.onmousedown = function() { return false }
        target.style.cursor = "default"
    }

    function reloadWindow() {
        window.location.href = window.location.href;
    }









    //execute custom function when press the "X" button in the popup
//    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "closeWindow", "AddBeforeCloseEvent({ closewindow:function(){reloadParentWindowAndClose();}});", true);
//    if the custom function returns true, then the window will not be closed.

    //or execute default function - in this case CloseRadWindowWithArgument(true);
//    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "closeWindow", "AddBeforeCloseEvent(null);", true);
    function onBeforeWindowClose(oWinow, args) {
        oWinow.remove_beforeClose(onBeforeWindowClose);
        var stopclosing = false;
        if (oWinow.Argument) {
            if (oWinow.Argument.closewindow) {
                stopclosing = oWinow.Argument.closewindow();
            }
            else {
                //CloseRadWindowWithReload(true);
                CloseRadWindowWithArgument(true);
            }
        }
        else {
            //CloseRadWindowWithReload(true);
            CloseRadWindowWithArgument(true);
        }

        args.set_cancel(stopclosing);

        // Reattach the handles after the window is closed.
        // RECOMMENDATION: If the DestroyOnClose="true" property is set in in the RadWindow's declaration,
        // then remove this line of the code:
        oWinow.add_beforeClose(onBeforeWindowClose);
    }

    function AddBeforeCloseEvent(args) {
        // Get the RadWindoiw object that contains this page
        var oWindow = GetRadWindow();
        oWindow.Argument = args;
        
        // Attach RadWindow's OnClientBeforeClose handler
        oWindow.add_beforeClose(onBeforeWindowClose);
    }

    function RemoveBeforeCloseEvent() {
        // Get the RadWindoiw object that contains this page
        var oWindow = GetRadWindow();
        
        // Remove RadWindow's OnClientBeforeClose handler
        oWindow.remove_beforeClose(onBeforeWindowClose);
    }

    function pageUnload() {
        // Get the RadWindoiw object that contains this page
        var oWindow = GetRadWindow();
        
        // NOTE! If the DestroyOnClose="true" is set in the RadWindow's declaration,
        // then the oWindow object will be 'null'.
        if (oWindow) {
            // Detach RadWindow's OnClientBeforeClose handler
            oWindow.remove_beforeClose(onBeforeWindowClose);
        }
    }
