]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/circ/offline_register.js
seconds instead of milliseconds
[Evergreen.git] / Open-ILS / xul / staff_client / chrome / content / circ / offline_register.js
1 function my_init() {
2         try {
3                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
4                 if (typeof JSAN == 'undefined') { throw( "The JSAN library object is missing."); }
5                 JSAN.errorLevel = "die"; // none, warn, or die
6                 JSAN.addRepository('..');
7                 JSAN.use('util.error'); g.error = new util.error();
8                 g.error.sdump('D_TRACE','my_init() for offline_register.xul');
9
10                 if (typeof window.xulG == 'object' && typeof window.xulG.set_tab_name == 'function') {
11                         try { window.xulG.set_tab_name('Standalone'); } catch(E) { alert(E); }
12                 }
13
14                 $('barcode').addEventListener('keypress',handle_keypress,false);
15                 $('submit').addEventListener('command',next_patron,false);
16
17                 JSAN.use('util.file');
18                 JSAN.use('util.widgets');
19
20                 var file; var list_data; var ml; var errors = '';
21
22                 file = new util.file('offline_ou_list'); 
23                 if (file._file.exists()) {
24                         list_data = file.get_object(); file.close();
25                         ml = util.widgets.make_menulist( list_data[0], list_data[1] );
26                         ml.setAttribute('id','home_ou'); $('x_home_ou').appendChild(ml);
27                 } else {
28                         errors += 'Missing library list.\n';
29                 }
30
31                 file = new util.file('offline_pgt_list');
32                 if (file._file.exists()) {
33                         list_data = file.get_object(); file.close();
34                         ml = util.widgets.make_menulist( list_data[0], list_data[1] );
35                         ml.setAttribute('id','profile'); $('x_profile').appendChild(ml);
36                 } else {
37                         errors += 'Missing profile list.\n';
38                 }
39
40                 file = new util.file('offline_cit_list'); 
41                 if (file._file.exists()) {
42                         list_data = file.get_object(); file.close();
43                         ml = util.widgets.make_menulist( list_data[0], list_data[1] );
44                         ml.setAttribute('id','ident_type'); $('x_ident_type').appendChild(ml);
45                 } else {
46                         errors += 'Missing identification type list.\n';
47                 }
48
49                 file = new util.file('offline_asv_list'); 
50                 if (file._file.exists()) {
51                         list_data = file.get_object(); file.close();
52                         render_surveys('x_surveys', list_data);
53                 } else {
54                         errors += 'Missing required surveys.\n';
55                 }
56
57                 if (errors != '') {
58                         alert('ERROR: Offline patron registration requires some server-generated files.  Please login periodically to retrieve these files.\n' + errors);
59                         location.href = 'about:blank';
60                 }
61
62                 $('passwd').value = parseInt(Math.random()*8999+1000);
63
64                 $('dob').addEventListener('change',handle_check_date,false);
65                 $('barcode').focus();
66
67                 var file = new util.file('offline_delta'); 
68                 if (file._file.exists()) { g.delta = file.get_object(); file.close(); } else { g.delta = 0; }
69
70         } catch(E) {
71                 var err_msg = "!! This software has encountered an error.  Please tell your friendly " +
72                         "system administrator or software developer the following:\ncirc/offline_register.xul\n" + E + '\n';
73                 try { g.error.sdump('D_ERROR',err_msg); } catch(E) { dump(err_msg); }
74                 alert(err_msg);
75         }
76 }
77
78 function $(id) { return document.getElementById(id); }
79
80 function handle_check_date(ev) {
81         ev.target.value = check_date(ev.target.value);
82 }
83
84 function check_date(value) {
85         JSAN.use('util.date');
86         try {
87                 if (! util.date.check('YYYY-MM-DD',value) ) { throw('Invalid Date'); }
88                 if (! util.date.check_past('YYYY-MM-DD',value) ) { throw('Patron needs to be born yesterday.'); }
89                 if ( util.date.formatted_date(new Date(),'%F') == value) { throw('Happy birthday!  You need to be more than 0 days old.'); }
90         } catch(E) {
91                 alert(E);
92                 value = '';
93         }
94         return value;
95 }
96
97 function render_surveys(node,obj) {
98         node = util.widgets.get(node);
99         util.widgets.remove_children(node);
100
101         for (var i in obj) {
102                 var survey = obj[i];
103                 var x_gb = document.createElement('groupbox'); node.appendChild(x_gb);
104                 var x_cp = document.createElement('caption'); 
105                 x_cp.setAttribute('label',i); x_gb.appendChild(x_cp);
106                 var x_d = document.createElement('description');
107                 x_d.appendChild( document.createTextNode( survey.description ) ); x_gb.appendChild(x_d);
108                 for (var j = 0; j < survey.questions.length; j++) {
109                         var question = survey.questions[j];
110                         var x_d = document.createElement('description');
111                         x_d.appendChild( document.createTextNode( (j+1) + ') ' + question.question ) ); 
112                         x_gb.appendChild( x_d );
113                         var x_hb = document.createElement('hbox'); x_hb.setAttribute('flex','1'); 
114                         x_gb.appendChild(x_hb);
115                         var x_spacer = document.createElement('spacer'); x_spacer.setAttribute('flex','1');
116                         x_hb.appendChild(x_spacer);
117                         var x_ml = util.widgets.make_menulist( [ ['Choose a response...',''] ].concat(question.answers) );
118                         x_ml.setAttribute('name','survey'); x_hb.appendChild(x_ml);
119                 }
120         }
121 }
122
123 function handle_keypress(ev) {
124         if ( (! ev.keyCode) || (ev.keyCode != 13) ) return;
125         switch(ev.target) {
126                 case $('barcode') : $('family_name').focus(); break;
127                 default: break;
128         }
129 }
130
131 function check_patron(obj) {
132         var errors = '';
133         if (! obj.user.billing_address.post_code ) {
134                 errors += 'Missing Address : Postal Code\n';
135                 $('post_code').focus();
136                 $('post_code').parentNode.setAttribute('style','background-color: red');
137         } else {
138                 $('post_code').parentNode.setAttribute('style','');
139         }
140         if (! obj.user.billing_address.state ) {
141                 errors += 'Missing Address : State\n';
142                 $('state').focus();
143                 $('state').parentNode.setAttribute('style','background-color: red');
144         } else {
145                 $('state').parentNode.setAttribute('style','');
146         }
147         if (! obj.user.billing_address.city ) {
148                 errors += 'Missing Address : City\n';
149                 $('city').focus();
150                 $('city').parentNode.setAttribute('style','background-color: red');
151         } else {
152                 $('city').parentNode.setAttribute('style','');
153         }
154         if (! obj.user.billing_address.street1 ) {
155                 errors += 'Missing Address : Line 1\n';
156                 $('street1').focus();
157                 $('street1').parentNode.setAttribute('style','background-color: red');
158         } else {
159                 $('street1').parentNode.setAttribute('style','');
160         }
161         if (! obj.user.ident_value ) {
162                 errors += 'Missing Identification Value\n';
163                 $('ident_value').focus();
164                 $('ident_value').parentNode.setAttribute('style','background-color: red');
165         } else {
166                 $('ident_value').parentNode.setAttribute('style','');
167         }
168         if (! obj.user.ident_type ) {
169                 errors += 'Missing Identification Type\n';
170                 $('ident_type').focus();
171                 $('ident_type').parentNode.setAttribute('style','background-color: red');
172         } else {
173                 $('ident_type').parentNode.setAttribute('style','');
174         }
175         if (! obj.user.dob ) {
176                 errors += 'Missing Date of Birth\n';
177                 $('dob').focus();
178                 $('dob').parentNode.parentNode.setAttribute('style','background-color: red');
179         } else {
180                 $('dob').parentNode.parentNode.setAttribute('style','');
181         }
182         if (! obj.user.first_given_name ) {
183                 errors += 'Missing First Name\n';
184                 $('first_given_name').focus();
185                 $('first_given_name').parentNode.setAttribute('style','background-color: red');
186         } else {
187                 $('first_given_name').parentNode.setAttribute('style','');
188         }
189         if (! obj.user.family_name ) {
190                 errors += 'Missing Last Name\n';
191                 $('family_name').focus();
192                 $('family_name').parentNode.setAttribute('style','background-color: red');
193         } else {
194                 $('family_name').parentNode.setAttribute('style','');
195         }
196         if (! obj.user.passwd ) {
197                 errors += 'Missing Password\n';
198                 $('passwd').focus();
199                 $('passwd').parentNode.setAttribute('style','background-color: red');
200         } else {
201                 $('passwd').parentNode.setAttribute('style','');
202         }
203         if (! obj.user.card.barcode ) {
204                 errors += 'Missing Barcode\n';
205                 $('barcode').focus();
206                 $('barcode').parentNode.setAttribute('style','background-color: red');
207         } else {
208                 $('barcode').parentNode.setAttribute('style','');
209         }
210         if (! obj.user.profile ) {
211                 errors += 'Missing Profile\n';
212                 $('profile').focus();
213                 $('profile').parentNode.setAttribute('style','background-color: red');
214         } else {
215                 $('profile').parentNode.setAttribute('style','');
216         }
217         if (! obj.user.home_ou ) {
218                 errors += 'Missing Home Library\n';
219                 $('home_ou').focus();
220                 $('home_ou').parentNode.setAttribute('style','background-color: red');
221         } else {
222                 $('home_ou').parentNode.setAttribute('style','');
223         }
224         if (errors != '') throw(errors);
225 }
226
227 function next_patron() {
228         try {
229                 var obj = {}
230                 obj.timestamp = parseInt( new Date().getTime() / 1000);
231                 obj.type = 'register';
232                 obj.user = {};
233                 obj.user.card = { 'barcode' : $('barcode').value };
234                 obj.user.profile = $('profile').value;
235                 obj.user.passwd = $('passwd').value;
236                 obj.user.ident_type = $('ident_type').value;
237                 obj.user.ident_value = $('ident_value').value;
238                 obj.user.first_given_name = $('first_given_name').value;
239                 obj.user.family_name = $('family_name').value;
240                 obj.user.home_ou = $('home_ou').value;
241                 obj.user.dob = $('dob').value;
242                 obj.user.billing_address = {};
243                 obj.user.billing_address.street1 = $('street1').value;
244                 obj.user.billing_address.street2 = $('street2').value;
245                 obj.user.billing_address.city = $('city').value;
246                 obj.user.billing_address.state = $('state').value;
247                 obj.user.billing_address.country = $('country').value;
248                 obj.user.billing_address.post_code = $('post_code').value;
249                 obj.user.survey_responses = [];
250
251                 var nl = document.getElementsByAttribute('name','survey');
252                 for (var i = 0; i < nl.length; i++) {
253                         var value = nl[i].value; if (value == '') continue;
254                         var values = JSON2js( value );
255                         var response = { 'survey' : values[2], 'question' : values[1], 'answer' : values[0] };
256                         obj.user.survey_responses.push( response );
257                 }
258
259                 try {
260                         check_patron(obj);
261                 } catch(E) {
262                         alert('Please fix the following:\n' + E);
263                         return;
264                 }
265
266                 JSAN.use('util.file'); var file = new util.file('pending_xacts');
267                 obj.delta = g.delta;
268                 file.append_object(obj);
269                 file.close();
270
271                 alert('Patron Registration Saved');
272
273                 $('passwd').value = parseInt(Math.random()*8999+1000);
274                 $('barcode').value = ''; $('ident_value').value = ''; $('first_given_name').value = '';
275                 $('family_name').value = ''; $('dob').value = ''; $('street1').value = '';
276                 $('street2').value = '';
277
278                 file = new util.file('offline_asv_list'); var list_data = file.get_object(); file.close();
279                 render_surveys('x_surveys', list_data);
280
281                 $('barcode').focus();
282
283         } catch(E) {
284                 dump(E+'\n'); alert(E);
285         }
286 }