﻿var max = 100;
var selectedfooditem = null;
var nutriinfocalculator1 = null;

function Initialise_NutriInfoCalculator()
{
    document.getElementById('calc2').style.display='';
    
    document.getElementById('calc2').style.left = (window.screen.availWidth - document.getElementById('calc2').clientWidth)/2 + document.documentElement.scrollLeft + 'px';  
    if(document.all?true:false)
        document.getElementById('calc2').style.top = (window.screen.availHeight - document.getElementById('calc2').clientHeight)/3 + document.documentElement.scrollTop + 'px';  
    else
        document.getElementById('calc2').style.top = (window.screen.availHeight * 0.05) + document.documentElement.scrollTop + 'px';  
    
    if(nutriinfocalculator1 == null)
        nutriinfocalculator1 = new NutriInfoCalculator();    
}

function NutriInfoCalculator()
{
    // Initialise the food category dropdownlist
    NutritionalInfoCalculator.GetFoodItemCategory(GetFoodItemCategory_Callback);
   
    // Attach searchbox onkeypress event
    document.getElementById('NutriInfoCalculator_SearchBox').onkeypress = function(evt)
    {
        if((document.all?event.keyCode:evt.which) == 13)
        {
            NutriInfoCalculator_Search();
            return false;
        }
        else{}
    }    
    document.getElementById('NutriInfoCalculator_SearchBox').value = "Enter search terms here...";
    
    document.getElementById('DDLFoodItemCategory')[0] = new Option("-- Select a category --","");
        
    document.getElementById('NutriInfoCalculator_Qty').onfocus = function()    // On focus, set default "0" to ""
    {
        if(this.value == "0")
            this.value = "";
        else
            this.select();
    }
    document.getElementById('NutriInfoCalculator_Qty').onblur = function()
    {
        if(this.value == "" || this.value == "0" || this.value == "0." || this.value == "0.0" || this.value == "0.00" || this.value == "0.000")    // On blur, set "" to default "0"
            this.value = "0";
        else
            this.value = this.value.trimLeftZero();
    }
    document.getElementById('NutriInfoCalculator_Qty').onkeyup = function()
    {
        clearTimeout(nutriinfo_timerid);
        nutriinfo_timerid = null;
        
        var qty = this.value.trimLeftZero(); 
        if(qty >= max)
        {  
            this.value = max;      
            document.getElementById('NutriInfoSlider').title = max;
        }
        else if(qty <= 0)
        {
            this.value = 1;                       
            document.getElementById('NutriInfoSlider').title = 1;
        }
        
        var target = Math.round(qty/max * 150);
        if(target < 0)
            target = 0;
        else if(target > 150)
            target = 150;  
            
        this.setAttribute("target",qty);
            
        NutriInfoSlider_Slide(target); 
        document.getElementById('NutriInfoSlider').title = qty;   
        
        if(selectedfooditem != null)
            PopulateFoodInfo(selectedfooditem);
    }
    document.getElementById('NutriInfoCalculator_Qty').onkeypress = function(evt)
    {
        var e = (document.all?event:evt);            
        return ValidateField(e,this.value,/^[0-9]+\.?[0-9]*$/);
    }
    document.getElementById('NutriInfoSlider').onmousedown = function(evt)
    {
        clearTimeout(nutriinfo_timerid);
        nutriinfo_timerid = null;
        
        slider_isdrag = true; 
        
        if(document.all?true:false)
		    slider_currmouseX = event.x;
        else
		    slider_currmouseX = evt.clientX;
      
        document.onselectstart = function(){return false;}
        document.onmousedown = function(){return false;}    //mozilla
        document.onmousemove = NutriInfoSlider_MoveMouseMove;	
        document.onmouseup = NutriInfoSlider_MoveMouseStop;
    }    
    
    document.getElementById('NutriInfoSlider').onmouseout = NutriInfoSlider_MoveMouseStop;
    
    document.getElementById('NutriInfoSliderBar').onmousedown = function(evt)
    {
        clearTimeout(nutriinfo_timerid);
        nutriinfo_timerid = null;
        
        var target = 0;
        if(document.all?true:false)
		    target = event.x - 10;
        else
		    target = evt.clientX - document.getElementById('calc2').offsetLeft - 55;
		    
        if(target < 10)
        {
            target = 2;
            document.getElementById('NutriInfoCalculator_Qty').value = "1";
            document.getElementById('NutriInfoSlider').title = "1";
        }
        else if(target > 140)
        {
            target = 150;   
            document.getElementById('NutriInfoCalculator_Qty').value = max;
            document.getElementById('NutriInfoSlider').title = max;    
        }  
        else
        {
            document.getElementById('NutriInfoCalculator_Qty').value = Math.round(target/150 * max);
            document.getElementById('NutriInfoSlider').title = Math.round(target/150 * max);
        } 
        
        NutriInfoSlider_Slide(target);   
                
//        if(selectedfooditem != null)
//            PopulateFoodInfo(selectedfooditem);
    }
}

