
 
/*
    Dom Ready
*/

$(function() {


    $('#income1').blur(function() {
        $(this).formatCurrency({ symbol: '', roundToDecimalPlace: 0 });
    });

    $('#income2').blur(function() {
        $(this).formatCurrency({ symbol: '', roundToDecimalPlace: 0 });
    });


    $('#btn1').click(function() {

        var error = 0;
        var income1 = $('#income1').asNumber();
        var income2 = $('#income2').asNumber();

        //alert(parseFloat(income1));
        if (income1 == '' || !parseFloat(income1)) {
            error = 1;
            alert('Income 1 is required');
            $('#income1').focus().select();
        }

        if (income2 != '' && !parseFloat(income2) && income2 > 0) {
            error = 1;
            alert('Income 2 should be numbers only');
            $('#income2').focus().select();
        }

        var totalIncome = $('#totalincome');
        if (error == 0) {
            if (income2 == 0) {
                // only income1 entered.
                totalIncome.val(
                        (5 * income1)
                    ).formatCurrency({ symbol: '', roundToDecimalPlace: 0 });
            } else {
                // both entered.
                totalIncome.val(
                        (5 * (parseFloat(income1) + parseFloat(income2)))
                    ).formatCurrency({ symbol: '', roundToDecimalPlace: 0 });

            }
        }

    });

});


// repayment types
// 0 - interest only
// 1 - normal repayment


function calculateRepayments() {
		loan = parseInt(document.calcform.loan.value);
		rate = document.calcform.rate.value;
		mtype = document.calcform.mortgagetype.selectedIndex; 
		
		if (!parseInt(loan) > 0){
			alert("Please enter a value for the loan. Must be whole pounds.");
			return false;
		}

		if (!parseInt(rate) > 0 | isNaN(rate)){
			alert("Please enter an interest rate.");
			return false;
		}

		term = parseInt(document.calcform.period.value);
		rate100 = (rate / 100)
		// (loan amount * decimal rate) / 12
		monthlyrepayments = ((loan * rate100) / 12);
		
		if (mtype == 0) { 
			monthlyrepayments = (Math.round(monthlyrepayments * 100)) / 100;
			document.calcform.answer.value = monthlyrepayments;
			return false; 
		} else if (mtype == 1) { // Full Repayment 
			var topline = (loan * rate * 12);
			var midline = Math.pow((1+(rate/1200)), (-term * 12));
			midline = (100 * 12 * (1 - midline));
			var answer = ((topline / midline) / 12);
			answer = (Math.round(answer * 100)) / 100;
			document.calcform.answer.value = answer; 
		}		
	return false;			
}

function calculateRange(){
		income1 = parseInt(document.rangeform.income1.value);
		income2 = parseInt(document.rangeform.income2.value);
		
		
		if(isNaN(income1)){
			income1 = 0;
		}
		
		if(isNaN(income2)){
			income2 = 0;
		}
		
		totalincome = (income1 + income2 );
		low = totalincome * 4;
		
		high = totalincome * 3.5;
		
		document.rangeform.low.value = low;
		document.rangeform.high.value = high;
		
		return false;
	}
