protected void btnAddNewLoan_Click(object sender, EventArgs e)
        {
            try
            {
                LoansBLL bll = new LoansBLL();
                if (txtBoxAddMaturityDate.SelectedDate.ToString() != "" && txtBoxAddSigningDate.SelectedDate.ToString() != "")
                {
                    DateTime dtMaturity = Convert.ToDateTime(txtBoxAddMaturityDate.SelectedDate.Value);
                    DateTime dtSigning = Convert.ToDateTime(txtBoxAddSigningDate.SelectedDate.Value);
                    SettingsBLL settingBL = new SettingsBLL();
                    Setting setting = settingBL.GetSettingyear("Loan Year Settings");
                    if (setting != null)
                    {
                        TimeSpan ts = dtMaturity - dtSigning;
                        int differenceYear = Convert.ToInt32((ts.TotalDays) / 365);
                        int year = Convert.ToInt16(setting.Value);

                        if (dtMaturity.Year > dtSigning.Year && (differenceYear) > year)
                        {
                            lblMessage.Text = "Maturity Date is not Valid";
                            lblMessage.Visible = true;
                            return;
                        }
                    }
                }
                else
                {
                    //lblMessage.Text = "Enter the Details";
                    //lblMessage.Visible = true;
                    RadWindowManager1.RadAlert("Maturity Date and Signing Date required", 300, 150, "realedge associates", "alertCallBackFn");
                    return;
                }

                // check to see either the loan has been added or updated
                bool isNewLoan = false;
                Loans loan = new Loans();
                if (!string.IsNullOrEmpty(hfLoanID.Value))
                {
                    isNewLoan = false;
                    loan.ID = Convert.ToInt32(hfLoanID.Value);
                    LogActivity("Update the Loan", "Update the existing loan", string.Empty);
                }
                else
                {
                    isNewLoan = true;
                    LogActivity("New Loan Added", "Add the new loan", string.Empty);
                }

                List<RatingDetails> ratingDetails = new List<RatingDetails>();

                ratingDetails = ShowCheckedNodes(tvCreditRating);

                foreach (var item in ratingDetails)
                {
                    switch (item.agencyName)
                    {
                        case "Moody's":
                            loan.CreditRatingModys = item.rating;
                            break;
                        case "S&P's":
                            loan.CreditRatingSPs = item.rating;
                            break;
                        case "Fitch":
                            loan.CreditRatingFitch = item.rating;
                            break;
                        case "ING":
                            loan.CreditRatingING = item.rating;
                            break;
                        default:
                            break;
                    }
                }

                loan.CodeName = ddlAddLoanCode.Text;
                loan.Borrower = ddlBorrower.Text;
                loan.Sector = ddlSector.SelectedValue; //txtBoxAddSector.Text;
                loan.Signing_Date = txtBoxAddSigningDate.SelectedDate.Value.ToShortDateString();
                loan.Maturity_Date = txtBoxAddMaturityDate.SelectedDate.Value.ToShortDateString();
                loan.FixedOrFloating = ddlAddFixedOrFloating.SelectedValue;

                loan.Margin = txtBoxAddMargin.Text;
                loan.Currency = ddlAddCurrency.SelectedValue;
                loan.Country = ddlCountry.SelectedValue;
                loan.CouponFrequency = ddlAddCouponFrequency.SelectedValue;
                loan.Gurantor = txtGurantor.Text;
                loan.Grid = txtGrid.Text;
                loan.SummitCreditEntity = txtSummitCredit.Text;
                if (txtBoxFacilitySize.Text != string.Empty)
                {
                    loan.FacilitySize = Convert.ToDecimal(txtBoxFacilitySize.Text).ToString("N");
                }
                loan.Bilateral = ddlAddBilateral.SelectedValue == "Yes";
                loan.Amortizing = ddlAmortizing.SelectedValue;
                loan.PP = txtPP.Text;
                //    loan.CreditRating = txtCRating.Text;
                loan.StructureID = txtStructureID.Text;
                //Coupon Date and Notional are missing.
                if (txtCouponDate.SelectedDate != null)
                {
                    loan.CouponDate = txtCouponDate.SelectedDate.Value.ToShortDateString();
                }
                loan.Notional = Convert.ToDecimal(txtNotional.Text).ToString("N").Trim();
                if (ddlAmortizing.SelectedValue == "Yes")
                {
                    if (txtAmortisationsStartDate.SelectedDate != null)
                    {
                        loan.AmortisationsStartPoint = txtAmortisationsStartDate.SelectedDate.Value.ToShortDateString();
                    }
                    if (txtAmortisations.Text != string.Empty)
                    {
                        loan.NoOfAmortisationPoint = Convert.ToInt16(txtAmortisations.Text);
                    }

                }
                else
                {
                    loan.Amortizing = "Yes";
                    DateTime cpnDT;
                    if (txtCouponDate.SelectedDate.ToString() == string.Empty)
                    {
                        cpnDT = AddBusinessDays(DateTime.Now, 10);
                        loan.AmortisationsStartPoint = cpnDT.ToShortDateString();
                    }
                    else
                    {
                        cpnDT = Convert.ToDateTime(txtCouponDate.SelectedDate);
                        loan.AmortisationsStartPoint = txtCouponDate.SelectedDate.Value.ToShortDateString();
                    }

                    loan.NoOfAmortisationPoint = 10;
                    LoanScheduleBL loanScheduleBL = new LoanScheduleBL();
                    DateTime tradeDate = DateTime.Now;

                    DataTable dtSchedule = loanScheduleBL.GenerateTable(15, Convert.ToInt16(loan.NoOfAmortisationPoint), Convert.ToDateTime(loan.AmortisationsStartPoint), loan.CouponFrequency.ToString(), loan.Notional.ToString(), Convert.ToDateTime(loan.Maturity_Date), Convert.ToDateTime(loan.CouponDate), Convert.ToDecimal(loan.Margin), Convert.ToString(loan.Currency), Convert.ToDateTime(tradeDate), AddBusinessDays(Convert.ToDateTime(tradeDate), 10));
                    Session["LoanSchedule"] = dtSchedule;
                }
                DAL.Login login = Session["LogedInUser"] as DAL.Login;
                loan.CreatedBy = login.Name;
                loan.LastEdited = DateTime.Now;

                switch (_operation)
                {
                    case "Add":
                        if (bll.CheckForLoanCode(ddlAddLoanCode.Text.Trim()))
                        {
                            bll.SaveLoan(loan);
                            login = Session["LogedInUser"] as DAL.Login;
                            LoanHistory loanHistory = new LoanHistory();
                            LoanHistoryBL historyBL = new LoanHistoryBL();
                            loanHistory.Action = "Add";
                            loanHistory.LoanName = loan.CodeName;
                            loanHistory.UserName = login.Name;
                            loanHistory.LastModified = DateTime.Now;
                            historyBL.SaveHistory(loanHistory);
                            // lblAddLoanMessage.Visible = true;          on 7-1
                            CalculateFactors();
                            LoanScheduleBL loanScheduleBL = new LoanScheduleBL();
                            if (Session["LoanSchedule"] != null)
                            {
                                dt1 = (DataTable)Session["LoanSchedule"];
                            }

                            foreach (DataRow dr in dt1.Rows)
                            {
                                LoanSchedule loanSchedule = new LoanSchedule();
                                loanSchedule.LoanID = loan.ID;
                                loanSchedule.StartDate = Convert.ToDateTime(dr["StartDate"]);
                                loanSchedule.EndDate = Convert.ToDateTime(dr["EndDate"]);
                                loanSchedule.CoupFrac = Convert.ToDecimal(dr["CoupFrac"]);
                                loanSchedule.Notation = Convert.ToDecimal(dr["Notation"]);
                                loanSchedule.Amortisation = Convert.ToDecimal(dr["Amortisation"]);
                                loanSchedule.Factor = Convert.ToDecimal(dr["Factor"]);
                                loanSchedule.Spread = Convert.ToDecimal(dr["Spread"]);
                                loanSchedule.AllInRate = Convert.ToDecimal(dr["AllInRate"]);
                                loanSchedule.CouponPaymentDate = Convert.ToDateTime(dr["CouponPaymentDate"]);
                                loanSchedule.RiskFreeDP1 = Convert.ToDecimal(dr["RiskFreeDP1"]);
                                loanSchedule.RiskFreeDP2 = Convert.ToDecimal(dr["RiskFreeDP2"]);
                                loanSchedule.FloatingRate = Convert.ToDecimal(dr["FloatingRate"]);
                                loanSchedule.Interest = Convert.ToDecimal(dr["Interest"]);
                                loanSchedule.Days = Convert.ToInt16(dr["Days"]);
                                loanSchedule.AmortisationInt = Convert.ToDecimal(dr["AmortisationInt"]);
                                loanSchedule.CreatedOn = DateTime.UtcNow;
                                loanScheduleBL.SaveLoanSchedule(loanSchedule);
                            }
                            //lblMessage.Text = "Loan added Successfully";
                            //lblMessage.Visible = true;
                            RadWindowManager1.RadAlert(ddlAddLoanCode.Text + " added Successfully", 300, 150, "realedge associates", "alertCallBackFn");
                        }
                        else
                        {
                            InsertDuplicateLoan();
                        }
                        break;
                    case "Edit":
                        bll.EditLoan(loan, Convert.ToInt16(Session["EditLoanID"]), 2);
                        login = Session["LogedInUser"] as DAL.Login;
                        LoanHistory loanHis = new LoanHistory();
                        LoanHistoryBL historiesBL = new LoanHistoryBL();
                        loanHis.Action = "Edit";
                        loanHis.LoanName = loan.CodeName;
                        loanHis.UserName = login.Name;
                        loanHis.LastModified = DateTime.Now;
                        historiesBL.SaveHistory(loanHis);
                        CalculateFactors();
                        LoanScheduleBL scheduleBL = new LoanScheduleBL();
                        if (Session["LoanSchedule"] != null)
                        {
                            dt1 = (DataTable)Session["LoanSchedule"];
                        }
                        scheduleBL.EditSchedule(Convert.ToInt16(Session["EditLoanID"]), dt1);

                        // Clear();
                        //lblMessage.Text = "Loan updated Successfully";
                        //lblMessage.Visible = true;
                        RadWindowManager1.RadAlert(ddlAddLoanCode.Text + " updated Successfully", 300, 150, "realedge associates", "alertCallBackFn");

                        //lblConfirm.Text = "Are you sure you want to add this loan to database?";
                        //_operation = "Add";
                        //Session.Remove("EditLoanID");
                        break;
                    default:
                        break;
                }

                // all loans
                List<Loans> allLoans = bll.GetLoans();
                BindLoansTab(allLoans);
                BindLoansData();
                BindHistoricalQuotesAndTradesTab();
                // check to see if the loan has been added or updated
                if (isNewLoan)
                {
                    ApplicationHub.NewLoanAdded(loan);
                }
                else
                {
                    ApplicationHub.RefreshLoans(allLoans);
                }
                hdnSaved.Value = "Y";
                BindLoanCode();
            }
            catch (Exception ex)
            {
                //lblAddLoanMessage.Visible = true;                     on 7-1
                //lblAddLoanMessage.Text = "Loan failed to save";           on 7-1
                //  lblMessage.Text = "Loan failed to save";
                lblMessage.Text = ex.Message;
                lblMessage.Text = ex.InnerException.Message;
                lblMessage.Visible = true;
                LogActivity("New Loan(Unsuccessfull)", "Unable to save the loan", ex.Message);
            }
        }
        protected void grdLoans_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                int id = Convert.ToInt32(e.CommandArgument);
                Session.Add("EditLoanID", id);
                Response.Redirect("default.aspx?page=addstaticloan");
            }
            else if (e.CommandName == "Delete")
            {
                int id = Convert.ToInt32(e.CommandArgument);
                LoansBLL loansBll = new LoansBLL();
                string loanName = Convert.ToString(loansBll.GetLoanByID(id).CodeName);
                loansBll.RemoveLoan(id);

                DAL.Login login = Session["LogedInUser"] as DAL.Login;
                LoanHistory history = new LoanHistory();
                LoanHistoryBL historyBL = new LoanHistoryBL();
                history.Action = "Delete";
                history.LastModified = DateTime.Now;
                history.LoanName = loanName;
                history.UserName = login.Name;
                historyBL.SaveHistory(history);

                RadWindowManager1.RadAlert("Loan removed successfully", 330, 180, "realedge associates", "alertCallBackFn");
            }
            BindLoansData();
        }