Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
474 lines (404 loc) · 14.1 KB
/
Copy pathscript.js
File metadata and controls
474 lines (404 loc) · 14.1 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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
// Emergency Contact Page JavaScript
document.addEventListener('DOMContentLoaded', function() {
initializePage();
});
// Initialize page functionality
function initializePage() {
updateAge();
updateLastUpdatedTimestamp();
setupPhotoHandling();
addAccessibilityFeatures();
}
// Calculate and update age
function updateAge() {
const birthDate = new Date('1985-03-15');
const today = new Date();
let age = today.getFullYear() - birthDate.getFullYear();
const monthDiff = today.getMonth() - birthDate.getMonth();
if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
const ageElement = document.getElementById('age');
if (ageElement) {
ageElement.textContent = age;
}
}
// Update timestamp functionality
function updateTimestamp() {
const now = new Date();
const options = {
year: 'numeric',
month: 'long',
day: 'numeric',
hour: '2-digit',
minute: '2-digit'
};
const timestamp = now.toLocaleDateString('en-US', options);
const lastUpdatedElement = document.getElementById('lastUpdated');
if (lastUpdatedElement) {
lastUpdatedElement.textContent = timestamp;
localStorage.setItem('emergencyContactLastUpdated', timestamp);
// Show confirmation
showNotification('Timestamp updated successfully!', 'success');
}
}
// Initialize last updated timestamp
function updateLastUpdatedTimestamp() {
const savedTimestamp = localStorage.getItem('emergencyContactLastUpdated');
const lastUpdatedElement = document.getElementById('lastUpdated');
if (lastUpdatedElement) {
if (savedTimestamp) {
lastUpdatedElement.textContent = savedTimestamp;
} else {
updateTimestamp(); // Set initial timestamp
}
}
}
// Print functionality
function printPage() {
// Hide interactive elements before printing
const interactiveElements = document.querySelectorAll(
'.print-btn, .photo-update-btn, .quick-call-btn, .emergency-call-btn, .service-btn, .update-btn'
);
interactiveElements.forEach(element => {
element.style.display = 'none';
});
// Add print-specific styles
const printStyles = document.createElement('style');
printStyles.innerHTML = `
@media print {
.emergency-header {
background: #dc2626 !important;
-webkit-print-color-adjust: exact;
color-adjust: exact;
}
.info-card {
page-break-inside: avoid;
margin-bottom: 1rem;
}
}
`;
document.head.appendChild(printStyles);
window.print();
// Restore interactive elements after printing
setTimeout(() => {
interactiveElements.forEach(element => {
element.style.display = '';
});
document.head.removeChild(printStyles);
}, 1000);
}
// Call functionality
function callNumber(phoneNumber) {
if ('navigator' in window && 'vibrate' in navigator) {
navigator.vibrate(200);
}
window.location.href = `tel:${phoneNumber}`;
// Log the call attempt
logCallAttempt(phoneNumber);
}
// Emergency call with confirmation
function emergencyCall(phoneNumber, contactName) {
const modal = document.getElementById('callModal');
const contactNameElement = document.getElementById('contactName');
const modalPhoneElement = document.getElementById('modalPhone');
if (modal && contactNameElement && modalPhoneElement) {
contactNameElement.textContent = contactName;
modalPhoneElement.textContent = formatPhoneNumber(phoneNumber);
modal.style.display = 'block';
// Store current call info for confirmation
modal.dataset.phoneNumber = phoneNumber;
modal.dataset.contactName = contactName;
}
}
// Confirm emergency call
function confirmCall() {
const modal = document.getElementById('callModal');
const phoneNumber = modal.dataset.phoneNumber;
const contactName = modal.dataset.contactName;
if (phoneNumber) {
callNumber(phoneNumber);
logEmergencyCall(phoneNumber, contactName);
}
closeModal();
}
// Close modal
function closeModal() {
const modal = document.getElementById('callModal');
if (modal) {
modal.style.display = 'none';
}
}
// Format phone number for display
function formatPhoneNumber(phoneNumber) {
// Remove all non-digit characters
const cleaned = phoneNumber.replace(/\D/g, '');
// Handle different phone number formats
if (cleaned.length === 10) {
return `(${cleaned.slice(0, 3)}) ${cleaned.slice(3, 6)}-${cleaned.slice(6)}`;
} else if (cleaned.length === 11 && cleaned[0] === '1') {
return `+1 (${cleaned.slice(1, 4)}) ${cleaned.slice(4, 7)}-${cleaned.slice(7)}`;
} else if (cleaned === '911') {
return '911';
}
return phoneNumber; // Return original if formatting fails
}
// Log call attempts for emergency tracking
function logCallAttempt(phoneNumber) {
const callLog = JSON.parse(localStorage.getItem('emergencyCallLog') || '[]');
const timestamp = new Date().toISOString();
callLog.push({
phoneNumber: phoneNumber,
timestamp: timestamp,
type: 'call_attempt'
});
// Keep only last 50 entries
if (callLog.length > 50) {
callLog.splice(0, callLog.length - 50);
}
localStorage.setItem('emergencyCallLog', JSON.stringify(callLog));
}
// Log emergency calls specifically
function logEmergencyCall(phoneNumber, contactName) {
const callLog = JSON.parse(localStorage.getItem('emergencyCallLog') || '[]');
const timestamp = new Date().toISOString();
callLog.push({
phoneNumber: phoneNumber,
contactName: contactName,
timestamp: timestamp,
type: 'emergency_call'
});
localStorage.setItem('emergencyCallLog', JSON.stringify(callLog));
}
// Photo handling functionality
function setupPhotoHandling() {
// Create file input for photo updates
const fileInput = document.createElement('input');
fileInput.type = 'file';
fileInput.accept = 'image/*';
fileInput.style.display = 'none';
fileInput.id = 'photoInput';
fileInput.addEventListener('change', function(event) {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function(e) {
const profileImage = document.getElementById('profileImage');
if (profileImage) {
profileImage.src = e.target.result;
// Save to localStorage for persistence
localStorage.setItem('emergencyContactPhoto', e.target.result);
showNotification('Photo updated successfully!', 'success');
}
};
reader.readAsDataURL(file);
}
});
document.body.appendChild(fileInput);
// Load saved photo if exists
const savedPhoto = localStorage.getItem('emergencyContactPhoto');
if (savedPhoto) {
const profileImage = document.getElementById('profileImage');
if (profileImage) {
profileImage.src = savedPhoto;
}
}
}
// Show photo options
function showPhotoOptions() {
const options = [
'Upload new photo',
'Take photo with camera',
'Reset to default'
];
const choice = prompt(`Choose an option:\n1. ${options[0]}\n2. ${options[1]}\n3. ${options[2]}\n\nEnter 1, 2, or 3:`);
switch(choice) {
case '1':
document.getElementById('photoInput').click();
break;
case '2':
openCamera();
break;
case '3':
resetPhoto();
break;
default:
break;
}
}
// Open camera for photo capture
function openCamera() {
if ('mediaDevices' in navigator && 'getUserMedia' in navigator.mediaDevices) {
// Create camera modal
const cameraModal = createCameraModal();
document.body.appendChild(cameraModal);
} else {
alert('Camera access is not supported in this browser. Please use the upload option instead.');
}
}
// Create camera modal
function createCameraModal() {
const modal = document.createElement('div');
modal.className = 'modal';
modal.style.display = 'block';
modal.innerHTML = `
<div class="modal-content" style="max-width: 600px;">
<h3>Take Photo</h3>
<video id="cameraVideo" width="400" height="300" autoplay style="border-radius: 8px;"></video>
<div style="margin-top: 1rem;">
<button onclick="capturePhoto()" class="confirm-btn">📸 Capture</button>
<button onclick="closeCameraModal()" class="cancel-btn">Cancel</button>
</div>
</div>
`;
// Start camera
navigator.mediaDevices.getUserMedia({ video: true })
.then(stream => {
const video = modal.querySelector('#cameraVideo');
video.srcObject = stream;
modal.stream = stream;
})
.catch(err => {
console.error('Error accessing camera:', err);
alert('Could not access camera. Please use the upload option instead.');
document.body.removeChild(modal);
});
return modal;
}
// Capture photo from camera
function capturePhoto() {
const video = document.getElementById('cameraVideo');
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
ctx.drawImage(video, 0, 0);
const dataURL = canvas.toDataURL('image/jpeg', 0.8);
// Update profile image
const profileImage = document.getElementById('profileImage');
if (profileImage) {
profileImage.src = dataURL;
localStorage.setItem('emergencyContactPhoto', dataURL);
}
closeCameraModal();
showNotification('Photo captured successfully!', 'success');
}
// Close camera modal
function closeCameraModal() {
const modals = document.querySelectorAll('.modal');
modals.forEach(modal => {
if (modal.stream) {
const tracks = modal.stream.getTracks();
tracks.forEach(track => track.stop());
}
document.body.removeChild(modal);
});
}
// Reset photo to default
function resetPhoto() {
const profileImage = document.getElementById('profileImage');
if (profileImage) {
profileImage.src = 'https://images.pexels.com/photos/1239291/pexels-photo-1239291.jpeg?auto=compress&cs=tinysrgb&w=400&h=400&fit=crop';
localStorage.removeItem('emergencyContactPhoto');
showNotification('Photo reset to default', 'info');
}
}
// Show notification
function showNotification(message, type = 'info') {
const notification = document.createElement('div');
notification.className = 'notification';
notification.textContent = message;
const colors = {
success: '#10b981',
error: '#ef4444',
warning: '#f59e0b',
info: '#3b82f6'
};
notification.style.cssText = `
position: fixed;
top: 100px;
right: 20px;
background: ${colors[type]};
color: white;
padding: 1rem 1.5rem;
border-radius: 8px;
box-shadow: 0 8px 20px rgba(0,0,0,0.2);
z-index: 10000;
font-weight: 600;
animation: slideIn 0.3s ease;
`;
document.body.appendChild(notification);
setTimeout(() => {
notification.style.animation = 'slideOut 0.3s ease forwards';
setTimeout(() => {
if (document.body.contains(notification)) {
document.body.removeChild(notification);
}
}, 300);
}, 3000);
}
// Add accessibility features
function addAccessibilityFeatures() {
// Add keyboard navigation
document.addEventListener('keydown', function(event) {
if (event.ctrlKey || event.metaKey) {
switch(event.key) {
case 'p':
event.preventDefault();
printPage();
break;
case '1':
event.preventDefault();
emergencyCall('911', 'Emergency Services');
break;
case '2':
event.preventDefault();
emergencyCall('+15553456789', 'David Johnson');
break;
}
}
if (event.key === 'Escape') {
closeModal();
closeCameraModal();
}
});
// Add focus management
const interactiveElements = document.querySelectorAll('button, a[href^="tel:"]');
interactiveElements.forEach((element, index) => {
element.setAttribute('tabindex', index + 1);
});
}
// Close modal when clicking outside
window.addEventListener('click', function(event) {
const modal = document.getElementById('callModal');
if (event.target === modal) {
closeModal();
}
});
// Add notification animations
const style = document.createElement('style');
style.textContent = `
@keyframes slideIn {
from { transform: translateX(100%); opacity: 0; }
to { transform: translateX(0); opacity: 1; }
}
@keyframes slideOut {
from { transform: translateX(0); opacity: 1; }
to { transform: translateX(100%); opacity: 0; }
}
`;
document.head.appendChild(style);
// Service Worker registration for offline functionality
if ('serviceWorker' in navigator) {
window.addEventListener('load', function() {
navigator.serviceWorker.register('/sw.js')
.then(function(registration) {
console.log('SW registered: ', registration);
})
.catch(function(registrationError) {
console.log('SW registration failed: ', registrationError);
});
});
}
// Emergency contact page ready
console.log('Emergency Contact Information Page loaded successfully');