function SetSavingAmounts ()
{
	if(!document.getElementById("ctrCostPerGallon")) return;

	var dCostPerGallon = document.getElementById("ctrCostPerGallon").value;
	var dAnnualMileage = document.getElementById("ctrAnnualMileage").value;
	var dMilesPerGallon = document.getElementById("ctrMilesPerGallon").value;

	$.post("/calculator/calculator.php",
		{costPerGallon:dCostPerGallon, annualMileage:dAnnualMileage, milesPerGallon:dMilesPerGallon}, OnCalculationComplete);
}

function OnCalculationComplete(result)
{
  document.getElementById("ctrSavingsAmount").value = result;
}

function EnterPress (event)
{
  if (event.keyCode == 13)
  {
    SetSavingAmounts();
  }
}

function CalcFormulaType_2(sCalcCoefficient, ctrCostPerGallon, ctrAnnualMileage, ctrMilesPerGallon, sProductCode)
{
    var dCostPerGallon = parseFloat(document.getElementById(ctrCostPerGallon).value.replace(/\$/,""));
    var dAnnualMileage = parseFloat(document.getElementById(ctrAnnualMileage).value.replace(/,/,""));
    var dMilesPerGallon = parseFloat(document.getElementById(ctrMilesPerGallon).value);
    var dPricePerTire = parseFloat(document.getElementById('tp_' + sProductCode).value);

    var FuelSaving = ((dCostPerGallon*dAnnualMileage)/dMilesPerGallon) - ((dCostPerGallon*dAnnualMileage)/(dMilesPerGallon*(1+sCalcCoefficient)));
    FuelSaving = Math.round(FuelSaving);
    var SavingByIncreasedTireLife = (dPricePerTire*4/(450000/dAnnualMileage)) - (dPricePerTire*4/(450000*1.31/dAnnualMileage));
    SavingByIncreasedTireLife = Math.round(SavingByIncreasedTireLife);
    var TotalSaving = FuelSaving + SavingByIncreasedTireLife;
    
    document.getElementById('as_' + sProductCode).innerHTML = "$" + String(FuelSaving);
    document.getElementById('itl_' + sProductCode).innerHTML = "$" + String(SavingByIncreasedTireLife);
    document.getElementById('tas_' + sProductCode).innerHTML = "$" + String(TotalSaving);
}
