function thousandFormat(theNumber) {
	theNumber = "" + theNumber;
	var newNumber = ""
	var threeCounter = 0;
	if (theNumber.indexOf('.') != -1) {
		newNumber = theNumber.substring(theNumber.indexOf('.'),theNumber.length);
		theNumber = theNumber.substring(0,theNumber.indexOf('.'));
	}
	for (counter=(theNumber.length)-1;counter > -1;counter--) {
		if (threeCounter == 3 && theNumber.charAt(counter) != '-') {
			newNumber = "," + newNumber;
			threeCounter = 0;
		}	
		newNumber = theNumber.charAt(counter) + newNumber;
		threeCounter ++;
	}
	if (newNumber.indexOf('.') == -1) newNumber += ".00";
	if ((newNumber.length - newNumber.indexOf('.')) != 3 && newNumber.indexOf('.') != -1) {
		newNumber += "0";
	}
	return newNumber;
}

function stripCrap(theValue) {
	var newValue = "";
	var permitted = "0123456789.";
	for (counter=0;counter <= theValue.length;counter++) {
		if (permitted.indexOf(theValue.charAt(counter)) == -1) {
			// do nutin'
		} else {
		newValue = newValue + theValue.charAt(counter);
		}
	}
	return newValue-0;
}

function monthsToYears(totalMonths) {
	var years = Math.floor(totalMonths/12);
	var months = totalMonths-(years*12);
	return years + " years, " + months + " months";
}

function calculate() {

	document.calcform.output.value = "";

	// Get Data
	var principal 			= stripCrap(document.calcform.principal.value);
	var annualInterest 		= stripCrap(document.calcform.anlInterest.value);
	var monthlyPayment 		= stripCrap(document.calcform.mthPayment.value);
	var extraMonthlyPayment = stripCrap(document.calcform.extPayment.value);

	if (principal == 0) {
		alert("Please enter the Principal Amount owed (in numbers)");
		return false;
	}
	if (annualInterest == 0) {
		alert("Please enter the Annual Interest (in numbers)");
		return false;
	}
	if (monthlyPayment == 0) {
		alert("Please enter your current Monthly Payment (in numbers)");
		return false;
	}
	if (extraMonthlyPayment == 0) {
		alert("Please enter an amount to add to your current Monthly Payment (in numbers)");
		return false;
	}

	// Monthly Interest
	if (annualInterest > 0.999) {
		annualInterest = annualInterest/100;
	}
	var monthlyInterest = annualInterest/12;

	// Loop through calc on original payments
	var monthCount1 = 0;
	var workingPrincipal = principal;
	var newWorkingPrincipal = 0;
	while (workingPrincipal > 0) {
		newWorkingPrincipal = ((1+monthlyInterest)*workingPrincipal)-monthlyPayment;
		if (newWorkingPrincipal > workingPrincipal) {
			document.calcform.output.value = " \nThe current monthly payment is not sufficient to cover the interest rate. This loan will never be payed off!";
			return false;
		}
		workingPrincipal = newWorkingPrincipal;
		monthCount1 ++;
	}
	var totalPayments1 = monthCount1*monthlyPayment;

	// Loop through calc on extra payments
	var monthCount2 = 0;
	workingPrincipal = principal;
	newWorkingPrincipal = 0;
	while (workingPrincipal > 0) {
		newWorkingPrincipal = ((1+monthlyInterest)*workingPrincipal)-(monthlyPayment+extraMonthlyPayment);
		if (newWorkingPrincipal > workingPrincipal) {
			document.calcform.output.value = " \nThe current monthly payment is not sufficient to cover the interest rate. This loan will never be payed off!";
			return false;
		}
		workingPrincipal = newWorkingPrincipal;
		monthCount2 ++;
	}
	var totalPayments2 = monthCount2*(monthlyPayment+extraMonthlyPayment);

	var yearTime1 = monthsToYears(monthCount1);
	var yearTime2 = monthsToYears(monthCount2);

	var totalSavings            = totalPayments1 - totalPayments2;
	var totalTimeSavings 		= monthCount1 - monthCount2;
	var totalTimeSavingsYears   = monthsToYears(totalTimeSavings);

	var returnOnInvestment = (totalSavings/(monthCount2*extraMonthlyPayment))/(monthCount2/12);
	returnOnInvestment = (Math.ceil(returnOnInvestment*10000))/100;
	returnOnInvestment += "%";

	var theOutputString = "";

	theOutputString += "Payment of $" + thousandFormat(monthlyPayment) + " per month - \n";
	theOutputString += "     Number of Payments: " + monthCount1 + "\n";
	theOutputString += "     Total Payments: $" + thousandFormat(totalPayments1) + "\n";
	theOutputString += "     Time: " + yearTime1 + "\n \n";
	theOutputString += "Payment of $" + thousandFormat(monthlyPayment + extraMonthlyPayment) + " per month - \n";
	theOutputString += "     Number of Payments: " + monthCount2 + "\n";
	theOutputString += "     Total Payments: $" + thousandFormat(totalPayments2) + "\n";
	theOutputString += "     Time: " + yearTime2 + "\n \n";
	theOutputString += "Total Monetary Savings: $" + thousandFormat(totalSavings) + "\n";
	theOutputString += "Total Saving in Time: " + totalTimeSavingsYears + "\n";
	theOutputString += "Total Return on Investment: " + returnOnInvestment;

	document.calcform.output.value = theOutputString;
}