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