function ValidateNumericField(sFieldName, sValue, nBlankFlag, nMsgFlag)
{
	if (sValue.length < 1){
		if (nBlankFlag == 0){
			if (nMsgFlag == 1)
				alert("Please enter a value for " + sFieldName + ".");
			return false;
		}
	}else{
		var decPtAt = 0;
		for (var i = 0; i < sValue.length; i++){
			var localChar = sValue.charAt(i);
			if (localChar < "0" || localChar > "9"){
				if (localChar == "." && decPtAt == 0){
					decPtAt = i + 1;
				}else{
					if (nMsgFlag == 1)
						alert("Please remove the '" + localChar + "' character from " + sFieldName + ".");
					return false;
				}
			}
		}
	}
	return true;
}

function formatFloat (expr, decplaces){
	var str = "" + Math.round( eval(expr) * Math.pow(10, decplaces) );
	while (str.length <= decplaces){
		str = "0" + str;
	}
	var decpoint = str.length - decplaces;
	return str.substring(0, decpoint) + "." + str.substring(decpoint, str.length);
}

function PeriodizeRate(annualRate, compoundPerYear, decimalFlag){
  if (decimalFlag == 1) periodicRate = annualRate/(compoundPerYear * 100);
  else periodicRate = annualRate/compoundPerYear;
  return periodicRate;
}
function CalculatePayoff()
{
	var currentPayment, intTerm, answer;
	var pctARate, pctPRate, currentBalance;

	currentBalance = document.calculator.balance.value;

	if (ValidateNumericField("your credit card balance", currentBalance, 0, 1) == false)
	{	currentBalance = 0.00;
	}
	else
	{	currentBalance = parseFloat(currentBalance); // Balance retrieved
	}
	pctARate = document.calculator.arate.value;

	if (ValidateNumericField("credit card interest rate", pctARate, 0, 1) == false)
	{	pctARate = 0.00;
	}
	else 
	{	pctARate = parseFloat(pctARate);  // Credit card rate retrieved
	}

	pctPRate = PeriodizeRate(pctARate, 12, 1);
	currentPayment = document.calculator.payment.value;
	intTerm = document.calculator.term.value;

	if (currentPayment == "" && ValidateNumericField("desired months until debt free", intTerm, 0, 1) == true)
	{ 
		// user is looking for payments/month
		answer = calcPayments(currentBalance, intTerm, pctPRate);
		document.calculator.answer.value = "$" + formatFloat(answer, 2) + " a Month";
	}
	else if(intTerm == "" && ValidateNumericField("payment amount per month", currentPayment, 0, 1) == true)
	{ 
		// user is looking for term
		answer = CalculateTerm(currentBalance, currentPayment, pctPRate);
		document.calculator.answer.value = answer + " Month(s)";
	}
	else
	{
		alert("OPPS! You entered a value for 'Payment amount per month' and 'Desired months until debt free'.  Please enter a value for only one field -- NOT BOTH.");
		return;
	}
}

function CalculateTerm(balance, payment, pRate)
{
	var interest, paymentCount;
	
	paymentCount = 0;
	while (balance > 0.00){
		interest = parseInt(balance) * pRate;
		if (parseInt(interest,10) > payment)
		{ 
			alert("OPPS! That payment amount isn't enough to cover the accumulated interest.  Try increasing your payment amount.");
			return "infinite";	
		}
		balance = parseInt(balance, 10) + parseInt(interest, 10) - parseInt(payment, 10);
		paymentCount = paymentCount + 1;
	}
	return paymentCount;
}

function calcPayments(balance, term, pRate)
{
	var temp1, paymentValue;

	temp1 = Math.pow((pRate + 1), term);
	paymentValue = ( balance / ( (1/pRate) - ( 1/(pRate * temp1)) ) );
	return parseFloat(paymentValue);
}