/*
         * Credit Report State Machine.
         *
         * Read the html credit report one line at time. Based on <div class= and
         * other html landmarks, navigate through the credit report and store its
         * contents in the creditSummary and UserDebtDetail classes.
         *
         */
        public void parse()
        {
            while ((line = cr.ReadLine()) != null)
            {
                line = line.Trim().ToLower();
                lineNum++;
                switch (state)
                {
                    case htmlState.seek:
                        {
                            if (line.StartsWith(tableLabels)) {
                                state = htmlState.readLabel;
                                paraCnt = 0;
                            } else if (line.StartsWith(creditComment)) {
                                state = htmlState.comment;
                            }
                            else if (line.StartsWith(creditScore))
                            {
                                state = htmlState.creditScore;
                            } else if (line.StartsWith(reportDate))
                            {
                                state = htmlState.reportDate;
                            }

                            break;
                        }
                    case htmlState.readLabel:
                        {
                            if (line.StartsWith(creditorName))
                            {
                                tmpCreditorName = stripLine();
                            }
                            else if (line.StartsWith(creditorPhone))
                            {
                                newdebtDetails = new UserDebtDetail();
                                newdebtDetails.debtCompanyName = tmpCreditorName;
                                newdebtDetails.debtCompanyPhone = stripLine();
            //                                Console.WriteLine("Creditor Phone: " + line());
                            }
                            else if (line.StartsWith(creditorAddr))
                            {
                                parseLenderAddress();
                          //      newdebtDetails.debtCompanyAddress = stripLine();
            //                                Console.WriteLine("Creditor Address: " + line);
                            }
                            else if (line.StartsWith(para))
                            {
                                paraCnt++;
                                if (paraCnt == 1)
                                {
                                    if (stripLine() == "account #")
                                    {
                                        tableType = creditTables.acctHistory;

                                    }
                                    else if (strippedLine == "total accounts")
                                    {
                                        tableType = creditTables.acctSummary;

                                    } else {
                                        state = htmlState.seek;
                                    }

                                }
                             //   Console.WriteLine(lineNum.ToString()+" Read label: "+paraCnt.ToString()+" "+stripLine());
                            }
                            else if (line.StartsWith(tableValues))
                            {
                                state = htmlState.readValue;
                            }
                            break;
                        }
                    case htmlState.readValue:
                        {
                            if (isPtag() == true)
                            {
                                if (tableType == creditTables.acctHistory) parseAcctHistory();
                                else if (tableType == creditTables.acctSummary) parseAcctSummary();
                            }
                            break;
                        }
                    case htmlState.comment:
                        {
                            /*
                             * Currently there is no place defined for this information.
                             * I am leaving it here because it might be used in the future
                             */
                            if (line.StartsWith(h2) || line.StartsWith(li))
                            {
            //                                Console.WriteLine(lineNum.ToString() + " Comment: " + stripLine());
                            }
                            else if (line.StartsWith(endDiv)) state = htmlState.seek;
                            break;
                        }
                    case htmlState.creditScore:
                        {
                            if (line.StartsWith("<h1"))
                            {
                                creditSum.creditScore = stripLine();
                                state = htmlState.seek;
                            }
                            break;
                        }
                    case htmlState.reportDate:
                        {
                            if (line.StartsWith(para))
                            {
                                // This is really Ugly. Brute strength is the name of the game;
                                stripLine();
                                string reportD = "";
                                for (int i = 0; i < strippedLine.Length; i++)
                                {
                                    if ((strippedLine[i] >= '0' &&
                                         strippedLine[i] <= '9') ||
                                         strippedLine[i] == '/')
                                    {
                                        reportD += strippedLine[i];
                                    }
                                }
                                creditSum.reportDate = Convert.ToDateTime(reportD).Date;
                                state = htmlState.seek;
                            }
                        }
                        break;
                     /*
                      * The following two cases are designed to caputure the 2 year payment history information.
                      * This feature will be provided in a future version of this program.
                      */
                    case htmlState.paymentHistory1:
                        {
                            if (line.StartsWith(paymentHistory))
                                state = htmlState.paymentHistory2;
                        }
                        break;
                    case htmlState.paymentHistory2:
                        {
                            if (line.StartsWith(paymentHistory))
                                state = htmlState.paymentHistory2;
                        }
                        break;
                    default:
                        {
                        }
                        break;
                }
            }
        }
        /*
         * The init method takes the full path of a html file and opens it.
         * The return value is true if the file can be opened sucessfully.
         * The return value is false if the file can not be opened.
         */
        public bool init(string file)
        {
            try
            {
                cr = new StreamReader(file);

                if (cr == null)
                {
                  //  Console.WriteLine("Error Opening file "+file);
                    return false;
                }
                /*
                 * We are now ready to parse a html credit report
                 */
                state = htmlState.seek;
                tableType = creditTables.none;
                paraCnt = 0;
                lineNum = 0;
                creditSum = new UserCreditSummary();
                debtDetails = new List<UserDebtDetail>();
                tagContinuation = false;
                return true;
            }
            catch (Exception e)
            {
                // Console.WriteLine(e.Message);
                return false;
            }
        }