protected void Page_Load(object sender, EventArgs e)
        {
            string UserEmail = "";

            HttpCookie myTestCookie = new HttpCookie("UKFilmLocationAdmin");
            myTestCookie = Request.Cookies["UKFilmLocationAdmin"];

            // Read the cookie information and display it.
            if (myTestCookie != null)
            {
                // User details collected

                UserEmail = myTestCookie.Value;

            }
            else
            {
                // No user cookie redirect to login

                Response.Redirect("/");
            }

            if (!IsPostBack)
            {

                UK_Film_Location_Class.UserDetails objUser = new UK_Film_Location_Class.UserDetails();

                objUser.getUserFromEmail(UserEmail);

                UK_Film_Location_Class.BookingDetails objBooking = new UK_Film_Location_Class.BookingDetails();
                UK_Film_Location_Class.OpportunityDetails objOpportunity = new UK_Film_Location_Class.OpportunityDetails();

                objBooking.UserID = objUser.RecordID.ToString();
                objBooking.BookingID = Request.QueryString["BookingID"];
                objOpportunity.OpportunityID = Request.QueryString["OpportunityID"];

                if (!string.IsNullOrEmpty(objBooking.BookingID))
                {
                    objBooking.getBooking(objBooking.BookingID);
                    objOpportunity.OpportunityID = objBooking.OpportunityID;

                    CreatePanel.Visible = false;
                    BookingPanel.Visible = true;
                }
                else
                {
                    objBooking.DateBooked = DateTime.Now;

                    CreatePanel.Visible = true;
                    BookingPanel.Visible = false;

                }

                BookingID.Value = objBooking.BookingID;

                objOpportunity.getOpportunity(objOpportunity.OpportunityID);

                UK_Film_Location_Class.LocationDetails objLocation = new UK_Film_Location_Class.LocationDetails();
                objLocation.getLocationFromID(objOpportunity.LocationID);

                UK_Film_Location_Class.ContactDetails objLocationContact = new UK_Film_Location_Class.ContactDetails();
                objLocationContact.getContact(objLocation.ContactID);

                UK_Film_Location_Class.ContactDetails objProjectContact = new UK_Film_Location_Class.ContactDetails();
                objProjectContact.getContact(objOpportunity.ContactID);

                UK_Film_Location_Class.ProjectDetails objProject = new UK_Film_Location_Class.ProjectDetails();
                objProject.getProjectDetails(objOpportunity.ProjectID);

                if (!string.IsNullOrEmpty(objBooking.BookingID)) BookingRefLiteral.Text = "(" + objBooking.BookingID + ") <a class=\"btn-default btn\" target=\"_blank\" href=\"/BookingFormPDF.php?BookingID=" + objBooking.BookingID + "\">Booking Form&nbsp;<i class=\"fa fa-file-pdf-o\"></i></a>";

                if (!string.IsNullOrEmpty(objBooking.ContractTemplate)) BookingRefLiteral.Text += " <a class=\"btn-default btn\" target=\"_blank\" href=\"" + objBooking.ContractTemplate + "\">Contract&nbsp;<i class=\"fa fa-file-word-o\"></i></a>";

                BookingRefLiteral.Text += " <a class=\"btn-default btn\" target=\"_blank\" href=\"http://" + UK_Film_Location_Class.UKFilmLocation.DefaultWebsite + "/InvoicePDF.php?BookingID=" + objBooking.BookingID + "\">Preview Invoice&nbsp;<i class=\"fa fa-file-pdf-o\"></i></a>";

                BookingLocationLiteral.Text = "<table width=\"100%\">";

                BookingLocationLiteral.Text += "<tr><td width=\"150\" style=\"font-size: larger;\">Job Ref</td><td style=\"font-size: larger;\"><a href=\"/OpportunityDetails.aspx?OpportunityID=" + objOpportunity.OpportunityID + "\">" + objOpportunity.JobRef + "-" + objBooking.BookingID + "&nbsp;<i class=\"fa fa-external-link-square\" title=\"Opportunity Details\"></i></a></td></tr>";
                BookingLocationLiteral.Text += "<tr><td>Project Title</td><td><a href=\"/ProjectDetails.aspx?ProjectID=" + objProject.ProjectID + "\">" + objProject.WorkingTitle + "&nbsp;<i class=\"fa fa-external-link-square\" title=\"Project Details\"></i></a></td></tr>";
                BookingLocationLiteral.Text += "</table><br>";

                BookingLocationLiteral.Text += "<table width=\"100%\">";
                BookingLocationLiteral.Text += "<tr><td width=\"150\" style=\"font-size: larger;\">Location Ref</td><td style=\"font-size: larger;\"><a href=\"/LocationDetails.aspx?LocationID=" + objOpportunity.LocationID + "\">" + objOpportunity.LocationID + "&nbsp;<i class=\"fa fa-external-link-square\" title=\"Location Details\"></i></a></td></tr>";
                BookingLocationLiteral.Text += "<tr><td>Date Booked</td><td>" + objBooking.DateBooked.ToShortDateString() + "</td></tr>";

                BookingLocationLiteral.Text += "<tr><td>Location Contact</td><td><a href=\"/ContactDetails.aspx?ContactID=" + objLocation.ContactID + "\">" + objLocationContact.Name + "&nbsp;<i class=\"fa fa-external-link-square\" title=\"Contact Details\"></i></a></td></tr>";
                BookingLocationLiteral.Text += "<tr><td>Company</td><td>" + objLocationContact.Company + "</td></tr>";
                BookingLocationLiteral.Text += "<tr><td>Tel</td><td>" + objLocationContact.Tel + "</td></tr>";
                BookingLocationLiteral.Text += "<tr><td>Mobile</td><td>" + objLocationContact.Mobile + "</td></tr>";

                BookingLocationLiteral.Text += "</table><br>";

                BookingLocationLiteral.Text += "<table width=\"100%\">";
                BookingLocationLiteral.Text += "<tr><td width=\"150\">Project Contact</td><td><a href=\"/ContactDetails.aspx?ContactID=" + objProjectContact.ContactID + "\">" + objProjectContact.Name + "&nbsp;<i class=\"fa fa-external-link-square\" title=\"Contact Details\"></i></a></td></tr>";
                BookingLocationLiteral.Text += "<tr><td>Company</td><td>" + objProjectContact.Company + "</td></tr>";
                BookingLocationLiteral.Text += "<tr><td>Tel</td><td>" + objProjectContact.Tel + "</td></tr>";
                BookingLocationLiteral.Text += "<tr><td>Mobile</td><td>" + objProjectContact.Mobile + "</td></tr>";
                BookingLocationLiteral.Text += "</table><br>";

                if (!string.IsNullOrEmpty(objBooking.ContractTemplate))
                {
                    BookingLocationLiteral.Text += "<p>";
                    BookingLocationLiteral.Text += "Contract Generated : <a class=\"btn-default btn\" href=\"/Bookings/" + objBooking.BookingID + "/" + objBooking.BookingID + ".docx\" target=\"_blank\">Download&nbsp;<i class=\"fa fa-file-word-o\"></i></a>";
                    BookingLocationLiteral.Text += "</p>";
                }

                BookingFeesLiteral.Text += "<table width=\"100%\">";

                BookingFeesLiteral.Text += "<tr><td>Agreed Agency Rate</td><td align=\"right\">" + objOpportunity.AgreedRate + "%</td></tr>";
                BookingFeesLiteral.Text += "<tr><td>VAT Rate</td><td align=\"right\">" + objOpportunity.VATRate + "%</td></tr>";
                BookingFeesLiteral.Text += "<tr><td>Agreed Location Rate (per day)</td><td align=\"right\">£" + objOpportunity.LocationRate.ToString("0.00") + "</td></tr>";
                BookingFeesLiteral.Text += "<tr><td>Agreed Location VAT (per day)</td><td align=\"right\">£" + objOpportunity.LocationVAT.ToString("0.00") + "</td></tr>";

            //                BookingFeesLiteral.Text += "<tr><td>Agreed Agency Fee</td><td align=\"right\">£" + objOpportunity.AgencyRate.ToString("0.00") + "</td></tr>";
            //                BookingFeesLiteral.Text += "<tr><td>Agreed Agency VAT</td><td align=\"right\">£" + objOpportunity.AgencyVAT.ToString("0.00") + "</td></tr>";

                BookingFeesLiteral.Text += "<tr><td>Location Deposit</td><td align=\"right\">£" + objOpportunity.Deposit.ToString("0.00") + "</td></tr>";
                BookingFeesLiteral.Text += "<tr><td>Damage Deposit</td><td align=\"right\">£" + objOpportunity.DamageDeposit.ToString("0.00") + "</td></tr>";

                BookingFeesLiteral.Text += "</table><br>";

                // Get list of bookings

                DBConnection = new MySqlConnection(objUKFilmLocation.DBConnect);

                DBCommand = DBConnection.CreateCommand();

                // Opportunities

                DBConnection.Open();

                DBCommand.CommandText = "select BookingDates.NoCrew, BookingDates.BookingDate, BookingDates.BookingID, BookingDates.StartTime, BookingDates.EndTime, BookingDates.Notes, BookingDates.BookingDateID, BookingDates.AgreedDayRate from BookingDates where BookingDates.BookingID = '" + objBooking.BookingID + "' order by BookingDates.BookingDate asc;";

                DBResult = DBCommand.ExecuteReader();

                int noDays = 0;

                double totalDayHire = 0;

                while (DBResult.Read())
                {
                    double AgreedRate = double.Parse(DBResult["AgreedDayRate"].ToString());

                    BookingsLiteral.Text += "<tr><td><i class=\"fa fa-calendar-o fa-2x\"></i></td><td>" + DateTime.Parse(DBResult["BookingDate"].ToString()).ToShortDateString() + "</td><td>" + DBResult["StartTime"].ToString() + "</td><td>" + DBResult["EndTime"].ToString() + "</td><td>" + DBResult["NoCrew"].ToString() + "</td><td align=\"right\" width=\"150\">£" + AgreedRate.ToString("0.00") + "</td><td align=\"right\" width=\"180\"><a href=\"/BookingDateDetails.aspx?BookingDateID=" + DBResult["BookingDateID"].ToString() + "\" class=\"btn-default btn\">View / Change</a></td></tr>";

                    BookingsLiteral.Text += "<tr><td colspan=\"3\">" + DBResult["Notes"].ToString() + "</td><td></td></tr>";

                    if (!string.IsNullOrEmpty(DBResult["AgreedDayRate"].ToString()))
                    {
                        totalDayHire += double.Parse(DBResult["AgreedDayRate"].ToString());
                    }

                    noDays++;
                }

                DBResult.Close();

                DBConnection.Close();

                if (noDays > 0) GenerateContractBTN.Visible = true;

                double thisLocationVAT = 0;

                BookingFeesLiteral.Text += "<table width=\"100%\">";
                if (objLocation.VATRegistered == "1")
                {
                    BookingFeesLiteral.Text += "<tr><td>Total Location hire for " + noDays + " days</td><td align=\"right\">£" + totalDayHire.ToString("0.00") + " + VAT = £" + ((totalDayHire * (objOpportunity.VATRate / 100)) + totalDayHire).ToString("0.00") + "</td></tr>";
                    thisLocationVAT = (totalDayHire * (objOpportunity.VATRate / 100));
                }
                else
                {
                    BookingFeesLiteral.Text += "<tr><td>Total Location hire for " + noDays + " days</td><td align=\"right\">£" + totalDayHire.ToString("0.00") + "</td></tr>";
                }

                BookingFeesLiteral.Text += "</table><br>";

                BookingFeesLiteral.Text += "<table width=\"100%\">";

                double thisAgencyFee = totalDayHire * (objOpportunity.AgreedRate / 100);
                double thisAgencyVAT = thisAgencyFee * (objOpportunity.VATRate / 100);

                BookingFeesLiteral.Text += "<tr><td>Agency Fee @ " + objOpportunity.AgreedRate.ToString() + "%</td><td align=\"right\">£" + thisAgencyFee.ToString("0.00") + "</td></tr>";
                BookingFeesLiteral.Text += "<tr><td>Agency VAT</td><td align=\"right\">£" + thisAgencyVAT.ToString("0.00") + "</td></tr>";

                BookingFeesLiteral.Text += "</table><br>";

                BookingFeesLiteral.Text += "<table width=\"100%\">";

                //    BookingFeesLiteral.Text += "Day Hire = " + totalDayHire + ", AgencyFee = " + thisAgencyFee + ", AgencyVAT = " + thisAgencyVAT + ", Location VAT = " + thisLocationVAT;

                BookingFeesLiteral.Text += "<tr><td><span style=\"font-size: larger;\">Total Hire</span></td><td align=\"right\"><span style=\"font-size: larger;\">£" + (totalDayHire + thisAgencyFee + thisAgencyVAT + thisLocationVAT).ToString("0.00") + "</span></td></tr>";
                BookingFeesLiteral.Text += "<tr><td colspan=\"2\">Plus £" + (objOpportunity.Deposit + objOpportunity.DamageDeposit).ToString("0.00") + " deposit(s)</td></tr>";
                BookingFeesLiteral.Text += "<tr><td><span style=\"font-size: larger;\">Total Payable</span></td><td align=\"right\"><span style=\"font-size: larger;\"><b>£" + (totalDayHire + thisAgencyFee + thisAgencyVAT + thisLocationVAT + objOpportunity.Deposit + objOpportunity.DamageDeposit).ToString("0.00") + "</b></span></td></tr>";

                BookingFeesLiteral.Text += "</table>";

                if (objBooking.InvoiceSent.ToString() != "01/01/0001 00:00:00")
                {
                    UK_Film_Location_Class.InvoiceDetails objInvoice = new UK_Film_Location_Class.InvoiceDetails();
                    objInvoice.getInvoice(BookingID.Value);

                    BookingFeesLiteral.Text += "<p><b>";
                    BookingFeesLiteral.Text += "<br><br>Invoice Sent : " + objBooking.InvoiceSent.ToString().Substring(0, 10) + " #" + objInvoice.RecordID;
                    BookingFeesLiteral.Text += "</b></p>";
                }

                DBConnection.Dispose();
            }
        }
        protected void SendInvoice_Click(object sender, EventArgs e)
        {
            UK_Film_Location_Class.InvoiceDetails objInvoice = new UK_Film_Location_Class.InvoiceDetails();

            UK_Film_Location_Class.BookingDetails objBooking = new UK_Film_Location_Class.BookingDetails();
            objBooking.getBooking(Request.QueryString["BookingID"]);

            UK_Film_Location_Class.OpportunityDetails objOpportunity = new UK_Film_Location_Class.OpportunityDetails();
            objOpportunity.getOpportunity(objBooking.OpportunityID);

            UK_Film_Location_Class.ContactDetails objProjectContact = new UK_Film_Location_Class.ContactDetails();
            objProjectContact.getContact(objOpportunity.ContactID);

            if (objBooking.InvoiceSent.ToString().Contains("01/01/0001"))
            {
                //objBooking.InvoiceSent = DateTime.Now;
                //objBooking.saveBooking();

                objInvoice.CreateInvoice(objBooking.BookingID);
            }
            else
            {
                objInvoice.getInvoice(objBooking.BookingID);
            }

            string thisMessage = "";

            thisMessage += "<p>To " + objProjectContact.Name + ",<br><br>";
            thisMessage += "<p><a href=\"http://" + UK_Film_Location_Class.UKFilmLocation.DefaultWebsite + "/InvoicePDF.php?BookingID=" + objBooking.BookingID + "\">Click here</a> to view your invoice, you can also view your invoices by logging in to <a href=\"http://UKFilmLocation.com\">UKFilmLocation.com</a> and navigating to your dashboard.</p><h4>Terms & Conditions</h4><ul><li>Payment may be made via BACS, TT or direct payment into a UK Bank. Add 4% for credit card payments</li><li>Hire is dependent on signed contracts, proof of identity, and proof of insurance being received prior to commencement of the shoot.</li><li>E&OE</li></ul><p>Many thanks for your order. We look forward to helping you again in the future.</p><p><b>UKFilmLocation.com</b></p>";

            //        UK_Film_Location_Class.UKFilmLocation.sendEmail(objProjectContact.Email, "Accounts", "*****@*****.**", "UKFilmLocation.com - Invoice : " + objInvoice.RecordID, thisMessage);
            UK_Film_Location_Class.UKFilmLocation.sendEmail("*****@*****.**", "Accounts", "*****@*****.**", "UKFilmLocation.com - Invoice : " + objInvoice.RecordID, thisMessage);
        }