$(document).ready(function(){var costSlider,incomeSlider;$(".numeric").numeric({allow:"."});$("#down-payment-borrowed").val($("#down-payment").val());$(".variable").change(function(e){if($.trim(this.value)===""){this.value=0}calculate(this)});costSlider=$("#cost-slider").slider({step:1000,orientation:"horizontal",slide:function(event,ui){$("#cost").val(ui.value);calculate(this)}});incomeSlider=$("#income-slider").slider({step:10,orientation:"horizontal",slide:function(event,ui){$("#income").val(ui.value);calculate(this)}});$("#reset-cost").click(function(e){var min=parseFloat(costSlider.slider("option","min")),value=min/0.5;costSlider.slider("value",value);$("#cost").val(value);calculate(costSlider.get()[0]);return false});$("#reset").button({icons:{primary:"ui-icon-arrowreturnthick-1-w"}});$("#email-button").button({icons:{primary:"ui-icon-mail-closed"}});$("#reset").click(function(e){$("#assessment-form").get()[0].reset();restorePreferences();setSliderRange(costSlider,$("#cost").val(),0.5);setSliderRange(incomeSlider,$("#income").val(),0.5);calculate()});$("#email-button").click(function(e){$("#email-dialog").dialog("open");$(".ui-dialog .ui-button").button("enable")});$("#email-dialog").dialog({autoOpen:false,resizable:false,draggable:false,modal:true,width:450,title:"Email Numbers",dialogClass:"email-dialog",buttons:{Send:function(){$(".ui-dialog .ui-button").button("disable");emailNumbers()},Cancel:function(){$(this).dialog("close")}}});$("#email-form").validate();setupHelp();if(inputParameters.cost){$("#cost").val(inputParameters.cost);$("#closing-costs").val(inputParameters.closingCosts);$("#other-initial-costs").val(inputParameters.otherInitialCosts);$("#purchase-tax").val(inputParameters.purchaseTax);$("#purchase-tax-cost").val(inputParameters.purchaseTaxAmount);$("#property-tax").val(inputParameters.propertyTax);$("#other-costs").val(inputParameters.otherCosts);$("#down-payment").val(inputParameters.downPayment);$("#down-payment-percent").val(inputParameters.downPaymentPercent);$("#down-payment-borrowed").val(inputParameters.downPaymentBorrowed);$("#mortgage-amount").val(inputParameters.mortgageAmount);$("#mortgage-interest-rate").val(inputParameters.mortgageRate);$("#down-payment-interest-rate").val(inputParameters.loanInterestRate);$("#mortgage-amortization").val(inputParameters.amortization);$("#income").val(inputParameters.income);$("#vacancy-percent").val(inputParameters.vacancyPercent);$("#other-income").val(inputParameters.otherIncome)}else{restorePreferences()}setSliderRange(costSlider,$("#cost").val(),0.5);setSliderRange(incomeSlider,$("#income").val(),0.5);calculate()});function restorePreferences(){var prefs=store.get("prefs");if(prefs&&typeof prefs==="object"){$("#down-payment-percent").val(prefs.downPaymentPercent),$("#purchase.tax").val(prefs.purchaseTax),$("#closing-costs").val(prefs.closingCosts),$("#other-costs").val(prefs.otherCosts),$("#down-payment-interest-rate").val(prefs.downPaymentBorrowedPercent),$("#mortgage-interest-rate").val(prefs.mortgagePercent),$("#mortgage-amortization").val(prefs.mortgageAmortization),$("#vacancy-percent").val(prefs.vacancyPercent)}else{storePreferences()}}function storePreferences(){var prefs={downPaymentPercent:$("#down-payment-percent").val(),purchaseTax:$("#purchase.tax").val(),closingCosts:$("#closing-costs").val(),otherCosts:$("#other-costs").val(),downPaymentBorrowedPercent:$("#down-payment-interest-rate").val(),mortgagePercent:$("#mortgage-interest-rate").val(),mortgageAmortization:$("#mortgage-amortization").val(),vacancyPercent:$("#vacancy-percent").val()};store.set("prefs",prefs)}(function($){$.fn.serializeJSON=function(){var json={};jQuery.map($(this).serializeArray(),function(n,i){json[n.name]=n.value});return json}})(jQuery);function emailNumbers(){$("#email-form").validate().form();var data=$("#assessment-form").serializeJSON(),buttonPanel;data.email=$("#email").val();data.subject=$("#subject").val();data.message=$("#message").val();data["monthly-cashflow"]=$("#monthly-income").text();data["annual-roi"]=$("#yearly-roi").text();data["cap-rate"]=$("#cap-rate").text();buttonPanel=$(".email-dialog .ui-dialog-buttonpane .ui-dialog-buttonset");buttonPanel.after('<div id="spinner"><img src="images/spinner.gif"/></div>');$.ajax(emailUri,{contentType:"application/json",dataType:"json",type:"POST",data:JSON.stringify(data),success:function(){alert("Email sent!");$("#email-dialog").dialog("close");$("#spinner").remove()},error:function(){alert("Email send failure!");$(".ui-dialog .ui-button").button("enable");$("#spinner").remove()}})}function calculate(){var mortgagePayment,loanPayment,downPayment,mortgageAmount,downPaymentPercent,cost,mortgageInterestRate,loanInterestRate,income,totalCosts,netIncome,roi,purchaseTax,propertyTaxes,totalInitialCosts,totalIncome,vacancyPercent,downPaymentBorrowed,annualIgnore,closingCosts,adjustedCost,otherInitialCosts,capRate;cost=parseFloat($("#cost").val());downPaymentPercent=parseFloat($("#down-payment-percent").val());mortgageInterestRate=parseFloat($("#mortgage-interest-rate").val());loanInterestRate=parseFloat($("#down-payment-interest-rate").val());downPaymentBorrowed=parseFloat($("#down-payment-borrowed").val());downPayment=parseFloat($("#down-payment").val());closingCosts=parseFloat($("#closing-costs").val());otherInitialCosts=parseFloat($("#other-initial-costs").val());income=parseFloat($("#income").val());purchaseTax=calculatePurchaseTax($("#purchase-tax").val(),$("#purchase-tax-cost").val(),cost);adjustedCost=cost+closingCosts+otherInitialCosts+purchaseTax;if(arguments.length>0){var el=arguments[0],jqEl;switch(el.id){case"annual-income":income=dollarRound(parseFloat(el.value)/12);case"income":setSliderRange($("#income-slider").slider("widget"),income,0.5);break;case"down-payment":downPaymentPercent=(downPayment/adjustedCost)*100;downPaymentBorrowed=downPayment;break;case"cost":setSliderRange($("#cost-slider").slider("widget"),cost,0.5);case"cost-slider":case"purchase-tax":case"purchase-tax-cost":case"closing-costs":case"other-initial-costs":case"down-payment-percent":downPayment=adjustedCost*(downPaymentPercent/100);downPaymentBorrowed=downPayment;break;default:downPayment=adjustedCost*(downPaymentPercent/100)}jqEl=$(el);if(jqEl.hasClass("annual")){var monthlyValue=dollarRound(parseFloat(el.value)/12);$("#"+el.id.slice(7)).val(monthlyValue);annualIgnore=el}}else{downPayment=adjustedCost*(downPaymentPercent/100)}mortgageAmount=adjustedCost-downPayment;mortgagePayment=calculateAmortizationPayment(mortgageAmount,$("#mortgage-amortization").val(),mortgageInterestRate/100,12);loanPayment=calculateInterestOnlyPayment(downPaymentBorrowed,loanInterestRate/100);propertyTaxes=parseFloat($("#property-tax").val());vacancyPercent=parseFloat($("#vacancy-percent").val())/100;totalIncome=income-(income*vacancyPercent)+parseFloat($("#other-income").val());totalCosts=mortgagePayment+loanPayment+propertyTaxes+parseFloat($("#other-costs").val());netIncome=totalIncome-totalCosts;roi=((netIncome*12)/downPayment)*100;if(isNaN(roi)){roi=0}capRate=((netIncome*12)/cost)*100;if(isNaN(capRate)){capRate=0}totalInitialCosts=downPayment+purchaseTax+closingCosts+otherInitialCosts;$("#down-payment").val(dollarRound(downPayment));$("#down-payment-percent").val(dollarRound(downPaymentPercent));$("#down-payment-borrowed").val(dollarRound(downPaymentBorrowed));$("#mortgage-amount").val(dollarRound(mortgageAmount));$("#mortgage-cost").val(mortgagePayment);$("#down-payment-interest-cost").val(loanPayment);$("#purchase-tax-cost").val(purchaseTax);$("#total-income").text(dollarRound(totalIncome).formatMoney(2,".",","));$("#total-annual-income").text(dollarRound(totalIncome*12).formatMoney(2,".",","));$("#total-initial-costs").text(dollarRound(totalInitialCosts).formatMoney(2,".",","));$("#total-ongoing-costs").text(dollarRound(totalCosts).formatMoney(2,".",","));$("#total-annual-ongoing-costs").text(dollarRound(totalCosts*12).formatMoney(2,".",","));$("#monthly-income").text(dollarRound(netIncome).formatMoney(2,".",","));if(netIncome<0){$("#monthly-income-value").addClass("in-the-red")}else{$("#monthly-income-value").removeClass("in-the-red")}$("#yearly-roi").text(dollarRound(roi));if(roi<0){$("#yearly-roi-value").addClass("in-the-red")}else{$("#yearly-roi-value").removeClass("in-the-red")}$("#cap-rate").text(dollarRound(capRate));if(capRate<0){$("#cap-rate-value").addClass("in-the-red")}else{$("#cap-rate-value").removeClass("in-the-red")}$(".monthly").each(function(i,e){var annualValue=dollarRound(parseFloat(e.value)*12),annualJqEl=$("#annual-"+e.id);if(annualJqEl.get(0)!==annualIgnore){annualJqEl.val(annualValue)}});storePreferences()}function setSliderRange(slider,value,percentRange){value=parseFloat(value);percentRange=parseFloat(percentRange);slider.slider("option","min",value-(value*percentRange));slider.slider("option","max",value+(value*percentRange));slider.slider("value",value)}function calculatePurchaseTax(taxType,taxEntered,purchaseCost){switch(taxType){case"bc-ptt":return calculateBCPropertyTransferTax(purchaseCost);case"bc-hst":return calculateBCHST(purchaseCost);case"other":return parseFloat(taxEntered);default:return 0}}function calculateAmortizationPayment(principal,amortization,interest,compounded){principal=parseFloat(principal);amortization=parseFloat(amortization);interest=parseFloat(interest);compounded=parseFloat(compounded);var effectiveRate=interest;if(compounded!==12){effectiveRate=Math.pow(interest/compounded+1,compounded)-1}var monthsInYear=12;var exp=amortization*monthsInYear;var monthlyInterest=effectiveRate/monthsInYear;var exponentCalc=Math.pow(monthlyInterest+1,exp);return dollarRound(principal*monthlyInterest*exponentCalc/(exponentCalc-1))}function calculateInterestOnlyPayment(principal,interest){principal=parseFloat(principal);interest=parseFloat(interest);return dollarRound(principal*interest/12)}function dollarRound(val){if(isNaN(val)||val===Infinity||val===-Infinity||typeof val==="undefined"||val===null){return 0}return Math.round(val*100)/100}Number.prototype.formatMoney=function(c,d,t){var n=this,c=isNaN(c=Math.abs(c))?2:c,d=d==undefined?",":d,t=t==undefined?".":t,s=n<0?"-":"",i=parseInt(n=Math.abs(+n||0).toFixed(c))+"",j=(j=i.length)>3?j%3:0;return s+(j?i.substr(0,j)+t:"")+i.substr(j).replace(/(\d{3})(?=\d)/g,"$1"+t)+(c?d+Math.abs(n-i).toFixed(c).slice(2):"")};function calculateBCPropertyTransferTax(purchaseCost){purchaseCost=parseFloat(purchaseCost);if(purchaseCost<=200000){return purchaseCost*0.01}return dollarRound((200000*0.01)+((purchaseCost-200000)*0.02))}function calculateBCHST(purchaseCost){var rebate;purchaseCost=parseFloat(purchaseCost);rebate=(0.07*purchaseCost)*0.7143;return dollarRound((purchaseCost*0.12)-(rebate>26250?26250:rebate))}function setupHelp(){var helpConfig=[{selector:"#reset-cost",helpHtml:"Use the Purchase Cost Reset button to reset the Purchase Cost to the price you entered before you adjusted the slider."},{selector:"#down-payment-help",helpHtml:"Enter the $ amount OR the % amount of the down payment.  The other will be calculated for you."},{selector:"#purchase-tax-help",helpHtml:'Select the purchase tax associated with your purchase, or "Other", to enter a different Purchase Tax amount.  If there is a purchase tax you\'d like us to add to this list, just ask!'},{selector:"#closing-costs-help",helpHtml:"Closing costs can be around 1-4% of your purchase price.  This includes things like notary, building inspection, appraisal fees, etc."},{selector:"#other-initial-costs-help",helpHtml:"Enter any other cost, for example: renovations, new appliances, etc."},{selector:"#down-payment-borrowed-help",helpHtml:"This is how much of the down payment will need to be borrowed at the particular interest rate. It is automatically calculated based on the down payment percentage, but you can enter a value if the entire down payment will not be borrowed."},{selector:"#mortgage-amount-help",helpHtml:"This is the calculated amount that will be required for a mortgage at the given interest rate.  It is the purchase cost less the down payment and also adds purchase tax, closing costs, and other initial costs."},{selector:"#down-payment-interest-cost-help",helpHtml:'The down payment interest is calculated based on the "Down Payment Borrowed", and the loan % (Under "Financing"). The interest payment shown is interest only.'},{selector:"#mortgage-cost-help",helpHtml:'The mortgage payment interest is calculated based on "Mortgage Amount $", "Mortgage Amount %", and Mortgage Amortization (under "Financing").'},{selector:"#property-tax-help",helpHtml:"Enter the expected property tax to be paid."},{selector:"#other-costs-help",helpHtml:"Enter any other recurring cost here, such as utilities, yard maintenance, house maintenance, etc."},{selector:"#income-help",helpHtml:"Enter the predicted total rental income the property will produce."},{selector:"#other-income-help",helpHtml:"Enter any other income you expect from the property such as laundry use, parking, etc."},{selector:"#vacancy-help",helpHtml:"Enter an expected percentage of vacancy in the property in a given year."}];for(var i in helpConfig){$(helpConfig[i].selector).CreateBubblePopup({innerHtml:helpConfig[i].helpHtml,themeName:"all-grey",themePath:"css/jquerybubblepopup-theme",width:300})}};
