private async void ConfigPane_AddServer_Go_Click(object sender, RoutedEventArgs e)
        {
            if (ConfigPane_AvailableJenkinsServerList.SelectedItem == null || !(ConfigPane_AvailableJenkinsServerList.SelectedItem is JenkinsServer))
            {
                return;
            }

            // Secure and store the new login credentials
            JenkinsTasks.SecureJenkinsCreds(
                ConfigPane_AddServer_Username_TextBox.Text,
                ConfigPane_AddServer_Token_TextBox.Text,
                (ConfigPane_AvailableJenkinsServerList.SelectedItem as JenkinsServer).id
                );

            // Test login
            if (!await JenkinsTasks.checkServerLogin(ConfigPane_AvailableJenkinsServerList.SelectedItem as JenkinsServer))
            {
                MessageBox.Show("Login failed. Verify you are using the correct user name and API token.", "Login Failed");
                return;
            }

            // Add the server to the config
            AddServerToUserConfig(ConfigPane_AvailableJenkinsServerList.SelectedItem as JenkinsServer);
            JenkinsInfo.Instance.ConfiguredJenkinsServers = LoadConfiguredServersFromUserConfig();

            // For some reason the ConfiguredServers thing won't bind properly, so we have to update it each time we change it
            Config_ConfiguredJenkinsServerList.ItemsSource = JenkinsInfo.Instance.ConfiguredJenkinsServers;

            // Deselect the available server, and empty the text boxes
            ConfigPane_AvailableJenkinsServerList.SelectedIndex = -1;
            ConfigPane_AddServer_Username_TextBox.Text          = null;
            ConfigPane_AddServer_Token_TextBox.Text             = null;
        }
        private async void LookupResults_Reset_Button_Click(object sender, RoutedEventArgs e)
        {
            APCAccount account = (APCAccount)(sender as Button).DataContext;

            if (account.JenkinsServer != null &&
                account.LookupType != null &&
                account.LookupValue != null &&
                account.LookupValue.Trim() != "")
            {
                await JenkinsTasks.RunAPCAccountLookup(account);
            }
        }
        private async void Button_ChangeInactivityTimeout_Click(object sender, RoutedEventArgs e)
        {
            APCAccount Account         = (APCAccount)(sender as Button).DataContext;
            string     NewTimeoutValue = newTimeoutValue_TextBox.Text.Trim();

            if (NewTimeoutValue == null || NewTimeoutValue == "")
            {
                return;
            }

            await JenkinsTasks.updateTimeout(Account, NewTimeoutValue);
        }
        private async void LookupResults_DatabaseList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count < 1 || e.AddedItems[0] == null || !((e.AddedItems[0] as APCDatabase) is APCDatabase))
            {
                return;
            }

            if ((e.AddedItems[0] as APCDatabase).UserLoadStatus == JenkinsBuildStatus.Failed || (e.AddedItems[0] as APCDatabase).UserLoadStatus == JenkinsBuildStatus.NotStarted)
            {
                (e.AddedItems[0] as APCDatabase).Users = await JenkinsTasks.getDatabaseUsers((e.AddedItems[0] as APCDatabase), (e.AddedItems[0] as APCDatabase).Database_APCAccount.JenkinsServer);
            }
        }
        private async void LookupResults_DatabaseBackups_RetainBackup_Click(object sender, RoutedEventArgs e)
        {
            if (!((APCDatabaseBackupRestorable)LookupResults_BackupList.SelectedItem is APCDatabaseBackupRestorable))
            {
                return;
            }

            APCDatabaseBackupRestorable RestorableBackup = (APCDatabaseBackupRestorable)LookupResults_BackupList.SelectedItem;

            if (RestorableBackup != null)
            {
                await JenkinsTasks.RetainDatabaseBackup(RestorableBackup);
            }
        }
        private async void Button_UnlockDatabase_Click(object sender, RoutedEventArgs e)
        {
            if (!((APCDatabase)LookupResults_DatabaseList.SelectedItem is APCDatabase))
            {
                return;
            }

            APCDatabase Database = (APCDatabase)LookupResults_DatabaseList.SelectedItem;

            if (Database != null)
            {
                await JenkinsTasks.unlockDatabase(Database);
            }
        }
        private async void Button_ResetUserPassword_Click(object sender, RoutedEventArgs e)
        {
            if (!((APCDatabaseUser)LookupResults_UserList.SelectedItem is APCDatabaseUser))
            {
                return;
            }

            APCDatabaseUser User = (APCDatabaseUser)LookupResults_UserList.SelectedItem;

            if (User != null)
            {
                await JenkinsTasks.resetUserPassword(User);
            }
        }
        private async void LookupResults_DatabaseBackups_LoadBackups_Click(object sender, RoutedEventArgs e)
        {
            if (!((APCDatabase)LookupResults_DatabaseList.SelectedItem is APCDatabase))
            {
                return;
            }

            APCDatabase Database = (APCDatabase)LookupResults_DatabaseList.SelectedItem;

            if (Database.Server != null &&
                Database.Name != null)
            {
                Database.Backups = await JenkinsTasks.getDatabaseBackups(Database, Database.Database_APCAccount.JenkinsServer);

                Database.RestoreableBackups = JenkinsTasks.GetRestorableBackupsFromFiles(Database.Backups);
            }
        }
        private async void NewLookupPane_LookupValueBox_KeyDown(object sender, KeyEventArgs e)
        {
            APCAccount account = (APCAccount)(sender as TextBox).DataContext;

            account.LookupValue = (sender as TextBox).Text; // Force this to apply for every keydown, as the data binding doesn't pass it otherwise until the box is left

            if (e.Key == Key.Enter || e.Key == Key.Return)
            {
                e.Handled = true;

                if (account.JenkinsServer != null &&
                    account.LookupType != null &&
                    account.LookupValue != null &&
                    account.LookupValue.Trim() != "")
                {
                    await JenkinsTasks.RunAPCAccountLookup(account);
                }
            }
        }
        private async void Button_ResendWelcomeEmail_Click(object sender, RoutedEventArgs e)
        {
            string SpecifiedEmail = specifyEmail_TextBox.Text.Trim();

            APCAccount         Account = (APCAccount)(sender as Button).DataContext;
            WelcomeEmailSendTo SendTo  = WelcomeEmailSendTo.PrimaryAccountEmail;

            if (specifyEmail_RadioButton.IsChecked == true)
            {
                SendTo = WelcomeEmailSendTo.SpecifiedEmail;

                if (SpecifiedEmail == null || SpecifiedEmail == "")
                {
                    return;
                }
            }

            await JenkinsTasks.resendWelcomeEmail(Account, SendTo, SpecifiedEmail);
        }
        public MainWindow()
        {
            if (!ValidateUserConfigFile())
            {
                CreateDefaultUserConfigFile();
            }

            ApplicationSettings.Load();

            JenkinsInfo.Instance.AvailableJenkinsServers  = JenkinsTasks.getJenkinsServerList();
            JenkinsInfo.Instance.ConfiguredJenkinsServers = LoadConfiguredServersFromUserConfig();
            JenkinsInfo.Instance.LookupTypeList           = JenkinsTasks.buildAPCLookupTypeList();

            if (JenkinsInfo.Instance.ConfiguredJenkinsServers.Count < 1)
            {
                CurrentWindowState.Instance.DisplayMode = WindowDisplayMode.Config;
            }
            else
            {
                CurrentWindowState.Instance.DisplayMode = WindowDisplayMode.Lookup;
            }

            LoadConfiguredServersFromUserConfig();

            InitializeComponent();

            LookupListPane_Lookups_ListBox.ItemsSource = LookupResults;

            APCAccount DebugAccount = new APCAccount()
            {
                LookupStatus                  = APCAccountLookupStatus.Successful,
                ResendWelcomeEmailStatus      = JenkinsBuildStatus.Failed,
                ChangeInactivityTimeoutStatus = JenkinsBuildStatus.Successful,
                IITID            = "12345",
                AccountName      = "DebugTest",
                Email            = "*****@*****.**",
                CreateDate       = "2000BC",
                TrialOrPaid      = "Trial",
                SerialNumber     = "12345-ABCDE-67890-FGHIJ",
                SeatCount        = "3",
                SuspendStatus    = "NotSuspended",
                ArchiveStatus    = "NotArchived",
                SiteName         = "DebugSite",
                IISServer        = "DBG1-DBGIIS-01",
                LoginUrl         = "http://localhost/",
                UploadUrl        = "http://localhost",
                ZuoraAccount     = "A00123456",
                DeleteStatus     = "NotDeleted",
                AccountType      = "ActPremiumCloudPlus",
                TimeoutValue     = "60",
                LookupTime       = DateTime.Now,
                LookupCreateTime = DateTime.Now,
                JenkinsServer    = new JenkinsServer()
                {
                    id   = "DBG1",
                    name = "Debug 1",
                    url  = "http://localhost/"
                }
            };

            //LookupResults.Add(DebugAccount);
        }