var $j=jQuery.noConflict();

$j(document).ready(function(){
	//Init
	$j('.oppOneSquareFootage, .oppTwoSquareFootage, .oppThreeSquareFootage, .oppFourSquareFootage, .oppFiveSquareFootage, .oppSixSquareFootage, .oppSevenSquareFootage, .oppEightSquareFootage, .oppNineSquareFootage, .oppTenSquareFootage, .oppElevenSquareFootage, .oppTwelveSquareFootage, .oppThirteenSquareFootage, .oppFourteenSquareFootage, .oppFifteenSquareFootage').attr("value", 0);
	$j('.oppOneFinanced, .oppTwoFinanced, .oppThreeFinanced, .oppFourFinanced, .oppFiveFinanced, .oppSixFinanced, .oppSevenFinanced, .oppEightFinanced, .oppNineFinanced, .oppTenFinanced, .oppElevenFinanced, .oppTwelveFinanced, .oppThirteenFinanced, .oppFourteenFinanced, .oppFifteenFinanced, .oppSixteenFinanced, .oppSeventeenFinanced').attr("value", 0);
	$j('.oppOneEstimate, .oppTwoEstimate, .oppThreeEstimate, .oppFourEstimate, .oppFiveEstimate, .oppSixEstimate, .oppSevenEstimate, .oppEightEstimate, .oppNineEstimate, .oppTenEstimate, .oppElevenEstimate, .oppTwelveEstimate, .oppThirteenEstimate, .oppFourteenEstimate, .oppFifteenEstimate, .oppSixteenEstimate, .oppSeventeenEstimate, .answer').attr("value", 0);
	
	//Clear Form on click
	$j('.clearForm').click(function () { 
		$j('.oppOne, .oppTwo, .oppThree, .oppFour, .oppFive, .oppSix, .oppSeven, .oppEight, .oppNine, .oppTen, .oppEleven, .oppTwelve, .oppThirteen, .oppFourteen, .oppFifteen, .oppSixteen, .oppSeventeen').attr("value", "");
		$j('.oppOneSquareFootage, .oppTwoSquareFootage, .oppThreeSquareFootage, .oppFourSquareFootage, .oppFiveSquareFootage, .oppSixSquareFootage, .oppSevenSquareFootage, .oppEightSquareFootage, .oppNineSquareFootage, .oppTenSquareFootage, .oppElevenSquareFootage, .oppTwelveSquareFootage, .oppThirteenSquareFootage, .oppFourteenSquareFootage, .oppFifteenSquareFootage').attr("value", 0);
		$j('.oppOneFinanced, .oppTwoFinanced, .oppThreeFinanced, .oppFourFinanced, .oppFiveFinanced, .oppSixFinanced, .oppSevenFinanced, .oppEightFinanced, .oppNineFinanced, .oppTenFinanced, .oppElevenFinanced, .oppTwelveFinanced, .oppThirteenFinanced, .oppFourteenFinanced, .oppFifteenFinanced, .oppSixteenFinanced, .oppSeventeenFinanced').attr("value", 0);
		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, .oppSevenRate, .oppEightRate, .oppNineRate, .oppTenRate, .oppElevenRate, .oppTwelveRate, .oppThirteenRate, .oppFourteenRate, .oppFifteenRate, .oppSixteenRate, .oppSeventeenRate').attr("value", fiveYearRate);
	$j('.oppOneMonths, .oppTwoMonths, .oppThreeMonths, .oppFourMonths, .oppFiveMonths, .oppSixMonths, .oppSevenMonths, .oppEightMonths, .oppNineMonths, .oppTenMonths, .oppElevenMonths, .oppTwelveMonths, .oppThirteenMonths, .oppFourteenMonths, .oppFifteenMonths, .oppSixteenMonths, .oppSeventeenMonths').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 = fifteenYearRate;
		else if(timeInMonths == 120) newRate = tenYearRate;
		else newRate = fiveYearRate;
		//console.log(newRate);
		
		$j('.oppOneRate, .oppTwoRate, .oppThreeRate, .oppFourRate, .oppFiveRate, .oppSixRate, .oppSevenRate, .oppEightRate, .oppNineRate, .oppTenRate, .oppElevenRate, .oppTwelveRate, .oppThirteenRate, .oppFourteenRate, .oppFifteenRate, .oppSixteenRate, .oppSeventeenRate').attr("value", newRate);
		$j('.oppOneMonths, .oppTwoMonths, .oppThreeMonths, .oppFourMonths, .oppFiveMonths, .oppSixMonths, .oppSevenMonths, .oppEightMonths, .oppNineMonths, .oppTenMonths, .oppElevenMonths, .oppTwelveMonths, .oppThirteenMonths, .oppFourteenMonths, .oppFifteenMonths, .oppSixteenMonths, .oppSeventeenMonths').attr("value", timeInMonths);
		
		doItAll();
	});
	
	//Limit the fields to numbers, backspace, del, tab, shift+tab
	$j('.oppOne, .oppTwo, .oppThree, .oppFour, .oppFive, .oppSix, .oppSeven, .oppEight, .oppNine, .oppTen, .oppEleven, .oppTwelve, .oppThirteen, .oppFourteen, .oppFifteen, .oppSixteen, .oppSeventeen').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, .oppSeven, .oppEight, .oppNine, .oppTen, .oppEleven, .oppTwelve, .oppThirteen, .oppFourteen, .oppFifteen').keyup(function(e){
		doIt($j(this).attr("class"));									
	});
	$j('.oppSixteen, .oppSeventeen').keyup(function(e){
		doSpecial($j(this).attr("class"));									 
	});
	
	$j('#heCalc tbody tr:odd').addClass("odd");
});

