private void SavePrincipalChanges(MerchantModel editMerchant, ApplicationDbContext ctx)
        {
            try
            {
                foreach (RepeaterItem rItem in rptrPrincipalChanges.Items)
                {
                    Label lblFieldName = (Label)rItem.FindControl("lblFieldName1");
                    Label lblNewValue = (Label)rItem.FindControl("lblNewValue1");
                    CheckBox cbConfirmed = (CheckBox)rItem.FindControl("cbConfirmed1");

                    if (lblFieldName != null && lblNewValue != null && cbConfirmed != null)
                    {
                        if (lblFieldName.Text == "First Name")
                        {
                            if (cbConfirmed.Checked)
                            {
                                AddChangeToAudit(editMerchant.RecordId, lblFieldName.Text, editMerchant.MerchantPrincipal.Contact.FirstName, lblNewValue.Text);

                                editMerchant.MerchantPrincipal.Contact.FirstName = lblNewValue.Text;
                            }
                            else
                            {
                                txtPrincipalFirstName.Text = editMerchant.MerchantPrincipal.Contact.FirstName;
                            }
                        }

                        if (lblFieldName.Text == "Last Name")
                        {
                            if (cbConfirmed.Checked)
                            {
                                AddChangeToAudit(editMerchant.RecordId, lblFieldName.Text, editMerchant.MerchantPrincipal.Contact.LastName, lblNewValue.Text);

                                editMerchant.MerchantPrincipal.Contact.LastName = lblNewValue.Text;
                            }
                            else
                            {
                                txtPrincipalLastName.Text = editMerchant.MerchantPrincipal.Contact.LastName;
                            }
                        }

                        if (lblFieldName.Text == "M.I.")
                        {
                            if (cbConfirmed.Checked)
                            {
                                AddChangeToAudit(editMerchant.RecordId, lblFieldName.Text, editMerchant.MerchantPrincipal.Contact.MiddleInitial, lblNewValue.Text);

                                editMerchant.MerchantPrincipal.Contact.MiddleInitial = lblNewValue.Text;
                            }
                            else
                            {
                                txtPrincipalMI.Text = editMerchant.MerchantPrincipal.Contact.MiddleInitial;
                            }
                        }

                        if (lblFieldName.Text == "D.O.B.")
                        {
                            if (cbConfirmed.Checked)
                            {
                                AddChangeToAudit(editMerchant.RecordId, lblFieldName.Text, editMerchant.MerchantPrincipal.PrincipalDoB.ToString(), lblNewValue.Text);

                                editMerchant.MerchantPrincipal.PrincipalDoB = Convert.ToDateTime(lblNewValue.Text);
                            }
                            else
                            {
                                if (editMerchant.MerchantPrincipal.PrincipalDoB.HasValue)
                                {
                                    ddlBirthYear.SelectedValue = editMerchant.MerchantPrincipal.PrincipalDoB.Value.Year.ToString();
                                    ddlBirthMonth.SelectedValue = editMerchant.MerchantPrincipal.PrincipalDoB.Value.Month.ToString();
                                    ddlBirthDay.SelectedValue = editMerchant.MerchantPrincipal.PrincipalDoB.Value.Day.ToString();
                                }
                                else
                                {
                                    ddlBirthYear.SelectedIndex = -1;
                                    ddlBirthMonth.SelectedIndex = -1;
                                    ddlBirthDay.SelectedIndex = -1;
                                }
                            }
                        }

                        if (lblFieldName.Text == "S.S.N.")
                        {
                            if (cbConfirmed.Checked)
                            {
                                String maskedOldSsn = MaskSsn(editMerchant.MerchantPrincipal.PrincipalSsn, editMerchant.MerchantPrincipal.RecordId);

                                Logic.Crypt crypto1 = new Logic.Crypt();
                                crypto1.CryptType = "SSN";
                                crypto1.CryptData = Convert.ToBase64String(PWDTK.StringToUtf8Bytes(lblNewValue.Text));
                                crypto1.Purpose = "86d4b5b2-b34a-4968-9fde-f8ed677bca8b";
                                crypto1.AdditionalData = editMerchant.MerchantPrincipal.RecordId.ToString();

                                byte[] newSsn = crypto1.Protect();

                                editMerchant.MerchantPrincipal.PrincipalSsn = newSsn;

                                AddChangeToAudit(editMerchant.RecordId, lblFieldName.Text, maskedOldSsn, MaskSsn(newSsn, editMerchant.MerchantPrincipal.RecordId));

                            }
                            else
                            {
                                if (editMerchant.MerchantPrincipal.PrincipalSsn != null)
                                {
                                    txtPrincipalSSN.Text = MaskSsn(editMerchant.MerchantPrincipal.PrincipalSsn, editMerchant.MerchantPrincipal.RecordId);
                                }
                                else
                                {
                                    txtPrincipalSSN.Text = "";
                                }
                            }

                            txtPrincipalSSN.Enabled = false;
                        }

                        if (lblFieldName.Text == "% Ownership")
                        {
                            if (cbConfirmed.Checked)
                            {
                                AddChangeToAudit(editMerchant.RecordId, lblFieldName.Text, editMerchant.MerchantPrincipal.PrincipalPctOwn.ToString(), lblNewValue.Text);

                                editMerchant.MerchantPrincipal.PrincipalPctOwn = Convert.ToDecimal(lblNewValue.Text.Substring(0, lblNewValue.Text.Length - 2));
                            }
                            else
                            {
                                if (editMerchant.MerchantPrincipal.PrincipalPctOwn.HasValue)
                                {
                                    txtPrincipalPctOwn.Text = editMerchant.MerchantPrincipal.PrincipalPctOwn.ToString();
                                }
                                else
                                {
                                    txtPrincipalPctOwn.Text = "";
                                }
                            }
                        }

                        if (lblFieldName.Text == "Title")
                        {
                            if (cbConfirmed.Checked)
                            {
                                AddChangeToAudit(editMerchant.RecordId, lblFieldName.Text, editMerchant.MerchantPrincipal.Contact.Title, lblNewValue.Text);

                                editMerchant.MerchantPrincipal.Contact.Title = lblNewValue.Text;
                            }
                            else
                            {
                                txtPrincipalTitle.Text = editMerchant.MerchantPrincipal.Contact.Title;
                            }
                        }

                        if (lblFieldName.Text == "DL Number")
                        {
                            if (cbConfirmed.Checked)
                            {
                                String maskedOldDL = MaskDLNumber(editMerchant.MerchantPrincipal.PrincipalDLNumber, editMerchant.MerchantPrincipal.RecordId);

                                Logic.Crypt crypto2 = new Logic.Crypt();
                                crypto2.CryptType = "DL";
                                crypto2.CryptData = Convert.ToBase64String(PWDTK.StringToUtf8Bytes(lblNewValue.Text));
                                crypto2.Purpose = "29dc7202-ae1f-4d38-9694-9d700a94897b";
                                crypto2.AdditionalData = editMerchant.MerchantPrincipal.RecordId.ToString();

                                byte[] newDl = crypto2.Protect();

                                editMerchant.MerchantPrincipal.PrincipalDLNumber = newDl;

                                AddChangeToAudit(editMerchant.RecordId, lblFieldName.Text, maskedOldDL, MaskDLNumber(newDl, editMerchant.MerchantPrincipal.RecordId));

                            }
                            else
                            {
                                txtPrincipalDLNumber.Text = MaskDLNumber(editMerchant.MerchantPrincipal.PrincipalDLNumber, editMerchant.MerchantPrincipal.RecordId);
                            }

                            txtPrincipalDLNumber.Enabled = false;
                        }

                        if (lblFieldName.Text == "DL State")
                        {
                            if (cbConfirmed.Checked)
                            {
                                string stateName = editMerchant.MerchantPrincipal.PrincipalDLState == null ? "" : editMerchant.MerchantPrincipal.PrincipalDLState.Name;

                                AddChangeToAudit(editMerchant.RecordId, lblFieldName.Text, stateName, lblNewValue.Text);

                                editMerchant.MerchantPrincipal.PrincipalDLState = ctx.GeoStates.First(gs => gs.Name == lblNewValue.Text);
                            }
                            else
                            {
                                if (editMerchant.MerchantPrincipal.PrincipalDLState != null)
                                {
                                    ddlPrincipalDLState.SelectedValue = editMerchant.MerchantPrincipal.PrincipalDLState.RecordId.ToString();
                                }
                                else
                                {
                                    ddlPrincipalDLState.SelectedIndex = -1;
                                }
                            }
                        }

                        if (lblFieldName.Text == "Address")
                        {
                            if (cbConfirmed.Checked)
                            {
                                if (editMerchant.MerchantPrincipal.Contact.Address != null)
                                {
                                    AddChangeToAudit(editMerchant.RecordId, lblFieldName.Text, editMerchant.MerchantPrincipal.Contact.Address.Address, lblNewValue.Text);

                                    editMerchant.MerchantPrincipal.Contact.Address.Address = lblNewValue.Text;
                                }
                                else
                                {
                                    AddChangeToAudit(editMerchant.RecordId, lblFieldName.Text, "", lblNewValue.Text);

                                    AddressModel newContactAddress = new AddressModel();
                                    newContactAddress.Address = lblNewValue.Text;
                                    ctx.Addresses.Add(newContactAddress);
                                    editMerchant.MerchantPrincipal.Contact.Address = newContactAddress;
                                }
                            }
                            else
                            {
                                if (editMerchant.MerchantPrincipal.Contact.Address != null)
                                {
                                    txtPrincipalAddress.Text = editMerchant.MerchantPrincipal.Contact.Address.Address;
                                }
                                else
                                {
                                    txtPrincipalAddress.Text = "";
                                }
                            }
                        }

                        if (lblFieldName.Text == "City")
                        {
                            if (cbConfirmed.Checked)
                            {
                                if (editMerchant.MerchantPrincipal.Contact.Address != null)
                                {
                                    AddChangeToAudit(editMerchant.RecordId, lblFieldName.Text, editMerchant.MerchantPrincipal.Contact.Address.City, lblNewValue.Text);

                                    editMerchant.MerchantPrincipal.Contact.Address.City = lblNewValue.Text;
                                }
                                else
                                {
                                    AddChangeToAudit(editMerchant.RecordId, lblFieldName.Text, "", lblNewValue.Text);

                                    AddressModel newContactAddress = new AddressModel();
                                    newContactAddress.City = lblNewValue.Text;
                                    ctx.Addresses.Add(newContactAddress);
                                    editMerchant.MerchantPrincipal.Contact.Address = newContactAddress;
                                }
                            }
                            else
                            {
                                if (editMerchant.MerchantPrincipal.Contact.Address != null)
                                {
                                    txtPrincipalCity.Text = editMerchant.MerchantPrincipal.Contact.Address.City;
                                }
                                else
                                {
                                    txtPrincipalCity.Text = "";
                                }
                            }
                        }

                        if (lblFieldName.Text == "Zip")
                        {
                            if (cbConfirmed.Checked)
                            {
                                if (editMerchant.MerchantPrincipal.Contact.Address != null)
                                {
                                    AddChangeToAudit(editMerchant.RecordId, lblFieldName.Text, editMerchant.MerchantPrincipal.Contact.Address.Zip, lblNewValue.Text);

                                    editMerchant.MerchantPrincipal.Contact.Address.Zip = lblNewValue.Text;
                                }
                                else
                                {
                                    AddChangeToAudit(editMerchant.RecordId, lblFieldName.Text, "", lblNewValue.Text);

                                    AddressModel newContactAddress = new AddressModel();
                                    newContactAddress.Zip = lblNewValue.Text;
                                    ctx.Addresses.Add(newContactAddress);
                                    editMerchant.MerchantPrincipal.Contact.Address = newContactAddress;
                                }
                            }
                            else
                            {
                                if (editMerchant.MerchantPrincipal.Contact.Address != null)
                                {
                                    txtPrincipalZip.Text = editMerchant.MerchantPrincipal.Contact.Address.Zip;
                                }
                                else
                                {
                                    txtPrincipalZip.Text = "";
                                }
                            }
                        }

                        if (lblFieldName.Text == "State")
                        {
                            if (cbConfirmed.Checked)
                            {
                                if (editMerchant.MerchantPrincipal.Contact.Address != null && editMerchant.MerchantPrincipal.Contact.Address.State != null)
                                {
                                    AddChangeToAudit(editMerchant.RecordId, lblFieldName.Text, editMerchant.MerchantPrincipal.Contact.Address.State.Name, lblNewValue.Text);

                                    editMerchant.MerchantPrincipal.Contact.Address.State = ctx.GeoStates.First(gs => gs.Name == lblNewValue.Text);
                                }
                                else
                                {
                                    AddChangeToAudit(editMerchant.RecordId, lblFieldName.Text, "", lblNewValue.Text);

                                    AddressModel newContactAddress = new AddressModel();
                                    newContactAddress.State = ctx.GeoStates.First(x => x.Name == lblNewValue.Text);
                                    ctx.Addresses.Add(newContactAddress);
                                    editMerchant.MerchantPrincipal.Contact.Address = newContactAddress;
                                }
                            }
                            else
                            {
                                if (editMerchant.MerchantPrincipal.Contact.Address.State != null)
                                {
                                    ddlPrincipalState.SelectedValue = editMerchant.MerchantPrincipal.Contact.Address.State.RecordId.ToString();
                                }
                                else
                                {
                                    ddlPrincipalState.SelectedIndex = -1;
                                }
                            }
                        }

                        if (lblFieldName.Text == "Home Phone")
                        {
                            if (cbConfirmed.Checked)
                            {
                                AddChangeToAudit(editMerchant.RecordId, lblFieldName.Text, editMerchant.MerchantPrincipal.Contact.HomePhone, lblNewValue.Text);

                                editMerchant.MerchantPrincipal.Contact.HomePhone = lblNewValue.Text;
                            }
                            else
                            {
                                txtPrincipalHomePhone.Text = editMerchant.MerchantPrincipal.Contact.HomePhone;
                            }
                        }

                        if (lblFieldName.Text == "Cell Phone")
                        {
                            if (cbConfirmed.Checked)
                            {
                                AddChangeToAudit(editMerchant.RecordId, lblFieldName.Text, editMerchant.MerchantPrincipal.Contact.CellPhone, lblNewValue.Text);

                                editMerchant.MerchantPrincipal.Contact.CellPhone = lblNewValue.Text;
                            }
                            else
                            {
                                txtPrincipalCellPhone.Text = editMerchant.MerchantPrincipal.Contact.CellPhone;
                            }
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                string userId = "";

                if (Request.IsAuthenticated)
                {
                    userId = Page.User.Identity.GetUserId();
                }
                _newLogic.WriteExceptionToDB(ex, "SavePrincipalChanges", 0, editMerchant.RecordId, userId);
            }
        }
        private void SaveBankingChanges(MerchantModel editMerchant, ApplicationDbContext ctx)
        {
            try
            {
                foreach (RepeaterItem rItem in rptrBankingChanges.Items)
                {
                    Label lblFieldName = (Label)rItem.FindControl("lblFieldName3");
                    Label lblNewValue = (Label)rItem.FindControl("lblNewValue3");
                    CheckBox cbConfirmed = (CheckBox)rItem.FindControl("cbConfirmed3");

                    if (lblFieldName != null && lblNewValue != null && cbConfirmed != null)
                    {
                        if (lblFieldName.Text == "Bank Name")
                        {
                            if (cbConfirmed.Checked)
                            {
                                AddChangeToAudit(editMerchant.RecordId, lblFieldName.Text, editMerchant.BankAccount.Bank.BankName, lblNewValue.Text);

                                editMerchant.BankAccount.Bank.BankName = lblNewValue.Text.Trim();
                            }
                            else
                            {
                                txtBankName.Text = editMerchant.BankAccount.Bank.BankName;
                            }
                        }

                        if (lblFieldName.Text == "Bank City")
                        {
                            if (cbConfirmed.Checked)
                            {
                                AddChangeToAudit(editMerchant.RecordId, lblFieldName.Text, editMerchant.BankAccount.Bank.BankCity, lblNewValue.Text);

                                editMerchant.BankAccount.Bank.BankCity = lblNewValue.Text.Trim();
                            }
                            else
                            {
                                txtBankCity.Text = editMerchant.BankAccount.Bank.BankCity;
                            }
                        }

                        if (lblFieldName.Text == "Bank Phone")
                        {
                            if (cbConfirmed.Checked)
                            {
                                AddChangeToAudit(editMerchant.RecordId, lblFieldName.Text, editMerchant.BankAccount.Bank.BankPhone, lblNewValue.Text);

                                editMerchant.BankAccount.Bank.BankPhone = lblNewValue.Text.Trim();
                            }
                            else
                            {
                                txtBankPhone.Text = editMerchant.BankAccount.Bank.BankPhone;
                            }
                        }

                        if (lblFieldName.Text == "Bank State")
                        {
                            if (cbConfirmed.Checked)
                            {
                                AddChangeToAudit(editMerchant.RecordId, lblFieldName.Text, editMerchant.BankAccount.Bank.BankState.Name, lblNewValue.Text);

                                editMerchant.BankAccount.Bank.BankState = ctx.GeoStates.First(gs => gs.Name == lblNewValue.Text);
                            }
                            else
                            {
                                ddlBankState.SelectedValue = editMerchant.BankAccount.Bank.BankState.RecordId.ToString();
                            }
                        }

                        if (lblFieldName.Text == "Debit Card")
                        {
                            if (cbConfirmed.Checked)
                            {
                                string oldDebitCard = MaskDebitCard(editMerchant.DebitCard.DebitCardNumber, editMerchant.DebitCard.RecordId);

                                Logic.Crypt crypto1 = new Logic.Crypt();
                                crypto1.CryptType = "Card";
                                crypto1.CryptData = Convert.ToBase64String(PWDTK.StringToUtf8Bytes(lblNewValue.Text));
                                crypto1.Purpose = "9c443c6a-87d5-47cc-9c4d-abf6a2bd2e06";
                                crypto1.AdditionalData = editMerchant.DebitCard.RecordId.ToString();

                                byte[] newDebitCard = crypto1.Protect();

                                editMerchant.DebitCard.DebitCardNumber = newDebitCard;
                                editMerchant.DebitCard.DebitCardFirst6 = txtDebitCardNumber.Text.Substring(0, 6);
                                editMerchant.DebitCard.DebitCardLast4 = txtDebitCardNumber.Text.Substring(txtDebitCardNumber.Text.Length - 4, 4);
                                editMerchant.DebitCard.RemainingDigits = Convert.ToInt16(txtDebitCardNumber.Text.Length - 10);

                                AddChangeToAudit(editMerchant.RecordId, lblFieldName.Text, oldDebitCard, MaskDebitCard(newDebitCard, editMerchant.DebitCard.RecordId));
                            }
                            else
                            {
                                txtDebitCardNumber.Text = MaskDebitCard(editMerchant.DebitCard.DebitCardNumber, editMerchant.DebitCard.RecordId);
                            }

                            txtDebitCardNumber.Enabled = false;
                        }

                        if (lblFieldName.Text == "Account Number")
                        {
                            if (cbConfirmed.Checked)
                            {
                                string oldAccNum = MaskAccountNumber(editMerchant.BankAccount.AccountNumber, editMerchant.BankAccount.RecordId);

                                Logic.Crypt crypto2 = new Logic.Crypt();
                                crypto2.CryptType = "Account";
                                crypto2.CryptData = Convert.ToBase64String(PWDTK.StringToUtf8Bytes(lblNewValue.Text.Trim()));
                                crypto2.Purpose = "4742b06b-9e12-48cf-9034-aadb15dc7a58";
                                crypto2.AdditionalData = editMerchant.BankAccount.RecordId.ToString();

                                byte[] newAccNum = crypto2.Protect();

                                editMerchant.BankAccount.AccountNumber = newAccNum;
                                editMerchant.BankAccount.AccountNumberLast4 = txtAccountNumber.Text.Substring(txtAccountNumber.Text.Length - 4, 4);
                                editMerchant.BankAccount.RemainingDigits = Convert.ToInt16(txtAccountNumber.Text.Length - 4);

                                AddChangeToAudit(editMerchant.RecordId, lblFieldName.Text, oldAccNum, MaskAccountNumber(newAccNum, editMerchant.BankAccount.RecordId));
                            }
                            else
                            {
                                txtAccountNumber.Text = MaskAccountNumber(editMerchant.BankAccount.AccountNumber, editMerchant.BankAccount.RecordId);
                            }

                            txtAccountNumber.Enabled = false;
                        }

                        if (lblFieldName.Text == "Exp Month")
                        {
                            if (cbConfirmed.Checked)
                            {
                                AddChangeToAudit(editMerchant.RecordId, lblFieldName.Text, editMerchant.DebitCard.DebitCardExpMonth, lblNewValue.Text);

                                editMerchant.DebitCard.DebitCardExpMonth = lblNewValue.Text;
                            }
                            else
                            {
                                ddlExpMonth.SelectedValue = editMerchant.DebitCard.DebitCardExpMonth.ToString();
                            }

                            ddlExpMonth.Enabled = false;
                        }

                        if (lblFieldName.Text == "Exp Year")
                        {
                            if (cbConfirmed.Checked)
                            {
                                AddChangeToAudit(editMerchant.RecordId, lblFieldName.Text, editMerchant.DebitCard.DebitCardExpYear, lblNewValue.Text);

                                editMerchant.DebitCard.DebitCardExpYear = lblNewValue.Text;
                            }
                            else
                            {
                                ddlExpYear.SelectedValue = editMerchant.DebitCard.DebitCardExpYear.ToString();
                            }

                            ddlExpYear.Enabled = false;
                        }

                        if (lblFieldName.Text == "Routing Number")
                        {
                            if (cbConfirmed.Checked)
                            {
                                AddChangeToAudit(editMerchant.RecordId, lblFieldName.Text, editMerchant.BankAccount.RoutingNumber, lblNewValue.Text);

                                editMerchant.BankAccount.RoutingNumber = lblNewValue.Text.Trim();
                            }
                            else
                            {
                                txtRoutingNumber.Text = editMerchant.BankAccount.RoutingNumber;
                            }

                            txtRoutingNumber.Enabled = false;
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                string userId = "";

                if (Request.IsAuthenticated)
                {
                    userId = Page.User.Identity.GetUserId();
                }
                _newLogic.WriteExceptionToDB(ex, "SaveBankingChanges", 0, editMerchant.RecordId, userId);
            }
        }