function GetFoodItemCategory_Callback(response)
{
    if(response.value != null && response.value.keys.length > 0)
    { 
        var ddlfooditemcategory = document.getElementById('DDLFoodItemCategory');
        ddlfooditemcategory.onchange = function()
        {
            selectedfooditem = null;
            
            document.getElementById('NutriInfoCalculator_SearchBox').value='Enter search terms here...';
            document.getElementById('NutriInfoCalculator_SearchBox').style.color='#CCCCCC';
            
            loader_timerid = setTimeout(Show_Loader,1000);  // Delay the showing of loading gif
            
            var selectedvalue = this.options[this.selectedIndex].value;
            NutritionalInfoCalculator.GetFoodListByCategory(selectedvalue,GetFoodListByCategory_Callback);
        }
       
        while(ddlfooditemcategory.length > 1)
            ddlfooditemcategory.remove(ddlfooditemcategory.length - 1);
            
        var foodCategoryIDs =response.value.keys;
        var foodCategoryNames = response.value.values;
               
        for(var i=0; i< foodCategoryIDs.length; i++)
        {  
            ddlfooditemcategory[i+1] = new Option(foodCategoryNames[i],foodCategoryIDs[i]);
            if(foodCategoryIDs[i] == 1)
                ddlfooditemcategory[i+1].selected = true;
        }        
        NutritionalInfoCalculator.GetFoodListByCategory(ddlfooditemcategory.options[ddlfooditemcategory.selectedIndex].value,GetFoodListByCategory_Callback);
    }
    else
    {}
}

