private void toggleBackButton(bool showIfValid)
 {
     if (showIfValid)
     {
         BackButton.Visibility = ProjectFunctions.BackButtonVisibility();
     }
     else
     {
         BackButton.Visibility = Visibility.Hidden;
     }
 }
        // ---------------------------------------------------------- //
        // -------------------- Data Management --------------------- //
        // ---------------------------------------------------------- //

        // ------------- Data retrieval ------------- //

        private void toggleBackButton()
        {
            BackButton.Visibility = ProjectFunctions.BackButtonVisibility();
            if (BackButton.IsVisible)
            {
                BackButton.ToolTip = ProjectFunctions.BackButtonTooltip();
                double adjust = 20;
                AddButton.Width   = AddButton.ActualWidth - adjust;
                AmendButton.Width = AmendButton.ActualWidth - adjust;
                Thickness margin = AmendButton.Margin;
                margin.Right        = margin.Right - adjust - 5;
                AmendButton.Margin  = margin;
                RemoveButton.Width  = RemoveButton.ActualWidth - adjust;
                margin              = RemoveButton.Margin;
                margin.Right        = margin.Right - (2 * (adjust + 5));
                RemoveButton.Margin = margin;
                BackButton.Width    = BackButton.ActualWidth - adjust;
            }
        }
        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                pageMode = PageFunctions.pageParameter(this, "Mode");
                Globals.ProjectSourceMode = pageMode;
                //Globals.ProjectSourcePage = "ProjectPage"; // Now set in Page Functions beforehand
                if (pageMode == PageFunctions.View || !Globals.MyPermissions.Allow("EditProjects"))
                {
                    viewOnly = true;
                }
                if (pageMode != PageFunctions.Lookup)
                {
                    PageFunctions.ShowFavouriteButton();
                }
            }
            catch (Exception generalException)
            {
                MessageFunctions.Error("Error retrieving query details", generalException);
                closePage(true);
            }

            if (pageMode == PageFunctions.View)
            {
                AddButton.Visibility = CommitButton.Visibility = Visibility.Hidden;
                AmendImage.SetResourceReference(Image.SourceProperty, "ViewIcon");
            }
            else if (pageMode == PageFunctions.Amend)
            {
                PageHeader.Content   = "Amend or Manage Projects";
                Instructions.Content = "Use filters to restrict results and column headers to sort them, then choose the required option.";
                HeaderImage2.SetResourceReference(Image.SourceProperty, "AmendIcon");
                CommitButton.Visibility = Visibility.Hidden;
                AddButton.Visibility    = Globals.MyPermissions.ShowOrCollapse("AddProjects");
            }
            else if (pageMode == PageFunctions.Lookup)
            {
                PageHeader.Content   = "Select Project";
                Instructions.Content = "Use filters to restrict results and column headers to sort them, then choose the desired project.";
                HeaderImage2.SetResourceReference(Image.SourceProperty, "SearchIcon");
                AddButton.Visibility  = AmendButton.Visibility = BackButton.Visibility = MoreButton.Visibility = Visibility.Hidden;
                CommitButton.Margin   = AmendButton.Margin;
                CancelButtonText.Text = "Cancel";
            }

            try
            {
                BackButton.Visibility = ProjectFunctions.BackButtonVisibility("ProjectPage");
                BackButton.ToolTip    = ProjectFunctions.BackButtonTooltip();
                refreshClientCombo();
                refreshPMsCombo();
                if (ProjectFunctions.PMFilterList.Exists(ssr => ssr.ID == Globals.MyStaffID))
                {
                    PMsCombo.SelectedItem = ProjectFunctions.PMFilterList.First(ssr => ssr.ID == Globals.MyStaffID);
                }
                refreshStatusCombo();
                toggleMoreButton();
            }
            catch (Exception generalException)
            {
                MessageFunctions.Error("Error refreshing filters", generalException);
                closePage(true);
            }
        }