private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            string navigationFrom;
            if (NavigationContext.QueryString.TryGetValue("navigationFrom", out navigationFrom))
            {
                NavigationService.RemoveBackEntry();
                NavigationService.RemoveBackEntry();
            }
            string name;

            string category;
            var settings = IsolatedStorageSettings.ApplicationSettings;
            if (NavigationContext.QueryString.TryGetValue("name", out name))
            {
                ObservableCollection<Class> classes;
                if (settings.TryGetValue<ObservableCollection<Class>>("classCollection", out classes))
                {
                    foreach (Class temp in classes)
                    {
                        if (temp.Name == name)
                        {
                            currentClass = temp;
                        }
                    }
                    ObservableCollection<Category> categories = new ObservableCollection<Category>();
                    categories = currentClass.Categories;
                    if (NavigationContext.QueryString.TryGetValue("category", out category))
                        foreach (Category temp in categories)
                        {
                            if (temp.Name == category)
                            {
                                currentCategory = temp;
                                break;
                            }
                        }
                }
            }
            PageTitle.Text = currentCategory.Name;
            grades = currentCategory.Assignments;
            recomputeAverage();

            App ap = (App)Application.Current;
            if (ap.isTrial)
            {
                adControl1.Visibility = Visibility.Visible;
            }
            else
            {
                adControl1.Visibility = Visibility.Collapsed;
            }






        }
        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            string name = "";
            var settings = IsolatedStorageSettings.ApplicationSettings;
         
            if (NavigationContext.QueryString.TryGetValue("name", out name))
            {
                PageTitle.Text = name;
            }

            if (settings.TryGetValue<ObservableCollection<Class>>("classCollection", out classes))
            {
                foreach (Class temp in classes)
                {
                    if (temp.Name == name)
                    {
                        currentClass = temp;
                    }
                }
                nameBox.Text = currentClass.Name;
                teacherBox.Text= currentClass.Teacher;
                categories=currentClass.Categories;
            }
            foreach(Category temp in categories)
            {
                categoryStrings.Add("" + temp.Name + "-" + temp.Weight + "%");
            }
            catBox.ItemsSource=categoryStrings;
            App ap = (App)Application.Current;
            if (ap.isTrial)
            {
                adControl1.Visibility = Visibility.Visible;
            }
            else
            {
                adControl1.Visibility = Visibility.Collapsed;
            }
            
          
        }
        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            string name;
            string category;
            string assignment;
            var settings = IsolatedStorageSettings.ApplicationSettings;
            if (NavigationContext.QueryString.TryGetValue("name", out name))
            {
                if (settings.TryGetValue<ObservableCollection<Class>>("classCollection", out classes))
                {
                    foreach (Class temp in classes)
                    {
                        if (temp.Name == name)
                            currentClass = temp;
                    }
                    ObservableCollection<Category> categories = new ObservableCollection<Category>();
                    categories = currentClass.Categories;
                    if (NavigationContext.QueryString.TryGetValue("category", out category))
                    {
                        foreach (Category temp in categories)
                        {
                            if (temp.Name == category)
                            {
                                currentCategory = temp;
                                break;
                            }
                        }
                        if (NavigationContext.QueryString.TryGetValue("assignment", out assignment))
                        {
                            grades = currentCategory.Assignments;
                            foreach (Assignments temp in grades)
                            {
                                if (temp.Name == assignment)
                                {
                                    currentAssignment = temp;

                                    break;
                                }
                            }
                        }


                    }
                }
            }
            PageTitle.Text = currentAssignment.Name;
            nameBox.Text = currentAssignment.Name;
            dateBox.Value = currentAssignment.Date;
            earnedBox.Text = currentAssignment.EarnedPoints.ToString();
            maxBox.Text = currentAssignment.MaxPoints.ToString();
            weightBox.Text = currentAssignment.Weight.ToString();



            App ap = (App)Application.Current;
            if (ap.isTrial)
            {
                adControl1.Visibility = Visibility.Visible;
            }
            else
            {
                adControl1.Visibility = Visibility.Collapsed;
            }

        }
        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            string navigationFrom;
            NavigationContext.QueryString.TryGetValue("from", out navigationFrom);
            if (navigationFrom == "true")
            {
                NavigationService.RemoveBackEntry();
                NavigationService.RemoveBackEntry();
            }
            var settings = IsolatedStorageSettings.ApplicationSettings;
            
            string name = "";
            if (NavigationContext.QueryString.TryGetValue("name", out name))
            {
                PageTitle.Text = name;
            }
           
            if(settings.TryGetValue<ObservableCollection<Class>>("classCollection",out classes))
            {
                foreach(Class temp in classes)
                {
                    if(temp.Name==name)
                    {
                        currentClass=temp;
                    }
                }
          
                teacherBlock.Text = "Taught By: "+currentClass.Teacher;
               
                ObservableCollection<Category> categories = currentClass.Categories;
                pageBox.ItemsSource = categories;
                
                int totalWeight=0;
                double average=0;
                foreach (Category temp in categories)
                {
                    if (temp.Assignments.Count > 0)
                    {
                        totalWeight += temp.Weight;
                        average += temp.Average * (temp.Weight / 100.0);
                    }
                }
                if (totalWeight > 0)
                {
                    average *= (100.0 / totalWeight);
                    averageBlock.Text = "You have a " + (int)Math.Round(average) + "% in this class";
                }
                else
                {
                    averageBlock.Text = "There is no average";
                }



            }
            App ap = (App)Application.Current;
            if (ap.isTrial)
            {
                adControl1.Visibility = Visibility.Visible;
            }
            else
            {
                adControl1.Visibility = Visibility.Collapsed;
            }
            
        }
        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            try
            {
                string name;
                string category;
                var settings = IsolatedStorageSettings.ApplicationSettings;
                if (NavigationContext.QueryString.TryGetValue("name", out name))
                {
                    ObservableCollection<Class> classes;
                    if (settings.TryGetValue<ObservableCollection<Class>>("classCollection", out classes))
                    {
                        foreach (Class temp in classes)
                        {
                            if (temp.Name == name)
                            {
                                currentClass = temp;
                            }
                        }
                        ObservableCollection<Category> categories = new ObservableCollection<Category>();
                        categories = currentClass.Categories;
                        if (NavigationContext.QueryString.TryGetValue("category", out category))
                            foreach (Category temp in categories)
                            {
                                if (temp.Name == category)
                                {
                                    currentCategory = temp;
                                    break;
                                }
                            }
                    }
                }
                grades = currentCategory.Assignments;
                string assgnname = nameBox.Text;
                string earned = earnedBox.Text;
                string max = maxBox.Text;
                string weight = weightBox.Text;
                if (assgnname == "" || earned == "" || earned == "" || max == "" | weight == "")
                {
                    throw new System.ArgumentNullException();
                }
                foreach (Assignments temp in grades)
                {
                    if (temp.Name == assgnname)
                    {
                        throw new System.ArgumentException();
                    }
                }

                Assignments newAssignment = new Assignments()
                {
                    Name = nameBox.Text,
                    Date = (DateTime)dateBox.Value,
                    EarnedPoints = int.Parse(earnedBox.Text),
                    MaxPoints = int.Parse(maxBox.Text),
                    Weight = int.Parse(weightBox.Text)
                };
                newAssignment.DateString = newAssignment.Date.ToShortDateString();
                currentCategory.TotalEarned += newAssignment.EarnedPoints*newAssignment.Weight;
                currentCategory.TotalMax += newAssignment.MaxPoints*newAssignment.Weight;
                
                grades.Add(newAssignment);
                currentCategory.Assignments = grades;
                NavigationService.GoBack();
            }
            catch (System.ArgumentNullException argnex)
            {
                MessageBox.Show("Every box must have a value");
            }
            catch (System.ArgumentException argex)
            {
                MessageBox.Show("There is already an assignment called " + nameBox.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Your data is improperly entered. Points and weights can only be whole numbers");
            }

        }
        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            try
            {
                var settings = IsolatedStorageSettings.ApplicationSettings;

                //var keys = settings.Keys;
           
                if (settings.TryGetValue<ObservableCollection<Class>>("classCollection", out classes))
                {


                    foreach (Class temp1 in classes)
                    {
                        if (temp1.Name == nameBox.Text)
                        {
                            throw new System.ArgumentException();
                        }
                    }
                    if (categories.Count == 0)
                    {
                        throw new System.ArgumentNullException();
                    }
                    int totalWeight = 0;
                    foreach (Category temp in categories)
                    {
                        totalWeight += temp.Weight;
                    }
                    if (totalWeight > 100 || totalWeight < 100)
                    {
                        throw new System.ArgumentOutOfRangeException();
                    }





                    string name = nameBox.Text;
                    string teacher = teacherBox.Text;
                    if (name == "" || teacher == "")
                    {
                        throw new System.Exception();
                    }


                    Class class1 = new Class()
                    {
                        Name = nameBox.Text,
                        Teacher = teacherBox.Text,
                       
                        Categories = categories
                    };
                    classes.Add(class1);
                  

                }

                NavigationService.GoBack();

            }
            catch (ArgumentNullException argnex)
            {
                MessageBox.Show("You must have at least one category");
            }
            catch (ArgumentOutOfRangeException argoorex)
            {
                MessageBox.Show("Your weights MUST add up to 100.");
            }
            catch (ArgumentException argex)
            {
                MessageBox.Show("The name " + nameBox.Text + " cannot be used");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Check your data. Make sure name and teacher have values");
            }
           

            

            


        }