//function hide(){
	//this hide function is in the bodyOnloadHideAll function for the <body onload "hide"> to work with multiple onload functions
  //document.getElementById("result").innerHTML = "";
//}


function calculate() {
    // Get the user's input from the form. Assume it is all valid.
    // Convert interest from a percentage to a decimal, and convert from
    // an annual rate to a monthly rate. Convert payment period in years
    // to the number of monthly payments.
    var miles = document.covinfo.miles.value;
    var currentMPG = document.covinfo.currentMPG.value;
    var newMPG = document.covinfo.newMPG.value;
	var gasPrice = document.covinfo.gasPrice.value;

	var currentCost = (miles/currentMPG)*gasPrice;
	var newCost = (miles/newMPG)*gasPrice;
	var savings = currentCost - newCost;
	
	
    // Check that the result is a finite number. If so, display the results
    if (!isNaN(savings)  && (savings != Number.POSITIVE_INFINITY) && (savings != Number.NEGATIVE_INFINITY)  
	&& !isNaN(currentCost) && (currentCost != Number.POSITIVE_INFINITY) && (currentCost != Number.NEGATIVE_INFINITY)
	&& !isNaN(newCost) && (newCost != Number.POSITIVE_INFINITY) && (newCost != Number.NEGATIVE_INFINITY)
	) {									


			//show results in div
			//document.getElementById("result").innerHTML = "Your result is "+round((monthly * payments) - principal);
			document.getElementById("result").innerHTML = "Gas for your current vehicle costs $"+round(currentCost)+".<br><br>Gas for the new vehicle will cost $"+round(newCost)+".<br><br>You will save $"+round(savings)+" per year on gas";
    }
    // Otherwise, the user's input was probably invalid, so don't
    // display anything.
    else {
        document.getElementById("result").innerHTML = "Please check your values for a proper calculation ";
		
    }
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}



function round(x) {
	// This simple method rounds a number to two decimal places.
  //return Math.round(x*100)/100;
  //rounds up to a whole number
  return Math.round(x);
}

