]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/opac/holds-validation.js
7345e3fca99e270e997c695e88dca72e7229120b
[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 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 validateHoldForm() {
76     var res = validateMethodSelections(document.getElementsByClassName("hold-alert-method"));
77     if (res.isValid) {
78         return confirmMultipleHolds();
79     } else {
80         alert(eg_opac_i18n.EG_MISSING_REQUIRED_INPUT);
81         res.culpritNames.forEach(function(n){
82             document.getElementsByName(n)[0].style.backgroundColor  = "yellow";
83         });
84         return false;
85     }
86 }
87