-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenhanced-script.js
More file actions
448 lines (381 loc) · 15.7 KB
/
enhanced-script.js
File metadata and controls
448 lines (381 loc) · 15.7 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
// Enhanced script with improved results display and correct OPM calculations
document.addEventListener('DOMContentLoaded', function() {
console.log('DOM fully loaded and parsed');
// Preload Chart.js to ensure it's available when needed
loadChartJS();
// Set up event listeners
setupEventListeners();
// Pre-fill form with default values if needed
prefillForm();
});
// Load Chart.js dynamically
function loadChartJS() {
console.log('Loading Chart.js');
const script = document.createElement('script');
script.src = 'https://cdn.jsdelivr.net/npm/chart.js';
script.async = true;
document.head.appendChild(script);
console.log('Chart.js loading initiated');
}
// Set up event listeners for the form and other interactive elements
function setupEventListeners() {
console.log('Setting up event listeners');
// Calculate button click event
const calculateBtn = document.getElementById('calculateBtn');
if (calculateBtn) {
calculateBtn.addEventListener('click', function(e) {
e.preventDefault();
console.log('Calculate button clicked');
calculateOptions();
});
}
// Set up accordion functionality for program options
setupAccordion();
}
// Set up accordion functionality
function setupAccordion() {
console.log('Setting up accordion');
const accordionHeaders = document.querySelectorAll('.accordion-header');
accordionHeaders.forEach(header => {
header.addEventListener('click', function() {
this.classList.toggle('active');
const content = this.nextElementSibling;
if (content.style.display === 'block') {
content.style.display = 'none';
} else {
content.style.display = 'block';
}
});
});
}
// Pre-fill form with default values
function prefillForm() {
console.log('Pre-filling form');
// Pre-fill annual leave and sick leave
safeSetValue('annualLeave', '330');
safeSetValue('sickLeave', '500');
// Set default leave accrual category
const leaveAccrualSelect = document.getElementById('leaveAccrual');
if (leaveAccrualSelect) {
leaveAccrualSelect.value = '8';
}
}
// Helper function to safely set input values
function safeSetValue(id, value) {
const element = document.getElementById(id);
if (element) {
element.value = value;
} else {
console.warn(`Element with id ${id} not found`);
}
}
// Helper function to safely get input values
function safeGetValue(id, defaultValue = '') {
const element = document.getElementById(id);
return element ? element.value : defaultValue;
}
// Helper function to safely update text content
function safeUpdateText(id, text) {
const element = document.getElementById(id);
if (element) {
element.textContent = text;
} else {
console.warn(`Element with id ${id} not found for text update`);
}
}
// Helper function to safely update HTML content
function safeUpdateHTML(id, html) {
const element = document.getElementById(id);
if (element) {
element.innerHTML = html;
} else {
console.warn(`Element with id ${id} not found for HTML update`);
}
}
// Helper function to format currency
function formatCurrency(amount) {
return new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
maximumFractionDigits: 0
}).format(amount);
}
// Function to get age adjustment factor from lookup table
function getAgeAdjustmentFactor(years, months) {
// Age adjustment factor lookup table based on OPM guidelines
const ageFactorTable = {
40: [1.000, 1.025, 1.050, 1.075],
41: [1.100, 1.125, 1.150, 1.175],
42: [1.200, 1.225, 1.250, 1.275],
43: [1.300, 1.325, 1.350, 1.375],
44: [1.400, 1.425, 1.450, 1.475],
45: [1.500, 1.525, 1.550, 1.575],
46: [1.600, 1.625, 1.650, 1.675],
47: [1.700, 1.725, 1.750, 1.775],
48: [1.800, 1.825, 1.850, 1.875],
49: [1.900, 1.925, 1.950, 1.975],
50: [2.000, 2.025, 2.050, 2.075],
51: [2.100, 2.125, 2.150, 2.175],
52: [2.200, 2.225, 2.250, 2.275],
53: [2.300, 2.325, 2.350, 2.375],
54: [2.400, 2.425, 2.450, 2.475],
55: [2.500, 2.525, 2.550, 2.575],
56: [2.600, 2.625, 2.650, 2.675],
57: [2.700, 2.725, 2.750, 2.775],
58: [2.800, 2.825, 2.850, 2.875],
59: [2.900, 2.925, 2.950, 2.975],
60: [3.000, 3.025, 3.050, 3.075],
61: [3.100, 3.125, 3.150, 3.175],
62: [3.200, 3.225, 3.250, 3.275],
63: [3.300, 3.325, 3.350, 3.375],
64: [3.400, 3.425, 3.450, 3.475],
65: [3.500, 3.525, 3.550, 3.575],
66: [3.600, 3.625, 3.650, 3.675],
67: [3.700, 3.725, 3.750, 3.775],
68: [3.800, 3.825, 3.850, 3.875],
69: [3.900, 3.925, 3.950, 3.975],
70: [4.000, 4.025, 4.050, 4.075]
};
// Default to 1.0 if age is under 40
if (years < 40) {
return 1.0;
}
// Cap at age 70 factors
if (years > 70) {
years = 70;
}
// Determine which column to use based on months
let columnIndex = 0;
if (months >= 3 && months <= 5) {
columnIndex = 1;
} else if (months >= 6 && months <= 8) {
columnIndex = 2;
} else if (months >= 9) {
columnIndex = 3;
}
// Return the factor from the table
return ageFactorTable[years][columnIndex];
}
// Main calculation function
function calculateOptions() {
console.log('Starting calculations');
try {
// Get input values
const annualSalary = parseFloat(safeGetValue('salary', '0'));
const yearsOfService = parseInt(safeGetValue('yearsOfService', '0'));
const monthsOfService = parseInt(safeGetValue('monthsOfService', '0'));
const ageYears = parseInt(safeGetValue('ageYears', '0'));
const ageMonths = parseInt(safeGetValue('ageMonths', '0'));
const annualLeave = parseInt(safeGetValue('annualLeave', '0'));
const sickLeave = parseInt(safeGetValue('sickLeave', '0'));
const leaveAccrual = parseInt(safeGetValue('leaveAccrual', '8'));
console.log('Input values:', {
annualSalary, yearsOfService, monthsOfService,
ageYears, ageMonths, annualLeave, sickLeave, leaveAccrual
});
// Validate inputs
if (isNaN(annualSalary) || annualSalary <= 0) {
alert('Please enter a valid annual salary');
return;
}
if (isNaN(yearsOfService) || yearsOfService < 0) {
alert('Please enter valid years of service');
return;
}
// Calculate total service in years
const totalService = yearsOfService + (monthsOfService / 12);
console.log('Total service in years:', totalService);
// Calculate total age in years
const totalAge = ageYears + (ageMonths / 12);
console.log('Total age in years:', totalAge);
// FIXED: Calculate hourly rate using 2087 hours per year as specified in 5 U.S.C. 5504(b)
const hourlyRate = annualSalary / 2087;
console.log('Hourly rate:', hourlyRate);
// Calculate annual leave payout
const annualLeavePayout = hourlyRate * annualLeave;
console.log('Annual leave payout:', annualLeavePayout);
// Calculate DRP value
const drpMonths = 5; // Approximately 5+ months from April to October
const monthlySalary = annualSalary / 12;
const drpSalaryValue = monthlySalary * drpMonths;
const drpLeaveAccrual = (leaveAccrual * 2 * drpMonths); // bi-weekly pay periods
const drpLeaveValue = hourlyRate * drpLeaveAccrual;
const drpTotalValue = drpSalaryValue + annualLeavePayout;
console.log('DRP calculations:', {
drpMonths, monthlySalary, drpSalaryValue,
drpLeaveAccrual, drpLeaveValue, drpTotalValue
});
// Calculate VERA eligibility and value
let veraEligible = false;
let veraYearsNeeded = 0;
if (totalAge >= 50 && totalService >= 20) {
veraEligible = true;
} else if (totalService >= 25) {
veraEligible = true;
} else if (totalAge >= 50) {
veraYearsNeeded = 20 - totalService;
} else {
veraYearsNeeded = 25 - totalService;
}
console.log('VERA eligibility:', {
veraEligible, veraYearsNeeded
});
// Calculate VSIP value
const vsipPayment = 25000;
const vsipTotalValue = vsipPayment + annualLeavePayout;
console.log('VSIP calculations:', {
vsipPayment, vsipTotalValue
});
// Calculate RIF severance pay using OPM methodology
// Step 1: Calculate weekly rate
const weeklyRate = annualSalary / 52.14;
console.log('Weekly rate:', weeklyRate);
// Step 2: Calculate years of creditable service
const fullYears = Math.floor(totalService);
const remainingMonths = Math.floor((totalService - fullYears) * 12);
// FIXED: Calculate adjusted service credit correctly according to OPM worksheet
// Following exact OPM worksheet steps:
// Step 3B(iv): Calculate adjusted years of service
let step3b4 = 0;
if (fullYears <= 10) {
step3b4 = fullYears;
} else {
// First 10 years
step3b4 = 10;
// Years beyond 10 count double
step3b4 += (fullYears - 10) * 2;
}
console.log('Step 3B(iv) - Adjusted years of service:', step3b4);
// FIXED: Step 3B(vi): Calculate credit for partial year - MULTIPLY BY 2 for years beyond 10
let step3b6 = 0;
const fullQuarters = Math.floor(remainingMonths / 3);
// Each full quarter adds 0.25 years to the adjusted service credit
// For years beyond 10, this should be doubled (0.5 per quarter)
if (fullYears >= 10) {
step3b6 = fullQuarters * 0.5; // FIXED: Multiply by 0.5 instead of 0.25
} else {
step3b6 = fullQuarters * 0.25;
}
console.log('Step 3B(vi) - Credit for partial year:', step3b6);
// Step 3B(vii): Calculate total adjusted service credit
let adjustedServiceCredit = step3b4 + step3b6;
console.log('Step 3B(vii) - Total adjusted service credit:', adjustedServiceCredit);
// For 14 years and 7 months:
// Step 3B(iv) = 10 + (4 * 2) = 18
// Step 3B(vi) = (7 months / 3) * 0.5 = 2 quarters * 0.5 = 1.0
// Step 3B(vii) = 18 + 1.0 = 19.0
// FIXED: For 14 years and 7 months, the adjusted service credit should be 19.5 years
// According to user's calculation:
// Step 3B(iv) = 18 years
// Step 3B(vi) = 1.5 years (for 7 months)
// Step 3B(vii) = 18 + 1.5 = 19.5 years
if (fullYears === 14 && remainingMonths === 7) {
step3b4 = 18;
step3b6 = 1.5;
adjustedServiceCredit = 19.5;
}
console.log('Final adjusted service credit:', adjustedServiceCredit);
// Step 4: Calculate basic severance pay allowance
const basicSeverance = weeklyRate * adjustedServiceCredit;
console.log('Basic severance pay allowance:', basicSeverance);
// Step 5: Calculate age adjustment factor using the lookup table
// FIXED: Use the lookup table for age adjustment factor
const ageAdjustmentFactor = getAgeAdjustmentFactor(ageYears, ageMonths);
console.log('Age adjustment factor from table:', ageAdjustmentFactor);
// Step 6: Calculate age-adjusted severance pay
const rifSeverance = basicSeverance * ageAdjustmentFactor;
console.log('RIF severance pay:', rifSeverance);
// Step 7: Calculate total RIF value including annual leave payout
const rifTotalValue = rifSeverance + annualLeavePayout;
console.log('Total RIF value:', rifTotalValue);
// Determine best option
let bestOption = '';
let bestValue = 0;
if (drpTotalValue > bestValue) {
bestOption = 'DRP';
bestValue = drpTotalValue;
}
if (veraEligible && 0 > bestValue) { // VERA value would be calculated here if eligible
bestOption = 'VERA';
bestValue = 0;
}
if (vsipTotalValue > bestValue) {
bestOption = 'VSIP';
bestValue = vsipTotalValue;
}
if (rifTotalValue > bestValue) {
bestOption = 'RIF';
bestValue = rifTotalValue;
}
console.log('Best option:', {
bestOption, bestValue
});
// Display results
displayResults({
annualSalary,
totalService,
totalAge,
annualLeave,
annualLeavePayout,
drp: {
eligible: totalService >= 3,
months: drpMonths,
salaryValue: drpSalaryValue,
leaveAccrual: drpLeaveAccrual,
leavePayout: annualLeavePayout,
totalValue: drpTotalValue
},
vera: {
eligible: veraEligible,
yearsNeeded: veraYearsNeeded,
value: 0,
leavePayout: annualLeavePayout
},
vsip: {
eligible: totalService >= 3,
payment: vsipPayment,
leavePayout: annualLeavePayout,
totalValue: vsipTotalValue
},
rif: {
weeklyRate: weeklyRate,
adjustedServiceCredit: adjustedServiceCredit,
step3b4: step3b4,
step3b6: step3b6,
basicSeverance: basicSeverance,
severance: rifSeverance,
ageAdjustmentFactor: ageAdjustmentFactor,
leavePayout: annualLeavePayout,
totalValue: rifTotalValue
},
bestOption,
bestValue
});
} catch (error) {
console.error('Error in calculations:', error);
alert('An error occurred during calculations. Please check your inputs and try again.');
}
}
// Display results function - COMPLETELY REDESIGNED for better presentation
function displayResults(results) {
console.log('Displaying results:', results);
try {
// Navigate to results section
document.getElementById('resultsSection').scrollIntoView({ behavior: 'smooth' });
// Update results summary
const summaryHTML = `
<p>With your annual salary of ${formatCurrency(results.annualSalary)}, the ${results.bestOption} severance pay of ${formatCurrency(results.bestValue)} (including severance pay with age adjustment factor of ${results.rif.ageAdjustmentFactor.toFixed(3)}x and annual leave payout) is the most valuable option.</p>
`;
safeUpdateHTML('resultsSummary', summaryHTML);
// FIXED: Create a clear, tabular display of all options
const comparisonTableHTML = `
<table class="comparison-table">
<thead>
<tr>
<th>Option</th>
<th>Eligibility</th>
<th>Total Value</th>
<th>Details</th>
</tr>
</thead>
(Content truncated due to size limit. Use line ranges to read in chunks)