
var nutritionXmlHttp;
var nutritionXML;
var currentSandwich;
var currentSandwichNode;
var nutritionURL = "/nutritionXML/nutrition.xml";


    var servingsize = 0.0;
    var calories = 0.0;
    var fatcalories = 0.0;
    var fat = 0.0;
    var saturatedfat = 0.0;
    var transfat = 0.0;
    var cholesterol = 0.0;
    var sodium = 0.0;
    var carbs = 0.0;
    var fiber = 0.0;
    var sugar = 0.0;
    var protein = 0.0;
    var vitamina = 0.0;
    var vitaminc = 0.0;
    var calcium = 0.0;
    var iron = 0.0;

function loadSandwich(sandwich)
{
    currentSandwich = sandwich;
    if(nutritionXML == null)
        loadXMLDoc();
}

function updateNutrition()
{
    servingsize = 0.0;
    calories = 0.0;
    fatcalories = 0.0;
    fat = 0.0;
    saturatedfat = 0.0;
    transfat = 0.0;
    cholesterol = 0.0;
    sodium = 0.0;
    carbs = 0.0;
    fiber = 0.0;
    sugar = 0.0;
    protein = 0.0;
    vitamina = 0.0;
    vitaminc = 0.0;
    calcium = 0.0;
    iron = 0.0;
    
        
    //do meats (from the XML file), double if neccesary
    var meats = currentSandwichNode.getElementsByTagName("meat");
    for(var m = 0; m < meats.length; m++)
    {
        var meatNode = getMeatNodeForSandwich(meats[m].firstChild.nodeValue);
        updateNutritionValues(meatNode, 1);
    }
        
    document.getElementById("nut_servingsize").innerHTML = round(servingsize);
    document.getElementById("nut_calories").innerHTML = round(calories);
    document.getElementById("nut_fatcalories").innerHTML = round(fatcalories);
    document.getElementById("nut_fat").innerHTML = round(fat);
    document.getElementById("nut_satfat").innerHTML = round(saturatedfat);
    document.getElementById("nut_transfat").innerHTML = round(transfat);
    document.getElementById("nut_cholesterol").innerHTML = round(cholesterol);
    document.getElementById("nut_sodium").innerHTML = round(sodium);
    document.getElementById("nut_carbs").innerHTML = round(carbs);
    document.getElementById("nut_fiber").innerHTML = round(fiber);
    document.getElementById("nut_sugar").innerHTML = round(sugar);
    document.getElementById("nut_protein").innerHTML = round(protein);
    document.getElementById("nut_vita").innerHTML = round(vitamina);
    document.getElementById("nut_vitc").innerHTML = round(vitaminc);
    document.getElementById("nut_calcium").innerHTML = round(calcium);
    document.getElementById("nut_iron").innerHTML = round(iron);
}

function round(value)
{
    value = Math.round(value * 10) / 10;
    if((value * 10) % 10 == 0)
        return value = value + ".0";
    return value;
}

function startOver()
{
    setDefaultValues();
}

function setDefaultValues()
{

    if(currentSandwichNode == null)
    {
        var sandwiches = nutritionXML.getElementsByTagName("sandwiches")[0].getElementsByTagName("sandwich");    
        for(var s = 0; s < sandwiches.length; s++)
        {
            if(sandwiches[s].getAttribute("id") == currentSandwich)
            {
                currentSandwichNode = sandwiches[s];
                break;
            }
        }
    }

        
    updateNutrition();
}

function changeBox(cbox, boolUpdate)
{
    var box = document.getElementById(cbox);
    box.checked = !box.checked;
    
    if(boolUpdate == null || boolUpdate == true)
        updateNutrition();
}

function getCheckedValue(radio)
{
	var radioObj = document.getElementsByName(radio);
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function setCheckedValue(radio, newValue, boolUpdate)
{
	var radioObj = document.getElementsByName(radio);
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
    if(boolUpdate == null || boolUpdate == true)
        updateNutrition();
}

function getCheckedActive(radio)
{
	var radioObj = document.getElementsByName(radio);
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.disabled)
			return !radioObj.disabled;
		else
			return true;
	return !radioObj[0].disabled;

	return true;
}

function setCheckedActive(radio, boolEnabled)
{
	var radioObj = document.getElementsByName(radio);
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.disabled = !boolEnabled;
		return;
	}
	for(var i = 0; i < radioLength; i++)
	{
		radioObj[i].disabled = !boolEnabled;
	}
}

function loadXMLDoc()
{
    if (window.XMLHttpRequest)
    {
        nutritionXmlHttp = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        nutritionXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (nutritionXmlHttp != null)
    {
        nutritionXmlHttp.onreadystatechange = XMLHttpStateChange;
        nutritionXmlHttp.open("GET", nutritionURL, true);
        nutritionXmlHttp.send(null);
    }
    else
    {
        alert("Your browser does not support XMLHTTP. Please update your browser to a recent version to use this site.");
    }
}

function XMLHttpStateChange()
{
    if (nutritionXmlHttp.readyState == 4)
    {
        if (nutritionXmlHttp.status == 200)
        {   
            nutritionXML = nutritionXmlHttp.responseXML.documentElement;
            setDefaultValues();
        }
        else
        {
            alert("Problem retrieving nutrition data. Please try reloading the page.");
        }
    }
}

function getMeatNodeForSandwich(strNode)
{
    var meats = nutritionXML.getElementsByTagName("meats")[0].getElementsByTagName("meat");
    for(var i = 0; i < meats.length; i++)
    {
        if(meats[i].getAttribute("id") == strNode)
        {
            return meats[i];
        }
    }
}

function getNodeValue(strNode, strColumn)
{
    var nodes = strNode.getElementsByTagName(strColumn);
    if(nodes[0] != null && nodes[0].firstChild != null && nodes[0].firstChild.nodeValue != "")
    {
        return nodes[0].firstChild.nodeValue;
    }
    return 0;
}

function updateNutritionValues(node, multiplier)
{
    servingsize += multiplier * getNodeValue(node, "servingsize");
    calories += multiplier * getNodeValue(node, "calories");
    fatcalories += multiplier * getNodeValue(node, "fatcalories");
    fat += multiplier * getNodeValue(node, "fat");
    saturatedfat += multiplier * getNodeValue(node, "saturatedfat");
    transfat += multiplier * getNodeValue(node, "transfat");
    cholesterol += multiplier * getNodeValue(node, "cholesterol");
    sodium += multiplier * getNodeValue(node, "sodium");
    carbs += multiplier * getNodeValue(node, "carbs");
    fiber += multiplier * getNodeValue(node, "fiber");
    sugar += multiplier * getNodeValue(node, "sugar");
    protein += multiplier * getNodeValue(node, "protein");
    vitamina += multiplier * getNodeValue(node, "vitamina");
    vitaminc += multiplier * getNodeValue(node, "vitaminc");
    calcium += multiplier * getNodeValue(node, "calcium");
    iron += multiplier * getNodeValue(node, "iron");
}
