protected void btnSaveClaimService_Click(object sender, EventArgs e)
        {
            ClaimService claimService = null;
            ClaimComment diary = null;
            AdjusterMaster adjuster = null;
            Claim myClaim = null;
            CarrierInvoiceProfile CarrierInvoice = null;

            int userID = SessionHelper.getUserId();
            int claimID = SessionHelper.getClaimID();
            int id = 0;
            int myAdjusterID = 0;
            int profileID = 0;

            Page.Validate("service");
            if (!Page.IsValid)
                return;

            id = Convert.ToInt32(ViewState["ClaimServiceID"]);

            //Get current claim info to pass through to emails
            ClaimManager cm = new ClaimManager();
            myClaim =  cm.Get(claimID);

            //AdjusterManager

            try {
                using (TransactionScope scope = new TransactionScope())
                {
                    using (ClaimServiceManager repository = new ClaimServiceManager())
                    {
                        if (id == 0)
                        {
                            claimService = new ClaimService();
                            claimService.ClaimID = claimID;
                        }
                        else
                        {
                            claimService = repository.Get(id);
                        }

                        claimService.ServiceQty = this.txtServiceQty.Value == null ? 0 : Convert.ToDecimal(txtServiceQty.Value);
                        claimService.ServiceDate = txtServiceDate.Date;
                        claimService.ServiceDescription = txtServiceDescription.Text.Trim();
                        claimService.ServiceTypeID = Convert.ToInt32(this.ddlInvoiceServiceType.SelectedValue);
                        claimService.UserID = userID;
                        claimService.AdjusterID = Convert.ToInt32(hf_serviceAdjusterID.Value);
                        claimService.Activity = ddlActivity.SelectedItem.Text;
                        claimService.InternalComments = txtMyComments.Text.Trim();
                        claimService.IsBillable = cbIsBillable.Checked;
                        claimService.Billed = false;
                        //save to db
                        claimService = repository.Save(claimService);

                        //string EmailService = ddlInvoiceServiceType.SelectedItem.Text;
                        //string EmailActivity = ddlActivity.SelectedItem.Text;
                        //string EmailDescription = txtServiceDescription.Text;
                        //string EmailInternal = txtMyComments.Text;
                        //string EmailQuantity = txtServiceQty.Text;
                        //string EmailDate = txtServiceDate.Text;

                    }

                    // diary
                    diary = new ClaimComment();
                    diary.ClaimID = claimID;
                    diary.CommentDate = DateTime.Now;
                    diary.UserId = userID;
                    diary.ActivityType = ddlActivity.SelectedItem.Text;
                    diary.InternalComments = txtMyComments.Text.Trim();
                    diary.CommentText = string.Format("Service: {0}, Description: {1}, Date {2:MM/dd/yyyy}, Qty: {3:N2}, Adjuster: {4}",
                                                ddlInvoiceServiceType.SelectedItem.Text,
                                                claimService.ServiceDescription,
                                                claimService.ServiceDate,
                                                claimService.ServiceQty,
                                                txtServiceAdjuster.Text
                                                );
                    ClaimCommentManager.Save(diary);

                    myAdjusterID = Convert.ToInt32(claimService.AdjusterID); //Convert.ToInt32(myClaim.AdjusterID);

                    //EMAIL ADJUSTER OC 10/21/2014
                    if (myAdjusterID != 0 || myAdjusterID != null)
                    {
                        adjuster = AdjusterManager.GetAdjusterId(myAdjusterID);
                    }
                    if (cbEmailAdjuster.Checked == true)
                    {
                        try
                        {
                            notifyAdjuster(adjuster, claimService, myClaim);
                        }
                        catch (Exception ex)
                        {
                            lblMessage.Text = "Unable to send email to adjuster";
                            lblMessage.CssClass = "error";
                        }
                    }
                    //EMAIL CLIENT CONTACT OC 10/22/2014
                    if (cbEmailClient.Checked == true) //Dont need to check if invoice Pro ID is empty becuase to get to this point, one has to exist already
                    {
                        if (Session["ComingFromAllClaims"] != null) //if the user got here straight from the all claims screen
                        {
                            profileID = Convert.ToInt32(Session["CarrierInvoiceID"]);
                        }
                        else//coming from claim detail page
                        {
                            profileID = Convert.ToInt32(Session["InvoiceProfileID"]);
                        }
                        CarrierInvoice = CarrierInvoiceProfileManager.Get(profileID);
                        try
                        {
                            notifyClientContact(CarrierInvoice, claimService, myClaim, adjuster);
                        }
                        catch (Exception ex)
                        {
                            lblMessage.Text = "Unable to send email to client contact";
                            lblMessage.CssClass = "error";
                        }

                    }
                    //EMAIL TO WHOMEVER
                    if(txtEmailTo.Text != "")
                    {

                        try
                        {
                            notifySpecifiedUser(adjuster, claimService, myClaim);
                        }
                        catch (Exception ex)
                        {
                            lblMessage.Text = "Unable to send email to adjuster";
                            lblMessage.CssClass = "error";
                        }
                    }

                    scope.Complete();

                }

                lblMessage.Text = "Service was saved successfully.";
                lblMessage.CssClass = "ok";

                // keep edit form active
                lbtnNewClaimService_Click(null, null);

                // refresh grid
                gvClaimService.DataSource = loadClaimServices(claimID);
                gvClaimService.DataBind();

            }
            catch (Exception ex)
            {
                Core.EmailHelper.emailError(ex);

                lblMessage.Text = "Unable to save claim service.";
                lblMessage.CssClass = "error";
            }
            //send email to adjuster
        }
        private void processClaimServices(int claimID, CarrierInvoiceProfile invoiceProfile, int invoiceID)
        {
            CarrierInvoiceProfileFeeItemized profileTEFee = null;
            List<ClaimService> claimServices = null;
            decimal lineTotal = 0;
            decimal quantity = 0;
            decimal rateAmount = 0;
            string serviceDescription = null;
            string serviceComments = null;
            string activity = null;
            string activityDescription = null;
            bool isBillable = true;
            bool billed = true;
            InvoiceServiceType invoiceServiceType = null;

            // get TE services for claim entered by adjuster
            using (ClaimServiceManager repositiory = new ClaimServiceManager())
            {
                claimServices = repositiory.GetAllForInvoice(claimID);
            }

            if (claimServices != null && claimServices.Count > 0)
            {

                foreach (ClaimService claimService in claimServices)
                {

                    profileTEFee = (from x in invoiceProfile.CarrierInvoiceProfileFeeItemized
                                 where x.ServiceTypeID == claimService.ServiceTypeID
                                 select x
                                 ).FirstOrDefault();
                    int claimServiceID = claimService.ClaimServiceID;
                    quantity = (claimService.ServiceQty ?? 0);
                    serviceDescription = claimService.InvoiceServiceType == null ? string.Empty : claimService.InvoiceServiceType.ServiceDescription;
                    invoiceServiceType = claimService.InvoiceServiceType;
                    activity = claimService.Activity == null ? string.Empty : claimService.Activity;
                    activityDescription = claimService.ServiceDescription;
                    isBillable = claimService.IsBillable.Value;
                    billed = claimService.Billed.Value;
                    if (profileTEFee != null)
                    {
                        // use override from invoice profile
                        if (profileTEFee.ItemRate > 0)
                        {
                            serviceComments = profileTEFee.ItemDescription;
                            rateAmount = profileTEFee.ItemRate;

                            lineTotal = rateAmount * quantity;
                            if (isBillable == true)
                            {
                                insertDetailLine(invoiceID, serviceDescription, activity, activityDescription, lineTotal, quantity, rateAmount, claimService.ServiceDate, serviceComments);
                                using (ClaimServiceManager myRepository = new ClaimServiceManager())
                                {
                                    ClaimService myClaimService = myRepository.Get(claimServiceID);
                                    myClaimService.Billed = true;
                                    myRepository.Save(myClaimService);
                                }

                            }
                        }
                        else if (profileTEFee.ItemPercentage > 0)
                        {
                        }
                    }
                    else
                    {
                        if ((invoiceServiceType.ServiceRate ?? 0) > 0)
                        {
                            rateAmount = invoiceServiceType.ServiceRate ?? 0;
                            quantity = claimService.ServiceQty ?? 0;
                            lineTotal = rateAmount * quantity;
                        }
                        else if ((invoiceServiceType.ServicePercentage ?? 0) > 0)
                        {
                        }
                        if (isBillable == true )
                        {
                            insertDetailLine(invoiceID, serviceDescription, activity, activityDescription, lineTotal, quantity, rateAmount, claimService.ServiceDate, serviceComments);
                            using (ClaimServiceManager myRepository = new ClaimServiceManager())
                            {
                                ClaimService myClaimService = myRepository.Get(claimServiceID);
                                myClaimService.Billed = true;
                                myRepository.Save(myClaimService);
                            }
                        }

                    }
                }
            }
        }
        protected void gvClaimService_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int id = 0;
            ClaimService claimService = null;

            if (e.CommandName == "DoEdit") {
                id = Convert.ToInt32(e.CommandArgument);

                using (ClaimServiceManager repository = new ClaimServiceManager()) {
                    claimService = repository.Get(id);

                    if (claimService != null) {
                        bindServiceTypes();

                        if (claimService.AdjusterMaster != null) {
                            txtServiceAdjuster.Text = claimService.AdjusterMaster.adjusterName;
                            hf_serviceAdjusterID.Value = claimService.AdjusterID.ToString();
                        }

                        txtServiceDate.Value = claimService.ServiceDate;
                        txtServiceDescription.Text = claimService.ServiceDescription;
                        txtServiceQty.Value = claimService.ServiceQty;
                        txtMyComments.Text = claimService.InternalComments;
                        cbIsBillable.Checked = claimService.IsBillable.Value;

                        try
                        {
                            Activity myActivity = ActivityManager.GetByAcctivity(claimService.Activity);
                            ddlActivity.SelectedValue =  myActivity.ActivityID.ToString();
                            RequiredFieldValidator4.Enabled = false;

                        }
                        catch (Exception ex)
                        {
                            ddlActivity.SelectedIndex = 0;
                            RequiredFieldValidator4.Enabled = true;
                            Core.EmailHelper.emailError(ex);
                        }
                        try
                        {
                            ddlInvoiceServiceType.SelectedValue = claimService.ServiceTypeID.ToString();

                        }
                        catch (Exception ex) {
                            ddlInvoiceServiceType.SelectedIndex = -1;
                            Core.EmailHelper.emailError(ex);
                        }

                        ViewState["ClaimServiceID"] = claimService.ClaimServiceID.ToString();

                        showServiceEditPanel();
                    }
                }
            }
            else if (e.CommandName == "DoDelete") {
                id = Convert.ToInt32(e.CommandArgument);
                lblMessage.Text = string.Empty;

                using (ClaimServiceManager repository = new Repository.ClaimServiceManager()) {
                    try {
                        claimService = repository.Get(id);
                        if (claimService != null) {
                            repository.Delete(id);

                            bindData(SessionHelper.getClaimID());
                        }
                    }
                    catch (Exception ex) {
                        lblMessage.Text = "Unable to delete service.";
                        lblMessage.CssClass = "error";

                        Core.EmailHelper.emailError(ex);
                    }

                }
            }
        }