private string BuildAdvancedSQL()
        {
            string toReturn = "SELECT c.id AS CLAIMID, cmp.id AS COMPANYID, cmp.name AS NAME FROM claims c " +
                              "LEFT JOIN Companies cmp ON c.company_id = cmp.id " +
                              "LEFT JOIN users u ON c.owner_id = u.id ";


            // WHERE
            if (cmbCompanyDropdown.Text != "")
            {
                string companyName = string.Empty;

                if (cmbCompanyDropdown.Text.LastIndexOf(" (") - 1 > 0)
                {
                    companyName = cmbCompanyDropdown.Text.Remove(cmbCompanyDropdown.Text.LastIndexOf("(") - 1);
                }

                if (cmbCompanyDropdown.FindStringExact(cmbCompanyDropdown.Text) >= 0)
                {
                    toReturn += BuildWhereSingle("cmp.name", companyName, DataTypes.Text, DataObject.SearchTypes.Exact);
                }
                else
                {
                    toReturn += BuildWhereSingle("cmp.name", cmbCompanyDropdown.Text, DataTypes.Text);
                }
            }

            toReturn += BuildWherePatientName(txtPatientName.Text, DataTypes.Text);
            toReturn += BuildWhereSingle("c.patient_dob", ctlPatientDOB.CurrentDateText, DataTypes.Date);
            if (CommonFunctions.IsNumeric(txtClaimAmount.Text))
            {
                // Have to multiply whatever they put in by 100, blech
                toReturn += BuildWhereSingle("c.amount_of_claim", (System.Convert.ToDecimal(txtClaimAmount.Text) * 100).ToString("#"), DataTypes.Numeric);
            }
            else
            {
                txtClaimAmount.Text = "";
            }
            toReturn += BuildWhereSingle("c.subscriber_group_number", txtGroupNum.Text, DataTypes.Text);
            toReturn += BuildWhereSingle("c.subscriber_group_name", txtGroupPlan.Text, DataTypes.Text);

            if (chkSentDateEnabled.Checked)
            {
                toReturn += BuildWhereSingle("c.sent_date", ctlSentDate.CurrentDateText, DataTypes.Date, ConvertToSearchType(cmbDateFilterType.SelectedIndex));
            }
            if (chkResentDateEnabled.Checked)
            {
                toReturn += BuildWhereSingle("c.resent_date", ctlResentDate.CurrentDateText, DataTypes.Date, ConvertToSearchType(cmbDateFilterType.SelectedIndex));
            }
            if (chkHoldDateEnabled.Checked)
            {
                toReturn += BuildWhereSingle("c.on_hold_date", ctlOnHoldDate.CurrentDateText, DataTypes.Date, ConvertToSearchType(cmbDateFilterType.SelectedIndex));
            }
            if (chkTracerDateEnabled.Checked)
            {
                toReturn += BuildWhereSingle("c.tracer_date", ctlTracerDate.CurrentDateText, DataTypes.Date, ConvertToSearchType(cmbDateFilterType.SelectedIndex));
            }
            if (chkLastUpdateEnabled.Checked)
            {
                toReturn += BuildWhereSingle("c.status_last_date", ctlLastUpdateDate.CurrentDateText, DataTypes.Date, ConvertToSearchType(cmbDateFilterType.SelectedIndex));
            }

            toReturn += BuildWhereOpen();
            toReturn += BuildWhereHideRecent();
            toReturn += BuildWhereTypes();
            toReturn += BuildWhereClinic();
            toReturn += BuildWhereOwner();

            // Replace the first instance of AND with WHERE
            Regex r = new Regex(" AND ", RegexOptions.IgnoreCase);

            toReturn = r.Replace(toReturn, " WHERE ", 1);

            // ORDER BY
            toReturn += " ORDER BY cmp.name, c.subscriber_group_name, c.patient_last_name, c.patient_first_name";

            return(toReturn);
        }
        private void tvwCall_AfterSelect(object sender, TreeViewEventArgs e)
        {
            CallTreeQuestionNode qNode = tvwCall.SelectedNode as CallTreeQuestionNode;

            currentQuestionNode = null;

            if (qNode != null)
            {
                if (!pnlDataEntry.Visible)
                {
                    pnlDataEntry.Visible = true;
                }

                if (currentChoicePanel != null)
                {
                    currentChoicePanel.Visible = false;
                }

                lblDataDescription.Text = qNode.Question.popup_question_text;

                switch (qNode.Question.type)
                {
                case question.QuestionTypes.Category:
                    currentChoicePanel   = null;
                    pnlDataEntry.Visible = false;
                    break;

                case question.QuestionTypes.Date:
                    currentChoicePanel  = pnlChoiceDate;
                    pnlDataEntry.Height = 66;
                    if (qNode.Choice != null)
                    {
                        DateTime choiceDate;
                        if (DateTime.TryParse(qNode.Choice.answer, out choiceDate))
                        {
                            ctlDate.CurrentDate = choiceDate;
                        }
                        else if (qNode.Choice.answer == "")
                        {
                            ctlDate.CurrentDate = null;
                        }
                        else
                        {
                            ctlDate.CurrentDate = DateTime.Now;
                        }
                    }
                    else
                    {
                        ctlDate.SetDefaultDate(DateTime.Now);
                    }
                    break;

                case question.QuestionTypes.LargeText:
                    currentChoicePanel  = pnlChoiceLargeText;
                    pnlDataEntry.Height = 125;
                    if (qNode.Choice != null)
                    {
                        txtLarge.Text = qNode.Choice.answer;
                    }
                    else
                    {
                        txtLarge.Text = "";
                    }
                    break;

                case question.QuestionTypes.MultipleChoice:
                    currentChoicePanel  = pnlChoiceMultiple;
                    pnlDataEntry.Height = 100;
                    cmbMultipleChoice.Items.Clear();

                    // Use ComboBox
                    foreach (multiple_choice_answer mca in qNode.Question.MultipleChoiceAnswers)
                    {
                        cmbMultipleChoice.Items.Add(mca);
                    }

                    if (qNode.Choice != null)
                    {
                        for (int i = 0; i < cmbMultipleChoice.Items.Count; i++)
                        {
                            if (cmbMultipleChoice.Items[i].ToString() == qNode.Choice.answer)
                            {
                                cmbMultipleChoice.SelectedIndex = i;
                                break;
                            }
                        }
                    }
                    else
                    {
                        cmbMultipleChoice.SelectedIndex = -1;
                    }



                    break;

                case question.QuestionTypes.NormalText:
                    currentChoicePanel  = pnlChoiceNormalText;
                    pnlDataEntry.Height = 66;
                    if (qNode.Choice != null)
                    {
                        txtNormal.Text = qNode.Choice.answer;
                    }
                    else
                    {
                        txtNormal.Text = "";
                    }
                    break;

                case question.QuestionTypes.Numeric:
                    currentChoicePanel  = pnlChoiceNumeric;
                    pnlDataEntry.Height = 66;
                    if (qNode.Choice != null)
                    {
                        if (CommonFunctions.IsNumeric(qNode.Choice.answer))
                        {
                            numNumber.Value = Convert.ToDecimal(qNode.Choice.answer);
                        }
                        else
                        {
                            numNumber.Value = 0;
                        }
                    }
                    else
                    {
                        numNumber.Value = 0;
                    }
                    break;

                case question.QuestionTypes.SmallText:
                    currentChoicePanel  = pnlChoiceSmallText;
                    pnlDataEntry.Height = 100;
                    if (qNode.Choice != null)
                    {
                        txtSmall.Text = qNode.Choice.answer;
                    }
                    else
                    {
                        txtSmall.Text = "";
                    }
                    break;

                case question.QuestionTypes.YesNo:
                    currentChoicePanel  = pnlChoiceYesNo;
                    pnlDataEntry.Height = 84;
                    if (qNode.Choice != null)
                    {
                        bool YesNo = CommonFunctions.FromYesNo(qNode.Choice.answer);
                        if (YesNo)
                        {
                            radYes.Checked = true;
                        }
                        else
                        {
                            radNo.Checked = true;
                        }
                    }
                    else
                    {
                        radYes.Checked = false;
                        radNo.Checked  = false;
                    }
                    break;

                default:
                    if (pnlDataEntry.Visible)
                    {
                        pnlDataEntry.Visible = false;
                    }
                    currentChoicePanel = null;
                    break;
                }

                if (qNode.Choice == null)
                {
                    cmdClearAnswer.Visible = false;
                }
                else
                {
                    cmdClearAnswer.Visible = false;
                }

                if (currentChoicePanel != null)
                {
                    currentChoicePanel.Visible = true;
                    currentQuestionNode        = qNode;
                }
            }
            else
            {
                pnlDataEntry.Visible = false;
                currentQuestionNode  = null;
            }
            tvwCall.SelectedNode.EnsureVisible();
        }