var currency_symbol = '$';

// returns number as string in x,xxx.xx format.
function currency( anynum )
{
	if( isNaN( parseFloat( anynum ) ) )
		return anynum;

	var tmp = '$ ' + anynum;
	if( tmp == '$ ' )
		return '';
	var absnum = Math.abs( anynum );
	var intnum = Math.floor( absnum );  // isolate integer portion

	var intstr = intnum.toString();
	var l = intstr.length;          //use a loop to insert commas - MUCH shorter and more general
	for( var n = 1; n <= Math.floor((l-1)/3); n++ )
		intstr = intstr.substring( 0,l-n*3 ) + ',' + intstr.substring( l-n*3, 999 );

	var decstr = Math.round((absnum-intnum)*100).toString(); //isolate decimal portion
	while (decstr.length < 2)
		decstr = '0' + decstr;

	var retval = intstr;
	if( decstr != '00' )
		retval += '.' + decstr;
	if (anynum < 0)
		retval = '(' + retval + ')';

	return retval;
	//var tmp_currency_symbol = (currency_symbol.length == 1 ? currency_symbol + ' ' : '');
	//return tmp_currency_symbol + retval;
	//return '<nobr>$ ' + retval + '</nobr>';
}

// returns an integer if possible; if value is not an integer, returns a blank string
function blankInt( value )
{
	value = '' + value; // convert to string
	value = value.replace(/[^0-9\.]/g, '' ); // eliminate all non-numeric characters
	value = value.replace(/\.\d*$/, '' ); // eliminate the decimals, if there are any
	var v = parseInt( value );

	return isNaN( v ) ? '' : v;
}

// returns a float if possible; if value is not a float, returns a blank string
function blankFloat( value )
{
	value = '' + value; // convert to string
	value = value.replace(/[^0-9\.]/g, '' ); // eliminate all non-numeric characters
	var v = parseFloat( value );

	return isNaN( v ) ? '' : v;
}

function auto_recalc()
{
	recalc_payment();

	// prevent IE for Mac from Auto-Refreshing, which causes the browser to collapse
	if (navigator.userAgent.indexOf( 'MSIE' ) == -1 || navigator.userAgent.indexOf( 'Mac' ) == -1) window.setTimeout( 'auto_recalc()', 1000 );
}

function recalc_payment()
{
	var principal = blankInt( document.getElementById('price').value );

	var rate = blankFloat( document.getElementById('rate').value );
	var ppy = 12; //
	var term = blankInt( document.getElementById('term').value );

	var paymentdiv = document.getElementById('paymentamount');
	if( principal > 0 && rate >= 0 && ppy > 0 && term > 0 )
	{
		var ppir = parseFloat(rate)/100/parseFloat(ppy);
		var air = (parseFloat(rate)/100/parseFloat(ppy))+1;
		var tnp = parseFloat(ppy)*parseFloat(term);
		var ntnp = -1*parseFloat(tnp);
		var pa = (Math.round(100*(parseFloat(principal)/((1-Math.pow(air,ntnp))/ppir)))/100);
		paymentdiv.innerHTML = currency_symbol + currency( blankInt( pa ) );
	}
	else
	{
		if( principal <= 0 )
			paymentdiv.innerHTML = currency_symbol + '0';
		else if( term == 0 )
			paymentdiv.innerHTML = currency_symbol + currency( blankInt( principal ) );
		else
			paymentdiv.innerHTML = '&nbsp;';
		//else
			//   alert( 'principal: ' + principal + '\nRate: ' + rate + '\nppy: ' + ppy + '\nterm: ' + term );
	}
}

