<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Axium.Web.Default" %>
<%@ Register TagPrefix="axi" TagName="ModalSpinner" Src="~/PatientAccess/Controls/ModalSpinner.ascx" %>
<%=Output%>
<axi:ModalSpinner ID="ModalSpinner" runat="server"/>
<input id="page" type="hidden" value="default" />

<asp:placeholder id="AutoLogout" runat="server"></asp:placeholder>

<script src="Includes/Scripts/PA2.0/pendo.js" type="text/javascript"></script>

<% if(KioskMode()){  %>
    <script type="text/javascript">
        window.addEventListener('DOMContentLoaded', function () {
            $("#headerContainer").remove();
        }, false);

        $(document).ready(function () {
            $('.menu').dropit();
        });

        window.parent.postMessage('loading', '*');
    </script>
<% } %>
<script type="text/javascript">

    window.addEventListener('message', receiveMessage, false);
    
    function receiveMessage(e) {
        if (e.data === 'resizeIframe') {
            resizeIframe('v2', window);
        }
        if (e.data === 'scrollToTop') {
            scrollToTop();
        }
        // Id data received from iframe is redirect, redirect to the default page
        if (e.data?.redirect === true) {
            window.location.href = "Default.aspx?id=1";
        }
    }

    function unhideAuthorizedRepresentative() {
        if ($("#AuthorRepresentDrp option").length > 0) {
            $("#AuthorRepresentDiv").css("display", "flex");
        }
    }

    function unhideSidebarItems() {
        $(".dashboard-nav-dropdown").removeAttr('hidden');
        $(".dashboard-nav-item").removeAttr('hidden');
    }

    function setChangeUsernameAndPasswordPage(pageUrl, pageId) {
        $(".card-username").attr("data-bs-toggle", "tooltip");
        $(".card-username").attr("data-bs-placement", "top");
        $(".card-username").attr("title", "Change username and password");
        $(".card-username").attr("onclick", "handleMenuItemClick('" + pageUrl + "', undefined, " + pageId + ");")
        $(".card-username").css("cursor", "pointer");
        $(".card-username").append("<i class='fa-solid fa-key fa-lg user-key-icon'></i>");

        initializeAllTooltips();
    }

    function resetAutoLogoutTimerClicked() {
        $.idleTimer("reset");
    }

    // function to get specific window
    function getChildWindow(id) {
       return document.getElementById(id).contentWindow;
    }

    function newCheckCanLeavePage(redirectUrl, redirectNode) {
      const iFrame = document.getElementById('iFrameId');
      
      const currentPageOnIframe = iFrame.getAttribute('src');
      
      const formIfrm = currentPageOnIframe.indexOf("iForms/FormList.aspx") !== -1 
        ? iFrame 
        : null;      // Get iForms frame 
        
      const patientFrm = currentPageOnIframe.indexOf("Users/PersonalInformationEdit.aspx") !== -1
       ? iFrame 
       : null;     // AX-17079

      // if not in iForms or not loaded validateNavMenu() or validateNavMenu return true AX-17079 or patient info; then redirect
        if (((formIfrm == null || !$.isFunction(formIfrm.contentWindow.newValidateNavMenu)) && (patientFrm == null || !$.isFunction(patientFrm.contentWindow.newCanLeavePatientInfo)))
            || ((formIfrm != null && formIfrm.contentWindow.newValidateNavMenu(redirectUrl, redirectNode)) || (patientFrm != null && patientFrm.contentWindow.newCanLeavePatientInfo(redirectUrl, undefined, redirectNode)))) {
         
          if (redirectUrl) {
              //window.location.href = redirectUrl;
              toggleNavItem(redirectNode);
              redirectIFrameTo(redirectUrl);
          }
          return true;
      }
      else {
          cancelPageRedirects();
          closeModalSpinner();
          return false;
      }
   }

   // AX-4481.1 For every PA menu, check if there is iForm open and has no saved answers; AX-17079 check if Patient Information no submitted
   function checkCanLeavePage(redirectUrl, redirectId) {
      var formIfrm = document.getElementById('formIfrmId');      // Get iForms frame 
      var patientFrm = document.getElementById('proIfrmId');     // AX-17079

       // if not in iForms or not loaded validateNavMenu() or validateNavMenu return true AX-17079 or patient info; then redirect
       if (((formIfrm == null || !$.isFunction(formIfrm.contentWindow.validateNavMenu)) && (patientFrm == null || !$.isFunction(patientFrm.contentWindow.canLeavePatientInfo)))
        || ((formIfrm != null && formIfrm.contentWindow.validateNavMenu(redirectUrl, redirectId)) || (patientFrm != null && patientFrm.contentWindow.canLeavePatientInfo(redirectUrl, redirectId)))) {
         if (redirectId !== '0')
            window.location.href = redirectUrl + redirectId;
         else
            window.location.href = redirectUrl;
      }
   }

   // AX-17079 For Personal Informatation Left Nav. Panel
   function navigateSideMenu(aClassName, redirectUrl, redirectId) {
      var patientFrm = document.getElementById('proIfrmId');     

      // if not in iForms or not loaded validateNavMenu() or validateNavMenu return true AX-17079 or patient info; then redirect
      if (patientFrm == null || !$.isFunction(patientFrm.contentWindow.canLeavePatientInfo) || patientFrm.contentWindow.canLeavePatientInfo(redirectUrl, redirectId)) {
         if (redirectId !== '0')
            window.location.href = redirectUrl + redirectId;
         else
            window.location.href = redirectUrl;
      }

    }

    function handleMenuItemClick(pageUrl, parentNode, pageId) {
        openModalSpinner();
        if (newCheckCanLeavePage(pageUrl, parentNode)) {
            toggleMobileMenu();
            changeUrl(pageId);
        }
    }
</script>
