void shippingRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DollarSaverDB.OrderLineItemRow lineItem = (DollarSaverDB.OrderLineItemRow)e.Item.DataItem;

                Label certificateNameLabel = (Label)e.Item.FindControl("certificateNameLabel");
                Label qtyLabel             = (Label)e.Item.FindControl("qtyLabel");

                certificateNameLabel.Text = lineItem.Certificate.AdvertiserName;
                qtyLabel.Text             = lineItem.Quantity.ToString();
            }
        }
        void certificateRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DollarSaverDB.OrderLineItemRow lineItem = (DollarSaverDB.OrderLineItemRow)e.Item.DataItem;

                Repeater numberRepeater = (Repeater)e.Item.FindControl("numberRepeater");

                numberRepeater.ItemDataBound += new RepeaterItemEventHandler(numberRepeater_ItemDataBound);

                numberRepeater.DataSource = lineItem.Numbers.Rows;
                numberRepeater.DataBind();
            }
        }
        void pickUpRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DollarSaverDB.OrderLineItemRow lineItem = (DollarSaverDB.OrderLineItemRow)e.Item.DataItem;

                Label certificateNameLabel = (Label)e.Item.FindControl("certificateNameLabel");
                Label deliveryNoteLabel    = (Label)e.Item.FindControl("deliveryNoteLabel");
                Label qtyLabel             = (Label)e.Item.FindControl("qtyLabel");

                certificateNameLabel.Text = lineItem.Certificate.AdvertiserName;
                deliveryNoteLabel.Text    = Server.HtmlEncode(lineItem.DeliveryNote).Replace(Environment.NewLine, "<BR>");
                qtyLabel.Text             = lineItem.Quantity.ToString();
            }
        }
        void lineItemRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DollarSaverDB.OrderLineItemRow lineItem = (DollarSaverDB.OrderLineItemRow)e.Item.DataItem;

                HyperLink certificateLink = (HyperLink)e.Item.FindControl("certificateLink");
                Label     priceLabel      = (Label)e.Item.FindControl("priceLabel");
                Label     qtyLabel        = (Label)e.Item.FindControl("qtyLabel");
                Label     totalLabel      = (Label)e.Item.FindControl("totalLabel");

                certificateLink.Text        = lineItem.ShortName;
                certificateLink.NavigateUrl = "~/admin/CertificateEdit.aspx?id=" + lineItem.CertificateId;
                priceLabel.Text             = lineItem.DiscountValue.ToString("$#,0.00");
                qtyLabel.Text   = lineItem.Quantity.ToString();
                totalLabel.Text = lineItem.Total.ToString("$#,0.00");

                GridView numberGrid        = (GridView)e.Item.FindControl("numberGrid");
                Label    nonPrintableLabel = (Label)e.Item.FindControl("nonPrintableLabel");
                if (lineItem.DeliveryType == DeliveryType.Print)
                {
                    nonPrintableLabel.Visible = false;
                    numberGrid.Visible        = true;

                    numberGrid.RowDataBound += new GridViewRowEventHandler(numberGrid_RowDataBound);

                    numberGrid.DataSource = lineItem.Numbers.Rows;
                    numberGrid.DataBind();
                }
                else
                {
                    nonPrintableLabel.Visible = true;

                    nonPrintableLabel.Text = "Not Printable";

                    if (lineItem.DeliveryType == DeliveryType.Ship)
                    {
                        nonPrintableLabel.Text += " - Mail to Customer";
                    }
                    else if (lineItem.DeliveryType == DeliveryType.PickUp)
                    {
                        nonPrintableLabel.Text += " - Pick Up<BR>" + Server.HtmlEncode(lineItem.DeliveryNote).Replace(Environment.NewLine, "<BR>");
                    }

                    numberGrid.Visible = false;
                }
            }
        }
        void returnedItemRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DollarSaverDB.OrderLineItemRow returnedItem = (DollarSaverDB.OrderLineItemRow)e.Item.DataItem;

                HyperLink certificateLink = (HyperLink)e.Item.FindControl("certificateLink");
                Label     priceLabel      = (Label)e.Item.FindControl("priceLabel");
                Label     qtyLabel        = (Label)e.Item.FindControl("qtyLabel");
                Label     totalLabel      = (Label)e.Item.FindControl("totalLabel");

                certificateLink.Text        = returnedItem.ShortName;
                certificateLink.NavigateUrl = "~/admin/CertificateEdit.aspx?id=" + returnedItem.CertificateId;
                priceLabel.Text             = returnedItem.DiscountValue.ToString("$#,0.00");
                qtyLabel.Text   = returnedItem.Quantity.ToString();
                totalLabel.Text = returnedItem.Total.ToString("$#,0.00");
            }
        }
        void lineItemRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DollarSaverDB.OrderLineItemRow lineItem = (DollarSaverDB.OrderLineItemRow)e.Item.DataItem;

                HyperLink certificateLink = (HyperLink)e.Item.FindControl("certificateLink");
                Label     priceLabel      = (Label)e.Item.FindControl("priceLabel");

                HiddenField lineItemIdHidden = (HiddenField)e.Item.FindControl("lineItemIdHidden");
                //DropDownList qtyList = (DropDownList)e.Item.FindControl("qtyList");
                Label qtyLabel = (Label)e.Item.FindControl("qtyLabel");

                Label totalLabel = (Label)e.Item.FindControl("totalLabel");

                certificateLink.Text        = lineItem.ShortName;
                certificateLink.NavigateUrl = "~/admin/CertificateEdit.aspx?id=" + lineItem.CertificateId;
                priceLabel.Text             = lineItem.DiscountValue.ToString("$#,0.00");

                lineItemIdHidden.Value = lineItem.OrderLineItemId.ToString();

                /*
                 * for (int qty=0; qty <= lineItem.Quantity; qty++) {
                 *  qtyList.Items.Add(qty.ToString());
                 * }
                 * qtyList.SelectedValue = lineItem.Quantity.ToString();
                 */
                qtyLabel.Text = lineItem.Quantity.ToString();

                totalLabel.Text = lineItem.Total.ToString("$#,0.00");

                GridView numberGrid = (GridView)e.Item.FindControl("numberGrid");

                numberGrid.Visible = true;

                numberGrid.RowDataBound += new GridViewRowEventHandler(numberGrid_RowDataBound);

                numberGrid.DataSource = lineItem.Numbers.Rows;
                numberGrid.DataBind();
            }
        }
        void lineItemRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DollarSaverDB.OrderLineItemRow lineItem = (DollarSaverDB.OrderLineItemRow)e.Item.DataItem;

                LinkButton  removeButton         = (LinkButton)e.Item.FindControl("removeButton");
                Label       certificateNameLabel = (Label)e.Item.FindControl("certificateNameLabel");
                HiddenField lineItemIdHidden     = (HiddenField)e.Item.FindControl("lineItemIdHidden");
                Label       priceLabel           = (Label)e.Item.FindControl("priceLabel");
                TextBox     qtyBox     = (TextBox)e.Item.FindControl("qtyBox");
                Label       totalLabel = (Label)e.Item.FindControl("totalLabel");

                removeButton.CommandArgument = lineItem.OrderLineItemId.ToString();
                certificateNameLabel.Text    = lineItem.Certificate.AdvertiserName;
                lineItemIdHidden.Value       = lineItem.OrderLineItemId.ToString();
                priceLabel.Text = lineItem.DiscountValue.ToString("$#,0.00");
                qtyBox.Text     = lineItem.Quantity.ToString();
                totalLabel.Text = lineItem.Total.ToString("$#,0.00");
            }
        }
        void lineItemRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DollarSaverDB.OrderLineItemRow lineItem = (DollarSaverDB.OrderLineItemRow)e.Item.DataItem;

                Label       certificateNameLabel = (Label)e.Item.FindControl("certificateNameLabel");
                PlaceHolder nonPrintableHolder   = (PlaceHolder)e.Item.FindControl("nonPrintableHolder");
                Label       nonPrintableLabel    = (Label)e.Item.FindControl("nonPrintableLabel");
                Label       priceLabel           = (Label)e.Item.FindControl("priceLabel");
                Label       qtyLabel             = (Label)e.Item.FindControl("qtyLabel");
                Label       totalLabel           = (Label)e.Item.FindControl("totalLabel");

                certificateNameLabel.Text = lineItem.Certificate.AdvertiserName;
                if (lineItem.DeliveryType != DeliveryType.Print)
                {
                    nonPrintableHolder.Visible = true;

                    if (lineItem.DeliveryType == DeliveryType.Ship)
                    {
                        nonPrintableLabel.Text = "Certificate(s) will be mailed";
                    }
                    else if (lineItem.DeliveryType == DeliveryType.PickUp)
                    {
                        nonPrintableLabel.Text = Server.HtmlEncode(lineItem.DeliveryNote).Replace(Environment.NewLine, "<BR>");
                    }
                }
                else
                {
                    nonPrintableHolder.Visible = false;
                }

                priceLabel.Text = lineItem.DiscountValue.ToString("$#,0.00");
                qtyLabel.Text   = lineItem.Quantity.ToString();
                totalLabel.Text = lineItem.Total.ToString("$#,0.00");
            }
        }
        void lineItemRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DollarSaverDB.OrderLineItemRow lineItem = (DollarSaverDB.OrderLineItemRow)e.Item.DataItem;

                HyperLink certificateLink = (HyperLink)e.Item.FindControl("certificateLink");
                Label     priceLabel      = (Label)e.Item.FindControl("priceLabel");
                Label     qtyLabel        = (Label)e.Item.FindControl("qtyLabel");
                Label     totalLabel      = (Label)e.Item.FindControl("totalLabel");

                certificateLink.Text        = lineItem.ShortName;
                certificateLink.NavigateUrl = "~/admin/CertificateEdit.aspx?id=" + lineItem.CertificateId;
                priceLabel.Text             = lineItem.DiscountValue.ToString("$#,0.00");
                qtyLabel.Text   = lineItem.Quantity.ToString();
                totalLabel.Text = lineItem.Total.ToString("$#,0.00");

                GridView numberGrid = (GridView)e.Item.FindControl("numberGrid");
                numberGrid.RowDataBound += new GridViewRowEventHandler(numberGrid_RowDataBound);

                numberGrid.DataSource = lineItem.Numbers.Rows;
                numberGrid.DataBind();
            }
        }
        void saveButton_Click(object sender, EventArgs e)
        {
            OrderLineItemTableAdapter     lineItemAdapter          = new OrderLineItemTableAdapter();
            CertificateNumberTableAdapter certificateNumberAdapter = new CertificateNumberTableAdapter();

            decimal subtotal = 0.0m;

            foreach (RepeaterItem item in lineItemRepeater.Items)
            {
                if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
                {
                    HiddenField lineItemIdHidden            = (HiddenField)item.FindControl("lineItemIdHidden");
                    int         lineItemId                  = Convert.ToInt32(lineItemIdHidden.Value);
                    DollarSaverDB.OrderLineItemRow lineItem = lineItemAdapter.GetOrderLineItem(lineItemId)[0];


                    //DropDownList qtyList = (DropDownList)item.FindControl("qtyList");
                    //int qty = Convert.ToInt32(qtyList.SelectedValue);


                    /*
                     * if (lineItem.Quantity != qty) {
                     *
                     *
                     *  int returnedQty = lineItem.Quantity - qty;
                     *  certificateNumberAdapter.Return(lineItem.OrderLineItemId, returnedQty);
                     *
                     *  if (qty == 0) {
                     *      lineItem.Returned = true;
                     *      lineItemAdapter.Update(lineItem);
                     *  } else {
                     *      lineItem.Quantity = qty;
                     *      lineItemAdapter.Update(lineItem);
                     *
                     *      DollarSaverDB.OrderLineItemRow returnedItem = order.ReturnedLineItems.GetLineItem(lineItem.CertificateId);
                     *
                     *      if (returnedItem != null) {
                     *          returnedItem.Quantity += returnedQty;
                     *          lineItemAdapter.Update(returnedItem);
                     *      } else {
                     *          lineItemAdapter.Insert(lineItem.OrderId, -1, returnedQty, lineItem.CertificateId,
                     *              lineItem.ShortName, lineItem.Description, lineItem.FaceValue, lineItem.Discount, lineItem.DiscountTypeId, lineItem.Printable, true);
                     *      }
                     *  }
                     *
                     * }
                     */

                    GridView numberGrid = (GridView)item.FindControl("numberGrid");

                    int returnedQty = 0;
                    foreach (GridViewRow certNumberRow in numberGrid.Rows)
                    {
                        CheckBox deleteBox = (CheckBox)certNumberRow.FindControl("deleteBox");

                        if (deleteBox.Checked)
                        {
                            returnedQty++;

                            HiddenField certNumberIdHidden = (HiddenField)certNumberRow.FindControl("certNumberIdHidden");
                            int         certNumberId       = Convert.ToInt32(certNumberIdHidden.Value);

                            DollarSaverDB.CertificateNumberRow certNumber =
                                certificateNumberAdapter.GetCertificateNumber(certNumberId)[0];

                            certNumber.SetOrderLineItemIdNull();
                            certificateNumberAdapter.Update(certNumber);
                        }
                    }


                    if (returnedQty > 0)
                    {
                        DollarSaverDB.OrderLineItemRow returnedItem = order.ReturnedLineItems.GetLineItem(lineItem.CertificateId);

                        if (returnedItem != null)
                        {
                            returnedItem.Quantity += returnedQty;
                            lineItemAdapter.Update(returnedItem);

                            if (returnedQty == lineItem.Quantity)
                            {
                                lineItemAdapter.Delete(lineItem.OrderLineItemId);
                            }
                            else
                            {
                                lineItem.Quantity -= returnedQty;
                                lineItemAdapter.Update(lineItem);
                                subtotal += lineItem.Total;
                            }
                        }
                        else
                        {
                            if (returnedQty == lineItem.Quantity)
                            {
                                lineItem.Returned = true;
                                lineItemAdapter.Update(lineItem);
                            }
                            else
                            {
                                lineItemAdapter.Insert(lineItem.OrderId, -1, returnedQty, lineItem.CertificateId,
                                                       lineItem.ShortName, lineItem.Description, lineItem.FaceValue, lineItem.Discount, lineItem.DiscountTypeId, lineItem.DeliveryTypeId, lineItem.DeliveryNote, true);

                                lineItem.Quantity -= returnedQty;
                                lineItemAdapter.Update(lineItem);
                                subtotal += lineItem.Total;
                            }
                        }
                    }
                    else
                    {
                        subtotal += lineItem.Total;
                    }
                }
            }

            order.SubTotal   = subtotal;
            order.GrandTotal = subtotal;

            OrderTableAdapter orderAdapter = new OrderTableAdapter();

            orderAdapter.Update(order);

            InfoMessage = "Order updated";
            Response.Redirect("~/admin/OrderView.aspx?id=" + orderId);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            lineItemRepeater.ItemDataBound += new RepeaterItemEventHandler(lineItemRepeater_ItemDataBound);
            lineItemRepeater.ItemCommand   += new RepeaterCommandEventHandler(lineItemRepeater_ItemCommand);

            updateButton.Click   += new EventHandler(updateButton_Click);
            continueButton.Click += new EventHandler(continueButton_Click);

            /*
             * if (Order != null &&
             *  (Order.OrderStatusId != 1 || Order.StationId != StationId)) {
             *  Order = null;
             *  Response.Redirect("~/Cart.aspx");
             * }*/

            if (displayCookieMessage)
            {
                itemHolder.Visible          = false;
                emptyCartHolder.Visible     = false;
                cookieMessageHolder.Visible = true;
                homeLink.NavigateUrl       += "?station_id=" + StationId;
                return;
            }
            else
            {
                cookieMessageHolder.Visible = false;
            }

            if (!Page.IsPostBack)
            {
                OrderTableAdapter orderAdapter = new OrderTableAdapter();

                int certificateId = GetCertificateIdFromQueryString();

                if ((Order == null && certificateId > 0) ||
                    (Order != null && Order.OrderStatusId != 1) ||
                    (Order != null && Order.StationId != StationId))
                {
                    Order = GenerateOrder();
                }


                if (certificateId > 0)
                {
                    CertificateTableAdapter certificateAdapter = new CertificateTableAdapter();

                    DollarSaverDB.CertificateDataTable certificateTable = certificateAdapter.GetCertificate(certificateId);

                    if (certificateTable.Count != 1)
                    {
                        Response.Redirect("~/Cart.aspx");
                    }

                    DollarSaverDB.CertificateRow certificate = certificateTable[0];

                    if (certificate.Advertiser.StationId != StationId)
                    {
                        Response.Redirect("~/Cart.aspx");
                    }

                    if (!certificate.IsActive || !certificate.Advertiser.IsActive)
                    {
                        InfoMessage = "Sorry, this certificate is no longer available";
                        Response.Redirect("~/Cart.aspx");
                    }

                    if (certificate.OnSaleDate > DateTime.Now)
                    {
                        InfoMessage = "Sorry, this certificate is not yet available";
                        Response.Redirect("~/Cart.aspx");
                    }

                    int quantity = GetValueFromQueryString("qty");

                    if (quantity == 0)
                    {
                        quantity = 1;
                    }

                    OrderLineItemTableAdapter orderLineItemAdapter = new OrderLineItemTableAdapter();

                    DollarSaverDB.OrderLineItemRow lineItem = Order.LineItems.GetLineItem(certificateId);


                    if (lineItem == null)
                    {
                        quantity = CheckQuantity(quantity, certificate);

                        if (quantity > 0)
                        {
                            Order.LineItems.AddOrderLineItemRow(Order.OrderId, -1, quantity,
                                                                certificate.CertificateId, certificate.ShortName, certificate.Description,
                                                                certificate.FaceValue, certificate.Discount, certificate.DiscountTypeId,
                                                                false, certificate.DeliveryTypeId, certificate.DeliveryNote);

                            orderLineItemAdapter.Update(Order.LineItems);
                        }
                    }
                    else
                    {
                        quantity += lineItem.Quantity;

                        quantity = CheckQuantity(quantity, certificate);

                        if (quantity == 0)
                        {
                            orderLineItemAdapter.Delete(lineItem.OrderLineItemId);
                        }
                        else
                        {
                            lineItem.Quantity = quantity;
                            orderLineItemAdapter.Update(lineItem);
                        }
                    }

                    UpdateLineItemModifiedDate();

                    if (InfoMessage == String.Empty)
                    {
                        InfoMessage = "Certificate(s) added to your cart";
                    }

                    // reload the cart
                    Response.Redirect("~/Cart.aspx");
                }


                if (Order != null && Order.LineItems.Count > 0)
                {
                    itemHolder.Visible      = true;
                    emptyCartHolder.Visible = false;

                    lineItemRepeater.DataSource = Order.LineItems.Rows;
                    lineItemRepeater.DataBind();

                    subTotalLabel.Text = Order.LineItems.SubTotal.ToString("$#,0.00");

                    continueShoppingLink.NavigateUrl = "~/Default.aspx?station_id=" + StationId;
                }
                else
                {
                    itemHolder.Visible      = false;
                    emptyCartHolder.Visible = true;
                }
            }
            else
            {
                // if page is postback, make sure the order is still in the session
                if (Order == null)
                {
                    InfoMessage = "Sorry, your session has timed out";
                    Response.Redirect("~/Cart.aspx");
                }
            }
        }
        void updateButton_Click(object sender, EventArgs e)
        {
            OrderLineItemTableAdapter lineItemAdapter = new OrderLineItemTableAdapter();

            foreach (RepeaterItem item in lineItemRepeater.Items)
            {
                HiddenField lineItemIdHidden = (HiddenField)item.FindControl("lineItemIdHidden");
                TextBox     qtyBox           = (TextBox)item.FindControl("qtyBox");

                int lineItemId = Int32.Parse(lineItemIdHidden.Value);

                DollarSaverDB.OrderLineItemRow lineItem = lineItemAdapter.GetOrderLineItem(lineItemId)[0];

                int newQuantity = lineItem.Quantity;

                try {
                    newQuantity = Int32.Parse(qtyBox.Text.Trim());
                } catch {
                    ErrorMessage = "Invalid quantity entered";
                    return;
                }

                if (newQuantity < 0)
                {
                    ErrorMessage = "New quantity must be greater than zero";
                    return;
                }


                if (newQuantity > lineItem.Certificate.MaxPurchaseQty && lineItem.Certificate.MaxPurchaseQty > 0)
                {
                    // set to max purchase qty and continue processing
                    newQuantity  = lineItem.Certificate.MaxPurchaseQty;
                    InfoMessage += "Sorry, you can only purchase a maximum of " + lineItem.Certificate.MaxPurchaseQty + " of certifcate " + lineItem.Certificate.AdvertiserName + "<BR />";
                }

                if (newQuantity != lineItem.Quantity && newQuantity >= 0)
                {
                    if (newQuantity == 0)
                    {
                        lineItemAdapter.Delete(lineItem.OrderLineItemId);
                    }
                    else
                    {
                        newQuantity = CheckQuantity(newQuantity, lineItem.Certificate);

                        if (newQuantity == 0)
                        {
                            lineItemAdapter.Delete(lineItem.OrderLineItemId);
                        }
                        else
                        {
                            lineItem.Quantity = newQuantity;
                            lineItemAdapter.Update(lineItem);
                        }
                    }
                }
            }

            UpdateLineItemModifiedDate();

            if (InfoMessage == String.Empty)
            {
                InfoMessage = "Cart updated";
            }

            Response.Redirect("~/Cart.aspx");
        }