// Redirect browser
function RedirectBrowser(stringURL)
{
    // Redirect client browser
    window.location = stringURL;
    
    // Return true
    return true;
}

// Go ro main page
function GotoMainPage() {
    // Redirect client
    return RedirectBrowser("/Default.aspx");

    // Return true
    return true;
}

// Master page onload actions
function PortalMasterOnLoad()
{
    // Fix RAD combobox widths
    //FixRadComboBoxes();
        
    // Return true
    return true;
}

// Get current Rad window
function GetRadWindow()
{
    // Define variables
    var objectWindow = null;

    // Check if window.radWindow is found
    if (window.radWindow) // window.radWindow was found. This will work in Mozilla in all cases, including clasic dialog
    {
        // Set Rad window object
        objectWindow = window.radWindow;
    }
    // Check if window.frameElement.radWindow is found
    else if (window.frameElement.radWindow) // window.frameElement.radWindow. This will work in IE
    {
        // Set Rad window object
        objectWindow = window.frameElement.radWindow;
    }
    // Return Rad window object
    return objectWindow;
}  

// Close current Rad window
function CloseRadWindowAJAX(booleanRunServerCommands, objectReturnValue)
{
    // Close Rad window
    var objectRadWindow = GetRadWindow();
    
    // Close window with parameter
    objectRadWindow.close(objectReturnValue);
    
    // Return passed parameter (if true then server side commands are executed, if false then not)
    return booleanRunServerCommands;
}

// Close Rad window if it exist
function CloseRadWindow(booleanReturnValue)
{
    // Define variables
    var objectWindow = null;

    // Get Rad window
    var objectRadWindow = GetRadWindow();
        
    // Close Rad window
    objectRadWindow.Close();
    
    // return
    return booleanReturnValue;
}

// Close AJAX wait window
function CloseAJAXWaitWindow()
{
    // Define variables
    var objectRadWindowManager = null;
    var objectWindow = null;

    // Get Rad window manager
    objectRadWindowManager = GetRadWindowManager();
    
    // Check if Rad window manager was found
    if (objectRadWindowManager) // Rad window manager was found
    {
       // Get Rad window
        var objectRadWindow = objectRadWindowManager.getWindowByName("windowAJAXWait");
        
        // Check if Rad window was found
        if (objectRadWindow) // Rad window was found
        {
            // Close Rad window
            objectRadWindow.Close();
        }
    }
    
    // Return true
    return true;
}

// Show Rad window if it exist
function ShowRadWindow(stringRadWindow)
{
    // Define variables
    var objectWindow = null;

    // Get Rad window manager
    var objectRadWindowManager = GetRadWindowManager();
        
    // Check if Rad window manager was found
    if (objectRadWindowManager) // Rad window manager was found
    {
        // Get Rad window
        objectWindow = objectRadWindowManager.getWindowByName(stringRadWindow);
        
        // Check if Rad window was found
        if (objectWindow) // Rad window was found
        {
            // Show window
            objectWindow.Show();
        }
    }
    
    // Return true
    return true;
}

// Create Rad window
function CreateRadWindow(stringName, stringPage, stringTitle, booleanModal, integerWidth, integerHeigth, booleanCenter, stringOnCloseFunction)
{            
    // Open Rad window
    var objectRadWindow = window.radopen(stringPage, stringName);
    
    // Set window to modal
    objectRadWindow.SetModal(booleanModal);
    
    // Set window size
    objectRadWindow.SetSize(integerWidth, integerHeigth);
    
    // Set call back function
    objectRadWindow.OnClientClose = stringOnCloseFunction;
    
    // Set window title
    objectRadWindow.SetTitle(stringTitle);
    
    // Check if window should be centered
    if (booleanCenter) // Window should be centered
    {
        // Center window
        objectRadWindow.Center();
    }
    
    // Return Rad window
    return objectRadWindow;
}

// Method to be executed when Rad Window is shown (loading content)
function RadWindowOnClientShow(sender, eventArgs)
{
    // Get Rad window
    var objectRadWindow = sender;
    
    // Set statusbar text
    objectRadWindow.set_status("Loading window...");
    
    // Return true
    return true;
}

// Method to be executed when Rad Window is loaded (finished loading content)
function RadWindowOnClientPageLoad(sender, eventArgs)
{
    // Get Rad window
    var objectRadWindow = sender;
    
    // Set statusbar text
    objectRadWindow.set_status("Window loaded.");
    
    // Return true
    return true;
}

// Get window width
function GetWindowWidth()
{
    // Define variables
    var integerWidth = 0;
    
    // Check if window.innerWidth is available (used on Mozilla and Opera)
    if (typeof(window.innerWidth) == 'number' ) // window.innerWidth is available
    {
        // Set width
        integerWidth = window.innerWidth;
    }
    // Check if document.documentElement.clientWidth is available (used on IE 6+ compliant mode)
    else if ( document.documentElement && document.documentElement.clientWidth ) // document.documentElement.clientWidth is available
    {
        // Set width
        integerWidth = document.documentElement.clientWidth;
    }
    // Check if document.body.clientWidth is available (last place to check)
    else if( document.body && document.body.clientWidth ) // document.body.clientWidth is available
    {
        // Set width
        integerWidth = document.body.clientWidth;
    }
    
    // Return window width
    return integerWidth;
}
 
