public CreatePoll()
        {
            InitializeComponent();

            try
            {
                Names = new ObservableCollection<NameIdModel>();
                progressIndicator = SystemTray.ProgressIndicator;
                progressIndicator = new ProgressIndicator();

                SystemTray.SetProgressIndicator(this, progressIndicator);
                progressIndicator.IsIndeterminate = true;
                progressIndicator.Text = "Pulling Groups...";
                Voting = new VotingClass();
                Questions = new TrulyObservableCollection<VotingQuestionClass>();

                QuestionList.ItemsSource = Questions;
                RecipientsList.ItemsSource = Names;

            }
            catch (Exception exception)
            {
                ErrorHandler.Save(exception, MobileTypeEnum.WP8);
            }
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            try
            {
                Voting = new VotingClass();
                //(App.Current as App).SecondPageObject = null;
                progressIndicator = SystemTray.ProgressIndicator;
                progressIndicator = new ProgressIndicator();
                SystemTray.SetProgressIndicator(this, progressIndicator);
                progressIndicator.IsIndeterminate = true;
                progressIndicator.Text = "Pulling Poll...";
                if (SettingsMobile.Instance.User == null)
                {
                    SqlFactory fact = new SqlFactory();
                    SettingsMobile.Instance.User = fact.GetProfile();
                }
                Voting.VotingId = Convert.ToInt64(this.NavigationContext.QueryString["pid"]);
                PullTopic();


            }
            catch (Exception exception)
            {
                ErrorHandler.Save(exception, MobileTypeEnum.WP8);
            }

        }
 public static VotingClass DeletePoll(VotingClass voting)
 {
     var response = Network.SendPackage(Network.ConvertObjectToStream(voting), MobileConfig.LEAGUE_POLLS_DELETE_URL);
     var stream = response.GetResponseStream();
     StreamReader read = new StreamReader(stream);
     string json = read.ReadToEnd();
     return Json.DeserializeObject<VotingClass>(json);
 }
        public EditPoll()
        {
            InitializeComponent();

            try
            {
                Names = new ObservableCollection<NameIdModel>();
                progressIndicator = SystemTray.ProgressIndicator;
                progressIndicator = new ProgressIndicator();

                Voting = new VotingClass();
                Questions = new TrulyObservableCollection<VotingQuestionClass>();

                QuestionList.ItemsSource = Questions;
                RecipientsList.ItemsSource = Names;

            }
            catch (Exception exception)
            {
                ErrorHandler.Save(exception, MobileTypeEnum.WP8);
            }
        }
        void PullTopic()
        {
            Dispatcher.BeginInvoke(delegate
            {
                progressIndicator.IsVisible = true;
                progressIndicator.Text = "Pulling Poll...";
            });

            Task.Run(new Action(() =>
            {
                try
                {
                    Voting = PollsMobile.GetPoll(SettingsMobile.Instance.User.MemberId, SettingsMobile.Instance.User.LoginId, Voting.VotingId);




                    Dispatcher.BeginInvoke(delegate
                    {
                        Pivot.Title = Voting.Title;
                        description.Text = Voting.Description;
                        IsAnonymous.Text = Voting.IsPollAnonymous.ToString();
                        RecipientsList.ItemsSource = Voting.Voters;
                        for (int i = 0; i < Voting.Questions.Count; i++)
                        {
                            PivotItem p = new PivotItem();
                            p.Header = "question " + (i + 1);
                            ScrollViewer sv = new ScrollViewer();
                            StackPanel sp = new StackPanel();
                            TextBlock question = new TextBlock();
                            question.Text = Voting.Questions[i].Question;
                            question.FontSize = 30;
                            question.Margin = new Thickness(0, 0, 0, 15);
                            question.TextWrapping = TextWrapping.Wrap;
                            if (Voting.Questions[i].QuestionType == QuestionTypeEnum.Multiple)
                            {
                                question.Text += " - MC";
                            }
                            sp.Children.Add(question);


                            foreach (var answer in Voting.Questions[i].Answers)
                            {
                                TextBlock tbAnswer = new TextBlock();
                                tbAnswer.Text = answer.Answer;
                                tbAnswer.FontSize = 25;
                                tbAnswer.TextWrapping = TextWrapping.Wrap;
                                sp.Children.Add(tbAnswer);

                                StackPanel answerStack = new StackPanel();
                                answerStack.Margin = new Thickness(15, 0, 0, 10);
                                TextBlock tbVotesCount = new TextBlock();
                                tbVotesCount.Text = Voting.Questions[i].Votes.Where(x => x.AnswerIds.Contains(answer.AnswerId)).Count() + " vote(s) " + Voting.Questions[i].GetPercentage(answer.AnswerId) + "%";
                                answerStack.Children.Add(tbVotesCount);

                                switch (Voting.IsPollAnonymous)
                                {
                                    case true:
                                        foreach (var vote in Voting.Questions[i].Votes.Where(x => x.AnswerIds.Contains(answer.AnswerId)).OrderBy(x => x.DerbyName))
                                        {
                                            if (!String.IsNullOrEmpty(vote.OtherText))
                                            {
                                                TextBlock comment = new TextBlock();
                                                comment.Text =" - "+ vote.OtherText;
                                                comment.FontSize = 20;
                                                comment.Margin = new Thickness(10, 0, 0, 10);
                                                comment.TextWrapping = TextWrapping.Wrap;
                                                answerStack.Children.Add(comment);
                                            }
                                        }
                                        break;
                                    case false:
                                        foreach (var vote in Voting.Questions[i].Votes.Where(x => x.AnswerIds.Contains(answer.AnswerId)).OrderBy(x => x.DerbyName))
                                        {
                                            TextBlock name = new TextBlock();
                                            name.Text = vote.DerbyName;
                                            answerStack.Children.Add(name);
                                            if (!String.IsNullOrEmpty(vote.OtherText))
                                            {
                                                TextBlock comment = new TextBlock();
                                                comment.Text = " - " + vote.OtherText;
                                                comment.FontSize = 20;
                                                comment.Margin = new Thickness(10, 0, 0, 10);
                                                comment.TextWrapping = TextWrapping.Wrap;
                                                answerStack.Children.Add(comment);
                                            }
                                        }
                                        break;
                                }
                                sp.Children.Add(answerStack);
                                ProgressBar bar = new ProgressBar();
                                bar.Margin = new Thickness(0, 0, 0, 15);
                                bar.Maximum = Voting.Questions[i].Votes.Count;
                                bar.Minimum = 0;
                                bar.Value = Voting.Questions[i].GetPercentage(answer.AnswerId);
                                sp.Children.Add(bar);

                            }
                            sv.Content = sp;
                            p.Content = sv;

                            Pivot.Items.Insert(i + 1, p);
                        }


                        progressIndicator.IsVisible = false;
                    });
                }
                catch (Exception exception)
                {
                    ErrorHandler.Save(exception, MobileTypeEnum.WP8);
                }
            }));


        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);
            try
            {






                var currentObject = (App.Current as App).SecondPageObject;
                if (currentObject != null)
                {
                    if (currentObject.GetType() == typeof(VotingQuestionClass))
                    {
                        var quest = ((VotingQuestionClass)currentObject);
                        var tempQuest = Questions.Where(x => x.QuestionId == quest.QuestionId).FirstOrDefault();
                        if (tempQuest == null)
                        {
                            Voting.Questions.Add(quest);
                            Questions.Add(quest);
                        }
                        else
                        {
                            if (quest.IsDeleted)
                            {
                                Questions.Remove(tempQuest);
                            }
                            else
                            {
                                int index = Questions.IndexOf(tempQuest);
                                Questions.Remove(tempQuest);
                                Questions.Insert(index, quest);
                                QuestionList.ItemsSource = null;
                                QuestionList.ItemsSource = Questions;
                            }
                        }
                    }
                    else if (currentObject.GetType() == typeof(ObservableCollection<NameIdModel>))
                        Names = ((ObservableCollection<NameIdModel>)currentObject);
                }
                else
                {
                    Voting = new VotingClass();
                    Questions = new TrulyObservableCollection<VotingQuestionClass>();
                    QuestionList.ItemsSource = Questions;
                    RecipientsList.ItemsSource = Names;
                    progressIndicator = SystemTray.ProgressIndicator;
                    progressIndicator = new ProgressIndicator();
                    SystemTray.SetProgressIndicator(this, progressIndicator);
                    progressIndicator.IsIndeterminate = true;
                    progressIndicator.Text = "Pulling Poll...";
                    if (SettingsMobile.Instance.User == null)
                    {
                        SqlFactory fact = new SqlFactory();
                        SettingsMobile.Instance.User = fact.GetProfile();
                    }
                    Voting.VotingId = Convert.ToInt64(this.NavigationContext.QueryString["pid"]);
                    PullTopic();
                }
                (App.Current as App).SecondPageObject = null;

            }
            catch (Exception exception)
            {
                ErrorHandler.Save(exception, MobileTypeEnum.WP8);
            }
        }
        void PullTopic()
        {
            Dispatcher.BeginInvoke(delegate
            {
                progressIndicator.IsVisible = true;
                progressIndicator.Text = "Pulling Poll...";
            });

            Task.Run(new Action(() =>
            {
                try
                {
                    Voting = PollsMobile.GetPoll(SettingsMobile.Instance.User.MemberId, SettingsMobile.Instance.User.LoginId, Voting.VotingId);




                    Dispatcher.BeginInvoke(delegate
                    {
                        Title.Text = Voting.Title;
                        Description.Text = Voting.Description;
                        OpenResults.IsChecked = Voting.IsOpenToLeague;
                        for (int i = 0; i < Voting.Questions.Count; i++)
                            Questions.Add(Voting.Questions[i]);
                        for (int i = 0; i < Voting.Voters.Count; i++)
                        {
                            Names.Add(new NameIdModel() { Id = Voting.Voters[i].MemberId.ToString(), Name = Voting.Voters[i].DerbyName });
                        }

                        progressIndicator.IsVisible = false;
                    });
                }
                catch (Exception exception)
                {
                    ErrorHandler.Save(exception, MobileTypeEnum.WP8);
                }
            }));


        }
        void PullTopic()
        {
            Dispatcher.BeginInvoke(delegate
            {
                progressIndicator.IsVisible = true;
                progressIndicator.Text = "Pulling Poll...";
            });

            Task.Run(new Action(() =>
            {
                try
                {
                    Voting = PollsMobile.GetPoll(SettingsMobile.Instance.User.MemberId, SettingsMobile.Instance.User.LoginId, Voting.VotingId);
                    
                    Dispatcher.BeginInvoke(delegate
                    {
                        Pivot.Title = Voting.Title;
                        description.Text = Voting.Description;
                        for (int i = 0; i < Voting.Questions.Count; i++)
                        {
                            PivotItem p = new PivotItem();
                            p.Header = "Question " + (i + 1);
                            StackPanel sp = new StackPanel();
                            TextBlock question = new TextBlock();
                            question.Text = Voting.Questions[i].Question;
                            question.FontSize = 30;
                            question.TextWrapping = TextWrapping.Wrap;

                            sp.Children.Add(question);

                            if (Voting.Questions[i].QuestionType == QuestionTypeEnum.Multiple)
                            {
                                foreach (var answer in Voting.Questions[i].Answers)
                                {
                                    CheckBox cb = new CheckBox();
                                    cb.Name = Voting.Questions[i].QuestionId + "-" + answer.AnswerId;
                                    cb.Checked += cb_Checked;
                                    cb.Unchecked += cb_Unchecked;
                                    TextBlock tb = new TextBlock();
                                    tb.Text = answer.Answer;
                                    cb.Content = tb;
                                    sp.Children.Add(cb);
                                }
                            }
                            else if (Voting.Questions[i].QuestionType == QuestionTypeEnum.Single)
                            {
                                foreach (var answer in Voting.Questions[i].Answers)
                                {
                                    RadioButton cb = new RadioButton();
                                    cb.Checked += rb_Checked;
                                    cb.GroupName = "g-" + Voting.Questions[i].QuestionId;
                                    cb.Name = Voting.Questions[i].QuestionId + "-" + answer.AnswerId;
                                    TextBlock tb = new TextBlock();
                                    tb.Text = answer.Answer;
                                    cb.Content = tb;
                                    sp.Children.Add(cb);
                                }
                            }
                            TextBlock comment = new TextBlock();
                            comment.Text = "Comment:";
                            TextBox commentTb = new TextBox();
                            commentTb.Name = "c-" + Voting.Questions[i].QuestionId;
                            commentTb.LostFocus += commentTb_LostFocus;
                            sp.Children.Add(comment);
                            sp.Children.Add(commentTb);

                            p.Content = sp;
                            Pivot.Items.Add(p);
                        }


                        progressIndicator.IsVisible = false;
                    });
                }
                catch (Exception exception)
                {
                    ErrorHandler.Save(exception, MobileTypeEnum.WP8);
                }
            }));


        }