-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
116 lines (96 loc) · 4.12 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
document.getElementById('breakdownToggle').addEventListener('change', function() {
const expenseBreakdown = document.getElementById('expenseBreakdown');
const expenseInput = document.getElementById('expenses');
if (this.checked) {
expenseBreakdown.classList.remove('hidden');
expenseInput.setAttribute('disabled', 'true');
} else {
expenseBreakdown.classList.add('hidden');
expenseInput.removeAttribute('disabled');
}
});
document.getElementById('addExpense').addEventListener('click', function() {
const expenseList = document.getElementById('expenseList');
const newExpense = document.createElement('div');
newExpense.classList.add('expense-item');
newExpense.innerHTML = `
<input type="text" class="expense-name" placeholder="Expense Name">
<input type="number" class="expense-value" min="0" placeholder="$ Amount">
`;
expenseList.appendChild(newExpense);
});
function showError(input, message) {
const inputGroup = input.closest('.input-group');
const error = document.createElement('div');
error.className = 'error-message';
error.innerText = message;
inputGroup.classList.add('error');
inputGroup.appendChild(error);
}
function clearErrors() {
document.querySelectorAll('.error-message').forEach(error => error.remove());
document.querySelectorAll('.input-group').forEach(group => group.classList.remove('error'));
}
document.getElementById('calculateBtn').addEventListener('click', function() {
clearErrors();
const income1 = document.getElementById('income1');
const income2 = document.getElementById('income2');
let expenses = document.getElementById('expenses');
if (!income1.value) {
showError(income1, "Please enter Partner 1's income.");
}
if (!income2.value) {
showError(income2, "Please enter Partner 2's income.");
}
if (document.getElementById('breakdownToggle').checked) {
let totalExpenses = 0;
let errorFound = false;
document.querySelectorAll('.expense-item').forEach(item => {
const value = item.querySelector('.expense-value').value;
if (!value || parseFloat(value) === 0) {
showError(item.querySelector('.expense-value'), "Please enter a valid expense.");
errorFound = true;
}
totalExpenses += parseFloat(value) || 0;
});
if (errorFound || totalExpenses === 0) {
return;
}
expenses.value = totalExpenses;
}
const totalIncome = parseFloat(income1.value) + parseFloat(income2.value);
const partner1Share = (parseFloat(income1.value) / totalIncome) * parseFloat(expenses.value);
const partner2Share = (parseFloat(income2.value) / totalIncome) * parseFloat(expenses.value);
document.getElementById('results').innerHTML = `
<h3>Results:</h3>
<p>Partner 1 should contribute: $${partner1Share.toFixed(2)}</p>
<p>Partner 2 should contribute: $${partner2Share.toFixed(2)}</p>
`;
if (Math.abs(income1.value - income2.value) / totalIncome > 0.3) {
document.getElementById('results').innerHTML += `
<p style="color: red;">Significant income disparity detected!</p>
`;
}
});
document.getElementById('downloadBtn').addEventListener('click', function() {
const income1 = document.getElementById('income1').value;
const income2 = document.getElementById('income2').value;
const resultsContent = document.getElementById('results').innerHTML;
const pdfContent = `
** Relationship Budget Report **
Partner 1 Income: $${income1}\n
Partner 2 Income: $${income2}\n
Contributions:\n${resultsContent.replace(/<[^>]+>/g, '')}
`;
const blob = new Blob([pdfContent], { type: 'application/pdf' });
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = 'budget_results.pdf';
link.click();
});
document.querySelectorAll('.info-icon').forEach(icon => {
icon.addEventListener('click', function() {
const tooltip = this.nextElementSibling;
tooltip.classList.toggle('show');
});
});