/// <summary>
        /// UpdateForDashboard
        /// </summary>        
        /// <param name="companyId">companyId</param>
        private void UpdateForDashboard(int companyId)
        {
            foreach (DashboardTDS.DashboardSupportTicketAssignedToMeRow row in (DashboardTDS.DashboardSupportTicketAssignedToMeDataTable)Table)
            {
                int supportTicketId = row.SupportTicketID;

                SupportTicketInformationActivityInformation supportTicketInformationActivityInformation = new SupportTicketInformationActivityInformation();
                supportTicketInformationActivityInformation.LoadAllBySupportTicketId(supportTicketId, companyId);

                int lastRefId = supportTicketInformationActivityInformation.GetLastAssignedUserRefId();

                if (row.RefID != lastRefId)
                {
                    row.Delete();
                }
            }
        }
        private void UpdateDatabase()
        {
            // Get ids
            int companyId = Int32.Parse(hdfCompanyId.Value);

            DB.Open();
            DB.BeginTransaction();
            try
            {
                // ... Update general todo list state
                SupportTicketInformationBasicInformation supportTicketInformationBasicInformation = new SupportTicketInformationBasicInformation(supportTicketInformationTDS);
                supportTicketInformationBasicInformation.Save(companyId);

                // ... Save to do list details
                SupportTicketInformationActivityInformation supportTicketInformationActivityInformation = new SupportTicketInformationActivityInformation(supportTicketInformationTDS);
                supportTicketInformationActivityInformation.Save(companyId);

                // ... Send mails
                int createdBy = Int32.Parse(hdfCreatedById.Value);
                string mailTo = "";
                string nameTo = "";

                // ... ... MailtTo, nameTo
                EmployeeGateway employeeGateway = new EmployeeGateway();
                employeeGateway.LoadByEmployeeId(createdBy);

                mailTo = employeeGateway.GetEMail(createdBy);
                nameTo = employeeGateway.GetFullName(createdBy);

                SupportTicketInformationActivityInformation supportTicketInformationActivityInformationForMails = new SupportTicketInformationActivityInformation(supportTicketInformationTDS);

                foreach (GridViewRow row in grdSupportTicket.Rows)
                {
                    bool sendMail = bool.Parse(((Label)row.FindControl("lblSendMail")).Text);

                    if (sendMail)
                    {
                        string subject = "";
                        string body = "";
                        string comment = "";

                        string type_ = ((Label)row.FindControl("lblType")).Text;
                        switch (type_)
                        {
                            case "AssignUser":
                                // Get mail information
                                subject = "A usser was assigned to the following ticket.";

                                // Mails body
                                body = body + "\nHi " + nameTo + ",\n\nA usser was assigned to the following ticket. \n";
                                body = body + "\n Subject: " + lblTitleSubjectName.Text;
                                body = body + "\n Created By: " + tbxCreatedBy.Text;
                                body = body + "\n Creation Date: " + tbxCreationDate.Text;
                                body = body + "\n Category: " + tbxCategoryName.Text;
                                string assignedUser = ((TextBox)row.FindControl("tbxUser")).Text;
                                body = body + "\n Assigned User: "******"\n Due Date: " + tbxDueDate.Text;
                                comment = ((TextBox)row.FindControl("tbxComments")).Text;
                                body = body + "\n New comment: " + comment;

                                //Send Mail
                                SendMail(mailTo, subject, body);
                                break;

                            case "AssignToOwner":
                                // Get mail information
                                subject = "The following ticket was assigned to it's owner.";

                                // Mails body
                                body = body + "\nHi " + nameTo + ",\n\nThe following ticket was assigned to it's owner. \n";
                                body = body + "\n Subject: " + lblTitleSubjectName.Text;
                                body = body + "\n Owner: " + tbxCreatedBy.Text;
                                body = body + "\n Creation Date: " + tbxCreationDate.Text;
                                body = body + "\n Category: " + tbxCategoryName.Text;
                                string assignedOwer = ((TextBox)row.FindControl("tbxUser")).Text;
                                body = body + "\n Assigned To Owner: " + assignedOwer;
                                body = body + "\n Due Date: " + tbxDueDate.Text;
                                comment = ((TextBox)row.FindControl("tbxComments")).Text;
                                body = body + "\n New comment: " + comment;

                                //Send Mail
                                SendMail(mailTo, subject, body);
                                break;

                            case "AddComment":
                                // Get mail information
                                subject = "A comment was added to the following ticket.";

                                // Mails body
                                body = body + "\nHi " + nameTo + ",\n\nA comment was added to the following ticket. \n";
                                body = body + "\n Subject: " + lblTitleSubjectName.Text;
                                body = body + "\n Created By: " + tbxCreatedBy.Text;
                                body = body + "\n Creation Date: " + tbxCreationDate.Text;
                                body = body + "\n Category: " + tbxCategoryName.Text;
                                body = body + "\n Assigned User: "******"\n Due Date: " + tbxDueDate.Text;
                                string commentAddedBy = ((TextBox)row.FindControl("tbxUser")).Text;
                                body = body + "\n Comment added by: " + commentAddedBy;
                                comment = ((TextBox)row.FindControl("tbxComments")).Text;
                                body = body + "\n New comment: " + comment;

                                //Send Mail
                                SendMail(mailTo, subject, body);
                                break;

                            case "CloseTicket":
                                // Get mail information
                                subject = "A support ticket was completed.";

                                // Mails body
                                body = body + "\nHi " + nameTo + ",\n\nThe following support ticket was completed. \n";
                                body = body + "\n Subject: " + lblTitleSubjectName.Text;
                                body = body + "\n Created By: " + tbxCreatedBy.Text;
                                body = body + "\n Creation Date: " + tbxCreationDate.Text;
                                body = body + "\n Category: " + tbxCategoryName.Text;
                                body = body + "\n Assigned User: "******"\n Due Date: " + tbxDueDate.Text;
                                string completedBy = ((TextBox)row.FindControl("tbxUser")).Text;
                                body = body + "\n Completed by: " + completedBy;
                                comment = ((TextBox)row.FindControl("tbxComments")).Text;
                                body = body + "\n New comment: " + comment;

                                //Send Mail
                                SendMail(mailTo, subject, body);
                                break;

                            case "OnHold":
                                // Get mail information
                                subject = "The following ticket was put on hold.";

                                // Mails body
                                body = body + "\nHi " + nameTo + ",\n\nThe following ticket was put on hold. \n";
                                body = body + "\n Subject: " + lblTitleSubjectName.Text;
                                body = body + "\n Created By: " + tbxCreatedBy.Text;
                                body = body + "\n Creation Date: " + tbxCreationDate.Text;
                                body = body + "\n Category: " + tbxCategoryName.Text;
                                body = body + "\n Assigned User: "******"\n Due Date: " + tbxDueDate.Text;
                                string onHoldBy = ((TextBox)row.FindControl("tbxUser")).Text;
                                body = body + "\n On hold by: " + onHoldBy;
                                comment = ((TextBox)row.FindControl("tbxComments")).Text;
                                body = body + "\n New comment: " + comment;

                                //Send Mail
                                SendMail(mailTo, subject, body);
                                break;
                        }
                    }
                }

                DB.CommitTransaction();

                // Store datasets
                supportTicketInformationTDS.AcceptChanges();
                Session["supportTicketInformationTDS"] = supportTicketInformationTDS;
            }
            catch (Exception ex)
            {
                DB.RollbackTransaction();

                string url = string.Format("./../../error_page.aspx?error={0}", ex.Message.Replace('\n', ' '));
                Response.Redirect(url);
            }
        }
        private void LoadActivityData(int supportTicketId, int companyId)
        {
            SupportTicketInformationActivityInformation supportTicketInformationActivityInformation = new SupportTicketInformationActivityInformation();
            supportTicketInformationActivityInformation.LoadAllBySupportTicketId(supportTicketId, companyId);
            int lastRefId = supportTicketInformationActivityInformation.GetLastAssignedUserRefId();

            SupportTicketInformationActivityInformationGateway supportTicketInformationActivityInformationGateway = new SupportTicketInformationActivityInformationGateway(supportTicketInformationActivityInformation.Data);
            supportTicketInformationActivityInformationGateway.LoadAllBySupportTicketId(supportTicketId, companyId);

            if (supportTicketInformationActivityInformationGateway.Table.Rows.Count > 0)
            {
                // For last assigned user
                tbxAssignedUser.Text = supportTicketInformationActivityInformationGateway.GetEmployeeFullName(supportTicketId, lastRefId);
                hdfAssignedUser.Value = supportTicketInformationActivityInformationGateway.GetEmployeeID(supportTicketId, lastRefId).ToString();
            }
        }
        private void GrdToDoAdd()
        {
            if (ValidateToDoFooter())
            {
                Page.Validate("activityDataNew");

                if (Page.IsValid)
                {
                    int employeeId = 0;
                    EmployeeGateway employeeGateway = new EmployeeGateway();
                    string dateTime_ = ((TextBox)grdSupportTicket.FooterRow.FindControl("tbxDateTimeNew")).Text;
                    string comment = ((TextBox)grdSupportTicket.FooterRow.FindControl("tbxCommentsNew")).Text;
                    string type_ = "";
                    string employeeFullName = "";

                    GridViewRow row = grdSupportTicket.FooterRow;
                    DropDownList action = ((DropDownList)row.FindControl("ddlActionsNew"));

                    switch (action.SelectedValue)
                    {
                        case "Assign To User":
                            employeeId = Int32.Parse(((DropDownList)grdSupportTicket.FooterRow.FindControl("ddlAssignToTeamMemberNew")).SelectedValue);
                            employeeGateway.LoadByEmployeeId(employeeId);
                            employeeFullName = employeeGateway.GetFullName(employeeId);
                            type_ = "AssignUser";
                            hdfSupportTicketState.Value = "In Progress";

                            break;

                        case "Assign To Owner":
                            employeeId = Int32.Parse(((DropDownList)grdSupportTicket.FooterRow.FindControl("ddlAssignToTeamMemberNew")).SelectedValue);
                            employeeGateway.LoadByEmployeeId(employeeId);
                            employeeFullName = employeeGateway.GetFullName(employeeId);
                            type_ = "AssignUser";
                            hdfSupportTicketState.Value = "In Progress";
                            break;

                        case "Add Comment":
                            int loginId = Convert.ToInt32(Session["loginID"]);
                            employeeId = employeeGateway.GetEmployeIdByLoginId(loginId);
                            type_ = "AddComment";
                            employeeFullName = employeeGateway.GetFullName(employeeId);
                            hdfSupportTicketState.Value = "In Progress";
                            break;

                        case "Complete":
                            employeeId = Int32.Parse(hdfAssignedUser.Value);
                            employeeGateway.LoadByEmployeeId(employeeId);
                            employeeFullName = employeeGateway.GetFullName(employeeId);
                            type_ = "CloseTicket";
                            hdfCompleted.Value = "True";
                            hdfSupportTicketState.Value = "Completed";
                            break;

                        case "Put On Hold":
                            int loginIdOnHold = Convert.ToInt32(Session["loginID"]);
                            employeeId = employeeGateway.GetEmployeIdByLoginId(loginIdOnHold);
                            type_ = "OnHold";
                            employeeFullName = employeeGateway.GetFullName(employeeId);
                            hdfSupportTicketState.Value = "On Hold";
                            break;
                    }

                    // static values
                    int supportTicketId = Int32.Parse(hdfSupportTicketId.Value);
                    int companyId = Int32.Parse(hdfCompanyId.Value);
                    bool inDatabase = false;
                    bool sendMail = true;

                    SupportTicketInformationBasicInformation generalModel = new SupportTicketInformationBasicInformation(supportTicketInformationTDS);
                    generalModel.UpdateState(supportTicketId, hdfSupportTicketState.Value);

                    SupportTicketInformationActivityInformation model = new SupportTicketInformationActivityInformation(supportTicketInformationTDS);
                    model.Insert(supportTicketId, employeeId, type_, DateTime.Parse(dateTime_), comment, false, companyId, inDatabase, employeeFullName, sendMail);

                    Session.Remove("activityInformationDummyForST");
                    Session["supportTicketInformationTDS"] = supportTicketInformationTDS;

                    grdSupportTicket.DataBind();
                }
            }
        }
        // ////////////////////////////////////////////////////////////////////////
        // EVENTS
        //
        protected void Page_Load(object sender, EventArgs e)
        {
            // Register client scripts
            this.RegisterClientScripts();

            if (!IsPostBack)
            {
                // Security check
                if (!(Convert.ToBoolean(Session["sgLFS_ITTST_SUPPORTTICKET_ADMIN"])))
                {
                    if (!(Convert.ToBoolean(Session["sgLFS_ITTST_SUPPORTTICKET_VIEW"]) && Convert.ToBoolean(Session["sgLFS_ITTST_SUPPORTTICKET_EDIT"])))
                    {
                        Response.Redirect("./../../error_page.aspx?error=" + "You are not authorized to view this page. Contact your system administrator.");
                    }
                }

                // Validate query string
                if (((string)Request.QueryString["source_page"] == null) || ((string)Request.QueryString["supportTicket_id"] == null) || ((string)Request.QueryString["action"] == null))
                {
                    Response.Redirect("./../../error_page.aspx?error=" + "Invalid query string in supportTicket_activity.aspx");
                }

                // Tag Page
                hdfCompanyId.Value = Session["companyID"].ToString();
                hdfSupportTicketId.Value = Request.QueryString["supportTicket_id"].ToString();
                hdfAction.Value = Request.QueryString["action"].ToString();
                hdfDashboard.Value = Request.QueryString["dashboard"].ToString();

                // If coming from
                int companyId = Int32.Parse(hdfCompanyId.Value.Trim());
                int currentSupportTicketId = Int32.Parse(hdfSupportTicketId.Value.ToString());

                Session.Remove("activityInformationDummyForST");

                // ... supportTicket_navigator2.aspx
                if (Request.QueryString["source_page"] == "supportTicket_navigator2.aspx")
                {
                    StoreNavigatorState();
                    supportTicketInformationTDS = new SupportTicketInformationTDS();
                    activityInformation = new SupportTicketInformationTDS.ActivityInformationDataTable();

                    SupportTicketInformationBasicInformation supportTicketInformationBasicInformation = new SupportTicketInformationBasicInformation(supportTicketInformationTDS);
                    supportTicketInformationBasicInformation.LoadAllBySupportTicketId(currentSupportTicketId, companyId);

                    SupportTicketInformationBasicInformationGateway supportTicketInformationBasicInformationGatewayForState = new SupportTicketInformationBasicInformationGateway(supportTicketInformationBasicInformation.Data);
                    string state = supportTicketInformationBasicInformationGatewayForState.GetState(currentSupportTicketId);
                    if (state == "Completed") hdfCompleted.Value = "True";

                    SupportTicketInformationActivityInformation supportTicketInformationActivityInformation = new SupportTicketInformationActivityInformation(supportTicketInformationTDS);
                    supportTicketInformationActivityInformation.LoadAllBySupportTicketId(currentSupportTicketId, companyId);

                    // Store dataset
                    Session["supportTicketInformationTDS"] = supportTicketInformationTDS;
                    Session["activityInformationForST"] = activityInformation;
                }

                // ... supportTicket_summary.aspx or supportTicket_edit.aspx
                if ((Request.QueryString["source_page"] == "supportTicket_summary.aspx") || (Request.QueryString["source_page"] == "supportTicket_edit.aspx"))
                {
                    StoreNavigatorState();
                    ViewState["update"] = Request.QueryString["update"];

                    // Restore dataset
                    supportTicketInformationTDS = (SupportTicketInformationTDS)Session["supportTicketInformationTDS"];
                    activityInformation = (SupportTicketInformationTDS.ActivityInformationDataTable)Session["activityInformationForST"];

                    if (ViewState["update"].ToString().Trim() == "yes")
                    {
                        SupportTicketInformationBasicInformation supportTicketInformationBasicInformation = new SupportTicketInformationBasicInformation(supportTicketInformationTDS);
                        supportTicketInformationBasicInformation.LoadAllBySupportTicketId(currentSupportTicketId, companyId);

                        SupportTicketInformationBasicInformationGateway supportTicketInformationBasicInformationGatewayForState = new SupportTicketInformationBasicInformationGateway(supportTicketInformationBasicInformation.Data);
                        string state = supportTicketInformationBasicInformationGatewayForState.GetState(currentSupportTicketId);
                        if (state == "Completed") hdfCompleted.Value = "True";

                        SupportTicketInformationActivityInformation supportTicketInformationActivityInformation = new SupportTicketInformationActivityInformation(supportTicketInformationTDS);
                        supportTicketInformationActivityInformation.LoadAllBySupportTicketId(currentSupportTicketId, companyId);

                        // Store dataset
                        Session["supportTicketInformationTDS"] = supportTicketInformationTDS;
                        Session["activityInformationForST"] = activityInformation;
                    }
                }

                // Prepare initial data
                // ... for subject
                SupportTicketInformationBasicInformationGateway supportTicketInformationBasicInformationGateway = new SupportTicketInformationBasicInformationGateway();
                supportTicketInformationBasicInformationGateway.LoadAllBySupportTicketId(currentSupportTicketId, companyId);
                lblTitleSubjectName.Text = " " + supportTicketInformationBasicInformationGateway.GetSubject(currentSupportTicketId);

                // ... Data for current to do list
                LoadSupportTicketData(currentSupportTicketId, companyId);
            }
            else
            {
                // Restore datasets
                supportTicketInformationTDS = (SupportTicketInformationTDS)Session["supportTicketInformationTDS"];
                activityInformation = (SupportTicketInformationTDS.ActivityInformationDataTable)Session["activityInformationForST"];
            }
        }
        protected void grdSupportTicket_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            Page.Validate("activityDataEdit");

            if (Page.IsValid)
            {
                bool sendMail = true;

                int supportTicketId = (int)e.Keys["SupportTicketID"];
                int refId = (int)e.Keys["RefID"];

                int employeeId = 0;
                int loginId = Convert.ToInt32(Session["loginID"]);

                EmployeeGateway employeeGateway = new EmployeeGateway();
                string comment = ((TextBox)grdSupportTicket.Rows[e.RowIndex].Cells[4].FindControl("tbxCommentsEdit")).Text;
                string employeeFullName = "";

                string type_ = ((Label)grdSupportTicket.Rows[e.RowIndex].Cells[4].FindControl("lblType")).Text;
                switch (type_)
                {
                    case "AssignUser":
                        employeeId = Int32.Parse(((DropDownList)grdSupportTicket.Rows[e.RowIndex].Cells[4].FindControl("ddlAssignToTeamMemberEdit")).SelectedValue);
                        employeeGateway.LoadByEmployeeId(employeeId);
                        employeeFullName = employeeGateway.GetFullName(employeeId);
                        break;

                    case "AddComment":
                        employeeId = employeeGateway.GetEmployeIdByLoginId(loginId);
                        EmployeeGateway employeeGatewayForComment = new EmployeeGateway();
                        employeeGatewayForComment.LoadByEmployeeId(employeeId);
                        employeeFullName = employeeGatewayForComment.GetFullName(employeeId);
                        break;

                    case "CloseTicket":
                        employeeId = Int32.Parse(hdfAssignedUser.Value);
                        employeeGateway.LoadByEmployeeId(employeeId);
                        employeeFullName = employeeGateway.GetFullName(employeeId);
                        hdfCompleted.Value = "True";
                        break;

                    case "OnHold":
                        employeeId = employeeGateway.GetEmployeIdByLoginId(loginId);
                        EmployeeGateway employeeGatewayForOnHold = new EmployeeGateway();
                        employeeGatewayForOnHold.LoadByEmployeeId(employeeId);
                        employeeFullName = employeeGatewayForOnHold.GetFullName(employeeId);
                        break;
                }

                SupportTicketInformationActivityInformation model = new SupportTicketInformationActivityInformation(supportTicketInformationTDS);
                model.Update(supportTicketId, refId, employeeId, employeeFullName, comment, sendMail);

                // Store dataset
                Session["supportTicketInformationTDS"] = supportTicketInformationTDS;
            }
            else
            {
                e.Cancel = true;
            }
        }
        protected void grdSupportTicket_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            // Repair Gridview, if the gridview is edition mode
            if (grdSupportTicket.EditIndex >= 0)
            {
                grdSupportTicket.UpdateRow(grdSupportTicket.EditIndex, true);
            }

            // Delete repairs
            int supportTicketId = (int)e.Keys["SupportTicketID"];
            int refId = (int)e.Keys["RefID"];
            bool sendMail = false;

            SupportTicketInformationActivityInformation model = new SupportTicketInformationActivityInformation(supportTicketInformationTDS);

            // Delete repair
            model.Delete(supportTicketId, refId, sendMail);

            // Store dataset
            Session["supportTicketInformationTDS"] = supportTicketInformationTDS;
        }