protected void InsertButton1_Click(object sender, EventArgs e)
        {
            var item = new HPPortal.Data.Models.ActionForTargetedGoal();

            if (PlanId != null && PlanId > 0)
            {
                item = _db.ActionForTargetedGoals.Include(p => p.Users).FirstOrDefault(p => p.ActionId == PlanId);
            }

            if (Session["User"] == null)
            {
                Response.Redirect("/Logon.aspx");
            }

            var user = Session["User"] as User;

            if (ModelState.IsValid)
            {
                //CommitToItem(item);
                var plan = _db.ActionForTargetedGoals.Include(p => p.Users).ToList().FirstOrDefault(i => i.ActionId == Convert.ToInt32(ddlTragetedGoals.SelectedValue));
                // Save changes
                if (plan != null)
                {
                    item.CreatedDate = DateTime.Now;
                    if (user != null)
                    {
                        item.CreatedUser = user.UserId;
                    }

                    item.Users           = GetSelectedUsers();
                    item.GoalName        = plan.GoalName;
                    item.PartnerId       = plan.PartnerId;
                    item.PreviousQuarter = plan.PreviousQuarter;
                    item.QuarterPlan     = plan.QuarterPlan;
                    item.QuarterYear     = plan.QuarterYear;
                    item.GoalName        = plan.GoalName;
                    item.ActionRequired  = TextBox1.Text.Trim();
                    _db.ActionForTargetedGoals.Add(item);
                    _db.SaveChanges();
                }
                string path = "TargetedGoals/ActionForTargetedGoals";
                Response.Redirect(string.Format("/{0}?pid={1}&qtr={2}", path, PartnerId, Quater));
            }
        }
        protected void InsertButton_Click(object sender, EventArgs e)
        {
            var item = new HPPortal.Data.Models.ActionForTargetedGoal();

            if (PlanId > 0)
            {
                item = _db.ActionForTargetedGoals.Include(p => p.Users).FirstOrDefault(p => p.ActionId == PlanId);
            }

            var exisitingGoalName = item.GoalName;

            if (Session["User"] == null)
            {
                Response.Redirect("/Logon.aspx");
            }

            var user = Session["User"] as User;

            if (ModelState.IsValid)
            {
                CommitToItem(item);
                var data = _db.ActionForTargetedGoals;
                if (PlanId > 0)
                {
                    item.ModifiedDate = DateTime.Now;
                    if (user != null)
                    {
                        item.ModifiedUser = user.UserId;
                    }

                    _db.Entry(item).State = EntityState.Modified;


                    var goals = data.Where(d => d.GoalName == exisitingGoalName && d.ActionId != item.ActionId);
                    if (goals != null)
                    {
                        foreach (var goal in goals)
                        {
                            goal.GoalName        = item.GoalName;
                            goal.PreviousQuarter = item.PreviousQuarter;
                            goal.QuarterPlan     = item.QuarterPlan;

                            goal.ModifiedDate = DateTime.Now;
                            if (user != null)
                            {
                                goal.ModifiedUser = user.UserId;
                            }

                            _db.Entry(goal).State = EntityState.Modified;
                        }
                    }
                    _db.SaveChanges();
                }
                else
                {
                    if (data != null && data.ToList().Exists(i => i.GoalName == item.GoalName))
                    {
                        UtilityBL.Alert("Goal already exists.", this);
                        gridAction.DataBind();
                        return;
                    }
                    // Save changes
                    item.CreatedDate = DateTime.Now;
                    if (user != null)
                    {
                        item.CreatedUser = user.UserId;
                    }

                    _db.ActionForTargetedGoals.Add(item);
                    _db.SaveChanges();
                }

                // send mail to assigned user
                var assignedUsers = item.Users;
                if (assignedUsers != null)
                {
                    var partner = _db.Partners.Find(item.PartnerId);
                    foreach (var assignedUser in assignedUsers)
                    {
                        var sendEmail = assignedUser.EmailNotification != null ? (bool)assignedUser.EmailNotification : true;
                        if (sendEmail)
                        {
                            string emailAddress = assignedUser.EmailId;
                            string subject      = @"HPJB Portal Targeted goal assigned.";
                            string message      = Utility.MailFormat.GetMessage(@"Targeted goal", assignedUser.Name, partner.PartnerName, item.QuarterYear);

                            var client = new MailService.MailServiceSoapClient();
                            client.SendMailMessagesAsync(ConfigurationManager.AppSettings["From"], emailAddress,
                                                         "", "", subject, message, "", "");

                            Utility.MailFormat.SendSMS(assignedUser.Mobile, assignedUser.Name, partner.PartnerName);
                        }
                    }
                }

                string path = "TargetedGoals/ActionForTargetedGoals";
                Response.Redirect(string.Format("/{0}?pid={1}&qtr={2}", path, PartnerId, Quater));
            }
        }