function doItAll(){
	doIt('oppOne');
	doIt('oppTwo');
	doIt('oppThree');
	doIt('oppFour');
	doIt('oppFive');
	doIt('oppSix');
	doIt('oppSeven');
	doIt('oppEight');
	doIt('oppNine');
	doIt('oppTen');
	doIt('oppEleven');
	doIt('oppTwelve');
	doIt('oppThirteen');
	doIt('oppFourteen');
	doIt('oppFifteen');
	doSpecial('oppSixteen');
	doSpecial('oppSeventeen');
	//doMainTotal();
}

function doIt(which){
	var answer = 1;
	var thingsToDo = [];

	//Add individuals
	$j("." + which).each(function(i, operand){
		if($j(operand).attr("value") == "") thingsToDo[i] = 0;
		else thingsToDo[i] = parseInt($j(operand).attr("value"));
    });
	for(var i = 0; i < thingsToDo.length; i++){
		answer *= thingsToDo[i];
	}
	$j("." + which + 'SquareFootage').attr("value", answer);
	if($j("." + which + 'SquareFootage').attr("value") == "") $j("." + which + 'SquareFootage').attr("value", 0)
	$j("." + which + 'Financed').attr("value", $j("." + which + 'SquareFootage').attr("value") * $j("." + which + 'Cost').attr("value"));
	if($j("." + which + 'Financed').attr("value") == "") $j("." + which + 'Financed').attr("value", 0)
	$j("." + which + 'Estimate').attr("value", calcMonthlyPayment($j("." + which + 'Financed').attr("value"), $j("." + which + 'Rate').attr("value"), $j("." + which + 'Months').attr("value")));
	
	//doMainTotal();
}

function doSpecial(which){
	var answer = $j("." + which).attr("value");
	
	if($j("." + which).attr("value") == "") $j("." + which).attr("value", 0)
	$j("." + which + 'Financed').attr("value", $j("." + which).attr("value") * $j("." + which + 'Cost').attr("value"));
	if($j("." + which + 'Financed').attr("value") == "") $j("." + which + 'Financed').attr("value", 0)
	$j("." + which + 'Estimate').attr("value", calcMonthlyPayment($j("." + which + 'Financed').attr("value"), $j("." + which + 'Rate').attr("value"), $j("." + which + 'Months').attr("value")));
}

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 = 1;
	var thingsToDo = [];
	
	answer = 0;
	thingsToDo = [];
	//Add Totals
	$j('.addThese').each(function(i, operand){
		if($j(operand).attr("value") == "") thingsToDo[i] = 0.00;
		else thingsToDo[i] = parseInt($j(operand).attr("value"));
    });
	for(var i = 0; i < thingsToDo.length; i++){
		answer += thingsToDo[i];
	}
	$j('.answer').attr("value", answer);
}*/
