var A1=0.28,A2=0.36,M1=0.33,M2=0.38,term=30,freq=12,periods=term*freq;function calculate_mortgage(widget){var good_inputs,monthly_income,monthly_debt,interest_rate,down_payment,estimated_taxes,estimated_insurance,tax_insurance,A_total_payment,M_total_payment,temp,A_limit,M_limit,payment,A_loan,M_loan,A_price,M_price,A_total,M_total;if(widget){monthly_income=parseFloat($("#calcYourInfoWidget #monthly_income").val())}else{monthly_income=parseFloat($("#monthly_income").val())}if(monthly_income==null||monthly_income==""||isNaN(monthly_income)||monthly_income<=0){if(widget){$("#calcYourInfoWidget #monthly_income").focus().addClass("fieldError")}else{$("#monthly_income").focus().addClass("fieldError")}return false}else{if(widget){$("#calcYourInfoWidget #monthly_income").removeClass("fieldError")}else{$("#monthly_income").removeClass("fieldError")}}term=parseFloat($("#term").val());periods=term*freq;monthly_debt=parseFloat($("#monthly_debt").val());down_payment=parseFloat($("#down_payment").val());if(isNaN(down_payment)){down=0}interest_rate=parseFloat($("#interest_rate").val());tax_insurance=get_tax_insurance();A_total_payment=monthly_income*A1;temp=(monthly_income*A2)-monthly_debt;if(temp<A_total_payment){A_limit="debt";A_total_payment=temp}else{A_limit="income"}M_total_payment=monthly_income*M1;temp=(monthly_income*M2)-monthly_debt;if(temp<M_total_payment){M_total_payment=temp}A_payment=A_total_payment-tax_insurance;A_loan=get_loan(interest_rate,A_payment);A_price=A_loan+down_payment;M_payment=M_total_payment-tax_insurance;M_loan=get_loan(interest_rate,M_payment);M_price=M_loan+down_payment;if(widget){$("#calcYourResultsWidget #house_price_A").text(format_number(A_price));$("#calcYourResultsWidget #loan_amt_A").text(format_number(A_loan));$("#calcYourResultsWidget #monthly_payment_A").text(format_number(A_payment));$("#calcYourResultsWidget #tax_insurance_A").text(format_number(tax_insurance));$("#calcYourResultsWidget #monthly_total_A").text(format_number(A_total_payment));$("#calcYourResultsWidget #house_price_M").text(format_number(M_price));$("#calcYourResultsWidget #loan_amt_M").text(format_number(M_loan));$("#calcYourResultsWidget #monthly_payment_M").text(format_number(M_payment));$("#calcYourResultsWidget #tax_insurance_M").text(format_number(tax_insurance));$("#calcYourResultsWidget #monthly_total_M").text(format_number(M_total_payment))}else{$("#house_price_A").text(format_number(A_price));$("#loan_amt_A").text(format_number(A_loan));$("#monthly_payment_A").text(format_number(A_payment));$("#tax_insurance_A").text(format_number(tax_insurance));$("#monthly_total_A").text(format_number(A_total_payment));$("#house_price_M").text(format_number(M_price));$("#loan_amt_M").text(format_number(M_loan));$("#monthly_payment_M").text(format_number(M_payment));$("#tax_insurance_M").text(format_number(tax_insurance));$("#monthly_total_M").text(format_number(M_total_payment))}return true}function equalHeight(group){tallest=0;group.each(function(){thisHeight=$(this).height();if(thisHeight>tallest){tallest=thisHeight}});group.height(tallest)}function get_tax_insurance(){var est_taxes,est_insurance,yearly_taxes,yearly_ins;est_taxes=$("#estimated_taxes").val();est_insurance=$("#estimated_insurance").val();if(est_taxes==""){yearly_taxes=0}else{yearly_taxes=parseFloat(est_taxes)}if(est_insurance==""){yearly_ins=0}else{yearly_ins=parseFloat(est_insurance)}return(yearly_taxes+yearly_ins)/freq}function get_loan(int_rate,pay){var int_rate2,discount;int_rate2=int_rate/(freq*100);discount=(Math.pow(1+int_rate2,periods)-1)/(int_rate2*Math.pow(1+int_rate2,periods));return pay*discount}function format_commas(num){var result='',str=String(num),length=str.length-1,current;for(var x=0;x<=length;x++){result+=current=str.charAt(x);if((x<length)&&((length-x)%3==0)&&(current!='-'))result+=','}return result}function format_number(num){var whole;if(isNaN(num)){return number}whole=Math.floor(num);return"$"+format_commas(whole)}function round_custom(num){if(isNaN(num)){return num}return Math.round(num*100)/100}