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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>Calculator</title>
|
| 7 |
+
<style>
|
| 8 |
+
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');
|
| 9 |
+
|
| 10 |
+
* {
|
| 11 |
+
box-sizing: border-box;
|
| 12 |
+
margin: 0;
|
| 13 |
+
padding: 0;
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
body {
|
| 17 |
+
font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
| 18 |
+
background: linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
|
| 19 |
+
min-height: 100vh;
|
| 20 |
+
display: flex;
|
| 21 |
+
align-items: center;
|
| 22 |
+
justify-content: center;
|
| 23 |
+
padding: 1rem;
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
.container {
|
| 27 |
+
width: 100%;
|
| 28 |
+
max-width: 28rem;
|
| 29 |
+
animation: scaleIn 0.2s ease-out;
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
@keyframes scaleIn {
|
| 33 |
+
0% { transform: scale(0.95); opacity: 0; }
|
| 34 |
+
100% { transform: scale(1); opacity: 1; }
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
@keyframes fadeIn {
|
| 38 |
+
0% { opacity: 0; }
|
| 39 |
+
100% { opacity: 1; }
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
/* Header */
|
| 43 |
+
.header {
|
| 44 |
+
text-align: center;
|
| 45 |
+
margin-bottom: 2rem;
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
.title {
|
| 49 |
+
font-size: 2rem;
|
| 50 |
+
font-weight: 700;
|
| 51 |
+
background: linear-gradient(to right, #22d3ee, #c084fc, #f472b6);
|
| 52 |
+
-webkit-background-clip: text;
|
| 53 |
+
background-clip: text;
|
| 54 |
+
color: transparent;
|
| 55 |
+
margin-bottom: 0.5rem;
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
.built-with {
|
| 59 |
+
font-size: 0.75rem;
|
| 60 |
+
color: #94a3b8;
|
| 61 |
+
transition: color 0.2s;
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
.built-with a {
|
| 65 |
+
color: inherit;
|
| 66 |
+
text-decoration: none;
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
.built-with a:hover {
|
| 70 |
+
color: #22d3ee;
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
/* Calculator Body */
|
| 74 |
+
.calculator {
|
| 75 |
+
background: linear-gradient(to bottom, #1e293b, #0f172a);
|
| 76 |
+
border-radius: 1.5rem;
|
| 77 |
+
padding: 1rem;
|
| 78 |
+
box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
|
| 79 |
+
border: 1px solid rgba(71, 85, 105, 0.3);
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
/* Display Screen */
|
| 83 |
+
.display {
|
| 84 |
+
background: linear-gradient(to bottom, #0f172a, #1e293b);
|
| 85 |
+
border-radius: 1rem;
|
| 86 |
+
padding: 1rem;
|
| 87 |
+
margin-bottom: 1rem;
|
| 88 |
+
box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.3);
|
| 89 |
+
border: 1px solid rgba(71, 85, 105, 0.2);
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
.display-previous {
|
| 93 |
+
text-align: right;
|
| 94 |
+
font-size: 0.875rem;
|
| 95 |
+
color: #64748b;
|
| 96 |
+
margin-bottom: 0.25rem;
|
| 97 |
+
min-height: 1.25rem;
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
.display-current {
|
| 101 |
+
text-align: right;
|
| 102 |
+
font-size: 2.5rem;
|
| 103 |
+
font-weight: 700;
|
| 104 |
+
color: white;
|
| 105 |
+
overflow: hidden;
|
| 106 |
+
text-overflow: ellipsis;
|
| 107 |
+
white-space: nowrap;
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
/* History Strip */
|
| 111 |
+
.history {
|
| 112 |
+
margin-bottom: 1rem;
|
| 113 |
+
padding: 0 0.5rem;
|
| 114 |
+
animation: fadeIn 0.3s ease-out;
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
.history-items {
|
| 118 |
+
display: flex;
|
| 119 |
+
gap: 0.25rem;
|
| 120 |
+
overflow-x: auto;
|
| 121 |
+
padding-bottom: 0.5rem;
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
.history-items::-webkit-scrollbar {
|
| 125 |
+
display: none;
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
.history-item {
|
| 129 |
+
font-size: 0.75rem;
|
| 130 |
+
color: #64748b;
|
| 131 |
+
background: rgba(30, 41, 59, 0.5);
|
| 132 |
+
padding: 0.25rem 0.5rem;
|
| 133 |
+
border-radius: 0.5rem;
|
| 134 |
+
white-space: nowrap;
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
/* Button Grid */
|
| 138 |
+
.buttons {
|
| 139 |
+
display: grid;
|
| 140 |
+
grid-template-columns: repeat(4, 1fr);
|
| 141 |
+
gap: 0.5rem;
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
.btn {
|
| 145 |
+
height: 3.5rem;
|
| 146 |
+
border-radius: 0.75rem;
|
| 147 |
+
font-weight: 600;
|
| 148 |
+
font-size: 1.125rem;
|
| 149 |
+
display: flex;
|
| 150 |
+
align-items: center;
|
| 151 |
+
justify-content: center;
|
| 152 |
+
transition: all 0.15s ease;
|
| 153 |
+
border: none;
|
| 154 |
+
cursor: pointer;
|
| 155 |
+
}
|
| 156 |
+
|
| 157 |
+
.btn:active {
|
| 158 |
+
transform: scale(0.95);
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
.btn:hover {
|
| 162 |
+
transform: scale(1.05);
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
.btn-number {
|
| 166 |
+
background: linear-gradient(to bottom, #e2e8f0, #cbd5e1);
|
| 167 |
+
color: #1e293b;
|
| 168 |
+
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
|
| 169 |
+
}
|
| 170 |
+
|
| 171 |
+
.btn-number:hover {
|
| 172 |
+
box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
|
| 173 |
+
}
|
| 174 |
+
|
| 175 |
+
.btn-operator {
|
| 176 |
+
background: linear-gradient(to bottom, #f97316, #ea580c);
|
| 177 |
+
color: white;
|
| 178 |
+
box-shadow: 0 4px 6px -1px rgba(249, 115, 22, 0.3);
|
| 179 |
+
}
|
| 180 |
+
|
| 181 |
+
.btn-operator:hover {
|
| 182 |
+
box-shadow: 0 10px 15px -3px rgba(249, 115, 22, 0.4);
|
| 183 |
+
}
|
| 184 |
+
|
| 185 |
+
.btn-equals {
|
| 186 |
+
background: linear-gradient(to bottom, #10b981, #059669);
|
| 187 |
+
color: white;
|
| 188 |
+
box-shadow: 0 4px 6px -1px rgba(16, 185, 129, 0.3);
|
| 189 |
+
}
|
| 190 |
+
|
| 191 |
+
.btn-equals:hover {
|
| 192 |
+
box-shadow: 0 10px 15px -3px rgba(16, 185, 129, 0.4);
|
| 193 |
+
}
|
| 194 |
+
|
| 195 |
+
.btn-function {
|
| 196 |
+
background: linear-gradient(to bottom, #475569, #334155);
|
| 197 |
+
color: white;
|
| 198 |
+
box-shadow: 0 4px 6px -1px rgba(71, 85, 105, 0.3);
|
| 199 |
+
}
|
| 200 |
+
|
| 201 |
+
.btn-function:hover {
|
| 202 |
+
box-shadow: 0 10px 15px -3px rgba(71, 85, 105, 0.4);
|
| 203 |
+
}
|
| 204 |
+
|
| 205 |
+
.btn-wide {
|
| 206 |
+
grid-column: span 2;
|
| 207 |
+
}
|
| 208 |
+
|
| 209 |
+
/* Footer */
|
| 210 |
+
.footer {
|
| 211 |
+
text-align: center;
|
| 212 |
+
color: #64748b;
|
| 213 |
+
font-size: 0.75rem;
|
| 214 |
+
margin-top: 1.5rem;
|
| 215 |
+
}
|
| 216 |
+
|
| 217 |
+
/* Responsive */
|
| 218 |
+
@media (min-width: 640px) {
|
| 219 |
+
.btn {
|
| 220 |
+
height: 4rem;
|
| 221 |
+
}
|
| 222 |
+
|
| 223 |
+
.display-current {
|
| 224 |
+
font-size: 3rem;
|
| 225 |
+
}
|
| 226 |
+
|
| 227 |
+
.buttons {
|
| 228 |
+
gap: 0.75rem;
|
| 229 |
+
}
|
| 230 |
+
}
|
| 231 |
+
</style>
|
| 232 |
+
</head>
|
| 233 |
+
<body>
|
| 234 |
+
<div class="container">
|
| 235 |
+
<!-- Header -->
|
| 236 |
+
<div class="header">
|
| 237 |
+
<h1 class="title">Calculator</h1>
|
| 238 |
+
<p class="built-with">
|
| 239 |
+
<a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" rel="noopener noreferrer">
|
| 240 |
+
Built with anycoder
|
| 241 |
+
</a>
|
| 242 |
+
</p>
|
| 243 |
+
</div>
|
| 244 |
+
|
| 245 |
+
<!-- Calculator -->
|
| 246 |
+
<div class="calculator">
|
| 247 |
+
<!-- Display -->
|
| 248 |
+
<div class="display">
|
| 249 |
+
<div class="display-previous" id="previousDisplay"></div>
|
| 250 |
+
<div class="display-current" id="currentDisplay">0</div>
|
| 251 |
+
</div>
|
| 252 |
+
|
| 253 |
+
<!-- History -->
|
| 254 |
+
<div class="history" id="historyContainer" style="display: none;">
|
| 255 |
+
<div class="history-items" id="historyItems"></div>
|
| 256 |
+
</div>
|
| 257 |
+
|
| 258 |
+
<!-- Buttons -->
|
| 259 |
+
<div class="buttons">
|
| 260 |
+
<button class="btn btn-function" data-action="clear">AC</button>
|
| 261 |
+
<button class="btn btn-function" data-action="toggleSign">±</button>
|
| 262 |
+
<button class="btn btn-function" data-action="percentage">%</button>
|
| 263 |
+
<button class="btn btn-operator" data-action="divide">÷</button>
|
| 264 |
+
|
| 265 |
+
<button class="btn btn-number" data-digit="7">7</button>
|
| 266 |
+
<button class="btn btn-number" data-digit="8">8</button>
|
| 267 |
+
<button class="btn btn-number" data-digit="9">9</button>
|
| 268 |
+
<button class="btn btn-operator" data-action="multiply">×</button>
|
| 269 |
+
|
| 270 |
+
<button class="btn btn-number" data-digit="4">4</button>
|
| 271 |
+
<button class="btn btn-number" data-digit="5">5</button>
|
| 272 |
+
<button class="btn btn-number" data-digit="6">6</button>
|
| 273 |
+
<button class="btn btn-operator" data-action="subtract">-</button>
|
| 274 |
+
|
| 275 |
+
<button class="btn btn-number" data-digit="1">1</button>
|
| 276 |
+
<button class="btn btn-number" data-digit="2">2</button>
|
| 277 |
+
<button class="btn btn-number" data-digit="3">3</button>
|
| 278 |
+
<button class="btn btn-operator" data-action="add">+</button>
|
| 279 |
+
|
| 280 |
+
<button class="btn btn-number btn-wide" data-digit="0">0</button>
|
| 281 |
+
<button class="btn btn-number" data-action="decimal">.</button>
|
| 282 |
+
<button class="btn btn-equals" data-action="equals">=</button>
|
| 283 |
+
</div>
|
| 284 |
+
</div>
|
| 285 |
+
|
| 286 |
+
<!-- Footer -->
|
| 287 |
+
<p class="footer">A beautiful calculator built with HTML, CSS & JavaScript</p>
|
| 288 |
+
</div>
|
| 289 |
+
|
| 290 |
+
<script>
|
| 291 |
+
// Calculator State
|
| 292 |
+
const state = {
|
| 293 |
+
display: '0',
|
| 294 |
+
previousValue: null,
|
| 295 |
+
operation: null,
|
| 296 |
+
waitingForOperand: false,
|
| 297 |
+
history: []
|
| 298 |
+
};
|
| 299 |
+
|
| 300 |
+
// DOM Elements
|
| 301 |
+
const currentDisplayEl = document.getElementById('currentDisplay');
|
| 302 |
+
const previousDisplayEl = document.getElementById('previousDisplay');
|
| 303 |
+
const historyContainer = document.getElementById('historyContainer');
|
| 304 |
+
const historyItems = document.getElementById('historyItems');
|
| 305 |
+
|
| 306 |
+
// Update Display
|
| 307 |
+
function updateDisplay() {
|
| 308 |
+
currentDisplayEl.textContent = state.display;
|
| 309 |
+
|
| 310 |
+
if (state.previousValue !== null && state.operation) {
|
| 311 |
+
previousDisplayEl.textContent = `${state.previousValue} ${state.operation}`;
|
| 312 |
+
} else {
|
| 313 |
+
previousDisplayEl.textContent = '';
|
| 314 |
+
}
|
| 315 |
+
}
|
| 316 |
+
|
| 317 |
+
// Update History
|
| 318 |
+
function updateHistory() {
|
| 319 |
+
if (state.history.length > 0) {
|
| 320 |
+
historyContainer.style.display = 'block';
|
| 321 |
+
historyItems.innerHTML = state.history
|
| 322 |
+
.map(item => `<span class="history-item">${item}</span>`)
|
| 323 |
+
.join('');
|
| 324 |
+
} else {
|
| 325 |
+
historyContainer.style.display = 'none';
|
| 326 |
+
}
|
| 327 |
+
}
|
| 328 |
+
|
| 329 |
+
// Input Digit
|
| 330 |
+
function inputDigit(digit) {
|
| 331 |
+
if (state.waitingForOperand) {
|
| 332 |
+
state.display = digit;
|
| 333 |
+
state.waitingForOperand = false;
|
| 334 |
+
} else {
|
| 335 |
+
state.display = state.display === '0' ? digit : state.display + digit;
|
| 336 |
+
}
|
| 337 |
+
updateDisplay();
|
| 338 |
+
}
|
| 339 |
+
|
| 340 |
+
// Input Decimal
|
| 341 |
+
function inputDecimal() {
|
| 342 |
+
if (state.waitingForOperand) {
|
| 343 |
+
state.display = '0.';
|
| 344 |
+
state.waitingForOperand = false;
|
| 345 |
+
} else if (!state.display.includes('.')) {
|
| 346 |
+
state.display += '.';
|
| 347 |
+
}
|
| 348 |
+
updateDisplay();
|
| 349 |
+
}
|
| 350 |
+
|
| 351 |
+
// Clear
|
| 352 |
+
function clear() {
|
| 353 |
+
state.display = '0';
|
| 354 |
+
state.previousValue = null;
|
| 355 |
+
state.operation = null;
|
| 356 |
+
state.waitingForOperand = false;
|
| 357 |
+
updateDisplay();
|
| 358 |
+
previousDisplayEl.textContent = '';
|
| 359 |
+
}
|
| 360 |
+
|
| 361 |
+
// Toggle Sign
|
| 362 |
+
function toggleSign() {
|
| 363 |
+
state.display = String(-parseFloat(state.display));
|
| 364 |
+
updateDisplay();
|
| 365 |
+
}
|
| 366 |
+
|
| 367 |
+
// Percentage
|
| 368 |
+
function percentage() {
|
| 369 |
+
state.display = String(parseFloat(state.display) / 100);
|
| 370 |
+
updateDisplay();
|
| 371 |
+
}
|
| 372 |
+
|
| 373 |
+
// Perform Operation
|
| 374 |
+
function performOperation(nextOperation) {
|
| 375 |
+
const inputValue = parseFloat(state.display);
|
| 376 |
+
|
| 377 |
+
if (state.previousValue === null) {
|
| 378 |
+
state.previousValue = inputValue;
|
| 379 |
+
} else if (state.operation) {
|
| 380 |
+
const currentValue = state.previousValue || 0;
|
| 381 |
+
let result;
|
| 382 |
+
|
| 383 |
+
switch (state.operation) {
|
| 384 |
+
case '+':
|
| 385 |
+
result = currentValue + inputValue;
|
| 386 |
+
break;
|
| 387 |
+
case '-':
|
| 388 |
+
result = currentValue - inputValue;
|
| 389 |
+
break;
|
| 390 |
+
case '×':
|
| 391 |
+
result = currentValue * inputValue;
|
| 392 |
+
break;
|
| 393 |
+
case '÷':
|
| 394 |
+
result = currentValue / inputValue;
|
| 395 |
+
break;
|
| 396 |
+
default:
|
| 397 |
+
result = inputValue;
|
| 398 |
+
}
|
| 399 |
+
|
| 400 |
+
state.display = String(result);
|
| 401 |
+
state.previousValue = result;
|
| 402 |
+
|
| 403 |
+
// Add to history
|
| 404 |
+
addToHistory(`${currentValue} ${state.operation} ${inputValue} = ${result}`);
|
| 405 |
+
}
|
| 406 |
+
|
| 407 |
+
state.waitingForOperand = true;
|
| 408 |
+
state.operation = nextOperation;
|
| 409 |
+
updateDisplay();
|
| 410 |
+
}
|
| 411 |
+
|
| 412 |
+
// Calculate
|
| 413 |
+
function calculate() {
|
| 414 |
+
if (!state.operation || state.previousValue === null) return;
|
| 415 |
+
|
| 416 |
+
const inputValue = parseFloat(state.display);
|
| 417 |
+
let result;
|
| 418 |
+
|
| 419 |
+
switch (state.operation) {
|
| 420 |
+
case '+':
|
| 421 |
+
result = state.previousValue + inputValue;
|
| 422 |
+
break;
|
| 423 |
+
case '-':
|
| 424 |
+
result = state.previousValue - inputValue;
|
| 425 |
+
break;
|
| 426 |
+
case '×':
|
| 427 |
+
result = state.previousValue * inputValue;
|
| 428 |
+
break;
|
| 429 |
+
case '÷':
|
| 430 |
+
result = state.previousValue / inputValue;
|
| 431 |
+
break;
|
| 432 |
+
default:
|
| 433 |
+
result = inputValue;
|
| 434 |
+
}
|
| 435 |
+
|
| 436 |
+
addToHistory(`${state.previousValue} ${state.operation} ${inputValue} = ${result}`);
|
| 437 |
+
state.display = String(result);
|
| 438 |
+
state.previousValue = null;
|
| 439 |
+
state.operation = null;
|
| 440 |
+
state.waitingForOperand = true;
|
| 441 |
+
updateDisplay();
|
| 442 |
+
}
|
| 443 |
+
|
| 444 |
+
// Add to History
|
| 445 |
+
function addToHistory(item) {
|
| 446 |
+
state.history = [...state.history, item].slice(-5);
|
| 447 |
+
updateHistory();
|
| 448 |
+
}
|
| 449 |
+
|
| 450 |
+
// Handle Button Clicks
|
| 451 |
+
document.querySelectorAll('.btn').forEach(button => {
|
| 452 |
+
button.addEventListener('click', () => {
|
| 453 |
+
const digit = button.dataset.digit;
|
| 454 |
+
const action = button.dataset.action;
|
| 455 |
+
|
| 456 |
+
if (digit !== undefined) {
|
| 457 |
+
inputDigit(digit);
|
| 458 |
+
} else if (action) {
|
| 459 |
+
switch (action) {
|
| 460 |
+
case 'clear':
|
| 461 |
+
clear();
|
| 462 |
+
break;
|
| 463 |
+
case 'toggleSign':
|
| 464 |
+
toggleSign();
|
| 465 |
+
break;
|
| 466 |
+
case 'percentage':
|
| 467 |
+
percentage();
|
| 468 |
+
break;
|
| 469 |
+
case 'decimal':
|
| 470 |
+
inputDecimal();
|
| 471 |
+
break;
|
| 472 |
+
case 'add':
|
| 473 |
+
performOperation('+');
|
| 474 |
+
break;
|
| 475 |
+
case 'subtract':
|
| 476 |
+
performOperation('-');
|
| 477 |
+
break;
|
| 478 |
+
case 'multiply':
|
| 479 |
+
performOperation('×');
|
| 480 |
+
break;
|
| 481 |
+
case 'divide':
|
| 482 |
+
performOperation('÷');
|
| 483 |
+
break;
|
| 484 |
+
case 'equals':
|
| 485 |
+
calculate();
|
| 486 |
+
break;
|
| 487 |
+
}
|
| 488 |
+
}
|
| 489 |
+
});
|
| 490 |
+
});
|
| 491 |
+
|
| 492 |
+
// Keyboard Support
|
| 493 |
+
document.addEventListener('keydown', (e) => {
|
| 494 |
+
const key = e.key;
|
| 495 |
+
|
| 496 |
+
if (key >= '0' && key <= '9') {
|
| 497 |
+
inputDigit(key);
|
| 498 |
+
} else if (key === '.') {
|
| 499 |
+
inputDecimal();
|
| 500 |
+
} else if (key === '+') {
|
| 501 |
+
performOperation('+');
|
| 502 |
+
} else if (key === '-') {
|
| 503 |
+
performOperation('-');
|
| 504 |
+
} else if (key === '*') {
|
| 505 |
+
performOperation('×');
|
| 506 |
+
} else if (key === '/') {
|
| 507 |
+
e.preventDefault();
|
| 508 |
+
performOperation('÷');
|
| 509 |
+
} else if (key === 'Enter' || key === '=') {
|
| 510 |
+
calculate();
|
| 511 |
+
} else if (key === 'Escape') {
|
| 512 |
+
clear();
|
| 513 |
+
} else if (key === 'Backspace') {
|
| 514 |
+
if (!state.waitingForOperand && state.display.length > 1) {
|
| 515 |
+
state.display = state.display.slice(0, -1);
|
| 516 |
+
} else {
|
| 517 |
+
state.display = '0';
|
| 518 |
+
}
|
| 519 |
+
updateDisplay();
|
| 520 |
+
}
|
| 521 |
+
});
|
| 522 |
+
</script>
|
| 523 |
+
</body>
|
| 524 |
+
</html>
|