protected void SaveForm()
        {
            try
            {
                hccCart userCart = hccCart.GetCurrentCart();
                CurrentGiftCert = hccCartItem.Gift_GenerateNew(userCart.CartID);

                if (CurrentGiftCert != null)
                {
                    CurrentGiftCert.ItemPrice = decimal.Parse(txtAmount.Text.Trim());

                    //recipient info
                    CurrentGiftCert.Gift_RecipientEmail   = txtRecipEmail.Text.Trim();
                    CurrentGiftCert.Gift_RecipientMessage = txtRecipMessage.Text.Trim();
                    CurrentGiftCert.DeliveryDate          = hccProductionCalendar.GetNext4Calendars().First().DeliveryDate;
                    CurrentGiftCert.GetOrderNumber(userCart);

                    AddressRecipient.AddressType = Enums.AddressType.GiftRecipient;
                    AddressRecipient.Save();
                    CurrentGiftCert.Gift_RecipientAddressId = AddressRecipient.PrimaryKeyIndex;

                    string itemFullName = string.Format("{0} - {1} - {2} - For: {3} {4}",
                                                        "Gift Certificate", CurrentGiftCert.Gift_RedeemCode, CurrentGiftCert.ItemPrice.ToString("c"),
                                                        AddressRecipient.CurrentAddress.FirstName, AddressRecipient.CurrentAddress.LastName);

                    CurrentGiftCert.ItemName = itemFullName;

                    CurrentGiftCert.Save();

                    HealthyChef.Templates.HealthyChef.Controls.TopHeader header =
                        (HealthyChef.Templates.HealthyChef.Controls.TopHeader) this.Page.Master.FindControl("TopHeader1");

                    if (header != null)
                    {
                        header.SetCartCount();
                    }

                    ClearForm();
                    lblFeedback.Text = "Gift certificate has been added to your cart.";
                }
            }
            catch
            {
                throw;
            }
        }
        //public string GetProgramImage(string programName)
        //{
        //    var fileName = "/userfiles/images/programs/" + programName.Replace(" ", "-") + ".jpg";

        //    if (!File.Exists(Server.MapPath(fileName)))
        //        return "";

        //    return "<img width='150' height='126' alt='' src='/userfiles/images/programs/" + programName.Replace(" ", "-") + ".jpg' class='left' />";
        //}

        protected void btn_add_to_cart_Click(object sender, EventArgs e)
        {
            try
            {
                Page.Validate("AddToCartGroup");

                if (Page.IsValid)
                {
                    hccCart userCart = hccCart.GetCurrentCart();

                    //Define form variables
                    int itemId   = Convert.ToInt32(Request.Form["plan_type"]);
                    int optionId = ((Request.Form["plan_option"] == null) ? 0 : Convert.ToInt32(Request.Form["plan_option"]));

                    //Select chosen Program Plan
                    hccProgramPlan plan = hccProgramPlan.GetById(itemId);
                    if (plan == null)
                    {
                        throw new Exception("ProgramPlan not found: " + itemId.ToString());
                    }

                    hccProgram       prog   = hccProgram.GetById(plan.ProgramID);
                    hccProgramOption option = hccProgramOption.GetBy(plan.ProgramID).Where(a => a.ProgramOptionID == optionId).SingleOrDefault();

                    int      numDays      = plan.NumDaysPerWeek * plan.NumWeeks;
                    int      numMeals     = numDays * plan.MealsPerDay;
                    decimal  dailyPrice   = plan.PricePerDay + option.OptionValue;
                    decimal  itemPrice    = numDays * dailyPrice;
                    DateTime deliveryDate = DateTime.Parse(ddl_delivery_date.SelectedValue);

                    MembershipUser user = Helpers.LoggedUser;

                    hccCartItem newItem = new hccCartItem
                    {
                        CartID        = userCart.CartID,
                        CreatedBy     = (user == null ? Guid.Empty : (Guid)user.ProviderUserKey),
                        CreatedDate   = DateTime.Now,
                        IsTaxable     = plan.IsTaxEligible,
                        ItemDesc      = plan.Description,
                        NumberOfMeals = numMeals,
                        //ItemName = string.Format("{0} - {1} - {2} - {3} & {4}", (prog == null ? string.Empty : prog.Name), plan.Name, option.OptionText, deliveryDate.ToShortDateString(), numMeals),
                        ItemName             = string.Format("{0} - {1} - {2} - {3}", (prog == null ? string.Empty : prog.Name), plan.Name, option.OptionText, deliveryDate.ToShortDateString()),
                        ItemPrice            = itemPrice,
                        ItemTypeID           = (int)Enums.CartItemType.DefinedPlan,
                        Plan_IsAutoRenew     = false, //chx_renew.Checked,
                        Plan_PlanID          = itemId,
                        Plan_ProgramOptionID = optionId,
                        DeliveryDate         = deliveryDate,
                        Quantity             = int.Parse(txt_quantity.Text),
                        UserProfileID        = ((ddlProfiles.Items.Count == 0) ? 0 : Convert.ToInt32(ddlProfiles.SelectedValue)),
                        IsCompleted          = false
                    };
                    Meals obj = new Meals();
                    obj.CartID    = newItem.CartID;
                    obj.MealCount = newItem.NumberOfMeals;
                    obj.NoOfWeeks = plan.NumWeeks;

                    var ID    = obj.CartID;
                    var Meal  = obj.MealCount;
                    var Weeks = obj.NoOfWeeks;

                    HealthyChef.Templates.HealthyChef.Controls.TopHeader header =
                        (HealthyChef.Templates.HealthyChef.Controls.TopHeader) this.Page.Master.FindControl("TopHeader1");
                    if (header != null)
                    {
                        header.MealsCountVal(ID, Meal);
                    }

                    newItem.GetOrderNumber(userCart);
                    int profileId = 0;
                    if (divProfiles.Visible)
                    {
                        profileId = int.Parse(ddlProfiles.SelectedValue);
                    }
                    else
                    {
                        if (CartUserASPNetId != Guid.Empty)
                        {
                            profileId = hccUserProfile.GetParentProfileBy(CartUserASPNetId).UserProfileID;
                        }
                    }

                    if (profileId > 0)
                    {
                        newItem.UserProfileID = profileId;
                    }

                    hccCartItem existItem = hccCartItem.GetBy(userCart.CartID, newItem.ItemName, profileId);

                    if (existItem == null)
                    {
                        newItem.Save();

                        hccProductionCalendar cal;

                        for (int i = 0; i < plan.NumWeeks; i++)
                        {
                            cal = hccProductionCalendar.GetBy(newItem.DeliveryDate.AddDays(7 * i));

                            if (cal != null)
                            {
                                hccCartItemCalendar cartCal = new hccCartItemCalendar {
                                    CalendarID = cal.CalendarID, CartItemID = newItem.CartItemID
                                };
                                cartCal.Save();
                            }
                            else
                            {
                                BayshoreSolutions.WebModules.WebModulesAuditEvent.Raise(
                                    "No production calendar found for Delivery Date: " + newItem.DeliveryDate.AddDays(7 * i).ToShortDateString(), this);
                            }
                        }
                    }
                    else
                    {
                        existItem.AdjustQuantity(existItem.Quantity + newItem.Quantity);
                    }

                    //Recurring Order Record
                    if (cbxRecurring.Checked)
                    {
                        List <hccRecurringOrder> lstRo = null;
                        if (Session["autorenew"] != null)
                        {
                            lstRo = ((List <hccRecurringOrder>)Session["autorenew"]);
                        }
                        else
                        {
                            lstRo = new List <hccRecurringOrder>();
                        }

                        //var filter = cartItemsRecurring.Where(ci => ci.ItemType == Enums.CartItemType.DefinedPlan);

                        //for(var i = 0; i < int.Parse(txt_quantity.Text); i++)
                        //{
                        lstRo.Add(new hccRecurringOrder
                        {
                            CartID         = userCart.CartID,
                            CartItemID     = newItem.CartItemID,
                            UserProfileID  = newItem.UserProfileID,
                            AspNetUserID   = userCart.AspNetUserID,
                            PurchaseNumber = userCart.PurchaseNumber,
                            TotalAmount    = newItem.ItemPrice
                        });
                        Session["autorenew"] = lstRo;
                        //}
                    }

                    HealthyChef.Templates.HealthyChef.Controls.TopHeader header1 =
                        (HealthyChef.Templates.HealthyChef.Controls.TopHeader) this.Page.Master.FindControl("TopHeader1");

                    if (header1 != null)
                    {
                        header.SetCartCount();
                    }

                    //Redirect user to Program Selection screen
                    Response.Redirect("~/meal-programs.aspx");
                    //multi_programs.ActiveViewIndex = 0;
                    //litMessage.Text = "Your Meal Program has been added to your cart.";
                }
                else
                {
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        protected void btnAddToCartClick(object sender, EventArgs e)
        {
            bool itemAdded = false;

            try
            {
                Button           btnAddToCart = (Button)sender;
                ListViewDataItem dataItem     = (ListViewDataItem)btnAddToCart.Parent;
                int            menuItemId     = int.Parse(lvwMealItems.DataKeys[dataItem.DataItemIndex].Value.ToString());
                hccMenuItem    menuItem       = hccMenuItem.GetById(menuItemId);
                hccCart        userCart       = hccCart.GetCurrentCart();
                MembershipUser user           = Helpers.LoggedUser;
                int            profileId      = GetProfileId(dataItem);
                int            sizeId         = GetSizeId(btnAddToCart);

                hccCartItem newItem = new hccCartItem
                {
                    CartID            = userCart.CartID,
                    CreatedBy         = (user == null ? Guid.Empty : (Guid)user.ProviderUserKey),
                    CreatedDate       = DateTime.Now,
                    IsTaxable         = menuItem.IsTaxEligible,
                    ItemDesc          = menuItem.Description,
                    ItemPrice         = hccMenuItem.GetItemPriceBySize(menuItem, sizeId),
                    ItemTypeID        = (int)Enums.CartItemType.AlaCarte,
                    DeliveryDate      = CurrentDeliveryDate,
                    Meal_MenuItemID   = menuItem.MenuItemID,
                    Meal_MealSizeID   = sizeId,
                    Meal_ShippingCost = hccDeliverySetting.GetBy(menuItem.MealType).ShipCost,
                    UserProfileID     = profileId,
                    Quantity          = GetQuantity(dataItem),
                    IsCompleted       = false
                };

                newItem.GetOrderNumber(userCart);

                List <CheckBox>     prefChks = new List <CheckBox>();
                List <RepeaterItem> rptPrefs = dataItem.FindControl("rptMealPrefs").Controls.OfType <RepeaterItem>().ToList();
                rptPrefs.ForEach(delegate(RepeaterItem ri) { prefChks.AddRange(ri.Controls.OfType <CheckBox>().Where(a => a.Checked)); });
                string prefsString = string.Empty;
                List <hccCartItemMealPreference> cartPrefs = new List <hccCartItemMealPreference>();

                foreach (CheckBox chkPref in prefChks)
                {
                    int           prefId = int.Parse(chkPref.Attributes["value"]);
                    hccPreference pref   = hccPreference.GetById(prefId);

                    if (pref != null)
                    {
                        if (string.IsNullOrWhiteSpace(prefsString))
                        {
                            prefsString += pref.Name;
                        }
                        else
                        {
                            prefsString += ", " + pref.Name;
                        }

                        cartPrefs.Add(new hccCartItemMealPreference {
                            CartItemID = newItem.CartItemID, PreferenceID = prefId
                        });
                    }
                }

                //newItem.ItemName = hccCartItem.BuildCartItemName(menuItem.MealType, (Enums.CartItemSize)sizeId, menuItem.Name, GetMealSides(dataItem), prefsString, newItem.DeliveryDate, newItem.Quantity);
                newItem.ItemName = hccCartItem.BuildCartItemName(menuItem.MealType, (Enums.CartItemSize)sizeId, menuItem.Name, GetMealSides(dataItem), prefsString, newItem.DeliveryDate);//, newItem.Quantity

                hccCartItem existItem = hccCartItem.GetBy(userCart.CartID, newItem.ItemName, profileId);

                if (existItem == null)
                {
                    newItem.Save();
                    cartPrefs.ForEach(delegate(hccCartItemMealPreference cartPref) { cartPref.CartItemID = newItem.CartItemID; cartPref.Save(); });
                }
                else
                {
                    existItem.Quantity += newItem.Quantity;
                    existItem.AdjustQuantity(existItem.Quantity);
                }

                AddCartALCMenuItem(dataItem, existItem ?? newItem, menuItem.MealTypeID);

                itemAdded = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            if (itemAdded)
            {
                HealthyChef.Templates.HealthyChef.Controls.TopHeader header =
                    (HealthyChef.Templates.HealthyChef.Controls.TopHeader) this.Page.Master.FindControl("TopHeader1");

                if (header != null)
                {
                    header.SetCartCount();
                }
            }
        }