function Digits (input,dec)
{
	input += "";                         // Make sure input is a string:
	var original_input = input;          // Keep original copy of input string:
	var amount = parseFloat(input);      // Get float value:
	var posval = "";
	var decval = "";
	if (isNaN (amount))                  // Return unmodified input if we weren't able to convert it:
		return original_input;
	if (dec == 2)
	{
		amount = Math.round (100 * amount);  // Express amount in pennies, rounded to the nearest penny:
		var string;
		if (amount < 10)
		{
				string = "00" + amount;      // Convert amount to string and pad with leading zeros if necessary:
		}
		else if (amount < 100)
		{
				string = "0" + amount;
		}
		else
		{
				string = "" + amount;
		}
		posval = string.substring(0,string.length - 2);
		decval = string.substring(string.length - 2,string.length);
	}
	else
	{
		posval  = "" + Math.round (amount);  // Express amount
	}
	var frontnums = "";
	var midnums   = "";
	if (posval.length > 6)
	{
		frontnums = posval.substring(0,(posval.length - (parseInt(posval.length / 3) * 3) ))+",";
	}
	if (posval.length > 3)
	{
		midnums   = posval.substring(posval.length - 6,posval.length - 3)+",";
	}
	lastnums   = posval.substring(posval.length - 3, posval.length);
	newposval = "$ "+frontnums+midnums+lastnums;
	if (dec == 2)
	{
		newposval = newposval+"."+decval;
	}
	return newposval;
	// Return formatted currency string:
}


	
function computeForm(which) {
       
	var numpmts			= ""+document.calculator.annpmts.options[document.calculator.annpmts.selectedIndex].value;
	var termyears		= parseInt(document.calculator.term.options[document.calculator.term.selectedIndex].value);
	var termperiods		= termyears * parseInt(numpmts);
	var amortyears		= parseInt(document.calculator.amort.options[document.calculator.amort.selectedIndex].value);
	var totperiods		= amortyears*parseInt(numpmts);

	var prinamt1   = document.calculator.principal.value;
	var calcpmt    = document.calculator.calcpayment.value;
	var i          = parseFloat(document.calculator.interest.value);

    while (calcpmt.indexOf("$") ==0 || calcpmt.indexOf(" ") ==0) {calcpmt = calcpmt.substring(1,calcpmt.length);}

	while (calcpmt.indexOf(",") != -1)  {
		var calcpmt = ""+calcpmt.substring(0,calcpmt.indexOf(","))+calcpmt.substring(calcpmt.indexOf(",")+1,calcpmt.length)
	}

	// get rid of dollar signs on principal input 

    while (prinamt1.indexOf("$") ==0 || prinamt1.indexOf(" ") ==0) {prinamt1 = prinamt1.substring(1,prinamt1.length);}
	while (prinamt1.indexOf(",") != -1)  {
		var prinamt1 = ""+prinamt1.substring(0,prinamt1.indexOf(","))+prinamt1.substring(prinamt1.indexOf(",")+1,prinamt1.length)
	}

	var calcamt   = parseFloat(calcpmt);
	if (calcamt  != calcpmt && calcpmt > 0 ) {
		document.calculator.calcpayment.value = " ERROR ";
		return;
	}


	if ( ( (document.calculator.interest.value == null || document.calculator.interest.value.length == 0) && which != 'i') ||
        ( (document.calculator.principal.value == null || document.calculator.principal.value.length == 0) && which != 'p') ) {
        return;
    }
	calcdiff = 99999;
	newpmt = 0;
	calcrate       = 0.09;
	if (which == "i" && calcpmt > 0 ) {
		while (Math.abs(calcdiff) > 0.1 ) {
			newpmt = (parseFloat(prinamt1) * calcrate) / ( 1 - ( 1/ Math.pow((1+calcrate),totperiods)));
			calcdiff = (calcpmt - newpmt);
			var sign = (calcdiff) / Math.abs(calcdiff)
			calcrate =  (0.01 * sign) * ( Math.abs(calcdiff) > 500 ) + (0.0001 * sign) * ( Math.abs(calcdiff) > 20 ) +  (0.00001 * sign) * ( Math.abs(calcdiff) > 10 ) + (0.000001 * sign) * ( Math.abs(calcdiff) > 1 ) +(0.0000001 * sign)  + calcrate;
		}
		i =  2 * ( Math.pow((1+calcrate),parseInt(numpmts)/2) - 1);  
		i = parseInt(i * 1000000)/10000;
	}

    if (isNaN (i))  {                // Return unmodified input if we weren't able to convert it:
		alert('The Interest rate ('+i+') is causing an error!  Please re-enter values ...');
		document.calculator.ustermbal.value  =  0;
		document.calculator.ustermint.value  =  0;
	
		document.calculator.cantermbal.value = 0;
		document.calculator.cantermint.value = 0;

		document.calculator.canpayment.value = 0; 
		document.calculator.uspayment.value  = 0; 

		document.calculator.canallint.value  = 0;
		document.calculator.usallint.value   = 0;
		return;
	}
    if (i < 0.3) {
        i = i * 100.0;   
	}

    document.calculator.interest.value = i+" %";
    i = i / 100.0;   

	var cani = Math.pow((1+i/2),(2/parseInt(numpmts))) - 1;  
	var canibase = Math.pow((1+i/2),(2/12)) - 1;  
	var usi  = i / parseInt(numpmts);
	var usibase  = i / 12;
 
	if (which == "p" && cani != 0 ) {
		

		calcbasepmt = calcpmt;
		calcperiods     = parseInt(numpmts);

		if ((numpmts == '52a' ||numpmts == '26a') && cani != 0 ) {
			var calcbasepmt = calcpmt * (parseInt(numpmts)/13) ;
			var prinamt1  = (calcbasepmt/canibase) * ( 1 - (1/(Math.pow((1+canibase),(amortyears*12))) ) ) ;
		} else {
			var prinamt1  = (calcpmt/cani) * ( 1 - (1/(Math.pow((1+cani),(totperiods))) ) ) ;
		}
		document.calculator.principal.value = Digits(prinamt1,0);

	} 
	
	if (which == "p" && cani == 0 ) {
		var prinamt1  = (calcpmt*totperiods) ;
		document.calculator.principal.value = Digits(prinamt1,0);
	} 

	var prinamt   = parseFloat(prinamt1);
	if (prinamt  != prinamt1) {
		document.calculator.principal.value = " ERROR ";
		return;
	}

	if (amortyears < termyears ) {
		alert('The Amortization ('+amortyears+')  must be greater than the term ('+termyears+') !  Please re-enter values ...');
		document.calculator.ustermbal.value  =  0;
		document.calculator.ustermint.value  =  0;
	
		document.calculator.cantermbal.value = 0;
		document.calculator.cantermint.value = 0;

		document.calculator.canpayment.value = 0; 
		document.calculator.uspayment.value  = 0; 

		document.calculator.canallint.value  = 0;
		document.calculator.usallint.value   = 0;
		return;
	}

	if ( cani == 0 ) {
		var canbasepmt  = prinamt/ (amortyears * 12) ;
		var usbasepmt   = canbasepmt;
	} else {
		var canbasepmt  = (prinamt * canibase) / ( 1 - ( 1/ Math.pow((1+canibase),(amortyears*12))));
		var usbasepmt   = (prinamt * usibase ) / ( 1 - ( 1/ Math.pow((1+ usibase),(amortyears*12))));
	}		

	var canpmt = canbasepmt;
	var uspmt  = usbasepmt;

	if ((numpmts == '52a' ||numpmts == '26a') && cani != 0 ) {
		var canpmt = canbasepmt / (parseInt(numpmts)/13) ;
		var uspmt  = usbasepmt /  (parseInt(numpmts)/13) ;
	} 

	if ((numpmts == '52' || numpmts == '26'|| numpmts == '2'|| numpmts == '1' ) && cani != 0 ) {
		var canpmt = (prinamt * cani) / ( 1 - ( 1/ Math.pow((1+cani),totperiods)));
		var uspmt  = (prinamt * usi ) / ( 1 - ( 1/ Math.pow((1+ usi),totperiods)));
	} 

	document.calculator.calcpayment.value = Digits(canpmt,2); 
	document.calculator.canpayment.value = Digits(canpmt,2); 
	document.calculator.uspayment.value  = Digits(uspmt,2); 

	var canbal = prinamt1;
	var usbal  = canbal;
	var canint = 0;
	var usint = 0;
	var cantotint = 0;
	var ustotint = 0;
	

    for (var j = 0; j < termperiods ; j++) {
	 	canint    = cani * canbal;
		cantotint = cantotint + canint;
		canbal    = canbal - (canpmt - canint); 

	 	usint    = usi * usbal
		ustotint = ustotint + usint
		usbal    = usbal - (uspmt - usint); 
		if (usbal < 0  ) { 
			usbal = 0;
			break;
		}
		if (canbal < 0 ) { 
			canbal = 0 
			break;
		}
	}
	
	if (usbal < 0  ) { usbal = 0 };
	if (canbal < 0 ) { canbal = 0 };

	document.calculator.ustermbal.value   = Digits(parseInt(usbal),0);
	document.calculator.ustermint.value   = Digits(parseInt(ustotint),0);

	document.calculator.cantermbal.value  = Digits(parseInt(canbal),0);
	document.calculator.cantermint.value  = Digits(parseInt(cantotint),0);

	var k = j;
    for (var j = termperiods; j < totperiods; j++) {
	 	canint    = cani * canbal;
		cantotint = cantotint + canint;
		canbal    = canbal - (canpmt - canint); 

	 	usint    = usi * usbal
		ustotint = ustotint + usint
		usbal    = usbal - (uspmt - usint); 
		k = j;	
		if (usbal < 0  ) { 
			usbal = 0;
			break;
		}
		if (canbal < 0 ) { 
			canbal = 0 
			break;
		}
	}
	caactyears = Digits((k+1)/parseInt(numpmts),2);
	if (cani == 0 ) {
		caactyears = amortyears ;
	} else {
		caactyears = caactyears.substring(1,caactyears.length);
	}
	document.calculator.caactyears.value  = ""+caactyears+" yrs";
	document.calculator.usactyears.value  = ""+caactyears+" yrs";
	document.calculator.canallint.value = Digits(parseInt(cantotint),0);
	document.calculator.usallint.value  = Digits(parseInt(ustotint),0);
	document.calculator.principal.value = Digits(prinamt,0);
}
