var FoodValues = new Array(20);
var NumServings;
var ExpireFuture = "expires=Wednesday, 09-Nov-2020 23:12:40 GMT";
var ExpirePast = "expires=Wednesday, 09-Nov-2000 23:12:40 GMT";

function SetRecipeValue(ReceipeValue) {
    GetAllFoodsFromCookie();
    
    var x = 1;
    while(FoodValues[x] != "")
        x = x + 1;
        
    document.cookie = "food" + x + "=" + ReceipeValue + ";" + ExpireFuture;
    SubmitFoodsForm();
}

function SubmitFoodsForm()
{
    GetAllFoodsFromCookie();
    if(NumServings == "0") NumServings = "1";
	document.recipebuilder.NumServings.value = NumServings
	document.recipebuilder.food1.value = FoodValues[1];
	document.recipebuilder.food2.value = FoodValues[2];
	document.recipebuilder.food3.value = FoodValues[3];
	document.recipebuilder.food4.value = FoodValues[4];
	document.recipebuilder.food5.value = FoodValues[5];
	document.recipebuilder.food6.value = FoodValues[6];
	document.recipebuilder.food7.value = FoodValues[7];
	document.recipebuilder.food8.value = FoodValues[8];
	document.recipebuilder.food9.value = FoodValues[9];
	document.recipebuilder.food10.value = FoodValues[10];
	document.recipebuilder.food11.value = FoodValues[11];
	document.recipebuilder.food12.value = FoodValues[12];
	document.recipebuilder.food13.value = FoodValues[13];
	document.recipebuilder.food14.value = FoodValues[14];
	document.recipebuilder.food15.value = FoodValues[15];
	document.recipebuilder.food10.value = FoodValues[16];
	document.recipebuilder.food11.value = FoodValues[17];
	document.recipebuilder.food12.value = FoodValues[18];
	document.recipebuilder.food13.value = FoodValues[19];
	document.recipebuilder.food14.value = FoodValues[20];
    document.recipebuilder.submit();
}

function SetNumServings()
{
    NumServings = document.NumServings.Servings.value;
	document.cookie = "NumServings=" + NumServings + ";" + ExpireFuture;
	SubmitFoodsForm();
}

function DisplayFood(VAR) 
{
    var vals = VAR.split('!');

    document.foodsearch.form.value = vals[0];
    document.foodsearch.multiplier.value = vals[1];
    document.foodsearch.units.value = vals[2];
    document.foodsearch.submit();
}

function DeleteFood(FoodName) 
{
    GetAllFoodsFromCookie();

    idx = FoodName.replace("food", "");
    idx = parseInt(idx);

	// Start at the index of the current value.  To remove a cookie from the middle of the list, overwrite each cookie value with the next value
    while (FoodValues[idx] != "") 
    {
        document.cookie = "food" + idx + "=" + FoodValues[idx + 1] + ";" & ExpireFuture;
        idx = idx + 1;
    }
    // ExpirePast the last cookie of the list since we have decreased the total number by one
    document.cookie = "food" + (idx - 1) + "='';" + ExpirePast;

    if ((idx - 1) == 1)
		Document.Cookie = "NumServings='';" + ExpirePast;

    SubmitFoodsForm();
}

function DeleteAllFoods() 
{
    var x;
    for (x = 1; x <= 20; x = x + 1)
        document.cookie = "food" + x + "='';" + ExpirePast;
    document.cookie = "NumServings='';" + ExpirePast;
    SubmitFoodsForm();
}

function GetAllFoodsFromCookie() {
    var x;
    for (x = 1; x <= 20; x = x + 1)
        FoodValues[x] = "";
        
    var foodValue;
    for (x = 1; x <= 20; x = x + 1) {
        foodValue = GetFoodFromCookie("food" + x);
        if (foodValue != "")
            FoodValues[x] = foodValue;
    }
    NumServings = GetFoodFromCookie("NumServings");
}

function GetFoodFromCookie(FoodName) {
    var pos;
    var foodText;

    var cookieText = document.cookie;
    pos = cookieText.indexOf(FoodName);
    if (pos == -1) return "";

    foodText = cookieText.substr(pos, cookieText.length - pos);

    pos = foodText.indexOf(";");
    if (pos > -1)
        foodText = foodText.substr(0, pos);

    pos = foodText.indexOf("=");
    if (pos > -1)
        foodText = foodText.substr(pos + 1, foodText.length - pos - 1);
        
    return foodText;

}

