/// <summary>
        /// Returns all child questions for a given question. Given answer is the answer to search for. If requires match is true,
        /// returns all child questions that match. If it's false, returns all non-matches.
        /// </summary>
        /// <param name="parentQuestion"></param>
        /// <param name="givenAnswer"></param>
        /// <param name="requiresMatch"></param>
        /// <returns></returns>
        private List <Control> GetChildQuestionsRecursive(question parentQuestion, string givenAnswer, bool requiresMatch)
        {
            List <Control> toReturn = new List <Control>();

            foreach (Control anItem in formControls[currentParentCategory.id])
            {
                if (anItem is LabelWithCategory)
                {
                    LabelWithCategory thisItem = (LabelWithCategory)anItem;

                    if ((thisItem.Category.parent == parentQuestion.id) && ((thisItem.Category.required_answer == givenAnswer) == requiresMatch))
                    {
                        toReturn.Add(thisItem);

                        if (ShowChildQuestions(thisItem.Category))
                        {
                            toReturn.AddRange(GetChildQuestionsRecursive(thisItem.Category, ""));
                        }
                    }
                }
                else if (anItem is CallQuestionAnswerDisplay)
                {
                    CallQuestionAnswerDisplay thisItem = (CallQuestionAnswerDisplay)anItem;

                    if ((thisItem.Question.parent == parentQuestion.id) && ((thisItem.Question.required_answer == givenAnswer) == requiresMatch))
                    {
                        toReturn.Add(thisItem);

                        if (ShowChildQuestions(thisItem.Question))
                        {
                            toReturn.AddRange(GetChildQuestionsRecursive(thisItem.Question, ""));
                        }
                    }
                }
                else
                {
                    throw new Exception("Unitialized type in formControls array. (Programmer error.)");
                }
            }

            return(toReturn);
        }
        void cqad_QuestionCleared(object sender, QuestionAnsweredEventArgs e)
        {
            _questionsAnsweredCount--;
            if (QuestionCleared != null)
            {
                QuestionCleared(this, new QuestionAnsweredEventArgs());
            }

            if (_choices.Contains(e.Choice))
            {
                _choices.Remove(e.Choice);
            }

            CallQuestionAnswerDisplay sentBy = (CallQuestionAnswerDisplay)sender;

            if (sentBy.Question.is_fork)
            {
                HideQuestions(sentBy, "");
            }
        }
        /// <summary>
        /// Hide all questions that do not have the given answer
        /// </summary>
        /// <param name="sentBy"></param>
        /// <param name="answer"></param>
        private void HideQuestions(CallQuestionAnswerDisplay sentBy, string answer)
        {
            // Need to show all subquestions here

            List <Control> toHide = GetChildQuestionsRecursive(sentBy.Question, answer, false);

            int totalSubtractHeight = 0;

            toHide.Sort(delegate(Control c1, Control c2) { return(c1.TabIndex.CompareTo(c2.TabIndex)); });
            foreach (Control aControl in toHide)
            {
                int top = 0;
                totalSubtractHeight += aControl.Height;
                if (aControl is LabelWithCategory)
                {
                    ((LabelWithCategory)aControl).IsHidden = true;
                    aControl.Height = ((LabelWithCategory)aControl).DefaultHeight;
                    top             = GetTop(aControl.TabIndex) + CATEGORYSPACER;
                }
                else if (aControl is CallQuestionAnswerDisplay)
                {
                    ((CallQuestionAnswerDisplay)aControl).IsHidden = true;
                    aControl.Height = ((CallQuestionAnswerDisplay)aControl).DefaultHeight;
                    top             = GetTop(aControl.TabIndex) + SPACER;
                }


                aControl.Location = new Point(aControl.Location.X, top);
            }

            if (toHide.Count > 0)
            {
                for (int i = toHide[toHide.Count - 1].TabIndex + 1; i < formControls[currentParentCategory.id].Count; i++)
                {
                    formControls[currentParentCategory.id][i].Top -= totalSubtractHeight;
                }
            }
        }
        void cqad_QuestionAnswered(object sender, QuestionAnsweredEventArgs e)
        {
            _questionsAnsweredCount++;
            CallQuestionAnswerDisplay sentBy = (CallQuestionAnswerDisplay)sender;

            if (sentBy.Question.is_fork)
            {
                pnlControls.SuspendLayout();
                pnlControls.AutoScroll = false;
                HideQuestions(sentBy, e.Choice.answer);

                // Need to show all subquestions here
                List <Control> toShow = GetChildQuestionsRecursive(sentBy.Question, e.Choice.answer);


                int totalAddedHeight = 0;
                // Controls aren't showing properly because controls in toShow aren't sorted by tabindex
                // Is likely also the problem with control hiding
                toShow.Sort(delegate(Control c1, Control c2) { return(c1.TabIndex.CompareTo(c2.TabIndex)); });

                int lastBottom = 0;
                if (toShow.Count > 0)
                {
                    lastBottom = GetTop(toShow[0].TabIndex);
                }
                foreach (Control aControl in toShow)
                {
                    int top = 0;
                    if (aControl is LabelWithCategory)
                    {
                        ((LabelWithCategory)aControl).IsHidden = false;
                        aControl.Height = ((LabelWithCategory)aControl).DefaultHeight;
                        top             = lastBottom + CATEGORYSPACER;
                    }
                    else if (aControl is CallQuestionAnswerDisplay)
                    {
                        ((CallQuestionAnswerDisplay)aControl).IsHidden = false;
                        aControl.Height = ((CallQuestionAnswerDisplay)aControl).DefaultHeight;
                        top             = lastBottom + SPACER;
                    }



                    totalAddedHeight += aControl.Height;
                    aControl.Location = new Point(aControl.Location.X, top);
                    lastBottom        = aControl.Bottom;
                }

                if (toShow.Count > 0)
                {
                    for (int i = toShow[toShow.Count - 1].TabIndex + 1; i < formControls[currentParentCategory.id].Count; i++)
                    {
                        formControls[currentParentCategory.id][i].Top += totalAddedHeight;
                    }
                }

                pnlControls.ResumeLayout();
                pnlControls.AutoScroll = true;
            }

            if (!_choices.Contains(e.Choice))
            {
                _choices.Add(e.Choice);
            }

            if (QuestionAnswered != null)
            {
                QuestionAnswered(sender, e);
            }
        }
        private void AddQuestion(question q, bool isRoot, bool hideQuestion)
        {
            int lastBottom;

            if (isRoot)
            {
                questionCount++;

                // Slight hack here because sometimes the root category does not have
                // a question in it, and the root level is deeper. Will have problems
                // with deeply nested questions, should be addressed at some point
                if (q.parent == currentParentCategory.id)
                {
                    displayDepth = 0;
                }
                else
                {
                    displayDepth = 1;
                }

                if (!categories.ContainsKey(q.parent))
                {
                    categories.Add(q.parent, displayDepth);
                }
            }
            else if (!categories.ContainsKey(q.parent))
            {
                questionCount++;

                if (categories.ContainsKey(q.ParentQuestion.parent))
                {
                    displayDepth = categories[q.ParentQuestion.parent] + 1;
                }
                else
                {
                    displayDepth++;
                }
                categories.Add(q.parent, displayDepth);
            }
            else
            {
                questionCount++;
                displayDepth = categories[q.parent];
            }

            if (q.is_fork)
            {
                if (!categories.ContainsKey(q.id))
                {
                    categories.Add(q.id, displayDepth);
                }
            }

            if (lastAdded == null)
            {
                lastBottom = 0;
            }
            else
            {
                lastBottom = lastAdded.Bottom;
            }

            if (q.type == question.QuestionTypes.Category)
            {
                AddCategoryLabel(q, hideQuestion);
            }
            else
            {
                CallQuestionAnswerDisplay cqad = new CallQuestionAnswerDisplay(q, null, CurrentCall);

                cqad.Location = new Point(DEFAULTX + (DEFAULTINDENT * displayDepth), lastBottom + SPACER);

                if (q.is_fork)
                {
                    cqad.Top += CATEGORYSPACER;
                }
                cqad.TabIndex          = controlCount;
                cqad.Name              = "cqad" + controlCount;
                cqad.Size              = new Size(Width - DEFAULTX - 15 - (DEFAULTINDENT * displayDepth), cqad.DefaultHeight);
                cqad.Anchor            = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
                cqad.BorderStyle       = BorderStyle.FixedSingle;
                cqad.QuestionAnswered += new EventHandler <QuestionAnsweredEventArgs>(cqad_QuestionAnswered);
                cqad.QuestionCleared  += new EventHandler <QuestionAnsweredEventArgs>(cqad_QuestionCleared);
                cqad.IsHidden          = hideQuestion;
                cqad.Size              = new Size(Width - DEFAULTX - 15 - (DEFAULTINDENT * displayDepth), cqad.DefaultHeight);
                cqad.DisplayDepth      = displayDepth;

                controlCount++;

                lastAdded = cqad;
                pnlControls.Controls.Add(cqad);
                formControls[currentParentCategory.id].Add(cqad);
            }
        }