	function setScale(myFloat,scale)
	{
		FloatVal = (Math.round(myFloat * Math.pow(10,scale))/Math.pow(10,scale)); 
		return parseFloat(FloatVal);
	}
	function numberFormat(num, type, locale) {
		//for now we throw away type... we assume 'currency', we assume US for locale
		// num = num.toString().replace(/\$|\,/g,''); //pointless... we know there's no commas already, and we don't know if we have String.replace()
		if(isNaN(num))
			num = "0";
		sign = (num == (num = Math.abs(num)));
		num = Math.floor(num*100+0.50000000001);
		cents = num%100;
		num = Math.floor(num/100).toString();
		if(cents<10)
			cents = "0" + cents;
		for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
			num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
		return (((sign)?'':'-') + '$' + num + '.' + cents);
	}
	function CalcEngine (myForm) {
		// some incoming parameters.
	  	this.myForm = myForm;
	  	if (myForm.p1.value.replace) {  //forget complex validation rules. We're only processing one input, and it gets turned into a float anyway.
	    	myForm.p1.value = myForm.p1.value.toString().replace(/\$|\,/g,'');
	  	} else {
	    	if (myForm.p1.value.indexOf(",") > -1) alert ("please re-enter your Loan Amount without commas.\n");
	  	}
	  	this.p1 = (isNaN(parseFloat(myForm.p1.value)) ? 0 : parseFloat(myForm.p1.value));
	    this.apr1 = (isNaN(parseFloat(myForm.apr1.value)) ? 0 : parseFloat(myForm.apr1.value));
		this.length = 10;
	    this.monthlyPaymentVal = 0;
	  	this.terms = 0;
	  	this.totalInterestVal = 0;
	  	this.totalPaymentVal = 0;
	    this.Output = writeOutput ; 
		// Level Amortization  //
	 	var amortize = new PlusStaffordAmortize(this.p1, this.apr1, this.length);
	  	amortize.doPaymentSchedule();
	    if (amortize.monthlyPayment < 50 & amortize.monthlyPayment > 0) {
	  	  // We need to do some more math if our montly payment is less than $50.
	    	// Is it as easy as setting the monthly payment and recalcing? //I don't know, Brandan, maybe you should get a clue.
	    	amortize = new PlusStaffordAmortize(this.p1, this.apr1, this.length);
		    amortize.setMonthlyPayment(50);
		    amortize.doPaymentSchedule();
	  	}
	    this.terms = (amortize.getMonthlyPayment() > 50) ? 120 : amortize.getLength();
	  	// set the computed values in session
	  	this.monthlyPaymentVal =  amortize.getMonthlyPayment();
	    this.totalPaymentVal = amortize.getTotalInterestPaid() + this.p1;
	    this.totalInterestVal = amortize.getTotalInterestPaid();
	}
	function writeOutput() {
	  this.myForm.levelMonths.value = this.terms;
	  this.myForm.plusStaffordLevelPayment.value = numberFormat(setScale(this.monthlyPaymentVal,2));
	  this.myForm.levelTotalCost.value = numberFormat(setScale(this.totalPaymentVal,2));
	  this.myForm.levelTotalInt.value = numberFormat(setScale(this.totalInterestVal,2));
	}
	function isNumberFloat(inputString)
	{
		var objRegExp = /^[\+\-]?\d*\.?\d*$/ 
	  	if(!objRegExp.test(inputString))
		{
			return false;
		}
		return true;
	}
	function Calculon(myForm) {
	  	var errmsg = '';
	  	if(isNaN(myForm.p1.value))
			errmsg += "Loan balance must be number.\r\n";
		if(!isNumberFloat(myForm.apr1.value))
			errmsg += "Rate must be number.\r\n";	
		if(errmsg == "")
		{
	  		var roboto = new CalcEngine(myForm);
	  		roboto.Output() ;
	  	}
	  	else
	  		alert(errmsg);	
	}