function GetFoodListByCategory_Callback(response)
{
    clearTimeout(loader_timerid);   // clear the delay timer
    document.getElementById('FoodListLoader').style.display = 'none';
    document.getElementById('DivFoodList').style.display = '';
    
    if(response.value != null)
    {
        var foodlistcontainer = document.getElementById('DivFoodList');
       
        while(foodlistcontainer.childNodes.length > 0)
            foodlistcontainer.removeChild(foodlistcontainer.childNodes[0]);

        var foodList = response.value;
               
        for(var i=0; i< foodList.length; i++)
        {  
            var divfooditem = document.createElement('div');
            divfooditem.style.padding = "2px";
            divfooditem.style.cursor = "pointer";
            divfooditem.innerHTML = foodList[i].foodName;
            divfooditem.setAttribute('index',i);
            divfooditem.setAttribute('unit',foodList[i].unit);    
            divfooditem.setAttribute('selected','false');      
            divfooditem.onmouseover = function()
            {
                if(this.getAttribute('selected') == 'false')
                {
                    this.style.backgroundColor = 'steelblue';
                    this.style.color = 'white';
                }
            }
            divfooditem.onmouseout = function()
            {
                if(this.getAttribute('selected') == 'false')
                {
                    this.style.backgroundColor = '';
                    this.style.color = '';
                }
            }
            divfooditem.onmouseup = function(evt)
            {
                clearTimeout(nutriinfo_timerid);
                nutriinfo_timerid = null;
                
                selectedfooditem = foodList[this.getAttribute('index')];
                
                document.getElementById('NutriInfoCalculator_Unit').innerHTML = this.getAttribute('unit');
                
                for(var i=0; i<foodlistcontainer.childNodes.length; i++)
                {
                    if(foodlistcontainer.childNodes[i] != this)
                    {
                        foodlistcontainer.childNodes[i].setAttribute('selected','false');
                        foodlistcontainer.childNodes[i].style.backgroundColor = '';
                        foodlistcontainer.childNodes[i].style.color = '';                        
                    }
                }
                this.setAttribute('selected','true');
                this.style.backgroundColor = 'lightsteelblue';
                this.style.color = '';
                
                document.getElementById('NutriInfoCalculator_Qty').value = "1";
                document.getElementById('NutriInfoCalculator_Qty').focus();
                    
                NutriInfoSlider_Slide(Math.round(document.getElementById('NutriInfoCalculator_Qty').value/max * 150)); 
                document.getElementById('NutriInfoSlider').title = document.getElementById('NutriInfoCalculator_Qty').value;   
            }
            foodlistcontainer.appendChild(divfooditem);
        }
        
        document.getElementById('DivFoodList').scrollTop = '0px';
        
        if(foodList.length > 0)
        {
            clearTimeout(nutriinfo_timerid);
            nutriinfo_timerid = null;
            
            selectedfooditem = foodList[0];
            
            document.getElementById('NutriInfoCalculator_Qty').value = "1";
            document.getElementById('NutriInfoCalculator_Qty').focus();
                
            NutriInfoSlider_Slide(Math.round(document.getElementById('NutriInfoCalculator_Qty').value/max * 150)); 
            document.getElementById('NutriInfoSlider').title = document.getElementById('NutriInfoCalculator_Qty').value;   
                        
            document.getElementById('NutriInfoCalculator_Unit').innerHTML = foodList[0].unit;
             
            foodlistcontainer.childNodes[0].setAttribute('selected','true');
            foodlistcontainer.childNodes[0].style.backgroundColor = 'lightsteelblue';
            foodlistcontainer.childNodes[0].style.color = '';
        }
    }
    else
    {}
}

var loader_timerid = null;
function NutriInfoCalculator_Search()
{
    var searchtext = document.getElementById('NutriInfoCalculator_SearchBox').value;
    
    while(searchtext.indexOf(" ") == 0)     // Remove leading whitespaces
        searchtext = searchtext.substring(1,searchtext.length);
    while(searchtext.indexOf("; ") >= 0)     // Remove commas spacing
        searchtext = searchtext.replace("; ",";");
        
    document.getElementById('NutriInfoCalculator_SearchBox').value = searchtext;  
     
    if(searchtext != "")   
    {
        loader_timerid = setTimeout(Show_Loader,1000);  // Delay the showing of loading gif
    
        var searchterms = searchtext.split(';');
        NutritionalInfoCalculator.SearchFood(searchterms,SearchFood_Callback);
    }
    else
        document.getElementById('NutriInfoCalculator_SearchBox').value = "";
}

