private void btnAddContact_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (btnAddContact.Content.ToString() != "Add")
                {
                    if (lblContact.Content == "?" || String.IsNullOrWhiteSpace(txtContact.Text))
                    {
                        System.Windows.MessageBox.Show("Please input correct format and/or fill all required fields");
                        return;
                    }
                }

                if (btnAddContact.Content.ToString() == "Add")
                {
                    grpContact.Visibility = Visibility.Visible;
                    btnAddContact.Content = "Save";
                    btnEdtContact.Content = "Cancel";
                    btnEdtContact.IsEnabled = true;
                    btnDelContact.Visibility = Visibility.Hidden;

                    tbAddress.IsEnabled = false;
                    tbContact.IsEnabled = false;
                    tbInfo.IsEnabled = false;

                }
                else if (btnAddContact.Content.ToString() == "Save")
                {


                    //for view
                    if (status == "View")
                    {
                        using (var ctx = new finalContext())
                        {
                            int ctr = ctx.AgentContacts.Count() + 1;
                            AgentContact con = new AgentContact { AgentID = aId, CNumber = ctr, Contact = txtContact.Text };
                            ctx.AgentContacts.Add(con);
                            ctx.SaveChanges();
                            var cts = from ct in ctx.AgentContacts
                                      where ct.AgentID == aId
                                      select new { ContactNumberID = ct.CNumber, Contact = ct.Contact };
                            dgContact.ItemsSource = cts.ToList();
                        }

                        reset();
                        return;

                    }

                    using (var ctx = new finalContext())
                    {
                        int ctr = ctx.TempAgentContact.Count() + 1;
                        TempAgentContact con = new TempAgentContact { CNumber = ctr, Contact = txtContact.Text };
                        ctx.TempAgentContact.Add(con);
                        ctx.SaveChanges();
                        var cts = from ct in ctx.TempAgentContact
                                  select new { ContactNumberID = ct.CNumber, Contact = ct.Contact };
                        dgContact.ItemsSource = cts.ToList();
                    }

                    reset();
                }
                else
                {
                    //for view
                    if (status == "View")
                    {
                        using (var ctx = new finalContext())
                        {

                            int num = Convert.ToInt32(getRow(dgContact, 0));
                            var con = ctx.AgentContacts.Where(x => x.CNumber == num && x.AgentID == aId).First();
                            con.Contact = txtContact.Text;
                            ctx.SaveChanges();
                            var cts = from ct in ctx.AgentContacts
                                      where ct.AgentID == aId
                                      select new { ContactNumberID = ct.CNumber, Contact = ct.Contact };
                            dgContact.ItemsSource = cts.ToList();

                        }
                        reset();
                        return;
                    }


                    using (var ctx = new finalContext())
                    {
                        int num = Convert.ToInt32(getRow(dgContact, 0));
                        var con = ctx.TempAgentContact.Where(x => x.CNumber == num).First();
                        con.Contact = txtContact.Text;
                        ctx.SaveChanges();
                        var cts = from ct in ctx.TempAgentContact
                                  select new { ContactNumberID = ct.CNumber, Contact = ct.Contact };
                        dgContact.ItemsSource = cts.ToList();
                        reset();

                    }
                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show("Runtime Error: " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
        }
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (lblFName.Content == "?" || lblLName.Content == "?" || lblMI.Content == "?" || lblEmail.Content == "?" || lblSuffix.Content == "?"
                    || String.IsNullOrWhiteSpace(txtFName.Text) || String.IsNullOrWhiteSpace(txtLName.Text))
                {
                    System.Windows.MessageBox.Show("Please input correct format and/or fill all required fields");
                    return;
                }

                if (status == "Add")
                {

                    DialogResult dr = System.Windows.Forms.MessageBox.Show("Are you sure you want to add this record?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (dr == System.Windows.Forms.DialogResult.No)
                    {
                        return;
                    }

                    if (txtFName.Text == "" || txtLName.Text == "")
                    {
                        System.Windows.MessageBox.Show("Please complete the required information", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                        return;
                    }

                    if (!Regex.IsMatch(txtFName.Text, @"^[a-zA-Z]+$"))
                    {

                    }


                    using (var ctx = new finalContext())
                    {
                        Agent agt = new Agent { FirstName = txtFName.Text, LastName = txtLName.Text,  Suffix = txtSuffix.Text, MI = txtMI.Text, Active = true,  Email = txtEmail.Text, Photo = ConvertImageToByteArray(selectedFileName) };

                        var query = from con in ctx.TempAgentAddresses
                                    select con;
                        foreach (var item in query)
                        {
                            AgentAddress add = new AgentAddress { AddressNumber=item.AddressNumber, Street = item.Street, City = item.City, Province = item.Province };
                            ctx.AgentAddresses.Add(add);
                        }

                        var query1 = from con in ctx.TempAgentContact
                                     select con;
                        foreach (var item in query1)
                        {
                            AgentContact con = new AgentContact { CNumber=item.CNumber, Contact = item.Contact };
                            ctx.AgentContacts.Add(con);
                        }
                        AuditTrail at = new AuditTrail { EmployeeID = UserID, DateAndTime = DateTime.Now, Action = "Added new Agent " + agt.FirstName + " " + agt.MI + " " + agt.LastName + " " + agt.Suffix};
                        ctx.AuditTrails.Add(at);
                        ctx.Agents.Add(agt);
                        ctx.SaveChanges();
                        System.Windows.MessageBox.Show("Added New Agent");
                        this.Close();


                    }
                }
                else
                {
                    
                    DialogResult dr = System.Windows.Forms.MessageBox.Show("Are you sure you want to update this record?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (dr == System.Windows.Forms.DialogResult.No)
                    {
                        return;
                    }
                    using (var ctx = new finalContext())
                    {
                        var agt = ctx.Agents.Find(aId);
                        agt.FirstName = txtFName.Text;
                        agt.LastName = txtLName.Text;
                        agt.Suffix = txtSuffix.Text;
                        agt.MI = txtMI.Text;
                        agt.Email = txtEmail.Text;
                        if (isChanged == true)
                        {
                            agt.Photo = ConvertImageToByteArray(selectedFileName);
                        }
                        AuditTrail at = new AuditTrail { EmployeeID = UserID, DateAndTime = DateTime.Now, Action = "Updated Agent " + agt.FirstName + " " + agt.MI + " " + agt.LastName + " " + agt.Suffix };
                        ctx.AuditTrails.Add(at);
                        ctx.SaveChanges();
                        System.Windows.MessageBox.Show("Agent Updated", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                        this.Close();
                    }
                     
                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show("Runtime Error: " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
        }