947698a53e57c1ddf1ca171f0d847cc6590f46ee
[Evergreen.git] / Evergreen / staff_client / chrome / content / evergreen / patron / patron_display.js
1 sdump('D_TRACE','Loading patron_display.js\n');
2
3 function patron_display_init(p) {
4         sdump('D_PATRON_DISPLAY',"TESTING: patron_display.js: " + mw.G['main_test_variable'] + '\n');
5         sdump('D_CONSTRUCTOR',arg_dump(arguments));
6
7         // gives: p.clamshell, p.right_panel, p.left_panel
8         patron_display_clamshell_init(p);
9
10         // gives: p.patron_items, p.redraw_patron_items
11         patron_display_patron_items_init(p);
12
13         // gives: p.patron_checkout_items, p.redraw_patron_checkout_items
14         patron_display_patron_checkout_items_init(p);
15
16         // gives: p.patron_holds, p.redraw_patron_holds
17         patron_display_patron_holds_init(p);
18
19         // gives: p.patron_bills, p.redraw_patron_bills
20         patron_display_patron_bills_init(p);
21
22         // gives: p.patron_edit, p.redraw_patron_edit
23         patron_display_patron_edit_init(p);
24
25         //patron_display_survey_init(p);
26
27         p.set_patron = function (au) {
28                 p.patron_edit._patron = au;
29                 return p._patron = au;
30         }
31
32         p.display_patron = function (exceptions) {
33                 if (!exceptions) exceptions = {};
34                 if (!exceptions.all) {
35                         if (!exceptions.patron_checkout_items) p.redraw_patron_checkout_items();
36                         if (!exceptions.patron_items) p.redraw_patron_items();
37                         if (!exceptions.patron_holds) p.redraw_patron_holds();
38                         if (!exceptions.patron_bills) p.redraw_patron_bills();
39                         if (!exceptions.patron_display_survey) patron_display_survey_init(p);
40                         //if (!exceptions.patron_edit) p.redraw_patron_edit();
41                 }
42                 return render_fm(p.w.document, { 'au' : p._patron });
43         }
44         p.redraw = p.display_patron;
45
46         p.retrieve_patron_via_barcode = function (barcode) {
47                 if (!barcode) barcode = patron_get_barcode( p._patron );
48                 p.set_patron( retrieve_patron_by_barcode( barcode ) );
49                 return p.display_patron( {} );
50         }
51
52         p.retrieve_patron_via_id = function (id, exceptions) {
53                 p.set_patron( retrieve_patron_by_id( id ) );
54                 return p.display_patron(exceptions);
55         }
56
57         p.refresh = function(exceptions) {
58                 if (p._patron) p.retrieve_patron_via_id( p._patron.id(), exceptions );
59         }
60
61         set_patron_display_widgets(p);
62
63         if (p.patron) {
64                 if (typeof(p.patron) == 'object') {
65                         //p._patron = p.patron;
66                         p.set_patron( p.patron );
67                         p.display_patron();
68                 } else
69                         p.retrieve_patron_via_barcode( p.patron );
70         }
71
72         if (p._patron && p._patron.alert_message()) {
73                 //snd_bad(); snd_bad();
74                 s_alert(
75                         'PATRON ALERT MESSAGE\n\n\n\n' +
76                         p._patron.alert_message() +
77                         '\n\n\n\nTo remove this alert permanently, Edit the patron and erase the message in "Alert Message".\n\n'
78                 );
79         }
80
81         sdump('D_TRACE_EXIT',arg_dump(arguments));
82         return p;
83 }
84
85 function set_patron_display_widgets(p) {
86         p.commandset_node.getElementsByAttribute('id','cmd_patron_refresh')[0].addEventListener(
87                 'command',
88                 function (ev) {
89                         p.refresh();
90                 },
91                 false
92         );
93
94         function gen_func(i) {
95                 // because otherwise i would be 5 for each closure
96                 return function(ev) {
97                         p.clamshell.set_second_deck(i);
98                 };
99         }
100         var cmds = [ 'cmd_patron_checkout', 'cmd_patron_items', 'cmd_patron_holds', 
101                 'cmd_patron_bills', 'cmd_patron_edit', 'cmd_patron_info' ]
102         for (var i in cmds) {
103                 p.commandset_node.getElementsByAttribute('id',cmds[i])[0].addEventListener(
104                         'command',
105                         gen_func(i),
106                         false
107                 );
108         }
109         p.commandset_node.getElementsByAttribute('id','cmd_patron_checkout')[0].addEventListener(
110                 'command',
111                 function () {
112                         focus_widget( p.w.document, 'patron_checkout_barcode_entry_textbox' );
113                 },
114                 false
115         );
116         p.commandset_node.getElementsByAttribute('id','cmd_patron_bills')[0].addEventListener(
117                 'command',
118                 function () {
119                         focus_widget( p.w.document, 'bill_payment_amount_textbox' );
120                 },
121                 false
122         );
123         p.commandset_node.getElementsByAttribute('id','cmd_patron_edit')[0].addEventListener(
124                 'command',
125                 function () {
126                         p.redraw_patron_edit();
127                 },
128                 false
129         );
130
131
132
133
134 }
135
136
137 function patron_display_clamshell_init(p) {
138         p.clamshell = clam_shell_init( { 'w' : p.w, 'node' : p.clamshell_node, 'debug' : p.app } );
139         p.left_panel = p.clamshell.first_deck;
140         p.right_panel = p.clamshell.second_deck;
141 }
142
143 function patron_display_patron_items_init(p) {
144         p.patron_items = patron_items_init( { 'w' : p.w, 'node' : p.patron_items_node, 'debug' : p.app } );
145
146         p.w.document.getElementById('item_print').addEventListener(
147                 'command',
148                 function(ev) {
149                         var params = { 
150                                 'au' : p._patron, 
151                                 'lib' : mw.G.user_ou,
152                                 'staff' : mw.G.user,
153                                 'header' : mw.G.itemsout_header,
154                                 'line_item' : mw.G.itemsout_line_item,
155                                 'footer' : mw.G.itemsout_footer
156                         };
157                         mw.print_itemsout_receipt( params );
158                 }, false
159         );
160
161         p.redraw_patron_items = function() {
162                 p.patron_items.clear_patron_items();
163                 if (!p._patron.checkouts()) patron_get_checkouts( p._patron );
164                 for (var i = 0; i < p._patron.checkouts().length; i++) {
165                         p.patron_items.add_patron_items( [ i ] );
166                 }
167         }
168
169         p.patron_items.register_patron_items_select_callback(
170                 function (ev) {
171                         sdump('D_PATRON_DISPLAY','Firing patron_items_select_callback\n');
172                         sdump('D_PATRON_DISPLAY','ev.target = ' + ev.target + '\n');
173                         var patron_items = get_list_from_tree_selection( p.patron_items.paged_tree.tree );
174                         /* grab cover art for selected item? */
175                 }
176         );
177         p.patron_items.register_flesh_patron_items_function(
178                 function (treeitem) {
179                         sdump('D_PATRON_DISPLAY','Firing flesh_patron_items_function\n');
180                         var record_id = treeitem.getAttribute('record_id'); 
181                         p.patron_items.map_patron_items_to_cols( p._patron.checkouts()[ record_id ], treeitem );
182                 }
183         );
184         p.patron_items.register_item_context_builder(
185                 function (ev) {
186                         sdump('D_PATRON_DISPLAY','Firing context_builder for patron_items\n');
187                         sdump('D_PATRON_DISPLAY','ev.target = ' + ev.target + '\np.patron_items.paged_tree.popup = ' + p.patron_items.paged_tree.popup + '\n');
188                         empty_widget(p.patron_items.paged_tree.popup);
189                         var patron_items = get_list_from_tree_selection( p.patron_items.paged_tree.tree );
190                         sdump('D_PATRON_DISPLAY','patron_items.length = ' + patron_items.length + '\n');
191
192                         /*** RENEW ***/
193                         var menuitem_pi_r = p.patron_items.paged_tree.w.document.createElement('menuitem');
194                         p.patron_items.paged_tree.popup.appendChild( menuitem_pi_r );
195                         menuitem_pi_r.setAttribute('label',getString('circ.context_renew'));
196                         menuitem_pi_r.addEventListener(
197                                 'command',
198                                 function (ev) {
199                                         sdump('D_PATRON_DISPLAY','Firing renew context for patron_items\n');
200                                         for (var i = 0; i < patron_items.length; i++) {
201                                                 try {
202                                                         var idx = patron_items[i].getAttribute('record_id'); 
203                                                         var circ = p._patron.checkouts()[ idx ].circ;
204                                                         renew_by_circ_id( circ.id() );
205                                                         p.refresh();
206                                                 } catch(E) {
207                                                         alert(E);
208                                                 }
209                                         }
210                                 },
211                                 false
212                         );
213
214                         /*** CHECKIN ***/
215                         var menuitem_pi_c = p.patron_items.paged_tree.w.document.createElement('menuitem');
216                         p.patron_items.paged_tree.popup.appendChild( menuitem_pi_c );
217                         menuitem_pi_c.setAttribute('label',getString('circ.context_checkin'));
218                         menuitem_pi_c.addEventListener(
219                                 'command',
220                                 function (ev) {
221                                         sdump('D_PATRON_DISPLAY','Firing checkin context for patron_items\n');
222                                         for (var i = 0; i < patron_items.length; i++) {
223                                                 try {
224                                                         var idx = patron_items[i].getAttribute('record_id'); 
225                                                         var copy = p._patron.checkouts()[ idx ].copy;
226                                                         var check = checkin_by_copy_barcode( copy.barcode(), null );
227                                                         if (check != null && check.status == 0) {
228                                                                 check.route_to = mw.G.org_tree_hash[check.route_to].shortname();
229                                                                 alert('Check In: ' + check.text + '  Route To: ' + check.route_to);
230                                                         }
231                                                         p.refresh();
232                                                 } catch(E) {
233                                                         alert(E);
234                                                 }
235                                         }
236                                 },
237                                 false
238                         );
239
240                         /*** LOST ***/
241                         var menuitem_pi_r = p.patron_items.paged_tree.w.document.createElement('menuitem');
242                         p.patron_items.paged_tree.popup.appendChild( menuitem_pi_r );
243                         menuitem_pi_r.setAttribute('label',getString('circ.context_lost'));
244                         menuitem_pi_r.addEventListener(
245                                 'command',
246                                 function (ev) {
247                                         sdump('D_PATRON_DISPLAY','Firing lost context for patron_items\n');
248                                         for (var i = 0; i < patron_items.length; i++) {
249                                                 try {
250                                                         var idx = patron_items[i].getAttribute('record_id'); 
251                                                         var circ = p._patron.checkouts()[ idx ].circ;
252                                                         mark_circ_as_lost( circ ) ;
253                                                         p.refresh();
254                                                 } catch(E) {
255                                                         alert(E);
256                                                 }
257                                         }
258                                 },
259                                 false
260                         );
261
262                         /* separator */
263                         var menuitem_pi_s = p.patron_items.paged_tree.w.document.createElement('menuseparator');
264                         p.patron_items.paged_tree.popup.appendChild( menuitem_pi_s );
265                         
266
267                         /*** COPY EDITOR ***/
268                         var menuitem_pi_ce = p.patron_items.paged_tree.w.document.createElement('menuitem');
269                         p.patron_items.paged_tree.popup.appendChild( menuitem_pi_ce );
270                         menuitem_pi_ce.setAttribute('label',getString('circ.context_edit'));
271                         menuitem_pi_ce.addEventListener(
272                                 'command',
273                                 function (ev) {
274                                         alert('Not Yet Implemented');
275                                         sdump('D_PATRON_DISPLAY','Firing copy editor context for patron_items\n');
276                                         for (var i = 0; i < patron_items.length; i++) {
277                                                 sdump('D_PATRON_DISPLAY','Firing copy edit context\n');
278                                         }
279                                 },
280                                 false
281                         );
282
283                         /*** OPAC ***/
284                         var menuitem_pi_o = p.patron_items.paged_tree.w.document.createElement('menuitem');
285                         p.patron_items.paged_tree.popup.appendChild( menuitem_pi_o );
286                         menuitem_pi_o.setAttribute('label',getString('circ.context_opac'));
287                         menuitem_pi_o.addEventListener(
288                                 'command',
289                                 function (ev) {
290                                         alert('Not Yet Implemented');
291                                         sdump('D_PATRON_DISPLAY','Firing opac context for patron_items\n');
292                                         for (var i = 0; i < patron_items.length; i++) {
293                                                 sdump('D_PATRON_DISPLAY','Firing opac context\n');
294                                         }
295                                 },
296                                 false
297                         );
298                         
299                 }
300         );
301 }
302
303 function patron_display_patron_checkout_items_init(p) {
304         p.patron_checkout_items = patron_checkout_items_init( { 'w' : p.w, 'node' : p.patron_checkout_items_node, 'debug' : p.app } );
305         var tb = p.patron_checkout_items_node.getElementsByAttribute('id','patron_checkout_barcode_entry_textbox')[0];
306         var submit_button = p.patron_checkout_items_node.getElementsByAttribute('id','patron_checkout_submit_barcode_button')[0];
307
308         var checkouts = [];
309
310         function print_receipt() {
311                 p._patron._current_checkouts = checkouts;
312                 var params = { 
313                         'au' : p._patron, 
314                         'lib' : mw.G.user_ou,
315                         'staff' : mw.G.user,
316                         'header' : mw.G.checkout_header,
317                         'line_item' : mw.G.checkout_line_item,
318                         'footer' : mw.G.checkout_footer
319                 };
320                 mw.print_checkout_receipt( params );
321         }
322
323         p.w.document.getElementById('checkout_print').addEventListener( 'command',print_receipt, false);
324
325         p.w.document.getElementById('checkout_done').addEventListener(
326                 'command',
327                 function () {
328                         if (p.w.document.getElementById('checkout_auto').checked) print_receipt(); 
329                         checkouts = []; p.display_patron(); tb.focus();
330                 },
331                 false
332         );
333         p.attempt_checkout = function(barcode) {
334                 try {
335                         //if (! is_barcode_valid(barcode) ) throw('Invalid Barcode');
336                         var permit_check = checkout_permit( barcode, p._patron.id(), 0 );
337                         if (permit_check.status == 0) {
338                                 var check = checkout_by_copy_barcode( barcode, p._patron.id() );
339                                 if (check) {
340                                         checkouts.push( check );
341                                         p.patron_checkout_items.add_checkout_items( [ checkouts.length - 1 ] );
342                                         var temp = p._patron.checkouts();
343                                         temp.push( check );
344                                         p._patron.checkouts( temp );
345                                         render_fm(p.w.document, { 'au' : p._patron }); // p.display_patron();
346                                         p.redraw_patron_items();
347                                         tb.value = '';
348                                 }
349                         } else {
350                                 throw(permit_check.text);
351                         }
352                 } catch(E) {
353                         tb.select();
354                         if (typeof(E) == 'object') {
355                                 handle_error(E,true);
356                         } else {
357                                 s_alert(E);
358                         }
359                 }
360                 tb.focus();
361         }
362
363         tb.addEventListener(
364                 'keypress',
365                 function(ev) {
366                         if (ev.keyCode == 13 || ev.keyCode == 77 ) { p.attempt_checkout( tb.value ); }
367                 },
368                 false
369         );
370         submit_button.addEventListener(
371                 'command',
372                 function(ev) {
373                         p.attempt_checkout( tb.value );
374                 },
375                 false
376         );
377
378         p.redraw_patron_checkout_items = function() {
379                 p.patron_checkout_items.clear_checkout_items();
380                 for (var i = 0; i < checkouts.length; i++) {
381                         p.patron_checkout_items.add_checkout_items( [ i ] );
382                 }
383         }
384
385         p.patron_checkout_items.register_patron_checkout_items_select_callback(
386                 function (ev) {
387                         sdump('D_PATRON_DISPLAY','Firing patron_checkout_items_select_callback\n');
388                         sdump('D_PATRON_DISPLAY','ev.target = ' + ev.target + '\n');
389                         var patron_checkout_items = get_list_from_tree_selection( p.patron_checkout_items.paged_tree.tree );
390                         /* grab cover art for selected item? */
391                 }
392         );
393         p.patron_checkout_items.register_flesh_patron_checkout_items_function(
394                 function (treeitem) {
395                         sdump('D_PATRON_DISPLAY','Firing flesh_patron_checkout_items_function\n');
396                         var record_id = treeitem.getAttribute('record_id'); 
397                         p.patron_checkout_items.map_patron_checkout_items_to_cols( checkouts[ record_id ], treeitem );
398                 }
399         );
400         p.patron_checkout_items.register_checkout_context_builder(
401                 function (ev) {
402                         sdump('D_PATRON_DISPLAY','Firing context_builder for patron_checkout_items\n');
403                         sdump('D_PATRON_DISPLAY','ev.target = ' + ev.target + '\np.patron_checkout_items.paged_tree.popup = ' + p.patron_checkout_items.paged_tree.popup + '\n');
404                         empty_widget(p.patron_checkout_items.paged_tree.popup);
405                         var patron_checkout_items = get_list_from_tree_selection( p.patron_checkout_items.paged_tree.tree );
406
407                         /*** CHECKIN ***/
408                         var menuitem_pci_c = p.patron_checkout_items.paged_tree.w.document.createElement('menuitem');
409                         p.patron_checkout_items.paged_tree.popup.appendChild( menuitem_pci_c );
410                         menuitem_pci_c.setAttribute('label',getString('circ.context_checkin'));
411                         menuitem_pci_c.addEventListener(
412                                 'command',
413                                 function (ev) {
414                                         sdump('D_PATRON_DISPLAY','Firing checkin context for patron_checkout_items\n');
415                                         var keep_these = [];
416                                         for (var i = 0; i < patron_checkout_items.length; i++) {
417                                                 try {
418                                                         var idx = patron_checkout_items[i].getAttribute('record_id'); 
419                                                         var copy = checkouts[ idx ].copy;
420                                                         var check = checkin_by_copy_barcode( copy.barcode(), null );
421                                                         if (check == null) { // change this to whatever it takes
422                                                                 keep_these.push( checkouts[ idx ] );    
423                                                         }
424                                                         if (check != null && check.status == 0) {
425                                                                 check.route_to = mw.G.org_tree_hash[check.route_to].shortname();
426                                                                 alert('Check In: ' + check.text + '  Route To: ' + check.route_to);
427                                                         }
428                                                         checkouts = keep_these;
429                                                         p.refresh();
430                                                 } catch(E) {
431                                                         alert(E);
432                                                 }
433                                         }
434                                 },
435                                 false
436                         );
437
438                         /* separator */
439                         var menuitem_pci_s = p.patron_checkout_items.paged_tree.w.document.createElement('menuseparator');
440                         p.patron_checkout_items.paged_tree.popup.appendChild( menuitem_pci_s );
441                         
442
443                         /*** COPY EDITOR ***/
444                         var menuitem_pci_ce = p.patron_checkout_items.paged_tree.w.document.createElement('menuitem');
445                         p.patron_checkout_items.paged_tree.popup.appendChild( menuitem_pci_ce );
446                         menuitem_pci_ce.setAttribute('label',getString('circ.context_edit'));
447                         menuitem_pci_ce.addEventListener(
448                                 'command',
449                                 function (ev) {
450                                         alert('Not Yet Implemented');
451                                         sdump('D_PATRON_DISPLAY','Firing copy editor context for patron_checkout_items\n');
452                                         for (var i = 0; i < patron_checkout_items.length; i++) {
453                                                 var idx = patron_checkout_items[i].getAttribute('record_id');
454                                                 sdump('D_PATRON_DISPLAY','Firing copy edit context\n');
455                                         }
456                                 },
457                                 false
458                         );
459
460                         /*** OPAC ***/
461                         var menuitem_pci_o = p.patron_checkout_items.paged_tree.w.document.createElement('menuitem');
462                         p.patron_checkout_items.paged_tree.popup.appendChild( menuitem_pci_o );
463                         menuitem_pci_o.setAttribute('label',getString('circ.context_opac'));
464                         menuitem_pci_o.addEventListener(
465                                 'command',
466                                 function (ev) {
467                                         alert('Not Yet Implemented');
468                                         sdump('D_PATRON_DISPLAY','Firing opac context for patron_checkout_items\n');
469                                         for (var i = 0; i < patron_checkout_items.length; i++) {
470                                                 var idx = patron_checkout_items[i].getAttribute('record_id');
471                                                 sdump('D_PATRON_DISPLAY','Firing opac context\n');
472                                         }
473                                 },
474                                 false
475                         );
476                         
477                 }
478         );
479 }
480
481 function patron_display_patron_holds_init(p) {
482         p.patron_holds = patron_holds_init( { 'w' : p.w, 'node' : p.patron_holds_node, 'debug' : p.app } );
483
484         p.redraw_patron_holds = function() {
485                 p.patron_holds.clear_patron_holds();
486                 if (!p._patron.hold_requests()) patron_get_holds( p._patron );
487                 for (var i = 0; i < p._patron.hold_requests().length; i++) {
488                         p.patron_holds.add_patron_holds( [ i ] );
489                 }
490         }
491
492         p.patron_holds.register_patron_holds_select_callback(
493                 function (ev) {
494                         sdump('D_PATRON_DISPLAY','Firing patron_holds_select_callback\n');
495                         sdump('D_PATRON_DISPLAY','ev.target = ' + ev.target + '\n');
496                         var patron_holds = get_list_from_tree_selection( p.patron_holds.paged_tree.tree );
497                         /* grab cover art for selected item? */
498                 }
499         );
500         p.patron_holds.register_flesh_patron_holds_function(
501                 function (treeitem) {
502                         sdump('D_PATRON_DISPLAY','Firing flesh_patron_holds_function\n');
503                         var record_id = treeitem.getAttribute('record_id'); 
504                         var hold = p._patron.hold_requests()[ record_id ];
505                         patron_get_hold_status(
506                                 hold,
507                                 function (request) {
508                                         var result = request.getResultObject();
509                                         hold.status( hold_status_as_text( result ) );
510                                         p.patron_holds.map_patron_holds_to_cols( hold, treeitem );
511                                 }
512                         );
513                 }
514         );
515         p.patron_holds.register_item_context_builder(
516                 function (ev) {
517                         sdump('D_PATRON_DISPLAY','Firing context_builder for patron_holds\n');
518                         sdump('D_PATRON_DISPLAY','ev.target = ' + ev.target + '\np.patron_holds.paged_tree.popup = ' + p.patron_holds.paged_tree.popup + '\n');
519                         empty_widget(p.patron_holds.paged_tree.popup);
520                         var patron_holds = get_list_from_tree_selection( p.patron_holds.paged_tree.tree );
521                         sdump('D_PATRON_DISPLAY','patron_holds.length = ' + patron_holds.length + '\n');
522
523                         /*** CANCEL HOLD ***/
524                         var menuitem_ph_ce = p.patron_holds.paged_tree.w.document.createElement('menuitem');
525                         p.patron_holds.paged_tree.popup.appendChild( menuitem_ph_ce );
526                         menuitem_ph_ce.setAttribute('label',getString('circ.context_cancel_hold'));
527                         menuitem_ph_ce.addEventListener(
528                                 'command',
529                                 function (ev) {
530                                         sdump('D_PATRON_DISPLAY','Firing cancel hold context for patron_holds\n');
531                                         for (var i = 0; i < patron_holds.length; i++) {
532                                                 sdump('D_PATRON_DISPLAY','Firing cancel edit context\n');
533                                                 var record_id = patron_holds[i].getAttribute('record_id');
534                                                 var hold = p._patron.hold_requests()[ record_id ];
535                                                 cancel_hold( hold );
536                                         }
537                                         p.refresh();
538                                 },
539                                 false
540                         );
541
542                         /* separator */
543                         var menuitem_ph_s = p.patron_holds.paged_tree.w.document.createElement('menuseparator');
544                         p.patron_holds.paged_tree.popup.appendChild( menuitem_ph_s );
545                         
546                         /*** COPY EDITOR ***/
547                         var menuitem_ph_ce = p.patron_holds.paged_tree.w.document.createElement('menuitem');
548                         p.patron_holds.paged_tree.popup.appendChild( menuitem_ph_ce );
549                         menuitem_ph_ce.setAttribute('label',getString('circ.context_edit'));
550                         menuitem_ph_ce.addEventListener(
551                                 'command',
552                                 function (ev) {
553                                         alert('Not Yet Implemented');
554                                         sdump('D_PATRON_DISPLAY','Firing copy editor context for patron_holds\n');
555                                         for (var i = 0; i < patron_holds.length; i++) {
556                                                 sdump('D_PATRON_DISPLAY','Firing copy edit context\n');
557                                         }
558                                 },
559                                 false
560                         );
561
562                         /*** OPAC ***/
563                         var menuitem_ph_o = p.patron_holds.paged_tree.w.document.createElement('menuitem');
564                         p.patron_holds.paged_tree.popup.appendChild( menuitem_ph_o );
565                         menuitem_ph_o.setAttribute('label',getString('circ.context_opac'));
566                         menuitem_ph_o.addEventListener(
567                                 'command',
568                                 function (ev) {
569                                         alert('Not Yet Implemented');
570                                         sdump('D_PATRON_DISPLAY','Firing opac context for patron_holds\n');
571                                         for (var i = 0; i < patron_holds.length; i++) {
572                                                 sdump('D_PATRON_DISPLAY','Firing opac context\n');
573                                         }
574                                 },
575                                 false
576                         );
577                         
578                 }
579         );
580 }
581
582 function patron_display_patron_bills_init(p) {
583         p.patron_bills = patron_bills_init( { 
584                 'w' : p.w, 
585                 'node' : p.patron_bills_node, 
586                 'debug' : p.app 
587         } );
588
589         p.patron_bills.refresh = function() { p.refresh(); }
590
591         p.redraw_patron_bills = function() {
592                 try {
593                         p.patron_bills.clear_patron_bills();
594                         if (!p._patron.bills) patron_get_bills( p._patron );
595                         p.patron_bills.add_patron_bills( p._patron.bills );
596                 } catch(E) {
597                         sdump('D_ERROR',js2JSON(E) + '\n');
598                 }
599         }
600 }
601
602 function patron_display_patron_edit_init(p) {
603         /*
604         p.patron_edit = patron_edit_init( { 
605                 'w' : p.w, 
606                 'node' : p.patron_edit_node, 
607                 'debug' : p.app
608         } );
609
610         p.patron_edit.redisplay = function() { p.display_patron( {'patron_edit':true} ); }
611         p.patron_edit.refresh = function() { p.refresh( {'patron_edit':true} ); }
612
613         p.redraw_patron_edit = function() {
614                 try {
615                         p.patron_edit.clear_patron_edit();
616                         p.patron_edit.add_rows( p._patron );
617                 } catch(E) {
618                         sdump('D_ERROR',js2JSON(E) + '\n');
619                 }
620         }
621         */
622         /* shoehorn in the old legacy stuff */
623         p.patron_edit = {};
624         p.redraw_patron_edit = function() { 
625                 empty_widget( p.patron_edit_node );
626                 setTimeout(
627                         function() {
628                                 var frame = p.w.document.createElement('iframe');
629                                 p.patron_edit_node.appendChild( frame );
630                                 frame.setAttribute('flex','1');
631                                 frame.setAttribute('src','chrome://evergreen/content/patron/patron_edit_legacy.xul');
632                                 frame.contentWindow.mw = mw;
633                                 frame.contentWindow.params = {};
634                                 var barcode = patron_get_barcode( p._patron );
635                                 frame.contentWindow.params.barcode = barcode;
636                                 frame.contentWindow.params._patron = p._patron;
637                                 frame.contentWindow.patron_save_callback = function ( params ) {
638                                         p._patron = params.au;
639                                         p.display_patron();
640                                 }
641                                 p.patron_edit.frame = frame;
642                         }, 0
643                 );
644         }
645 }
646
647 function patron_display_survey_init(p) {
648         var gb = p.w.document.getElementById('PatronDisplaySurvey_groupbox');
649         var count = gb.childNodes.length; for (var i = 1; i < count; i++) { gb.removeChild( gb.lastChild ); } // all but caption
650         mw.user_request(
651                 'open-ils.circ', 'open-ils.circ.survey.retrieve.required', [ mw.G.auth_ses[0] ],
652                 function (request) {
653                         var surveys = request.getResultObject();
654                         for (var i = 0; i < surveys.length; i++) {
655
656                                 var survey = a_get(surveys, i);
657                         
658                                 var hbox = document.createElement('hbox');
659                                 gb.appendChild(hbox);
660
661                                 var name_label = document.createElement('label');
662                                 name_label.setAttribute('value',survey.name());
663                                 hbox.appendChild(name_label);
664
665                                 var answer_label = document.createElement('label');
666                                 answer_label.setAttribute('value','');
667                                 hbox.appendChild(answer_label);
668
669                                 var taken_label = document.createElement('label');
670                                 taken_label.setAttribute('value','Not Taken');
671                                 hbox.appendChild(taken_label);
672
673                                 mw.user_request(
674                                         'open-ils.circ', 'open-ils.circ.survey.response.retrieve',
675                                         [ mw.G.auth_ses[0], survey.id(), p._patron.id() ],
676                                         function (request) {
677                                                 var responses = request.getResultObject();
678                                                 if (responses.length > 0) {
679                                                         var response = responses.pop(); // last response
680                                                         var date;
681                                                         if (response.effective_date()) {
682                                                                 date = formatted_date( response.effective_date(), '%D' );
683                                                         } else {
684                                                                 date = formatted_date( response.answer_date(), '%D' );
685                                                         }
686                                                         taken_label.setAttribute('value',date);
687                                                         var answer = find_id_object_in_list( // first answer
688                                                                 find_id_object_in_list(
689                                                                         survey.questions(),
690                                                                         response.question()
691                                                                 ).answers(),
692                                                                 response.answer()
693                                                         ).answer();
694                                                         answer_label.setAttribute('value',answer);
695                                                 }
696                                         }
697                                 );
698                         }
699                 }
700         );
701 }