protected void btnRegister_Click(object sender, EventArgs e)
        {
            int accNum = int.Parse(txtAccountNumber.Text);

            var _db = new CustomerContext();
            var query = (from c in _db.Customers
                         join a in _db.Accounts
                         on c.CustomerID equals a.CustomerID
                         where a.AccountNumber == accNum && c.Email == txtEmail.Text
                         select c);

            foreach (Customer cust in query)
            {
                if (cust.OnlineCustomer == false)
                {
                    cust.OnlineCustomer = true;
                    cust.UserName = txtUserName.Text;
                    cust.CustomerPassword = txtPassword.Text;

                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Record Inserted Successfully')", true);
                }
                else
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Record Inserted Unsuccessfully')", true);
                }
            }

            _db.SaveChanges();
        }
        protected void btnLogin_Click(object sender, EventArgs e)
        {

            using (var _db = new CustomerContext())
            {
                try
                {
                    Customer currentCustomer = (from c in _db.Customers
                                                where c.UserName == txtUsername.Text && c.CustomerPassword == txtPassword.Text
                                                select c).First();

                    if (currentCustomer.UserName == txtUsername.Text && currentCustomer.CustomerPassword == txtPassword.Text)
                    {
                        CustomSessionObject.Current.SessionUsername = txtUsername.Text.Trim();
                        CustomSessionObject.Current.LoginStatus = true;
                        Response.Redirect("LoggedIn.aspx", true);
                    }
                    else
                    {
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Login Unsuccessful')", true);
                        CustomSessionObject.Current.LoginStatus = false;
                    }
                }
                catch (Exception ex)
                {
                    FireBugWriter.Write(ex.Message);
                }
            }
        }
        protected void btnRecover_Click(object sender, EventArgs e)
        {
            using (var _db = new CustomerContext())
            {
                try
                {
                    var password = (from p in _db.Customers
                                    where p.UserName == txtUsername.Text && p.Email == txtEmail.Text
                                    select new { p.CustomerPassword }).First();

                    using (MailMessage message = new MailMessage())
                    {
                        message.From = new MailAddress("*****@*****.**");
                        message.To.Add(new MailAddress(txtEmail.Text));
                        message.Subject = "Password Recovery";
                        message.Body = "You have recieved this email because you have forgot your password." + Environment.NewLine +
                                       "Your password is " + password.CustomerPassword + Environment.NewLine +
                                       "Please login using this password";
                    }

            
                    
                }
                catch (Exception ex)
                {
                    FireBugWriter.Write(ex.Message);
                }
            }
        }
 public List<Transaction> GetTransactions()
 {
     using (var cxt = new CustomerContext())
     {
         List<Transaction> allTransactions = cxt.Transactions.ToList();
         return allTransactions;
     }
 }
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
            Response.Cache.SetNoStore();

            if (CustomSessionObject.Current.SessionUsername == null)
            {
                Response.Redirect("ErrorPage.aspx", true);
            }


            if (!IsPostBack)
            {
                DisplaySessionValue();
                LoadAccountNumber();
                string username = CustomSessionObject.Current.SessionUsername;


                using (var _db = new CustomerContext())
                {
                    try
                    {
                        var match = (from c in _db.Customers
                                     join a in _db.Accounts on c.CustomerID equals a.CustomerID
                                     where c.UserName == username
                                     select new
                                     {
                                         c.FirstName,
                                         c.Surname,
                                         a.AccountNumber,
                                         c.AddressLine1,
                                         c.AddressLine2,
                                         c.City,
                                         c.County,
                                         c.CustomerID,
                                         a.AccountType,
                                         a.Balance
                                     }).FirstOrDefault();

                        lblName.Text = match.FirstName + " " + match.Surname;
                        lblAccountNumber.Text = match.AccountNumber.ToString();
                        lblAddressLine1.Text = match.AddressLine1;
                        lblAddressLine2.Text = match.AddressLine2;
                        lblCity.Text = match.City;
                        lblCounty.Text = match.County;

                        lblAccountNum.Text = match.AccountNumber.ToString();
                        lblAccountType.Text = match.AccountType;
                        lblAccountBalance.Text = CurrencyFormat(match.Balance);
                    }
                    catch (Exception ex)
                    {
                        FireBugWriter.Write(ex.Message);
                    }
                }
            }
        }
        public IQueryable<Branch> GetBranches()
        {
            IQueryable<Branch> query = null;
            var cxt = new CustomerContext();
            try
            {
                query = cxt.Branches;
            }
            catch (Exception ex)
            {
                FireBugWriter.Write(ex.Message);
            }

            return query;
        }
        private void PrimeCustomerDropdowns()
        {
            using (var cxt = new CustomerContext())
            {
                try
                {
                    List<Customer> customerList = cxt.Customers.ToList();

                    foreach (Customer customer in customerList)
                    {
                        ListItem item = new ListItem(customer.FirstName + " " + customer.Surname, customer.CustomerID.ToString());
                        cboCustomerDetails.Items.Add(item);
                        cboCustomers.Items.Add(item);
                        cboViewCustomers.Items.Add(item);
                    }
                }
                catch (Exception ex)
                {
                    FireBugWriter.Write(ex.Message);
                }
            }
        }
        private void PrimeAccountDropDown()
        {
            int customerID = int.Parse(cboCustomerDetails.SelectedValue);

            using (var cxt = new CustomerContext())
            {
                try
                {
                    var accounts = cxt.Accounts.Where(a => a.CustomerID == customerID);

                    foreach (var account in accounts)
                    {
                        ListItem item = new ListItem(account.AccountNumber.ToString());
                        cboAccounts.Items.Add(item);
                    }
                }
                catch (Exception ex)
                {
                    FireBugWriter.Write(ex.Message);
                }
            }
        }
        private void SerializeCustomerInfo(StreamWriter writer)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(Customer));
            int customerId = int.Parse(cboViewCustomers.SelectedValue);

            using (var cxt = new CustomerContext())
            {
                try
                {
                    Customer customer = cxt.Customers.Where(c => c.CustomerID == customerId).First();
                    xmlSerializer.Serialize(writer, customer);
                }
                catch (Exception ex)
                {
                    FireBugWriter.Write(ex.Message);
                }
            }
        }
        private void DeleteAccount()
        {
            int accountNumber = int.Parse(cboAccounts.SelectedItem.ToString());

            using (var cxt = new CustomerContext())
            {
                try
                {
                    cxt.Accounts.Remove(cxt.Accounts.Where(a => a.AccountNumber == accountNumber).First());
                    cxt.SaveChanges();
                    cboAccounts.Visible = false;
                }
                catch (Exception ex)
                {
                    FireBugWriter.Write(ex.Message);
                }
            }
        }
        private void RemoveCustomer()
        {
            bool hasAccounts = false;

            using (var cxt = new CustomerContext())
            {
                int customerID = int.Parse(cboCustomers.SelectedValue);

                try
                {
                    var customerWithAccounts = (from c in cxt.Customers
                                                join a in cxt.Accounts
                                                on c.CustomerID equals a.CustomerID
                                                where c.CustomerID == customerID
                                                select a.AccountNumber);

                    foreach (var account in customerWithAccounts)
                    {
                        if (account == 0)
                            hasAccounts = false;
                        else
                            hasAccounts = true;
                    }

                    if (hasAccounts)
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alert", "alert('Customer has open Accounts!')", true);
                    else
                    {
                        cxt.Customers.Remove(cxt.Customers.Where(c => c.CustomerID == customerID).First());
                        cxt.SaveChanges();
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alert", "alert('Customer Record Deleted.')", true);
                    }
                }
                catch (Exception ex)
                {
                    FireBugWriter.Write(ex.Message);
                }
            }

        }
        private void AddCustomer()
        {
            Customer newCustomer = new Customer();

            newCustomer.FirstName = txtFname.Text;
            newCustomer.Surname = txtLname.Text;
            newCustomer.Email = txtEmail.Text;
            newCustomer.Phone = txtPhone.Text;
            newCustomer.AddressLine1 = txtAddress1.Text;
            newCustomer.AddressLine2 = txtAddress2.Text;
            newCustomer.City = txtCity.Text;
            newCustomer.County = cboCounties.SelectedItem.ToString();

            using (var cxt = new CustomerContext())
            {
                try
                {
                    cxt.Customers.Add(newCustomer);
                    cxt.SaveChanges();
                }
                catch (Exception ex)
                {
                    FireBugWriter.Write(ex.Message);
                }
            }
        }
        protected void btnTransfer_Click(object sender, EventArgs e)
        {
            bool success = false;

            int fromAccount, toAccount, amount;
            string description = txtDescription.Text;
            string transType = "Transfer";
            DateTime stamp = DateTime.Now;
            string transRef = "Online Transfer";

            fromAccount = int.Parse(txtAccountNumber.Text);
            toAccount = int.Parse(txtToAccountNumber.Text);
            amount = int.Parse(txtAmount.Text);

            
                Transaction newTransaction = new Transaction();
                newTransaction.TransactionAccountNumber = fromAccount;
                newTransaction.DestinationAccountNumber = toAccount;
                newTransaction.TransactionType = transType;
                newTransaction.TransactionReference = transRef;
                newTransaction.TransactionAmount = amount;
                newTransaction.TransactionDateTime = stamp;
                newTransaction.TransactionDescription = description;

                using (var _db = new CustomerContext())
                {
                    try
                    {
                        _db.Transactions.Add(newTransaction);
                        success = true;
                        _db.SaveChanges();
                    }
                    catch (Exception ex)
                    {
                        FireBugWriter.Write(ex.Message);
                    }
                }


                using (var db = new CustomerContext())
                {
                    int deductAmount = int.Parse(txtAmount.Text);
                    int accountNumber = int.Parse(txtAccountNumber.Text);
                    try
                    {
                        var query = (from a in db.Accounts
                                     where a.AccountNumber == accountNumber
                                     select a).First();

                        int newBalance = query.Balance - deductAmount;
                        query.Balance = newBalance;
                        db.SaveChanges();
                    }
                    catch (Exception ex)
                    {
                        FireBugWriter.Write(ex.Message);
                    }
                }
                using (var db = new CustomerContext())
                {
                    int addAmount = int.Parse(txtAmount.Text);
                    int destinationAccount = int.Parse(txtToAccountNumber.Text);
                    try
                    {
                        var query = (from a in db.Accounts
                                     where a.AccountNumber == destinationAccount
                                     select a).First();

                        int newBalance = query.Balance + addAmount;
                        query.Balance = newBalance;
                        db.SaveChanges();
                    }
                    catch (Exception ex)
                    {
                        FireBugWriter.Write(ex.Message);
                    }
                }
                if (success)
                {
                    txtAmount.Text = string.Empty;
                    txtDescription.Text = string.Empty;
                    txtReference.Text = string.Empty;
                    txtToAccountNumber.Text = string.Empty;
                }
        }
        public void LoadAccountNumber()
        {
            string username = CustomSessionObject.Current.SessionUsername;

            using (var _db = new CustomerContext())
            {
                try
                {
                    var query = (from c in _db.Customers
                                 join a in _db.Accounts on c.CustomerID equals a.CustomerID
                                 where c.UserName == username
                                 select new { a.AccountNumber }).FirstOrDefault();

                    txtAccountNumber.Text = query.AccountNumber.ToString();
                    txtExtAccountNumber.Text = query.AccountNumber.ToString();
                }
                catch (Exception ex)
                {
                    FireBugWriter.Write(ex.Message);
                }
            }
        }
        protected void btnTransfer_Click(object sender, EventArgs e)
        {
            bool success = false;

            int fromAccount, toAccount, amount;
            string description = txtDescription.Text;
            string transType = "Transfer";
            DateTime stamp = DateTime.Now;
            string transRef = "Online Transfer";

            fromAccount = int.Parse(txtFromAccount.Text);
            toAccount = int.Parse(txtToAccount.Text);
            amount = int.Parse(txtAmount.Text);

            
                Transaction newTransaction = new Transaction();
                newTransaction.TransactionAccountNumber = fromAccount;
                newTransaction.DestinationAccountNumber = toAccount;
                newTransaction.TransactionType = transType;
                newTransaction.TransactionReference = transRef;
                newTransaction.TransactionAmount = amount;
                newTransaction.TransactionDateTime = stamp;
                newTransaction.TransactionDescription = description;

                using (var _db = new CustomerContext())
                {
                    try
                    {
                        _db.Transactions.Add(newTransaction);
                        success = true;
                        _db.SaveChanges();
                        UpdateBalance();

                        if (success)
                        {
                            txtFromAccount.Text = string.Empty;
                            txtToAccount.Text = string.Empty;
                            txtAmount.Text = string.Empty;
                        }
                    }
                    catch (Exception ex)
                    {
                        FireBugWriter.Write(ex.Message);
                    }
                }
        }
        public static List<Transaction> GetTransactions()
        {
            using (var _db = new CustomerContext())
            {
                try
                {
                    var query = (from t in _db.Transactions
                                 join a in _db.Accounts on t.TransactionAccountNumber equals a.AccountNumber
                                 join c in _db.Customers on a.CustomerID equals c.CustomerID
                                 where c.CustomerID == a.CustomerID
                                 select t).ToList();

                    return query.ToList();
                }
                catch (Exception ex)
                {
                    FireBugWriter.Write(ex.Message);
                }
            }
        }
        private void ViewCustomerDetails()
        {
            int id = int.Parse(cboViewCustomers.SelectedValue);

            using (var cxt = new CustomerContext())
            {
                try
                {
                    Customer currentCustomer = cxt.Customers.Where(c => c.CustomerID == id).First();

                    lblDisplayFname.Text = currentCustomer.FirstName;
                    lblDisplayLname.Text = currentCustomer.Surname;
                    lblDisplayEmail.Text = currentCustomer.Email;
                    lblDisplayPhone.Text = currentCustomer.Phone;
                    lblDisplayAddress1.Text = currentCustomer.AddressLine1;
                    lblDisplayAddress2.Text = currentCustomer.AddressLine2;
                    lblDisplayCity.Text = currentCustomer.City;
                    lblDisplayCounty.Text = currentCustomer.County;
                }
                catch (Exception ex)
                {
                    FireBugWriter.Write(ex.Message);
                }
            }
        }
        public void UpdateBalance()
        {
            using (var db = new CustomerContext())
            {
                try
                {
                    int deductAmount = int.Parse(txtAmount.Text);
                    int accountNumber = int.Parse(txtFromAccount.Text);

                    var query = (from a in db.Accounts
                                 where a.AccountNumber == accountNumber
                                 select a).First();

                    int newBalance = query.Balance - deductAmount;
                    query.Balance = newBalance;
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    FireBugWriter.Write(ex.Message);
                }
            }

            using (var db = new CustomerContext())
            {
                int addAmount = int.Parse(txtAmount.Text);
                int destinationAccount = int.Parse(txtToAccount.Text);
                try
                {
                    var query = (from a in db.Accounts
                                 where a.AccountNumber == destinationAccount
                                 select a).First();

                    int newBalance = query.Balance + addAmount;
                    query.Balance = newBalance;
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    FireBugWriter.Write(ex.Message);
                }
            }
            Response.Redirect(Request.RawUrl);
        }