//void CheckedSelectedItems()
    //{
    //    foreach (RepeaterItem item in rep.Items)
    //    {
    //        CheckBox chk = (CheckBox)item.FindControl("chk");
    //        HiddenField hidProductID = (HiddenField)item.FindControl("hidProductID");

    //        if (chk.Checked)
    //        {
    //            if (!pgc.SelectedIDs.Contains(int.Parse(hidProductID.Value)))
    //            {
    //                pgc.SelectedIDs.Add(int.Parse(hidProductID.Value));
    //                //ProductMachineItems.Add(prepareCartRecord(item));
    //            }
    //        }
    //        else
    //        {
    //            if (pgc.SelectedIDs.Contains(int.Parse(hidProductID.Value)))
    //            {
    //                pgc.SelectedIDs.Remove(int.Parse(hidProductID.Value));
    //                //ProductMachineItems.Remove(prepareCartRecord(item));
    //            }
    //        }
    //    }
    //}
    void PopulateGrid()
    {
        ListData res = new ListData(1, 1, 1);
        dsProduct.ProductDataTable tbl = da.Product_Search(txtSearchCode.Text, txtSearchName.Text, (int.Parse(rdRecon.SelectedValue) == 1 ? 0 : 1), DateTime.Now, (rdIsPromotion.SelectedValue == "1" ? true : false), out res, pgc.CurrentPage);
        pgc.Populate(res);

        byte[] ls = new byte[tbl.Rows.Count];
        rep.DataSource = ls;
        rep.DataBind();

        for (int i = 0; i <= ls.Length - 1; i++)
        {
            dsProduct.ProductRow drProduct = (dsProduct.ProductRow)tbl.Rows[i];
            RepeaterItem item = (RepeaterItem)rep.Items[i];
            HtmlTableRow tr = (HtmlTableRow)item.FindControl("trRow");
            tr.Attributes.Add("class", (i % 2 == 0) ? "gridData" : "gridDataAlt");

            HiddenField hidProductID = (HiddenField)item.FindControl("hidProductID");
            hidProductID.Value = drProduct.ID.ToString();

            CheckBox chk = (CheckBox)item.FindControl("chk");

            LinkButton lnkDetails = (LinkButton)item.FindControl("lnkDetails");
            lnkDetails.Text = drProduct.Name;
            lnkDetails.CommandArgument = drProduct.ID.ToString();
            
            Literal litCode = (Literal)item.FindControl("litCode");
            litCode.Text = drProduct.Code;

            Button btnAddToCart = (Button)item.FindControl("btnAddToCart");
            btnAddToCart.CommandArgument = i.ToString();

            int quantity = 1;
            string price = "0.00 (Unknown Tier)";

            dsPOProductItem _dsPOProductItem = new dsPOProductItem();
            dsPOProductItem.POProductItemRow drPOProductItem = da.POProductItem_GetPOQuantity(WebLib.LoggedInUser.DealerUserID, new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1), DateTime.Now);
            if (drPOProductItem != null && drPOProductItem.Quantity > 0)
                    quantity = drPOProductItem.Quantity + 1;

            dsProductPriceTier _dsProductPriceTier = new dsProductPriceTier();
            dsProductPriceTier.ProductPriceTierRow drProductPriceTier = da.ProductPriceTier_Get(drProduct.ID, quantity);
            if (drProductPriceTier != null)
            {
                switch (drProductPriceTier.TierName)
                {
                    case "Tier1":
                        //price = drProductPriceTier.Price.ToString(WebLib.CurrencyFormat) + " (Tier 1)";
                        price = drProductPriceTier.Price.ToString(WebLib.CurrencyFormat) + " (Normal)";
                        break;

                    case "Tier2":
                        //price = drProductPriceTier.Price.ToString(WebLib.CurrencyFormat) + " (Tier 2)";
                        price = drProductPriceTier.Price.ToString(WebLib.CurrencyFormat) + " (Normal)";
                        break;

                    case "Tier3":
                        //price = drProductPriceTier.Price.ToString(WebLib.CurrencyFormat) + " (Tier 3)";
                        price = drProductPriceTier.Price.ToString(WebLib.CurrencyFormat) + " (Normal)";
                        break;

                    case "NoTier":
                        //price = drProductPriceTier.Price.ToString(WebLib.CurrencyFormat) + " (No Tier)";
                        price = drProductPriceTier.Price.ToString(WebLib.CurrencyFormat) + " (Normal)";
                        break;
                }
            }

            Label lblPrice = (Label)item.FindControl("lblPrice");
            lblPrice.Text = price;
            
            TextBox txtPrice = (TextBox)item.FindControl("txtPrice");
            txtPrice.Text = "0.00";

            RadioButtonList rdOrderType = (RadioButtonList)item.FindControl("rdOrderType");
            rdOrderType.Attributes.Add("onClick", "javascript:CheckedOrderType('" + rdOrderType.ClientID + "','" + lblPrice.ClientID + "','" + txtPrice.ClientID + "');");

            //if (pgc.SelectedIDs.Contains(drProduct.ID))
            //{
            //    chk.Checked = true;
            //}
            //else
            //{
            //    chk.Checked = false;
            //}
        }    
    }
    private void BindDataMachine()
    {
        dsCart.ViewProductCartDataTable tblCart = da.Cart_DisplayMachine(WebLib.LoggedInUser.DealerUserID, true);

        byte[] ls = new byte[tblCart.Rows.Count];
        repMachine.DataSource = ls;
        repMachine.DataBind();

        int totalQuantityMachineItem = da.Cart_TotalMachine(WebLib.LoggedInUser.DealerUserID);
        for (int i = 0; i <= ls.Length - 1; i++)
        {
            dsCart.ViewProductCartRow drProductCart = (dsCart.ViewProductCartRow)tblCart.Rows[i];
            RepeaterItem item = (RepeaterItem)repMachine.Items[i];

            totalQuantityMachine += drProductCart.Quantity;
        }

        for (int i = 0; i <= ls.Length - 1; i++)
        {
            dsCart.ViewProductCartRow drProductCart = (dsCart.ViewProductCartRow)tblCart.Rows[i];
            RepeaterItem item = (RepeaterItem)repMachine.Items[i];
            HtmlTableRow tr = (HtmlTableRow)item.FindControl("trRow");
            tr.Attributes.Add("class", (i % 2 == 0) ? "GridRow2" : "GridRowAlt2");

            Literal litNo = (Literal)item.FindControl("litNo");
            litNo.Text = (i + 1).ToString() + ".";

            Literal litEDPCode = (Literal)item.FindControl("litEDPCode");
            litEDPCode.Text = drProductCart.Code;

            Literal litName = (Literal)item.FindControl("litName");
            litName.Text = drProductCart.Name +(drProductCart.IsPromotion ? " <b>[Promotion]</b>" : "");

            Literal litUnitPrice = (Literal)item.FindControl("litUnitPrice");
            litUnitPrice.Text = (drProductCart.UnitPrice).ToString(WebLib.CurrencyFormat);

            TextBox txtQuantity = (TextBox)item.FindControl("txtQuantity");
            txtQuantity.Text = drProductCart.Quantity.ToString();

            LinkButton lnkUpdateCart = (LinkButton)item.FindControl("lnkUpdateCart");
            lnkUpdateCart.CommandArgument = drProductCart.ID.ToString();

            LinkButton lnkRemove = (LinkButton)item.FindControl("lnkRemove");
            lnkRemove.CommandArgument = drProductCart.ID.ToString();

            Repeater repService = (Repeater)item.FindControl("repService");

            decimal totalOptionalItemPrice = 0;

            if (drProductCart.ItemTypeEnum == (short)ItemType.Accessory)
            {
                Literal litTotalPrice = (Literal)item.FindControl("litTotalPrice");
                litTotalPrice.Text = ((drProductCart.UnitPrice * drProductCart.Quantity)).ToString(WebLib.CurrencyFormat);
                totalPriceMachine += Decimal.Round((drProductCart.UnitPrice * drProductCart.Quantity), 2);
            }
            else
            {
                string tempOptionals = "";

                #region Machine Item
                Repeater repMachineItem = (Repeater)item.FindControl("repMachineItem");
                dsMachineItem.MachineItemDataTable tblItem = da.MachineItem_Search(drProductCart.CartItemID, (int)ProductItemType.Machine, true);
                if (tblItem.Rows.Count > 0)
                {
                    repMachineItem.DataSource = new byte[tblItem.Rows.Count];
                    repMachineItem.DataBind();

                    tr = (HtmlTableRow)repMachineItem.Controls[0].Controls[0].FindControl("trRow");
                    tr.Attributes.Add("class", (i % 2 == 0) ? "GridRow2" : "GridRowAlt2");
                    int machineCount = 0;
                    //tempOptionals += "<br /><b><u>Service</u></b>";
                    foreach (dsMachineItem.MachineItemRow drItem in tblItem.Rows)
                    {
                        tr = (HtmlTableRow)repMachineItem.Items[machineCount].FindControl("trRow");
                        tr.Attributes.Add("class", (i % 2 == 0) ? "GridRow2" : "GridRowAlt2");

                        Literal litMachineEDPCode = (Literal)repMachineItem.Items[machineCount].FindControl("litMachineEDPCode");
                        litMachineEDPCode.Text = drItem.Code;

                        Literal litMachineName = (Literal)repMachineItem.Items[machineCount].FindControl("litMachineName");
                        litMachineName.Text = drItem.Name;

                        //tempOptionals += drItem.Name + "<br />";
                        machineCount++;
                    }
                }
                #endregion

                tblItem = da.MachineItem_Search(drProductCart.CartItemID, (int)ProductItemType.Service, true);
                if (tblItem.Rows.Count > 0)
                {
                    repService.DataSource = new byte[tblItem.Rows.Count];
                    repService.DataBind();

                    tr = (HtmlTableRow)repService.Controls[0].Controls[0].FindControl("trRow");
                    tr.Attributes.Add("class", (i % 2 == 0) ? "GridRow2" : "GridRowAlt2");
                    int serviceCount = 0;
                    //tempOptionals += "<br /><b><u>Service</u></b>";
                    foreach (dsMachineItem.MachineItemRow drItem in tblItem.Rows)
                    {
                        tr = (HtmlTableRow)repService.Items[serviceCount].FindControl("trRow");
                        tr.Attributes.Add("class", (i % 2 == 0) ? "GridRow2" : "GridRowAlt2");

                        Literal litServiceEDPCode = (Literal)repService.Items[serviceCount].FindControl("litServiceEDPCode");
                        litServiceEDPCode.Text = drItem.Code;

                        Literal litServiceName = (Literal)repService.Items[serviceCount].FindControl("litServiceName");
                        litServiceName.Text = drItem.Name;

                        //tempOptionals += drItem.Name + "<br />";
                        serviceCount++;
                    }
                }
                else
                    repService.Visible = false;

                Repeater repConfig = (Repeater)item.FindControl("repConfig");

                tblItem = da.MachineItem_Search(drProductCart.CartItemID, (int)ProductItemType.Config, true);
                if (tblItem.Rows.Count > 0)
                {
                    repConfig.DataSource = new byte[tblItem.Rows.Count];
                    repConfig.DataBind();

                    int configCount = 0;

                    tr = (HtmlTableRow)repConfig.Controls[0].Controls[0].FindControl("trRow");
                    tr.Attributes.Add("class", (i % 2 == 0) ? "GridRow2" : "GridRowAlt2");
                    //tempOptionals += "<br /><b><u>Config</u></b>";
                    foreach (dsMachineItem.MachineItemRow drItem in tblItem.Rows)
                    {
                        tr = (HtmlTableRow)repConfig.Items[configCount].FindControl("trRow");
                        tr.Attributes.Add("class", (i % 2 == 0) ? "GridRow2" : "GridRowAlt2");

                        Literal litConfigEDPCode = (Literal)repConfig.Items[configCount].FindControl("litConfigEDPCode");
                        litConfigEDPCode.Text = drItem.Code;

                        Literal litConfigName = (Literal)repConfig.Items[configCount].FindControl("litConfigName");
                        litConfigName.Text = drItem.Name;

                        //tempOptionals += "<br />" + drItem.Name;
                        configCount++;
                    }
                }

                Repeater repOthers = (Repeater)item.FindControl("repOthers");

                tblItem = da.MachineItem_Search(drProductCart.CartItemID, (int)ProductItemType.OthersPromotionItems, true);
                if (tblItem.Rows.Count > 0)
                {
                    repOthers.DataSource = new byte[tblItem.Rows.Count];
                    repOthers.DataBind();

                    int othersCount = 0;
                    tr = (HtmlTableRow)repOthers.Controls[0].Controls[0].FindControl("trRow");
                    tr.Attributes.Add("class", (i % 2 == 0) ? "GridRow2" : "GridRowAlt2");
                    //tempOptionals += "<br /><b><u>Config</u></b>";
                    foreach (dsMachineItem.MachineItemRow drItem in tblItem.Rows)
                    {
                        tr = (HtmlTableRow)repOthers.Items[othersCount].FindControl("trRow");
                        tr.Attributes.Add("class", (i % 2 == 0) ? "GridRow2" : "GridRowAlt2");
                        Literal litOthersEDPCode = (Literal)repOthers.Items[othersCount].FindControl("litOthersEDPCode");
                        litOthersEDPCode.Text = drItem.Code;

                        Literal litOthersName = (Literal)repOthers.Items[othersCount].FindControl("litOthersName");
                        litOthersName.Text = drItem.Name;

                        othersCount++;
                    }
                }

                Repeater repOptional = (Repeater)item.FindControl("repOptional");

                dsCartOptionalItem.ViewCartOptionalItemDataTable tblViewCartOptionalItem = da.CartOptionalItem_Display(drProductCart.ID);
                if (tblViewCartOptionalItem.Rows.Count > 0)
                {
                    repOptional.DataSource = new byte[tblViewCartOptionalItem.Rows.Count];
                    repOptional.DataBind();

                    tr = (HtmlTableRow)repOptional.Controls[0].Controls[0].FindControl("trRow");
                    tr.Attributes.Add("class", (i % 2 == 0) ? "GridRow2" : "GridRowAlt2");
                    //litEDPCode.Text += "<br />";
                    //tempOptionals += "<br /><b><u>Optional</u></b>";
                }

               
                int optCount = 0;
                foreach (dsCartOptionalItem.ViewCartOptionalItemRow row in tblViewCartOptionalItem)
                {
                    tr = (HtmlTableRow)repOptional.Items[optCount].FindControl("trRow");
                    tr.Attributes.Add("class", (i % 2 == 0) ? "GridRow2" : "GridRowAlt2");

                    Literal litOptionalEDPCode = (Literal)repOptional.Items[optCount].FindControl("litOptionalEDPCode");
                    litOptionalEDPCode.Text = row.Code;

                    Literal litOptionalName = (Literal)repOptional.Items[optCount].FindControl("litOptionalName");
                    litOptionalName.Text = row.Name;

                    Literal litOptionalUnitPrice = (Literal)repOptional.Items[optCount].FindControl("litOptionalUnitPrice");
                    litOptionalUnitPrice.Text = row.Price.ToString(WebLib.CurrencyFormat);

                    TextBox txtOptionalQuantity = (TextBox)repOptional.Items[optCount].FindControl("txtOptionalQuantity");
                    txtOptionalQuantity.Text = row.Quantity.ToString();

                    HiddenField hidOptionalItemID = (HiddenField)repOptional.Items[optCount].FindControl("hidOptionalItemID");
                    hidOptionalItemID.Value = row.ID.ToString();

                    //litEDPCode.Text += "<br />" + row.Code;
                    //tempOptionals += "<br />" + row.Name;
                    totalOptionalItemPrice += row.UnitPrice * row.Quantity;

                    optCount++;
                }

                //litName.Text += "<br /><br />";

                //if (tempOptionals != "")
                //    litEDPCode.Text += "<br />";
                
                HiddenField hidIsNoTier = (HiddenField)item.FindControl("hidIsNoTier");

                if (drProductCart.OrderTypeEnum == (short)OrderType.Normal)
                {
                    int quantity = 0;
                    dsPOProductItem.POProductItemRow drPOProductItem = da.POProductItem_GetPOQuantity(WebLib.LoggedInUser.DealerUserID, new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1), DateTime.Now);
                    if (drPOProductItem != null && drPOProductItem.Quantity > 0)
                        quantity = drPOProductItem.Quantity;// +1;

                    string price = "";
                    dsProductPriceTier _dsProductPriceTier = new dsProductPriceTier();
                    dsProductPriceTier.ProductPriceTierRow drProductPriceTier = da.ProductPriceTier_Get(drProductCart.CartItemID, (quantity + totalQuantityMachineItem));
                    if (drProductPriceTier != null)
                    {
                        switch (drProductPriceTier.TierName)
                        {
                            case "Tier1":
                                price = drProductPriceTier.Price.ToString(WebLib.CurrencyFormat) + " (Normal)";
                                break;

                            case "Tier2":
                                price = drProductPriceTier.Price.ToString(WebLib.CurrencyFormat) + " (Normal)";
                                break;

                            case "Tier3":
                                price = drProductPriceTier.Price.ToString(WebLib.CurrencyFormat) + " (Normal)";
                                break;

                            case "NoTier":
                                price = drProductPriceTier.Price.ToString(WebLib.CurrencyFormat) + " (Normal)";
                                break;
                        }

                        litUnitPrice.Text = price;

                        Literal litTotalPrice = (Literal)item.FindControl("litTotalPrice");
                        litTotalPrice.Text = (((drProductPriceTier != null ? drProductPriceTier.Price : drProductCart.UnitPrice) * drProductCart.Quantity) + totalOptionalItemPrice).ToString(WebLib.CurrencyFormat);
                        totalPriceMachine += Decimal.Round((((drProductPriceTier != null ? drProductPriceTier.Price : drProductCart.UnitPrice) * drProductCart.Quantity) + totalOptionalItemPrice), 2);
                    }

                }
                else
                {
                    litUnitPrice.Text = (drProductCart.UnitPrice).ToString(WebLib.CurrencyFormat);
                    Literal litTotalPrice = (Literal)item.FindControl("litTotalPrice");
                    litTotalPrice.Text = ((drProductCart.UnitPrice * drProductCart.Quantity) + totalOptionalItemPrice).ToString(WebLib.CurrencyFormat);
                    totalPriceMachine += Decimal.Round((drProductCart.UnitPrice * drProductCart.Quantity) + totalOptionalItemPrice, 2);
                }

                
            }


            
        }

        litMachineTotalQuantity.Text = totalQuantityMachine.ToString();
        litMachineTotalPrice.Text = "RM " + totalPriceMachine.ToString(WebLib.CurrencyFormat);
    }