protected void Button2_Click(object sender, EventArgs e)
        {
            int empaddId = 0;
            int employee_id = Convert.ToInt32(Request.Cookies["empid"].Value);

                using (lucia_dataEntities allData = new lucia_dataEntities())
                {
                    var updateE = from emp in allData.Employees
                                  where employee_id == emp.id
                                  select emp;
                    foreach (var r in updateE)
                    {
                        r.job_description = jobDescription.Text;
                        r.work_phone = phone.Text;
                        if(!String.IsNullOrEmpty(emaila.Text))
                        r.email = emaila.Text;
                       empaddId= r.emp_add_id;
                    }
                    var empadd = from empaddress in allData.Emp_Add
                                 where empaddress.id == empaddId
                                 select empaddress;
                    foreach (var a in empadd)
                    {
                        a.state = state.Text;
                        a.suburb = suburb.Text;
                        a.street = streetName.Text;
                        a.street_number = Convert.ToInt32(streetNumber.Text);
                        a.emp_add_postal_code = Convert.ToInt32(postcode.Text);
                        if (!String.IsNullOrEmpty(unitNumber.Text))
                          a.unit_number = Convert.ToInt32(unitNumber.Text);
                    }
                    allData.SaveChanges();
                    Label11.Text = "Your profile has been sucessfully changed";
                }
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            using (lucia_dataEntities allData = new lucia_dataEntities())
            {
                 int customer_id = Convert.ToInt32(Request.Cookies["customer_id"].Value);
                 var direct = from dir in allData.Direct_Debit
                              where customer_id == dir.customer_id
                              select dir;
                 var credit = from cre in allData.Credit_Card
                              where customer_id == cre.customer_id
                              select cre;

                if (RadioButtonList1.SelectedValue == "Direct Debit")
                {
                    foreach (var r in direct)
                    {
                        allData.DeleteObject(r);
                    }
                    foreach (var c in credit)
                    {
                        allData.DeleteObject(c);
                    }

                    Direct_Debit debitCard = Direct_Debit.CreateDirect_Debit(1, nameofFI.Text, debitAccName.Text, debitType.SelectedValue, Convert.ToInt32(BSB.Text), Convert.ToInt32(debitAccNo.Text), customer_id);
                    allData.AddToDirect_Debit(debitCard);
                    allData.SaveChanges();
                }

                else if (RadioButtonList1.SelectedValue == "Credit Card")
                {
                    foreach (var r in direct)
                    {
                        allData.DeleteObject(r);
                    }
                    foreach (var c in credit)
                    {
                        allData.DeleteObject(c);
                    }
                    Credit_Card creditCard = Credit_Card.CreateCredit_Card(creditCardNo.Text, 14, creditCName.Text, creditType.SelectedValue, eDate.Text, customer_id, 2);
                    allData.AddToCredit_Card(creditCard);
                    allData.SaveChanges();
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                using (lucia_dataEntities alldata = new lucia_dataEntities())
                {
                    int empaddId = 0;
                    int employee_id = Convert.ToInt32(Request.Cookies["empid"].Value);
                    empdeID.Text = employee_id.ToString();
                    var employee = from emp in alldata.Employees
                                   where employee_id == emp.id
                                   select emp;
                    foreach (var r in employee)
                    {
                        empdeID.Text = Convert.ToString(r.id);
                        jobDescription.Text = r.job_description;
                        phone.Text = r.work_phone;
                        emaila.Text = r.email;
                        empaddId = r.emp_add_id;
                    }

                    var empadd = from add in alldata.Emp_Add
                                 where empaddId == add.id
                                 select add;
                    foreach (var a in empadd)
                    {
                        state.Text = a.state;
                        suburb.Text = a.suburb;
                        streetName.Text = a.street;
                        streetNumber.Text = Convert.ToString(a.street_number);
                        unitNumber.Text = Convert.ToString(a.unit_number);
                        postcode.Text = Convert.ToString(a.emp_add_postal_code);
                    }
                }
            }
        }
        //This method should create all related data and insert them accordingly!
        //Associated data tables include: Application, Customer, Trade_reference, Director, Card, Cardholder, Payment (credit or debit), Acknowledgement, Cust_Postal_Address, Dir_Add, Vehicle
        //Because most tables have FKs toward Customer, so Customer will be created first, and Customer.ID will be then used to associate to other tables.
        //Becareful with FK chains!
        //This method is the main body of inserting input data into database.
        //Currently, all optional fields are not inserted!!
        //will put if statements in place in a later version!
        //Please dont forget!
        protected void fillTheForm(object sender, WizardNavigationEventArgs e)
        {
            int defaultID = 0;
            lucia_dataEntities allData = new lucia_dataEntities();
            //Create customer address first;
            Cust_Postal_Address cust_address = Cust_Postal_Address.CreateCust_Postal_Address(defaultID, DropDownList2.Text, txSuburb.Text, txStreet.Text, Convert.ToInt32(txStreetNumber.Text), Convert.ToInt32(txPostCode.Text));
            //assign optional unit number to customer
            if (!String.IsNullOrEmpty(txUnitNumber.Text))
                cust_address.unit_number = Convert.ToInt32(txUnitNumber.Text);
            //add customer address to database set
            allData.AddToCust_Postal_Address(cust_address);

            defaultID++;
            //now create customer, reference cust_address
            Customer customer = Customer.CreateCustomer(defaultID, txBusinessName.Text, txTradingName.Text, Convert.ToInt32(txBusinessYear.Text), txABN.Text, txNatureOperation.Text, cust_address.id, txFirstName.Text, txLastName.Text, txPosition.Text, txPhoneNumber.Text, txMobileNumber.Text, txEmail.Text, Convert.ToDecimal(txCreditLimit.Text), DropDownList1.Text);
            customer.Cust_Postal_Address = cust_address;
            //assign optional middle name for customer
            if (!String.IsNullOrEmpty(txMiddleName.Text))
                customer.contact_middle_name = txMiddleName.Text;
            //assign optional fax
            if (!String.IsNullOrEmpty(txFaxNumber.Text))
                customer.fax_no = txFaxNumber.Text;
            //add customer to database set
            allData.AddToCustomers(customer);

            defaultID++;
            //now create application, use customer.id to reference foreign key
            APJProjv1.Application application = APJProjv1.Application.CreateApplication(defaultID, "submitted", DateTime.Now, 0, 0, 0, 0, customer.id);

            defaultID++;
            //now create trade_reference, this table will include 2 references and 1 existing fuel supplier
            Trade_Reference reference1 = Trade_Reference.CreateTrade_Reference(defaultID, txReferenceName1.Text, txReferencePhone1.Text, txExisting.Text, fuelSPhone.Text, application.id, txReferenceName2.Text, txReferencePhone2.Text);

            defaultID++;
            //now add both items into database
            allData.AddToApplications(application);
            allData.AddToTrade_Reference(reference1);
            //create address item for director 1
            Dir_Add direct_address1 = Dir_Add.CreateDir_Add(defaultID, DropDownList3.Text, stSuburb1.Text, stSName1.Text, Convert.ToInt32(stSNumber1.Text), Convert.ToInt32(stPost1.Text));
            //assign optional unit number for director 1 address
            if (!String.IsNullOrEmpty(stUnit1.Text))
                direct_address1.unit_number = Convert.ToInt32(stUnit1.Text);

            //add dir_add1 to database
            allData.AddToDir_Add(direct_address1);

            defaultID++;
            //create director1's profile, associate him with address1 and customer and application item;
            //PLEASE NOTE!! TEST WHETHER FOREIGN KEYS HAVE BEEN PROPERLY SAVED IN DATABASE!!
            //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            Director director1 = Director.CreateDirector(defaultID, stFName1.Text, stLName1.Text, direct_address1.id, customer.id, application.id);
            //assign optional middle name for director 1
            if (!String.IsNullOrEmpty(stMName1.Text))
                director1.m_name = stMName1.Text;

            //add phone to director 1
            director1.phone = stPhoneNo1.Text;
            //store director1 to database
            allData.AddToDirectors(director1);

            //------
            defaultID++;
            //create address item for director 2
            Dir_Add direct_address2 = Dir_Add.CreateDir_Add(defaultID, DropDownList4.Text, stSuburb2.Text, stSName2.Text, Convert.ToInt32(stSNumber2.Text), Convert.ToInt32(stPost2.Text));
            defaultID++;
            //assign optional unit number for director address 2
            if (!String.IsNullOrEmpty(stUnit2.Text))
                direct_address2.unit_number = Convert.ToInt32(stUnit2.Text);
            //add address 2 to database
            allData.AddToDir_Add(direct_address2);
            defaultID++;
            //create director 2
            Director director2 = Director.CreateDirector(defaultID, stFName2.Text, stLName2.Text, direct_address2.id, customer.id, application.id);
            defaultID++;
            //assign middle name to director 2
            if (!String.IsNullOrEmpty(TextBox7.Text))
                director2.m_name = TextBox7.Text;
            //add phone number to director 2
            director2.phone = stPhoneNo2.Text;
            //add director 2 to database;
            allData.AddToDirectors(director2);

            if (cardOptionList2.SelectedValue == "For Person")
            {
                defaultID++;
                //create a card item first
                Card card = Card.CreateCard(defaultID, 0, nameOnCard.Text, customer.id, "disabled", 1);
                //add card to database
                allData.AddToCards(card);
                //create cardholder item
                //UI does not have last name in place,will duplicate first name twice
                //###################NEED TO BE FIXED!!!#############################
                Cardholder cardholder = Cardholder.CreateCardholder(defaultID, TextBox1.Text, card.card_no);
                //add cardholder to database
                allData.AddToCardholders(cardholder);

                //now create reference table for card and product;
                for (int i = 0; i < CheckBoxList1.Items.Count; i++)
                {
                    if (CheckBoxList1.Items[i].Selected)
                    {
                        Product_Card_Reference rf = Product_Card_Reference.CreateProduct_Card_Reference(card.card_no, CheckBoxList1.Items[i].Text, defaultID);
                        defaultID++;
                        allData.AddToProduct_Card_Reference(rf);
                    }
                }
            }
            //vehicle specific
            else if (cardOptionList2.SelectedValue == "For Vehicle")
            {
                defaultID++;
                //create a card item first
                Card card = Card.CreateCard(defaultID, 0, nameOnCard.Text, customer.id, "disabled", 1);
                //add card to database
                allData.AddToCards(card);
                //create viechel item
                Vehicle vehicle = Vehicle.CreateVehicle(TextBox1.Text, card.card_no);
                //now create reference table for card and product;
                for (int i = 0; i < CheckBoxList1.Items.Count; i++)
                {
                    if (CheckBoxList1.Items[i].Selected)
                    {
                        Product_Card_Reference rf = Product_Card_Reference.CreateProduct_Card_Reference(card.card_no, CheckBoxList1.Items[i].Text, defaultID);
                        defaultID++;
                        allData.AddToProduct_Card_Reference(rf);
                    }
                }
                //add vehicle to database
                allData.AddToVehicles(vehicle);

            }

            //From here, we insert card items!
            //#########################################
            //This section may need further refinement, because current UI for card is not acceptable, we only use it for testing purpose!
            //#########################################
            //person specific
            /*     if (RadioButton1.Checked)
                 {
                     defaultID++;
                     //create a card item first
                     Card card = Card.CreateCard(defaultID, 0, nameOnCard.Text, customer.id, "disabled", 1);
                     //add card to database
                     allData.AddToCards(card);
                     //create cardholder item
                     //UI does not have last name in place,will duplicate first name twice
                     //###################NEED TO BE FIXED!!!#############################
                     Cardholder cardholder = Cardholder.CreateCardholder(defaultID, cdHolderName1.Text,card.card_no);
                     //add cardholder to database
                     allData.AddToCardholders(cardholder);

                     //now create reference table for card and product;
                     for (int i=0;i< CheckBoxList1.Items.Count; i++)
                     {
                         if (CheckBoxList1.Items[i].Selected)
                         {
                             Product_Card_Reference rf = Product_Card_Reference.CreateProduct_Card_Reference(card.card_no, CheckBoxList1.Items[i].Text, defaultID);
                             defaultID++;
                             allData.AddToProduct_Card_Reference(rf);
                         }
                     }
                 }
                 //vehicle specific
                 else if (RadioButton2.Checked)
                 {
                     defaultID++;
                     //create a card item first
                     Card card = Card.CreateCard(defaultID, 0, nameOnCard.Text, customer.id, "disabled", 1);
                     //add card to database
                     allData.AddToCards(card);
                     //create viechel item
                     Vehicle vehicle = Vehicle.CreateVehicle(carReNo1.Text, card.card_no);
                     //now create reference table for card and product;
                     for (int i = 0; i < CheckBoxList1.Items.Count; i++)
                     {
                         if (CheckBoxList1.Items[i].Selected)
                         {
                             Product_Card_Reference rf = Product_Card_Reference.CreateProduct_Card_Reference(card.card_no, CheckBoxList1.Items[i].Text, defaultID);
                             defaultID++;
                             allData.AddToProduct_Card_Reference(rf);
                         }
                     }
                     //add vehicle to database
                     allData.AddToVehicles(vehicle);

                 }*/
            //###################################################
            //########END OF CARD CREATION#######################

            //Create payment item
            if (RadioButtonList1.SelectedValue == debit_card)
            {
                //create a debit card item
                //The first parameter is just an ID
                Direct_Debit debitCard = Direct_Debit.CreateDirect_Debit(defaultID, nameofFI.Text, debitAccName.Text, debitType.SelectedValue, Convert.ToInt32(BSB.Text), Convert.ToInt32(debitAccNo.Text), customer.id);
                //add this payment item to database
                allData.AddToDirect_Debit(debitCard);
                application.payment_option = debit_card;
            }
            else if (RadioButtonList1.SelectedValue == credit_card)
            {
                //create a credit card item
                //#########################
                //UI does not support date selection yet between 14/28
                //###NEED TO MODIFY UI!!############
                Credit_Card creditCard = Credit_Card.CreateCredit_Card(creditCardNo.Text, 14, creditCName.Text, creditType.SelectedValue, eDate.Text, customer.id, defaultID);
                //add item to database
                allData.AddToCredit_Card(creditCard);
                application.payment_option = credit_card;
            }

            allData.SaveChanges();
            Label57.Text = "successful!!";
            allData.Dispose();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            int account_id = Convert.ToInt32(Request.Cookies["account_id"].Value);
            Response.Cookies["viewMode"].Value = "customerEdit";
            Response.Cookies["fromDate"].Value = "";

            Response.Cookies["toDate"].Value = "";
            using (lucia_dataEntities allData = new lucia_dataEntities())
            {
                var result = from account in allData.Accounts
                             where account.id == account_id
                             select account;
                //duplicaton detector
                int i = 0;
                foreach (var r in result)
                {
                    i++;
                    //duplication detected!
                    if (i != 1)
                    {
                        Response.Cookies["error"].Value = "customerLoggedInError: duplicateAccount!";
                        Response.Redirect("~/applicationManagement/ErrorPage.aspx");
                    }
                    //retrieve customer id
                    Response.Cookies["customer_id"].Value = r.Customer.id.ToString();

                    if (r.status == "active")
                    {
                        userName.Text = r.Customer.contact_first_name;
                        credit_limit.Text = r.credit_limit.ToString();
                        //#########################################################
                        //Credit avaliable needs to be implemented!!!!!
                        //####################################################
                        credit_avaliable.Text = r.credit_limit.ToString();
                        Account_status.Text = r.status;

                        //Now set warning message
                        //Warning message includes: card PIN resetted;
                        var PINcards = from card in allData.Cards
                                       where card.pin_reset == 0
                                       where card.cust_id==r.Customer.id
                                       select card;
                        int pin_card_count = 0;
                        foreach (var p in PINcards)
                            pin_card_count++;
                        if (pin_card_count != 0)
                            Warning_message.Text = "You currently have " + pin_card_count.ToString() + " card(s)' PIN resetted, please visit your nearest station to enter new PIN(s)!";
                    }
                    else
                    {
                        userName.Text = r.Customer.contact_first_name;
                        credit_limit.Text = "Not avaliable";
                        //#########################################################
                        //Credit avaliable needs to be implemented!!!!!
                        //####################################################
                        credit_avaliable.Text = "Not avaliable";
                        Account_status.Text = r.status;
                        Warning_message.Text = "Your account is currently inactive, please contact APJ staff for further information.";

                    }
                }
            }
        }
        void Page_PreInit(Object sender, EventArgs e)
        {
            int account_id = Convert.ToInt32(Request.Cookies["account_id"].Value);

            using (lucia_dataEntities allData = new lucia_dataEntities())
            {
                var result = from account in allData.Accounts
                             where account.id == account_id
                             select account;
                int i = 0;
                foreach (var r in result)
                {
                    i++;
                    //duplication detected!
                    if (i != 1)
                    {
                        Response.Cookies["error"].Value = "customerLoggedInError: duplicateAccount!";
                        Response.Redirect("~/applicationManagement/ErrorPage.aspx");
                    }
                    //retrieve customer id
                    Response.Cookies["customer_id"].Value = r.Customer.id.ToString();

                    if (r.status != "active")
                    {
                        this.MasterPageFile = "~/Site4.master";
                    }
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            using (lucia_dataEntities allData = new lucia_dataEntities())
            {

                Response.Cookies["viewMode"].Value = "employeeView";
                int employee_id = Convert.ToInt32(Request.Cookies["empid"].Value);
                var result = from emp in allData.Employees
                             where emp.id == employee_id
                             select emp;
                foreach (var r in result)
                {
                    empName.Text = r.f_name;
                }

                var apps = from app in allData.Applications
                           where app.status == "submitted"
                           select app;
                int count = 0;
                foreach (var a in apps)
                    count++;
                applicationNumber.Text = count.ToString();
            }
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            using (lucia_dataEntities allData = new lucia_dataEntities())
            {
                 int customer_id = Convert.ToInt32(Request.Cookies["customer_id"].Value);
                 var direct = from dir in allData.Direct_Debit
                              where customer_id == dir.customer_id
                              select dir;
                 var credit = from cre in allData.Credit_Card
                              where customer_id == cre.customer_id
                              select cre;

                if (RadioButtonList1.SelectedValue == "Direct Debit")
                {
                    foreach (var r in direct)
                    {
                        allData.DeleteObject(r);
                    }
                    foreach (var c in credit)
                    {
                        allData.DeleteObject(c);
                    }

                    Direct_Debit debitCard = Direct_Debit.CreateDirect_Debit(1, nameofFI.Text, debitAccName.Text, debitType.SelectedValue, Convert.ToInt32(BSB.Text), Convert.ToInt32(debitAccNo.Text), customer_id);
                    allData.AddToDirect_Debit(debitCard);
                    allData.SaveChanges();
                }

                else if (RadioButtonList1.SelectedValue == "Credit Card")
                {
                    foreach (var r in direct)
                    {
                        allData.DeleteObject(r);
                    }
                    foreach (var c in credit)
                    {
                        allData.DeleteObject(c);
                    }
                    Credit_Card creditCard = Credit_Card.CreateCredit_Card(creditCardNo.Text, 14, creditCName.Text, creditType.SelectedValue, eDate.Text, customer_id, 2);
                    allData.AddToCredit_Card(creditCard);
                    allData.SaveChanges();
                }

                if (Request.Cookies["viewMode"].Value.Contains("customer"))
                {
                    operationLog log = operationLog.CreateoperationLog(customer_id, customer_id, "Payment modification", 1,DateTime.Now);
                    allData.AddTooperationLogs(log);
                    allData.SaveChanges();
                }
                else
                {
                    int empid = Convert.ToInt32(Request.Cookies["empid"].Value);
                    operationLog log = operationLog.CreateoperationLog(empid, customer_id, "Payment modification", 1,DateTime.Now);
                    allData.AddTooperationLogs(log);
                    allData.SaveChanges();
                }

                Button2.Enabled = false;
                Label57.Text = "New payment details are successfully saved!";
            }
        }