function round(number,X)
{
    X = (!X ? 2 : X);
    return Math.floor(number*Math.pow(10,X))/Math.pow(10,X);
}


function round2(n) {
  var s = "" + Math.round(n * 100) / 100
  var i = s.indexOf('.')
  if (i < 0) return s + ".00"
  var t = s.substring(0, i + 1) + s.substring(i + 1, i + 3)
  if (i + 2 == s.length) t += "0"
  return t
}

function calcArb()
{

        // If fractional odds specified, show as decimal as well
        if (eval( document.arbcalculator.frac_odds_1.value )+1 >=1)
        {
        document.arbcalculator.dec_odds_1.value = eval( document.arbcalculator.frac_odds_1.value ) + 1;
        }
        
        if (eval( document.arbcalculator.frac_odds_2.value )+1 >=1)
        {
        document.arbcalculator.dec_odds_2.value = eval( document.arbcalculator.frac_odds_2.value ) + 1;
        }
        
        if (eval( document.arbcalculator.frac_odds_3.value )+1 >=1)
        {
        document.arbcalculator.dec_odds_3.value = eval( document.arbcalculator.frac_odds_3.value ) + 1;
        }
        
        if (eval( document.arbcalculator.frac_odds_4.value )+1 >=1)
        {
        document.arbcalculator.dec_odds_4.value = eval( document.arbcalculator.frac_odds_4.value ) + 1;
        }
        
        if (eval( document.arbcalculator.frac_odds_5.value )+1 >=1)
        {
        document.arbcalculator.dec_odds_5.value = eval( document.arbcalculator.frac_odds_5.value ) + 1;
        }
        
        if (eval( document.arbcalculator.frac_odds_6.value )+1 >=1)
        {
        document.arbcalculator.dec_odds_6.value = eval( document.arbcalculator.frac_odds_6.value ) + 1;
        }
        
        var odds1 = eval( document.arbcalculator.dec_odds_1.value ) ;
	var odds2 = eval( document.arbcalculator.dec_odds_2.value ) ;
	var odds3 = eval( document.arbcalculator.dec_odds_3.value ) ;
	var odds4 = eval( document.arbcalculator.dec_odds_4.value ) ;
	var odds5 = eval( document.arbcalculator.dec_odds_5.value ) ;
	var odds6 = eval( document.arbcalculator.dec_odds_6.value ) ;

	if( isNaN(odds1) ) cover1 = 0; else { cover1 = (100/odds1); }
	if( isNaN(odds2) ) cover2 = 0; else { cover2 = (100/odds2); }
	if( isNaN(odds3) ) cover3 = 0; else { cover3 = (100/odds3); }
	if( isNaN(odds4) ) cover4 = 0; else { cover4 = (100/odds4); }
	if( isNaN(odds5) ) cover5 = 0; else { cover5 = (100/odds5); }
	if( isNaN(odds6) ) cover6 = 0; else { cover6 = (100/odds6); }

	var cover = (cover1+cover2+cover3+cover4+cover5+cover6);

	var temp = "";

	var total = document.arbcalculator.total_stake.value;

	if( isNaN(odds1) )
		odds1 = 0;
	else
	{
                stake1 = ((cover1/cover)*total).toFixed(2);
	}

	if( isNaN(odds2) )
		odds2 = 0;
	else
	{
                stake2 = ((cover2/cover)*total).toFixed(2);
	}

	if( isNaN(odds3) )
		odds3 = 0;
	else
	{
		stake3 = ((cover3/cover)*total).toFixed(2);
	}

	if( isNaN(odds4) )
		odds4 = 0;
	else
	{
		stake4 = ((cover4/cover)*total).toFixed(2);
	}

	if( isNaN(odds5) )
		odds5 = 0;
	else
	{
		stake5 = ((cover5/cover)*total).toFixed(2);
	}

	if( isNaN(odds6) )
		odds6 = 0;
	else
	{
		stake6 = ((cover6/cover)*total).toFixed(2);
	}
	
	if( (odds1) == 0 ) stake1 = 0;
	if( (odds2) == 0 ) stake2 = 0;
	if( (odds3) == 0 ) stake3 = 0;
	if( (odds4) == 0 ) stake4 = 0;
	if( (odds5) == 0 ) stake5 = 0;
	if( (odds6) == 0 ) stake6 = 0;

        under_round = cover1 + cover2 + cover3 + cover4 + cover5 + cover6 ;
        true_return = (((100-under_round)/under_round)*100).toFixed(3);
        if (stake1 != 0) document.getElementById("Stake1").innerHTML = "£"+stake1;
	if (stake2 != 0) document.getElementById("Stake2").innerHTML = "£"+stake2;
	if (stake3 != 0) document.getElementById("Stake3").innerHTML = "£"+stake3;
	if (stake4 != 0) document.getElementById("Stake4").innerHTML = "£"+stake4;
	if (stake5 != 0) document.getElementById("Stake5").innerHTML = "£"+stake5;
	if (stake6 != 0) document.getElementById("Stake6").innerHTML = "£"+stake6;


	if( 100 <= cover )
	{
        document.getElementById("profitability").innerHTML = "<font color=red>"+ true_return + "%";
        document.getElementById("Profit").innerHTML = "<font color=red>£"+((total * true_return)/100).toFixed(2);
        document.getElementById("TotalReturn").innerHTML = "<font color=red>£"+ (stake1 * odds1).toFixed(2);
        }
        else
        {
        document.getElementById("profitability").innerHTML = true_return + "%";
        document.getElementById("Profit").innerHTML = "£"+((total * true_return)/100).toFixed(2);
        document.getElementById("TotalReturn").innerHTML = "£"+ (stake1 * odds1).toFixed(2);
        }
        return;
}

