



function pmt(IR, NP, PV) {

  var PMT = (PV * IR) / (1 - Math.pow(1 + IR, -NP))

  return round_decimals(PMT, 2)

}



function round_decimals(original_number, decimals) {

  var result1 = original_number * Math.pow(10, decimals)

  var result2 = Math.round(result1)

  var result3 = result2 / Math.pow(10, decimals)

  return (result3)

}





$(function() {



	$("#cash_flow_calculator input[type=text]").focus(

		function(){

			if (($(this).val()=='(ex. 85000)') || ($(this).val()=='(ex. 1000)') || ($(this).val()=='(ex. 5.5)'))

				$(this).val('').removeClass('gray');

		}

	);

	

	$("#cash_flow_calculator input[type=text]").blur(

		function(){

			if (($(this).val()=='') && ($(this).attr('name')=='purchase_price'))

				$(this).val('(ex. 85000)').addClass('gray');

			if (($(this).val()=='') && ($(this).attr('name')=='monthly_rental'))

				$(this).val('(ex. 1000)').addClass('gray');

			if (($(this).val()=='') && ($(this).attr('name')=='interest_rate'))

				$(this).val('(ex. 5.5)').addClass('gray');

		}

	);

	



	$("#cash_flow_calculator input[type=submit]").click(

		function(){



			var purchase_price=parseFloat(($("#cash_flow_calculator input[name=purchase_price]").val()).replace('$',''));

			var monthly_rental=parseFloat(($("#cash_flow_calculator input[name=monthly_rental]").val()).replace('$',''));

			

			var interest_rate_i=$("#cash_flow_calculator input[name=interest_rate]").val();

			var interest_rate=parseFloat(interest_rate_i);

			if ((interest_rate>1) || (interest_rate_i.search('%')!=-1))

				interest_rate=interest_rate/100;

			

			// first table

			

			$("#cash_flow_calculator td.purchase_price").html("$ "+number_format(purchase_price));

			$("#cash_flow_calculator td.closing_costs1").html("$ "+number_format(purchase_price*0.01));

			var total_initial_investment1=purchase_price*0.01+purchase_price;

			$("#cash_flow_calculator .total_initial_investment1").html("$ "+number_format(total_initial_investment1));

			$("#cash_flow_calculator td.monthly_rental").html("$ "+number_format(monthly_rental));

			var monthly_taxes1=(purchase_price*0.0225) / 12;

			$("#cash_flow_calculator td.monthly_taxes1").html("$ "+number_format(monthly_taxes1));

			$("#cash_flow_calculator td.monthly_insurance1").html("$ "+number_format(850/12));

			$("#cash_flow_calculator td.monthly_property_management_fees1").html("$ "+number_format(monthly_rental*0.09));

			$("#cash_flow_calculator .net_monthly_cash_flow1").html("$ "+number_format(monthly_rental-monthly_taxes1-(850/12)-(monthly_rental*0.09)));

			$("#cash_flow_calculator td.annual_gross_rental_income1").html("$ "+number_format(monthly_rental*12));

			$("#cash_flow_calculator td.annual_taxes1").html("$ "+number_format(monthly_taxes1*12));

			$("#cash_flow_calculator td.annual_insurance1").html("$ "+number_format(850));

			$("#cash_flow_calculator td.annual_property_management_fees1").html("$ "+number_format(monthly_rental*0.09*12));

			var annual_net_income1=monthly_rental*12-monthly_taxes1*12-850-monthly_rental*0.09*12;

			$("#cash_flow_calculator td.annual_net_income1").html("$ "+number_format(annual_net_income1));

			$("#cash_flow_calculator .return_on_investment1").html(sprintf("%.2f",annual_net_income1/total_initial_investment1*100)+" %");



			// second table

			

			//$("#cash_flow_calculator td.purchase_price").html(purchase_price);

			$("#cash_flow_calculator td.80_loan_amount2").html("$ "+number_format(purchase_price*0.8));

			$("#cash_flow_calculator td.20_down_payment2").html("$ "+number_format(purchase_price*0.2));

			$("#cash_flow_calculator td.closing_costs2").html("$ "+number_format(purchase_price*0.06));

			var total_initial_investment2=purchase_price*0.06+purchase_price*0.2;

			$("#cash_flow_calculator .total_initial_investment2").html("$ "+number_format(total_initial_investment2));

			$("#cash_flow_calculator td.loan_amount2").html("$ "+number_format(purchase_price*0.8));

			$("#cash_flow_calculator td.interest_rate").html(sprintf("%.2f",interest_rate*100)+" %");

			var principal_and_interest=pmt((interest_rate/12),360,purchase_price*0.8);

			$("#cash_flow_calculator td.principal_and_interest").html("$ "+number_format(principal_and_interest));

			$("#cash_flow_calculator td.monthly_taxes2").html("$ "+number_format(purchase_price*0.0225/12));

			$("#cash_flow_calculator td.monthly_insurance2").html("$ "+number_format(850/12));

			var monthly_mortgage_payment2=principal_and_interest+purchase_price*0.0225/12+850/12;

			$("#cash_flow_calculator td.monthly_mortgage_payment2").html("$ "+number_format(monthly_mortgage_payment2));

			$("#cash_flow_calculator td.monthly_property_management_fees2").html("$ "+number_format(monthly_rental*0.09));

			$("#cash_flow_calculator .net_monthly_cash_flow2").html("$ "+number_format(monthly_rental-monthly_mortgage_payment2-monthly_rental*0.09));

			$("#cash_flow_calculator td.annual_gross_rental_income2").html("$ "+number_format(monthly_rental*12));

			$("#cash_flow_calculator td.annual_mortgage_payment2").html("$ "+number_format(monthly_mortgage_payment2*12));

			$("#cash_flow_calculator td.annual_property_management_fees2").html("$ "+number_format(monthly_rental*0.09*12));

			var annual_net_income2=(monthly_rental-monthly_mortgage_payment2-monthly_rental*0.09)*12;

			$("#cash_flow_calculator td.annual_net_income2").html("$ "+number_format(annual_net_income2));

			$("#cash_flow_calculator .return_on_investment2").html(sprintf("%.2f",annual_net_income2/total_initial_investment2*100)+" %");

			

			if ($("#cash_flow_calculator #cfc_purchase_type_cash").is(':checked'))

			{

				$("#cash_flow_calculator .cfc_output_financed:visible").slideUp();

				$("#cash_flow_calculator .cfc_output_cash:hidden").slideDown();

			}

				

			if ($("#cash_flow_calculator #cfc_purchase_type_financed").is(':checked'))

			{

				$("#cash_flow_calculator .cfc_output_cash:visible").slideUp();

				$("#cash_flow_calculator .cfc_output_financed:hidden").slideDown();

			}

				

			

		}

	);





});
