public string getGuestId()
        {
            ThreeHotelEntities ct = new ThreeHotelEntities();

            Guest guest = new Guest();
            guest = (from x in ct.Guests
                     where x.GuestId.Contains("G")
                     orderby x.GuestId descending
                     select x).First();

            string a = guest.GuestId;
            string b = a.Substring(4, 1);

            int d = Convert.ToInt32(b);
            string e = Convert.ToString(d + 1);
            string c = "";
            if (d + 1 > 999)
                c = "G" + e;
            else if (d + 1 > 99)
                c = "G0" + e;
            else if (d + 1 > 9)
                c = "G00" + e;
            else
                c = "G000" + e;

            return c;
        }
        public Boolean checkMember(string username, string pwd)
        {
            Boolean status = false;
            try
            {
                ThreeHotelEntities ct = new ThreeHotelEntities();

                User guest = new User();
                string ppwd = Encrypt(pwd);
                var q = from x in ct.Users
                        where x.MembershipId == username && x.Password == ppwd
                        select x;
                User u = q.First();

                if (u == null)
                    status = false;
                else
                    status = true;

                return status;
            }
            catch
            {
                return status;
            }
        }
        protected void btnContinue_Click(object sender, EventArgs e)
        {
            int refNo = Convert.ToInt16(txtReservationNo.Text);
            string familyName = txtLastName.Text;
            string guestId = null;

            ThreeHotelEntities ctx = new ThreeHotelEntities();

            Guest guest = ctx.Guests.Where(x=> x.FamilyName == familyName).FirstOrDefault();
            if (guest != null)
            {
                guestId = guest.GuestId.Trim();
            }
            else
            {
                lblMsg.Text = "Invalid Booking Reference No or Family/Last Name";
            }

            Booking booking = ctx.Bookings.Where(x => x.BookingReferenceId == refNo && x.GuestId == guestId).FirstOrDefault();
            if (booking != null && guestId != null)
            {
                Session["bookingRef"] = refNo;
                Response.Redirect("RoomReservations-view.aspx");
            }
            if (booking == null)
            {
                lblMsg.Text = "Invalid Booking Reference No or Family/Last Name";
            }
        }
        protected void btnCancelReservation_Click(object sender, EventArgs e)
        {
            if(ckCancel.Checked == true)
            {
                int bookingRef = Convert.ToInt32(Session["bookingRef"].ToString());

                ThreeHotelEntities ctx = new ThreeHotelEntities();
                //Booking booking = ctx.Bookings.Where(x => x.GuestId == id).FirstOrDefault();
                Booking booking = ctx.Bookings.Where(x => x.BookingReferenceId == bookingRef).FirstOrDefault();
                RoomBooked roomBooked = ctx.RoomBookeds.Where(r => r.BookingReferenceId == bookingRef).FirstOrDefault();
                if (booking != null && roomBooked != null)
                {
                    booking.Status = "Cancelled";
                    roomBooked.Status = "Cancelled";
                    ctx.SaveChanges();
                    lblMsg.Text = "Reservation successfully cancelled.";
                }
                else
                {
                    lblMsg.Text = "Kindly select the check box if you wish to cancel your reservation.";
                }

            }
            else
            {
                lblMsg.Text = "Kindly select the check box if you wish to cancel your reservation.";
            }
        }
        public Guest getByMemberId(string id)
        {
            ThreeHotelEntities ct = new ThreeHotelEntities();
            Guest guest = new Guest();

            guest = (from x in ct.Guests
                     where x.GuestId == id
                     select x).First();

            return guest;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            //ObjectDataSource1.FilterExpression = "Booking Reference = " + Session["bookingRef"].ToString();
            if (Session["bookingRef"] != null)
            {
                int bookingRef = Convert.ToInt16(Session["bookingRef"].ToString());

                //to populate the gridview
                ThreeHotelEntities ctx = new ThreeHotelEntities();
                List<PastTransaction> tl = ctx.PastTransactions.Where(x => x.Booking_Ref == bookingRef).ToList();
                if (tl != null)
                {
                    GridView1.DataSource = tl;
                    GridView1.DataBind();
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Id"] == null)
            {
                Response.Redirect("SignIn.aspx");
            }

            else
            {
                string id = Session["Id"].ToString();
                lblMemberId.Text = id;

                Guest guest = new Guest();
                GuestController guestController = new GuestController();
                guest = guestController.getByMemberId(id);
                string points = Convert.ToString(guest.Points);
                if (points == null)
                {
                    points = "0";
                }
                lblPoints.Text = points;

                string name = guest.Salutation + guest.FirstName + guest.FamilyName;
                lblName.Text = name;

                //to populate the gridview
                ThreeHotelEntities ctx = new ThreeHotelEntities();
                List<PastTransaction> tl = ctx.PastTransactions.Where(x => x.Member_No == id).ToList();
                if (tl != null)
                {
                    GridView1.DataSource = tl;
                    GridView1.DataBind();
                }
                else
                {
                    lblPastTransaction.Text = "No reservations with the hotel has been made.";
                }

            }
        }
        public void MemberUpdate(string id, string salut, string familyName, string fName, string email, string address, string ph, string country)
        {
            ThreeHotelEntities ct = new ThreeHotelEntities();
            Guest g = new Guest();

            g = (from x in ct.Guests
                 where x.GuestId == id
                 select x).First();

            g.Salutation = salut;
            g.FamilyName = familyName;
            g.FirstName = fName;

            g.Email = email;
            g.Address = address;
            g.PhoneNo = ph;
            g.Country = country;

            ct.SaveChanges();
        }
        public string MemberSave(string salut, string familyName, string fName, string pwd, string email, string address, string ph, string country)
        {
            string id;
            ThreeHotelEntities ct = new ThreeHotelEntities();
            Guest g = new Guest();
            User user = new User();

            id = getMemberId();
            g.GuestId = id;

            g.Salutation = salut;
            g.FamilyName = familyName;
            g.FirstName = fName;
            user.MembershipId = id;
            user.Password = Encrypt(pwd);
            g.Email = email;
            g.Address = address;
            g.PhoneNo = ph;
            g.Country = country;

            ct.Guests.Add(g);
            ct.Users.Add(user);
            ct.SaveChanges();

            SendMail(fName, familyName, email, id);
            return id;
        }
        public void MemberChangePwd(string id, string newpwd)
        {
            ThreeHotelEntities ct = new ThreeHotelEntities();
            User g = new User();

            g = (from x in ct.Users
                 where x.MembershipId == id
                 select x).First();

            g.Password = Encrypt(newpwd);

            ct.SaveChanges();
        }
        protected void btnCompleteReservation_Click(object sender, EventArgs e)
        {
            if (Session["Id"] != null)
            {
                member = true;
            }

            ThreeHotelEntities ctx = new ThreeHotelEntities();

            char[] toTrim = { 'm', 'g', '0' };
            string guestID = "";

            //commit to database - Guest

            //if non-member, create new guest by addding row to Guest table
            if (!member)
            {
                Guest addGuest = new Guest();
                var guest = (from x in ctx.Guests where x.GuestId.Contains("G") orderby x.GuestId descending select x).First();

                if (guest != null)
                {
                    int lastNo = Int32.Parse(guest.GuestId.TrimStart("GMgm0".ToCharArray()));
                    int nextNo = lastNo + 1;
                    guestID = "G" + nextNo.ToString().PadLeft(4, '0');
                    addGuest.GuestId = guestID;
                }

                if (ddlSalutation.SelectedValue != string.Empty)
                {
                    addGuest.Salutation = ddlSalutation.SelectedValue;
                }

                if (txtFamilyName.Text != string.Empty)
                    addGuest.FamilyName = txtFamilyName.Text;

                if (txtFirstName.Text != string.Empty)
                    addGuest.FirstName = txtFirstName.Text;

                email = txtEmail.Text;
                if (email != string.Empty)
                    addGuest.Email = email;

                if (txtAddress.Text != string.Empty)
                    addGuest.Address = txtAddress.Text;

                if (txtPhoneNo.Text != string.Empty)
                    addGuest.PhoneNo = txtPhoneNo.Text;

                if (ddlCountry.SelectedValue != string.Empty)
                    addGuest.Country = ddlCountry.SelectedValue;

                ctx.Guests.Add(addGuest);

                ctx.SaveChanges();
            }

            //if member, add points earned to his account
            if(member)
            {
                //MemberID session is saved when member signs in
                guestID = Session["Id"].ToString();

                var guest = ctx.Guests.Where(g => g.GuestId == guestID).FirstOrDefault();
                if (guest != null)
                {
                    int currentPoints = Convert.ToInt16(guest.Points);
                    int newPoints = currentPoints + pointsEarned;
                    guest.Points = newPoints;
                    ctx.SaveChanges();
                }
            }

            //commit to database - Booking
            Booking addBooking = new Booking();

            if (Session["SpecialRateCode"] != string.Empty)
            {
                addBooking.SpecialRateCode = Session["SpecialRateCode"].ToString();
            }

            addBooking.CheckIn = Convert.ToDateTime(Session["CheckInDB"].ToString());
            addBooking.CheckOut = Convert.ToDateTime(Session["CheckOutDB"].ToString());
            addBooking.NoOfRooms = Convert.ToInt16(Session["RoomCount"].ToString());
            addBooking.Tax = taxRate;
            addBooking.ServiceCharge = serviceChargeRate;

            if (member)
            {
                addBooking.PointsEarned = pointsEarned;
            }
            else
            {
                addBooking.PointsEarned = 0;
            }

            addBooking.GuestId = guestID;

            if (txtCardNo.Text != string.Empty)
            {
                addBooking.CreditCardNo = Convert.ToInt64(txtCardNo.Text);
            }

            if (rbtnCardType.SelectedValue != string.Empty)
            {
                addBooking.CreditCardType = rbtnCardType.SelectedValue;
            }

            if (ddlMonth.SelectedValue != string.Empty && ddlYear.SelectedValue != string.Empty)
            {
                addBooking.CardExpiredDate = ddlMonth.SelectedValue + ddlYear.SelectedValue;
            }

            addBooking.TotalCost = totalCost;

            ctx.Bookings.Add(addBooking);
            ctx.SaveChanges();

            //commit to database - RoomBooked
            RoomBooked addRoomBooked = new RoomBooked();
            var booking = (from x in ctx.Bookings orderby x.BookingReferenceId descending select x).First();

            if (booking != null)
            {
                bookingRefId = Convert.ToInt32(booking.BookingReferenceId);
                addRoomBooked.BookingReferenceId = bookingRefId;
            }

            addRoomBooked.RoomType = Session["RoomType"].ToString();
            addRoomBooked.Rate = Convert.ToDouble(Session["Rate"].ToString());

            ctx.RoomBookeds.Add(addRoomBooked);
            ctx.SaveChanges();

            if (txtEmail.Text != string.Empty)
            {
                Session["Email"] = txtEmail.Text;
            }
            Session["BookingRefID"] = bookingRefId;
            Session["TotalCost"] = totalCost;
            Session["Email"] = txtEmail.Text;
            string mailadd = txtEmail.Text;
            Session["CostNights"] = costNights;
            Session["TaxSc"] = taxSC;
            Session["NoOfNights"] = noOfNights;
            if (txtFamilyName.Text != string.Empty)
            {
                Session["FamilyName"] = txtFamilyName.Text;
            }

            SendMail(bookingRefId, mailadd);
            Response.Redirect("RoomReservations-confirmation.aspx");
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if ((string)Session["CheckIn"] == string.Empty)
                {
                    Response.Redirect("RoomReservations-input.aspx");
                }

                //populate search field
                String[] RoomOptions = new[] { "1", "2", "3" };
                foreach (string option in RoomOptions)
                {
                    DropDownListRoomCount.Items.Add(new ListItem(option, option));
                }

                populateByRoom();
                populateByAdult();

                //set search field with previously chosen values
                checkInDatePicker.Text = Session["CheckIn"].ToString();
                checkOutDatePicker.Text = Session["CheckOut"].ToString();
                DropDownListRoomCount.Text = Session["RoomCount"].ToString();
                DropDownListAdult.Text = Session["AdultCount"].ToString();
                DropDownListChildren.Text = Session["ChildrenCount"].ToString();
            }

            //Search database for available rooms
            ThreeHotelEntities ctx = new ThreeHotelEntities();
            var result = ctx.SearchOccupancy(Convert.ToDateTime(Session["CheckInDB"]), Convert.ToDateTime(Session["CheckOutDB"]));

            int[] count = new int[8];
            string[] roomType = new string[8] { "DCK", "DCT", "DSK", "DST", "HCK", "HCT", "HSK", "HST" };

            foreach (var item in result)
            {
                switch (item.RoomType)
                {
                    case "DCK":
                        count[0]++;
                        break;
                    case "DCT":
                        count[1]++;
                        break;
                    case "DSK":
                        count[2]++;
                        break;
                    case "DST":
                        count[3]++;
                        break;
                    case "HCK":
                        count[4]++;
                        break;
                    case "HCT":
                        count[5]++;
                        break;
                    case "HSK":
                        count[6]++;
                        break;
                    case "HST":
                        count[7]++;
                        break;
                    default:
                        break;
                }
            }

            int[] roomQty = new int[8] { 20, 20, 20, 20, 20, 20, 20, 20 };
            string[] availability = new string[8];
            int unavailableCount = 0;

            for (int i = 0; i < 8; i++)
            {
                if ((roomQty[i] - count[i]) >= Convert.ToInt16(Convert.ToString(Session["RoomCount"])))
                {
                    availability[i] = "available";
                }
                else
                {
                    unavailableCount++;
                    availability[i] = "unavailable";
                }

            }

            if (unavailableCount == 8)
            {
                lblAllUnavailable.Text = "All rooms are unavailable for the chosen date range " + Session["CheckIn"] + " to " + Session["CheckOut"] + " .";
            }
            else
            {
                //Check DCK and DCT
                if (availability[0] == "available" && availability[1] == "available")
                {
                    rbtn_DCK.Enabled = true;
                    rbtn_DCT.Enabled = true;

                    rbtn_DCK.Checked = true;
                }
                else if (availability[0] == "unavailable" && availability[1] == "unavailable")
                {
                    btnBook_DC.Enabled = false;
                    btnBook_DC.Text = "SOLD OUT!";
                    btnBook_DC.BorderStyle = BorderStyle.None;
                    btnBook_DC.BackColor = System.Drawing.ColorTranslator.FromHtml("#FFFFFF");
                    btnBook_DC.BorderColor = System.Drawing.ColorTranslator.FromHtml("#FFFFFF");
                    btnBook_DC.ForeColor = System.Drawing.ColorTranslator.FromHtml("#000000");
                }
                else if (availability[0] == "available")
                {
                    rbtn_DCK.Enabled = true;
                    rbtn_DCK.Checked = true;
                }
                else if (availability[1] == "available")
                {
                    rbtn_DCT.Enabled = true;
                    rbtn_DCT.Checked = true;
                }

                //check DSK and DST

                if (availability[2] == "available" && availability[3] == "available")
                {
                    rbtn_DSK.Enabled = true;
                    rbtn_DST.Enabled = true;

                    rbtn_DSK.Checked = true;
                }
                else if (availability[2] == "unavailable" && availability[3] == "unavailable")
                {
                    btnBook_DS.Enabled = false;
                    btnBook_DS.Text = "SOLD OUT!";
                    btnBook_DS.BorderStyle = BorderStyle.None;
                    btnBook_DS.BackColor = System.Drawing.ColorTranslator.FromHtml("#FFFFFF");
                    btnBook_DS.BorderColor = System.Drawing.ColorTranslator.FromHtml("#FFFFFF");
                    btnBook_DS.ForeColor = System.Drawing.ColorTranslator.FromHtml("#000000");
                }
                else if (availability[2] == "available")
                {
                    rbtn_DSK.Enabled = true;
                    rbtn_DSK.Checked = true;
                }
                else if (availability[3] == "available")
                {
                    rbtn_DST.Enabled = true;
                    rbtn_DST.Checked = true;
                }

                //Check HCK and HCT
                if (availability[4] == "available" && availability[5] == "available")
                {
                    rbtn_HCK.Enabled = true;
                    rbtn_HCT.Enabled = true;

                    rbtn_HCK.Checked = true;
                }
                else if (availability[4] == "unavailable" && availability[5] == "unavailable")
                {
                    btnBook_HC.Enabled = false;
                    btnBook_HC.Text = "SOLD OUT!";
                    btnBook_HC.BorderStyle = BorderStyle.None;
                    btnBook_HC.BackColor = System.Drawing.ColorTranslator.FromHtml("#FFFFFF");
                    btnBook_HC.BorderColor = System.Drawing.ColorTranslator.FromHtml("#FFFFFF");
                    btnBook_HC.ForeColor = System.Drawing.ColorTranslator.FromHtml("#000000");
                }
                else if (availability[4] == "available")
                {
                    rbtn_HCK.Enabled = true;
                    rbtn_HCK.Checked = true;
                }
                else if (availability[5] == "available")
                {
                    rbtn_HCT.Enabled = true;
                    rbtn_HCT.Checked = true;
                }

                //check HSK and HST

                if (availability[6] == "available" && availability[7] == "available")
                {
                    rbtn_HSK.Enabled = true;
                    rbtn_HST.Enabled = true;

                    rbtn_HSK.Checked = true;
                }
                else if (availability[6] == "unavailable" && availability[7] == "unavailable")
                {
                    btnBook_HS.Enabled = false;
                    btnBook_HS.Text = "SOLD OUT!";
                    btnBook_HS.BorderStyle = BorderStyle.None;
                    btnBook_HS.BackColor = System.Drawing.ColorTranslator.FromHtml("#FFFFFF");
                    btnBook_HS.BorderColor = System.Drawing.ColorTranslator.FromHtml("#FFFFFF");
                    btnBook_HS.ForeColor = System.Drawing.ColorTranslator.FromHtml("#000000");
                }
                else if (availability[6] == "available")
                {
                    rbtn_HSK.Enabled = true;
                    rbtn_HSK.Checked = true;
                }
                else if (availability[7] == "available")
                {
                    rbtn_HST.Enabled = true;
                    rbtn_HST.Checked = true;
                }
            }
        }