// Refresh parent page   
function RefreshParentPage(stringURL)
{
    // Check if page is loaded that should be refreshed
    if (Left(GetRadWindow().BrowserWindow.location.pathname, String(stringURL).length).toLowerCase() == stringURL.toLowerCase()) // Page is loaded and should be refreshed
    {
        // Reload page
        GetRadWindow().BrowserWindow.location.reload();
    }
}

// Left function
function Left(stringText, integerLength)
{
    // Check wanted length
    if (integerLength <= 0) // Length is zero or less
        // Return empty string
        return "";
    else if (integerLength > String(stringText).length) // Text is shorter than wanted length
        // Return whole text
        return stringText;
    else // Text is longer than specified length
        // Return wanted amount of characters
        return String(stringText).substring(0, integerLength);
}

// Left function
function Right(stringText, integerLength)
{
    // Check wanted length
    if (integerLength <= 0) // Length is zero or less
        // Return empty string
        return "";
    else if (integerLength > String(stringText).length) // Text is shorter than wanted length
        // Return whole text
        return stringText;
    else {// Text is longer than specified length
       // Return wanted amount of characters
       return String(stringText).substr(String(stringText).length - integerLength, integerLength)
    }
}

// Disable all controls on page
function DisableControls()
{
    // Show AJAX wait window
    ShowRadWindow("windowAJAXWait");
    
    // Return true
    return true;
}

// Enable all controls on page
function EnableControls()
{
    // Close AJAX wait window
    CloseAJAXWaitWindow();
    
    // Return true
    return true;
}

// Show context menu
function ShowContextMenu(stringRadMenu, e)
{
    // Get menu
    var menu = stringRadMenu;
    
    // Show menu
    menu.Show(e);

    // Stop IE from closing the menu
    e.cancelBubble = true;
    
    // Check if stopPropagation exists
    if (e.stopPropagation) // Propagation exists
    {
        // Stop Firefox/Modzilla/Opera from closing the menu
        e.stopPropagation();
    }
    
    // Return true
    return true;
}

// Fix Rad comboboxes
function FixRadComboBoxes()  
{
    // Check if Rad comboboxes exists on page
    if (window.tlrkComboBoxes != undefined)
    {
        // Loop trough all comboboxes
        for (var integerIndex in tlrkComboBoxes)
        {
            // Check if Rad combobox exists
            if (tlrkComboBoxes[integerIndex] != null)
                {
                // Check if ClientWidhtHidden is defined
                if (tlrkComboBoxes[integerIndex].ClientWidthHidden != null)
                {
                    // Fix combobox
                    tlrkComboBoxes[integerIndex].FixUp(tlrkComboBoxes[integerIndex].InputDomElement, true);
                }
            }
        }
    }
    
    // Return true
    return true;
}

// When search combobox result is selected
function RadComboboxSearchSelectedIndexChanged(sender, eventArgs)
{
    // Introduce variables
    var arrayValue = new Array();
            
    // Add values to AJAX request
    arrayValue.push("CenteroSearchCombobox");
    arrayValue.push(Right(sender.get_id(), sender.get_id().lastIndexOf('_') + 1));
    arrayValue.push(String(eventArgs.get_item().get_index()));

    // Send AJAX request and return value
    return SendAJAXRequest(arrayValue);
    //return SendAJAXRequestWithTarget(sender.get_id(), '{"Command":"Select","Index":' + String(eventArgs.get_item().get_index()) + '}');
}

// When user clicks context menu item in Rad treeview
function RadTreeViewClientContextMenuItemClicking(sender, eventArgs)
{
    // Introduce variables
    var booleanReturnValue = true;
    
    // Get menu item clicked
    var objectMenuItem = eventArgs.get_menuItem();
    
    // Check if item is for closing the menu
    if (objectMenuItem.get_value() == "CloseMenu")
    {
        // Close menu
        objectMenuItem.get_menu().hide();
        
        // Set return value
        booleanReturnValue = false;
    }
    
    // Return value
    return booleanReturnValue;
}

// When Rad toolbar button is clicked
function onButtonClicked(sender, eventArgs)
{
    // Check if button is for export
    if (Left(eventArgs.get_item().get_value(), 6).toLowerCase() == "export") // Button is for export
    {
        // Get Rad grid
        var classRadGrid = $find(eventArgs.get_item().get_commandName());
        
        // Check if Grid was found
        if (classRadGrid != null) // Grid was found
        {
            // Get master table view
            var classMasterTableView = classRadGrid.get_masterTableView();
            
            // Check if master table view was found
            if (classMasterTableView != null) // Master table view found
            {
                // Check if export is for Excel
                if (Right(eventArgs.get_item().get_value(), 5).toLowerCase() == "excel") // Export is for Excel
                {
                    // Export grid
                    classMasterTableView.exportToExcel("CLPMExport.xls")
                }
                else if (Right(eventArgs.get_item().get_value(), 4).toLowerCase() == "word") // Export is for Word
                {
                    // Export grid
                    classMasterTableView.exportToWord("CLPMExport.doc")
                }
                else if (Right(eventArgs.get_item().get_value(), 3).toLowerCase() == "pdf") // Export is for PDF
                {
                    // Export grid
                    classMasterTableView.exportToPdf("CLPMExport.pdf")
                }
                else if (Right(eventArgs.get_item().get_value(), 3).toLowerCase() == "csv") // Export is for Word
                {
                    // Export grid
                    classMasterTableView.exportToCsv("CLPMExport.csv")
                }
                
                // Return false
                return false;
            }
        }
    }
    
    // Return true
    return true;
}