function SearchFood_Callback(response)
{
    clearTimeout(loader_timerid);   // clear the delay timer
    document.getElementById('FoodListLoader').style.display = 'none';
    document.getElementById('DivFoodList').style.display = '';
    
    document.getElementById('DDLFoodItemCategory').options[0].selected = true;
    
    if(response.value != null)
    {
        var foodlistcontainer = document.getElementById('DivFoodList');
       
        while(foodlistcontainer.childNodes.length > 0)
            foodlistcontainer.removeChild(foodlistcontainer.childNodes[0]);
            
        var foodRows = response.value.Rows;
               
        for(var i=0; i< foodRows.length; i++)
        {  
            var divfooditem = document.createElement('div');
            divfooditem.style.padding = "2px";
            divfooditem.style.cursor = "pointer";
            
            // Underline the search words
            var searchterms = document.getElementById('NutriInfoCalculator_SearchBox').value.toLowerCase().split(';');
            for(var x=0; x<searchterms.length; x++)
            {
                if(searchterms[x] != '')
                {
                    var matchedIndex = foodRows[i].FoodName.toLowerCase().indexOf(searchterms[x]);                
                    if(matchedIndex >= 0)
                    {    
                        divfooditem.innerHTML = foodRows[i].FoodName.substring(0,matchedIndex) + "<font color='green'><b>" + foodRows[i].FoodName.substring(matchedIndex,searchterms[x].length + matchedIndex) + "</b></font>" + foodRows[i].FoodName.substring(searchterms[x].length + matchedIndex,foodRows[i].FoodName.length);
                        break;
                    }
                }
            }
            
            divfooditem.setAttribute('index',i); 
            divfooditem.setAttribute('unit',foodRows[i].StdServingUnit);
            divfooditem.setAttribute('selected','false');                 
            divfooditem.onmouseover = function()
            {
                if(this.getAttribute('selected') == 'false')
                {
                    this.style.backgroundColor = 'steelblue';
                    this.style.color = 'white';
                }
            }
            divfooditem.onmouseout = function()
            {
                if(this.getAttribute('selected') == 'false')
                {
                    this.style.backgroundColor = '';
                    this.style.color = '';
                }
            }
            divfooditem.onmouseup = function(evt)
            {
                clearTimeout(nutriinfo_timerid);
                nutriinfo_timerid = null;
                
                selectedfooditem = foodRows[this.getAttribute('index')];
                
                document.getElementById('NutriInfoCalculator_Unit').innerHTML = this.getAttribute('unit');
                
                for(var i=0; i<foodlistcontainer.childNodes.length; i++)
                {
                    if(foodlistcontainer.childNodes[i] != this)
                    {
                        foodlistcontainer.childNodes[i].setAttribute('selected','false');
                        foodlistcontainer.childNodes[i].style.backgroundColor = '';
                        foodlistcontainer.childNodes[i].style.color = '';                        
                    }
                }
                this.setAttribute('selected','true');
                this.style.backgroundColor = 'lightsteelblue';
                this.style.color = '';
                
                document.getElementById('NutriInfoCalculator_Qty').value = "1";
                document.getElementById('NutriInfoCalculator_Qty').focus();
                    
                NutriInfoSlider_Slide(Math.round(document.getElementById('NutriInfoCalculator_Qty').value/max * 150)); 
                document.getElementById('NutriInfoSlider').title = document.getElementById('NutriInfoCalculator_Qty').value;
            }
            foodlistcontainer.appendChild(divfooditem);
        }
        
        document.getElementById('DivFoodList').scrollTop = '0px';
        
        if(foodRows.length > 0)
        {
            clearTimeout(nutriinfo_timerid);
            nutriinfo_timerid = null;
            
            selectedfooditem = foodRows[0];
            
            document.getElementById('NutriInfoCalculator_Qty').value = "1";
            document.getElementById('NutriInfoCalculator_Qty').focus();
                
            NutriInfoSlider_Slide(Math.round(document.getElementById('NutriInfoCalculator_Qty').value/max * 150)); 
            document.getElementById('NutriInfoSlider').title = document.getElementById('NutriInfoCalculator_Qty').value;   
                        
            document.getElementById('NutriInfoCalculator_Unit').innerHTML = foodRows[0].StdServingUnit;
             
            foodlistcontainer.childNodes[0].setAttribute('selected','true');
            foodlistcontainer.childNodes[0].style.backgroundColor = 'lightsteelblue';
            foodlistcontainer.childNodes[0].style.color = '';
        }
    }
    else
    {}
}

function Show_Loader()
{
    document.getElementById('FoodListLoader').style.display = '';
    document.getElementById('DivFoodList').style.display = 'none';
}

function ValidateField(evt,fieldvalue,expression)
{
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    if((!document.all?true:false) && (charCode == 8 || charCode == 13 || charCode == 46))   // Mozilla: backspace, enter, delete
        return true;
    else
        return (new RegExp(expression).test(fieldvalue + String.fromCharCode(charCode)));
}

