{"version":3,"sources":["_main.js"],"names":["loansCalculator","monthlyBudget","document","getElementById","borrowAmountInput","aprHelpLink","querySelector","aprHelpContent","aprButtons","querySelectorAll","exactAPRContent","exactAPRInput","customAprValue","isCustomApr","calculateLoanDetails","type","totalAmount","monthlyPayment","numberOfYears","apr","monthlyInterestRate","numberOfPayments","totalRepayment","totalInterestPaid","Math","pow","monthlyPaymentAmountLocation","numberOfMonthsLocation","totalBorrowAmountLocation","totalCostOfCreditLocation","APRValueLocation","totalRepayAmountLocation","monthlyPaymentAmount","parseFloat","toLocaleString","style","currency","totalAmountFormat","totalCreditFormat","APRValueFormat","totalRepayAmountFormat","textContent","setSelectedAPR","radioButtons","selectedValue","forEach","radioButton","checked","value","init","borrowAmount","aprValue","calcType","addEventListener","monthlyAmount","this","classList","remove","add","event","numericValue","target","replace","year","button","length","slice","preventDefault","toggle"],"mappings":"AAAA,MAAAA,gBAAA,WACA,MAAAC,EAAAC,SAAAC,eAAA,iBACAC,EAAAF,SAAAC,eAAA,gBACAE,EAAAH,SAAAI,cAAA,cACAC,EAAAL,SAAAI,cAAA,iBACAE,EAAAN,SAAAO,iBAAA,qBACAC,EAAAR,SAAAI,cAAA,qBACAK,EAAAT,SAAAC,eAAA,kBACAD,SAAAI,cAAA,+BAEA,IACAM,EADAC,GAAA,EAGA,SAAAC,EAAAC,EAAAC,EAAAC,EAAAC,EAAAC,GAEA,MAAAC,EAAAD,EAAA,GAAA,IAGAE,EAAA,GAAAH,EACA,IAAAI,EACAC,EAEA,WAAAR,GAMAO,GAJAL,EACAD,EAAAI,GAAA,EAAAI,KAAAC,IAAA,EAAAL,GAAAC,KAGAA,EAGAE,EAAAD,EAAAN,IAOAM,EAAAL,EAAAI,EAGAE,EAAAD,GAPAN,EACAC,GAAAG,GAAA,EAAAI,KAAAC,IAAA,EAAAL,GAAAC,OAUA,MAAAK,EAAAxB,SAAAC,eAAA,wBACAwB,EAAAzB,SAAAC,eAAA,kBACAyB,EAAA1B,SAAAC,eAAA,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,EAEA,CApPA,GAsPAxC,SAAAmD,iBAAA,oBAAA,WACArD,gBAAAiD,MACA","file":"loansCalculator.min.js","sourcesContent":["const loansCalculator = (function () {\r\n\tconst monthlyBudget = document.getElementById('monthlyBudget');\r\n\tconst borrowAmountInput = document.getElementById('borrowAmount');\r\n\tconst aprHelpLink = document.querySelector('.help-link');\r\n\tconst aprHelpContent = document.querySelector('.help-content');\r\n\tconst aprButtons = document.querySelectorAll('input[name=\"APR\"]');\r\n\tconst exactAPRContent = document.querySelector('.exactAPR-content');\r\n\tconst exactAPRInput = document.getElementById('aprExactAmount');\r\n\tconst checkedRadioButton = document.querySelector('input[name=\"years\"]:checked');\r\n\r\n\tlet isCustomApr = false;\r\n\tlet customAprValue;\r\n\r\n\tfunction calculateLoanDetails(type, totalAmount, monthlyPayment, numberOfYears, apr) {\r\n\t\t// Calculate monthly interest rate\r\n\t\tconst monthlyInterestRate = apr / 12 / 100;\r\n\r\n\t\t// Calculate total number of payments\r\n\t\tconst numberOfPayments = numberOfYears * 12;\r\n\t\tlet totalRepayment;\r\n\t\tlet totalInterestPaid;\r\n\r\n\t\tif (type === 'borrow') {\r\n\t\t\t// Calculate monthly payment using the formula\r\n\t\t\tmonthlyPayment =\r\n\t\t\t\t(totalAmount * monthlyInterestRate) / (1 - Math.pow(1 + monthlyInterestRate, -numberOfPayments));\r\n\r\n\t\t\t// Calculate total repayment amount\r\n\t\t\ttotalRepayment = monthlyPayment * numberOfPayments;\r\n\r\n\t\t\t// Calculate total amount of interest paid\r\n\t\t\ttotalInterestPaid = totalRepayment - totalAmount;\r\n\t\t} else {\r\n\t\t\t// Calculate loan amount (total borrowed) using the formula\r\n\t\t\ttotalAmount =\r\n\t\t\t\tmonthlyPayment / (monthlyInterestRate / (1 - Math.pow(1 + monthlyInterestRate, -numberOfPayments)));\r\n\r\n\t\t\t// Calculate total repayment amount\r\n\t\t\ttotalRepayment = monthlyPayment * numberOfPayments;\r\n\r\n\t\t\t// Calculate total amount of interest paid\r\n\t\t\ttotalInterestPaid = totalRepayment - totalAmount;\r\n\t\t}\r\n\r\n\t\t//- Putting the values into the html\r\n\t\tconst monthlyPaymentAmountLocation = document.getElementById('monthlyPaymentAmount');\r\n\t\tconst numberOfMonthsLocation = document.getElementById('numberOfMonths');\r\n\t\tconst totalBorrowAmountLocation = document.getElementById('totalBorrowAmount');\r\n\t\tconst totalCostOfCreditLocation = document.getElementById('totalCostOfCredit');\r\n\t\tconst APRValueLocation = document.getElementById('APRValue');\r\n\t\tconst totalRepayAmountLocation = document.getElementById('totalRepayAmount');\r\n\r\n\t\tconst monthlyPaymentAmount = parseFloat(monthlyPayment).toLocaleString('en-GB', {\r\n\t\t\tstyle: 'currency',\r\n\t\t\tcurrency: 'GBP',\r\n\t\t});\r\n\t\tconst totalAmountFormat = parseFloat(totalAmount).toLocaleString('en-GB', {\r\n\t\t\tstyle: 'currency',\r\n\t\t\tcurrency: 'GBP',\r\n\t\t});\r\n\t\tconst totalCreditFormat = totalRepayment.toLocaleString('en-GB', {\r\n\t\t\tstyle: 'currency',\r\n\t\t\tcurrency: 'GBP',\r\n\t\t});\r\n\t\tconst APRValueFormat = `${apr}%`;\r\n\t\tconst totalRepayAmountFormat = totalInterestPaid.toLocaleString('en-GB', {\r\n\t\t\tstyle: 'currency',\r\n\t\t\tcurrency: 'GBP',\r\n\t\t});\r\n\r\n\t\tnumberOfMonthsLocation.textContent = numberOfPayments;\r\n\t\tmonthlyPaymentAmountLocation.textContent = monthlyPaymentAmount;\r\n\t\ttotalBorrowAmountLocation.textContent = totalAmountFormat;\r\n\t\ttotalCostOfCreditLocation.textContent = totalCreditFormat;\r\n\t\tAPRValueLocation.textContent = APRValueFormat;\r\n\t\ttotalRepayAmountLocation.textContent = totalRepayAmountFormat;\r\n\t}\r\n\r\n\tfunction setSelectedAPR(radioButtons) {\r\n\t\t// Initialize a variable to store the selected value\r\n\t\tlet selectedValue = '';\r\n\r\n\t\t// Loop through the radio buttons to find the selected one\r\n\t\tradioButtons.forEach(function (radioButton) {\r\n\t\t\tif (radioButton.checked) {\r\n\t\t\t\tselectedValue = radioButton.value;\r\n\t\t\t}\r\n\t\t});\r\n\r\n\t\t// Now you can use the selectedValue variable as needed\r\n\t\t// You can also return the value if you need to use it outside of this function\r\n\t\treturn selectedValue;\r\n\t}\r\n\r\n\tfunction init() {\r\n\t\t//- Setting the base values for the initial search.\r\n\t\tlet borrowAmount = borrowAmountInput.value;\r\n\t\tlet monthlyPayment = monthlyBudget.value;\r\n\t\tlet numberOfYears = 3;\r\n\t\tlet aprValue = setSelectedAPR(aprButtons);\r\n\t\tlet calcType = 'budget';\r\n\r\n\t\t//- Monthly / Total Amount\r\n\t\tconst calcTypeRadioButtons = document.querySelectorAll('input[name=\"repaymentType\"]');\r\n\t\tcalcTypeRadioButtons.forEach(function (radioButton) {\r\n\t\t\tradioButton.addEventListener('change', function () {\r\n\t\t\t\tconst monthlyAmount = document.getElementById('monthlyAmount');\r\n\t\t\t\tconst totalAmount = document.getElementById('totalAmount');\r\n\t\t\t\tcalcType = this.value;\r\n\t\t\t\tif (this.value === 'budget') {\r\n\t\t\t\t\tmonthlyAmount.classList.remove('display-none');\r\n\t\t\t\t\ttotalAmount.classList.add('display-none');\r\n\t\t\t\t} else if (this.value === 'borrow') {\r\n\t\t\t\t\tmonthlyAmount.classList.add('display-none');\r\n\t\t\t\t\ttotalAmount.classList.remove('display-none');\r\n\t\t\t\t}\r\n\t\t\t\tcalculateLoanDetails(\r\n\t\t\t\t\tcalcType,\r\n\t\t\t\t\tborrowAmount,\r\n\t\t\t\t\tmonthlyPayment,\r\n\t\t\t\t\tnumberOfYears,\r\n\t\t\t\t\tisCustomApr ? customAprValue : aprValue\r\n\t\t\t\t);\r\n\t\t\t});\r\n\t\t});\r\n\r\n\t\t//- Monthly Budget Amount\r\n\t\tmonthlyBudget.addEventListener('input', function (event) {\r\n\t\t\t// Check if the pressed key is not a number\r\n\t\t\t// Get the entered value\r\n\t\t\tconst inputValue = event.target.value;\r\n\r\n\t\t\t// Remove any non-numeric characters from the input value\r\n\t\t\tconst numericValue = inputValue.replace(/\\D/g, '');\r\n\r\n\t\t\t// Set the input value to the sanitized numeric value\r\n\t\t\tevent.target.value = numericValue;\r\n\t\t\tmonthlyPayment = event.target.value;\r\n\r\n\t\t\tcalculateLoanDetails(\r\n\t\t\t\tcalcType,\r\n\t\t\t\tborrowAmount,\r\n\t\t\t\tevent.target.value,\r\n\t\t\t\tnumberOfYears,\r\n\t\t\t\tisCustomApr ? customAprValue : aprValue\r\n\t\t\t);\r\n\t\t});\r\n\r\n\t\t//- Total Amount\r\n\t\tborrowAmountInput.addEventListener('input', function (event) {\r\n\t\t\t// Check if the pressed key is not a number\r\n\t\t\t// Get the entered value\r\n\t\t\tconst inputValue = event.target.value;\r\n\r\n\t\t\t// Remove any non-numeric characters from the input value\r\n\t\t\tconst numericValue = inputValue.replace(/\\D/g, '');\r\n\r\n\t\t\t// Set the input value to the sanitized numeric value\r\n\t\t\tevent.target.value = numericValue;\r\n\t\t\tborrowAmount = event.target.value;\r\n\r\n\t\t\tcalculateLoanDetails(\r\n\t\t\t\tcalcType,\r\n\t\t\t\tevent.target.value,\r\n\t\t\t\tmonthlyPayment,\r\n\t\t\t\tnumberOfYears,\r\n\t\t\t\tisCustomApr ? customAprValue : aprValue\r\n\t\t\t);\r\n\t\t});\r\n\r\n\t\t// Years\r\n\t\tconst aprYears = document.querySelectorAll('input[name=\"years\"]');\r\n\t\taprYears.forEach(function (year) {\r\n\t\t\tyear.addEventListener('click', function () {\r\n\t\t\t\tnumberOfYears = year.value;\r\n\t\t\t\tcalculateLoanDetails(\r\n\t\t\t\t\tcalcType,\r\n\t\t\t\t\tborrowAmount,\r\n\t\t\t\t\tmonthlyPayment,\r\n\t\t\t\t\tnumberOfYears,\r\n\t\t\t\t\tisCustomApr ? customAprValue : aprValue\r\n\t\t\t\t);\r\n\t\t\t});\r\n\t\t});\r\n\r\n\t\t// APR\r\n\t\taprButtons.forEach(function (button) {\r\n\t\t\tbutton.addEventListener('click', function () {\r\n\t\t\t\tif (button.value === 'Exact') {\r\n\t\t\t\t\texactAPRContent.classList.add('active');\r\n\t\t\t\t\taprValue = '10';\r\n\t\t\t\t\tisCustomApr = true;\r\n\t\t\t\t\texactAPRInput.addEventListener('input', function (event) {\r\n\t\t\t\t\t\tconst maxLength = 5;\r\n\t\t\t\t\t\tcustomAprValue = event.target.value;\r\n\t\t\t\t\t\tif (customAprValue.length > maxLength) {\r\n\t\t\t\t\t\t\tcustomAprValue = customAprValue.slice(0, maxLength);\r\n\t\t\t\t\t\t\tevent.target.value = customAprValue;\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t\tcalculateLoanDetails(calcType, borrowAmount, monthlyPayment, numberOfYears, customAprValue);\r\n\t\t\t\t\t});\r\n\t\t\t\t\tcalculateLoanDetails(\r\n\t\t\t\t\t\tcalcType,\r\n\t\t\t\t\t\tborrowAmount,\r\n\t\t\t\t\t\tmonthlyPayment,\r\n\t\t\t\t\t\tnumberOfYears,\r\n\t\t\t\t\t\tisCustomApr ? customAprValue : aprValue\r\n\t\t\t\t\t);\r\n\t\t\t\t} else {\r\n\t\t\t\t\tisCustomApr = false;\r\n\t\t\t\t\texactAPRContent.classList.remove('active');\r\n\t\t\t\t\taprValue = button.value;\r\n\t\t\t\t\tcalculateLoanDetails(\r\n\t\t\t\t\t\tcalcType,\r\n\t\t\t\t\t\tborrowAmount,\r\n\t\t\t\t\t\tmonthlyPayment,\r\n\t\t\t\t\t\tnumberOfYears,\r\n\t\t\t\t\t\tisCustomApr ? customAprValue : aprValue\r\n\t\t\t\t\t);\r\n\t\t\t\t}\r\n\t\t\t});\r\n\t\t});\r\n\r\n\t\t// APR Help Link\r\n\t\taprHelpLink.addEventListener('click', function (event) {\r\n\t\t\tevent.preventDefault(); // Prevent the default behavior of the link\r\n\t\t\taprHelpLink.classList.toggle('active');\r\n\t\t\taprHelpContent.classList.toggle('active'); // Toggle the 'active' class to show/hide the content\r\n\t\t});\r\n\r\n\t\tcalculateLoanDetails(\r\n\t\t\tcalcType,\r\n\t\t\tborrowAmount,\r\n\t\t\tmonthlyPayment,\r\n\t\t\tnumberOfYears,\r\n\t\t\tisCustomApr ? customAprValue : aprValue\r\n\t\t);\r\n\t}\r\n\r\n\treturn {\r\n\t\tinit: init,\r\n\t\tcalculateLoanDetails: calculateLoanDetails,\r\n\t\tsetSelectedAPR: setSelectedAPR,\r\n\t};\r\n})();\r\n\r\ndocument.addEventListener('DOMContentLoaded', function () {\r\n\tloansCalculator.init();\r\n});\r\n"]}