function Add(l)
{
    var q = document.AlcoholTest.q;
    var sel = document.AlcoholTest.sel;
    var drink = document.AlcoholTest.drinks;
    var last = sel.options.length;
    var tag = drink.options[drink.selectedIndex].text;
    var val = drink.options[drink.selectedIndex].value;

    n = q.value;

    to = n + " - " + tag;

    vo = n + "|" + val;

    sel.options[last] = new Option(to, vo);
}

function AddOther(l)
{
    var ml = document.AlcoholTest.ml;
    var av = document.AlcoholTest.av;
    var sel = document.AlcoholTest.sel;

    var last = sel.options.length;

    qty = parseInt(ml.value);

    if(isNaN(qty) == true || qty <= 0)
    {
        if(l == "it")
            msg = "Inserire la quantità in millilitri";
        else
            msg = "Enter quantity in milliliters";

        alert(msg);

        ml.focus();

        return;
    }

    alc = parseInt(av.value);

    if(isNaN(alc) == true || alc <= 0)
    {
        if(l == "it")
            msg = "Inserire il volume alcolico";
        else
            msg = "Enter alcohol volume";

        alert(msg);

        av.focus();

        return;
    }

    var tag = "";

    if(l == "it")
        tag = "Altra Bevanda";
    else
        tag = "Other drink";

    tag += " (" + qty + "ml/" + alc + "%)";

    val = "1|" + qty + "|" + alc;

    sel.options[last] = new Option(tag, val);
}

function RemoveAll()
{
    var sel = document.AlcoholTest.sel;

    sel.options.length = 0;
}

function Remove()
{
    var sel = document.AlcoholTest.sel;

    if(sel.selectedIndex >= 0)
    {
        sel.options[sel.selectedIndex] = null;
    }
}

function GetAlcoholGrams(v, p)
{
    var g = ((v * p) / 100) * 0.789;

    return g;
}

function Proceed(l)
{
    var w = document.AlcoholTest.weight;
    var h = document.AlcoholTest.height;
    var sel = document.AlcoholTest.sel;

    wgt = parseInt(w.value);

    if(isNaN(wgt) == true || wgt <= 0)
    {
        if(l == "it")
            msg = "Inserire il peso";
        else
            msg = "Enter weight";

        alert(msg);

        w.focus();

        return;
    }

    hgt = parseInt(h.value);

    if(isNaN(hgt) == true || hgt <= 0)
    {
        if(l == "it")
            msg = "Inserire l'altezza";
        else
            msg = "Enter height";

        alert(msg);

        h.focus();

        return;
    }

    if(sel.options.length == 0)
    {
        if(l == "it")
            msg = "Inserire almeno una bevanda";
        else
            msg = "Enter at least one drink";

        alert(msg);

        return;
    }

    var ag = 0;

    for(var i=0; i < sel.options.length; i++)
    {
        var drink = sel.options[i].value.split('|');
			 	
        vol = parseFloat(drink[0]) * parseFloat(drink[1]);

        ag += GetAlcoholGrams(vol, parseFloat(drink[2]));
    }

    document.AlcoholTest.a.value = ag;

    document.AlcoholTest.submit();
}
