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