]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/opac/holds-validation.js
842366c50ea281ef413e96557737ce6294387de3
[working/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 validateHoldForm() {
64     var res = validateMethodSelections(document.getElementsByClassName("hold-alert-method"));
65     if (res.isValid)
66     {
67         return true;
68     } else {
69         alert ("Please complete hold notification method info.");
70         res.culpritNames.forEach(function(n){
71             document.getElementsByName(n)[0].style.backgroundColor  = "yellow";
72         });
73         return false;
74     }
75 }
76