-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
320 lines (275 loc) · 13.3 KB
/
script.js
File metadata and controls
320 lines (275 loc) · 13.3 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
document.addEventListener('DOMContentLoaded', function() {
// Get DOM elements
const joinDateInput = document.getElementById('joinDate');
const contributionPercentInput = document.getElementById('contributionPercent');
const salaryInput = document.getElementById('salary');
const paycheckContributionInput = document.getElementById('paycheckContribution');
const offeringPriceInput = document.getElementById('offeringPrice');
const purchasePriceInput = document.getElementById('purchasePrice');
const calculateBtn = document.getElementById('calculateBtn');
const contributionModeToggle = document.getElementById('contributionMode');
const percentageModeDiv = document.getElementById('percentageMode');
const paycheckModeDiv = document.getElementById('paycheckMode');
// Result elements
const nextOfferingValue = document.getElementById('nextOfferingValue');
const purchaseDateValue = document.getElementById('purchaseDateValue');
const lookbackPriceValue = document.getElementById('lookbackPriceValue');
const currentPriceValue = document.getElementById('currentPriceValue');
const purchasePriceValue = document.getElementById('purchasePriceValue');
const contributionValue = document.getElementById('contributionValue');
const sharesReceivedValue = document.getElementById('sharesReceivedValue');
const marketValueValue = document.getElementById('marketValueValue');
const potentialGainValue = document.getElementById('potentialGainValue');
const percentageReturnValue = document.getElementById('percentageReturnValue');
// Get label elements for stock prices
const offeringPriceLabel = document.querySelector('label[for="offeringPrice"]');
const purchasePriceLabel = document.querySelector('label[for="purchasePrice"]');
// Add event listeners
joinDateInput.addEventListener('change', updateOfferingPeriod);
calculateBtn.addEventListener('click', calculateESPP);
contributionModeToggle.addEventListener('change', toggleContributionMode);
// Function to format dates
function formatDate(date) {
return date.toLocaleDateString('en-US', {
year: 'numeric',
month: 'short',
day: 'numeric'
});
}
// Function to get next trading day if date falls on weekend
function getNextTradingDay(date) {
const day = date.getDay();
if (day === 6) { // Saturday
date.setDate(date.getDate() + 2);
} else if (day === 0) { // Sunday
date.setDate(date.getDate() + 1);
}
return date;
}
// Function to get offering period start date
function getOfferingStartDate(year, month) {
let date = new Date(year, month, month === 1 ? 28 : 31); // Feb 28 or Aug 31
return getNextTradingDay(date);
}
// Function to get next offering period
function getNextOfferingPeriod(joinDate) {
const today = new Date();
const currentYear = today.getFullYear();
// Define next possible offering periods (looking ahead 2 years)
const offeringStarts = [];
for (let year = currentYear - 1; year <= currentYear + 1; year++) {
// February 28th offering
offeringStarts.push({
start: getOfferingStartDate(year, 1), // February
end: getOfferingStartDate(year + 2, 1) // February 2 years later
});
// August 31st offering
offeringStarts.push({
start: getOfferingStartDate(year, 7), // August
end: getOfferingStartDate(year + 2, 7) // August 2 years later
});
}
// Sort offering periods by start date
offeringStarts.sort((a, b) => a.start - b.start);
// Find the next valid offering period based on join date
const validOffering = offeringStarts.find(period =>
period.start > joinDate && period.start >= today
);
if (!validOffering) return null;
// Calculate purchase dates for this offering period
const purchaseDates = [];
let currentDate = new Date(validOffering.start);
// Add 4 purchase dates (6 months apart)
for (let i = 0; i < 4; i++) {
let purchaseDate = new Date(currentDate);
purchaseDate.setMonth(purchaseDate.getMonth() + 6);
purchaseDates.push(getNextTradingDay(purchaseDate));
currentDate = purchaseDate;
}
return {
start: validOffering.start,
end: validOffering.end,
purchaseDates: purchaseDates
};
}
// Function to create a clickable date link
function createDateLink(date) {
const formattedDate = formatDate(date);
const investorUrl = 'https://investors.paloaltonetworks.com/stock-information/historical-price-lookup';
return `<span class="date-link"
onclick="window.open('${investorUrl}', '_blank')"
title="Click to look up PANW stock price for ${formattedDate} on Palo Alto Networks Investor Relations site"
>${formattedDate}</span>`;
}
// Function to determine and display next offering period
function updateOfferingPeriod() {
const joinDate = new Date(joinDateInput.value);
if (isNaN(joinDate.getTime())) return;
const period = getNextOfferingPeriod(joinDate);
if (!period) return;
// Update the offering period display
nextOfferingValue.textContent = `${formatDate(period.start)} to ${formatDate(period.end)}`;
// Update the stock price input labels with clickable dates
offeringPriceLabel.innerHTML = `Stock price at offering start (USD) (${createDateLink(period.start)}):`;
purchasePriceLabel.innerHTML = `Expected stock price at purchase (USD) (${createDateLink(period.purchaseDates[0])}):`;
}
// Function to toggle between contribution modes
function toggleContributionMode() {
if (contributionModeToggle.checked) {
percentageModeDiv.classList.add('hidden');
paycheckModeDiv.classList.remove('hidden');
// If percentage mode has values, calculate and set paycheck amount
if (salaryInput.value && contributionPercentInput.value) {
const annualSalary = parseFloat(salaryInput.value);
const percentage = parseFloat(contributionPercentInput.value);
const paycheckAmount = (annualSalary * (percentage / 100)) / 24;
paycheckContributionInput.value = paycheckAmount.toFixed(2);
}
} else {
percentageModeDiv.classList.remove('hidden');
paycheckModeDiv.classList.add('hidden');
// If paycheck amount is set, calculate and set percentage
if (paycheckContributionInput.value) {
const paycheckAmount = parseFloat(paycheckContributionInput.value);
const annualContribution = paycheckAmount * 24;
if (salaryInput.value) {
const percentage = (annualContribution / parseFloat(salaryInput.value)) * 100;
contributionPercentInput.value = Math.min(15, Math.max(1, percentage)).toFixed(1);
}
}
}
}
// Function to calculate ESPP benefits
function calculateESPP() {
// Get input values
const joinDate = new Date(joinDateInput.value);
let contributionAmount;
if (contributionModeToggle.checked) {
// Per-paycheck mode
const paycheckAmount = parseFloat(paycheckContributionInput.value);
if (isNaN(paycheckAmount) || paycheckAmount <= 0) {
alert('Please enter a valid paycheck contribution amount');
return;
}
contributionAmount = paycheckAmount * 12; // 6 months = 12 paychecks
} else {
// Percentage mode
const contributionPercent = parseFloat(contributionPercentInput.value);
const annualSalary = parseFloat(salaryInput.value);
if (isNaN(contributionPercent) || contributionPercent < 1 || contributionPercent > 15) {
alert('Contribution percentage must be between 1% and 15%');
return;
}
if (isNaN(annualSalary) || annualSalary <= 0) {
alert('Please enter a valid annual salary');
return;
}
const sixMonthSalary = annualSalary / 2;
contributionAmount = sixMonthSalary * (contributionPercent / 100);
}
const offeringPrice = parseFloat(offeringPriceInput.value);
const expectedPurchasePrice = parseFloat(purchasePriceInput.value);
// Validate remaining inputs
if (isNaN(joinDate.getTime())) {
alert('Please enter a valid join date');
return;
}
if (isNaN(offeringPrice) || offeringPrice <= 0) {
alert('Please enter a valid offering price');
return;
}
if (isNaN(expectedPurchasePrice) || expectedPurchasePrice <= 0) {
alert('Please enter a valid purchase price');
return;
}
const period = getNextOfferingPeriod(joinDate);
if (!period) {
alert('Could not determine offering period');
return;
}
// Calculate ESPP details
const esppDetails = calculateESPPDetails(
joinDate,
contributionAmount,
offeringPrice,
expectedPurchasePrice,
period
);
// Display results
displayResults(esppDetails);
}
// Update calculateESPPDetails to work with direct contribution amount
function calculateESPPDetails(joinDate, contributionAmount, offeringPrice, currentPrice, period) {
// Calculate purchase price (15% discount off the lower of lookback or current price)
const lowerPrice = Math.min(offeringPrice, currentPrice);
const discountedPurchasePrice = lowerPrice * 0.85;
// Check $25,000 annual limit
const annualLimit = 25000;
const maxSharesPerYear = Math.floor(annualLimit / offeringPrice);
// Calculate shares received (rounded down to nearest whole share)
let sharesReceived = Math.floor(contributionAmount / discountedPurchasePrice);
// Apply annual limit if necessary
if (sharesReceived > maxSharesPerYear) {
sharesReceived = maxSharesPerYear;
contributionAmount = maxSharesPerYear * discountedPurchasePrice;
}
// Calculate market value of shares at current price
const marketValue = sharesReceived * currentPrice;
// Calculate potential gain
const potentialGain = marketValue - contributionAmount;
// Calculate percentage return
const percentageReturn = (potentialGain / contributionAmount) * 100;
return {
nextOfferingPeriod: {
start: period.start,
end: period.end
},
purchaseDate: period.purchaseDates[0],
lookbackPrice: offeringPrice,
currentPrice: currentPrice,
purchasePrice: discountedPurchasePrice,
contributionAmount: contributionAmount,
sharesReceived: sharesReceived,
marketValue: marketValue,
potentialGain: potentialGain,
percentageReturn: percentageReturn
};
}
// Function to display results
function displayResults(esppDetails) {
// Format dates
const formatDate = (date) => {
return date.toLocaleDateString('en-US', {
year: 'numeric',
month: 'long',
day: 'numeric'
});
};
// Format currency
const formatCurrency = (amount) => {
return new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD'
}).format(amount);
};
// Format percentage
const formatPercentage = (percent) => {
return new Intl.NumberFormat('en-US', {
style: 'percent',
minimumFractionDigits: 2,
maximumFractionDigits: 2
}).format(percent / 100);
};
// Update result elements
nextOfferingValue.textContent = `${formatDate(esppDetails.nextOfferingPeriod.start)} to ${formatDate(esppDetails.nextOfferingPeriod.end)}`;
purchaseDateValue.textContent = formatDate(esppDetails.purchaseDate);
lookbackPriceValue.textContent = formatCurrency(esppDetails.lookbackPrice);
currentPriceValue.textContent = formatCurrency(esppDetails.currentPrice);
purchasePriceValue.textContent = formatCurrency(esppDetails.purchasePrice);
contributionValue.textContent = formatCurrency(esppDetails.contributionAmount);
sharesReceivedValue.textContent = esppDetails.sharesReceived.toFixed(0);
marketValueValue.textContent = formatCurrency(esppDetails.marketValue);
potentialGainValue.textContent = formatCurrency(esppDetails.potentialGain);
percentageReturnValue.textContent = formatPercentage(esppDetails.percentageReturn);
}
});