roll = function(a,b)
{
    this.sum = 0;
    for (this.rollCount=0; this.rollCount<a; this.rollCount++)
    {
        this.sum = this.sum + Math.ceil(b*Math.random());
    }
    return this.sum;
}

getById = function(e)
{
    return document.getElementById(e);
}

numCategories = 8;

categories = {
"virtue":
    ["Honour",
    "Humility",
    "Kindness",
    "Temperance",
    "Chastity",
    "Patience",
    "Generosity",
    "Fortitude"],
"sin":
    ["Treachery",
    "Pride",
    "Envy/Spite",
    "Gluttony",
    "Lust",
    "Wrath",
    "Avarice",
    "Sloth"]
};

function describe(category, num)
{
    type = (num < 11) ? "virtue":"sin";
    diff = Math.floor(Math.abs(num - 10.5));
    
    if (diff < 1)
    {
        return "--";
    }
    else
    {
        return categories[type][category]+" ("+diff+")";
    }
}

function updateBar(category, num)
{
    type = (num < 11) ? "virtue":"sin";
    diff = Math.floor(Math.abs(num - 10.5));
    
    if (diff >= 1)
    {
        for (j=1;j<=diff;j++)
        {
            barEl = getById(type+"_"+category+"_"+j);
            barEl.className = type;
        }
    }
}

function clearBars()
{
    for (cat=0; cat<numCategories; cat++)
    {
        for (count=1; count<8; count++)
        {
            clear = getById("virtue_"+cat+"_"+count);
            clear.className="";
            clear = getById("sin_"+cat+"_"+count);
            clear.className="";
        }
    }
}

function doRoll()
{
    clearBars();
    for (i=0; i<numCategories; i++)
    {
        el = getById("category_"+i);
        result = roll(3,6);
        
        el.innerHTML = result;
        
        desc = getById("categoryDescription_"+i);
        desc.innerHTML = describe(i,result); 
        updateBar(i,result);
    }
}

