protected void btnConfirmOrder_Click(object sender, EventArgs e)
    {
        try
        {
            ltrMessage.Visible = false;

            if (Session[enumSessions.OrderId.ToString()] == null) /* If someone clicks on back button - when the confirmation message is shown.*/
            {
                Response.Redirect("Login.aspx");
            }
            else if (dlProducts.Items.Count == 0)
            {
                ltrMessage.Visible = true;
                ltrMessage.Text    = "No item is added to confirm the order. Please add an item first to confirm the order.";
            }
            else
            {
                LinqToSqlDataContext db = new LinqToSqlDataContext();
                var orderStatus         = (from o in db.Orders
                                           where o.OrderId == Convert.ToInt32(Session[enumSessions.OrderId.ToString()])
                                           select o.OrderStatusId).SingleOrDefault();
                if (orderStatus != 1)
                {
                    Response.Redirect("Categories.aspx");
                }
                this.MaintainScrollPositionOnPostBack = true;

                if (divNewAddress.Visible)
                {
                    Page.Validate("grpDeliveryAddress");
                }

                if (divInstallationAddress.Visible)
                {
                    Page.Validate("grpInstallationAddress");
                }

                if (String.IsNullOrEmpty(txtOrderRefNo.Text.Trim()) == true)
                {
                    ltrMessage.Text         = "Please enter the ARC Order Reference number.";
                    txtOrderRefNo.BackColor = System.Drawing.Color.Yellow;
                    txtOrderRefNo.Focus();

                    return;
                }
                else
                {
                    Session[enumSessions.OrderRef.ToString()] = txtOrderRefNo.Text;
                }

                if (IsValid)
                {
                    btnConfirmOrder.Enabled = false;
                    int    addressId             = 0;
                    int    installationAddressId = 0;
                    string contactno             = txtDeliContactNo.Text.ToString().Trim();
                    string OrderNo = String.Empty;
                    if (rdoInstallerAddress.Checked)
                    {
                        addressId = InstallerBAL.SaveInstallerAddress(Session[enumSessions.InstallerCompanyID.ToString()].ToString(), txtInstContactName1.Text, contactno, 0, "", "", "", "", "", "", Session[enumSessions.User_Name.ToString()].ToString());
                    }
                    else if (ddlarcdeliveryaddresses.SelectedValue != "0" && (chkEditAddress.Checked == false) && !string.IsNullOrEmpty(ddlarcdeliveryaddresses.SelectedValue))
                    {
                        addressId = Convert.ToInt32(ddlarcdeliveryaddresses.SelectedValue);
                    }
                    else
                    {
                        int countryId = 0;
                        int.TryParse(ddlCountry.SelectedValue, out countryId);
                        addressId = InstallerBAL.SaveInstallerAddress("", txtDeliContactName.Text, contactno, countryId, txtDeliAddressOne.Text, txtDeliAddressTwo.Text, txtDeliTown.Text, txtDeliCounty.Text, txtDeliPostcode.Text, ddlCountry.SelectedItem.Text, Session[enumSessions.User_Name.ToString()].ToString());
                    }

                    if (chkInstallationAddress.Checked)
                    {
                        instadd_differs = true;
                        int countryId = 0;
                        int.TryParse(ddlInstCountry.SelectedValue, out countryId);
                        installationAddressId = InstallerBAL.SaveInstallerAddress("", txtInstContactName.Text, txtInstContactNumber.Text, countryId,
                                                                                  txtInstAddressOne.Text, txtInstAddressTwo.Text, txtInstTown.Text,
                                                                                  txtInstCounty.Text, txtInstPostCode.Text, ddlInstCountry.SelectedItem.Text, Session[enumSessions.User_Name.ToString()].ToString());
                    }
                    //Added below code by Atiq on 11-08-2016 for ESI changes
                    string alarmDelArcCode = string.Empty;
                    if (ddlArcBranches.SelectedIndex > 0)
                    {
                        var arcData = (from a in db.AlarmDeliveryARCMappings
                                       where a.ID == Convert.ToInt32(ddlArcBranches.SelectedValue)
                                       select a.Branch_ARC_Code).SingleOrDefault();
                        alarmDelArcCode = arcData;
                    }
                    int orderid = Convert.ToInt32(Session[enumSessions.OrderId.ToString()].ToString());
                    var ordrNo  = db.USP_ConfirmOrderDetails(orderid, txtOrderRefNo.Text, Convert.ToDecimal(lblDeliveryTotal.Text), addressId,
                                                             ddlDeliveryTypes.SelectedValue == String.Empty ? 0 : Convert.ToInt32(ddlDeliveryTypes.SelectedValue), txtInstructions.Text,
                                                             VATRate, Session[enumSessions.User_Name.ToString()].ToString(), installationAddressId, txtInstContactName1.Text,
                                                             alarmDelArcCode).SingleOrDefault();
                    if (ordrNo != null)
                    {
                        OrderNo = ordrNo.OrderNo;
                    }
                    db.SubmitChanges();

                    int orderHasEmizonProducts = (from O in db.Orders
                                                  join
                                                  oi in db.OrderItems on O.OrderId equals oi.OrderId
                                                  join
                                                  p in db.Products on oi.ProductId equals p.ProductId
                                                  where
                                                  O.OrderId == orderid
                                                  &&
                                                  p.IsEmizonProduct == true
                                                  &&
                                                  p.ProductType == "Product"
                                                  select p.EM_ProductCode).Count();

                    // ** Send to logistcs
                    SendEmailDTO sendEmaildto = new SendEmailDTO();
                    sendEmaildto.ARCOrderRefNo       = txtOrderRefNo.Text;
                    sendEmaildto.orderDate           = lblOrderDate.Text;
                    sendEmaildto.userID              = Session[enumSessions.User_Id.ToString()].ToString();
                    sendEmaildto.userName            = Session[enumSessions.User_Name.ToString()].ToString();
                    sendEmaildto.userEmail           = Session[enumSessions.User_Email.ToString()].ToString();
                    sendEmaildto.orderID             = Session[enumSessions.OrderId.ToString()].ToString();
                    sendEmaildto.DdeliveryType       = ddlDeliveryTypes.SelectedItem.Text;
                    sendEmaildto.deliveryCost        = lblDeliveryTotal.Text;
                    sendEmaildto.installerID         = Session[enumSessions.InstallerCompanyID.ToString()].ToString();
                    sendEmaildto.specialInstructions = txtInstructions.Text;

                    //Send to CSL Orders INBOX // do not send to emizon queue as its internal email and EM is not mandatory
                    SendEmail.SendEmailWithPrice(OrderNo, mailTO, sendEmaildto, mailFrom, mailCC, false, false, 0);

                    // ** Send to customers
                    if (Session[enumSessions.User_Role.ToString()] != null && Session[enumSessions.User_Role.ToString()].ToString() == enumRoles.ARC_Admin.ToString())
                    {
                        SendEmail.SendEmailWithoutPrice(OrderNo, Session[enumSessions.User_Email.ToString()].ToString(), sendEmaildto,
                                                        mailFrom, mailCC + "," + LoadARCEmailCC(), false, (orderHasEmizonProducts > 0) ? true : false, orderid);
                    }
                    else
                    {
                        SendEmail.SendEmailWithPrice(OrderNo, Session[enumSessions.User_Email.ToString()].ToString(), sendEmaildto,
                                                     mailFrom, mailCC + "," + LoadARCEmailCC(), false, (orderHasEmizonProducts > 0) ? true : false, orderid);
                    }

                    db.Dispose();
                    Session[enumSessions.OrderId.ToString()]     = null;
                    Session[enumSessions.OrderNumber.ToString()] = OrderNo;

                    //Add Emizon page redirection here
                    if (orderHasEmizonProducts > 0)
                    {
                        Response.Redirect("OrderConfirmationEM.aspx?id=" + orderid.ToString());
                    }
                    else
                    {
                        Response.Redirect("OrderConfirmation.aspx");
                    }
                }
                else
                {
                    btnConfirmOrder.Enabled = true;
                    txtDeliContactName.Focus();
                    this.MaintainScrollPositionOnPostBack = false;
                }
            }
        }
        catch (System.Threading.ThreadAbortException ex)
        {
            //
        }
        catch (Exception objException)
        {
            CSLOrderingARCBAL.LinqToSqlDataContext db;
            db = new CSLOrderingARCBAL.LinqToSqlDataContext();
            db.USP_SaveErrorDetails(Request.Url.ToString(), "btnConfirmOrder_Click",
                                    Convert.ToString(objException.Message), Convert.ToString(objException.InnerException),
                                    Convert.ToString(objException.StackTrace), "", HttpContext.Current.Request.UserHostAddress,
                                    false, Convert.ToString(HttpContext.Current.Session[enumSessions.User_Id.ToString()]));
        }
    }