protected void IncidentsGrid_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
        {
            if (e.Parameters.StartsWith("Closeincident"))
            {
                var splitted = e.Parameters.Split(';');

                DataClassesDataContext db = new DataClassesDataContext(Handler_Global.connectString);
                var incidentID            = IncidentsGrid.GetRowValues(Convert.ToInt32(splitted[1]), "IncidentID");
                if (incidentID != null)
                {
                    var incident = db.Incidents.FirstOrDefault(em => em.IncidentID == Convert.ToInt64(incidentID));
                    //Session["CommentsIncidentID"] = incident.IncidentID;
                    //if incident already closed, just ignore this stupid user
                    if (incident.IncidentStateID == Core.Handler_Incidents.Incident_State_Closed)
                    {
                        return;
                    }
                    var usersession = Session["User"];
                    if (usersession == null)
                    {
                        return;
                    }
                    var user = (User)usersession;
                    Core.Handler_Incidents.Close_Incident(user, incident);
                    IncidentsGrid.DataBind();
                }
            }
        }
        protected void Incidents_AddIncident_SubmitButton_Click(object sender, EventArgs e)
        {
            DataClassesDataContext db = new DataClassesDataContext(Handler_Global.connectString);
            var incidentTypeID        = Convert.ToInt16(Incidents_AddIncident_Type_ComboBox.Value);
            var incidentSourceID      = Convert.ToInt16(Incidents_AddIncident_Source_ComboBox.Value);
            var source = db.IncidentSources.FirstOrDefault <IncidentSource>(a => a.IncidentSourceID == Convert.ToInt16(incidentSourceID));

            if (source == null)
            {
                return;
            }
            var user = (User)Session["User"];

            if (user == null)
            {
                return;
            }
            var newIncident = new Incident();

            newIncident.IncidentTypeID   = incidentTypeID;
            newIncident.IncidentStateID  = Core.Handler_Incidents.Incident_State_New;
            newIncident.IncidentSourceID = source.IncidentSourceID;
            newIncident.Place            = Incidents_AddIncident_Place_TextBox.Text;
            if (source.RequiresExtraInfo1 == 1)
            {
                newIncident.IncidentSourceExtraInfo1 = Incidents_AddIncident_Extrainfo1_TextBox.Text;
            }
            if (source.RequiresExtraInfo2 == 1)
            {
                newIncident.IncidentSourceExtraInfo2 = Incidents_AddIncident_Extrainfo2_TextBox.Text;
            }
            if (source.RequiresExtraInfo3 == 1)
            {
                newIncident.IncidentSourceExtraInfo3 = Incidents_AddIncident_Extrainfo3_TextBox.Text;
            }
            var result = Core.Handler_Incidents.Add_Incident(user, newIncident);

            if (result.StatusID == Core.Handler_Operations.Opeartion_Status_Success)
            {
                IncidentsGrid.DataBind();
                Incidents_Add_Popup.ShowOnPageLoad = false;
                return;
            }
            Incidents_Add_Popup.HeaderText          = "اضافة بلاغ جديد";
            Incidents_AddIncident_status_label.Text = result.Text;
            Incidents_Add_Popup.ShowOnPageLoad      = true;
        }
        protected void CommentsCreateNew_Click(object sender, EventArgs e)
        {
            // Response.Write("hahahahhahahahahahahah");
            PopulateCommentPopUp();
            Incident_Comments_PopUp.ShowOnPageLoad = true;
            Incident_Comments_PopUp.HeaderText     = "التعليقات";
            // var Comments_CommentsCreateNew_TextBox = (ASPxTextBox)(CommentsGrid.FindTitleTemplateControl("CommentsCreateNew_TextBox"));
            CommentsCreateNew_TextBox.Focus();
            if (CommentsCreateNew_TextBox == null)
            {
                return;
            }
            if (CommentsCreateNew_TextBox.Text == "")
            {
                return;
            }
            var IncidentID = Session["CommentsIncidentID"];

            if (IncidentID == null)
            {
                return;
            }
            var sessionuser = Session["User"];

            if (sessionuser == null)
            {
                return;
            }
            var user = (User)sessionuser;
            DataClassesDataContext db = new DataClassesDataContext(Handler_Global.connectString);

            var newComment = new IncidentsComment();

            newComment.IncidentID = Convert.ToInt64(IncidentID.ToString());
            newComment.Text       = CommentsCreateNew_TextBox.Text;

            var result = Core.Handler_Incidents.Add_New_Comment(user, newComment);

            if (result.StatusID == Core.Handler_Operations.Opeartion_Status_Success)
            {
                CommentsGrid.DataBind();
                IncidentsGrid.DataBind();
                CommentsCreateNew_TextBox.Text = "";
            }
        }
        protected void AttahcIncidentSubmitButton_Click(object sender, EventArgs e)
        {
            if (AttachIncident_GridLookUp.Value == null)
            {
                return;
            }
            if (Request.Form["AttachIncidentMappingID"] != null)
            {
                if (Request.Form["AttachIncidentMappingID"].ToString().Equals(DBNull.Value) ||
                    Request.Form["AttachIncidentMappingID"].ToString() == "")
                {
                    return;
                }
                DataClassesDataContext db = new DataClassesDataContext(Handler_Global.connectString);

                var mappingID = Convert.ToInt16(Request.Form["AttachIncidentMappingID"]);
                // var mappingID = OpsLiveGrid.GetRowValues(rowIndex, "AhwalMappingID");
                var personmapping = db.AhwalMappings.FirstOrDefault <AhwalMapping>(em => em.AhwalMappingID == Convert.ToInt64(mappingID));

                if (personmapping != null)
                {
                    //we have to check the current state of the person, if he is not in one of the allowed statees, we cannot handover the incident to him

                    var selectedIncidentID = AttachIncident_GridLookUp.Value;
                    if (selectedIncidentID != null)
                    {
                        var incidentObj = db.Incidents.FirstOrDefault <Incident>(a => a.IncidentID == Convert.ToInt64(selectedIncidentID));
                        if (incidentObj == null)
                        {
                            return;
                        }
                        var usersession = (User)Session["User"];
                        if (usersession == null)
                        {
                            return;
                        }
                        var user = (User)usersession;
                        Core.Handler_Incidents.HandOver_Incident_To_Person(user, personmapping, incidentObj);
                        OpsLiveGrid.DataBind();
                        IncidentsGrid.DataBind();
                        OpsLive_IncidentAttach_PopUp.ShowOnPageLoad = false;
                    }
                }
            }
        }
        protected void AttachedIncident_Release_Button_Click(object sender, EventArgs e)
        {
            if (Request.Form["AttachIncidentMappingID"] != null)
            {
                if (Request.Form["AttachIncidentMappingID"].ToString().Equals(DBNull.Value) ||
                    Request.Form["AttachIncidentMappingID"].ToString() == "")
                {
                    return;
                }
                DataClassesDataContext db = new DataClassesDataContext(Handler_Global.connectString);

                var mappingID = Convert.ToInt16(Request.Form["AttachIncidentMappingID"]);
                // var mappingID = OpsLiveGrid.GetRowValues(rowIndex, "AhwalMappingID");
                var personmapping = db.AhwalMappings.FirstOrDefault <AhwalMapping>(em => em.AhwalMappingID == Convert.ToInt64(mappingID));

                if (personmapping != null)
                {
                    if (personmapping.IncidentID != null && !personmapping.IncidentID.Equals(DBNull.Value))
                    {
                        var incidentObj = db.Incidents.FirstOrDefault <Incident>(a => a.IncidentID == personmapping.IncidentID);
                        if (incidentObj == null)
                        {
                            return;
                        }
                        var usersession = (User)Session["User"];
                        if (usersession == null)
                        {
                            return;
                        }
                        var user = (User)usersession;
                        Core.Handler_Incidents.UnHandOver_Incident_To_Person(user, personmapping, incidentObj);
                        OpsLiveGrid.DataBind();
                        IncidentsGrid.DataBind();
                    }
                }
            }
            OpsLive_States_PopUp.ShowOnPageLoad = false;
        }
 protected void IncidentsRefresh_Click(object sender, EventArgs e)
 {
     IncidentsGrid.DataBind();
 }