import {estimateHomeCosts} from './homebuyer-cost.mjs'; const fields = [ ['price','Home price ($)',350000],['down','Down payment ($)',35000], ['rate','Annual note rate (%)',6],['years','Fixed term (years)',30], ['closing','Closing costs excluding prepaids ($)',7000], ['prepaids','Prepaids and initial escrow ($)',3000], ['credits','Applicable confirmed credits ($)',3000], ['deposit','Deposit already credited ($)',5000], ['tax','Annual property tax ($)',3600],['insurance','Annual homeowners insurance ($)',1500], ['pmi','Monthly mortgage insurance ($)',90],['hoa','Monthly HOA dues ($)',0], ['maintenance','Annual maintenance reserve ($)',3500], ['overlap','Prepaid taxes or insurance also counted in year one ($)',1200], ]; const form = document.querySelector('[data-homebuyer-form]'); const results = document.querySelector('[data-homebuyer-result]'); const status = document.querySelector('[data-homebuyer-status]'); const money = new Intl.NumberFormat('en-US',{style:'currency',currency:'USD'}); const compare = form.querySelector('[name="compare"]'); const lists = [...form.querySelectorAll('[data-scenario]')]; const print = document.querySelector('[data-homebuyer-print]'); for (const [index,list] of lists.entries()) { for (const [name,caption,initial] of fields) { const label = document.createElement('label'); const input = document.createElement('input'); label.append(caption); input.name = name + index; input.type = 'number'; input.required = true; input.min = '0'; input.max = String(name === 'rate' ? 100 : name === 'years' ? 50 : 1000000000); input.step = name === 'years' ? '1' : '.01'; input.value = String(index === 1 && name === 'price' ? 375000 : initial); label.append(input); list.append(label); } } function updateComparison() { lists[1].disabled = lists[1].hidden = !compare.checked; } updateComparison(); compare.addEventListener('change',updateComparison); function hideResults() { results.hidden = true; print.hidden = true; status.textContent = ''; } form.addEventListener('input',hideResults); form.addEventListener('submit',event=>{ event.preventDefault(); hideResults(); const data = new FormData(form); try { const modeled = (compare.checked ? [0,1] : [0]).map(index=>{ try { return estimateHomeCosts(Object.fromEntries(fields.map(([name])=>[name,Number(data.get(name+index))]))); } catch (error) { throw new Error('Home '+(index ? 'B' : 'A')+': '+error.message); } }); const table = document.createElement('table'); const caption = table.createCaption(); caption.textContent = 'Estimated homebuying costs in US dollars'; const header = table.createTHead().insertRow(); for(const label of ['Estimate',...modeled.map((_,i)=>i?'Home B':'Home A')]){ const cell=document.createElement('th'); cell.scope='col';cell.textContent=label;header.append(cell); } const body = table.createTBody(); for (const [key,label] of Object.entries({ cashToClose:'Cash still due at closing',totalUpfront:'Total upfront cash including deposit', principalInterest:'Monthly principal and interest',monthlyHousing:'Estimated monthly housing budget', firstYear:'Estimated cash budget through the first year', })) { const row = body.insertRow(); const title = document.createElement('th'); title.scope = 'row';title.textContent = label;row.append(title); for(const item of modeled)row.insertCell().textContent=money.format(item[key]); } results.replaceChildren(table); results.hidden = print.hidden = false; status.textContent = modeled.length === 2 ? 'Comparison ready for both homes.' : 'Estimate ready for Home A.'; window.gtag?.('event',modeled.length === 2?'homebuyer_comparison_complete':'homebuyer_estimate_complete',{tool:'homebuyer-cost-comparison',surface:window.self===window.top?'site':'embed'}); } catch(error) { status.textContent = error.message; } }); print.addEventListener('click',()=>window.print()); const attribution = document.querySelector('[data-homebuyer-attribution]'); if (attribution && new URLSearchParams(location.search).get('attribution') === 'off') attribution.hidden = true; const partner = new URLSearchParams(location.search).get('partner'); if (attribution) { const destination = new URL(attribution.closest('a').href); destination.searchParams.set('utm_source',partner && /^[a-z0-9-]{1,60}$/.test(partner) ? partner : 'homebuyer_widget'); destination.searchParams.set('utm_medium','referral'); destination.searchParams.set('utm_campaign','homebuying_costs'); attribution.closest('a').href = destination.href; }