Allow 'Enter' key to run Verify Credentials user check
[working/Evergreen.git] / Open-ILS / xul / staff_client / server / main / verify_credentials.js
1 function verify_init() {
2     try {
3         commonStrings = document.getElementById('commonStrings');
4         patronStrings = document.getElementById('patronStrings');
5
6         if (typeof JSAN == 'undefined') {
7             throw(
8                 commonStrings.getString('common.jsan.missing')
9             );
10         }
11
12         JSAN.errorLevel = "die"; // none, warn, or die
13         JSAN.addRepository('..');
14
15         JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.stash_retrieve();
16         XML_HTTP_SERVER = data.server_unadorned;
17
18         JSAN.use('util.network'); var net = new util.network();
19         document.getElementById('cmd_verify').addEventListener(
20             'command',
21             function() {
22                 try {
23                     var barcode = document.getElementById('barcode_prompt').value;
24                     var name = document.getElementById('name_prompt').value;
25                     var password = document.getElementById('password_prompt').value; 
26                     var req = net.simple_request(
27                         'AUTH_VERIFY_CREDENTIALS',
28                         [ 
29                             ses(), 
30                             barcode,
31                             name,
32                             hex_md5( password )
33                         ]
34                     );
35
36                     if (typeof req.ilsevent != 'undefined') { throw(req); }
37
38                     var msg_area = document.getElementById('messages');
39                     var hbox = document.createElement('hbox'); msg_area.insertBefore(hbox, msg_area.firstChild);
40                     var success_msg = document.createElement('description'); hbox.appendChild(success_msg);
41                     success_msg.setAttribute('class', String(req) == '1' ? 'success_text' : 'failure_text');
42                     success_msg.appendChild(
43                         document.createTextNode( 
44                             String(req) == '1' ? 
45                                 patronStrings.getString('staff.patron.cmd_verify_credentials.correct_credentials') : 
46                                 patronStrings.getString('staff.patron.cmd_verify_credentials.incorrect_credentials') 
47                         )
48                     );
49                     var name_msg = document.createElement('description'); hbox.appendChild(name_msg);
50                     name_msg.appendChild(
51                         document.createTextNode(
52                             patronStrings.getFormattedString('staff.patron.cmd_verify_credentials.name_feedback',[name]) 
53                         )
54                     );
55                     var barcode_msg = document.createElement('description'); hbox.appendChild(barcode_msg);
56                     barcode_msg.appendChild(
57                         document.createTextNode(
58                             patronStrings.getFormattedString('staff.patron.cmd_verify_credentials.barcode_feedback',[barcode]) 
59                         )
60                     );
61                     var date_msg = document.createElement('description'); hbox.appendChild(date_msg);
62                     date_msg.appendChild(
63                         document.createTextNode(
64                             new Date()
65                         )
66                     );
67
68                 } catch(E) {
69                     alert(E);
70                 }
71                 if (document.getElementById('name_prompt').disabled) {
72                     document.getElementById('password_prompt').focus();
73                 } else {
74                     document.getElementById('name_prompt').focus();
75                 }
76                 document.getElementById('password_prompt').value = '';
77             },
78             false
79         );
80         document.getElementById('cmd_retrieve').addEventListener(
81             'command',
82             function() {
83                 var barcode = document.getElementById('barcode_prompt').value;
84                 var name = document.getElementById('name_prompt').value;
85                 var req = net.simple_request(
86                     'FM_AU_ID_RETRIEVE_VIA_BARCODE_OR_USERNAME',
87                     [
88                         ses(),
89                         barcode,
90                         name
91                     ]
92                 );
93                 if (typeof req.ilsevent != 'undefined') { 
94                     alert (req.desc);
95                     document.getElementById('name_prompt').focus();
96                 } else {
97                     xulG.set_patron_tab( {}, { 'id' : req } );
98                 }
99             },
100             false
101         );
102
103         if (xulG.barcode) {
104             document.getElementById('name_prompt').disabled = true;
105             document.getElementById('name_prompt').value = xulG.usrname;
106             document.getElementById('barcode_prompt').disabled = true;
107             document.getElementById('barcode_prompt').value = xulG.barcode;
108             document.getElementById('cmd_retrieve').setAttribute('disabled','true');
109             document.getElementById('password_prompt').focus();
110         } else {
111             document.getElementById('name_prompt').focus();
112         }
113
114         document.getElementById('password_prompt').addEventListener('keypress',function(e) {
115             if ( e.keyCode && (e.keyCode == 13 || e.keyCode == 77)) {
116                 var x = document.createEvent('Event');
117                 x.initEvent('command',true,false);
118                 document.getElementById('cmd_verify').dispatchEvent(x);
119             }
120         });
121
122     } catch(E) {
123         alert(E);
124     }
125 }