function roundNumber(num, dec)
{
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}
function calculate()
{
    var homeSidePrice = document.getElementById("home_side_price").value;
    var awaySidePrice = document.getElementById("away_side_price").value;
    var drawPrice = document.getElementById("draw_price").value;
    var totalStake = document.getElementById("total_stake").value;
    
    var homeSideStake = document.getElementById("home_side_stake");
    var awaySideStake = document.getElementById("away_side_stake");
    var drawStake = document.getElementById("draw_stake");
    var homeSidePercentage = document.getElementById("home_side_percentage");
    var awaySidePercentage = document.getElementById("away_side_percentage");
    var drawPercentage = document.getElementById("draw_percentage");
    var totalPercentage = document.getElementById("total_percentage");
    var profitability = document.getElementById("profitability");
    var profit = document.getElementById("profit");
    var totalReturn = document.getElementById("total_return");
    
    while(homeSideStake.hasChildNodes())
        homeSideStake.removeChild(homeSideStake.childNodes[0]);
    while(awaySideStake.hasChildNodes())
        awaySideStake.removeChild(awaySideStake.childNodes[0]);
    while(drawStake.hasChildNodes())
        drawStake.removeChild(drawStake.childNodes[0]);
    while(homeSidePercentage.hasChildNodes())
        homeSidePercentage.removeChild(homeSidePercentage.childNodes[0]);
    while(awaySidePercentage.hasChildNodes())
        awaySidePercentage.removeChild(awaySidePercentage.childNodes[0]);
    while(drawPercentage.hasChildNodes())
        drawPercentage.removeChild(drawPercentage.childNodes[0]);
    while(totalPercentage.hasChildNodes())
        totalPercentage.removeChild(totalPercentage.childNodes[0]);
    while(profitability.hasChildNodes())
        profitability.removeChild(profitability.childNodes[0]);    
    while(profit.hasChildNodes())
        profit.removeChild(profit.childNodes[0]);
    while(totalReturn.hasChildNodes())
        totalReturn.removeChild(totalReturn.childNodes[0]);

    homeSidePrice = parseFloat(homeSidePrice);
    awaySidePrice = parseFloat(awaySidePrice);
    drawPrice = parseFloat(drawPrice);
    totalStake = parseFloat(totalStake);
    if(isNaN(homeSidePrice) || isNaN(awaySidePrice) || isNaN(totalStake))
    {
        homeSideStake.appendChild(document.createTextNode("?"));
        awaySideStake.appendChild(document.createTextNode("?"));
        drawStake.appendChild(document.createTextNode("?"));
        totalReturn.appendChild(document.createTextNode("?"));
        profit.appendChild(document.createTextNode("?"));
        profitability.appendChild(document.createTextNode(("?")));
        return;
    }
    drawPrice = isNaN(drawPrice) ? null : drawPrice;    
    
    var homeSidePercentageValue = 100 / homeSidePrice;
    var awaySidePercentageValue = 100 / awaySidePrice;
    var drawPercentageValue = drawPrice == null ? null : 100 / drawPrice;
    var totalPercentageValue = homeSidePercentageValue + awaySidePercentageValue + (drawPrice == null ? 0 : drawPercentageValue);
    homeSidePercentage.appendChild(document.createTextNode(roundNumber(homeSidePercentageValue, 1) + "%"));
    awaySidePercentage.appendChild(document.createTextNode(roundNumber(awaySidePercentageValue, 1) + "%"));
    if(drawPrice != null)
        drawPercentage.appendChild(document.createTextNode(roundNumber(drawPercentageValue, 1) + "%"));
    totalPercentage.appendChild(document.createTextNode(roundNumber(totalPercentageValue, 1) + "%"));
    
    var priceCount = 2 + (drawPrice == null ? 0 : 1 );
    var factor = ((totalStake / priceCount) / priceCount) * 100;
    var homeSideStakeRatio = factor / homeSidePrice;
    var awaySideStakeRatio = factor / awaySidePrice;
    var drawStakeRatio = (drawPrice == null ? null : factor / drawPrice);
    var stakeRatioTotal = homeSideStakeRatio + awaySideStakeRatio + (drawStakeRatio == null ? 0 : drawStakeRatio);
    factor = totalStake / stakeRatioTotal;
    var homeSideStakeValue = homeSideStakeRatio * factor;
    var awaySideStakeValue = awaySideStakeRatio * factor;
    homeSideStake.appendChild(document.createTextNode("$" + Math.ceil(homeSideStakeValue)));
    awaySideStake.appendChild(document.createTextNode("$" + Math.ceil(awaySideStakeValue)));
    if(drawPrice != null)
        drawStake.appendChild(document.createTextNode("$" + Math.ceil(drawStakeRatio * factor)));
    var grossReturn = homeSideStakeValue * homeSidePrice;
    totalReturn.appendChild(document.createTextNode("$" + Math.ceil(grossReturn)));
    profit.appendChild(document.createTextNode("$" + Math.ceil(grossReturn - totalStake)));
    profitability.appendChild(document.createTextNode(roundNumber((grossReturn - totalStake) / totalStake * 100, 1) + "%"));
}