var $j=jQuery.noConflict();

$j(document).ready(function(){
	//Init
	$j('.oppOneEstimate, .oppTwoEstimate, .oppThreeEstimate, .oppFourEstimate, .oppFiveEstimate, .oppSixEstimate, .answer').attr("value", 0);
	
	//Clear Form on click
	$j('.clearForm').click(function () { 
		$j('.oppOne, .oppTwo, .oppThree, .oppFour, .oppFive, .oppSix').attr("value", "");
		$j('.description').attr("value", "");
		doItAll();
	});
	
	//get rates
	var fiveYearRate = $j('#5year').next().next().text();
	fiveYearRate = parseFloat(fiveYearRate.substr(0, fiveYearRate.length));
	var tenYearRate = $j('#10year').next().next().text();
	tenYearRate = parseFloat(tenYearRate.substr(0, tenYearRate.length));
	var fifteenYearRate = $j('#15year').next().next().text();
	fifteenYearRate = parseFloat(fifteenYearRate.substr(0, fifteenYearRate.length));
	
	//Change rate checkbox
	$j('#5year, #10year, #15year').attr('checked', false);//incase of refresh
	$j('#5year').attr('checked', true);
	$j('.oppOneRate, .oppTwoRate, .oppThreeRate, .oppFourRate, .oppFiveRate, .oppSixRate').attr("value", fiveYearRate);
	$j('.oppOneMonths, .oppTwoMonths, .oppThreeMonths, .oppFourMonths, .oppFiveMonths, .oppSixMonths').attr("value", 60);
		
	$j('#5year, #10year, #15year').click(function(){
		$j('#5year, #10year, #15year').attr('checked', false);
		$j(this).attr('checked', true);
		
		var timeInMonths = $j(this).attr("value");
		var newRate = 0;
		if(timeInMonths == 180) newRate = 6.99;
		else if(timeInMonths == 120) newRate = 5.99;
		else newRate = 5.25;
		
		$j('.oppOneRate, .oppTwoRate, .oppThreeRate, .oppFourRate, .oppFiveRate, .oppSixRate').attr("value", newRate);
		$j('.oppOneMonths, .oppTwoMonths, .oppThreeMonths, .oppFourMonths, .oppFiveMonths, .oppSixMonths').attr("value", timeInMonths);
		
		doItAll();
	});
	
	//Limit the fields to numbers, backspace, del, tab, shift+tab
	$j('.oppOne, .oppTwo, .oppThree, .oppFour, .oppFive, .oppSix').keypress(function(e){
	    if( e.which!=11 && e.which!=9 && e.which!=8 && e.which!=0 && (e.which<48 || e.which>57))
			return false;
	});
	//Perform calculations each time data is entered
	$j('.oppOne, .oppTwo, .oppThree, .oppFour, .oppFive, .oppSix').keyup(function(e){
		doIt($j(this).attr("class"));									
	});
	
	$j('#dbCalc tbody tr:odd').addClass("odd");
});

function doItAll(){
	doIt('oppOne');
	doIt('oppTwo');
	doIt('oppThree');
	doIt('oppFour');
	doIt('oppFive');
	doIt('oppSix');
	doMainTotal();
}

function doIt(which){
	var answer = 0;
	
	if($j("." + which).attr("value") != "") 
		answer = $j("." + which).attr("value");
	$j("." + which + 'Estimate').attr("value", calcMonthlyPayment(answer, $j("." + which + 'Rate').attr("value"), $j("." + which + 'Months').attr("value")));
	
	doMainTotal();
}

function calcMonthlyPayment(p, i, n){//Principal, Interest, time in months
	var j = i / 1200;
	
	return roundNumber((p * ( j / (1 - Math.pow((1 + j), -n)))) * 1.005, 2);
}

function roundNumber(num, dec) {
	return Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
}

function doMainTotal(){
	var answer = 0;
	
	//Add Totals
	$j('.addThese').each(function(i, operand){
		if($j(operand).attr("value") != 0)
			answer += parseFloat($j(operand).attr("value"));
    });
	answer = roundNumber(answer, 2);
	$j('.answer').attr("value", answer);
}
