<%@ Page Language="C#" AutoEventWireup="true" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server" />
        <telerik:RadWindowManager ID="RadWindowManager1" runat="server">
            <Windows>
                <telerik:RadWindow ID="RadWindow1" runat="server" Modal="true" Width="800px" Height="600px" />
            </Windows>
        </telerik:RadWindowManager>

        <script type="text/javascript">
            function OnClientFileOpen(sender, args) {

                /***Newly Added */
                var filePath = args.get_path();
                var fileExtension = filePath.substring(filePath.lastIndexOf('.') + 1).toLowerCase();

                var viewerUrl = '';

                // Determine the action based on file extension
                switch (fileExtension) {
                    case 'pdf':
                    case 'xls':
                    case 'xlsx':
                    case 'xlsm':
                    case 'doc':
                    case 'docx':
                        // Cancel the default open event for non-image and video/audio files
                        args.set_cancel(true);
                        viewerUrl = "PdfViewer.aspx?file=" + encodeURIComponent(filePath);
                        // Open the file in a RadWindow
                        var oWnd = window.radopen(viewerUrl, "RadWindow1");
                        //oWnd.setSize(900, 600);
                        oWnd.maximize();
                        oWnd.set_behaviors(Telerik.Web.UI.WindowBehaviors.Maximize + Telerik.Web.UI.WindowBehaviors.Close + Telerik.Web.UI.WindowBehaviors.Move);
                        break;

                    case 'msg':
                        // Handle .msg files by prompting a download
                        args.set_cancel(true); // Cancel the default open event
                        window.location = filePath; // Redirect to the file path to initiate download
                        break;

                    default:
                        
                        var windowManager = sender.get_windowManager()

                        // Setting the Width & Height
                        windowManager.set_width(900);
                        windowManager.set_height(600);

                        // Setting the Initial Behaviors
                        windowManager.set_initialBehaviors(Telerik.Web.UI.WindowBehaviors.Maximize);

                        // Setting the Behaviors
                        windowManager.set_behaviors(Telerik.Web.UI.WindowBehaviors.Maximize + Telerik.Web.UI.WindowBehaviors.Close + Telerik.Web.UI.WindowBehaviors.Move);

                        setTimeout(function () {
                            var radWindow = sender.get_windowManager().get_windows(); 
                            
                            var iframeDocument = radWindow[0].get_contentFrame().contentWindow.document;

                            // Disable the context menu in the iframe
                            iframeDocument.addEventListener('contextmenu', function (event) {
                                event.preventDefault();
                                alert('Right-click context menu is disabled on this page.');
                            }, false);


                            //disable shortcuts
                            iframeDocument.addEventListener('keydown', function (event) {
                                if ((event.ctrlKey || event.metaKey) && (event.key === 's' || event.key === 'p' || event.key === 'c')) {
                                    event.preventDefault();
                                    // Optionally, you can alert the user that this action is disabled.
                                    alert('Saving, printing, and copying are disabled on this page.');
                                }
                            }, false);
                        }, 100);

                        

                        return;
                        break;
                }
            }

            // Disable the context menu
            document.addEventListener('contextmenu', function (event) {
                event.preventDefault();
                // Optionally, you can inform the user that the right-click functionality is disabled.
                alert('Right-click context menu is disabled on this page.');
            }, false);

            //disable shortcuts
            document.addEventListener('keydown', function (event) {
                if ((event.ctrlKey || event.metaKey) && (event.key === 's' || event.key === 'p' || event.key === 'c')) {
                    event.preventDefault();
                    // Optionally, you can alert the user that this action is disabled.
                    alert('Saving, printing, and copying are disabled on this page.');
                }
            }, false);

        </script>

        <telerik:RadFileExplorer runat="server" ID="RadFileExplorer1" Width="100%" Height="400px" EnableOpenFile="true"
            OnClientFileOpen="OnClientFileOpen">
            <Configuration ViewPaths="~/Files" UploadPaths="~/Files" DeletePaths="~/Files" />
        </telerik:RadFileExplorer>
    </form>
</body>
</html>
