function calculate_mortgage() {
    var mortgageAmount = document.getElementById('totalamount');
    var repaymentPeriod = document.getElementById('repaymentperiod');
    var interestRate = document.getElementById('interestrate');
    var monthlyRepayment = document.getElementById('monthlyrepayment');
    var interestOnly = document.getElementById('interestonly');
    var monthlyRepayment12 = document.getElementById('monthlyrepayment12');
    var interestOnly12 = document.getElementById('interestonly12');
    
    if ((mortgageAmount == null || mortgageAmount.length == 0) ||
        (interestRate == null || interestRate.length == 0) ||
        (repaymentPeriod == null || repaymentPeriod.length == 0)) 
    {
        //alert('not all fields filled in');
        return;
    }

    // making sure that entries are valid by using check number
    if (!checkNumber(mortgageAmount, 1, 999999, "Mortgage Amount") ||
        !checkNumber(interestRate, .001, 1000, "Interest Rate") ||
        !checkNumber(repaymentPeriod, 5, 25, "Period")) 
    {
        monthlyRepayment.value = "";
        return;
    }
    mortgageAmount.style.backgroundColor = '#FFFFFF';
    interestRate.style.backgroundColor = '#FFFFFF';
    repaymentPeriod.style.backgroundColor = '#FFFFFF';

    interestRateVal = interestRate.value / 100;
    var P = ((mortgageAmount.value*interestRateVal)/12) * (1/(1-(Math.pow(1/(1+interestRateVal),repaymentPeriod.value))));
    monthlyRepayment.value = poundsPence( P );
    P = ((mortgageAmount.value*0.12)/12) * (1 / (1-(Math.pow((1/1.12),repaymentPeriod.value))));
    monthlyRepayment12.value = poundsPence( P );
    P = (mortgageAmount.value*interestRateVal)/12;
    interestOnly.value = poundsPence( P );
    P = (mortgageAmount.value*0.12)/12;
    interestOnly12.value = poundsPence( P );
    monthlyRepayment.style.backgroundColor = '#FFFFFF';
    monthlyRepayment.style.color= '#000000';
    monthlyRepayment12.style.backgroundColor = '#FFFFFF';
    monthlyRepayment12.style.color= '#000000';
    interestOnly.style.backgroundColor = '#FFFFFF';
    interestOnly.style.color= '#000000';
    interestOnly12.style.backgroundColor = '#FFFFFF';
    interestOnly12.style.color= '#000000';
}


function poundsPence( N ) {
    S = new String( N );
    var i = S.indexOf('.');
    if (i != -1) {
        S = S.substr( 0, i+3 );
        if (S.length-i < 3) {
            S = S + '0';
        }
    }
    return S;
}

//clears form
function clearForm(form) {
    form.A.value = "";
    form.T.value = "";
    form.R.value = "";
}
function checkNumber(input, min, max, msg) {
    msg = "The " + msg + " field has an invalid amount: (" + input.value + ")";
    //this makes sure that the number is a number
    var str = input.value;
    for (var i = 0; i < str.length; i++) {
        var ch = str.substring( i, i + 1)
        if ((ch < "0" || "9" < ch) && ch != '.') {
            input.style.backgroundColor = '#FFFF00';
            alert(msg);
            return false;
        }
    }
    //this makes sure that the number lies between the min and max values allowed
    var num = 0 + str
    if (num < min || max < num) {
        input.style.backgroundColor = '#FFFF00';
        alert(msg + " Please put into the range " + min + ".." + max);
        return false;
    }
    input.value = str;
    return true;
}
