public static qHtl_DailySchoolDistrictAbsenteeSummary GetDailySummaryByDate(string curr_date)
        {
            qHtl_DailySchoolDistrictAbsenteeSummary summary = new qHtl_DailySchoolDistrictAbsenteeSummary();

            summary.container.Select(new DbQuery
            {
                Top     = "Top(1)",
                Where   = string.Format("MarkAsDelete = 0 AND DataDate = '" + curr_date + "'"),
                OrderBy = "DataDate DESC"
            });

            if (summary.DailySchoolDistrictAbsenteeSummaryID > 0)
            {
                return(summary);
            }
            else
            {
                return(null);
            }
        }
        public static qHtl_DailySchoolDistrictAbsenteeSummary GetDailySummaryPriorOrAfter(string direction, string curr_date)
        {
            qHtl_DailySchoolDistrictAbsenteeSummary summary = new qHtl_DailySchoolDistrictAbsenteeSummary();

            DateTime eval_date = new DateTime();

            eval_date = Convert.ToDateTime(curr_date);
            string sql      = string.Empty;
            string order_by = string.Empty;

            if (direction == "prior")
            {
                sql      = "MarkAsDelete = 0 AND DataDate < '" + curr_date + "'";
                order_by = "DataDate DESC";
            }
            else if (direction == "after")
            {
                sql      = "MarkAsDelete = 0 AND DataDate > '" + curr_date + "'";
                order_by = "DataDate ASC";
            }

            curr_date = Convert.ToString(eval_date.ToShortDateString());

            summary.container.Select(new DbQuery
            {
                Top     = "Top(1)",
                Where   = sql,
                OrderBy = order_by
            });

            if (summary.DailySchoolDistrictAbsenteeSummaryID > 0)
            {
                return(summary);
            }
            else
            {
                return(null);
            }
        }
        public static qHtl_DailySchoolDistrictAbsenteeSummary GetMostRecentDailySummary()
        {
            qHtl_DailySchoolDistrictAbsenteeSummary summary = new qHtl_DailySchoolDistrictAbsenteeSummary();

            summary.container.Select(new DbQuery
            {
                Top = "Top(1)",
                Where = string.Format("MarkAsDelete = 0"),
                OrderBy = "DataDate DESC"
            });

            if (summary.DailySchoolDistrictAbsenteeSummaryID > 0) return summary;
            else return null;
        }
        public static qHtl_DailySchoolDistrictAbsenteeSummary GetDailySummaryPriorOrAfter(string direction, string curr_date)
        {
            qHtl_DailySchoolDistrictAbsenteeSummary summary = new qHtl_DailySchoolDistrictAbsenteeSummary();

            DateTime eval_date = new DateTime();
            eval_date = Convert.ToDateTime(curr_date);
            string sql = string.Empty;
            string order_by = string.Empty;

            if (direction == "prior")
            {
                sql = "MarkAsDelete = 0 AND DataDate < '" + curr_date + "'";
                order_by = "DataDate DESC";
            }
            else if (direction == "after")
            {
                sql = "MarkAsDelete = 0 AND DataDate > '" + curr_date + "'";
                order_by = "DataDate ASC";
            }

            curr_date = Convert.ToString(eval_date.ToShortDateString());

            summary.container.Select(new DbQuery
            {
                Top = "Top(1)",
                Where = sql,
                OrderBy = order_by
            });

            if (summary.DailySchoolDistrictAbsenteeSummaryID > 0) return summary;
            else return null;
        }
    protected void ProcessAbsenteeData(string[] to_pass)
    {
        int school_district_id = 0;

        if (school_district_id == 0)
            school_district_id = 1;

        qHtl_AbsenteeAnalysisVariable variables = new qHtl_AbsenteeAnalysisVariable(school_district_id);

        string mode = to_pass[0];
        string f_name = to_pass[1];
        lblFileName.Text = fileFullName;
        string fileName = string.Format("{0}{1}", Server.MapPath(Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["Resources_AbsenteeFolder"])), f_name);
        string message = string.Empty;
        string str_solution_start_date = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["Solution_StartDate"]);

        if (File.Exists(fileName))
        {
            List<string[]> data = parseCSV(fileName);

            message = "RESULTS<br>";
            string eval_date = string.Empty;
            int curr_school_id = 0;
            int curr_school_district_id = 0;
            string curr_school_name = string.Empty;
            string curr_school_level = string.Empty;
            int curr_school_total_absent = 0;
            int curr_school_total_unknown = 0;
            int curr_school_total_other = 0;
            int curr_school_total_sick = 0;
            int curr_school_gastrointestinal = 0;
            int curr_school_respiratory = 0;
            int curr_school_rash = 0;
            int curr_school_other_illness = 0;
            int curr_school_unknown_illness = 0;
            int curr_school_total_enrolled = 0;
            decimal curr_school_rate = 0;
            int num_school_saves = 0;

            bool overall_success = false;
            int total_elementary_schools = 0;
            int total_elementary_students_enrolled = 0;
            int total_elementary_students_absent = 0;
            decimal total_elementary_absentee_rate = 0;
            int total_junior_high_schools = 0;
            int total_junior_high_students_enrolled = 0;
            int total_junior_high_students_absent = 0;
            decimal total_junior_high_absentee_rate = 0;
            int total_high_schools = 0;
            int total_high_school_students_enrolled = 0;
            int total_high_school_students_absent = 0;
            decimal total_high_school_absentee_rate = 0;
            int total_students_enrolled = 0;
            decimal total_absentee_rate = 0;

            int summary_total_absent = 0;
            int summary_total_sick = 0;
            int summary_total_gastrointestinal = 0;
            int summary_total_respiratory = 0;
            int summary_total_rash = 0;
            int summary_total_other_illness = 0;
            int summary_total_unknown_illness = 0;
            int summary_total_enrolled = 0;

            List<double> summary_sch_rates = new List<double>();
            List<double> sch_rates = new List<double>();
            List<double> elem_rates = new List<double>();
            List<double> jr_rates = new List<double>();
            List<double> high_rates = new List<double>();
            List<double> illness_rates = new List<double>();
            List<double> gast_rates = new List<double>();
            List<double> resp_rates = new List<double>();
            List<double> rash_rates = new List<double>();
            List<double> othr_rates = new List<double>();
            List<double> unkn_rates = new List<double>();

            List<ClassroomAbsenteeData> a_data = new List<ClassroomAbsenteeData>();

            string curr_descriptor = string.Empty;
            int prior_school_id = 0;

            string absent_date = string.Empty;
            string school_name = string.Empty;
            string school_type = string.Empty;
            string grade_level = string.Empty;
            string grade_number = string.Empty;
            string class_name = string.Empty;
            string teacher_name = string.Empty;
            int days_in_session = 0;
            int total_enrolled = 0;
            int total_absent = 0;
            int total_unknown = 0;
            int total_other = 0;
            int total_sick = 0;
            int gastrointestinal = 0;
            int respiratory = 0;
            int rash = 0;
            int other_illness = 0;
            int unknown_illness = 0;

            bool success = false;
            string reason = string.Empty;

            int i = 0;
            int s = 0;
            foreach (string[] c in data)
            {
                if (i > 0)
                {
                    absent_date = c[0];
                    school_name = c[1];
                    school_name = school_name.Trim();
                    school_type = c[2];
                    school_type = school_type.Trim();
                    grade_level = c[3];
                    grade_level = grade_level.Trim();
                    class_name = c[4];
                    class_name = class_name.Trim();
                    teacher_name = c[5];
                    teacher_name = teacher_name.Trim();
                    days_in_session = Convert.ToInt32(c[6]);
                    total_enrolled = Convert.ToInt32(c[7]);
                    total_absent = Convert.ToInt32(c[8]);
                    total_unknown = Convert.ToInt32(c[9]);
                    total_other = Convert.ToInt32(c[10]);
                    total_sick = Convert.ToInt32(c[11]);
                    gastrointestinal = Convert.ToInt32(c[12]);
                    respiratory = Convert.ToInt32(c[13]);
                    rash = Convert.ToInt32(c[14]);
                    other_illness = Convert.ToInt32(c[15]);
                    unknown_illness = Convert.ToInt32(c[16]);

                    if (school_type.ToLower().Contains("elem"))
                    {
                        int curr_grade_number = 0;
                        if (grade_level.ToLower().Contains("kinder"))
                            curr_grade_number = 0;
                        else if (grade_level.ToLower().Contains("pre"))
                            curr_grade_number = -1;
                        else
                        {
                            try
                            {
                                curr_grade_number = Convert.ToInt32(grade_level);
                            }
                            catch
                            {
                                curr_grade_number = -2;
                            }
                        }
                        string extra_sort_val = string.Empty;
                        if (curr_grade_number == -1 || curr_grade_number == 0)
                            extra_sort_val = "a";

                        // add to list
                        a_data.Add(new ClassroomAbsenteeData()
                        {
                            sort_by = absent_date + " " + school_name + " " + extra_sort_val + "" + curr_grade_number,
                            absent_date = absent_date,
                            school_name = school_name,
                            school_type = school_type,
                            grade_level = grade_level,
                            grade_number = curr_grade_number,
                            class_name = class_name,
                            teacher_name = teacher_name,
                            days_in_session = days_in_session,
                            total_enrolled = total_enrolled,
                            total_absent = total_absent,
                            total_unknown = total_unknown,
                            total_other = total_other,
                            total_sick = total_sick,
                            gastrointestinal = gastrointestinal,
                            respiratory = respiratory,
                            rash = rash,
                            other_illness = other_illness,
                            unknown_illness = unknown_illness
                        });
                    }
                }

                i++;
            }

            // sort array to insure that it is ordered by school
            a_data.Sort((a, b) => a.sort_by.CompareTo(b.sort_by));

            // clear initial list
            data.Clear();

            int class_count = 0;
            List<double> curr_school_rates = new List<double>();
            List<double> curr_school_all_prior_rates = new List<double>();
            List<double> curr_district_all_prior_rates = new List<double>();

            foreach (var a in a_data)
            {

                class_count ++;

                absent_date = a.absent_date;
                school_name = a.school_name;
                school_type = a.school_type;
                grade_level = a.grade_level;
                grade_number = Convert.ToString(a.grade_number);
                class_name = a.class_name;
                teacher_name = a.teacher_name;
                days_in_session = a.days_in_session;
                total_enrolled = a.total_enrolled;
                total_absent = a.total_absent;
                total_unknown = a.total_unknown;
                total_other = a.total_other;
                total_sick = a.total_sick;
                gastrointestinal = a.gastrointestinal;
                respiratory = a.respiratory;
                rash = a.rash;
                other_illness = a.other_illness;
                unknown_illness = a.unknown_illness;

                curr_descriptor = teacher_name + ", Grade: " + grade_level + " at " + school_name;

                DateTime curr_date = new DateTime();
                curr_date = Convert.ToDateTime(absent_date);

                if (mode == "process")
                {
                    if (String.IsNullOrEmpty(eval_date))
                    {
                        eval_date = absent_date;
                        DateTime curr_eval_date = new DateTime();
                        curr_eval_date = Convert.ToDateTime(eval_date);

                        string final_eval_date = curr_eval_date.ToShortDateString();

                        qHtl_DailyClassroomAbsenteeData.DeleteClassroomAbsenteeDataByDate(final_eval_date);
                        qHtl_DailySchoolAbsenteeData.DeleteSchoolAbsenteeDataByDate(final_eval_date);
                        qHtl_DailySchoolDistrictAbsenteeSummary.DeleteSchoolDistrictAbsenteeSummaryByDate(final_eval_date);
                    }

                    // match the current school and get the school and district_id
                    // first make sure no apostrophes
                    string name_check = school_name.Replace("'", "");
                    var curr_school = qOrg_School.GetSchoolBySelector(name_check);

                    if (curr_school.SchoolID > 0)
                    {
                        // evalute if new school and run appropriate elements
                        curr_school_id = curr_school.SchoolID;
                        if (prior_school_id == 0)
                        {
                            prior_school_id = curr_school_id;
                            curr_school_district_id = curr_school.SchoolDistrictID;
                            curr_school_name = curr_school.School;
                            curr_school_level = curr_school.SchoolLevel;
                        }
                        else if (prior_school_id != curr_school_id || class_count == a_data.Count)
                        {
                            // save values
                            qHtl_DailySchoolAbsenteeData s_data = new qHtl_DailySchoolAbsenteeData();
                            s_data.Created = DateTime.Now;
                            s_data.MarkAsDelete = 0;
                            s_data.SchoolDistrictID = curr_school_district_id;
                            s_data.SchoolID = prior_school_id;
                            s_data.DataDate = Convert.ToDateTime(eval_date).Date;
                            s_data.School = curr_school_name;
                            s_data.SchoolLevel = curr_school_level;
                            s_data.DaysInSession = 1;

                            decimal eval_school_total_absent = Convert.ToDecimal(curr_school_total_absent);
                            decimal eval_school_total_enrolled = Convert.ToDecimal(curr_school_total_enrolled);
                            decimal eval_school_rate = 0;

                            if (num_school_saves > 0)
                            {
                                curr_school_total_absent += total_absent;
                                curr_school_total_unknown += total_unknown;
                                curr_school_total_other += total_other;
                                curr_school_total_sick += total_sick;
                                curr_school_gastrointestinal += gastrointestinal;
                                curr_school_respiratory += respiratory;
                                curr_school_rash += rash;
                                curr_school_other_illness += other_illness;
                                curr_school_unknown_illness += unknown_illness;
                                curr_school_total_enrolled += total_enrolled;
                            }
                            s_data.TotalAbsent = curr_school_total_absent;
                            s_data.TotalUnknown = curr_school_total_unknown;
                            s_data.TotalOther = curr_school_total_other;
                            s_data.TotalSick = curr_school_total_sick;
                            s_data.Gastrointestinal = curr_school_gastrointestinal;
                            s_data.Respiratory = curr_school_respiratory;
                            s_data.Rash = curr_school_rash;
                            s_data.OtherIllness = curr_school_other_illness;
                            s_data.UnknownIllness = curr_school_unknown_illness;
                            s_data.TotalEnrolled = curr_school_total_enrolled;

                            eval_school_rate = Decimal.Divide(curr_school_total_absent, s_data.TotalEnrolled) * 100;
                            s_data.Rate = eval_school_rate;

                            // school rate warning level -- need to get all prior day rates for this school
                            // curr_school_all_prior_rates --> load all prior rates into this list
                            double eval_rate = 0;
                            curr_school_all_prior_rates = qHtl_DailySchoolAbsenteeData.GetAllPriorDailyRatesforSpecificSchool(str_solution_start_date, Convert.ToString(DateTime.Now), prior_school_id, Convert.ToDouble(eval_school_rate));
                            if (variables.SDFormulaType == "population")
                                eval_rate = CalculateSTDPopulationFromList(curr_school_all_prior_rates);      // used to be curr_school_rates when was based on individual classrooms
                            else
                                eval_rate = CalculateSTDFromList(curr_school_all_prior_rates);

                            decimal overal_std = Convert.ToDecimal(eval_rate);
                            s_data.RateSTD = overal_std;

                            // school daily status evaluations
                            int school_illness_absences = (curr_school_gastrointestinal + curr_school_respiratory + curr_school_rash + curr_school_other_illness + curr_school_unknown_illness);

                            decimal school_gast_sym = CalculateSymptomRate(curr_school_gastrointestinal, school_illness_absences);
                            s_data.GastrointestinalRate = school_gast_sym;

                            decimal school_resp_sym = CalculateSymptomRate(curr_school_respiratory, school_illness_absences);
                            s_data.RespiratoryRate = school_resp_sym;

                            decimal school_rash_sym = CalculateSymptomRate(curr_school_rash, school_illness_absences);
                            s_data.RashRate = school_rash_sym;

                            decimal school_othr_sym = CalculateSymptomRate(curr_school_other_illness, school_illness_absences);
                            s_data.OtherRate = school_othr_sym;

                            decimal school_unkn_sym = CalculateSymptomRate(curr_school_unknown_illness, school_illness_absences);
                            s_data.UnknownRate = school_unkn_sym;

                            s_data.Insert();
                            num_school_saves++;

                            summary_sch_rates.Add(Convert.ToDouble(eval_school_rate));

                            // CALCULATE PRIOR SCHOOL RATE AVERAGES - 2 week average rate, prior year average, historic average
                            DateTime solution_start_date = new DateTime();
                            solution_start_date = Convert.ToDateTime(variables.SolutionStartDate);
                            DateTime end_date = new DateTime();
                            end_date = Convert.ToDateTime(s_data.DataDate);
                            DateTime two_weeks_prior = new DateTime();
                            two_weeks_prior = end_date.AddDays(-14);

                            decimal two_week_prior_rate = qHtl_DailySchoolAbsenteeData.CalculateSchoolValuesForRange(Convert.ToString(two_weeks_prior), Convert.ToString(end_date), s_data.Rate, s_data.SchoolID, "rate");
                            decimal historic_rate = qHtl_DailySchoolAbsenteeData.CalculateSchoolValuesForRange(Convert.ToString(solution_start_date), Convert.ToString(end_date), s_data.Rate, s_data.SchoolID, "rate");
                            decimal historic_rate_std = qHtl_DailySchoolAbsenteeData.CalculateSchoolValuesForRange(Convert.ToString(solution_start_date), Convert.ToString(end_date), s_data.RateSTD, s_data.SchoolID, "std");

                            s_data.TwoWeekRate = two_week_prior_rate;
                            s_data.HistoricRate = historic_rate;
                            s_data.HistoricRateSTD = historic_rate_std;
                            s_data.Update();

                            // reset values
                            curr_school_id = curr_school.SchoolID;
                            prior_school_id = curr_school_id;
                            curr_school_district_id = curr_school.SchoolDistrictID;
                            curr_school_name = curr_school.School;
                            curr_school_level = curr_school.SchoolLevel;
                            curr_school_total_absent = 0;
                            curr_school_total_unknown = 0;
                            curr_school_total_other = 0;
                            curr_school_total_sick = 0;
                            curr_school_gastrointestinal = 0;
                            curr_school_respiratory = 0;
                            curr_school_rash = 0;
                            curr_school_other_illness = 0;
                            curr_school_unknown_illness = 0;
                            curr_school_total_enrolled = 0;
                            curr_school_rate = 0;
                            s = 0;
                            curr_school_rates.Clear();

                            if (curr_school.SchoolLevel.ToLower().Contains("elem"))
                            {
                                total_elementary_schools++;
                            }
                            else if (curr_school.SchoolLevel.ToLower().Contains("jr") || curr_school.SchoolLevel.ToLower().Contains("middle"))
                            {
                                total_junior_high_schools++;
                            }
                            else if (curr_school.SchoolLevel.ToLower().Contains("high"))
                            {
                                total_high_schools++;
                            }
                        }

                        // run rate calculation
                        decimal eval_total_absent = Convert.ToDecimal(total_absent);
                        decimal eval_total_enrolled = Convert.ToDecimal(total_enrolled);
                        decimal curr_rate = 0;
                        if (eval_total_enrolled > 0)
                            curr_rate = (eval_total_absent / eval_total_enrolled) * 100;

                        // create daily absentee data
                        qHtl_DailyClassroomAbsenteeData c_data = new qHtl_DailyClassroomAbsenteeData();
                        c_data.Created = DateTime.Now;
                        c_data.MarkAsDelete = 0;
                        c_data.SchoolDistrictID = curr_school.SchoolDistrictID;
                        c_data.SchoolID = curr_school.SchoolID;
                        c_data.DataDate = Convert.ToDateTime(eval_date).Date;
                        c_data.School = curr_school.School;
                        c_data.SchoolLevel = curr_school.SchoolLevel;
                        c_data.GradeLevel = grade_level;
                        c_data.ClassRoom = class_name;
                        c_data.GradeNumber = Convert.ToInt32(grade_number);
                        c_data.Instructor = teacher_name;
                        c_data.DaysInSession = days_in_session;
                        c_data.Rate = curr_rate;
                        c_data.TotalAbsent = total_absent;
                        c_data.TotalUnknown = total_unknown;
                        c_data.TotalOther = total_other;
                        c_data.TotalSick = total_sick;
                        c_data.Gastrointestinal = gastrointestinal;
                        c_data.Respiratory = respiratory;
                        c_data.Rash = rash;
                        c_data.OtherIllness = other_illness;
                        c_data.UnknownIllness = unknown_illness;
                        c_data.TotalEnrolled = total_enrolled;
                        c_data.Insert();

                        // add values to overall school values
                        curr_school_total_absent += total_absent;
                        curr_school_total_unknown += total_unknown;
                        curr_school_total_other += total_other;
                        curr_school_total_sick += total_sick;
                        curr_school_gastrointestinal += gastrointestinal;
                        curr_school_respiratory += respiratory;
                        curr_school_rash += rash;
                        curr_school_other_illness += other_illness;
                        curr_school_unknown_illness += unknown_illness;
                        curr_school_total_enrolled += total_enrolled;
                        curr_school_rate += curr_rate;
                        s++;

                        // add values to daily summary values
                        if (c_data.SchoolLevel.ToLower().Contains("elem"))
                        {
                            total_elementary_students_enrolled += total_enrolled;
                            total_elementary_students_absent += total_absent;
                            total_elementary_absentee_rate += curr_rate;
                            elem_rates.Add(Convert.ToDouble(curr_rate));
                        }
                        else if (c_data.SchoolLevel.ToLower().Contains("jr") || c_data.SchoolLevel.ToLower().Contains("middle"))
                        {
                            total_junior_high_students_enrolled += total_enrolled;
                            total_junior_high_students_absent += total_absent;
                            total_junior_high_absentee_rate += curr_rate;
                            jr_rates.Add(Convert.ToDouble(curr_rate));
                        }
                        else if (c_data.SchoolLevel.ToLower().Contains("high"))
                        {
                            total_high_school_students_enrolled += total_enrolled;
                            total_high_school_students_absent += total_absent;
                            total_high_school_absentee_rate += curr_rate;
                            high_rates.Add(Convert.ToDouble(curr_rate));
                        }
                        total_students_enrolled += total_enrolled;
                        total_absentee_rate += curr_rate;

                        // add to overall list
                        sch_rates.Add(Convert.ToDouble(curr_rate));

                        // add to curr school list
                        curr_school_rates.Add(Convert.ToDouble(curr_rate));

                        // add to other lists
                        illness_rates.Add(Convert.ToDouble(gastrointestinal + respiratory + rash + other_illness + unknown_illness));
                        gast_rates.Add(Convert.ToDouble(gastrointestinal));
                        resp_rates.Add(Convert.ToDouble(respiratory));
                        rash_rates.Add(Convert.ToDouble(rash));
                        othr_rates.Add(Convert.ToDouble(other_illness));
                        unkn_rates.Add(Convert.ToDouble(unknown_illness));

                        // add values to summary list
                        summary_total_absent += total_absent;
                        summary_total_sick += total_sick;
                        summary_total_gastrointestinal += gastrointestinal;
                        summary_total_respiratory += respiratory;
                        summary_total_rash += rash;
                        summary_total_other_illness += other_illness;
                        summary_total_unknown_illness += unknown_illness;
                        summary_total_enrolled += total_enrolled;

                        success = true;
                        overall_success = true;
                    }
                }
                else if (mode == "test")
                {
                    success = true;
                }

                i++;

                if (mode == "process" && success == true)
                {
                    message += "Line " + i + " SUCCESS - " + curr_descriptor + " successfully added to absentee data<br>";
                }
                else if (mode == "test" && success == true)
                {
                    message += "Line " + i + " SUCCESS - " + curr_descriptor + " will be processed<br>";
                }
                else if (i > 1)
                {
                    message += "Line " + i + " FAILURE - could not be added for the following reason: " + reason + "<br>";
                }
                else
                {
                    message += "Line " + i + " LINE NOT PROCESSED - header line in the file<br>";
                }
            }

            // write summary data
            if (overall_success == true)
            {
                qHtl_DailySchoolDistrictAbsenteeSummary d_data = new qHtl_DailySchoolDistrictAbsenteeSummary();
                d_data.Created = DateTime.Now;
                d_data.DataDate = Convert.ToDateTime(eval_date).Date;
                d_data.SchoolDistrictID = curr_school_district_id;

                if (total_elementary_schools > 0)
                {
                    d_data.NumElementarySchools = total_elementary_schools;
                    d_data.TotalElementaryStudents = total_elementary_students_enrolled;
                    d_data.TotalElementaryStudentsAbsent = total_elementary_students_absent;
                    decimal eval_num_elementary_students_enrolled = Convert.ToDecimal(total_elementary_students_enrolled);
                    decimal eval_elem_rate = Convert.ToDecimal(total_elementary_students_absent / eval_num_elementary_students_enrolled) * 100;
                    d_data.ElementarySchoolAbsenteeRate = eval_elem_rate;
                }

                if (total_junior_high_schools > 0)
                {
                    d_data.NumJuniorHighs = total_junior_high_schools;
                    d_data.TotalJuniorHighStudents = total_junior_high_students_enrolled;
                    d_data.TotalJuniorHighStudentsAbsent = total_junior_high_students_absent;
                    decimal eval_num_junior_high_schools = Convert.ToDecimal(total_junior_high_schools);
                    decimal eval_jr_rate = Convert.ToDecimal(total_junior_high_absentee_rate / eval_num_junior_high_schools) * 100;
                    d_data.JuniorHighSchoolAbsenteeRate = eval_jr_rate;
                }

                if (total_high_schools > 0)
                {
                    d_data.NumHighSchools = total_high_schools;
                    d_data.TotalHighSchoolStudents = total_junior_high_students_enrolled;
                    d_data.TotalHighSchoolStudentsAbsent = total_high_school_students_absent;
                    decimal eval_num_high_schools = Convert.ToDecimal(total_high_schools);
                    decimal eval_high_rate = Convert.ToDecimal(total_high_school_absentee_rate / eval_num_high_schools) * 100;
                    d_data.HighSchoolAbsenteeRate = eval_high_rate;
                }

                // overall absentee rate
                decimal eval_total_enrolled = Convert.ToDecimal(total_elementary_students_enrolled + total_high_school_students_enrolled + total_high_school_students_enrolled);
                decimal eval_total_absent = Convert.ToDecimal(total_elementary_students_absent + total_junior_high_absentee_rate + total_high_school_students_absent);
                decimal eval_total_rate = Convert.ToDecimal(eval_total_absent / eval_total_enrolled) * 100;
                d_data.OverallAbsenteeRate = eval_total_rate;

                // calculate warning levels
                double elem_eval_rate = 0;
                if (variables.SDFormulaType == "population")
                    elem_eval_rate = CalculateSTDPopulationFromList(elem_rates);
                else
                    elem_eval_rate = CalculateSTDFromList(elem_rates);
                decimal elem_overal_std = Convert.ToDecimal(elem_eval_rate);
                d_data.ElementarySchoolAbsenteeSTD = elem_overal_std;

                double jr_eval_rate = 0;
                if (variables.SDFormulaType == "population")
                    jr_eval_rate = CalculateSTDPopulationFromList(jr_rates);
                else
                    jr_eval_rate = CalculateSTDFromList(jr_rates);
                decimal jr_overal_std = Convert.ToDecimal(jr_eval_rate);
                d_data.JuniorHighSchoolAbsenteeSTD = jr_overal_std;

                double high_eval_rate = 0;
                if (variables.SDFormulaType == "population")
                    high_eval_rate = CalculateSTDPopulationFromList(high_rates);
                else
                    high_eval_rate = CalculateSTDFromList(high_rates);
                decimal high_overal_std = Convert.ToDecimal(high_eval_rate);
                d_data.HighSchoolAbsenteeSTD = high_overal_std;

                double eval_rate = 0;
                if (variables.SDFormulaType == "population")
                    eval_rate = CalculateSTDPopulationFromList(summary_sch_rates);
                else
                    eval_rate = CalculateSTDFromList(summary_sch_rates);
                decimal overal_std = Convert.ToDecimal(eval_rate);
                d_data.OverallAbsenteeSTD = overal_std;

                // calculate detailed rates -- illness and symptom
                decimal illness_overall_rate = (summary_total_absent / summary_total_enrolled) * 100;
                d_data.IllnessRate = illness_overall_rate;

                int total_illness_absences = (summary_total_gastrointestinal + summary_total_respiratory + summary_total_rash + summary_total_other_illness + summary_total_unknown_illness);

                decimal gast_overall_std = CalculateSymptomRate(summary_total_gastrointestinal, total_illness_absences);
                d_data.GastrointestinalRate = gast_overall_std;

                decimal resp_overall_std = CalculateSymptomRate(summary_total_respiratory, total_illness_absences);
                d_data.RespiratoryRate = resp_overall_std;

                decimal rash_overall_std = CalculateSymptomRate(summary_total_rash, total_illness_absences);
                d_data.RashRate = rash_overall_std;

                decimal othr_overall_std = CalculateSymptomRate(summary_total_other_illness, total_illness_absences);
                d_data.OtherRate = othr_overall_std;

                decimal unkn_overall_std = CalculateSymptomRate(summary_total_unknown_illness, total_illness_absences);
                d_data.UnknownRate = unkn_overall_std;

                d_data.TotalAbsent = summary_total_absent;
                d_data.TotalSick = summary_total_sick;
                d_data.Gastrointestinal = summary_total_gastrointestinal;
                d_data.Respiratory = summary_total_respiratory;
                d_data.Rash = summary_total_rash;
                d_data.OtherIllness = summary_total_other_illness;
                d_data.UnknownIllness = summary_total_unknown_illness;
                d_data.TotalEnrolled = summary_total_enrolled;

                d_data.Insert();

                // CALCULATE PRIOR SCHOOL RATE AVERAGES - 2 week average rate, prior year average, historic average
                DateTime solution_start_date = new DateTime();
                solution_start_date = Convert.ToDateTime(variables.SolutionStartDate);
                DateTime end_date = new DateTime();
                end_date = Convert.ToDateTime(d_data.DataDate);
                DateTime two_weeks_prior = new DateTime();
                two_weeks_prior = end_date.AddDays(-14);

                // historic rate
                decimal two_week_prior_rate = qHtl_DailySchoolDistrictAbsenteeSummary.CalculateAbsenteeRateForRange(Convert.ToString(two_weeks_prior), Convert.ToString(end_date), d_data.OverallAbsenteeRate);
                decimal historic_rate = qHtl_DailySchoolDistrictAbsenteeSummary.CalculateAbsenteeRateForRange(Convert.ToString(solution_start_date), Convert.ToString(end_date), d_data.OverallAbsenteeRate);
                d_data.TwoWeekAbsenteeRate = two_week_prior_rate;
                d_data.HistoricAbsenteeRate = historic_rate;

                // historic std
                decimal historic_std = qHtl_DailySchoolDistrictAbsenteeSummary.CalculateAbsenteeRateSTDForRange(Convert.ToString(solution_start_date), Convert.ToString(end_date), d_data.OverallAbsenteeSTD);
                d_data.HistoricAbsenteeSTD = historic_std;

                // district prior rate stds
                double eval_historic_district_std = 0;
                curr_district_all_prior_rates = qHtl_DailySchoolDistrictAbsenteeSummary.GetAllPriorDailyRatesforDistrict(str_solution_start_date, Convert.ToString(DateTime.Now), 0);
                if (variables.SDFormulaType == "population")
                    eval_historic_district_std = CalculateSTDPopulationFromList(curr_district_all_prior_rates);      // used to be curr_school_rates when was based on individual classrooms
                else
                    eval_historic_district_std = CalculateSTDFromList(curr_district_all_prior_rates);

                decimal overall_district_std = Convert.ToDecimal(eval_historic_district_std);
                d_data.HistoricDistrictAbsenteeSTD = overall_district_std;

                // run analysis for this date
                DateTime curr_date = DateTime.Now;
                curr_date = Convert.ToDateTime(eval_date);

                int num_schools = qHtl_DailySchoolAbsenteeData.AnalyzeDailySchoolDataByDate(curr_date, school_district_id);

                // run code to count number of daily warnings
                int num_a_warn = qHtl_DailySchoolAbsenteeData.GetNumWarningsByTypeAndDay("A", curr_date);
                int num_b_warn = qHtl_DailySchoolAbsenteeData.GetNumWarningsByTypeAndDay("B", curr_date);
                int num_c_warn = qHtl_DailySchoolAbsenteeData.GetNumWarningsByTypeAndDay("C", curr_date);
                int num_d_warn = qHtl_DailySchoolAbsenteeData.GetNumWarningsByTypeAndDay("D", curr_date);
                int num_e_warn = qHtl_DailySchoolAbsenteeData.GetNumWarningsByTypeAndDay("E", curr_date);
                int num_f_warn = qHtl_DailySchoolAbsenteeData.GetNumWarningsByTypeAndDay("F", curr_date);

                d_data.NumAWarnings = num_a_warn;
                d_data.NumBWarnings = num_b_warn;
                d_data.NumCWarnings = num_c_warn;
                d_data.NumDWarnings = num_d_warn;
                d_data.NumEWarnings = num_e_warn;
                d_data.NumFWarnings = num_f_warn;
                d_data.Update();
            }

            if (mode == "process")
            {
                lblMessage.Text = message;
                plhStep3.Visible = false;
                plhStep3Completed.Visible = true;
            }
            else if (mode == "test")
            {
                lblTestOutput.Text = message;
                plhStep3.Visible = true;
                plhStep2Completed.Visible = true;
                lblUploadFail.Text = "";
            }
        }
    }
        public static int AnalyzeDailySchoolDataByDate(DateTime curr_date, int school_district_id)
        {
            int schools_analyzed = 0;

            var schools = qHtl_DailySchoolAbsenteeData.GetDailySchoolAbsenteeDataCollectionByDate(curr_date);

            if (schools != null)
            {
                if (schools.Count > 0)
                {
                    qHtl_AbsenteeAnalysisVariable variables = new qHtl_AbsenteeAnalysisVariable(school_district_id);

                    foreach (var s in schools)
                    {
                        // delete any warnings for this school on this date
                        qHtl_HealthWarning.DeleteWarningsBySchoolAndDate(s.SchoolID, Convert.ToString(s.DataDate));

                        var summary = new qHtl_DailySchoolDistrictAbsenteeSummary();

                        if (summary != null)
                        {
                            if (summary.DataDate != s.DataDate)
                                summary = qHtl_DailySchoolDistrictAbsenteeSummary.GetDailySummaryByDate(Convert.ToString(s.DataDate));
                        }
                        else
                        {
                            summary = qHtl_DailySchoolDistrictAbsenteeSummary.GetDailySummaryByDate(Convert.ToString(s.DataDate));
                        }

                        bool a_warning_status = false;
                        string a_warning_evals = string.Empty;
                        bool b_warning_status = false;
                        string b_warning_evals = string.Empty;
                        bool c_warning_status = false;
                        string c_warning_evals = string.Empty;
                        bool d_warning_status = false;
                        string d_warning_evals = string.Empty;
                        bool e_warning_status = false;
                        string e_warning_evals = string.Empty;
                        bool f_warning_status = false;
                        string f_warning_evals = string.Empty;
                        string absentee_status = "green";
                        string absentee_status_7day = "green";
                        string absentee_status_school = "green";
                        string absentee_status_school_7day = "green";
                        string illness_status = "green";
                        string gast_status = "green";
                        string resp_status = "green";
                        string rash_status = "green";
                        string othr_status = "green";
                        string unkn_status = "green";
                        string warning_type = "School Daily Absentee Status Warning";
                        string warning_status = string.Empty;
                        string warning_title = string.Empty;
                        string warning_text = string.Empty;

                        int num_warnings = 0;

                        // perform A-E warning analysis
                        DateTime anal_date = new DateTime();
                        anal_date = Convert.ToDateTime(s.DataDate);
                        var data_days = GetDailySchoolAbsenteeDataCollectionByDateRangeAndSchool(8, anal_date.AddDays(1), s.SchoolID);
                        int x = 0;
                        bool run_check = true;
                        decimal curr_rate = 0;
                        decimal prior_rate = 0;

                        // check A -- One point/day above 3 sigma
                        decimal historic_avg = 0;
                        decimal curr_std = 0;
                        decimal a_eval = 0;
                        foreach (var d in data_days)
                        {
                            x++;
                            if (x == 1)
                            {
                                curr_rate = d.Rate;
                                curr_std = d.RateSTD;
                                historic_avg = d.HistoricRate;
                            }
                        }
                        if (curr_std > 0)
                        {
                            a_eval = historic_avg + (3 * curr_std);
                            a_warning_evals = Convert.ToString(Math.Round(a_eval, 2));
                            if (curr_rate > a_eval)
                                a_warning_status = true;
                        }

                        // check F -- 1 day above 2 sigma
                        decimal f_eval = 0;
                        foreach (var d in data_days)
                        {
                            x++;
                            if (x == 1)
                            {
                                curr_rate = d.Rate;
                                curr_std = d.RateSTD;
                                historic_avg = d.HistoricRate;
                            }
                        }
                        if (curr_std > 0)
                        {
                            f_eval = historic_avg + (2 * curr_std);
                            f_warning_evals = Convert.ToString(Math.Round(f_eval, 2));
                            if (curr_rate > f_eval)
                                f_warning_status = true;
                        }

                        // check B -- 2 out of 3 days above 2 standard deviations
                        x = 0;
                        curr_std = 0;
                        curr_rate = 0;
                        historic_avg = 0;
                        decimal b_value = 0;
                        int b_meets = 0;
                        foreach (var d in data_days)
                        {
                            if (x < 3)
                            {
                                curr_rate = d.Rate;
                                curr_std = d.RateSTD;
                                historic_avg = d.HistoricRate;

                                if (x == 0 || x == 1 || x == 2)
                                {
                                    b_value = historic_avg + (2 * curr_std);
                                    b_warning_evals += " " + Convert.ToString(Math.Round(b_value, 2));
                                    if (curr_rate > b_value)
                                        b_meets++;
                                }
                            }
                            x++;
                        }
                        if (b_meets >= 2)
                            b_warning_status = true;

                        // check C -- 4 out of 5 days above 1 standard deviations
                        x = 0;
                        curr_std = 0;
                        curr_rate = 0;
                        historic_avg = 0;
                        decimal c_value = 0;
                        int c_meets = 0;
                        foreach (var d in data_days)
                        {
                            if (x < 5)
                            {
                                curr_rate = d.Rate;
                                curr_std = d.RateSTD;
                                historic_avg = d.HistoricRate;

                                if (x == 0 || x == 1 || x == 2 || x == 3 || x == 4)
                                {
                                    c_value = historic_avg + (1 * curr_std);
                                    c_warning_evals += " " + Convert.ToString(Math.Round(c_value, 2));
                                    if (curr_rate > c_value)
                                        c_meets++;
                                }
                            }
                            x++;
                        }
                        if (c_meets >= 4)
                            c_warning_status = true;

                        // check D -- 8 consecutive days above the moving average
                        x = 0;
                        int d_check_threshold = 8;
                        run_check = true;
                        int d_num_days = 0;
                        decimal moving = 0;
                        prior_rate = 0;
                        foreach (var d in data_days)
                        {
                            if (x == 0)
                                moving = d.HistoricRate;

                            if (x < d_check_threshold)
                            {
                                curr_rate = d.Rate;
                                if (run_check == true)
                                {
                                    if (curr_rate > moving)
                                    {
                                        run_check = true;
                                        d_num_days++;
                                    }
                                    else
                                        run_check = false;
                                }
                                prior_rate = curr_rate;
                                d_warning_evals += " " + Convert.ToString(Math.Round(curr_rate, 2));
                                x++;
                            }
                        }
                        if (d_num_days == d_check_threshold)
                            d_warning_status = true;

                        // check E -- 6 days of increasing absentee rates
                        x = 0;
                        int e_check_threshold = 6;
                        run_check = true;
                        int e_num_days = 0;
                        curr_rate = 0;
                        prior_rate = 0;
                        foreach (var d in data_days)
                        {
                            if (x < e_check_threshold)
                            {
                                curr_rate = d.Rate;
                                if (run_check == true)
                                {
                                    if (curr_rate < prior_rate)
                                    {
                                        run_check = true;
                                        e_num_days++;
                                    }
                                    else
                                        run_check = false;
                                }
                                prior_rate = curr_rate;
                                e_warning_evals += " " + Convert.ToString(Math.Round(curr_rate, 2));
                                x++;
                            }
                        }
                        if (e_num_days == e_check_threshold)
                            e_warning_status = true;

                        // perform analysis
                        if (s.Rate < (summary.OverallAbsenteeSTD * variables.GreenRateSTDMultiplier))
                            absentee_status = "green";
                        else if (s.Rate >= (summary.OverallAbsenteeSTD * variables.YellowRateSTDMultiplier))
                        {
                            absentee_status = "yellow";
                            warning_status += "yellow ";
                            warning_status += "red ";
                            warning_title = "Yellow Status | Absentee Rate at " + s.School + " (" + s.SchoolLevel + " School) on " + String.Format("{0:MMMM d, yyyy}", s.DataDate);
                            warning_text += "<strong>Yellow Status</strong> | Absentee Rate at " + s.School + " (" + s.SchoolLevel + " School) on " + String.Format("{0:MMMM d, yyyy}", s.DataDate);
                            warning_text += "<br><strong>Reason</strong>: Absentee rate of " + Math.Round(s.Rate, 2) + "% is greater than " + (summary.OverallAbsenteeSTD * variables.YellowRateSTDMultiplier) + "% or less than " + (summary.OverallAbsenteeSTD * variables.RedRateSTDMultiplier) + "% of the school's enrollment.<br><br>";
                            num_warnings++;
                        }
                        else if (s.Rate >= (s.RateSTD * variables.RedRateSTDMultiplier))
                        {
                            absentee_status = "red";
                            warning_status += "red ";
                            warning_title = "Red Status | Absentee Rate at " + s.School + " (" + s.SchoolLevel + " School) on " + String.Format("{0:MMMM d, yyyy}", s.DataDate);
                            warning_text += "<strong>Red Status</strong> | Absentee Rate at " + s.School + " (" + s.SchoolLevel + " School) on " + String.Format("{0:MMMM d, yyyy}", s.DataDate);
                            warning_text += "<br><strong>Reason</strong>: Absentee rate of " + Math.Round(s.Rate, 2) + "% is greater than " + (summary.OverallAbsenteeSTD * variables.RedRateSTDMultiplier) + " of the school's enrollment.<br><br>";
                            num_warnings++;
                        }

                        if (s.IllnessRate < variables.GreenIllnessBoundary)
                            illness_status = "green";
                        else if (s.IllnessRate >= variables.GreenIllnessBoundary && s.IllnessRate <= variables.RedIllnessBoundary)
                        {
                            illness_status = "yellow";
                            warning_status += "yellow ";
                            warning_title = "Yellow Status | Illness Rate at " + s.School + " (" + s.SchoolLevel + " School) on " + String.Format("{0:MMMM d, yyyy}", s.DataDate);
                            warning_text += "<strong>Yellow Status</strong> | Illness Rate at " + s.School + " (" + s.SchoolLevel + " School) on " + String.Format("{0:MMMM d, yyyy}", s.DataDate);
                            warning_text += "<br><strong>Reason</strong>: Illness rate of " + Math.Round(s.IllnessRate, 2) + "% is between " + variables.GreenIllnessBoundary + " and " + variables.RedIllnessBoundary + " of the school's enrollment.<br><br>";
                            num_warnings++;
                        }
                        else if (s.IllnessRate > variables.RedIllnessBoundary)
                        {
                            illness_status = "red";
                            warning_status += "red ";
                            warning_title = "Red Status | Illness Rate at " + s.School + " (" + s.SchoolLevel + " School) on " + String.Format("{0:MMMM d, yyyy}", s.DataDate);
                            warning_text += "<strong>Red Status</strong> | Illness Rate at " + s.School + " (" + s.SchoolLevel + " School) on " + String.Format("{0:MMMM d, yyyy}", s.DataDate);
                            warning_text += "<br><strong>Reason</strong>: Illness rate of " + Math.Round(s.IllnessRate, 2) + "% is greater than " + variables.RedIllnessBoundary + " of the school's enrollment.<br><br>";
                            num_warnings++;
                        }

                        if (s.GastrointestinalRate < variables.GreenGastrointestinalBoundary)
                            gast_status = "green";
                        else if (s.GastrointestinalRate >= variables.GreenGastrointestinalBoundary && s.GastrointestinalRate <= variables.RedGastrointestinalBoundary)
                        {
                            gast_status = "yellow";
                            warning_status += "yellow ";
                            warning_title = "Yellow Status | Gastrintestinal Symptom Rate at " + s.School + " (" + s.SchoolLevel + " School) on " + String.Format("{0:MMMM d, yyyy}", s.DataDate);
                            warning_text += "<strong>Yellow Status</strong> | Gastrintestinal Symptom Rate at " + s.School + " (" + s.SchoolLevel + " School) on " + String.Format("{0:MMMM d, yyyy}", s.DataDate);
                            warning_text += "<br><strong>Reason</strong>: Gastrintestinal symptom rate of " + Math.Round(s.GastrointestinalRate, 2) + "% is between " + variables.GreenGastrointestinalBoundary + " and " + variables.RedGastrointestinalBoundary + " of all school absences with reported symptoms.<br><br>";
                            num_warnings++;
                        }
                        else if (s.GastrointestinalRate > variables.RedGastrointestinalBoundary)
                        {
                            gast_status = "red";
                            warning_status += "red ";
                            warning_title = "Red Status | Gastrintestinal Symptom Rate at " + s.School + " (" + s.SchoolLevel + " School) on " + String.Format("{0:MMMM d, yyyy}", s.DataDate);
                            warning_text += "<strong>Red Status</strong> | Gastrintestinal Symptom Rate at " + s.School + " (" + s.SchoolLevel + " School) on " + String.Format("{0:MMMM d, yyyy}", s.DataDate);
                            warning_text += "<br><strong>Reason</strong>: Gastrintestinal symptom rate of " + Math.Round(s.GastrointestinalRate, 2) + "% is greater than " + variables.RedGastrointestinalBoundary + " of all school absences with reported symptoms.<br><br>"; ;
                            num_warnings++;
                        }

                        if (s.RespiratoryRate < variables.GreenRespiratoryBoundary)
                            resp_status = "green";
                        else if (s.RespiratoryRate >= variables.GreenRespiratoryBoundary && s.RespiratoryRate <= variables.RedRespiratoryBoundary)
                        {
                            resp_status = "yellow";
                            warning_status += "yellow ";
                            warning_title = "Yellow Status | Respiratory Symptom Rate at " + s.School + " (" + s.SchoolLevel + " School) on " + String.Format("{0:MMMM d, yyyy}", s.DataDate);
                            warning_text += "<strong>Yellow Status</strong> | Respiratory Symptom Rate at " + s.School + " (" + s.SchoolLevel + " School) on " + String.Format("{0:MMMM d, yyyy}", s.DataDate);
                            warning_text += "<br><strong>Reason</strong>: Respiratory symptom rate of " + Math.Round(s.RespiratoryRate, 2) + "% is between " + variables.GreenRespiratoryBoundary + " and " + variables.RedRespiratoryBoundary + " of all school absences with reported symptoms.<br><br>";
                            num_warnings++;
                        }
                        else if (s.RespiratoryRate > variables.RedRespiratoryBoundary)
                        {
                            resp_status = "red";
                            warning_status += "red ";
                            warning_title = "Red Status | Respiratory Symptom Rate at " + s.School + " (" + s.SchoolLevel + " School) on " + String.Format("{0:MMMM d, yyyy}", s.DataDate);
                            warning_text += "<strong>Red Status</strong> | Respiratory Symptom Rate at " + s.School + " (" + s.SchoolLevel + " School) on " + String.Format("{0:MMMM d, yyyy}", s.DataDate);
                            warning_text += "<br><strong>Reason</strong>: Respiratory symptom rate of " + Math.Round(s.RespiratoryRate, 2) + "% is greater than " + variables.RedRespiratoryBoundary + "% of all school absences with reported symptoms.<br><br>"; ;
                            num_warnings++;
                        }

                        if (s.RashRate < variables.GreenRashBoundary)
                            rash_status = "green";
                        else if (s.RashRate >= variables.GreenRashBoundary && s.RespiratoryRate <= variables.RedRashBoundary)
                        {
                            rash_status = "yellow";
                            warning_status += "yellow ";
                            warning_title = "Yellow Status | Rash Symptom Rate at " + s.School + " (" + s.SchoolLevel + " School) on " + String.Format("{0:MMMM d, yyyy}", s.DataDate);
                            warning_text += "<strong>Yellow Status</strong> | Rash Symptom Rate at " + s.School + " (" + s.SchoolLevel + " School) on " + String.Format("{0:MMMM d, yyyy}", s.DataDate);
                            warning_text += "<br><strong>Reason</strong>: Rash symptom rate of " +Math.Round(s.RashRate, 2) + "% is between " + variables.GreenRashBoundary + "% and " + variables.RedRashBoundary + "% of all school absences with reported symptoms.<br><br>";
                            num_warnings++;
                        }
                        else if (s.RashRate > variables.RedRashBoundary)
                        {
                            rash_status = "red";
                            warning_status += "red ";
                            warning_title = "Red Status | Rash Symptom Rate at " + s.School + " (" + s.SchoolLevel + " School) on " + String.Format("{0:MMMM d, yyyy}", s.DataDate);
                            warning_text += "<strong>Red Status</strong> | Rash Symptom Rate at " + s.School + " (" + s.SchoolLevel + " School) on " + String.Format("{0:MMMM d, yyyy}", s.DataDate);
                            warning_text += "<br><strong>Reason</strong>: Rash symptom rate of " + Math.Round(s.RashRate, 2) + "% is greater than " + variables.RedRashBoundary + "% of all school absences with reported symptoms.<br><br>"; ;
                            num_warnings++;
                        }

                        if (s.OtherRate < variables.GreenOtherIllnessBoundary)
                            othr_status = "green";
                        else if (s.OtherRate >= variables.GreenOtherIllnessBoundary && s.RespiratoryRate <= variables.RedOtherIllnessBoundary)
                        {
                            othr_status = "yellow";
                            warning_status += "yellow ";
                            warning_title = "Yellow Status | Other Illness Symptom Rate at " + s.School + " (" + s.SchoolLevel + " School) on " + String.Format("{0:MMMM d, yyyy}", s.DataDate);
                            warning_text += "<strong>Yellow Status</strong> | Other Illness Rate at " + s.School + " (" + s.SchoolLevel + " School) on " + String.Format("{0:MMMM d, yyyy}", s.DataDate);
                            warning_text += "<br><strong>Reason</strong>: Other illness symptom rate of " + Math.Round(s.OtherRate, 2) + "% is between " + variables.GreenOtherIllnessBoundary + "% and " + variables.RedOtherIllnessBoundary + "% of all school absences with reported symptoms.<br><br>";
                            num_warnings++;
                        }
                        else if (s.OtherRate > variables.RedOtherIllnessBoundary)
                        {
                            othr_status = "red";
                            warning_status += "yellow ";
                            warning_title = "Red Status | Other Illness Symptom Rate at " + s.School + " (" + s.SchoolLevel + " School) on " + String.Format("{0:MMMM d, yyyy}", s.DataDate);
                            warning_text += "<strong>Red Status</strong> | Other Illness Symptom Rate at " + s.School + " (" + s.SchoolLevel + " School) on " + String.Format("{0:MMMM d, yyyy}", s.DataDate);
                            warning_text += "<br><strong>Reason</strong>: Other illness symptom rate of " + Math.Round(s.OtherRate, 2) + "% is greater than " + variables.RedOtherIllnessBoundary + "% of all school absences with reported symptoms.<br><br>"; ;
                            num_warnings++;
                        }

                        if (s.UnknownRate < variables.GreenUnknownIllnessBoundary)
                            unkn_status = "green";
                        else if (s.UnknownRate >= variables.GreenUnknownIllnessBoundary && s.RespiratoryRate <= variables.RedUnknownIllnessBoundary)
                        {
                            unkn_status = "yellow";
                            warning_status += "yellow ";
                            warning_title = "Yellow Status | Unknown Illness Symptom Rate at " + s.School + " (" + s.SchoolLevel + " School) on " + String.Format("{0:MMMM d, yyyy}", s.DataDate);
                            warning_text += "<strong>Yellow Status</strong> | Unknown Illness Rate at " + s.School + " (" + s.SchoolLevel + " School) on " + String.Format("{0:MMMM d, yyyy}", s.DataDate);
                            warning_text += "<br><strong>Reason</strong>: Unknown illness symptom rate of " + Math.Round(s.UnknownRate, 2) + "% is between " + variables.GreenUnknownIllnessBoundary + "% and " + variables.RedUnknownIllnessBoundary + "% of all school absences with reported symptoms.<br><br>";
                            num_warnings++;
                        }
                        else if (s.UnknownRate > variables.RedUnknownIllnessBoundary)
                        {
                            unkn_status = "red";
                            warning_status += "red ";
                            warning_title = "Red Status | Unknown Illness Symptom Rate at " + s.School + " (" + s.SchoolLevel + " School) on " + String.Format("{0:MMMM d, yyyy}", s.DataDate);
                            warning_text += "<strong>Red Status</strong> | Unknown Illness Rate at " + s.School + " (" + s.SchoolLevel + " School) on " + String.Format("{0:MMMM d, yyyy}", s.DataDate);
                            warning_text += "<br><strong>Reason</strong>: Unknown illness symptom rate of " + Math.Round(s.OtherRate, 2) + "% is greater than " + variables.RedUnknownIllnessBoundary + "% of all school absences with reported symptoms.<br><br>"; ;
                            num_warnings++;
                        }

                        // update record
                        qHtl_DailySchoolAbsenteeData curr_school = new qHtl_DailySchoolAbsenteeData(s.SchoolID, curr_date);
                        curr_school.AbsenteeStatus = absentee_status;
                        curr_school.AbsenteeStatus7Day = absentee_status_7day;
                        curr_school.AbsenteeStatusSchool = absentee_status_school;
                        curr_school.AbsenteeStatusSchool7Day = absentee_status_school_7day;
                        curr_school.IllnessStatus = illness_status;
                        curr_school.GastrointestinalStatus = gast_status;
                        curr_school.RespiratoryStatus = resp_status;
                        curr_school.RashStatus = rash_status;
                        curr_school.OtherIllnessStatus = othr_status;
                        curr_school.UnknownIllnessStatus = unkn_status;
                        curr_school.A_Warning = a_warning_status;
                        curr_school.A_WarningValues = a_warning_evals;
                        curr_school.B_Warning = b_warning_status;
                        curr_school.B_WarningValues = b_warning_evals;
                        curr_school.C_Warning = c_warning_status;
                        curr_school.C_WarningValues = c_warning_evals;
                        curr_school.D_Warning = d_warning_status;
                        curr_school.D_WarningValues = d_warning_evals;
                        curr_school.E_Warning = e_warning_status;
                        curr_school.E_WarningValues = e_warning_evals;
                        curr_school.F_Warning = f_warning_status;
                        curr_school.F_WarningValues = f_warning_evals;
                        curr_school.Update();

                        if (num_warnings > 1)
                            warning_title = "Multiple School Absentee Status Warnings at " + s.School + " (" + s.SchoolLevel + " School) on " + String.Format("{0:MMMM d, yyyy}", s.DataDate);

                        if (num_warnings > 0)
                            CreateSchoolWarning(s.SchoolID, Convert.ToDateTime(s.DataDate), warning_status, warning_type, warning_title, warning_text);
                    }

                    schools_analyzed = schools.Count;
                }
            }

            return schools_analyzed;
        }
    protected void Page_Load(object sender, EventArgs e)
    {
        int curr_school_district_id = 0;
        if (school_district_id == 0)
            school_district_id = 1;

        qHtl_AbsenteeAnalysisVariable variables = new qHtl_AbsenteeAnalysisVariable(school_district_id);

        if (!Page.IsPostBack)
        {
            var summary = new qHtl_DailySchoolDistrictAbsenteeSummary();

            if (String.IsNullOrEmpty(Request.QueryString["currDate"]))
            {
                summary = qHtl_DailySchoolDistrictAbsenteeSummary.GetMostRecentDailySummary();
            }
            else
            {
                string eval_curr_date = Convert.ToString(Request.QueryString["currDate"]);
                summary = qHtl_DailySchoolDistrictAbsenteeSummary.GetDailySummaryByDate(eval_curr_date);
            }

            if (summary != null)
            {
                if (summary.DailySchoolDistrictAbsenteeSummaryID > 0)
                {
                    DateTime curr_date = new DateTime();
                    curr_date = Convert.ToDateTime(summary.DataDate);
                    DailySchoolAbsenteeDashboard.CurrDate = curr_date;

                    Session["curr_date"] = curr_date;

                    litDataDate.Text = String.Format("{0:dddd, MMMM d, yyyy}", curr_date);

                    // possibly add daily school level details control
                }

                schooldistrictsidebar.Summary = summary;
                //DailySummaryCharts.Summary = summary;

                var prior_summary = qHtl_DailySchoolDistrictAbsenteeSummary.GetDailySummaryPriorOrAfter("prior", Convert.ToString(summary.DataDate));
                var after_summary = qHtl_DailySchoolDistrictAbsenteeSummary.GetDailySummaryPriorOrAfter("after", Convert.ToString(summary.DataDate));

                if (prior_summary != null)
                {
                    if (prior_summary.DailySchoolDistrictAbsenteeSummaryID > 0)
                    {
                        string prior_date = String.Format("{0:M/d/yyyy}", prior_summary.DataDate);
                        litDateBefore.Text = "<a href=\"default.aspx?currDate=" + prior_date + "\" class=\"btn btn-large\"><i class=\"glyphicon-step_backward\"></i></a>";
                    }
                }
                if (after_summary != null)
                {
                    if (after_summary.DailySchoolDistrictAbsenteeSummaryID > 0)
                    {
                        string after_date = String.Format("{0:M/d/yyyy}", after_summary.DataDate);
                        litDateAfter.Text = "<a href=\"default.aspx?currDate=" + after_date + "\" class=\"btn btn-large\"><i class=\"glyphicon-step_forward\"></i></a>";
                    }
                }
            }
        }

        if (!String.IsNullOrEmpty(Request.QueryString["schoolDistrictID"]))
        {
            curr_school_district_id = Convert.ToInt32(Request.QueryString["spaceID"]);
        }
        else
        {
            // get first space associated with this user
            var districts = qPtl_SchoolDistrictAdmin_View.GetSchoolDistrictAdminsByUser(Convert.ToInt32(Context.Items["UserID"]));
            int i = 0;
            foreach (var d in districts)
            {
                if (i == 0)
                {
                    curr_school_district_id = d.SchoolDistrictID;

                    // set session variable

                }
                i++;
            }
        }
        school_district_id = curr_school_district_id;
        loadPageInfo(curr_school_district_id);
        schooldistrictsidebar.SchoolDistrictID = curr_school_district_id;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (school_district_id == 0)
                school_district_id = 1;

            qHtl_AbsenteeAnalysisVariable variables = new qHtl_AbsenteeAnalysisVariable(school_district_id);

            if (summary == null)
            {
                if (String.IsNullOrEmpty(Request.QueryString["currDate"]))
                {
                    summary = qHtl_DailySchoolDistrictAbsenteeSummary.GetMostRecentDailySummary();
                }
                else
                {
                    string eval_curr_date = Convert.ToString(Request.QueryString["currDate"]);
                    summary = qHtl_DailySchoolDistrictAbsenteeSummary.GetDailySummaryByDate(eval_curr_date);
                }
            }

            if (summary != null)
            {
                if (summary.DailySchoolDistrictAbsenteeSummaryID > 0)
                {
                    DateTime curr_date = new DateTime();
                    curr_date = Convert.ToDateTime(summary.DataDate);
                    litMostRecentDate.Text = String.Format("{0:MMMM d, yyyy}", curr_date);
                    litEnrolled.Text = String.Format("{0:0,0}", summary.TotalEnrolled);
                    litAbsences.Text = String.Format("{0:0,0}", summary.TotalAbsent);

                    int total_illness = summary.Gastrointestinal + summary.Rash + summary.Respiratory + summary.Rash + summary.OtherIllness;
                    int total_unknown = summary.TotalAbsent - total_illness;
                    litIllness.Text = String.Format("{0:0,0}", total_illness);
                    litUnknown.Text = String.Format("{0:0,0}", total_unknown);
                    litRate.Text = Convert.ToString(Math.Round(summary.OverallAbsenteeRate, 2));
                    litMovingAverage.Text = Convert.ToString(Math.Round(summary.HistoricAbsenteeRate, 2));

                    decimal num_gast = Convert.ToDecimal(summary.Gastrointestinal);
                    decimal num_resp = Convert.ToDecimal(summary.Respiratory);
                    decimal num_rash = Convert.ToDecimal(summary.Rash);
                    decimal num_othr = Convert.ToDecimal(summary.OtherIllness);
                    decimal num_illness_total = num_gast + num_resp + num_rash + num_othr;

                    decimal percent_gast = 0;
                    decimal percent_resp = 0;
                    decimal percent_rash = 0;
                    decimal percent_othr = 0;

                    if (num_gast > 0)
                    {
                        percent_gast = num_gast / num_illness_total;
                        percent_gast = percent_gast * 100;
                    }
                    if (num_resp > 0)
                    {
                        percent_resp = num_resp / num_illness_total;
                        percent_resp = percent_resp * 100;
                    }
                    if (num_rash > 0)
                    {
                        percent_rash = num_rash / num_illness_total;
                        percent_rash = percent_rash * 100;
                    }
                    if (num_othr > 0)
                    {
                        percent_othr = num_othr / num_illness_total;
                        percent_othr = percent_othr * 100;
                    }
                    litGastBar.Text = "style=\"width:" + percent_gast + "%\"";
                    litRespBar.Text = "style=\"width:" + percent_resp + "%\"";
                    litRashBar.Text = "style=\"width:" + percent_rash + "%\"";
                    litOthrBar.Text = "style=\"width:" + percent_othr + "%\"";

                    litGastrointestinal.Text = "(" + Convert.ToString(summary.Gastrointestinal) + ")";
                    litRespiratory.Text = "(" + Convert.ToString(summary.Respiratory) + ")";
                    litRash.Text = "(" + Convert.ToString(summary.Rash) + ")";
                    litOtherIllness.Text = "(" + Convert.ToString(summary.OtherIllness) + ")";

                    litProcessed.Text = Convert.ToString(summary.Created);

                    if (num_warnings > 0)
                        litNumWarnings.Text = "<a href=\"/manage/school-districts/school-health-warnings.aspx\">" + num_warnings + " A-E Warnings</a>";
                    else
                    {
                        int num_a_e_warnings = 0;

                        num_a_e_warnings = qHtl_DailySchoolAbsenteeData.CountNumberAEWarningsByDate(Convert.ToDateTime(summary.DataDate));

                        if (num_a_e_warnings > 0)
                            litNumWarnings.Text = "<i class=\"icon-warning-sign\"></i> <strong>(" + num_a_e_warnings + ")</strong> Absentee Warnings";
                        else
                            litNumWarnings.Text = "No warnings for this date";
                    }
                }
            }
        }
    }