protected void btnUnderwritingComplete_Click(object sender, EventArgs e)
        {
            MerchantModel editMerchant = new MerchantModel();
            UnderwritingResultModel underwritingResult = new UnderwritingResultModel();
            UnderwritingResultModel oldUnderwritingResult = new UnderwritingResultModel();
            Int32 merchantRecordId;
            String adminId;

            try
            {
                merchantRecordId = Convert.ToInt32(hMerchantRecordId.Value);
                adminId = Context.User.Identity.GetUserId();

                using (ApplicationDbContext ctx = new ApplicationDbContext())
                {
                    editMerchant = ctx.Merchants.First(m => m.RecordId == merchantRecordId);

                    underwritingResult = ctx.UnderwritingResults.Where(ur => ur.Merchant.RecordId == merchantRecordId && ur.Active == true).First();

                    underwritingResult.CorpInfoResult = Convert.ToInt16(rblUWCorpInfoVerifiedResult.SelectedValue);
                    underwritingResult.CorpInfoNotes = txtUWCorpInfoVerifiedNotes.Text;

                    underwritingResult.BusLicStatusResult = Convert.ToInt16(rblUWBusLicStatusResult.SelectedValue);
                    underwritingResult.BusLicStatusNotes = txtUWBUsLicStatusVerifiedNotes.Text;

                    underwritingResult.EINResult = Convert.ToInt16(rblUWEINVerifiedResult.SelectedValue);
                    underwritingResult.EINNotes = txtUWEINVerifiedNotes.Text;

                    underwritingResult.PrincipalResult = Convert.ToInt16(rblUWPrincipalVerifiedResult.SelectedValue);
                    underwritingResult.PrincipalNotes = txtUWPrincipalVerifiedNotes.Text;

                    underwritingResult.CardSalesIndicatorResult = Convert.ToInt16(rblUWCardSalesIndicatorsVerifiedResult.SelectedValue);
                    underwritingResult.CardSalesIndicatorNotes = txtUWCardSalesIndicatorsVerifiedNotes.Text;

                    underwritingResult.BankingInfoResult = Convert.ToInt16(rblUWBankingInfoVerifiedResult.SelectedValue);
                    underwritingResult.BankingInfoNotes = txtUWBankingInfoVerifiedNotes.Text;

                    underwritingResult.MCCResult = Convert.ToInt16(rblUWMCCVerifiedResult.SelectedValue);
                    underwritingResult.MCCNotes = txtUWMCCVerifiedNotes.Text;

                    underwritingResult.BVIResult = Convert.ToInt16(rblUWBVIVerifiedResult.SelectedValue);
                    underwritingResult.BVINotes = txtUWBVIVerifiedNotes.Text;

                    underwritingResult.TaxLiensResult = Convert.ToInt16(rblUWTaxLiensVerifiedResult.SelectedValue);
                    underwritingResult.TaxLiensNotes = txtUWTaxLiensVerifiedNotes.Text;

                    underwritingResult.RiskIndicatorResult = Convert.ToInt16(rblUWRiskIndicatorVerifiedResult.SelectedValue);
                    underwritingResult.RiskIndicatorNotes = txtUWRiskIndicatorVerifiedNotes.Text;

                    underwritingResult.OFACMatchResult = Convert.ToInt16(rblUWOFACMatchVerifiedResult.SelectedValue);
                    underwritingResult.OFACMatchNotes = txtUWOFACMatchVerifiedNotes.Text;

                    if (underwritingResult.CorpInfoResult == 1
                        && underwritingResult.BusLicStatusResult == 1
                        && underwritingResult.EINResult == 1
                        && underwritingResult.PrincipalResult == 1
                        && underwritingResult.CardSalesIndicatorResult == 1
                        && underwritingResult.BankingInfoResult == 1
                        && underwritingResult.MCCResult == 1
                        && underwritingResult.BVIResult == 1
                        && underwritingResult.TaxLiensResult == 1
                        && underwritingResult.RiskIndicatorResult == 1
                        && underwritingResult.OFACMatchResult == 1
                        )
                    {
                        underwritingResult.UnderwritingDecision = "Pass";
                        editMerchant.UnderwritingStatus = ctx.UnderwritingStatuses.First(us => us.StatusDescription == "Approved");
                    }
                    else
                    {
                        underwritingResult.UnderwritingDecision = "Fail";
                        editMerchant.UnderwritingStatus = ctx.UnderwritingStatuses.First(us => us.StatusDescription == "Denied");

                        if (editMerchant.MerchantStatus.StatusDescription == "Enrolled")
                        {
                            editMerchant.MerchantStatus = ctx.MerchantStatuses.First(ms => ms.StatusDescription == "Denied");
                            editMerchant.DenialNotes = "Underwriting Decline.  See Underwriting Results for more information.";
                            editMerchant.DeniedBy = ctx.Users.First(u => u.Id == adminId);
                            editMerchant.DeniedDate = DateTime.UtcNow;
                            editMerchant.ApprovedAdvanceAmount = 0;
                            editMerchant.AvailableAdvanceAmount = 0;

                            //Send Email
                            Logic.Messaging messaging = new Logic.Messaging();
                            Boolean emailSent = false;
                            var template = messaging.GetTemplate("DeniedEnrollment");

                            if (template != null)
                            {
                                String html = messaging.GetTemplateHtml(template);

                                foreach (EmailTemplateVariableModel variable in messaging.GetRequiredVariables(template))
                                {
                                    if (editMerchant.MerchantPrincipal != null && editMerchant.MerchantPrincipal.Contact != null)
                                    {
                                        if (variable.VariableName == "MERCHANT_FIRST_NAME")
                                        {
                                            html = html.Replace("<$" + variable.VariableName + ">", editMerchant.MerchantPrincipal.Contact.FirstName);
                                            html = html.Replace("<$" + variable.VariableName + ">", editMerchant.MerchantPrincipal.Contact.FirstName);
                                        }
                                        if (variable.VariableName == "MERCHANT_LAST_NAME")
                                        {
                                            html = html.Replace("&lt;$" + variable.VariableName + "&gt;", editMerchant.MerchantPrincipal.Contact.LastName);
                                            html = html.Replace("<$" + variable.VariableName + ">", editMerchant.MerchantPrincipal.Contact.LastName);
                                        }
                                    }
                                    else
                                    {
                                        if (variable.VariableName == "MERCHANT_FIRST_NAME")
                                        {
                                            html = html.Replace("&lt;$" + variable.VariableName + "&gt;", "");
                                            html = html.Replace("<$" + variable.VariableName + ">", "");
                                        }
                                        if (variable.VariableName == "MERCHANT_LAST_NAME")
                                        {
                                            html = html.Replace("&lt;$" + variable.VariableName + "&gt;", "");
                                            html = html.Replace("<$" + variable.VariableName + ">", "");
                                        }
                                    }
                                    if (variable.VariableName == "MERCHANT_NAME")
                                    {
                                        html = html.Replace("&lt;$" + variable.VariableName + "&gt;", editMerchant.CorpName);
                                        html = html.Replace("<$" + variable.VariableName + ">", editMerchant.CorpName);
                                    }
                                    if (editMerchant.Business.Address != null)
                                    {
                                        if (variable.VariableName == "MERCHANT_ADDRESS")
                                        {
                                            html = html.Replace("&lt;$" + variable.VariableName + "&gt;", editMerchant.Business.Address.Address ?? "");
                                            html = html.Replace("<$" + variable.VariableName + ">", editMerchant.Business.Address.Address ?? "");
                                        }
                                        if (variable.VariableName == "MERCHANT_CITY")
                                        {
                                            html = html.Replace("&lt;$" + variable.VariableName + "&gt;", editMerchant.Business.Address.City ?? "");
                                            html = html.Replace("<$" + variable.VariableName + ">", editMerchant.Business.Address.City ?? "");
                                        }
                                        if (variable.VariableName == "MERCHANT_ZIP")
                                        {
                                            html = html.Replace("&lt;$" + variable.VariableName + "&gt;", editMerchant.Business.Address.Zip ?? "");
                                            html = html.Replace("<$" + variable.VariableName + ">", editMerchant.Business.Address.Zip ?? "");
                                        }
                                        if (editMerchant.Business.Address.State != null)
                                        {
                                            if (variable.VariableName == "MERCHANT_STATE")
                                            {
                                                html = html.Replace("&lt;$" + variable.VariableName + "&gt;", editMerchant.Business.Address.State.Name ?? "");
                                                html = html.Replace("<$" + variable.VariableName + ">", editMerchant.Business.Address.State.Name ?? "");
                                            }
                                        }
                                        else
                                        {
                                            if (variable.VariableName == "MERCHANT_STATE")
                                            {
                                                html = html.Replace("&lt;$" + variable.VariableName + "&gt;", "");
                                                html = html.Replace("<$" + variable.VariableName + ">", "");
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if (variable.VariableName == "MERCHANT_ADDRESS")
                                        {
                                            html = html.Replace("&lt;$" + variable.VariableName + "&gt;", "");
                                            html = html.Replace("<$" + variable.VariableName + ">", "");
                                        }
                                        if (variable.VariableName == "MERCHANT_CITY")
                                        {
                                            html = html.Replace("&lt;$" + variable.VariableName + "&gt;", "");
                                            html = html.Replace("<$" + variable.VariableName + ">", "");
                                        }
                                        if (variable.VariableName == "MERCHANT_STATE")
                                        {
                                            html = html.Replace("&lt;$" + variable.VariableName + "&gt;", "");
                                            html = html.Replace("<$" + variable.VariableName + ">", "");
                                        }
                                        if (variable.VariableName == "MERCHANT_ZIP")
                                        {
                                            html = html.Replace("&lt;$" + variable.VariableName + "&gt;", "");
                                            html = html.Replace("<$" + variable.VariableName + ">", "");
                                        }
                                    }
                                    if (variable.VariableName == "DENY_NOTES")
                                    {
                                        html = html.Replace("&lt;$" + variable.VariableName + "&gt;", editMerchant.DenialNotes);
                                        html = html.Replace("<$" + variable.VariableName + ">", editMerchant.DenialNotes);
                                    }
                                }

                                if (ctx.Users.Any(u => u.Merchant.RecordId == editMerchant.RecordId))
                                {
                                    IQueryable<ApplicationUser> userList = ctx.Users.Where(u => u.Merchant.RecordId == editMerchant.RecordId);

                                    if (userList != null)
                                    {
                                        foreach (ApplicationUser thisUser in userList)
                                        {
                                            if (html != null)
                                            {
                                                emailSent = messaging.SendEmail(thisUser.Email, template.EmailSubject, html, template, editMerchant, thisUser);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            editMerchant.MerchantStatus = ctx.MerchantStatuses.First(ms => ms.StatusDescription == "Suspended");

                            MerchantSuspensionModel suspension = new MerchantSuspensionModel();

                            suspension.Merchant = ctx.Merchants.First(m => m.RecordId == editMerchant.RecordId);
                            suspension.OtherReasonNotes = "Failed Underwriting. See Undewriting Results for more information.";
                            suspension.SuspendedBy = ctx.Users.First(u => u.Id == adminId);
                            suspension.SuspensionReason = ctx.MerchantSuspensionReasons.First(msr => msr.ReasonName == "Failed Underwriting Result");
                            suspension.Timestamp = DateTime.UtcNow;

                            ctx.MerchantSuspensions.Add(suspension);

                            //Send Email
                            Logic.Messaging messaging = new Logic.Messaging();
                            Boolean emailSent = false;
                            var template = messaging.GetTemplate("MerchantSuspended");

                            String suspendNotes = "Failed Underwriting.  Contact Customer Service for more information.";

                            if (template != null)
                            {
                                String html = messaging.GetTemplateHtml(template);

                                foreach (EmailTemplateVariableModel variable in messaging.GetRequiredVariables(template))
                                {
                                    if (editMerchant.MerchantPrincipal != null && editMerchant.MerchantPrincipal.Contact != null)
                                    {
                                        if (variable.VariableName == "MERCHANT_FIRST_NAME")
                                        {
                                            html = html.Replace("&lt;$" + variable.VariableName + "&gt;", editMerchant.MerchantPrincipal.Contact.FirstName);
                                            html = html.Replace("<$" + variable.VariableName + ">", editMerchant.MerchantPrincipal.Contact.FirstName);
                                        }
                                        if (variable.VariableName == "MERCHANT_LAST_NAME")
                                        {
                                            html = html.Replace("&lt;$" + variable.VariableName + "&gt;", editMerchant.MerchantPrincipal.Contact.LastName);
                                            html = html.Replace("<$" + variable.VariableName + ">", editMerchant.MerchantPrincipal.Contact.LastName);
                                        }
                                    }
                                    else
                                    {
                                        if (variable.VariableName == "MERCHANT_FIRST_NAME")
                                        {
                                            html = html.Replace("&lt;$" + variable.VariableName + "&gt;", "");
                                            html = html.Replace("<$" + variable.VariableName + ">", "");
                                        }
                                        if (variable.VariableName == "MERCHANT_LAST_NAME")
                                        {
                                            html = html.Replace("&lt;$" + variable.VariableName + "&gt;", "");
                                            html = html.Replace("<$" + variable.VariableName + ">", "");
                                        }
                                    }
                                    if (variable.VariableName == "MERCHANT_NAME")
                                    {
                                        html = html.Replace("&lt;$" + variable.VariableName + "&gt;", editMerchant.CorpName);
                                        html = html.Replace("<$" + variable.VariableName + ">", editMerchant.CorpName);
                                    }
                                    if (editMerchant.Business.Address != null)
                                    {
                                        if (variable.VariableName == "MERCHANT_ADDRESS")
                                        {
                                            html = html.Replace("&lt;$" + variable.VariableName + "&gt;", editMerchant.Business.Address.Address ?? "");
                                            html = html.Replace("<$" + variable.VariableName + ">", editMerchant.Business.Address.Address ?? "");
                                        }
                                        if (variable.VariableName == "MERCHANT_CITY")
                                        {
                                            html = html.Replace("&lt;$" + variable.VariableName + "&gt;", editMerchant.Business.Address.City ?? "");
                                            html = html.Replace("<$" + variable.VariableName + ">", editMerchant.Business.Address.City ?? "");
                                        }
                                        if (variable.VariableName == "MERCHANT_ZIP")
                                        {
                                            html = html.Replace("&lt;$" + variable.VariableName + "&gt;", editMerchant.Business.Address.Zip ?? "");
                                            html = html.Replace("<$" + variable.VariableName + ">", editMerchant.Business.Address.Zip ?? "");
                                        }
                                        if (editMerchant.Business.Address.State != null)
                                        {
                                            if (variable.VariableName == "MERCHANT_STATE")
                                            {
                                                html = html.Replace("&lt;$" + variable.VariableName + "&gt;", editMerchant.Business.Address.State.Name ?? "");
                                                html = html.Replace("<$" + variable.VariableName + ">", editMerchant.Business.Address.State.Name ?? "");
                                            }
                                        }
                                        else
                                        {
                                            if (variable.VariableName == "MERCHANT_STATE")
                                            {
                                                html = html.Replace("&lt;$" + variable.VariableName + "&gt;", "");
                                                html = html.Replace("<$" + variable.VariableName + ">", "");
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if (variable.VariableName == "MERCHANT_ADDRESS")
                                        {
                                            html = html.Replace("&lt;$" + variable.VariableName + "&gt;", "");
                                            html = html.Replace("<$" + variable.VariableName + ">", "");
                                        }
                                        if (variable.VariableName == "MERCHANT_CITY")
                                        {
                                            html = html.Replace("&lt;$" + variable.VariableName + "&gt;", "");
                                            html = html.Replace("<$" + variable.VariableName + ">", "");
                                        }
                                        if (variable.VariableName == "MERCHANT_STATE")
                                        {
                                            html = html.Replace("&lt;$" + variable.VariableName + "&gt;", "");
                                            html = html.Replace("<$" + variable.VariableName + ">", "");
                                        }
                                        if (variable.VariableName == "MERCHANT_ZIP")
                                        {
                                            html = html.Replace("&lt;$" + variable.VariableName + "&gt;", "");
                                            html = html.Replace("<$" + variable.VariableName + ">", "");
                                        }
                                    }
                                    if (variable.VariableName == "SUSPEND_NOTES")
                                    {
                                        html = html.Replace("&lt;$" + variable.VariableName + "&gt;", suspendNotes);
                                        html = html.Replace("<$" + variable.VariableName + ">", suspendNotes);
                                    }
                                }

                                if (ctx.Users.Any(u => u.Merchant.RecordId == editMerchant.RecordId))
                                {
                                    IQueryable<ApplicationUser> userList = ctx.Users.Where(u => u.Merchant.RecordId == editMerchant.RecordId);

                                    if (userList != null)
                                    {
                                        foreach (ApplicationUser thisUser in userList)
                                        {
                                            if (html != null)
                                            {
                                                emailSent = messaging.SendEmail(thisUser.Email, template.EmailSubject, html, template, editMerchant, thisUser);
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        ctx.SaveChanges();
                    }

                    ctx.SaveChanges();
                    UpdateStatusLabel();
                    gridUnderwritingHistory.Rebind();

                }

                pnlNewUnderwriting.Visible = false;
                pnlNewUnderwriting2.Visible = false;
                pnlUnderwritingHistory.Visible = true;
                pnlUnderwritingButtons.Visible = true;
                pnlUnderwritingAdvancedButtons.Visible = false;

                btnUnderwritingCancel.Visible = false;
                btnUnderwritingCancel2.Visible = false;
                btnUnderwritingTryAgain.Visible = false;
                btnUnderwritingComplete.Visible = false;
                btnUnderwritingSave.Visible = false;
                btnAddNewUnderwriting.Visible = true;
                btnUnderwritingContinue.Visible = false;

            }
            catch (System.Exception ex)
            {
                _newLogic.WriteExceptionToDB(ex, "btnUnderwritingComplete_Click");
            }
        }
        protected void btnSuspendMerchant_Click(object sender, EventArgs e)
        {
            try
            {
                using (ApplicationDbContext ctx = new ApplicationDbContext())
                {
                    int merchantId;
                    Int32.TryParse(hMerchantRecordId.Value, out merchantId);
                    string adminId = Context.User.Identity.GetUserId();

                    RadTextBox txtMerchantSuspensionNotes = (RadTextBox)StatusView.FindControl("txtMerchantSuspensionNotes");

                    if (ctx.Merchants.Any(m => m.RecordId == merchantId))
                    {
                        MerchantModel merchant = ctx.Merchants.First(m => m.RecordId == merchantId);
                        MerchantSuspensionModel suspension = new MerchantSuspensionModel();
                        Int32 reasonId = 0;
                        Int32.TryParse(ddlSuspendReasons.SelectedValue, out reasonId);

                        merchant.MerchantStatus = ctx.MerchantStatuses.First(ms => ms.StatusDescription == "Suspended");
                        suspension.Merchant = merchant;
                        suspension.OtherReasonNotes = txtMerchantSuspensionNotes.Text;
                        suspension.SuspendedBy = ctx.Users.First(u => u.Id == adminId);
                        suspension.SuspensionReason = ctx.MerchantSuspensionReasons.First(msr => msr.RecordId == reasonId);
                        suspension.Timestamp = DateTime.UtcNow;

                        ctx.MerchantSuspensions.Add(suspension);

                        ctx.SaveChanges();

                        StatusChangeModel statusChange = new StatusChangeModel
                        {
                            AdminUser = ctx.Users.First(u => u.Id == adminId),
                            Merchant = merchant,
                            MerchantStatus = merchant.MerchantStatus,
                            UnderwritingStatus = merchant.UnderwritingStatus,
                            Notes = suspension.SuspensionReason.ReasonName,
                            Timestamp = DateTime.UtcNow
                        };

                        ctx.StatusChanges.Add(statusChange);

                        ctx.SaveChanges();

                        //Send Email
                        Logic.Messaging messaging = new Logic.Messaging();
                        Boolean emailSent = false;
                        var template = messaging.GetTemplate("MerchantSuspended");

                        String suspendNotes = ddlSuspendReasons.SelectedText + ": " + txtMerchantSuspensionNotes.Text;

                        if (template != null)
                        {
                            String html = messaging.GetTemplateHtml(template);

                            foreach (EmailTemplateVariableModel variable in messaging.GetRequiredVariables(template))
                            {
                                if (merchant.MerchantPrincipal != null && merchant.MerchantPrincipal.Contact != null)
                                {
                                    if (variable.VariableName == "MERCHANT_FIRST_NAME")
                                    {
                                        html = html.Replace("&lt;$" + variable.VariableName + "&gt;", merchant.MerchantPrincipal.Contact.FirstName);
                                        html = html.Replace("<$" + variable.VariableName + ">", merchant.MerchantPrincipal.Contact.FirstName);
                                    }
                                    if (variable.VariableName == "MERCHANT_LAST_NAME")
                                    {
                                        html = html.Replace("&lt;$" + variable.VariableName + "&gt;", merchant.MerchantPrincipal.Contact.LastName);
                                        html = html.Replace("<$" + variable.VariableName + ">", merchant.MerchantPrincipal.Contact.LastName);
                                    }
                                }
                                else
                                {
                                    if (variable.VariableName == "MERCHANT_FIRST_NAME")
                                    {
                                        html = html.Replace("&lt;$" + variable.VariableName + "&gt;", "");
                                        html = html.Replace("<$" + variable.VariableName + ">", "");
                                    }
                                    if (variable.VariableName == "MERCHANT_LAST_NAME")
                                    {
                                        html = html.Replace("&lt;$" + variable.VariableName + "&gt;", "");
                                        html = html.Replace("<$" + variable.VariableName + ">", "");
                                    }
                                }
                                if (variable.VariableName == "MERCHANT_NAME")
                                {
                                    html = html.Replace("&lt;$" + variable.VariableName + "&gt;", merchant.CorpName);
                                    html = html.Replace("<$" + variable.VariableName + ">", merchant.CorpName);
                                }
                                if (merchant.Business.Address != null)
                                {
                                    if (variable.VariableName == "MERCHANT_ADDRESS")
                                    {
                                        html = html.Replace("&lt;$" + variable.VariableName + "&gt;", merchant.Business.Address.Address ?? "");
                                        html = html.Replace("<$" + variable.VariableName + ">", merchant.Business.Address.Address ?? "");
                                    }
                                    if (variable.VariableName == "MERCHANT_CITY")
                                    {
                                        html = html.Replace("&lt;$" + variable.VariableName + "&gt;", merchant.Business.Address.City ?? "");
                                        html = html.Replace("<$" + variable.VariableName + ">", merchant.Business.Address.City ?? "");
                                    }
                                    if (variable.VariableName == "MERCHANT_ZIP")
                                    {
                                        html = html.Replace("&lt;$" + variable.VariableName + "&gt;", merchant.Business.Address.Zip ?? "");
                                        html = html.Replace("<$" + variable.VariableName + ">", merchant.Business.Address.Zip ?? "");
                                    }
                                    if (merchant.Business.Address.State != null)
                                    {
                                        if (variable.VariableName == "MERCHANT_STATE")
                                        {
                                            html = html.Replace("&lt;$" + variable.VariableName + "&gt;", merchant.Business.Address.State.Name ?? "");
                                            html = html.Replace("<$" + variable.VariableName + ">", merchant.Business.Address.State.Name ?? "");
                                        }
                                    }
                                    else
                                    {
                                        if (variable.VariableName == "MERCHANT_STATE")
                                        {
                                            html = html.Replace("&lt;$" + variable.VariableName + "&gt;", "");
                                            html = html.Replace("<$" + variable.VariableName + ">", "");
                                        }
                                    }
                                }
                                else
                                {
                                    if (variable.VariableName == "MERCHANT_ADDRESS")
                                    {
                                        html = html.Replace("&lt;$" + variable.VariableName + "&gt;", "");
                                        html = html.Replace("<$" + variable.VariableName + ">", "");
                                    }
                                    if (variable.VariableName == "MERCHANT_CITY")
                                    {
                                        html = html.Replace("&lt;$" + variable.VariableName + "&gt;", "");
                                        html = html.Replace("<$" + variable.VariableName + ">", "");
                                    }
                                    if (variable.VariableName == "MERCHANT_STATE")
                                    {
                                        html = html.Replace("&lt;$" + variable.VariableName + "&gt;", "");
                                        html = html.Replace("<$" + variable.VariableName + ">", "");
                                    }
                                    if (variable.VariableName == "MERCHANT_ZIP")
                                    {
                                        html = html.Replace("&lt;$" + variable.VariableName + "&gt;", "");
                                        html = html.Replace("<$" + variable.VariableName + ">", "");
                                    }
                                }
                                if (variable.VariableName == "SUSPEND_NOTES")
                                {
                                    html = html.Replace("&lt;$" + variable.VariableName + "&gt;", suspendNotes);
                                    html = html.Replace("<$" + variable.VariableName + ">", suspendNotes);
                                }
                            }

                            if (ctx.Users.Any(u => u.Merchant.RecordId == merchant.RecordId))
                            {
                                IQueryable<ApplicationUser> userList = ctx.Users.Where(u => u.Merchant.RecordId == merchant.RecordId);

                                if (userList != null)
                                {
                                    foreach (ApplicationUser thisUser in userList)
                                    {
                                        if (html != null)
                                        {
                                            emailSent = messaging.SendEmail(thisUser.Email, template.EmailSubject, html, template, merchant, thisUser);
                                        }
                                    }
                                }
                            }
                        }
                    }

                    lblMerchantActionMessage.Text = "Merchant Suspended Successfully.";

                    UpdateStatusLabel();

                    txtMerchantSuspensionNotes.Text = "";
                    ddlSuspendReasons.SelectedIndex = -1;

                }
            }
            catch (System.Exception ex)
            {
                lblMerchantActionMessage.Text = "Merchant was unable to be Suspeded.  Please contact your system administrator for more information.";
                _newLogic.WriteExceptionToDB(ex, "btnSuspendMerchant_Click");
            }
        }
        protected void btnConfirmUsers_Click(object sender, EventArgs e)
        {
            pnlUserList.Visible = true;
            pnlAddUserToMerchant.Visible = false;

            pnlUserButtons.Visible = true;
            btnAddUserToMerchant.Visible = true;
            btnConfirmUsers.Visible = false;
            btnCancelUserChanges.Visible = false;

            Hashtable values = new Hashtable();

            if (rlvAvailableUsers.SelectedItems.Any())
            {
                foreach (RadListViewDataItem item in rlvAvailableUsers.SelectedItems)
                {
                    using (ApplicationDbContext ctx = new ApplicationDbContext())
                    {
                        LinkButton btn = (LinkButton)item.FindControl("btnUserSelection");

                        if (ctx.Users.Any(x => x.UserName == btn.Text))
                        {
                            ApplicationUser user = ctx.Users.Include(x => x.Merchant).FirstOrDefault(x => x.UserName == btn.Text);

                            Int32 merchantId = Convert.ToInt32(hMerchantRecordId.Value);
                            MerchantModel merchant = ctx.Merchants.FirstOrDefault(x => x.RecordId == merchantId);

                            if (merchant != null)
                            {
                                user.Merchant = merchant;
                                user.DisassociatedMerchant = null;
                                user.HasBeenDisassociated = false;
                                ctx.SaveChanges();

                                Logic.Messaging messaging = new Logic.Messaging();
                                Boolean emailSent = false;
                                var template = messaging.GetTemplate("UserAssociated");

                                if (template != null)
                                {
                                    String html = messaging.GetTemplateHtml(template);

                                    foreach (EmailTemplateVariableModel variable in messaging.GetRequiredVariables(template))
                                    {
                                        if (variable.VariableName == "MERCHANT_NAME")
                                        {
                                            html = html.Replace("&lt;$" + variable.VariableName + "&gt;", merchant.CorpName);
                                            html = html.Replace("<$" + variable.VariableName + ">", merchant.CorpName);
                                        }
                                        if (variable.VariableName == "USERNAME")
                                        {
                                            html = html.Replace("&lt;$" + variable.VariableName + "&gt;", user.UserName);
                                            html = html.Replace("<$" + variable.VariableName + ">", user.UserName);
                                        }
                                        if (variable.VariableName == "USER_FIRST_NAME")
                                        {
                                            html = html.Replace("&lt;$" + variable.VariableName + "&gt;", user.FirstName);
                                            html = html.Replace("<$" + variable.VariableName + ">", user.FirstName);
                                        }
                                        if (variable.VariableName == "USER_LAST_NAME")
                                        {
                                            html = html.Replace("&lt;$" + variable.VariableName + "&gt;", user.LastName);
                                            html = html.Replace("<$" + variable.VariableName + ">", user.LastName);
                                        }
                                        if (variable.VariableName == "USER_PHONE")
                                        {
                                            html = html.Replace("&lt;$" + variable.VariableName + "&gt;", user.PhoneNumber);
                                            html = html.Replace("<$" + variable.VariableName + ">", user.PhoneNumber);
                                        }
                                        if (variable.VariableName == "USER_EMAIL")
                                        {
                                            html = html.Replace("&lt;$" + variable.VariableName + "&gt;", user.Email);
                                            html = html.Replace("<$" + variable.VariableName + ">", user.Email);
                                        }

                                    }
                                    if (html != null && user != null)
                                    {
                                        emailSent = messaging.SendEmail(user.Email, template.EmailSubject, html, template, merchant, user);
                                    }
                                }

                            }
                        }
                    }
                }
            }

            rlvAvailableUsers.ClearSelectedItems();
            Session.Clear();
            gridUserList.Rebind();
            rlvAvailableUsers.Rebind();
        }
        protected void btnDenyAdvance_Click(object sender, EventArgs e)
        {
            try
            {
                using (ApplicationDbContext ctx = new ApplicationDbContext())
                {
                    int advanceId;
                    Decimal availableAdvAmount = 0;

                    foreach (GridDataItem item in gridActiveAdvances.Items)
                    {
                        if (item.EditFormItem.IsInEditMode)
                        {
                            Label lblActionMessage = (Label)item.EditFormItem.FindControl("lblActionMessage");
                            RadTextBox txtDenyNotes = (RadTextBox)item.EditFormItem.FindControl("txtDenyNotes");

                            advanceId = Convert.ToInt32(item.GetDataKeyValue("RecordId"));
                            AdvanceModel advance = ctx.Advances.First(a => a.RecordId == advanceId);
                            MerchantModel merchant = ctx.Merchants.First(m => m.RecordId == advance.Merchant.RecordId);

                            string userId = Context.User.Identity.GetUserId();

                            advance.Status = ctx.AdvanceStatuses.First(advs => advs.StatusDescription == "Denied");
                            advance.FundingStatus = ctx.FundingStatuses.First(fs => fs.StatusDescription == "Rejected");
                            advance.PrincipalBalance = 0;
                            advance.FeeBalance = 0;
                            advance.ApprovedTimestamp = DateTime.UtcNow;
                            advance.LastUpdateTimestamp = DateTime.UtcNow;
                            advance.ApprovedBy = ctx.Users.First(u => u.Id == userId);
                            advance.AdminNotes = txtDenyNotes.Text;

                            availableAdvAmount = merchant.AvailableAdvanceAmount.Value + advance.PrincipalAmount.Value;
                            merchant.AvailableAdvanceAmount = availableAdvAmount > merchant.ApprovedAdvanceAmount.Value ? merchant.ApprovedAdvanceAmount.Value : availableAdvAmount;

                            ctx.SaveChanges();

                            gridActiveAdvances.MasterTableView.ClearEditItems();

                            gridAdvanceHistory.Rebind();
                            gridActiveAdvances.Rebind();

                            Session["ActionMessage"] = "The Advance and Funding Statuses have been successfully updated.";

                            //Send Email
                            Logic.Messaging messaging = new Logic.Messaging();
                            Boolean emailSent = false;
                            var template = messaging.GetTemplate("DeniedAdvance");

                            if (template != null)
                            {
                                String html = messaging.GetTemplateHtml(template);

                                foreach (EmailTemplateVariableModel variable in messaging.GetRequiredVariables(template))
                                {
                                    if (variable.VariableName == "MERCHANT_FIRST_NAME")
                                    {
                                        html = html.Replace("&lt;$" + variable.VariableName + "&gt;", merchant.MerchantPrincipal.Contact.FirstName);
                                        html = html.Replace("<$" + variable.VariableName + ">", merchant.MerchantPrincipal.Contact.FirstName);
                                    }
                                    if (variable.VariableName == "MERCHANT_LAST_NAME")
                                    {
                                        html = html.Replace("&lt;$" + variable.VariableName + "&gt;", merchant.MerchantPrincipal.Contact.LastName);
                                        html = html.Replace("<$" + variable.VariableName + ">", merchant.MerchantPrincipal.Contact.LastName);
                                    }
                                    if (variable.VariableName == "MERCHANT_NAME")
                                    {
                                        html = html.Replace("&lt;$" + variable.VariableName + "&gt;", merchant.CorpName);
                                        html = html.Replace("<$" + variable.VariableName + ">", merchant.CorpName);
                                    }
                                    if (variable.VariableName == "MERCHANT_ADDRESS")
                                    {
                                        html = html.Replace("&lt;$" + variable.VariableName + "&gt;", merchant.Business.Address.Address);
                                        html = html.Replace("<$" + variable.VariableName + ">", merchant.Business.Address.Address);
                                    }
                                    if (variable.VariableName == "MERCHANT_CITY")
                                    {
                                        html = html.Replace("&lt;$" + variable.VariableName + "&gt;", merchant.Business.Address.City);
                                        html = html.Replace("<$" + variable.VariableName + ">", merchant.Business.Address.City);
                                    }
                                    if (variable.VariableName == "MERCHANT_STATE")
                                    {
                                        html = html.Replace("&lt;$" + variable.VariableName + "&gt;", merchant.Business.Address.State.Name);
                                        html = html.Replace("<$" + variable.VariableName + ">", merchant.Business.Address.State.Name);
                                    }
                                    if (variable.VariableName == "MERCHANT_ZIP")
                                    {
                                        html = html.Replace("&lt;$" + variable.VariableName + "&gt;", merchant.Business.Address.Zip);
                                        html = html.Replace("<$" + variable.VariableName + ">", merchant.Business.Address.Zip);
                                    }
                                    if (variable.VariableName == "ADVANCE_AMOUNT")
                                    {
                                        html = html.Replace("&lt;$" + variable.VariableName + "&gt;", advance.PrincipalAmount.ToString());
                                        html = html.Replace("<$" + variable.VariableName + ">", advance.PrincipalAmount.ToString());
                                    }
                                    if (variable.VariableName == "DENY_NOTES")
                                    {
                                        html = html.Replace("&lt;$" + variable.VariableName + "&gt;", advance.AdminNotes);
                                        html = html.Replace("<$" + variable.VariableName + ">", advance.AdminNotes);
                                    }

                                }

                                if (html != null && advance.RequestedBy != null)
                                {
                                    emailSent = messaging.SendEmail(advance.RequestedBy.Email, template.EmailSubject, html, template, merchant, advance.RequestedBy);
                                }

                            }
                        }
                    }
                }

            }
            catch (System.Exception ex)
            {
                Session["ActionMessage"] = "An error has occurred while attempting to deny this Advance.  Please contact your system administrator for additional information.";

                _newLogic.WriteExceptionToDB(ex, "btnDenyAdvance_Click");
            }
        }
        protected void btnApproveMerchant_Click(object sender, EventArgs e)
        {
            try
            {
                using (ApplicationDbContext ctx = new ApplicationDbContext())
                {
                    int merchantId;
                    Int32.TryParse(hMerchantRecordId.Value, out merchantId);
                    string adminId = Context.User.Identity.GetUserId();

                    RadTextBox txtMerchantApprovalNotes = (RadTextBox)StatusView.FindControl("txtMerchantApprovalNotes");

                    if (ctx.Merchants.Any(m => m.RecordId == merchantId))
                    {
                        MerchantModel merchant = ctx.Merchants.First(m => m.RecordId == merchantId);
                        Decimal approvedAdvanceAmount = 0;
                        Decimal.TryParse(txtApprovedAdvanceAmount.Text, out approvedAdvanceAmount);

                        merchant.MerchantStatus = ctx.MerchantStatuses.First(ms => ms.StatusDescription == "Active");
                        merchant.ApprovedBy = ctx.Users.First(u => u.Id == adminId);
                        merchant.ApprovalNotes = txtMerchantApprovalNotes.Text;
                        merchant.ApprovedDate = DateTime.UtcNow.Date;
                        merchant.ApprovedAdvanceAmount = approvedAdvanceAmount;
                        merchant.AvailableAdvanceAmount = approvedAdvanceAmount;

                        ctx.SaveChanges();

                        StatusChangeModel statusChange = new StatusChangeModel
                        {
                            AdminUser = ctx.Users.First(u => u.Id == adminId),
                            Merchant = merchant,
                            MerchantStatus = merchant.MerchantStatus,
                            UnderwritingStatus = merchant.UnderwritingStatus,
                            Notes = merchant.ApprovalNotes,
                            Timestamp = DateTime.UtcNow
                        };

                        ctx.StatusChanges.Add(statusChange);

                        ctx.SaveChanges();

                        //Send Email
                        Logic.Messaging messaging = new Logic.Messaging();
                        Boolean emailSent = false;
                        var template = messaging.GetTemplate("ApprovedEnrollment");

                        if (template != null)
                        {
                            String html = messaging.GetTemplateHtml(template);

                            foreach (EmailTemplateVariableModel variable in messaging.GetRequiredVariables(template))
                            {
                                if (merchant.MerchantPrincipal != null && merchant.MerchantPrincipal.Contact != null)
                                {
                                    if (variable.VariableName == "MERCHANT_FIRST_NAME")
                                    {
                                        html = html.Replace("&lt;$" + variable.VariableName + "&gt;", merchant.MerchantPrincipal.Contact.FirstName);
                                        html = html.Replace("<$" + variable.VariableName + ">", merchant.MerchantPrincipal.Contact.FirstName);
                                    }
                                    if (variable.VariableName == "MERCHANT_LAST_NAME")
                                    {
                                        html = html.Replace("&lt;$" + variable.VariableName + "&gt;", merchant.MerchantPrincipal.Contact.LastName);
                                        html = html.Replace("<$" + variable.VariableName + ">", merchant.MerchantPrincipal.Contact.LastName);
                                    }
                                }
                                else
                                {
                                    if (variable.VariableName == "MERCHANT_FIRST_NAME")
                                    {
                                        html = html.Replace("&lt;$" + variable.VariableName + "&gt;", "");
                                        html = html.Replace("<$" + variable.VariableName + ">", "");
                                    }
                                    if (variable.VariableName == "MERCHANT_LAST_NAME")
                                    {
                                        html = html.Replace("&lt;$" + variable.VariableName + "&gt;", "");
                                        html = html.Replace("<$" + variable.VariableName + ">", "");
                                    }
                                }
                                if (variable.VariableName == "MERCHANT_NAME")
                                {
                                    html = html.Replace("&lt;$" + variable.VariableName + "&gt;", merchant.CorpName);
                                    html = html.Replace("<$" + variable.VariableName + ">", merchant.CorpName);
                                }
                                if (merchant.Business.Address != null)
                                {
                                    if (variable.VariableName == "MERCHANT_ADDRESS")
                                    {
                                        html = html.Replace("&lt;$" + variable.VariableName + "&gt;", merchant.Business.Address.Address ?? "");
                                        html = html.Replace("<$" + variable.VariableName + ">", merchant.Business.Address.Address ?? "");
                                    }
                                    if (variable.VariableName == "MERCHANT_CITY")
                                    {
                                        html = html.Replace("&lt;$" + variable.VariableName + "&gt;", merchant.Business.Address.City ?? "");
                                        html = html.Replace("<$" + variable.VariableName + ">", merchant.Business.Address.City ?? "");
                                    }
                                    if (variable.VariableName == "MERCHANT_ZIP")
                                    {
                                        html = html.Replace("&lt;$" + variable.VariableName + "&gt;", merchant.Business.Address.Zip ?? "");
                                        html = html.Replace("<$" + variable.VariableName + ">", merchant.Business.Address.Zip ?? "");
                                    }
                                    if (merchant.Business.Address.State != null)
                                    {
                                        if (variable.VariableName == "MERCHANT_STATE")
                                        {
                                            html = html.Replace("&lt;$" + variable.VariableName + "&gt;", merchant.Business.Address.State.Name ?? "");
                                            html = html.Replace("<$" + variable.VariableName + ">", merchant.Business.Address.State.Name ?? "");
                                        }
                                    }
                                    else
                                    {
                                        if (variable.VariableName == "MERCHANT_STATE")
                                        {
                                            html = html.Replace("&lt;$" + variable.VariableName + "&gt;", "");
                                            html = html.Replace("<$" + variable.VariableName + ">", "");
                                        }
                                    }
                                }
                                else
                                {
                                    if (variable.VariableName == "MERCHANT_ADDRESS")
                                    {
                                        html = html.Replace("&lt;$" + variable.VariableName + "&gt;", "");
                                        html = html.Replace("<$" + variable.VariableName + ">", "");
                                    }
                                    if (variable.VariableName == "MERCHANT_CITY")
                                    {
                                        html = html.Replace("&lt;$" + variable.VariableName + "&gt;", "");
                                        html = html.Replace("<$" + variable.VariableName + ">", "");
                                    }
                                    if (variable.VariableName == "MERCHANT_STATE")
                                    {
                                        html = html.Replace("&lt;$" + variable.VariableName + "&gt;", "");
                                        html = html.Replace("<$" + variable.VariableName + ">", "");
                                    }
                                    if (variable.VariableName == "MERCHANT_ZIP")
                                    {
                                        html = html.Replace("&lt;$" + variable.VariableName + "&gt;", "");
                                        html = html.Replace("<$" + variable.VariableName + ">", "");
                                    }
                                }
                                if (variable.VariableName == "APPROVED_ADVANCE_AMOUNT")
                                {
                                    html = html.Replace("&lt;$" + variable.VariableName + "&gt;", merchant.ApprovedAdvanceAmount.ToString());
                                    html = html.Replace("<$" + variable.VariableName + ">", merchant.ApprovedAdvanceAmount.ToString());
                                }
                            }

                            if (ctx.Users.Any(u => u.Merchant.RecordId == merchant.RecordId))
                            {
                                IQueryable<ApplicationUser> userList = ctx.Users.Where(u => u.Merchant.RecordId == merchant.RecordId);

                                if (userList != null)
                                {
                                    foreach (ApplicationUser thisUser in userList)
                                    {
                                        if (html != null)
                                        {
                                            String newHtml = html.Replace("&lt;$USERNAME&gt;", thisUser.UserName);
                                            newHtml = newHtml.Replace("<$USERNAME>", thisUser.UserName);

                                            emailSent = messaging.SendEmail(thisUser.Email, template.EmailSubject, newHtml, template, merchant, thisUser);
                                        }
                                    }
                                }
                            }
                        }
                    }

                    lblMerchantActionMessage.Text = "Merchant Approved Successfully.";

                    UpdateStatusLabel();
                }
            }
            catch (System.Exception ex)
            {
                lblMerchantActionMessage.Text = "Merchant was unable to be approved.  Please contact your system administrator for more information.";
                _newLogic.WriteExceptionToDB(ex, "btnApproveMerchant_Click");
            }
        }