function CalculateIdealWeight() {

    var inches = document.iw.inches.value;
    var feet = document.iw.feet.value;
    var frame = document.iw.frame.value;

    if ((inches == "") | (isInteger(inches) == false)) {
        alert("Error. Enter a valid value for 'inches'");
        return;
    }
    if ((feet == "") | (isInteger(feet) == false)) {
        alert("Error. Enter a valid value for 'feet'");
        return;
    }

    var gender = "";
    if (document.iw.gender1.checked == true)
        gender = "male";
    else if (document.iw.gender2.checked == true)
        gender = "female";
    else {
        alert("Error. Enter a valid value for 'gender'");
        return;
    }

    inches = parseInt(inches);
    feet = parseInt(feet);
    inches = (feet * 12) + inches;

    var weight = 0;
    if (gender == "male") {
        if (inches >= 60) {
            inches = inches - 60;
            weight = 106 + (6 * inches);
        }
        else {
            weight = 76 + (2.5 * (inches));
        }
    }
    else {
        if (inches >= 60) {
            inches = inches - 60;
            weight = 100 + (5 * inches);
        }
        else {
            weight = 70 + (2.5 * (inches));
        }
    }

    if (frame == "small")
        weight = weight - (weight * .07);
    else if (frame == "large")
        weight = weight + (weight * .07)

    weight = roundNumber(weight, 0);
    weight = weight + 1;
    alert("Your ideal weight is " + weight + " pounds");

}

function CalculateDCI() {

    var age = document.dci.age.value;
    var inches = document.dci.inches.value;
    var feet = document.dci.feet.value;
    var pounds = document.dci.pounds.value;
    var activity = document.dci.activity.value;

    if ((age == "") | (isInteger(age) == false)) {
        alert("Error. Enter a valid value for 'age'");
        return;
    }
    if ((inches == "") | (isInteger(inches) == false)) {
        alert("Error. Enter a valid value for 'inches'");
        return;
    }
    if ((feet == "") | (isInteger(feet) == false)) {
        alert("Error. Enter a valid value for 'feet'");
        return;
    }
    if ((pounds == "") | (isInteger(pounds) == false)) {
        alert("Error. Enter a valid value for 'pounds'");
        return;
    }

    var gender = "";
    if (document.dci.gender1.checked == true)
        gender = "male";
    else if (document.dci.gender2.checked == true)
        gender = "female";
    else {
        alert("Error. Enter a valid value for 'gender'");
        return;
    }
    age = parseInt(age);
    inches = parseInt(inches);
    pounds = parseInt(pounds);
    feet = parseInt(feet);
    activity = parseFloat(activity);
    
    inches = feet * 12 + inches;
    inches = inches * 2.5;
    pounds = pounds / 2.2;

    if (gender == "male") {
        pounds = 66 + (13.5 * pounds);
        inches = 5 * inches;
        age = 6.7 * age;
    }
    else if (gender == "female") {
        pounds = 650 + (9.4 * pounds);
        inches = 1.7 * inches;
        age = 4.7 * age;
    }

    var DCIVal = (pounds + inches - age) * activity;
    DCIVal = roundNumber(DCIVal, 0);
    alert("Your daily caloric intake is: " + DCIVal + " calories");
}

function MakeEmailAddress(Label) {
    var str = new Array();

    str[1] = "<a class='GreenLink' href='mailto:scb";
    str[2] = "row@";
    str[3] = "yaho";
    str[4] = "o.c";
    str[5] = "om'>" + Label + "</a>";

    x = 1;
    while (x <= 5) {
        document.write(str[x]);
        x = x + 1;
    }
}



function CalculateBMI() {
    var feet = document.BMI.feet.value;
    var inches = document.BMI.inches.value;
    var pounds = document.BMI.pounds.value;

    if ((feet == "") | (isInteger(feet) == false)) {
        alert("Error. Enter a valid value for 'feet'");
        return;
    }
    if ((inches == "") | (isInteger(inches) == false)) {
        alert("Error. Enter a valid value for 'inches'");
        return;
    }
    if ((pounds == "") | (isInteger(pounds) == false)) {
        alert("Error. Enter a valid value for 'pounds'");
        return;
    }
    inches = (feet * 12) + (inches * 1);

    var BMIval = (pounds / (inches * inches)) * 703;
    BMIval = roundNumber(BMIval, 1);
    alert("Your Body Mass Index is: " + BMIval);


}

function isInteger(s) {
    return (s.toString().search(/^-?[0-9]+$/) == 0);
}

function roundNumber(num, dec) {
    var result = Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec);
    return result;
}

function OpenPopUpWindow(url) {
    window.open(url, 'PopUpWindow', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,Width=600,height=260,screenX=50,screenY=50,top=50,left=50')
}