

    function deleteMeals(countryLabel)
    {                   
     deleteAllItemsFromSession(countryLabel);  
      
     //alert("Delete Meals");
    }
    /*
    function getProductList(selComponent, selOptionalItem, mealItemId, changeValue)
    {
        var itemList = $.cookie('itemsAdded');
        var itemListTotal = $.cookie('meal_total_url');
        var pids=itemList.split("|");
        var pidsTotal;
        if(itemListTotal!=null) 
            pidsTotal = itemListTotal.split("|");
        var productList = '';
        var separator = '';
                        
        for ( i = 0; i < pids.length; i++ )
        {
            var pid=pids[i];
            var pidTotal = '';
            if(pidsTotal!=null)
                pidTotal = pidsTotal[i];
            
            if(pid!=null && pid!='')
            {
                var ItemInfo=pid.split("_");
                var itemId=ItemInfo[0];
                var complist=$.cookie('compList'+pid); // Unselected Component List
                var selCompList = '';
                var selOptionalItemList = '';
                alert(pid+'=='+mealItemId);
                if(pid==mealItemId || (changeValue=='multi' && pid==mealItemId))
                {
                    if(complist!=null && complist!='')
                    {
                        if(selComponent!='')
                        {
                        // Selected Component List without Optional Items
                        selCompList = '$' + selComponent;
                        // Selected Optional Items List
                        selOptionalItemList = '$' + selOptionalItem;
                        }
                        // Item Id and Unselected Component List
                        itemId = itemId + ',' + complist;
                    }
                    
                    productList = productList + separator + itemId + selCompList + selOptionalItemList;
                    if(separator=='') separator='|';
                }
                else if(changeValue!='delete')
                {
                    if(pidsTotal==null || pidsTotal[i]=='')
                    {
                        pidTotal = itemId;
                        if(complist!=null && complist=='')
                            pidTotal = pidTotal + ',' + complist;
                    }
                    productList = productList + separator + pidTotal;
                    if(separator=='') separator='|'; 
                }
                
            }
        }
        
        $.cookie('meal_total_url', productList, {path:'/'});
        return productList;
    }*/
    
    function getProductList(countryLabel)
    {
        var itemList = $.cookie('itemsAdded_'+countryLabel);
        var pids=itemList.split("|");
        var productList = '';
        var separator = '';
        for ( i = 0; i < pids.length; i++ )
        {
            var pid=pids[i];
            if(pid!=null && pid!='')
            {
                var itemDetail = '';
                var ItemInfo=pid.split("_");
                itemDetail =ItemInfo[0];
                var compList=$.cookie('compList'+pid+'_'+countryLabel);
                var revCompList=$.cookie('revCompList'+pid+'_'+countryLabel);
                var optionalList=$.cookie('optionalList'+pid+'_'+countryLabel);
                var reverseItemId=$.cookie('reverseItem'+pid+'_'+countryLabel);
                if(compList!=null && compList!='')
                {
                    itemDetail = itemDetail + ',' + compList;
                    if(revCompList!=null && revCompList!='')
                    {
                        itemDetail += '$' + revCompList;
                        if(optionalList!=null && optionalList!='')
                            itemDetail += '$' + optionalList;
                    } 
                }
                else if(reverseItemId!=null && reverseItemId!='')
                {
                    itemDetail = reverseItemId;
                }
                
                productList += separator + itemDetail;
                if(separator=='') separator='|';
            }
        }
        
        return productList;
    }
    
    var callCounter = 1;
    var country = 'US';
    var mealAjaxObj = null;
    function getTotalVal(countryCode, countryLabel)
    {
        var productList = getProductList(countryLabel);
      // alert('Product List :: ' + productList); 
      
        country = countryCode;
        url = '/getuknutrition/mealBuilderInfo.do?products='+productList+'&countryCode='+countryCode+'&liveData=true';
      
        if(mealAjaxObj != null) mealAjaxObj.abort();
        mealAjaxObj = $.ajax({
            url: url,
            type: 'GET',
            data: '',                                                                    
            cache: false,
            timeout : '20000',                                                                                                 
            error: function()
            {
                if(callCounter<=3)
                {
                    getTotalVal(countryCode,countryLabel);
                    callCounter ++;
                }
                return false;   
            },
            success: function(html)
            {
               
                var startIndex =html.indexOf("<!--mealbuilderstart-->"); 
                var endIndex = html.indexOf("<!--mealbuilderend-->");
                var flyOut = html.substring(startIndex,endIndex);
                //alert("flyOut===>"+flyOut);
                
                if(flyOut!=null && flyOut!='') 
                {    
                    $('#meal_builder_calc').html(flyOut);
                     
                }
                var dataObj = document.getElementById('meal_builder_calc');
                $(".data2:last-child",dataObj).css("border-bottom","none");
                $(".data2:last-child .colsleft",dataObj).addClass("dataColsleft"); 
                callCounter = 1;
            } 
         });
        
        $('#loadingSwf').remove();
        $('#meal_builder_calc').css("display","block");
        $('#calc_total').show(); // show summary 
        $('#calc_divide').show();  
        $('#meal_builder_emptyMsg').css("display","none");   
        $('#ctl_buttons').css("display","block");  
        $('#empty_builder').css("display","block");    
    }
         
    function showSummmary(productId) 
    {
           
        
        // alert("showSummmary==========>"+productId); 
        
        $('#nutrition_wrappers'+productId+' .percent').each(function(){
              
                var id = this.id;
                if(id !='')
                { 
                
                    //alert("id====>"+id);
                    var sub=$(this).html().trim(); 
                    //alert("sub====>"+sub+"========length=======>"+sub.length);
                    var value =0;
                    var index = sub.trim().indexOf('%'); 
                   
                    if(index>-1)
                    {
                        
                        value= parseFloat(sub.substring(1, index));
                   
                    } 
                    var tableValue=$('#summary tbody tr #'+id).text();                      
                    var index2 = tableValue.indexOf('%'); 
                    if(index2>-1)
                    {
                    tableValue= parseFloat(tableValue.substring(1,index2)); 
                    //alert("tableValue==percent===>"+tableValue); 
                    
                    }  
                   
                    var sumValue=tableValue;
                    if(!isNaN(value))
                    sumValue=parseFloat(sumValue+value);
                                    
                    $('#summary tbody tr #'+id).html('('+sumValue+'%)');
                
                }
                
                //$('#meal_builder_data').css("display","block"); 
              // $('#calc_total').show(); // show summary
                
       
        }); 
        
        
              
        $('#nutrition_wrappers'+productId+' .large_number').each(function() 
        { 
                       
                var id = this.id;
                //alert("id====>"+id);
                //alert($(this).children().html());
                var sub=$(this).children().html();                        
                var value = parseFloat($(this).text());                
                //alert("value===>"+value);
                if(id !='')
                {
                    
                    var percentValue=$('#summary tbody tr #'+id).children();
                    /*
                    if(percentValue.html()==0 || percentValue.html()==null){
                    
                       percentValue.html('(0%)');
                    } */
                    var sumValue=parseFloat($('#summary tbody tr #'+id).text());
                    if(!isNaN(value))
                     sumValue+=value;
                     
                    
                    
                    $('#summary tbody tr #'+id).html(sumValue+sub).append(percentValue);  
                                     
                }
        
        }); 
        
        $('#loadingSwf').remove();
        $('#meal_builder_calc').css("display","block");
        $('#calc_total').show(); // show summary 
        $('#calc_divide').show();  
    }
    
    
    
    function getItems(cookie_value,countryCode,appStyle,countryLabel)
    {
        //alert("getItems=======>"+cookie_value);
        
        if(cookie_value!=null && cookie_value!=''){ 
            //$('#meal_builder').css("display","block"); 
            var pids=cookie_value.split("|")
                        
            for ( i = 0; i < pids.length; i++ )
            {
                var pid=pids[i];//alert('getItems'+pid);
                if(pid!=null && pid!='')
                getProductDetails(pid,countryCode,countryLabel); 
                
            }  
        
        } 
        else
        {
            $('#loadingSwf').remove();
            $('#meal_builder_calc').css("display","none");  
            $('#meal_builder_emptyMsg').css("display","block"); 
            $('#ctl_buttons').css("display","none");  
            $('#empty_builder').css("display","none");    
            $('#empty_meal_builder').css("display","block");
            
        }
    
    }  
    
    var counter = 1;
    //var prevItem = '';
    
    function getProductDetails(pid,countryCode,countryLabel)             
    {   
            //alert("=====in getProductDetails()===="+pid);
            
            var pars='';
            var ItemInfo=pid.split("_");                                       
            var itemId=ItemInfo[0];
            var complist=$.cookie('compList'+pid+'_'+countryLabel);
            var revCompList = $.cookie('revCompList'+pid+'_'+countryLabel);
            var selOptionalList = $.cookie('optionalList'+pid+'_'+countryLabel);
            var reverseItemId = $.cookie('reverseItem'+pid+'_'+countryLabel);
            if(complist!=null && complist!='')
            {                      
                mode='Recalculate';
                pars+= 'mode='+mode+'&complist='+complist;
                if(revCompList!=null && revCompList!='')
                {
                    pars += '&reverseItem='+revCompList;
                }
                if(selOptionalList!=null && selOptionalList!='')
                {
                    pars += '&selectedOptionalComponents='+selOptionalList;
                }
            }
            else if(reverseItemId!=null && reverseItemId!='')
            {
                pars = 'mode=recalculate&reverseTrackedItemId='+reverseItemId; 
            }
          
            url ='/getuknutrition/itemDetailInfo.do?item='+itemId+'&liveData=true&showFlyOut=no&mealBuilder=yes&countryCode='+countryCode+'&showAllergen=true&divId='+pid;
           //alert('url :: ' + url);  
           //alert('pars :: ' + pars);
            $.ajax({           
                        url: url,
                        type: 'GET',
                        data: pars,                                                                    
                        cache: false,                                                                                                      
                        error: function(){                      
                            if(counter<=30)
                            {
                                //alert('Trying for==='+pid+'===>'+counter); 
                                getProductDetails(pid,countryCode,countryLabel);
                                counter++;
                            }                            
                        },
                        success: function(html){
                        //alert(html); 
                            var startIndex =html.indexOf("<!--maelbuilderstart-->"); 
                            var endIndex = html.indexOf("<!--maelbuilderend-->");
                            
                            var flyOut = html.substring(startIndex,endIndex);
                            //alert(flyOut);
                            if(flyOut!=null && flyOut!='')
                            {
                               // alert("pid==success=>"+pid);  
                                $('#calc_list').append(flyOut);
                                $('#item_body'+pid+'.calc_item_body').hide(); 
                                getTotalVal(countryCode,countryLabel);
                            }
                            var dataObj = document.getElementById('calc_list');
                            $(".data2:last-child",dataObj).css("border-bottom","none");
                            $(".data2:last-child .colsleft",dataObj).addClass("dataColsleft");
                            $("#new_grd_tlb tr",dataObj).each(function(){
                                $(this).find("td:last").addClass("bdr-rgt");
                            });
                            $("#new_grd_tlb tr:last",dataObj).addClass("wrap-status");
                            $("#new_grd_tlb tr:last",dataObj).find("td:last").addClass("rgt-crvy"); 
                            
                            $('#item_body' + pid + ' #ingredient_toggle',dataObj).toggle( 
                                function() {
                                    $(this).addClass('active');
                                    $('#item_body' + pid + ' .ingredient_item').slideDown();
                                    return false;
                                },
                                function() {
                                    $(this).removeClass('active');
                                    $('#item_body' + pid + ' .ingredient_item').slideUp();  
                                    return false;
                                }
                            );
                            counter = 1;
                            //prevItem = '';
                        } 
            });
    
    
    }
                
 
 /* getProductDetails method end */
      
        function show(id)
        {
            $('#item_head'+id).each(function()
            { 
             
                    var  _a = $(this).next('.calc_item_body'); 
                    if ($(_a).is(':visible'))
                    { 
                        $(_a).hide(); 
                        $(this).removeClass('active');
                        $('.product_information',this).css("display","block"); 
                    } 
                    else 
                    { 
                        $(_a).slideDown(); 
                        $(this).addClass('active');
                        $('.product_information',this).css("display","none");
                        
                        var mutexClasses = []; 
                        var mutexId = '';
                        var count = 0;
                         
                        $('dd',_a).each(function(i){
                           var classList = $(this).attr('class').split(' ');
                           
                           if(classList.length>1)
                           {
                               if(!(mutexId==classList[1] || classList[1]==''))
                               {
                                   mutexId = classList[1];
                                   mutexClasses[count] = mutexId;
                                   count++;
                                   //alert("MutexId :: " + mutexId + " Count:: " + count);
                               }
                           }
                        }); 
                        
                        for(var i=0; i<mutexClasses.length; i++) {
                            if(!($('dd:first').hasClass(mutexClasses[i])))
                                $('dd.'+mutexClasses[i]+':first').css("margin-top","8px");
                            $('dd.'+mutexClasses[i]+':last').css("padding-bottom","20px");
                        }
                    }
                    enable_buttons();
                    return false; 
            
            }); 
        }  
    
    
        function getRecalculateDetails(itemId,url,selComp,selOptionalItem,countryLabel)
        { 
                //alert("=========getRecalculateDetails()==========");
                currentItem = itemId;
                $.ajax(
                
                {
                
                        url: url,
                        type: 'GET',
                        data: '',                                                                    
                        cache: false,
                        timeout : '20000',                                                                                                 
                        error: function()
                        {
                            if(counter<=3)
                            {
                                getRecalculateDetails(itemId,url,selComp,selOptionalItem,countryLabel);
                                counter++;
                            }
                            return false;   
                        },
                        success: function(html)
                        {
                                //alert(html); 
                                var startIndex =html.indexOf("<!--maelbuilderbodystart-->"); 
                                var endIndex = html.indexOf("<!--maelbuilderbodyend-->");
                                var flyOut = html.substring(startIndex,endIndex);
                                //alert("flyOut===>"+flyOut);
                                if(flyOut!=null && flyOut!='') 
                                {   
                                    $('#item_body'+itemId).html(flyOut);
                                    getTotalVal(country,countryLabel);
                                }
                                
                                var _a = $('#item_body'+itemId);
                                var mutexClasses = []; 
                                var mutexId = '';
                                var count = 0;
                                 
                                $('dd',_a).each(function(i){
                                   var classList = $(this).attr('class').split(' ');
                                   
                                   if(classList.length>1)
                                   {
                                       if(!(mutexId==classList[1] || classList[1]==''))
                                       {
                                           mutexId = classList[1];
                                           mutexClasses[count] = mutexId;
                                           count++;
                                           //alert("MutexId :: " + mutexId + " Count:: " + count);
                                       }
                                   }
                                }); 
                                
                                for(var i=0; i<mutexClasses.length; i++) {
                                    if(!($('dd:first').hasClass(mutexClasses[i])))
                                        $('dd.'+mutexClasses[i]+':first').css("margin-top","8px");
                                    $('dd.'+mutexClasses[i]+':last').css("padding-bottom","20px");
                                }
                                
                                counter = 1;
                        } 
                
                });  
        }  
    
    


        function updateSummary()
        {
        
                //alert("=====Update table====");
                $('#calc_total table td span.sub ').each(function() 
                
                {  
                
                    var nutritionData = $('#calc_list');
                    var id = this.id;
                         
                    var value=0;
                    if(id!='' && id!=null) 
                    {
                       //alert("id===>"+id); 
                        $('.nutrition_wrapper #'+id,nutritionData).each(function()
                        {  
                        
                            //alert("detail=====>"+$(this).html()); 
                            var sub=$(this).html().trim();
                            var index = sub.indexOf('%');
                            if(index>-1)
                            {
                                if(!isNaN(parseFloat(sub.substring(1,index))))
                                value+=parseFloat(sub.substring(1,index));
                            }
                        
                        
                        });
                        
                       
                    
                    }  
                    $(this).html('('+value+'%)')
                    
                });
                
                
                $('#calc_total table td.data ').each(function() 
                { 
                    var nutritionData = $('#calc_list');
                    var id = this.id;
                    //alert("id===>"+id); 
                    var sub='';                        
                    var value = 0;
                    var percentValue=$(this).children();  
                    
                    $('.nutrition_wrapper #'+id, nutritionData).each(function() 
                    { 
                    
                        //alert("detail=====>"+$(this).html());
                        if(!isNaN(parseFloat($(this).html())))
                        value += parseFloat($(this).html());
                        sub=$(this).children().html();
                        //alert("value 2========>"+value);
                    
                    });
                    
                    
                    $(this).html(value+sub).append(percentValue); 
                    
                
                }); 
        }
        


        function getRecalculateMultiDetails(oldItemId,itemId,url,countryLabel)
        { 
            //alert("=========getRecalculateDetails()=========="+url);
            //currentItem = itemId;
            updateCookie(oldItemId,itemId,countryLabel); 
            
            $.ajax(
            {
            
                url: url,
                type: 'GET',
                data: '',                                                                    
                cache: false,
                timeout : '20000',                                                                                                 
                error: function()
                {
                    if(counter <= 3)
                    {
                        getRecalculateMultiDetails(oldItemId,itemId,url,countryLabel);
                        counter++;
                    }  
                    return false;   
                },
                success: function(html)
                {
                    //alert(html); 
                    
                    var bodyStartIndex =html.indexOf("<!--maelbuilderbodystart-->"); 
                    var bodyEndIndex = html.indexOf("<!--maelbuilderbodyend-->");
                    var headStartIndex =html.indexOf("<!--maelbuilderstart-->"); 
                    var headEndIndex = html.indexOf("<!--maelbuilderheadend-->");
                    
                    var headHtml = html.substring(headStartIndex,headEndIndex);
                    var bodyHtml = html.substring(bodyStartIndex,bodyEndIndex);
                   //alert(oldItemId + ' :: ' + itemId);
                    if(bodyHtml!='' && headHtml !='')
                    {
                        $('#item_head'+oldItemId).replaceWith(headHtml);                    
                        $('#item_body'+oldItemId).html(bodyHtml);
                        $('#item_body'+oldItemId).attr('id','item_body'+itemId);
                        $('#item_head'+itemId+' .product_information').css("display","none");                    
                        getTotalVal(country,countryLabel);                    
                    }           
                    var dataObj = document.getElementById('calc_list'); 
                    $('#item_body' + itemId + ' #ingredient_toggle',dataObj).toggle( 
                                function() {
                                    $(this).addClass('active');
                                    $('#item_body' + itemId + ' .ingredient_item').slideDown();
                                    return false;
                                },
                                function() {
                                    $(this).removeClass('active');
                                    $('#item_body' + itemId + ' .ingredient_item').slideUp();  
                                    return false;
                                }
                    ); 
                    counter = 1;
                    $("#new_grd_tlb tr").each(function(){
                    $(this).find("td:last").addClass("bdr-rgt");
                    });
                    $("#new_grd_tlb tr:last").addClass("wrap-status");
                    $("#new_grd_tlb tr:last").find("td:last").addClass("rgt-crvy");
                    //$("#autosize .data2").css("border-bottom","none");
                } 
            
                 
            
            });
        
        }  
        