var slider_isdrag = false;
var slider_currmouseX;
function NutriInfoSlider_MoveMouseMove(evt)
{
    if(slider_isdrag) 
	{ 
	    var currX = parseInt(document.getElementById('NutriInfoSlider').style.left.replace('px',''));
	    
        if(document.all?true:false)
		{
		    var diffX = event.x - slider_currmouseX;									
			slider_currmouseX = event.x;
		}
		else
		{		    
		    var diffX = evt.clientX - slider_currmouseX;			
			slider_currmouseX = evt.clientX;	
		}
		
		if((currX + diffX) > 150 || (currX + diffX) < 0)
		{}
		else
		{
		    document.getElementById('NutriInfoSlider').style.left = currX + diffX + 'px';
		    document.getElementById('NutriInfoCalculator_Qty').value =  Math.round((currX + diffX)/150 * max);
		    document.getElementById('NutriInfoSlider').title = Math.round((currX + diffX)/150 * max);
		    
            document.getElementById('NutriInfoCalculator_Qty').setAttribute("target",Math.round((currX + diffX)/150 * max));
            if(selectedfooditem != null)
                PopulateFoodInfo(selectedfooditem);   
		} 
	} 
}

function NutriInfoSlider_MoveMouseStop()
{
	slider_isdrag = false;
	
	document.onselectstart = null;
	document.onmousedown = null;    // mozilla
	document.onmousemove = null;	
	document.onmouseup = null;
	
	slider_currmouseX = 0;	
	
	if(document.getElementById('NutriInfoCalculator_Qty').value == "0")
	{
	    document.getElementById('NutriInfoCalculator_Qty').value = "1";
	    document.getElementById('NutriInfoSlider').title = "1";
	    NutriInfoSlider_Slide(Math.round(1/max * 150)); 
	}
	
//	if(selectedfooditem != null)
//        PopulateFoodInfo(selectedfooditem);
} 

var nutriinfo_timerid = null;
var increment = 7;
function NutriInfoSlider_Slide(target)
{         
    var currX = parseInt(document.getElementById('NutriInfoSlider').style.left.replace('px',''));   
    var diffX = Math.abs(target - currX);
    
    if(diffX > increment)
        document.getElementById('NutriInfoCalculator_Qty').setAttribute("target",Math.round((currX + increment)/150 * max));
    else    
        document.getElementById('NutriInfoCalculator_Qty').setAttribute("target",Math.round(target/150 * max));
    
    if(selectedfooditem != null)
        PopulateFoodInfo(selectedfooditem); 
        
    if(currX < target && diffX > increment)
    {
        document.getElementById('NutriInfoSlider').style.left = currX + increment + 'px';
        nutriinfo_timerid = setTimeout('NutriInfoSlider_Slide(' + target + ')',1);
    }
    else if(currX > target && diffX > increment)
    {
        document.getElementById('NutriInfoSlider').style.left = currX - increment + 'px';
        nutriinfo_timerid = setTimeout('NutriInfoSlider_Slide(' + target + ')',1);
    }
    else
    {
        document.getElementById('NutriInfoSlider').style.left = target + 'px';
        
        clearTimeout(nutriinfo_timerid);
        nutriinfo_timerid = null;
    }    
}

String.prototype.trimLeftZero = function()
{
//    if(this == "")
//        return "0";
    if(this == "")
        return "";
        
    else if(this.indexOf(".") == 0)
        return "0" + this;
        
    var text = this;
        
    if(text.indexOf(".") > 0)  // Take care of decimal string
    {
        var befDecimal = text.split(".")[0];
        while(befDecimal.substring(0,1)=="0" && befDecimal.length > 1)
        {
            befDecimal = befDecimal.replace("0","");
        }
        text = befDecimal + "." + text.split(".")[1];
    }
    else
    {
        while(text.substring(0,1)=="0" && text.length > 1)
        {
            text = text.replace("0","");
        }                        
    }    
    return text;
}

