1 /* JS form validation for holds page alert methods */
2 function resetBackgrounds(names){
3 for (var key in names) {
4 if (names.hasOwnProperty(key)) {
5 var l = document.getElementsByName(names[key]);
7 l[0].style.backgroundColor = "";
13 function validateMethodSelections (alertMethodCboxes) {
14 var needsPhone = false;
17 var needsEmail = false;
22 var inputNames = { e: "email_address", ph: "phone_notify", sms: "sms_notify", carrier: "sms_carrier"};
23 resetBackgrounds(inputNames);
25 //Array.from(alertMethodCboxes).forEach(function(cbox){
26 for (var i = 0; i < alertMethodCboxes.length; i++){
27 var cbox = alertMethodCboxes[i];
28 if (cbox.checked && !cbox.disabled) {
30 case "email_notify_checkbox":
32 hasEmail = document.getElementsByName(inputNames.e)[0].innerHTML !== "";
34 case "phone_notify_checkbox":
36 hasPhone = document.getElementsByName(inputNames.ph)[0].value !== "";
38 case "sms_notify_checkbox":
40 var smsNumInput = document.getElementsByName(inputNames.sms)[0];
41 hasSms = document.getElementsByName(inputNames.carrier)[0].value !== "" && smsNumInput.value !== ""; // todo: properly validate phone nums
48 var emailOK = (needsEmail && hasEmail) || (!needsEmail);
49 var phoneOK = needsPhone && hasPhone || (!needsPhone);
50 var smsOK = needsSms && hasSms || (!needsSms);
53 culprits.push("phone_notify");
56 culprits.push("sms_notify", "sms_carrier");
59 var isFormOK = emailOK && phoneOK && smsOK;
60 return { isValid: isFormOK, culpritNames : culprits };
63 function confirmMultipleHolds() {
65 var numSelect = document.getElementById("num_copies");
67 var num = parseInt(numSelect.value);
69 result = window.confirm(eg_opac_i18n.EG_MULTIHOLD_MESSAGE.format(num));
75 function isValidDate(dateString){
76 // First check for the pattern
77 if(!/^\d{1,2}\/\d{1,2}\/\d{4}$/.test(dateString))
80 // Parse the date parts to integers
81 var parts = dateString.split("/");
82 var day = parseInt(parts[1], 10);
83 var month = parseInt(parts[0], 10);
84 var year = parseInt(parts[2], 10);
86 var today = new Date();
88 if (today.getFullYear() > year){
91 else if (today.getFullYear() == year ) {
92 if(today.getMonth() +1 > month) {
95 else if (today.getMonth() +1 == month) {
96 if (today.getDate() > day) return false;
100 // Check the ranges of month and year
101 if(year < 2000 || year > 3000 || month == 0 || month > 12) return false;
103 var monthLength = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
105 // Adjust for leap years
106 if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) monthLength[1] = 29;
108 // Check the range of the day
109 return day > 0 && day <= monthLength[month - 1];
112 function validateHoldForm() {
113 var res = validateMethodSelections(document.getElementsByClassName("hold-alert-method"));
115 if (document.getElementById('hold_suspend').checked && document.getElementById('thaw_date').value.length > 0){
116 //check that the date is not in the past
117 if(!isValidDate(document.getElementById('thaw_date').value)) {
118 alert(eg_opac_i18n.EG_INVALID_DATE);
119 document.getElementById('thaw_date').style.backgroundColor = "yellow";
126 var result = confirmMultipleHolds();
128 var submit_element = document.getElementById("place_hold_submit");
129 submit_element.disabled = true;
133 alert(eg_opac_i18n.EG_MISSING_REQUIRED_INPUT);
134 res.culpritNames.forEach(function(n){
135 document.getElementsByName(n)[0].style.backgroundColor = "yellow";