// Reload all the jokes from the database
        private async void Button_All_Jokes_Async(object sender, RoutedEventArgs e)
        {
            // Mouse cursor changes to wait when loading data
            var defaultCursor = Window.Current.CoreWindow.PointerCursor;

            Window.Current.CoreWindow.PointerCursor = new CoreCursor(CoreCursorType.Wait, 1);

            try
            {
                CelebrityJokes.Clear();
                var result = await httpCelebrityJokesUri.GetAsync(celebrityJokeUri);

                var celebrityJokeJson = await result.Content.ReadAsStringAsync();

                var celebrityJokes = JsonConvert.DeserializeObject <CelebrityJoke[]>(celebrityJokeJson);
                Window.Current.CoreWindow.PointerCursor = defaultCursor;
                foreach (CelebrityJoke CJ in celebrityJokes)
                {
                    CelebrityJokes.Add(CJ);
                }
            }

            catch (HttpRequestException ex)
            {
                Window.Current.CoreWindow.PointerCursor = defaultCursor;
                var dialog = new MessageDialog(ex.GetType().Name + ": Database connection error. Please check VPN and network connection.");
                await dialog.ShowAsync();

                FileLogger.LogExceptions(DateTime.Now + " " + ex.GetType().Name + ex.Message + ex.StackTrace + "\n" + "\n");
            }
        }
        // Creates a new joke
        private void Button_NewJoke(object sender, RoutedEventArgs e)
        {
            CelebrityJokes.Clear();
            var newJoke = new CelebrityJoke()
            {
                Text = "",
                CelebrityMentioned = "",
                Author             = "",
                Rate = 0
            };

            CelebrityJokes.Add(newJoke);
        }
        // Searching for a celebrity mentioned in the joke and showing the relevant jokes
        private async void Button_Search_Async(object sender, RoutedEventArgs e)
        {
            var input = SearchField.Text;

            SqlConnectionStringBuilder connStringBuilder = new SqlConnectionStringBuilder
            {
                DataSource     = "donau.hiof.no",
                UserID         = "andreot",
                InitialCatalog = "andreot",
                Password       = "******"
            };

            var inputSearch = SearchField.Text;

            try
            {
                //Initialize the Sql-connection
                using (SqlConnection conn = new SqlConnection(connStringBuilder.ToString()))
                {
                    if (inputSearch != "")
                    {
                        //Initialize the Sql-command.
                        //Sql-command : Selects the JokeID from the rows who are matching the search
                        SqlCommand cmd = new SqlCommand("SELECT JokeID FROM dbo.Jokes WHERE CelebrityMentioned LIKE'%" + inputSearch + "%'", conn);

                        if (conn.State == ConnectionState.Closed)
                        {
                            //Opens the database connection
                            conn.Open();

                            // Sends the CommandText to the Connection and builds a SqlDataReader.
                            var reader = cmd.ExecuteReader();
                            CelebrityJokes.Clear();

                            if (reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                    var jokeID = reader.GetInt32(0);
                                    //Gets relevant jokes from database
                                    var result = await httpCelebrityJokesUri.GetAsync(celebrityJokeUri + "//" + jokeID.ToString());

                                    var celebrityJokeJson = await result.Content.ReadAsStringAsync();

                                    var celebrityJokes = JsonConvert.DeserializeObject <CelebrityJoke>(celebrityJokeJson);

                                    // Add relevant jokes to list
                                    CelebrityJokes.Add(celebrityJokes);
                                }
                            }

                            else
                            {
                                var dialog = new MessageDialog("Sorry we couldn't find a joke containing \"" + inputSearch + "\"");
                                await dialog.ShowAsync();
                            }
                        }
                    }
                    else
                    {
                        var dialog = new MessageDialog("Please fill out the search field");
                        await dialog.ShowAsync();
                    }
                }
            }

            catch (SqlException ex)
            {
                var dialog = new MessageDialog("Something went wrong with the SQL connection.");
                await dialog.ShowAsync();

                FileLogger.LogExceptions(DateTime.Now + " " + ex.GetType().Name + ex.Message + ex.StackTrace + "\n" + "\n");
            }
        }
        //Adds the new joke to the list
        private async void Button_Add_Joke_Async(object sender, RoutedEventArgs e)
        {
            var stringContent = new StringContent(JsonConvert.SerializeObject(SelectedJoke), Encoding.UTF8, "application/json");

            try
            {
                if (SelectedJoke != null)
                {
                    if (SelectedJoke.CelebrityMentioned != "" && SelectedJoke.Author != "" && SelectedJoke.Text != "")
                    {
                        // Add the new joke to the database
                        await httpCelebrityJokesUri.PostAsync(celebrityJokeUri, stringContent).ConfigureAwait(false);

                        await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                                                                                                                    async() =>
                        {
                            var dialog = new MessageDialog("Joke added to the database successfully");
                            await dialog.ShowAsync();
                        });
                    }

                    else
                    {
                        var dialog = new MessageDialog("Please fill in the required fields.");
                        await dialog.ShowAsync();
                    }
                }

                else
                {
                    var dialog = new MessageDialog("Please select a joke.");
                    await dialog.ShowAsync();
                }
            }

            catch (HttpRequestException ex)
            {
                var dialog = new MessageDialog(ex.GetType().Name + ": Cannot add joke to database.Please check VPN or network connection.");
                await dialog.ShowAsync();

                FileLogger.LogExceptions(DateTime.Now + " " + ex.GetType().Name + ex.Message + ex.StackTrace + "\n" + "\n");
            }

            try
            {
                await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                                                                                                            async() =>
                {
                    CelebrityJokes.Clear();
                    var result            = await httpCelebrityJokesUri.GetAsync(celebrityJokeUri);
                    var celebrityJokeJson = await result.Content.ReadAsStringAsync();
                    var celebrityJokes    = JsonConvert.DeserializeObject <CelebrityJoke[]>(celebrityJokeJson);

                    foreach (CelebrityJoke CJ in celebrityJokes)
                    {
                        CelebrityJokes.Add(CJ);
                    }
                });
            }

            catch (HttpRequestException ex)
            {
                var dialog = new MessageDialog(ex.GetType().Name + ": Database connection error. Please check VPN and network connection.");
                await dialog.ShowAsync();

                FileLogger.LogExceptions(DateTime.Now + " " + ex.GetType().Name + ex.Message + ex.StackTrace + "\n" + "\n");
            }
        }
        // Loads jokes from the database when page is loaded
        private async void Page_Loaded_Async(object sender, RoutedEventArgs e)
        {
            //Shows a message with the countdown before the weekend
            DayOfWeek dayOfWeek = DateTime.Now.DayOfWeek;

            switch (dayOfWeek)
            {
            case DayOfWeek.Monday:
                WeekendCountdownMessage.Text = Days.Friday - Days.Monday + " DAYS UNTIL THE WEEKEND";
                break;

            case DayOfWeek.Tuesday:
                WeekendCountdownMessage.Text = Days.Friday - Days.Tuesday + " DAYS UNTIL THE WEEKEND";
                break;

            case DayOfWeek.Wednesday:
                WeekendCountdownMessage.Text = Days.Friday - Days.Wednesday + " DAYS UNTIL THE WEEKEND";
                break;

            case DayOfWeek.Thursday:
                WeekendCountdownMessage.Text = Days.Friday - Days.Thursday + " DAY UNTIL THE WEEKEND";
                break;

            case DayOfWeek.Friday:
                WeekendCountdownMessage.Text = "WEEKEND STARTS TONIGHT";
                break;

            case DayOfWeek.Saturday:
                WeekendCountdownMessage.Text = "LET'S ENJOY THE WEEKEND";
                break;

            case DayOfWeek.Sunday:
                WeekendCountdownMessage.Text = "SUNDAY MOOD";
                break;
            }

            // Mouse cursor changes  when loading data
            var defaultCursor = Window.Current.CoreWindow.PointerCursor;

            Window.Current.CoreWindow.PointerCursor = new CoreCursor(CoreCursorType.Wait, 1);

            try
            {
                var result = await httpCelebrityJokesUri.GetAsync(celebrityJokeUri);

                var celebrityJokeJson = await result.Content.ReadAsStringAsync();

                var celebrityJokes = JsonConvert.DeserializeObject <CelebrityJoke[]>(celebrityJokeJson);
                Window.Current.CoreWindow.PointerCursor = defaultCursor;

                foreach (CelebrityJoke CJ in celebrityJokes)
                {
                    CelebrityJokes.Add(CJ);
                }
            }

            catch (HttpRequestException ex)
            {
                Window.Current.CoreWindow.PointerCursor = defaultCursor;
                var dialog = new MessageDialog(ex.GetType().Name + ": Database connection error. Please check VPN and network connection.");
                await dialog.ShowAsync();

                FileLogger.LogExceptions(DateTime.Now + " " + ex.GetType().Name + ex.Message + ex.StackTrace + "\n" + "\n");
            }
        }