private void SetAvailableData()
    {
        GetAllCustomerGroup();

        AvailableFilteredCustomerGroup = AllGroups.Where(p => !IncludedGroup.Any(inc => inc.CustomerGroupID == p.CustomerGroupID))
                                         .Where(p => !ExcludedGroup.Any(exc => exc.CustomerGroupID == p.CustomerGroupID)).ToList();
        //Hide regular excluded customer groups for new condition
        if (ConditionID == 0 && ExcludedConditionGroup != null && ExcludedConditionGroup.Count > 0)
        {
            AvailableFilteredCustomerGroup = AvailableFilteredCustomerGroup.Where(p => !ExcludedConditionGroup.Any(inc => inc.CustomerGroupID == p.CustomerGroupID)).ToList();
        }


        string strFilter = functioninput.Text;

        if (functionradio1.Checked)
        {
            AvailableFilteredCustomerGroup = AvailableFilteredCustomerGroup.Where(p => p.Name.StartsWith(strFilter, StringComparison.OrdinalIgnoreCase)).ToList();
        }
        else
        {
            AvailableFilteredCustomerGroup = AvailableFilteredCustomerGroup.Where(p => p.Name.IndexOf(strFilter, StringComparison.OrdinalIgnoreCase) >= 0).ToList();
        }


        lstSelected.DataSource = IncludedGroup;
        lstSelected.DataBind();
        lstExcluded.DataSource = ExcludedGroup;
        lstExcluded.DataBind();

        lstAvailable.DataSource = AvailableFilteredCustomerGroup;
        lstAvailable.DataBind();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            Response.Expires = -1;
            infobar.Visible  = false;
            ResolveDepedencies();
            GetQueryStrings();
            bCreateGroupOrProgramFromOffer          = MyCommon.Fetch_CM_SystemOption(134) == "1" ? true : false;
            bEnableRestrictedAccessToUEOfferBuilder = MyCommon.Fetch_SystemOption(249) == "1" ? true : false;
            AssignPageTitle("term.offer", "term.eligibilitycustomercondition", OfferID.ToString());
            if (!IsPostBack)
            {
                SetUpAndLocalizePage();
                CustomerGroupConditions objCustomerGroupConditions = m_CustCondition.GetOfferCustomerCondition(OfferID, EngineID);
                if (objCustomerGroupConditions != null)
                {
                    IncludedConditionGroup = (from p in objCustomerGroupConditions.IncludeCondition
                                              where p.Deleted == false & p.CustomerGroupID != 0
                                              select p.CustomerGroup).ToList();
                    ExcludedConditionGroup = (from p in objCustomerGroupConditions.ExcludeCondition
                                              where p.Deleted == false & p.CustomerGroupID != 0
                                              select p.CustomerGroup).ToList();
                }
                GetOfferEligibleCustomerCondition();
                GetAllCustomerGroup();

                IncludedGroup = (from p in OfferEligibileCustomerGroupCondition.IncludeCondition
                                 where p.Deleted == false
                                 select p.CustomerGroup).ToList();
                List <CustomerGroup> IncludedGroupsWithPhrase = IncludedGroup.Where(p => p.PhraseID != null).ToList();
                foreach (CustomerGroup cgroup in IncludedGroupsWithPhrase)
                {
                    cgroup.Name = PhraseLib.Lookup((Int32)cgroup.PhraseID, LanguageID).Replace("&#39;", "'");
                }

                ExcludedGroup = (from p in OfferEligibileCustomerGroupCondition.ExcludeCondition
                                 where p.Deleted == false
                                 select p.CustomerGroup).ToList();

                SetAvailableData();
                chkDisallow_Edit.Checked = OfferEligibileCustomerGroupCondition.DisallowEdit;
                chkHouseHold.Checked     = OfferEligibileCustomerGroupCondition.HouseHoldEnabled;
                chkOffline.Checked       = OfferEligibileCustomerGroupCondition.EvaluateOfflineCustomer;
                SetButtons();
                DisableControls();
            }
            else
            {
                GetValuesFromHidden();
                ScriptManager.RegisterStartupScript(this, this.GetType(), "selectAndFocus", " SetFoucs();", true);
            }
        }
        catch (Exception ex)
        {
            infobar.InnerText = ErrorHandler.ProcessError(ex);
            infobar.Visible   = true;
        }
    }
    protected void btnCreate_Click(object sender, EventArgs e)
    {
        string Name = string.Empty;

        if (MyCommon.Parse_Quotes(Logix.TrimAll(functioninput.Text)) != null)
        {
            Name = Convert.ToString(MyCommon.Parse_Quotes(Logix.TrimAll(functioninput.Text)));
        }
        if (!String.IsNullOrEmpty(Name))
        {
            int AvailableListCount = AvailableFilteredCustomerGroup.Where(p => p.Name.Equals(functioninput.Text, StringComparison.OrdinalIgnoreCase)).ToList().Count;
            int IncludedGroupCount = IncludedGroup.Where(p => p.Name.Equals(functioninput.Text, StringComparison.OrdinalIgnoreCase)).ToList().Count;
            int ExcludedGroupCount = ExcludedGroup.Where(p => p.Name.Equals(functioninput.Text, StringComparison.OrdinalIgnoreCase)).ToList().Count;

            bool isValidText = true;
            if (functioninput.Text.ToLower().Equals(PhraseLib.Lookup("term.anycardholder", LanguageID).ToLower()) || functioninput.Text.ToLower().Equals(PhraseLib.Lookup("term.anycustomer", LanguageID).ToLower()) || functioninput.Text.ToLower().Equals(PhraseLib.Lookup("term.newcardholders", LanguageID).ToLower()))
            {
                isValidText = false;
            }
            if (!isValidText)
            {
                string alertMessage = Copient.PhraseLib.Lookup("term.enter", LanguageID) + " " + Copient.PhraseLib.Lookup("term.valid", LanguageID).ToLower() + " " + Copient.PhraseLib.Lookup("term.customergroup", LanguageID).ToLower() + " " + Copient.PhraseLib.Lookup("term.name", LanguageID).ToLower();
                ScriptManager.RegisterStartupScript(UpdatePanelMain, UpdatePanelMain.GetType(), "AlertMessage", " AlertMessage('" + alertMessage + "'); ", true);
            }
            else if (IncludedGroupCount > 0 || ExcludedGroupCount > 0)
            {
                string alertMessage = Copient.PhraseLib.Lookup("term.customergroup", LanguageID) + ": " + Name + " " + Copient.PhraseLib.Lookup("term.is", LanguageID).ToLower() + " " + Copient.PhraseLib.Lookup("term.already", LanguageID).ToLower() + " " + Copient.PhraseLib.Lookup("term.selected", LanguageID).ToLower();
                ScriptManager.RegisterStartupScript(UpdatePanelMain, UpdatePanelMain.GetType(), "AlertMessage", " AlertMessage('" + alertMessage + "');", true);
            }
            else if (AvailableListCount > 0)
            {
                string alertMessage = Copient.PhraseLib.Lookup("term.existing", LanguageID) + " " + Copient.PhraseLib.Lookup("term.customergroup", LanguageID).ToLower() + ": " + Name + " " + Copient.PhraseLib.Lookup("offer.message", LanguageID).ToLower();
                ScriptManager.RegisterStartupScript(UpdatePanelMain, UpdatePanelMain.GetType(), "AlertMessage", " AlertMessage('" + alertMessage + "');", true);

                IncludedGroup.Add(AvailableFilteredCustomerGroup.Where(p => p.Name.Equals(functioninput.Text, StringComparison.OrdinalIgnoreCase)).FirstOrDefault());
                HandleSelectedForSpecialGroup();
                SetAvailableData();
                SetButtons();
            }
            else
            {
                IncludedGroup.Add(CreateCustomerGroup());
                SetAvailableData();
                SetButtons();
            }
        }
        else
        {
            string alertMessage = Copient.PhraseLib.Lookup("term.enter", LanguageID) + " " + Copient.PhraseLib.Lookup("term.valid", LanguageID).ToLower() + " " + Copient.PhraseLib.Lookup("term.customergroup", LanguageID).ToLower() + " " + Copient.PhraseLib.Lookup("term.name", LanguageID).ToLower();
            ScriptManager.RegisterStartupScript(UpdatePanelMain, UpdatePanelMain.GetType(), "AlertMessage", " AlertMessage('" + alertMessage + "');", true);
        }
    }
 protected void select1_Click(object sender, EventArgs e)
 {
     if (lstAvailable.SelectedItem != null)
     {
         foreach (int i in lstAvailable.GetSelectedIndices())
         {
             IncludedGroup.Add(AvailableFilteredCustomerGroup[i]);
         }
         HandleSelectedForSpecialGroup();
         SetAvailableData();
     }
     SetButtons();
 }
    private void HandleSelectedForSpecialGroup()
    {
        var group = IncludedGroup.Where(p => p.CustomerGroupID == SystemCacheData.GetAnyCardHolderGroup().CustomerGroupID || p.CustomerGroupID == SystemCacheData.GetAnyCustomerGroup().CustomerGroupID).SingleOrDefault();

        if (group != null)
        {
            IncludedGroup.Clear();
            IncludedGroup.Add(group);
            select1.Enabled = false;
        }
        else
        {
            select1.Enabled = true;
        }
    }
    protected void deselect1_Click(object sender, EventArgs e)
    {
        if (lstSelected.SelectedItem != null)
        {
            if (ExcludedGroup.Count > 0 && lstSelected.GetSelectedIndices().Count() == IncludedGroup.Count())
            {
                infobar.InnerText = PhraseLib.Lookup("term-ValidationOnDeleteForAllSelected", LanguageID).Replace("&#39;", "'");
                infobar.Visible   = true;
                return;
            }

            //need to reverse the order - find the issue in case of removing the items from index 0
            var desc = from j in lstSelected.GetSelectedIndices().ToList()
                       orderby j descending
                       select j;

            foreach (int i in desc)
            {
                IncludedGroup.RemoveAt(i);
            }
            SetAvailableData();
        }
        SetButtons();
    }
    private void Save()
    {
        bool   isNewCondition   = false;
        string strIncludedGroup = string.Empty;
        string strExcludedGroup = string.Empty;

        try
        {
            if (OfferEligibileCustomerGroupCondition.ConditionID == 0)
            {
                isNewCondition = true;
            }
            if (chkDisallow_Edit.Visible)
            {
                OfferEligibileCustomerGroupCondition.DisallowEdit = chkDisallow_Edit.Checked;
            }

            if (spnHouseHold.Visible)
            {
                OfferEligibileCustomerGroupCondition.HouseHoldEnabled = chkHouseHold.Checked;
            }

            if (spnOffline.Visible)
            {
                OfferEligibileCustomerGroupCondition.EvaluateOfflineCustomer = chkOffline.Checked;
            }

            //Updated Include List
            var deletedlist = OfferEligibileCustomerGroupCondition.IncludeCondition.Where(p => !IncludedGroup.Any(inc => inc.CustomerGroupID == p.CustomerGroupID));
            foreach (CMS.AMS.Models.CustomerConditionDetails custdetail in deletedlist)
            {
                custdetail.Deleted = true;
            }
            historyString = PhraseLib.Lookup("history.con-customer-edit", LanguageID) + ": ";
            foreach (CMS.AMS.Models.CustomerGroup CustGroup in IncludedGroup)
            {
                if (!OfferEligibileCustomerGroupCondition.IncludeCondition.Exists(p => p.CustomerGroupID == CustGroup.CustomerGroupID))
                {
                    CMS.AMS.Models.CustomerConditionDetails condetail = new CMS.AMS.Models.CustomerConditionDetails();
                    condetail.CustomerGroupID = CustGroup.CustomerGroupID;
                    OfferEligibileCustomerGroupCondition.IncludeCondition.Add(condetail);
                    strIncludedGroup = strIncludedGroup + CustGroup.CustomerGroupID.ToString() + ",";
                }
            }
            historyString = historyString + strIncludedGroup.TrimEnd(',');
            //Update Exclude List exc

            var deletedexclist = OfferEligibileCustomerGroupCondition.ExcludeCondition.Where(p => !ExcludedGroup.Any(exc => exc.CustomerGroupID == p.CustomerGroupID));
            foreach (CMS.AMS.Models.CustomerConditionDetails custdetail in deletedexclist)
            {
                custdetail.Deleted = true;
            }
            bool IsExcludedExist = false;
            foreach (CMS.AMS.Models.CustomerGroup CustGroup in ExcludedGroup)
            {
                if (!OfferEligibileCustomerGroupCondition.ExcludeCondition.Exists(p => p.CustomerGroupID == CustGroup.CustomerGroupID))
                {
                    CMS.AMS.Models.CustomerConditionDetails condetail = new CMS.AMS.Models.CustomerConditionDetails();
                    condetail.CustomerGroupID = CustGroup.CustomerGroupID;
                    strExcludedGroup          = strExcludedGroup + CustGroup.CustomerGroupID.ToString() + ",";
                    OfferEligibileCustomerGroupCondition.ExcludeCondition.Add(condetail);
                    IsExcludedExist = true;
                }
            }
            if (ExcludedConditionGroup != null)
            {
                //if it is a new condition then add excluded customer groups which are currently exist in regualr customer condition
                if (isNewCondition)
                {
                    foreach (CMS.AMS.Models.CustomerGroup CustGroup in ExcludedConditionGroup)
                    {
                        if (!OfferEligibileCustomerGroupCondition.ExcludeCondition.Exists(p => p.CustomerGroupID == CustGroup.CustomerGroupID))
                        {
                            CMS.AMS.Models.CustomerConditionDetails condetail = new CMS.AMS.Models.CustomerConditionDetails();
                            condetail.CustomerGroupID = CustGroup.CustomerGroupID;
                            strExcludedGroup          = strExcludedGroup + CustGroup.CustomerGroupID.ToString() + ",";
                            OfferEligibileCustomerGroupCondition.ExcludeCondition.Add(condetail);
                            IsExcludedExist = true;
                        }
                    }
                }
                else
                {
                    //if it is an existing eligibility condition and user attempt to remove group from excluded condition which is exist in regular excluded condition then ask user to delete remove the same from regualr condition as well
                    var         mustExcludedList = deletedexclist.Where(p => ExcludedConditionGroup.Any(exc => exc.CustomerGroupID == p.CustomerGroupID)).Select(z => z.CustomerGroup);
                    List <long> ExcludedGroupIds = new List <long>();
                    foreach (CustomerGroup item in mustExcludedList)
                    {
                        ExcludedGroupIds.Add(item.CustomerGroupID);
                    }
                    if (ExcludedGroupIds.Count > 0)
                    {
                        //Delete the excluded condition
                        m_CustCondition.DeleteRegulerExcludedConditionsByCustomerGroupIDs(OfferID, EngineID, ExcludedGroupIds);
                    }
                }
            }
            if (IsExcludedExist)
            {
                historyString = historyString + " " + PhraseLib.Lookup("term.excluding", LanguageID) + " " + strExcludedGroup.TrimEnd(',');
            }
            m_offer.CreateUpdateOfferEligibleCustomerCondition(OfferID, EngineID, OfferEligibileCustomerGroupCondition);
            if (isNewCondition)
            {
                CMS.AMS.Models.CustomerGroup CustomerGroup = new CMS.AMS.Models.CustomerGroup();
                CustomerGroup.Name         = string.Format(Constants.DEFAULT_OFFER_GROUP_NAME, hdnOfferName.Value);
                CustomerGroup.IsOptinGroup = true;
                m_CustGroup.CreateOptInCustomerGroup(CustomerGroup);
                CMS.AMS.Models.CustomerGroupConditions CustomerCondition = new CMS.AMS.Models.CustomerGroupConditions();
                CustomerCondition.DisallowEdit         = OfferEligibileCustomerGroupCondition.DisallowEdit;
                CustomerCondition.RequiredFromTemplate = OfferEligibileCustomerGroupCondition.RequiredFromTemplate;
                CustomerCondition.IncludeCondition     = new List <CMS.AMS.Models.CustomerConditionDetails>();
                CustomerCondition.IncludeCondition.Add(new CMS.AMS.Models.CustomerConditionDetails());
                CustomerCondition.IncludeCondition[0].CustomerGroupID = CustomerGroup.CustomerGroupID;
                m_offer.CreateDefaultCustomerCondition(OfferID, EngineID, CustomerCondition);
            }
            m_offer.UpdateOfferStatusToModified(OfferID, EngineID, CurrentUser.AdminUser.ID);
            m_OAWService.ResetOfferApprovalStatus(OfferID);
            WriteToActivityLog();
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Close", "CloseModel()", true);
        }
        catch (Exception ex)
        {
            infobar.InnerText = ErrorHandler.ProcessError(ex);
            infobar.Visible   = true;
        }
    }