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 principal = normalizeNum(document.loandata.principal.value);
    var interest = normalizeNum(document.loandata.interest.value);
    var payments = normalizeNum(document.loandata.years.value);
	var docompute = 1;

	// work around for 0%
	//if (interest == 0) 
	//	{interest = 0.000001;}

	// Normalize Input
	if (document.loandata.principal.value == "") { 
		docompute = 0;
	} else {
		document.loandata.principal.value = formatCurrency(principal); 
	}
	if (document.loandata.interest.value == "") { 
		docompute = 0;
	} else {
		document.loandata.interest.value = formatPercent(interest); 
	}
	if (document.loandata.years.value == "") { 
		docompute = 0;
	} else {
		document.loandata.years.value = formatNumber(payments); 
	}
	
	// compute
	if (payments !=0) {
		payments = payments * 12;
	} else {
		payments = 1;
	}
	// Now compute the monthly payment figure, using esoteric math.
	var monthly;
	if (interest != 0) {
		interest = interest / 100 / 12;
		var x = Math.pow(1 + interest, payments);
		monthly = (principal*x*interest)/(x-1);
	} else {
		monthly = principal / payments;
	}

    // Check that the result is a finite number. If so, display the results
    if (
		(docompute == 1) &&
		!isNaN(monthly) && 
        (monthly != Number.POSITIVE_INFINITY) &&
        (monthly != Number.NEGATIVE_INFINITY)) {

        document.loandata.payment.value = formatCurrency(round(monthly));
        document.loandata.total.value = formatCurrency(round(monthly * payments));
        document.loandata.totalinterest.value = 
            formatCurrency(round((monthly * payments) - principal));
    }
    // Otherwise, the user's input was probably invalid, so don't
    // display anything.
    else {
        document.loandata.payment.value = "";
        document.loandata.total.value = "";
        document.loandata.totalinterest.value = "";
    }
}

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


// Remove non-numeric values
function normalizeNum(strValue)
{
	var outNum = strValue;
	outNum = outNum.replace("$", "");
	outNum = outNum.replace("%", "");
	outNum = outNum.replace(",", "");
	if (outNum == '') {outNum = '0';}
	return outNum;
}

function formatCurrency(strValue)
{
	strValue = strValue.toString().replace(/\$|\,/g,'');
	dblValue = parseFloat(strValue);

	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue*100+0.50000000001);
	intCents = dblValue%100;
	strCents = intCents.toString();
	dblValue = Math.floor(dblValue/100).toString();
	if(intCents<10)
		strCents = "0" + strCents;
	for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
		dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
		dblValue.substring(dblValue.length-(4*i+3));
	return (((blnSign)?'':'-') + '$' + dblValue + '.' + strCents);
}


function formatPercent(strValue)
{
	return strValue + '%';
}


function formatNumber(strValue)
{
	strValue = strValue.toString().replace(/\$|\,/g,'');
	dblValue = parseFloat(strValue);

	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue*100+0.50000000001);
	intCents = dblValue%100;
	strCents = intCents.toString();
	dblValue = Math.floor(dblValue/100).toString();
	if(intCents<10)
		strCents = "0" + strCents;
	for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
		dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
		dblValue.substring(dblValue.length-(4*i+3));
	return (((blnSign)?'':'-') + dblValue + ((strCents == '00')?'':'.' + strCents));
}