//function hidePmtResult(){
	//this hide function is in the bodyOnloadHideAll function for the <body onload "hide"> to work with multiple onload functions

  //document.getElementById("pmtResult").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 purchasePrice = document.loanpmt.purchasePrice.value;
    var downpmt = document.loanpmt.downpmt.value;
    var interest = document.loanpmt.interest.value;
	var months = document.loanpmt.months.value;
	var term = months / 12;
	
	var IN = interest /12;
	var PE = months;
	
	if (isNaN(downpmt)) {
	    downpmt =0;	
	}
	
	var PR = purchasePrice - downpmt;
	
	
	var payment = (PR * IN) / (1 - Math.pow(1 + IN, -PE));
 
	
    // Check that the result is a finite number. If so, display the results
    if (!isNaN(payment)  && (payment != Number.POSITIVE_INFINITY) && (payment != Number.NEGATIVE_INFINITY)  
	) {									


			//show results in div
			//document.getElementById("result").innerHTML = "Your result is "+round((monthly * payments) - principal);
			document.getElementById("pmtResult").innerHTML = "Amount of the loan:\t$" + PR + "<br>" +
"Annual interest rate:\t" + interest * 100 + "% <br>" +
"Term of the loan:\t" + term + " years<br><br>" +
"Monthly payment:\t$" + round(payment)+"<br><br>This payment does not take into account the sales tax you will be charged and any additional fees you may have to pay ";
    }
    // Otherwise, the user's input was probably invalid, so don't
    // display anything.
    else {
        document.getElementById("pmtResult").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);
}


