var CALCIMG_URLPREFIX=MEDIA_URL+"/res/img/calculators/";function updateOptionsDisplay(c,b){if(c=="simple"){var a=["interest","rates","years"];var d=$("calcTableBody")}else{if(c=="debtrepayment"){var a=["balance","debtRates","payments"];var d=$("Calc2")}else{throw new Exception("Unknown calculator type")}}a.each(function(f){var g=$(f+"Header");var e=$(f+"Btn");if(b==f){g.addClassName("selected");e.src=CALCIMG_URLPREFIX+f+"Inactive.png";e.style.cursor="default"}else{g.removeClassName("selected");e.src=CALCIMG_URLPREFIX+f+"Active.png";e.style.cursor="pointer"}});if(d.style.display=="none"){d.style.display="block";d.style.paddingBottom="20px"}}function LoanValues(){this.loan=getNumber("loanVal");this.yearlyRate=getNumber("rateVal");this.year=getNumber("monthVal");this.pay=calculateMoPayment(this.loan,this.yearlyRate,this.year)}LoanValues.prototype.invalid=function(){return !this.loan||!this.yearlyRate||!this.year};function calculateMoPayment(c,b,d){var a=d*12;var e=b/1200;return c*(e/(1-(Math.pow((1+e),-a))))}function updateMoPay(c,f,b,a,d){var e=calculateMoPayment(b,a,d);$("colMoPay"+f+c).update(formatCurrency(e))}function updateLoanResults(l){var k=new LoanValues();if(k.invalid()){alert("Please provide non-zero values for loan amount, interest rate, and number of years");return}$("colAmountBox").update(formatCurrency(k.loan));$("colInterestBox").update(k.yearlyRate+"%");$("colYearsBox").update(k.year);var n=$("payVal");n.update(formatCurrency(k.pay));if(n){n.style.fontWeight="700";n.style.color="#000";n.style.fontSize="20px";n.style.padding="0";n.style.padding=".52em 0"}if(l=="interest"){var h=function(o){$("colAmountMinus"+o).update(formatCurrency(k.loan-k.loan*(o/100)));$("colAmountMinus"+o).style.backgroundColor="#f1ffed"};var d=function(o){$("colIntMinus"+o).update(k.yearlyRate+"%");$("colIntMinus"+o).style.backgroundColor="#fff"};var g=function(o){$("colYearMinus"+o).update(k.year);$("colYearMinus"+o).style.backgroundColor="#fff"};var b=function(o){updateMoPay(o,"Minus",k.loan-k.loan*(o/100),k.yearlyRate,k.year)};var c=function(o){$("colAmountPlus"+o).update(formatCurrency(k.loan+k.loan*(o/100)));$("colAmountPlus"+o).style.backgroundColor="#f1ffed"};var a=function(o){$("colIntPlus"+o).update(k.yearlyRate+"%");$("colIntPlus"+o).style.backgroundColor="#fff"};var e=function(o){$("colYearPlus"+o).update(k.year);$("colYearPlus"+o).style.backgroundColor="#fff"};var j=function(o){updateMoPay(o,"Plus",k.loan+k.loan*(o/100),k.yearlyRate,k.year)}}else{if(l=="rates"){var m=function(p,o){if(o){var r=1}else{var r=-1}var q=k.yearlyRate+(r*k.yearlyRate*(p/100));return q.toFixed(2)};var h=function(o){$("colAmountMinus"+o).update(formatCurrency(k.loan));$("colAmountMinus"+o).style.backgroundColor="#fff"};var d=function(o){$("colIntMinus"+o).update(m(o,false)+"%");$("colIntMinus"+o).style.backgroundColor="#f1ffed"};var g=function(o){$("colYearMinus"+o).update(k.year);$("colYearMinus"+o).style.backgroundColor="#fff"};var b=function(o){updateMoPay(o,"Minus",k.loan,m(o,false),k.year)};var c=function(o){$("colAmountPlus"+o).update(formatCurrency(k.loan));$("colAmountPlus"+o).style.backgroundColor="#fff"};var a=function(o){$("colIntPlus"+o).update(m(o,true)+"%");$("colIntPlus"+o).style.backgroundColor="#f1ffed"};var e=function(o){$("colYearPlus"+o).update(k.year);$("colYearPlus"+o).style.backgroundColor="#fff"};var j=function(o){updateMoPay(o,"Plus",k.loan,m(o,true),k.year)}}else{if(l=="years"){var h=function(o){$("colAmountMinus"+o).update(formatCurrency(k.loan));$("colAmountMinus"+o).style.backgroundColor="#fff"};var d=function(o){$("colIntMinus"+o).update(k.yearlyRate.toFixed(2)+"%");$("colIntMinus"+o).style.backgroundColor="#fff"};var g=function(o){$("colYearMinus"+o).update(k.year-k.year*(o/100));$("colYearMinus"+o).style.backgroundColor="#f1ffed"};var b=function(o){updateMoPay(o,"Minus",k.loan,k.yearlyRate,k.year-k.year*(o/100))};var c=function(o){$("colAmountPlus"+o).update(formatCurrency(k.loan));$("colAmountPlus"+o).style.backgroundColor="#fff"};var a=function(o){$("colIntPlus"+o).update(k.yearlyRate.toFixed(2)+"%");$("colIntPlus"+o).style.backgroundColor="#fff"};var e=function(o){$("colYearPlus"+o).update(k.year+k.year*(o/100));$("colYearPlus"+o).style.backgroundColor="#f1ffed"};var j=function(o){updateMoPay(o,"Plus",k.loan,k.yearlyRate,k.year+k.year*(o/100))}}}}for(var f=15;f>=5;f-=5){h(f);d(f);g(f);b(f);c(f);a(f);e(f);j(f)}updateOptionsDisplay("simple",l)}function updateAffordability(){var g=getNumber("income");var c=getNumber("debtpmts");var f=getNumber("downpmt");var e=getNumber("intrrate");var d=new LoanStats(g,c,f,e);if(!d.isReady()){alert("Please provide values for monthly income, down payment, debt payments, and interest rate.");return}if(d.totalMonthlyPayment<=0||d.loanAmt<=0){alert("Your monthly debt to income ratio is too high. Please reduce your debt payments and try again.");return}$("calcResults").style.display="none";$("Calc3").style.display="block";var a=formatCurrency(d.totalMonthlyPayment);$("pmi").update(formatCurrency(d.pmi));$("monthlyTotal").update(a);$("monthlyPrinc").update(formatCurrency(d.monthlyPrincAndIntr));$("loanAmount").update(formatCurrency(d.loanAmt));$("homeowners").update(formatCurrency(LoanStats.insurance));$("proptax").update(formatCurrency(LoanStats.propertyTaxes));$("homePurchasePrice").update(formatCurrency(d.maxAllowableHouseValue));$("downPaymentPrcnt").update(d.downPmtPercentage+"%");var b=$("monthlyPayments");b.update(a);if(b){b.style.fontWeight="700";b.style.color="#000";b.style.fontSize="20px";b.style.padding="0";b.style.padding=".52em 0"}}function LoanStats(d,a,c,b){this.mortgageInterestRate=b/(12*100);this.monthlyIncome=d;this.debtPmts=a;this.downPmt=c;this.totalMonthlyPayment=this.monthlyIncome*0.39-this.debtPmts;if(this.downPmt==false){this.downPmt=0}if(this.debtPmts==false){this.debtPmts=0}this.pmi=0;this.calcLoanAmtStats();if(this.downPmtPercentage<20){this.pmi=this.loanAmt/100000*75;this.calcLoanAmtStats()}}LoanStats.prototype.calcLoanAmtStats=function(){this.monthlyPrincAndIntr=this.totalMonthlyPayment-this.pmi-LoanStats.insurance-LoanStats.propertyTaxes;this.loanAmt=(this.monthlyPrincAndIntr)*(1-Math.pow(1+this.mortgageInterestRate,-LoanStats.duration))/this.mortgageInterestRate;this.maxAllowableHouseValue=this.loanAmt+this.downPmt;var a=100*this.downPmt/this.maxAllowableHouseValue;this.downPmtPercentage=a.round()};LoanStats.prototype.isReady=function(){if(!this.monthlyIncome||this.monthlyIncome<800||!this.mortgageInterestRate||isNaN(this.loanAmt)){return false}return true};LoanStats.propertyTaxes=292;LoanStats.insurance=41;LoanStats.duration=360;function DebtValues(){this.balance=getNumber("balanceVal");this.yearlyRate=getNumber("debtRateVal");this.fixedPmt=getNumber("debtMonthVal");this.payoffMonths=getNumber("payoffTime")}DebtValues.prototype.invalid=function(){return !this.balance||!this.yearlyRate||(!this.fixedPmt&&!this.payoffMonths)};DebtValues.prototype.updateRow=function(g,n,m,c,o){m=m||this.balance;c=c||this.yearlyRate;o=o||this.fixedPmt;m=Math.round(m);c=Math.round(c*100)/100;o=Math.round(o);var k=calculatePayoffTime(m,c,o);if(isNaN(k)){k="Payment is too low"}else{k+=" months"}var d=$("colBalance"+g);d.update(formatCurrency(m));var b=$("colInt"+g);b.update(c+"%");var h=$("colPayments"+g);h.update(formatCurrency(o));var e=$("colMoToPay"+g);e.update(k);var a;switch(n){case"balance":a=d;var l=[b,h];break;case"int":a=b;var l=[d,h];break;default:a=h;var l=[d,b];break}var f;for(f=0;f<l.length;f++){l[f].style.backgroundColor="#fff"}a.style.backgroundColor="#f1ffed"};function calculatePayoffTime(d,a,c){var b=a/1200;return Math.round(Math.log(1-(d/c)*b)/Math.log(1+b))*-1}function calculateMinimumPayment(f,c,b,a){var e=c/1200;var d=f*e/(1-(Math.pow((1+e),-b)));if(!a){return Math.round(d)}else{return d}}var gHaveRunDebtRepayment=false;var gDataDebtRepayment="";function updateDebtResults(j){var h=new DebtValues();if(h.invalid()){alert("Please provide non-zero values for balance amount, interest rate, and amount of monthly payments");return}if(!h.fixedPmt){h.fixedPmt=calculateMinimumPayment(h.balance,h.yearlyRate,h.payoffMonths)}var f=calculatePayoffTime(h.balance,h.yearlyRate,h.fixedPmt);if(isNaN(f)||(f==Infinity)){var a=calculateMinimumPayment(h.balance,h.yearlyRate,36);alert("Expected monthly payment is too low. For example, the minimum payment on a 36 month plan would be $"+a);return}var e,k,m=h.balance,l=h.yearlyRate/1200;var g,b=0,p=0;for(e=1;e<=f;e++){k=m*l;g=h.fixedPmt-k;m=m-g;b+=k;p+=g}var d=Math.round(p);var o=Math.round(b);gDataDebtRepayment="Principal;"+d+"\nInterest;"+o;if(gHaveRunDebtRepayment){amChartInited("debtrepayment")}else{gHaveRunDebtRepayment=true}$("chartprincipal").update(formatCurrency(d));$("chartinterest").update(formatCurrency(o));$("CalcGraphs").style.display="block";$("CalcGraphsInactive").style.display="none";$("balance").update(formatCurrency(h.balance));$("interestRate").update(h.yearlyRate+"%");$("expectedMonthlyPayment").update(formatCurrency(h.fixedPmt));var c=$("payoffTimeSummary");c.update(f+" months");c.style.fontSize="21px";c.style.fontWeight="bold";$("payoffOptions").update("Click on a column to see results for similar loan amounts, interest rates, and durations");if(j=="balance"){var n=function(q){h.updateRow(q,"balance",h.balance+(h.balance*q/100),null,null)}}else{if(j=="debtRates"){var n=function(q){h.updateRow(q,"int",null,h.yearlyRate+(h.yearlyRate*q/100),null)}}else{var n=function(q){h.updateRow(q,"fixedpmt",null,null,h.fixedPmt+(h.fixedPmt*q/100))}}}for(var e=30;e>=-30;e-=10){if(e){n(e)}}updateOptionsDisplay("debtrepayment",j)}function debtRepaymentInputChg(a){if(a=="debtMonthVal"){var b="payoffTime"}else{var b="debtMonthVal"}if(getNumber(a)){$(a+"Label").removeClassName("payoffInactive");$(b+"Label").addClassName("payoffInactive");$(b).value=""}else{$(a+"Label").addClassName("payoffInactive");if(getNumber(b)){$(b+"Label").removeClassName("payoffInactive")}}}var gHaveRunAmortization=false;var gDataAmortization="";var gDataIntrPrin="";var gDataIntrPrinMaxPmt="";function amortization(){var c=getNumber("loanVal");var b=getNumber("rateVal");var d=getNumber("monthVal");var a=d*12;$("loanAmortizationChart").style.display="block";$("principalPaymentChart").style.display="block";$("scrollableDiv").style.display="block";$("loanAmortizationChartStatic").style.display="none";$("principalPaymentChartStatic").style.display="none";$("amortizationNote").style.display="none";if(!c||!b||!d){alert("Please provide non-zero values for Loan Amount, Interest Rate, and Number of Years");return}if(b>=100){alert("Please provide an interest rate that is less than 100%.");return}calcAmorMoPmt(c,b,a);$("Calc2").style.display="block";if(gHaveRunAmortization){amChartInited("flashBalChart");amChartInited("prinAndInterest")}else{gHaveRunAmortization=true}}function calcAmorMoPmt(r,d,m){var e=formatCurrency(r);var l=d/1200;var h=calculateMinimumPayment(r,d,m,true);var j=formatCurrency(Math.round(h));var p=1;var f=0;var b=0;var a;var o=new Element("tbody");var s=new Date();gDataAmortization="";gDataIntrPrin="";gDataIntrPrinMaxPmt=Math.round(h);var c=0;var q=0;var k=0;for(p=1;p<=m;p++){s.setMonth(s.getMonth()+1);f=r*l;b=h-f;r=r-b;a=new Element("tr");addElementWithText(a,p+" - "+translateMonth(s.getMonth())+" "+s.getFullYear(),new Element("td",{width:"150",style:"text-align:left;padding-left:5px;font-weight:700"}));addElementWithText(a,j,new Element("td",{width:"180"}));addElementWithText(a,formatCurrency(Math.round(f)),new Element("td",{width:"140"}));addElementWithText(a,formatCurrency(Math.round(b)),new Element("td",{width:"160"}));addElementWithText(a,"",new Element("td",{width:"7",style:"border-top:none;border-bottom:none;background-color:#fff;"}));addElementWithText(a,formatCurrency(Math.round(r)),new Element("td",{width:"170"}));o.appendChild(a);c+=f;q+=b;if(p%12==0){k=p/12;gDataAmortization+=k+";"+Math.round(c)+";"+Math.round(q)+";"+Math.round(r)+"\n";gDataIntrPrin+=k+";"+Math.round(f)+"\n"}}$("colAmountBox").update(j);$("colInterestBox").update(m);$("colYearsBox").update(formatCurrency(Math.round(m*h)));var n=$("payVal");n.update(e);n.style.fontSize="20px";n.style.fontWeight="bold";var g=$("fullTableBody");if(g){g.parentNode.removeChild(g)}$("fullTable").appendChild(o);o.id="fullTableBody"}function amReturnData(a,b){alert("chart id: "+a+", data: "+b)}function amChartInited(b){var a=$(b);if(a&&a.setData){if(b=="flashBalChart"&&gDataAmortization&&gDataAmortization.length){a.setData(gDataAmortization)}else{if(b=="prinAndInterest"&&gDataIntrPrin&&gDataIntrPrin.length){a.setData(gDataIntrPrin);a.setParam("guides.guide[0].end_value",gDataIntrPrinMaxPmt);a.setParam("values.y_left.max",gDataIntrPrinMaxPmt);a.setParam("values.y_left.strict_min_max",true)}else{if(b=="debtrepayment"&&gDataDebtRepayment&&gDataDebtRepayment.length){a.setData(gDataDebtRepayment)}}}}};