void PopulateByIncidentId(decimal incidentId)
        {
            var entities = new PSsqmEntities();

            var incident = EHSIncidentMgr.SelectIncidentById(entities, incidentId);

            string plantName = EHSIncidentMgr.SelectPlantNameById((decimal)incident.DETECT_PLANT_ID);

            lblPlantName.Text  = String.Format("Location: {0}", plantName);
            lblIncidentId.Text = String.Format("Incident ID: {0}", incidentId);
            //lblCaseId.Text = String.Format("Incident ID: {0}", incidentId);

            string  incidentType   = EHSIncidentMgr.SelectIncidentTypeByIncidentId(incidentId);
            decimal incidentTypeId = EHSIncidentMgr.SelectIncidentTypeIdByIncidentId(incidentId);
            decimal companyId      = incident.DETECT_COMPANY_ID;
            var     questions      = EHSIncidentMgr.SelectIncidentQuestionList(incidentTypeId, companyId, 0);

            questions.AddRange(EHSIncidentMgr.SelectIncidentQuestionList(incidentTypeId, companyId, 1));

            // Date/Time

            ltrDate.Text = incident.INCIDENT_DT.ToLongDateString();

            var timeQuestion = questions.FirstOrDefault(q => q.QuestionId == (decimal)EHSQuestionId.TimeOfDay);

            if (timeQuestion != null)
            {
                string timeAnswer = (from a in entities.INCIDENT_ANSWER
                                     where
                                     a.INCIDENT_ID == incident.INCIDENT_ID &&
                                     a.INCIDENT_QUESTION_ID == (decimal)EHSQuestionId.TimeOfDay
                                     select a.ANSWER_VALUE).FirstOrDefault();

                if (!string.IsNullOrEmpty(timeAnswer))
                {
                    ltrTime.Text = Convert.ToDateTime(timeAnswer).ToShortTimeString();
                }
            }

            // Incident Type

            ltrIncidentType.Text = incidentType;

            // Description

            ltrDescription.Text = "<div style=\"width: 600px; word-wrap: break-word;\">" + Server.HtmlEncode(incident.DESCRIPTION) + "</div>";

            // Root Cause(s)

            var rootCauseQuestion = questions.FirstOrDefault(q => q.QuestionId == (decimal)EHSQuestionId.RootCause);

            if (rootCauseQuestion != null)
            {
                string rootCauseAnswer = (from a in entities.INCIDENT_ANSWER
                                          where
                                          a.INCIDENT_ID == incidentId &&
                                          a.INCIDENT_QUESTION_ID == (decimal)EHSQuestionId.RootCause
                                          select a.ANSWER_VALUE).FirstOrDefault();

                if (!string.IsNullOrEmpty(rootCauseAnswer))
                {
                    ltrRootCause.Text = "<div style=\"width: 600px; word-wrap: break-word;\">" + Server.HtmlEncode(rootCauseAnswer) + "</div>";
                }
            }


            // Containment

            var containmentQuestion = questions.FirstOrDefault(q => q.QuestionId == (decimal)EHSQuestionId.Containment);

            if (containmentQuestion != null)
            {
                string containmentAnswer = (from a in entities.INCIDENT_ANSWER
                                            where
                                            a.INCIDENT_ID == incidentId &&
                                            a.INCIDENT_QUESTION_ID == (decimal)EHSQuestionId.Containment
                                            select a.ANSWER_VALUE).FirstOrDefault();

                if (!string.IsNullOrEmpty(containmentAnswer))
                {
                    ltrContainment.Text = "<div style=\"width: 600px; word-wrap: break-word;\">" + Server.HtmlEncode(containmentAnswer) + "</div>";
                }
            }

            // Corrective Actions

            var correctiveQuestion = questions.FirstOrDefault(q => q.QuestionId == (decimal)EHSQuestionId.CorrectiveActions);

            if (correctiveQuestion != null)
            {
                string correctiveAnswer = (from a in entities.INCIDENT_ANSWER
                                           where
                                           a.INCIDENT_ID == incidentId &&
                                           a.INCIDENT_QUESTION_ID == (decimal)EHSQuestionId.CorrectiveActions
                                           select a.ANSWER_VALUE).FirstOrDefault();

                if (!string.IsNullOrEmpty(correctiveAnswer))
                {
                    ltrCorrectiveActions.Text = "<div style=\"width: 600px; word-wrap: break-word;\">" + Server.HtmlEncode(correctiveAnswer) + "</div>";
                }
            }

            // Photos

            BindAttachmentsIncident(incident.INCIDENT_ID);
        }
        void PopulateByProblemCaseId(decimal problemCaseId)
        {
            PROB_CASE probCase = ProblemCase.LookupCase(entities, problemCaseId);

            if (probCase != null)
            {
                List <INCIDENT> incidentList = ProblemCase.LookupProbIncidentList(entities, probCase);

                ltrDate.Text = probCase.CREATE_DT.ToString();
                if (incidentList.Count > 0)
                {
                    var incident = incidentList[0];

                    string plantName = EHSIncidentMgr.SelectPlantNameById((decimal)incident.DETECT_PLANT_ID);
                    lblPlantName.Text  = String.Format("Location: {0}", plantName);
                    lblIncidentId.Text = String.Format("Incident ID: {0}", incident.INCIDENT_ID);
                    //lblCaseId.Text = String.Format("Problem Case ID: {0}", probCase.PROBCASE_ID);

                    string  incidentType   = EHSIncidentMgr.SelectIncidentTypeByIncidentId(incident.INCIDENT_ID);
                    decimal incidentTypeId = EHSIncidentMgr.SelectIncidentTypeIdByIncidentId(incident.INCIDENT_ID);
                    decimal companyId      = incident.DETECT_COMPANY_ID;
                    var     questions      = EHSIncidentMgr.SelectIncidentQuestionList(incidentTypeId, companyId, 0);
                    questions.AddRange(EHSIncidentMgr.SelectIncidentQuestionList(incidentTypeId, companyId, 1));

                    // Date/Time

                    ltrDate.Text = incidentList[0].INCIDENT_DT.ToLongDateString();

                    var timeQuestion = questions.FirstOrDefault(q => q.QuestionId == 5);
                    if (timeQuestion != null)
                    {
                        string timeAnswer = (from a in entities.INCIDENT_ANSWER
                                             where
                                             a.INCIDENT_ID == incident.INCIDENT_ID &&
                                             a.INCIDENT_QUESTION_ID == 5
                                             select a.ANSWER_VALUE).FirstOrDefault();

                        if (!string.IsNullOrEmpty(timeAnswer))
                        {
                            ltrTime.Text = Convert.ToDateTime(timeAnswer).ToShortTimeString();
                        }
                    }

                    // Incident Type

                    ltrIncidentType.Text = incidentType;

                    // Description

                    ltrDescription.Text = "<div style=\"width: 600px; word-wrap: break-word;\">" + Server.HtmlEncode(probCase.DESC_LONG) + "</div>";


                    // Root Cause(s)

                    List <PROB_CAUSE_STEP> probCauses = (from i in entities.PROB_CAUSE_STEP
                                                         where
                                                         i.PROBCASE_ID == problemCaseId &&
                                                         i.IS_ROOTCAUSE == true
                                                         select i).ToList();
                    if (probCauses.Count > 0)
                    {
                        ltrRootCause.Text = "<ul>";
                        foreach (var pc in probCauses)
                        {
                            ltrRootCause.Text += "<li>" + Server.HtmlEncode(pc.WHY_OCCUR) + "</li>";
                        }
                        ltrRootCause.Text += "</ul>";
                    }

                    // Containment

                    var containment = (from i in entities.PROB_CONTAIN
                                       where
                                       i.PROBCASE_ID == problemCaseId
                                       select new
                    {
                        Disposition = i.INITIAL_DISPOSITION,
                        Action = i.INITIAL_ACTION,
                        Results = i.INITIAL_RESULTS
                    }).FirstOrDefault();

                    if (containment != null)
                    {
                        ltrContainment.Text = "<ul><li>Initial Disposition: " + Server.HtmlEncode(containment.Disposition) + "</li>" +
                                              "<li>Action: " + Server.HtmlEncode(containment.Action) + "</li>" +
                                              "<li>Results: " + Server.HtmlEncode(containment.Results) + "</li>" +
                                              "</ul>";
                    }

                    // Corrective Actions

                    var correctiveActions = (from i in entities.PROB_CAUSE_ACTION
                                             where
                                             i.PROBCASE_ID == problemCaseId
                                             select i.ACTION_DESC).ToList();
                    if (correctiveActions.Count > 0)
                    {
                        ltrCorrectiveActions.Text = "<ul>";
                        foreach (var caDesc in correctiveActions)
                        {
                            ltrCorrectiveActions.Text += "<li>" + Server.HtmlEncode(caDesc) + "</li>";
                        }
                        ltrCorrectiveActions.Text += "</ul>";
                    }

                    // Photos

                    BindAttachmentsProbCase(incident.INCIDENT_ID, probCase.PROBCASE_ID);
                }
                else
                {
                    pnlContent.Visible = false;
                    pnlError.Visible   = true;
                }
            }
        }
        // "Mini 8D" fields at the end of every incident form - to be hidden in the 8D Definition step
        //List<decimal> problemFieldIds = new List<decimal>() { 69, 24, 27, 70, 64, 65, 72, 66, 67 };

        //public bool HideProblemFields = false;

        public int Refresh(decimal incidentId, int[] steps)
        {
            int     status         = 0;
            var     entities       = new PSsqmEntities();
            var     companyId      = SessionManager.UserContext.WorkingLocation.Company.COMPANY_ID;
            decimal?incidentTypeId = (from i in entities.INCIDENT where i.INCIDENT_ID == incidentId select i.ISSUE_TYPE_ID).FirstOrDefault();

            var sb = new StringBuilder();

            if (incidentTypeId != null)
            {
                foreach (int step in steps)
                {
                    var questions = EHSIncidentMgr.SelectIncidentQuestionList((decimal)incidentTypeId, companyId, step);

                    sb.AppendLine("<table class=\"lightTable\" cellspacing=\"0\" style=\"width: 100%\">");
                    foreach (var q in questions)
                    {
                        try
                        {
                            string answer = (from a in entities.INCIDENT_ANSWER
                                             where a.INCIDENT_ID == incidentId && a.INCIDENT_QUESTION_ID == q.QuestionId
                                             select a.ANSWER_VALUE).FirstOrDefault();
                            answer = (answer == null) ? "" : answer;
                            answer = answer.Replace("<a href", "<a target=\"blank\" href");
                            string answerText = "";
                            if (!string.IsNullOrEmpty(answer) ||
                                q.QuestionType == EHSIncidentQuestionType.Attachment ||
                                q.QuestionType == EHSIncidentQuestionType.PageOneAttachment)
                            {
                                switch (q.QuestionType)
                                {
                                case EHSIncidentQuestionType.Date:
                                    answer     = DateTime.Parse(answer, CultureInfo.GetCultureInfo("en-US")).ToShortDateString();
                                    answerText = Server.HtmlEncode(answer);
                                    break;

                                case EHSIncidentQuestionType.Time:
                                    answer     = DateTime.Parse(answer, CultureInfo.GetCultureInfo("en-US")).ToShortTimeString();
                                    answerText = Server.HtmlEncode(answer);
                                    break;

                                case EHSIncidentQuestionType.DateTime:
                                    answer     = DateTime.Parse(answer, CultureInfo.GetCultureInfo("en-US")).ToString();
                                    answerText = Server.HtmlEncode(answer);
                                    break;

                                case EHSIncidentQuestionType.LocationDropdown:
                                    answer     = EHSIncidentMgr.SelectPlantNameById(Convert.ToDecimal(answer));
                                    answerText = Server.HtmlEncode(answer);
                                    break;

                                case EHSIncidentQuestionType.UsersDropdown:
                                    answer     = EHSIncidentMgr.SelectUserNameById(Convert.ToDecimal(answer));
                                    answerText = Server.HtmlEncode(answer);
                                    break;

                                case EHSIncidentQuestionType.UsersDropdownLocationFiltered:
                                    answer     = EHSIncidentMgr.SelectUserNameById(Convert.ToDecimal(answer));
                                    answerText = Server.HtmlEncode(answer);
                                    break;

                                case EHSIncidentQuestionType.Attachment:
                                    answerText = GetUploadedFiles(40, incidentId, (step + 1).ToString());
                                    break;

                                case EHSIncidentQuestionType.PageOneAttachment:
                                    answerText = GetUploadedFiles(40, incidentId, (step + 1).ToString());
                                    break;

                                default:
                                    if (answer == "Yes")
                                    {
                                        answerText = Resources.LocalizedText.Yes;
                                    }
                                    else if (answer == "No")
                                    {
                                        answerText = Resources.LocalizedText.No;
                                    }
                                    else
                                    {
                                        answerText = answer;
                                    }
                                    break;
                                }
                            }

                            sb.AppendLine(string.Format("<tr><td style=\"width: 33%;\">{0}</td><td>{1}</td></tr>", q.QuestionText, answerText));
                        }
                        catch { status = -1; }
                    }
                }
                sb.AppendLine("</table>");
            }

            litIncidentDetails.Text = sb.ToString();

            return(status);
        }