https://ahopetodream.com/wp-content/themes/ahopetodream_theme_verified/js/nomination.js?v=1757081505
1"use strict"; 2 3(function (htd) { 4 htd.nomination = htd.nomination || {}; 5 6 htd.nomination.submitNominationWithConfirmedAddresses = function () { 7 const childAddressRevision = __HTD.nomination.getAddressRevision('child'); 8 const nominatorAddressRevision = __HTD.nomination.getAddressRevision('nominator'); 9 10 if (childAddressRevision === null) { 11 __HTD.toast.show({ id: 'toast', title: __HTD.translations.translate('missing-child-address-revision'), type: 'error' }); 12 return; 13 } 14 15 if (nominatorAddressRevision === null) { 16 __HTD.toast.show({ id: 'toast', title: __HTD.translations.translate('missing-nominator-address-revision'), type: 'error' }); 17 return; 18 } 19 20 __HTD.modal.hide('confirm-addresses'); 21 __HTD.nomination.submitNomination(null, childAddressRevision, nominatorAddressRevision); 22 }; 23 24 htd.nomination.getAddressRevision = function (addressType) { 25 const addressConfirmation = document.querySelector(`.${addressType}-address-confirmation`); 26 27 if (addressConfirmation.classList.contains('hidden')) 28 // The address type is not being revised. 29 return; 30 31 const originalContainer = addressConfirmation.querySelector(`.${addressType}-original-container`); 32 33 if (originalContainer.classList.contains('bg-primary')) 34 // Original address was selected. 35 // Return true, indicating to use the address as is. 36 return true; 37 38 const revisedContainer = addressConfirmation.querySelector(`.${addressType}-revised-container`); 39 const revised = revisedContainer.querySelector(`.${addressType}-revised`); 40 const revisedClasses = revised.classList; 41 42 if (!revisedClasses.contains('hidden')) 43 return revised.revisedAddress; 44 45 // Assume the user did not make a selection. 46 return null; 47 }; 48 49 htd.nomination.submitNomination = function (event, childAddressRevision, nominatorAddressRevision) { 50 const formId = 'nomination-form'; 51 52 __HTD.nomination.disable(formId); 53 54 event?.preventDefault(); 55 56 const childAddress = { 57 type: 'Child' 58 }; 59 60 if (typeof childAddressRevision === 'object') { 61 childAddress.values = childAddressRevision; 62 } else { 63 childAddress.values = { 64 streetAddress1: document.getElementById('child-street-address').value, 65 city: document.getElementById('child-city').value, 66 postalCode: document.getElementById('child-postal-code').value 67 }; 68 69 __HTD.nomination.setRegion(childAddress, 'child-state', 'child-province'); 70 } 71 72 const nominatorAddress = { 73 type: 'Nominator' 74 }; 75 76 if (typeof nominatorAddressRevision === 'object') { 77 nominatorAddress.values = nominatorAddressRevision; 78 } else { 79 nominatorAddress.values = { 80 streetAddress1: document.getElementById('nominator-street-address').value, 81 city: document.getElementById('nominator-city').value, 82 postalCode: document.getElementById('nominator-postal-code').value 83 }; 84 85 __HTD.nomination.setRegion(nominatorAddress, 'nominator-state', 'nominator-province'); 86 } 87 88 const nomination = { 89 childFirstName: document.getElementById('child-first-name').value, 90 childLastName: document.getElementById('child-last-name').value, 91 childAge: parseInt(document.getElementById('child-age').value), 92 childGuardianFirstName: document.getElementById('guardian-first-name').value, 93 childGuardianLastName: document.getElementById('guardian-last-name').value, 94 childPhoneNumber: document.getElementById('guardian-phone-number').value, 95 childGuardianEmailAddress: document.getElementById('guardian-email-address').value, 96 childCountryCode: document.getElementById('child-country').value, 97 nominatorFirstName: document.getElementById('nominator-first-name').value, 98 nominatorLastName: document.getElementById('nominator-last-name').value, 99 nominatorRelationToChild: document.getElementById('nominator-relation').value, 100 nominatorNonProfitName: document.getElementById('nominator-non-profit-name').value, 101 nominatorPhoneNumber: document.getElementById('nominator-phone-number').value, 102 nominatorEmailAddress: document.getElementById('nominator-email-address').value, 103 nominatorCountryCode: document.getElementById('nominator-country').value, 104 writtenRecommendation: document.getElementById('written-recommendation').value, 105 addresses: [ 106 childAddress, 107 nominatorAddress 108 ], 109 useChildAddressAsIs: typeof childAddressRevision === 'boolean' ? childAddressRevision : false, 110 useNominatorAddressAsIs: typeof nominatorAddressRevision === 'boolean' ? nominatorAddressRevision : false, 111 }; 112 113 if (!__HTD.nomination.validateNomination(nomination, childAddress, nominatorAddress)) 114 return; 115 116 const ajaxOptions = { 117 resource: 'nomination', 118 formValues: nomination, 119 successHandler: response => { 120 return response 121 .json() 122 .then(result => { 123 if (!result) 124 return; 125 126 let toastMessage, toastType; 127 128 if (result.isSuccess) { 129 // The nomination was received successfully. 130 __HTD.nomination.clearNominationForm(); 131 132 toastMessage = __HTD.translations.translate('success-message'); 133 toastType = 'success'; 134 } else { 135 // There was a problem in the server with the nomination. 136 const errorCode = result.errorCode; 137 const translationKey = `nomination-error-${errorCode}`; 138 139 toastMessage = __HTD.translations.translate(translationKey); 140 toastType = 'error'; 141 142 if (toastMessage === translationKey) { 143 // The error could not be translated. 144 // Check if we can handle the error code in some other way. 145 switch (errorCode) { 146 case 108: // AddressSuggestions 147 { 148 if (__HTD.nomination.setRevisionAddress('child', result.childOriginalFormattedAddress, result.childFormattedAddressSuggestions, result.childAddressSuggestions) 149 | __HTD.nomination.setRevisionAddress('nominator', result.nominatorOriginalFormattedAddress, result.nominatorFormattedAddressSuggestions, result.nominatorAddressSuggestions)) { 150 toastMessage = null; // Unset this value to prevent the toast from showing. 151 __HTD.modal.show('confirm-ad
151dresses'); 152 } else { 153 console.error(`Server returned code '${result.code}' and error-code '${errorCode}', but did not provide enough information for address revision.`); 154 toastMessage = __HTD.translations.translate('temporary-server-error'); 155 } 156 157 break; 158 } 159 default: 160 console.error(`Unhandled result code '${result.code}' and error-code '${errorCode}' found.`); 161 toastMessage = __HTD.translations.translate('temporary-server-error'); 162 break; 163 } 164 } 165 } 166 167 __HTD.nomination.enable('nomination-form', toastMessage, toastType); 168 }); 169 }, 170 failureHandler: () => { 171 __HTD.nomination.enable('nomination-form', __HTD.translations.translate('server-error'), 'error'); 172 }, 173 errorHandler: () => { 174 __HTD.nomination.enable('nomination-form', __HTD.translations.translate('temporary-server-error'), 'error'); 175 } 176 }; 177 178 __HTD.submitAjaxForm(ajaxOptions); 179 }; 180 181 htd.nomination.setRevisionAddress = function (addressType, originalFormattedAddress, formattedAddressSuggestions, addressSuggestions) { 182 const addressConfirmation = document.querySelector(`.${addressType}-address-confirmation`); 183 184 if (!addressSuggestions) { 185 addressConfirmation.classList.add('hidden'); 186 return false; 187 } 188 189 const revisedContainer = addressConfirmation.querySelector(`.${addressType}-revised-container`); 190 const originalContainer = addressConfirmation.querySelector(`.${addressType}-original-container`); 191 192 revisedContainer.classList.remove('bg-primary', 'text-white', 'font-bold'); 193 originalContainer.classList.remove('bg-primary', 'text-white', 'font-bold'); 194 195 const originalLine1 = addressConfirmation.querySelector(`.${addressType}-original-line-1`); 196 const originalLine2 = addressConfirmation.querySelector(`.${addressType}-original-line-2`); 197 198 originalLine1.innerHTML = originalFormattedAddress[0]; 199 originalLine2.innerHTML = originalFormattedAddress[1]; 200 201 const warning = revisedContainer.querySelector(`.${addressType}-revised-warning`); 202 const revised = revisedContainer.querySelector(`.${addressType}-revised`); 203 204 if (addressSuggestions.length) { 205 const formattedAddressSuggestion = formattedAddressSuggestions[0]; 206 const revisedLine1 = revised.querySelector(`.${addressType}-revised-line-1`); 207 const revisedLine2 = revised.querySelector(`.${addressType}-revised-line-2`); 208 209 revised.revisedAddress = addressSuggestions[0]; 210 revisedLine1.innerHTML = formattedAddressSuggestion[0]; 211 revisedLine2.innerHTML = formattedAddressSuggestion[1]; 212 213 warning.classList.add('hidden'); 214 revised.classList.remove('hidden'); 215 } else { 216 warning.classList.remove('hidden'); 217 revised.classList.add('hidden'); 218 } 219 220 addressConfirmation.classList.remove('hidden'); 221 return true; 222 }; 223 224 htd.nomination.setRegion = function (address, stateElementId, provinceElementId) { 225 const stateElement = document.getElementById(stateElementId); 226 227 if (stateElement.classList.contains('hidden')) { 228 const provinceElement = document.getElementById(provinceElementId); 229 230 address.values.province = provinceElement.value; 231 return; 232 } 233 234 address.values.state = stateElement.value; 235 }; 236 237 htd.nomination.validateNomination = function (nomination, childAddress, nominatorAddress) { 238 const formId = 'nomination-form'; 239 const form = document.getElementById(formId); 240 const translate = __HTD.translations.translate; 241 242 if (!nomination.childFirstName) { 243 form.querySelector('#child-first-name').focus(); 244 __HTD.nomination.enable(formId, translate('missing-child-first-name'), 'error'); 245 return false; 246 } 247 if (!nomination.childLastName) { 248 form.querySelector('#child-last-name').focus(); 249 __HTD.nomination.enable(formId, translate('missing-child-last-name'), 'error'); 250 return false; 251 } 252 if (!nomination.childAge) { 253 form.querySelector('#child-age').focus(); 254 __HTD.nomination.enable(formId, translate('missing-child-age'), 'error'); 255 return false; 256 } 257 if (!nomination.childGuardianFirstName) { 258 form.querySelector('#guardian-first-name').focus(); 259 __HTD.nomination.enable(formId, translate('missing-guardian-first-name'), 'error'); 260 return false; 261 } 262 if (!nomination.childGuardianLastName) { 263 form.querySelector('#guardian-last-name').focus(); 264 __HTD.nomination.enable(formId, translate('missing-guardian-last-name'), 'error'); 265 return false; 266 } 267 if (!nomination.childPhoneNumber) { 268 form.querySelector('#guardian-phone-number').focus(); 269 __HTD.nomination.enable(formId, translate('missing-guardian-phone-number'), 'error'); 270 return false; 271 } 272 if (!nomination.childCountryCode) { 273 form.querySelector('#child-country').focus(); 274 __HTD.nomination.enable(formId, translate('missing-child-country'), 'error'); 275 return false; 276 } 277 if (!childAddress.values.streetAddress1) {
278 form.querySelector('#child-street-address').focus(); 279 __HTD.nomination.enable(formId, translate('missing-child-street-address'), 'error'); 280 return false; 281 } 282 if (!childAddress.values.city) { 283 form.querySelector('#child-city').focus(); 284 __HTD.nomination.enable(formId, translate('missing-child-city'), 'error'); 285 return false; 286 } 287 if (!__HTD.nomination.validateRegion(form, nomination.childCountryCode, 'child-state', 'child-province')) 288 return false; 289 if (!childAddress.values.postalCode) { 290 form.querySelector('#child-postal-code').focus(); 291 __HTD.nomination.enable(formId, translate('missing-child-postal-code'), 'error'); 292 return false; 293 } 294 if (!nomination.nominatorFirstName) { 295 form.querySelector('#nominator-first-name').focus(); 296 __HTD.nomination.enable(formId, translate('missing-nominator-first-name'), 'error'); 297 return false; 298 } 299 if (!nomination.nominatorLastName) { 300 form.querySelector('#nominator-last-name').focus(); 301 __HTD.nomination.enable(formId, translate('missing-nominator-last-name'), 'error'); 302 return false; 303 } 304 if (!nomination.nominatorRelationToChild) { 305 form.querySelector('#nominator-relation').focus(); 306 __HTD.nomination.enable(formId, translate('missing-nominator-relation'), 'error'); 307 return false; 308 } 309 if (!nomination.nominatorNonProfitName) { 310 form.querySelector('#nominator-non-profit-name').focus(); 311 __HTD.nomination.enable(formId, translate('missing-nominator-non-profit-name'), 'error'); 312 return false; 313 } 314 if (!nomination.nominatorPhoneNumber) { 315 form.querySelector('#nominator-phone-number').focus(); 316 __HTD.nomination.enable(formId, translate('missing-nominator-phone-number'), 'error'); 317 return false; 318 } 319 if (!nomination.nominatorEmailAddress) { 320 form.querySelector('#nominator-email-address').focus(); 321 __HTD.nomination.enable(formId, translate('missing-nominator-email-address'), 'error'); 322 return false; 323 } 324 if (!nomination.nominatorCountryCode) { 325 form.querySelector('#nominator-country').focus(); 326 __HTD.nomination.enable(formId, translate('missing-nominator-country'), 'error'); 327 return false; 328 } 329 if (!nominatorAddress.values.streetAddress1) { 330 form.querySelector('#nominator-street-address').focus(); 331 __HTD.nomination.enable(formId, translate('missing-nominator-street-address'), 'error'); 332 return false; 333 } 334 if (!nominatorAddress.values.city) { 335 form.querySelector('#nominator-city').focus(); 336 __HTD.nomination.enable(formId, translate('missing-nominator-city'), 'error'); 337 return false; 338 } 339 if (!__HTD.nomination.validateRegion(form, nomination.childCountryCode, 'nominator-state', 'nominator-province')) 340 return false; 341 if (!nominatorAddress.values.postalCode) { 342 form.querySelector('#nominator-postal-code').focus(); 343 __HTD.nomination.enable(formId, translate('missing-nominator-postal-code'), 'error'); 344 return false; 345 } 346 if (!nomination.writtenRecommendation) { 347 form.querySelector('#written-recommendation').focus(); 348 __HTD.nomination.enable(formId, translate('missing-written-recommendation'), 'error'); 349 return false; 350 } 351 352 return true; 353 }; 354 355 htd.nomination.validateRegion = function (form, countryCode, stateElementId, provinceElementId) { 356 const translate = __HTD.translations.translate; 357 358 switch (countryCode) { 359 case 'CAN': 360 const provinceElement = document.getElementById(provinceElementId); 361 362 if (provinceElement.value) 363 return true; 364 365 form.querySelector(`#${provinceElementId}`).focus(); 366 __HTD.nomination.enable(form.id, translate(`missing-${provinceElementId}`), 'error'); 367 return false; 368 case 'USA': 369 const stateElement = document.getElementById(stateElementId); 370 371 if (stateElement.value) 372 return true; 373 374 form.querySelector(`#${stateElementId}`).focus(); 375 __HTD.nomination.enable(form.id, translate(`missing-${stateElementId}`), 'error'); 376 return false; 377 default: 378 return false; 379 } 380 }; 381 382 htd.nomination.disable = function (formId) { 383 const submitButton = document.querySelector(`#${formId} button`); 384 385 submitButton.disabled = true; 386 submitButton.innerHTML = `${__HTD.translations.translate('submitting')}...`; 387 }; 388 389 htd.nomination.enable = function (formId, toastMessage, toastType) { 390 const submitButton = document.querySelector(`#${formId} button`); 391 392 if (toastMessage) 393 __HTD.toast.show({ id: 'toast', title: toastMessage, type: toastType }); 394 395 submitButton.disabled = false; 396 submitButton.innerHTML = __HTD.translations.translate('submit'); 397 }; 398 399 htd.nomination.countryChanged = function (countryElementId, stateElementId, provinceElementId) { 400 const stateElement = document.getElementById(stateElementId); 401 const provinceElement = document.getElementById(provinceElementId); 402 403 if (document.getElementById(countryElementId).value === 'CAN') { 404 stateElement.classList.add('hidden'); 405 provinceElement.classList.remove('hidden'); 406 return; 407 } 408 409 stateElement.classList.remove('hidden'); 410 provinceElement.classList.add('hidden'); 411 }; 412 413 htd.nomination.clearPostalCodeForm = function () { 414 document.getElementById('postal-code-availability').value = ''; 415 }; 416 417 htd.nomination.clearNominationForm = function () {
418 document.getElementById('child-street-address').value = ''; 419 document.getElementById('child-city').value = ''; 420 document.getElementById('child-postal-code').value = ''; 421 document.getElementById('nominator-street-address').value = ''; 422 document.getElementById('nominator-city').value = ''; 423 document.getElementById('nominator-postal-code').value = ''; 424 document.getElementById('child-first-name').value = ''; 425 document.getElementById('child-last-name').value = ''; 426 document.getElementById('child-age').value = ''; 427 document.getElementById('guardian-first-name').value = ''; 428 document.getElementById('guardian-last-name').value = ''; 429 document.getElementById('guardian-phone-number').value = ''; 430 document.getElementById('guardian-email-address').value = ''; 431 document.getElementById('nominator-first-name').value = ''; 432 document.getElementById('nominator-last-name').value = ''; 433 document.getElementById('nominator-non-profit-name').value = ''; 434 document.getElementById('nominator-relation').value = ''; 435 document.getElementById('nominator-phone-number').value = ''; 436 document.getElementById('nominator-email-address').value = ''; 437 document.getElementById('written-recommendation').value = ''; 438 }; 439 440 htd.nomination.revisedAddressClick = function (event, addressType) { 441 const addressConfirmation = event.target.closest(`.${addressType}-address-confirmation`); 442 const revisedContainer = addressConfirmation.querySelector(`.${addressType}-revised-container`); 443 const originalContainer = addressConfirmation.querySelector(`.${addressType}-original-container`); 444 445 revisedContainer.classList.add('bg-primary', 'text-white', 'font-bold'); 446 originalContainer.classList.remove('bg-primary', 'text-white', 'font-bold'); 447 }; 448 449 htd.nomination.originalAddressClick = function (event, addressType) { 450 const addressConfirmation = event.target.closest(`.${addressType}-address-confirmation`); 451 const revisedContainer = addressConfirmation.querySelector(`.${addressType}-revised-container`); 452 const originalContainer = addressConfirmation.querySelector(`.${addressType}-original-container`); 453 454 revisedContainer.classList.remove('bg-primary', 'text-white', 'font-bold'); 455 originalContainer.classList.add('bg-primary', 'text-white', 'font-bold'); 456 }; 457 458 htd.nomination.disablePostalCodeForm = function () { 459 document.querySelector('#availability-postal-code').disabled = true; 460 461 const submitButton = document.querySelector('#postal-code-nomination-form button'); 462 463 submitButton.disabled = true; 464 submitButton.innerHTML = `${__HTD.translations.translate('submitting')}...`; 465 }; 466 467 htd.nomination.enablePostalCodeForm = function () { 468 document.querySelector('#availability-postal-code').disabled = false; 469 470 const submitButton = document.querySelector('#postal-code-nomination-form button'); 471 472 submitButton.disabled = false; 473 submitButton.innerHTML = __HTD.translations.translate('submit'); 474 }; 475 476 htd.nomination.submitPostalCodeForm = function (event) { 477 event.preventDefault(); 478 __HTD.toast.hide('postal-code-nomination-form-toast'); 479 __HTD.nomination.disablePostalCodeForm(); 480 481 const postalCode = document.getElementById('availability-postal-code').value; 482 483 if (!postalCode) { 484 __HTD.toast.show({ id: 'postal-code-nomination-form-toast', title: __HTD.translations.translate('missing-postal-code'), type: 'error' }); 485 __HTD.nomination.enablePostalCodeForm(); 486 return; 487 } 488 489 const ajaxOptions = { 490 resource: 'postal-code-availability', 491 formValues: postalCode, 492 responseHandler: () => __HTD.nomination.enablePostalCodeForm(), 493 successHandler: response => { 494 return response 495 .json() 496 .then(result => { 497 if (!result) 498 return; 499 500 if (result.availability === 'Available') { 501 __HTD.nomination.loadNominationForm(document.getElementById('availability-postal-code').value); 502 document.getElementById('nomination-form-container').innerHTML = `${__HTD.translations.translate('loading')}...`; 503 return; 504 } 505 506 __HTD.toast.show({ id: 'postal-code-nomination-form-toast', title: __HTD.translations.translate(`availability-${result.availability}`), type: 'error' }); 507 }); 508 }, 509 failureHandler: response => { 510 if (response.status === 400) { 511 window.alert(__HTD.translations.translate('refresh-browser')); 512 return; 513 } 514 515 __HTD.toast.show({ id: 'postal-code-nomination-form-toast', title: 'Error', subtitle: __HTD.translations.translate('server-error'), type: 'error' }); 516 }, errorHandler: () => { 517 __HTD.toast.show({ id: 'postal-code-nomination-form-toast', title: 'Error', subtitle: __HTD.translations.translate('server-error'), type: 'error' }); 518 } 519 }; 520 521 __HTD.submitAjaxForm(ajaxOptions); 522 }; 523 524 htd.nomination.loadNominationForm = function (postalCode) { 525 const country = document.getElementById('form-parameters-country')?.value; 526 const region = document.getElementById('form-parameters-region')?.value; 527 const city = document.getElementById('form-parameters-city')?.value; 528 const queryParams = new URLSearchParams(); 529 530 postalCode = postalCode || document.getElementById('form-parameters-postal-code')?.value; 531 532 if (country) 533 queryParams.set('country', country); 534 if (region) 535 queryParams.set('region', region); 536 if (city) 537 queryParams.set('city', city); 538 if (postalCode) 539 queryParams.set('postal-code', postalCode); 540 541 const ajaxOptions = { 542 resource: 'nomination', 543 errorHandler: __HTD.translations.translate('form-not-available') 544 }; 545 546 __HTD.loadAjaxForm('#nomination-form-container', ajaxOptions); 547 }; 548})(window.__HTD = window.__HTD || {});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.