
// This function displays the red error messages at the bottom of the calculator.
function catchErrorPI(errorString){
	
	if (IE4plus){
		document.CalculatorFormPI.ResultPI.innerHTML = '<span class="calcTextError">Error:  ' + errorString + '</span>';
	} else if (NS4 || NS6){
		document.CalculatorFormPI.ResultPI.document.write('<span class="calcTextError">Error:  ' + errorString + '</span>');
		document.CalculatorFormPI.ResultPI.document.close();
	}
}

// This function is called whenever values from the input boxes are needed to ensure values are properly formatted.
function getValuePI(theString){

	var noJunk = "";
	var withDollar = "";
	var foundDecimal = 0;
	var foundAlphaChar = 0;
	theString += "";

	if (theString == "") { return(0); }
	for (i=0; i <= theString.length; i++){
		var thisChar = theString.substring(i, i+1);
		if (thisChar == "."){
			foundDecimal = 1;
			noJunk = noJunk + thisChar;
		}
		if ((thisChar < "0") || (thisChar > "9")){
			if ((thisChar != "$") && (thisChar !=".") && (thisChar != ",") && (thisChar != " ") && (thisChar !="")) foundAlphaChar = 1;
		}else{
			withDollar = withDollar + thisChar;
			noJunk = noJunk + thisChar;
		}

		if ((thisChar == "$") || (thisChar == ".") || (thisChar == ",")){
			withDollar = withDollar + thisChar;
		}
	}
	if (foundDecimal) { return parseFloat(noJunk); }
	else if (noJunk.length > 0) { return parseFloat(noJunk); }
	else return 0;
}

// This function is called whenever a number is needed to be formatted into a percent.
function formatPercentPI(theNumber){
	
	theNumber = getValuePI(theNumber);
	decimalPlaces = 3; 
	with (Math) theNumber = (round(theNumber * pow(10,decimalPlaces))) / pow(10,decimalPlaces); 
	return(theNumber + "%");
}

// This function is called whenever a number needs to be formatted into a U.S. dollar amount.
function formatUSCurrencyPI(theNumber){
	
	var isNegative = 0;

	if (theNumber != ""){
		var workingNumber = theNumber + ""; // Evaluate to a string

	if (workingNumber.charAt(0) == "-"){ 
		isNegative = 1;
		workingNumber = workingNumber.substring(1, workingNumber.length);
	}

	var withoutChars = "";

	for (x=0; x<=((workingNumber.length)-1); x++){
		thisChar = workingNumber.charAt(x);
		charAsNum = parseFloat(thisChar);

		if ( ((thisChar >= "0") & (thisChar <= "9")) || (thisChar == ".")  ){ 
			withoutChars += workingNumber.charAt(x);
		}
	}
	workingNumber = withoutChars;
	decimalPoint = workingNumber.indexOf(".");

	if (decimalPoint == -1){
		dollarValue = workingNumber;
		centsValue = "00";
	} else if (decimalPoint == 0){
		dollarValue = "0";
		centsValue = workingNumber.substring(decimalPoint + 1, workingNumber.length);
	} else {
		dollarValue = workingNumber.substring(0, decimalPoint);
		if (decimalPoint == (workingNumber.length - 1)){
			 centsValue = "00";
		} else {
			centsValue = workingNumber.substring(decimalPoint + 1, workingNumber.length);
			centsValue += "0";
			centsValue = centsValue.charAt(0) + centsValue.charAt(1);
		}
	}

	var theString = dollarValue;
	var totalCommas = Math.floor((theString.length - 1) / 3);
	var dollarAmt = "";
	x=dollarValue.length;
	position = 0;

	while (x > 0){
		x = x - 1;
		thisChar = dollarValue.charAt(x);
		rounded = Math.round(position/3);

		if ( (position/3 == rounded ) & (position != 0) ){
			dollarAmt = "," + dollarAmt;
		}
		dollarAmt = thisChar +  dollarAmt;
		position = position + 1;
	}

	if (isNegative){
		theString = "$" + dollarAmt + "." + centsValue;
	} else { 
		theString = "$" + dollarAmt + "." + centsValue;
	}
		return (theString);
	} else {
		return("");
	}
}

// This function is called when the user clicks on the "Calculate" button.
function calculatePI(){
	var errorString = '';
	
	if ((getValuePI(document.CalculatorFormPI.MiddleInputFieldFormPI.value) == '0') || (getValuePI(document.CalculatorFormPI.BottomInputFieldFormPI.value) == '0')) {
			errorString = 'All fields are required.';
			//catchErrorPI(errorString);
		} else {
			var TermInputFieldPIfull = document.CalculatorFormPI.TermInputFieldPI.options[document.CalculatorFormPI.TermInputFieldPI.selectedIndex].value;
			var TermInputFieldPI = TermInputFieldPIfull.substring(0,2);
			var MiddleInputFieldFormPI = getValuePI(document.CalculatorFormPI.MiddleInputFieldFormPI.value);
			var BottomInputFieldFormPI = getValuePI(document.CalculatorFormPI.BottomInputFieldFormPI.value);
		
		}
	if (errorString == ''){
		
			doMonthlyPayment(TermInputFieldPI, MiddleInputFieldFormPI, BottomInputFieldFormPI)
		
	}
}

// This function calculates how much the monthly payments would need to be for the user to reach their goal.
function doMonthlyPayment(loanYears, iRateAnnual, loanAmount){
	
	var monthlyPayment;
	
	monthlyPayment = figureMonthlyPayment(loanYears, iRateAnnual, loanAmount);
	monthlyPayment = Math.round(monthlyPayment * 100)/100;
	monthlyPayment = formatUSCurrencyPI(monthlyPayment);
	
	// Display calculation results.
	document.CalculatorFormPI.calcResultPI.value =  monthlyPayment;
}

// This function is called by the "doMonthlyPayment" function.
function figureMonthlyPayment(loanYears, iRateAnnual, loanAmount){
	
	var iRateMonthly;
	var totalPayments;
	
	iRateAnnual = (iRateAnnual / 100);
	iRateMonthly = (iRateAnnual / 12);
	totalPayments = (loanYears * 12);
	if (iRateAnnual == 0){
		monthlyPayment = (loanAmount / totalPayments)
	} else {
		monthlyPayment = (loanAmount * iRateMonthly) / (1 - Math.pow((1+iRateMonthly), (-1*totalPayments)) );
	}
  return(monthlyPayment);
}
