
function validateNumber(localItem, minValue, maxValue, msg) {
    var decPtAt = 0;
    for (var i = 0; i < localItem.value.length; i++){
        var localChar  = localItem.value.charAt(i);
        if (localChar < "0" || localChar > "9"){
            if (localChar == "." && decPtAt == 0){
                decPtAt = i + 1;
            }else{
                if (localChar == " "){
                    localChar = "Spaces cannot";
                }else if (localChar == "."){
                    localChar = "Only one decimal point can";
                }else{
                    localChar = "The character \"" + localChar
                                + "\" cannot";
                }
                alert(localChar + " be used in the " + msg + " field." +
                    "  Please correct your entry to use only numerical" +
                    " values, a single decimal point, and no spaces or" +
                    " commas.");
                localItem.focus();
                localItem.select();
                return false;
            }
        }
    }
    return checkLimits(localItem, minValue, maxValue, msg);
}

function checkLimits(localItem, minValue, maxValue, msg) {
    if (localItem.value < minValue || localItem.value > maxValue){
        alert("Value for the " + msg + " field must be between "
            + minValue + " and " + maxValue + ".");
        localItem.focus();
        localItem.select();
        return false;
    }
    return true;
}

function getInterestPaid(totalPaid, balance, principal) {
    return totalPaid - (principal - balance);
}

/**
 * returns loan balance after n payments have been made
 *
 * a: interest per year
 * p: amount of each equal payment
 * n: number of months elapsed
 */
function getBalance(principal, a, p, n) {
    var i = a / 1200;
    var balance = principal * Math.pow(1+i, n);
    balance = balance - (p / i * (Math.pow(1+i, n) - 1));
    return balance;
}

function getPayment(a,n,p) {
    var AnnualInterestRate = a/100;
    var MonthRate=AnnualInterestRate/12;
	
    return Math.ceil((p*MonthRate)/(1-Math.pow((1+MonthRate),(-1*n)))*100)/100;
}