function PopulateFoodInfo(fooditem)
{
    var qty = parseFloat(document.getElementById('NutriInfoCalculator_Qty').getAttribute("target"));
    
    var altColor = false;
    if(fooditem.qty == null)
    {
        qty = qty/fooditem.StdServingQty;   // From dataset row
         
        var qty_g = fooditem.WeightPerStdServing_g/100 * qty;
        
        altColor = CreateFoodInfoItem(document.getElementById("calories_kcal"),(fooditem.CaloriesPerStdServing_kcal * qty).roundTo() + ' kcal',altColor);                       
        altColor = CreateFoodInfoItem(document.getElementById("water_g"),(fooditem.Water_g * qty_g).roundTo() + ' g',altColor);
        altColor = CreateFoodInfoItem(document.getElementById("energy_kcal"),(fooditem.Energy_kcal * qty_g).roundTo() + ' kcal',altColor);
        altColor = CreateFoodInfoItem(document.getElementById("protein_g"),(fooditem.Protein_g * qty_g).roundTo() + ' g',altColor);
        altColor = CreateFoodInfoItem(document.getElementById("totalFat_g"),(fooditem.TotalFat_g * qty_g).roundTo() + ' g',altColor);
        altColor = CreateFoodInfoItem(document.getElementById("satFat_g"),(fooditem.SatFat_g * qty_g).roundTo() + ' g',altColor);
        altColor = CreateFoodInfoItem(document.getElementById("monoFat_g"),(fooditem.MonoFat_g * qty_g).roundTo() + ' g',altColor);
        altColor = CreateFoodInfoItem(document.getElementById("polyFat_g"),(fooditem.PolyFat_g * qty_g).roundTo() + ' g',altColor);
        altColor = CreateFoodInfoItem(document.getElementById("cholestrol_mg"),(fooditem.Cholestrol_mg * qty_g).roundTo() + ' mg',altColor);
        altColor = CreateFoodInfoItem(document.getElementById("CHO_g"),(fooditem.CHO_g * qty_g).roundTo() + ' g',altColor);
        altColor = CreateFoodInfoItem(document.getElementById("sugar_g"),(fooditem.Sugar_g * qty_g).roundTo() + ' g',altColor);
        altColor = CreateFoodInfoItem(document.getElementById("fibre_g"),(fooditem.Fibre_g * qty_g).roundTo() + ' g',altColor);
        // food.vitaA_mcg
        altColor = CreateFoodInfoItem(document.getElementById("vitA_mg"),(fooditem.VitA_mcg * qty_g).roundTo() + ' mg',altColor);
        altColor = CreateFoodInfoItem(document.getElementById("vitB1_mg"),(fooditem.VitB1_mg * qty_g).roundTo() + ' mg',altColor);
        altColor = CreateFoodInfoItem(document.getElementById("vitB2_mg"),(fooditem.VitB2_mg * qty_g).roundTo() + ' mg',altColor);
        altColor = CreateFoodInfoItem(document.getElementById("vitC_mg"),(fooditem.VitC_mg * qty_g).roundTo() + ' mg',altColor);
        altColor = CreateFoodInfoItem(document.getElementById("sodium_mg"),(fooditem.Sodium_mg * qty_g).roundTo() + ' mg',altColor);
        altColor = CreateFoodInfoItem(document.getElementById("potassium_mg"),(fooditem.Potassium_mg * qty_g).roundTo() + ' mg',altColor);
        altColor = CreateFoodInfoItem(document.getElementById("calcium_mg"),(fooditem.Calcium_mg * qty_g).roundTo() + ' mg',altColor);
        altColor = CreateFoodInfoItem(document.getElementById("phosphorus_mg"),(fooditem.Phosphorus_mg * qty_g).roundTo() + ' mg',altColor);
        CreateFoodInfoItem(document.getElementById("iron_mg"),(fooditem.Iron_mg * qty_g).roundTo() + ' mg',altColor);       
    }
    else
    {  
        qty = qty/fooditem.qty;
        
        altColor = CreateFoodInfoItem(document.getElementById("calories_kcal"),(fooditem.calories_kcal * qty).roundTo() + ' kcal',altColor);
        altColor = CreateFoodInfoItem(document.getElementById("water_g"),(fooditem.water_g * qty).roundTo() + ' g',altColor);
        altColor = CreateFoodInfoItem(document.getElementById("energy_kcal"),(fooditem.energy_kcal * qty).roundTo() + ' kcal',altColor);
        altColor = CreateFoodInfoItem(document.getElementById("protein_g"),(fooditem.protein_g * qty).roundTo() + ' g',altColor);
        altColor = CreateFoodInfoItem(document.getElementById("totalFat_g"),(fooditem.totalFat_g * qty).roundTo() + ' g',altColor);
        altColor = CreateFoodInfoItem(document.getElementById("satFat_g"),(fooditem.satFat_g * qty).roundTo() + ' g',altColor);
        altColor = CreateFoodInfoItem(document.getElementById("monoFat_g"),(fooditem.monoFat_g * qty).roundTo() + ' g',altColor);
        altColor = CreateFoodInfoItem(document.getElementById("polyFat_g"),(fooditem.polyFat_g * qty).roundTo() + ' g',altColor);
        altColor = CreateFoodInfoItem(document.getElementById("cholestrol_mg"),(fooditem.cholestrol_mg * qty).roundTo() + ' mg',altColor);
        altColor = CreateFoodInfoItem(document.getElementById("CHO_g"),(fooditem.CHO_g * qty).roundTo() + ' g',altColor);
        altColor = CreateFoodInfoItem(document.getElementById("sugar_g"),(fooditem.sugar_g * qty).roundTo() + ' g',altColor);
        altColor = CreateFoodInfoItem(document.getElementById("fibre_g"),(fooditem.fibre_g * qty).roundTo() + ' g',altColor);
        // food.vitaA_mcg
        altColor = CreateFoodInfoItem(document.getElementById("vitA_mg"),(fooditem.vitA_mcg * qty).roundTo() + ' mg',altColor);
        altColor = CreateFoodInfoItem(document.getElementById("vitB1_mg"),(fooditem.vitB1_mg * qty).roundTo() + ' mg',altColor);
        altColor = CreateFoodInfoItem(document.getElementById("vitB2_mg"),(fooditem.vitB2_mg * qty).roundTo() + ' mg',altColor);
        altColor = CreateFoodInfoItem(document.getElementById("vitC_mg"),(fooditem.vitC_mg * qty).roundTo() + ' mg',altColor);
        altColor = CreateFoodInfoItem(document.getElementById("sodium_mg"),(fooditem.sodium_mg * qty).roundTo() + ' mg',altColor);
        altColor = CreateFoodInfoItem(document.getElementById("potassium_mg"),(fooditem.potassium_mg * qty).roundTo() + ' mg',altColor);
        altColor = CreateFoodInfoItem(document.getElementById("calcium_mg"),(fooditem.calcium_mg * qty).roundTo() + ' mg',altColor);
        altColor = CreateFoodInfoItem(document.getElementById("phosphorus_mg"),(fooditem.phosphorus_mg * qty).roundTo() + ' mg',altColor);
        CreateFoodInfoItem(document.getElementById("iron_mg"),(fooditem.iron_mg * qty).roundTo() + ' mg',altColor);       
    }
}

function CreateFoodInfoItem(nutrientcontainer, nutrientvalue, altcolor)
{
//    if(nutrientvalue.split(' ')[0] != "0.00")
//    {
        nutrientcontainer.getElementsByTagName('td')[1].innerHTML = nutrientvalue;
        nutrientcontainer.style.display = '';        
        nutrientcontainer.style.backgroundColor = (altcolor?"#EEEEEE":"white");
        
        if(nutrientvalue.split(' ')[0] == "0")
            nutrientcontainer.getElementsByTagName('td')[1].style.color = "#AAAAAA";
        else
            nutrientcontainer.getElementsByTagName('td')[1].style.color = "";
        
        altcolor = !altcolor;
//    }
//    else                
//        nutrientcontainer.style.display = 'none';
    
    return altcolor;
}

Number.prototype.roundTo = function(digit)
{
    var places = 1;
    
    if(digit != null)
        places = digit;
   
   return Math.round(Math.round(this  * Math.pow(10,places))/Math.pow(10,places));
}