]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/opac/holds-validation.js
LP 1772053: Cleanup Dan's code.
[Evergreen.git] / Open-ILS / web / js / ui / default / opac / holds-validation.js
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]);
6             if (l.length > 0) {
7                 l[0].style.backgroundColor  = "";
8             }
9         }
10     }
11 }
12
13 function validateMethodSelections (alertMethodCboxes) {
14     var needsPhone = false;
15     var hasPhone = false;
16
17     var needsEmail = false;
18     var hasEmail = false;
19
20     var needsSms = false;
21     var hasSms = false;
22     var inputNames = { e: "email_address", ph: "phone_notify", sms: "sms_notify", carrier: "sms_carrier"};
23     resetBackgrounds(inputNames);
24
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) {
29             switch(cbox.id){
30                 case "email_notify_checkbox":
31                     needsEmail = true;
32                     hasEmail = document.getElementsByName(inputNames.e)[0].innerHTML !== "";
33                     break;
34                 case "phone_notify_checkbox":
35                     needsPhone = true;
36                     hasPhone = document.getElementsByName(inputNames.ph)[0].value !== "";
37                     break;
38                 case "sms_notify_checkbox":
39                     needsSms = true;
40                     var smsNumInput = document.getElementsByName(inputNames.sms)[0];
41                     hasSms = document.getElementsByName(inputNames.carrier)[0].value !== "" && smsNumInput.value !== ""; // todo: properly validate phone nums
42                 break;
43             }
44         }
45     }
46
47     var culprits = [];
48     var emailOK = (needsEmail && hasEmail) || (!needsEmail);
49     var phoneOK = needsPhone && hasPhone || (!needsPhone);
50     var smsOK = needsSms && hasSms || (!needsSms);
51
52     if (!phoneOK) {
53         culprits.push("phone_notify");
54     }
55     if (!smsOK) {
56         culprits.push("sms_notify", "sms_carrier");
57     }
58
59     var isFormOK = emailOK && phoneOK && smsOK;
60     return { isValid: isFormOK, culpritNames : culprits };
61 }
62
63 function confirmMultipleHolds() {
64     var result = true;
65     var numSelect = document.getElementById("num_copies");
66     if (numSelect) {
67         var num = parseInt(numSelect.value);
68         if (num > 1) {
69             result = window.confirm(eg_opac_i18n.EG_MULTIHOLD_MESSAGE.format(num));
70         }
71     }
72     return result;
73 }
74
75 function isValidDate(dateString){
76     // First check for the pattern
77     if(!/^\d{1,2}\/\d{1,2}\/\d{4}$/.test(dateString))
78         return false;
79
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);
85
86     var today = new Date();
87
88     if (today.getFullYear() > year){
89       return false;
90     }
91     else if (today.getFullYear() == year ) {
92       if(today.getMonth() +1 > month) {
93          return false;
94       }
95       else if (today.getMonth() +1 == month) {
96          if (today.getDate() > day) return false;
97       }
98    }
99
100     // Check the ranges of month and year
101     if(year < 2000 || year > 3000 || month == 0 || month > 12) return false;
102
103     var monthLength = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
104
105     // Adjust for leap years
106     if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))  monthLength[1] = 29;
107
108     // Check the range of the day
109     return day > 0 && day <= monthLength[month - 1];
110 }
111
112 function validateHoldForm() {
113     var res = validateMethodSelections(document.getElementsByClassName("hold-alert-method"));
114
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";
120           return false;
121        }
122     }
123
124
125     if (res.isValid) {
126         var result = confirmMultipleHolds();
127         if (result) {
128             var submit_element = document.getElementById("place_hold_submit");
129             submit_element.disabled = true;
130         }
131         return result;
132     } else {
133         alert(eg_opac_i18n.EG_MISSING_REQUIRED_INPUT);
134         res.culpritNames.forEach(function(n){
135             document.getElementsByName(n)[0].style.backgroundColor  = "yellow";
136         });
137         return false;
138     }
139 }
140