protected void lbSave_Click(object sender, EventArgs e)
        {
            string urlKey = txtUrlKey.Text.Trim();

            // If urlKey is empty, regenerate with given name
            if (urlKey == string.Empty)
            {
                urlKey         = AdminStoreUtility.GetFriendlyUrlKey(txtRuleAlias.Text.Trim());
                txtUrlKey.Text = urlKey;
            }

            // Make sure urlKey is unique
            if (((OfferService.GetOfferRuleByUrlKey(urlKey) != null) && (OfferService.GetOfferRuleByUrlKey(urlKey).Id != QueryOfferRuleId)))
            {
                enbNotice.Message = "Offer was failed to save. URL key is not unique.";
            }
            else
            {
                string htmlMsg;

                if (ftbDesc.Visible)
                {
                    htmlMsg = AdminStoreUtility.CleanFtbOutput(ftbDesc.Text);
                }
                else
                {
                    htmlMsg = txtDescription.Text.Trim();
                }

                // Retrieve rule from database
                var rule = OfferService.GetOfferRuleById(QueryOfferRuleId);

                rule.Name                 = txtRuleName.Text.Trim();
                rule.ProceedForNext       = Convert.ToBoolean(rblProceed.SelectedValue);
                rule.IsActive             = Convert.ToBoolean(rblStatus.SelectedValue);
                rule.PromoCode            = txtPromoCode.Text.Trim();
                rule.UsesPerCustomer      = Convert.ToInt32(txtUsesPerCust.Text.Trim());
                rule.Priority             = Convert.ToInt32(txtPriority.Text.Trim());
                rule.HtmlMessage          = htmlMsg;
                rule.OfferedItemIncluded  = cbOfferedItemIncluded.Checked;
                rule.Alias                = txtRuleAlias.Text.Trim();
                rule.PointSpendable       = chkPointSpendable.Checked;
                rule.UrlRewrite           = urlKey;
                rule.UseInitialPrice      = cbUseInitialPrice.Checked;
                rule.NewCustomerOnly      = cbNewCustomerOnly.Checked;
                rule.ShowCountDown        = cbShowCountDownTimer.Checked;
                rule.DisplayOnProductPage = cbDisplayOnProductPage.Checked;
                rule.OfferTypeId          = Convert.ToInt32(ddlOfferTypes.SelectedValue);

                if (ddlRelatedTypes.SelectedValue != string.Empty)
                {
                    switch (ddlRelatedTypes.SelectedValue)
                    {
                    case "products":
                        rule.RelatedProducts = txtRelatedItems.Text.ToString();
                        break;

                    case "brands":
                        rule.RelatedBrands = txtRelatedItems.Text.ToString();
                        break;

                    case "category":
                        rule.RelatedCategory = txtRelatedItems.Text.ToString();
                        break;
                    }
                }
                else
                {
                    rule.RelatedProducts = string.Empty;
                    rule.RelatedBrands   = string.Empty;
                    rule.RelatedCategory = string.Empty;
                }

                if (!string.IsNullOrEmpty(txtDateFrom.Text.Trim()))
                {
                    rule.StartDate = DateTime.ParseExact(txtDateFrom.Text, AppConstant.DATE_FORM1, CultureInfo.InvariantCulture);
                }
                else
                {
                    rule.StartDate = null;
                }

                if (!string.IsNullOrEmpty(txtDateTo.Text.Trim()))
                {
                    rule.EndDate = DateTime.ParseExact(txtDateTo.Text, AppConstant.DATE_FORM1, CultureInfo.InvariantCulture);
                }
                else
                {
                    rule.EndDate = null;
                }

                rule.ShortDescription = txtShortDescription.Text;

                string longDesc;

                if (!ftbLongDesc.Visible && txtLongDesc.Visible)
                {
                    if (txtLongDesc.Text != string.Empty)
                    {
                        longDesc = txtLongDesc.Text.Trim();
                    }
                    else
                    {
                        longDesc = txtShortDescription.Text.Trim();
                    }
                }
                else if (ftbLongDesc.Visible)
                {
                    longDesc = AdminStoreUtility.CleanFtbOutput(ftbLongDesc.Text);
                }
                else
                {
                    longDesc = txtShortDescription.Text.Trim();
                }

                rule.LongDescription      = longDesc;
                rule.ShowInOfferPage      = chkShowOnOfferPage.Checked;
                rule.DisplayOnHeaderStrip = chkDisplayOnHeaderStrip.Checked;
                rule.OfferLabel           = txtOfferLabel.Text.Trim();
                rule.DisableOfferLabel    = cbDisableOfferLabel.Checked;
                rule.OfferUrl             = txtViewOfferURL.Text.Trim();

                if (fuSmallImage.HasFile)
                {
                    string filename = rule.Id.ToString() + "_small" + Path.GetExtension(fuSmallImage.FileName).ToLower();
                    fuSmallImage.SaveAs(MediaSettings.OfferMediaLocalPath + filename);

                    rule.SmallImage = filename;
                }

                if (fuLargeImage.HasFile)
                {
                    string filename = rule.Id.ToString() + "_large" + Path.GetExtension(fuLargeImage.FileName).ToLower();
                    fuLargeImage.SaveAs(MediaSettings.OfferMediaLocalPath + filename);

                    rule.LargeImage = filename;
                }

                object freeProductItself  = DBNull.Value;
                object freeProductId      = DBNull.Value;
                object freeProductPriceId = DBNull.Value;
                object freeProductQty     = DBNull.Value;

                // Determine if there is any free item settings
                if (rbFreeItself.Checked || rbFreeItem.Checked)
                {
                    freeProductItself = rbFreeItself.Checked;

                    if (rbFreeItem.Checked)
                    {
                        freeProductId      = txtFreeProductId.Text.Trim();
                        freeProductPriceId = txtFreeProductPriceId.Text.Trim();
                        freeProductQty     = txtFreeQuantity.Text.Trim();
                    }
                }

                object discountQtyStep = DBNull.Value;
                object minimumAmount   = DBNull.Value;
                object discountAmount  = DBNull.Value;
                int    rewardPoint     = 0;

                if (txtDiscountQtyStep.Text.Trim() != string.Empty)
                {
                    discountQtyStep = txtDiscountQtyStep.Text.Trim();
                }

                if (txtMinimumAmount.Text.Trim() != string.Empty)
                {
                    minimumAmount = txtMinimumAmount.Text.Trim();
                }

                if (txtDiscountAmount.Text.Trim() != string.Empty)
                {
                    discountAmount = txtDiscountAmount.Text.Trim();
                }

                if (txtRewardPoint.Text.Trim() != string.Empty)
                {
                    rewardPoint = Convert.ToInt32(txtRewardPoint.Text.Trim());
                }

                // Retrieve size target if any
                object optionOperatorId = DBNull.Value;
                object optionOperator   = DBNull.Value;
                object optionOperand    = DBNull.Value;

                if (txtOption.Text.Trim() != string.Empty)
                {
                    optionOperand    = txtOption.Text.Trim();
                    optionOperatorId = ddlOptionOperator.SelectedValue;
                    optionOperator   = OfferService.GetOfferOperator(Convert.ToInt32(optionOperatorId)).Operator;
                }

                int offerActionId = OfferService.GetOfferRuleById(QueryOfferRuleId).Action.Id;

                // Assign action to this rule object's action
                var newAction = new OfferAction
                {
                    Id          = offerActionId,
                    OfferRuleId = QueryOfferRuleId
                };

                if (discountAmount != DBNull.Value)
                {
                    newAction.DiscountAmount = Convert.ToDecimal(discountAmount);
                }
                if (freeProductItself != DBNull.Value)
                {
                    newAction.FreeProductItself = Convert.ToBoolean(freeProductItself);
                }
                if (freeProductId != DBNull.Value)
                {
                    newAction.FreeProductId = Convert.ToInt32(freeProductId);
                }
                if (freeProductPriceId != DBNull.Value)
                {
                    newAction.FreeProductPriceId = Convert.ToInt32(freeProductPriceId);
                }
                if (freeProductQty != DBNull.Value)
                {
                    newAction.FreeProductQty = Convert.ToInt32(freeProductQty);
                }
                newAction.OfferActionAttributeId = Convert.ToInt32(ddlAction.SelectedValue);
                newAction.Name   = ddlAction.SelectedItem.Text;
                newAction.IsCart = true;
                if (discountQtyStep != DBNull.Value)
                {
                    newAction.DiscountQtyStep = Convert.ToInt32(discountQtyStep);
                }
                if (minimumAmount != DBNull.Value)
                {
                    newAction.MinimumAmount = Convert.ToDecimal(minimumAmount);
                }
                newAction.RewardPoint = rewardPoint;
                if (optionOperatorId != DBNull.Value)
                {
                    newAction.OptionOperatorId = Convert.ToInt32(optionOperatorId);
                }
                if (optionOperand != DBNull.Value)
                {
                    newAction.OptionOperand = Convert.ToString(optionOperand);
                }

                if ((optionOperator != DBNull.Value) && (optionOperatorId != DBNull.Value))
                {
                    newAction.OptionOperator = new OfferOperator {
                        Id       = Convert.ToInt32(optionOperatorId),
                        Operator = Convert.ToString(optionOperator)
                    }
                }
                ;

                newAction.XValue = Convert.ToInt32(txtXValue.Text.Trim());
                newAction.YValue = Convert.ToDecimal(txtYValue.Text.Trim());

                rule.Action = newAction;

                var proceed = true;

                // Assign root condition to this rule object's condition
                rule.Condition = OfferUtility.OfferRuleConditions[rule.Id];

                if (rule.Condition == null)
                {
                    enbNotice.Message = "Offer was failed to update. There is no condition setup for this offer. It could be due to session lost. Please try to create again.";
                    proceed           = false;
                }

                // Assign root condition to this action object's condition
                rule.Action.Condition = OfferUtility.OfferActionConditions[rule.Id];

                if (rule.Action.Condition == null)
                {
                    enbNotice.Message = "Offer was failed to update. There is no <u>action</u> condition setup for this offer. It could be due to session lost. Please try to create again.";
                    proceed           = false;
                }

                if (proceed)
                {
                    // Update database
                    var success = OfferService.ProcessOfferUpdation(rule);

                    if (success)
                    {
                        enbNotice.Message = "Offer was updated successfully.";
                    }
                    else
                    {
                        enbNotice.Message = "Offer was failed to update.";
                    }
                }

                LoadCartOfferInfo();
            }
        }
        protected void lbSave_Click(object sender, EventArgs e)
        {
            string urlKey = txtUrlKey.Text.Trim();

            // If urlKey is empty, regenerate with given name
            if (urlKey == string.Empty)
            {
                urlKey         = AdminStoreUtility.GetFriendlyUrlKey(txtRuleAlias.Text.Trim());
                txtUrlKey.Text = urlKey;
            }

            // Make sure urlKey is unique
            if (((OfferService.GetOfferRuleByUrlKey(urlKey) != null) && (OfferService.GetOfferRuleByUrlKey(urlKey).Id != QueryOfferRuleId)))
            {
                enbNotice.Message = "Offer was failed to save. URL key is not unique.";
            }
            else
            {
                var rule = OfferService.GetOfferRuleById(QueryOfferRuleId);

                rule.UrlRewrite     = urlKey;
                rule.Name           = txtRuleName.Text.Trim();
                rule.ProceedForNext = Convert.ToBoolean(rblProceed.SelectedValue);
                rule.IsActive       = Convert.ToBoolean(rblStatus.SelectedValue);
                rule.Priority       = Convert.ToInt32(txtPriority.Text.Trim());
                rule.Alias          = txtRuleAlias.Text.Trim();
                rule.ShowOfferTag   = chkShowOfferTag.Checked;
                rule.ShowRRP        = chkShowRRP.Checked;
                rule.OfferUrl       = txtViewOfferURL.Text.Trim();
                rule.PointSpendable = chkPointSpendable.Checked;
                rule.ShowCountDown  = cbShowCountDownTimer.Checked;

                if (!string.IsNullOrEmpty(txtDateFrom.Text.Trim()))
                {
                    rule.StartDate = DateTime.ParseExact(txtDateFrom.Text, AppConstant.DATE_FORM1, CultureInfo.InvariantCulture);
                }
                else
                {
                    rule.StartDate = null;
                }

                if (!string.IsNullOrEmpty(txtDateTo.Text.Trim()))
                {
                    rule.EndDate = DateTime.ParseExact(txtDateTo.Text, AppConstant.DATE_FORM1, CultureInfo.InvariantCulture);
                }
                else
                {
                    rule.EndDate = null;
                }

                rule.ShortDescription = txtShortDescription.Text;

                string htmlMsg;

                if (ftbLongDesc.Visible)
                {
                    htmlMsg = AdminStoreUtility.CleanFtbOutput(ftbLongDesc.Text);
                }
                else
                {
                    htmlMsg = txtShortDescription.Text.Trim();
                }

                rule.LongDescription      = htmlMsg;
                rule.ShowInOfferPage      = chkShowOnOfferPage.Checked;
                rule.DisplayOnHeaderStrip = chkDisplayOnHeaderStrip.Checked;
                rule.OfferLabel           = txtOfferLabel.Text.Trim();
                rule.DisableOfferLabel    = cbDisableOfferLabel.Checked;
                rule.DisplayOnProductPage = cbDisplayOnProductPage.Checked;
                rule.OfferTypeId          = Convert.ToInt32(ddlOfferTypes.SelectedValue);

                if (ddlRelatedTypes.SelectedValue != string.Empty)
                {
                    switch (ddlRelatedTypes.SelectedValue)
                    {
                    case "products":
                        rule.RelatedProducts = txtRelatedItems.Text.ToString();
                        break;

                    case "brands":
                        rule.RelatedBrands = txtRelatedItems.Text.ToString();
                        break;

                    case "category":
                        rule.RelatedCategory = txtRelatedItems.Text.ToString();
                        break;
                    }
                }
                else
                {
                    rule.RelatedProducts = string.Empty;
                    rule.RelatedBrands   = string.Empty;
                    rule.RelatedCategory = string.Empty;
                }

                if (fuSmallImage.HasFile)
                {
                    string filename = rule.Id.ToString() + "_small" + Path.GetExtension(fuSmallImage.FileName).ToLower();
                    fuSmallImage.SaveAs(MediaSettings.OfferMediaLocalPath + filename);

                    rule.SmallImage = filename;
                }

                if (fuLargeImage.HasFile)
                {
                    string filename = rule.Id.ToString() + "_large" + Path.GetExtension(fuLargeImage.FileName).ToLower();
                    fuLargeImage.SaveAs(MediaSettings.OfferMediaLocalPath + filename);

                    rule.LargeImage = filename;
                }

                var newAction = new OfferAction
                {
                    Id                     = rule.Action.Id,
                    OfferRuleId            = rule.Action.OfferRuleId,
                    DiscountAmount         = Convert.ToDecimal(txtDiscountAmount.Text),
                    OfferActionAttributeId = Convert.ToInt32(ddlAction.SelectedValue),
                    Name                   = ddlAction.SelectedItem.Text,
                    IsCatalog              = true
                };

                // Retrieve size target if any
                string optionOperatorId = null;
                string optionOperator   = null;
                string optionOperand    = null;

                if (txtOption.Text.Trim() != string.Empty)
                {
                    optionOperand    = txtOption.Text.Trim();
                    optionOperatorId = ddlOptionOperator.SelectedValue;
                    optionOperator   = OfferService.GetOfferOperator(Convert.ToInt32(optionOperatorId)).Operator;
                }

                if (!string.IsNullOrEmpty(optionOperatorId))
                {
                    newAction.OptionOperatorId = Convert.ToInt32(optionOperatorId);
                }
                if (!string.IsNullOrEmpty(optionOperand))
                {
                    newAction.OptionOperand = Convert.ToString(optionOperand);
                }
                if ((!string.IsNullOrEmpty(optionOperator)) && (!string.IsNullOrEmpty(optionOperatorId)))
                {
                    newAction.OptionOperator = new OfferOperator
                    {
                        Id       = Convert.ToInt32(optionOperatorId),
                        Operator = Convert.ToString(optionOperator)
                    }
                }
                ;

                rule.Action = newAction;

                var proceed = true;

                // Assign root condition to this rule object's condition
                rule.Condition = OfferUtility.OfferRuleConditions[rule.Id];

                if (rule.Condition == null)
                {
                    enbNotice.Message = "Offer was failed to update. There is no condition setup for this offer. It could be due to session lost. Please try to create again.";
                    proceed           = false;
                }

                if (proceed)
                {
                    // Update database
                    var success = OfferService.ProcessOfferUpdation(rule);

                    if (success)
                    {
                        enbNotice.Message = "Offer was updated successfully.";
                    }
                    else
                    {
                        enbNotice.Message = "Offer was failed to update.";
                    }

                    LoadCatalogInfo();
                }
            }
        }