PageSourceSearch

https://www.martinco.com/wp-content/plugins/tpj-wp-plugin/assets/frontend/js/tpj_mortgage.js?ver=v1.0.7

js martinco.com collected 2026-09-26 04:04:55 UTC 7,993 bytes, 203 lines download raw bytes

1"use strict";
2(function() {
3    let is_calculating = false;
4    const moneyFormat = wNumb({
5        mark: '.',
6        thousand: ',',
7        decimals: 2
8    });
9    const moneyFormatNoDecimals = wNumb({
10        thousand: ',',
11        decimals: 0
12    });
13
14    const required_ids = ['mortgage_calculator', 'loan_amount', 'length_of_loan_in_years', 
15    'interest_rate', 'mortgage_calculator_success', 'mortgage_calculator_success_value', 'mortgage_calculator_error'];
16
17    function can_run_mortgage() {
18        let can = true;
19        for (let i = 0; i < required_ids.length; i++) {
20            const id = required_ids[i];
21            if (!jQuery(`#${id}`)[0]) {
22                can = false;
23            }
24        }
25        return can;
26    }
27
28    function tpjIsMobile() {
29        let out = false;
30        if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
31            out = true;
32        }
33        return out;
34    }
35
36    function calculateMortgagePayment(borrowedMoney, lengthOfLoanInYears, interestRate) {
37        
38        jQuery('#mortgage_calculator_error').hide();
39        jQuery('#mortgage_calculator_success').hide();
40        const is_borrowedMoneyError = Number.isNaN(borrowedMoney);
41        const is_lengthOfLoanInYears = Number.isNaN(lengthOfLoanInYears);
42        const is_interestRate = Number.isNaN(interestRate);
43
44        const hasErrors = is_borrowedMoneyError || is_lengthOfLoanInYears || is_interestRate;
45        if (hasErrors) {
46            jQuery('#mortgage_calculator_error').show();
47            return;
48        }
49
50        const amountInput = jQuery('#amountInput');
51        const lengthOfLoanInput = jQuery('#lengthOfLoanInput');
52        const interestRateInput = jQuery('#interestRateInput');
53
54        // We take initial values
55        let lengthOfLoan = lengthOfLoanInYears * 12;
56        const calculedInterest = interestRate / 100;
57        const interestReady = calculedInterest / 12;
58        // We start the calculations
59        const percentage = interestReady;
60        const percentagePlusOne = interestReady + 1;
61        const exponentiationOperator = (percentagePlusOne ** lengthOfLoan);
62        const firstDividend = percentage * exponentiationOperator;
63        const secondDividend = exponentiationOperator - 1;
64        const division = firstDividend / secondDividend;
65        const mortgage = borrowedMoney;
66        const quotas = mortgage * division;
67
68        jQuery('#mortgage_calculator_success').show();
69        jQuery('#mortgage_calculator_success_value').html(moneyFormat.to(quotas));
70    }
71
72    function init_mortgage_calc() {
73        jQuery('#loan_amount').on('keyup', function(keyEvent) {
74            if (is_calculating) { return; }
75            jQuery('#mortgage_calculator_success').hide();
76            jQuery('#mortgage_calculator_error').hide();
77            if (use_inline_currency_format) {
78                if (!tpjIsMobile()) {
79                    formatCurrency(jQuery(this), keyEvent);
80                }
81            }
82        });
83
84        jQuery('#loan_amount').on('blur', function(keyEvent) {
85            if (use_inline_currency_format) {
86                if (!tpjIsMobile()) {
87                    formatCurrency(jQuery(this), keyEvent, "blur");
88                }
89            }
90        });
91
92        jQuery('#length_of_loan_in_years').on('keyup', function() {
93            if (is_calculating) { return; }
94            jQuery('#mortgage_calculator_success').hide();
95            jQuery('#mortgage_calculator_error').hide();
96        });
97
98        jQuery('#interest_rate').on('keyup', function() {
99            if (is_calculating) { return; }
100            jQuery('#mortgage_calculator_success').hide();
101            jQuery('#mortgage_calculator_error').hide();
102        });
103
104        jQuery('#mortgage_calculator').on('submit', function(event) {
105            event.preventDefault();
106            is_calculating = true;
107            runCalc();
108            setTimeout(() => {
109                is_calculating = false;
110            }, 500);
111        })
112        function formatNumber(n) {
113            // format number 1000000 to 1,234,567
114            return n.replace(/\D/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ",")
115        }
116        function formatCurrency(input, keyEvent, blur) {
117            if (keyEvent) {
118                const code = keyEvent.keyCode || keyEvent.which;
119                if (code === 16 || code === 91) {
120                    return;
121                }
122            }
123            let input_val = input.val();
124            
125            if (input_val === "") { return; }
126            
127            let original_len = input_val.length;
128          
129            let caret_pos = input.prop("selectionStart");
130            if (input_val.indexOf(".") >= 0) {
131              let decimal_pos = input_val.indexOf(".");
132              let left_side = input_val.substring(0, decimal_pos);
133              let right_side = input_val.substring(decimal_pos);
134              left_side = formatNumber(left_side);
135              right_side = formatNumber(right_side);
136              
137              if (blur === "blur") {
138                right_side += "00";
139              }
140
141              right_side = right_side.substring(0, 2);
142              if (use_currency_symbol) {
143                input_val = currency_symbol + left_side + "." + right_side;  
144              } else {
145                input_val = left_side + "." + right_side;
146              }
147          
148            } else {
149              input_val = formatNumber(input_val);
150              if (use_currency_symbol) {
151                input_val = currency_symbol + input_val; 
152              }
153            }
154            
155            input.val(input_val);
156            let updated_len = input_val.length;
157            caret_pos = updated_len - original_len + caret_pos;
158            input[0].setSelectionRange(caret_pos, caret_pos);
159        }
160
161        function runCalc() {
162            let loan_amount = jQuery('#loan_amount').val();
163            if (use_inline_currency_format) {
164                if (loan_amount !== '') {
165                    const decimal_position = loan_amount.indexOf('.');
166                    if (decimal_position !== -1) {
167                        loan_amount = loan_amount.substring(0, decimal_position);
168                    }
169                    loan_amount = loan_amount.replaceAll(',', '');
170                }
171            }
172            if (use_currency_symbol) {
173                loan_amount = loan_amount.replaceAll(currency_symbol, '');
174            }
175            loan_amount = parseFloat(loan_amount);
176            const length_of_loan_in_years = parseFloat(jQuery('#length_of_loan_in_years').val());
177            const interest_rate = parseFloat(jQuery('#interest_rate').val());
178            calculateMortgagePayment(loan_amount, length_of_loan_in_years, interest_rate);
179        }
180
181        const autocalculate_on_init = jQuery('#mortgage_calculator').attr('data-autocalculate-if-preffield') === 'true';
182        const use_inline_currency_format = jQuery('#mortgage_calculator').attr('data-use-inline-currency-format') === 'true';
183        const currency_symbol = jQuery('#mortgage_calculator').attr('data-use-currency-symbol');
184		const use_inline_currency_format_on_load = jQuery('#mortgage_calculator').attr('data-use_inline_currency_format_on_load') === 'true';
185        const use_currency_symbol = use_inline_currency_format !== 'false';
186        const has_loan_amount = jQuery('#loan_amount').val() !== '';
187        const has_length_of_loan_in_years = jQuery('#length_of_loan_in_years').val() !== '';
188        const has_interest_rate = jQuery('#interest_rate').val() !== '';
189
190        if (autocalculate_on_init && has_loan_amount && has_length_of_loan_in_years && has_interest_rate) {
191			if (use_inline_currency_format_on_load && !tpjIsMobile()) {
192				formatCurrency(jQuery('#loan_amount'), null);
193			}
194            runCalc();
195        }
196    }
197
198    jQuery(document).ready(function() {
199        if (can_run_mortgage()) {
200            init_mortgage_calc();
201        }
202    });
203})();

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.