// Debt Calc code for section # 2

function valNumericStr(strFieldName, strValue, intBlankFlag, intMsgFlag){
// intBlankFlag == 0: can't be blank, intBlankFlag == !0: can be blank
// intMsgFlag == 0: no error messages, intMsgFlag == 1:  error messages
	if (strValue.length < 1){
		if (intBlankFlag == 0){
			if (intMsgFlag == 1)
				alert("Please enter a value for " + strFieldName + ".");
			return false;
		}
	}else{
		var decPtAt = 0;
		for (var i = 0; i < strValue.length; i++){
			var localChar = strValue.charAt(i);
			if (localChar < "0" || localChar > "9"){
				if (localChar == "." && decPtAt == 0){
					decPtAt = i + 1;
				}else{
					if (intMsgFlag == 1)
						alert("Please remove the '" + localChar + "' character from " + strFieldName + ".");
					return false;
				}
			}
		}
	}
	return true;
}
function formatFloat (expr, decplaces){
// Based on Danny Goodman's JavaScript Bible (3rd ed) pg. 569
	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 switchFunction()
{
	var curPayment, intTerm, answer;
	var pctARate, pctPRate, curBalance;

	curBalance = document.calculator.balance.value;
	if (valNumericStr("balance", curBalance, 0, 1) == false) curBalance = 0.00;
	else curBalance = parseFloat(curBalance); // Balance retrieved
	pctARate = document.calculator.arate.value;
	if (valNumericStr("interest rate", pctARate, 0, 1) == false) pctARate = 0.00;
	else pctARate = parseFloat(pctARate);  // Credit card rate retrieved
	pctPRate = periodizeRate(pctARate, 12, 1);
	curPayment = document.calculator.payment.value;
	intTerm = document.calculator.term.value;
	
	if (curPayment == "see below" && valNumericStr("months until balance is paid", intTerm, 0, 1) == true)
	{ // user is looking for payments/month
		answer = calcPayments(curBalance, intTerm, pctPRate);
		document.calculator.answer.value = "$" + formatFloat(answer, 2) + " every month";
	}
	else if(intTerm == "see below" && valNumericStr("payment per month", curPayment, 0, 1) == true)
	{ // user is looking for term
		answer = calcTerm(curBalance, curPayment, pctPRate);
		document.calculator.answer.value = answer + " months";
	}
	else
	{
		//alert("Please enter a value for either 'payment per month' or 'months until paid', not both.");
		return;
	}
}
function calcTerm(balance, payment, pRate){
	var interest, paymentCount;
	
	paymentCount = 0;
	while (balance > 0.00){
		interest = parseInt(balance) * pRate;
		if (parseInt(interest,10) > payment)
		{ 
			alert("Your payments are not enough to cover the accumulated interest.  Try increasing the payments.");
			return "infinite";	
		}
		balance = parseInt(balance, 10) + parseInt(interest, 10) - parseInt(payment, 10);
		paymentCount = paymentCount + 1;
//    alert("balance: " + balance + " | pRate: " + pRate + " | interestAmount: " + interestAmount)
	}
	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);
}

