add_css_class is now addCSSClass
[Evergreen.git] / Evergreen / staff_client / chrome / content / evergreen / patron / patron_edit_legacy.js
1 mw.sdump('D_LEGACY','Loading patron.js\n');
2 //var newdoctype = document.implementation.createDocumentType("HTML", "-//W3C//DTD HTML 4.01 Transitional//EN","");
3 var HTMLdoc;// = document.implementation.createDocument("", "", newdoctype);
4 try {
5         if (typeof(PATRON)=='object') { 
6                 mw.sdump('D_LEGACY','Already loaded\n');
7                 exit; 
8         }
9 } catch(E) {
10 }
11 var PATRON = {};
12 PATRON['search'] = {};
13 PATRON['search_results'] = [];
14 PATRON['checkouts'] = [];
15 var hash_aua = {};
16 var hash_au = {};
17 var hash_ac = {};
18 var backup_au = {};
19 //var hash_ap = {};
20 var response_list = [];
21 var local_stat_cats = [];
22 var local_stat_cat_entries = {};
23 var patron_hits_per_page = 20;
24
25 var new_id = -1;
26
27 var patron_list_columns = [ 
28         { 'f' : 'prefix', 'v' : 'Prefix', 'hidden' : true },
29         { 'f' : 'family_name', 'v' : 'Family Name', 'primary' : true },
30         { 'f' : 'first_given_name', 'v' : 'First Name' },
31         { 'f' : 'second_given_name', 'v' : 'Middle Name' },
32         { 'f' : 'suffix', 'v' : 'Suffix', 'hidden' : false },
33         { 'f' : 'email', 'v' : 'Email', 'hidden' : true  },
34         { 'f' : 'day_phone', 'v' : 'Day Phone', 'hidden' : false },
35         { 'f' : 'evening_phone', 'v' : 'Evening Phone', 'hidden' : true },
36         { 'f' : 'other_phone', 'v' : 'Other Phone', 'hidden' : true },
37         { 'f' : 'ident_value', 'v' : 'Ident Value', 'hidden' : false },
38         { 'f' : 'ident_type', 'v' : 'Ident Type', 'hidden' : true },
39         { 'f' : 'ident_value2', 'v' : 'Ident Value 2', 'hidden' : true },
40         { 'f' : 'ident_type2', 'v' : 'Ident Type 2', 'hidden' : true},
41         { 'f' : 'dob', 'v' : 'Date of Birth', 'hidden' : true},
42         { 'f' : 'active', 'v' : 'Active', 'hidden' : true},
43         { 'f' : 'expire_date', 'v' : 'Expire Date', 'hidden' : true},
44         { 's' : 'home_ou', 'f' : 'shortname', 'v' : 'Home Lib', 'hidden' : true},
45         { 'f' : 'usrname', 'v' : 'Login Name', 'hidden' : true},
46         { 'f' : 'usrgroup', 'v' : 'Group', 'hidden' : true},
47         { 's' : 'mailing_address', 'f' : 'street1', 'v' : 'Mailing Address - Street 1', 'hidden' : true},
48         { 's' : 'mailing_address', 'f' : 'street2', 'v' : 'Mailing Address - Street 2', 'hidden' : true},
49         { 's' : 'mailing_address', 'f' : 'city', 'v' : 'Mailing Address - City', 'hidden' : true},
50         { 's' : 'mailing_address', 'f' : 'state', 'v' : 'Mailing Address - State', 'hidden' : true},
51         { 's' : 'mailing_address', 'f' : 'post_code', 'v' : 'Mailing Address - ZIP', 'hidden' : false},
52         { 's' : 'billing_address', 'f' : 'street1', 'v' : 'Physical Address - Street 1', 'hidden' : true},
53         { 's' : 'billing_address', 'f' : 'street2', 'v' : 'Physical Address - Street 2', 'hidden' : true},
54         { 's' : 'billing_address', 'f' : 'city', 'v' : 'Physical Address - City', 'hidden' : true},
55         { 's' : 'billing_address', 'f' : 'state', 'v' : 'Physical Address - State', 'hidden' : true},
56         { 's' : 'billing_address', 'f' : 'post_code', 'v' : 'Physical Address - ZIP', 'hidden' : false}
57 ];
58
59
60 function patron_init() {
61         mw.sdump('D_LEGACY','**** TESTING: patron.js: patron_init(): ' + mw.G.main_test_variable + '\n');
62         mw.sdump('D_LEGACY','PATRON = ' + js2JSON(PATRON) + '\n');
63         var textbox = document.getElementById('patron_scan_textbox');
64         if (textbox) textbox.addEventListener("keypress",patron_handle_keypress,false);
65         /* 
66         Originally, the idea here was to setup the PATRON object based on barcode if the bundle loader
67         set a params.barcode.  This would also switch the barcode scan prompt to the patron summary
68         sidebar.
69         */
70         patron_init_if_barcode();
71         document.getElementById('patron_new_card_button').addEventListener(
72                 'command',
73                 function(ev) { 
74                         var a = yns_alert('Activate a new PINES card?','PINES Card','Yes - Previous card lost/stolen','No',null,'Check here to confirm'); 
75                         if (a == 0) {
76                                 var card = new ac(); card.id( new_id-- ); card.isnew( '1' ); 
77                                 card.barcode( 'REQUIRED' ); //PATRON.barcode = 'REQUIRED'; 
78                                 card.usr( PATRON.au.id() ); 
79                                 hash_ac[ card.id() ] =  card; 
80                                 PATRON.au.card( card ); 
81                                 var cards = PATRON.au.cards(); 
82                                 for (var i = 0; i < cards.length; i++) { cards[i].active('0'); cards[i].ischanged('1'); }
83                                 cards.push( card ); PATRON.au.cards( cards );
84                                 var barcode_e = document.getElementById('patron_edit_system_barcode_textbox');
85                                 barcode_e.disabled = false; barcode_e.value = 'REQUIRED';
86                         }
87                 },
88                 false
89         );
90 }
91
92 function patron_handle_keypress(ev) {
93         mw.sdump('D_LEGACY',arg_dump(arguments));
94         if (ev.keyCode && ev.keyCode == 13) {
95                 switch(this) {
96                         case document.getElementById('patron_scan_textbox') :
97                                 ev.preventDefault();
98                                 PATRON.scan_submit(ev);
99                         break;
100                         default:
101                         break;
102                 }
103         }
104 }
105
106 function patron_new_init() {
107         mw.sdump('D_LEGACY',arg_dump(arguments));
108         mw.sdump('D_LEGACY','**** TESTING: patron.js: patron_new_init(): ' + mw.G.main_test_variable + '\n');
109         mw.sdump('D_LEGACY','PATRON = ' + js2JSON(PATRON) + '\n');
110         PATRON.au = new au();
111         PATRON.au.id( new_id-- );
112         PATRON.au.isnew( '1' );
113         PATRON.au.profile( mw.G.ap_list[0].id() );
114         PATRON.au.ident_type( mw.G.cit_list[0].id() );
115         PATRON.au.home_ou( mw.G.user_ou.id() );
116         PATRON.au.addresses( [] );
117         PATRON.au.stat_cat_entries( [] );
118
119                 var card = new ac();
120                 card.id( new_id-- );
121                 card.isnew( '1' );
122                 card.barcode( 'REQUIRED' );
123                 //PATRON.barcode = 'REQUIRED';
124                 card.usr( PATRON.au.id() );
125                 hash_ac[ card.id() ] =  card;
126
127         PATRON.au.card( card );
128         PATRON.au.cards( [ card ] );
129         PATRON.related_refresh(PATRON.au.id());
130
131         var passwd = document.getElementById( 'patron_edit_system_new_passwd_textbox' );
132         passwd.value = Math.floor( (Math.random() * 9000) + 1000 );
133         PATRON.au.passwd( passwd.value );
134
135         var textbox = document.getElementById( 'patron_edit_system_barcode_textbox' );
136         textbox.select(); textbox.focus();
137 }
138
139 function patron_edit_init() {
140         mw.sdump('D_LEGACY','**** TESTING: patron.js: patron_edit_init(): ' + mw.G.main_test_variable + '\n');
141         mw.sdump('D_LEGACY','PATRON = ' + js2JSON(PATRON) + '\n');
142         //mw.sdump('D_LEGACY','PATRON.au.array.length = ' + PATRON.au.array.length + '\n');
143         populate_patron_edit_library_menu();
144         populate_patron_edit_prefix_menu();
145         populate_patron_edit_suffix_menu();
146         populate_patron_edit_profile_menu();
147         populate_patron_edit_ident_type_menu();
148         populate_patron_edit_ident_type_menu2();
149         PATRON.summary_refresh();
150         local_stat_cats = mw.G.actsc_list;
151         populate_rows_with_local_stat_cats(
152                 local_stat_cats,
153                 local_stat_cat_entries,
154                 'pescg_rows',
155                 true
156         );
157         PATRON.summary_refresh();
158 }
159
160 function patron_init_if_barcode() {
161         mw.sdump('D_LEGACY',arg_dump(arguments));
162         try {
163                 if ( params.barcode ) {
164                         mw.sdump('D_LEGACY','patron_init(): patron.refresh()\n');
165                         PATRON.retrieve_patron(params.barcode);
166                         var deck = document.getElementById('patron_scan_deck');
167                         if (deck) { deck.setAttribute('selectedIndex','1'); }
168                 }
169         } catch(E) {
170                 //mw.sdump('D_LEGACY','patron_init_if_barcode ERROR: ' + js2JSON(E) + '\n');
171                 mw.sdump('D_LEGACY','patron_init_if_barcode failed\n');
172         }
173 }
174
175 function populate_patron_edit_library_menu() {
176         mw.sdump('D_LEGACY',arg_dump(arguments));
177         try {
178                 //populate_lib_list(
179                 populate_lib_list_with_branch(
180                         'patron_edit_system_library_menulist',
181                         'patron_edit_system_library_menupopup',
182                         mw.G.user_ou,
183                         mw.G.org_tree,
184                         true
185                 );
186         } catch(E) {
187                 mw.sdump('D_LEGACY','populate_patron_edit_library_menu ERROR: ' + js2JSON(E) + '\n');
188                 //mw.sdump('D_LEGACY','populate_patron_edit_library_menu failed\n');
189         }
190 }
191
192 function set_patron_edit_library_menu() {
193         mw.sdump('D_LEGACY',arg_dump(arguments));
194         var menuitem_id = 
195                 'libitem' +
196                 find_ou(
197                         mw.G.org_tree,
198                         PATRON.au.home_ou()
199                 ).id();
200         var menuitem = document.getElementById(
201                 menuitem_id
202         );
203         var homelib_e = document.getElementById('patron_edit_system_library_menulist');
204         if (homelib_e) { 
205                 homelib_e.selectedItem = menuitem; 
206                 mw.sdump('D_LEGACY','\tShould be set\n');
207         }
208 }
209
210 function populate_patron_edit_prefix_menu() {
211         mw.sdump('D_LEGACY',arg_dump(arguments));
212         try {
213                 populate_name_prefix(
214                         'patron_edit_system_prefix_menulist',
215                         'patron_edit_system_prefix_menupopup'   
216                 );
217         } catch(E) {
218                 //mw.sdump('D_LEGACY','populate_patron_edit_prefix_menu ERROR: ' + js2JSON(E) + '\n');
219                 mw.sdump('D_LEGACY','populate_patron_edit_prefix_menu failed\n');
220         }
221 }
222
223 function populate_patron_edit_suffix_menu() {
224         mw.sdump('D_LEGACY',arg_dump(arguments));
225         try {
226                 populate_name_suffix(
227                         'patron_edit_system_suffix_menulist',   
228                         'patron_edit_system_suffix_menupopup'   
229                 );
230         } catch(E) {
231                 //mw.sdump('D_LEGACY','populate_patron_edit_suffix_menu ERROR: ' + js2JSON(E) + '\n');
232                 mw.sdump('D_LEGACY','populate_patron_edit_suffix_menu failed\n');
233         }
234 }
235
236 function populate_patron_edit_profile_menu() {
237         mw.sdump('D_LEGACY',arg_dump(arguments));
238         try {
239                 populate_user_profile(
240                         'patron_edit_system_profile_menulist',  
241                         'patron_edit_system_profile_menupopup'
242                 );
243         } catch(E) {
244                 //mw.sdump('D_LEGACY','populate_patron_edit_profile_menu ERROR: ' + js2JSON(E) + '\n');
245                 mw.sdump('D_LEGACY','populate_patron_edit_profile_menu failed\n');
246         }
247 }
248
249 function set_patron_edit_profile_menu() {
250         mw.sdump('D_LEGACY',arg_dump(arguments));
251         var menuitem_id = 'apitem' + PATRON.au.profile();
252         var menuitem = document.getElementById(
253                 menuitem_id
254         );
255         var profile_e = document.getElementById('patron_edit_system_profile_menulist');
256         if (profile_e) { profile_e.selectedItem = menuitem; }
257 }
258
259 function populate_patron_edit_ident_type_menu() {
260         mw.sdump('D_LEGACY',arg_dump(arguments));
261         try {
262                 populate_ident_types(
263                         'patron_edit_system_id1type_menulist',  
264                         'patron_edit_system_id1type_menupopup',
265                         '1'
266                 );
267         } catch(E) {
268                 mw.sdump('D_LEGACY','populate_patron_edit_ident_type_menu ERROR: ' + js2JSON(E) + '\n');
269                 //mw.sdump('D_LEGACY','populate_patron_edit_ident_type_menu failed\n');
270         }
271 }
272
273 function set_patron_edit_ident_type_menu() {
274         mw.sdump('D_LEGACY',arg_dump(arguments));
275         var menuitem_id = 'cit1item' + PATRON.au.ident_type();
276         var menuitem = document.getElementById(
277                 menuitem_id
278         );
279         var id1 = document.getElementById('patron_edit_system_id1type_menulist');
280         if (id1) { id1.selectedItem = menuitem; }
281 }
282
283 function populate_patron_edit_ident_type_menu2() {
284         mw.sdump('D_LEGACY',arg_dump(arguments));
285         try {
286                 populate_ident_types(
287                         'patron_edit_system_id2type_menulist',  
288                         'patron_edit_system_id2type_menupopup',
289                         '2'
290                 );
291         } catch(E) {
292                 //mw.sdump('D_LEGACY','populate_patron_edit_ident_type_menu2 ERROR: ' + js2JSON(E) + '\n');
293                 mw.sdump('D_LEGACY','populate_patron_edit_ident_type_menu2 failed\n');
294         }
295 }
296
297 function set_patron_edit_ident_type_menu2() {
298         mw.sdump('D_LEGACY',arg_dump(arguments));
299         var menuitem_id = 'cit2item' + PATRON.au.ident_type2();
300         var menuitem = document.getElementById(
301                 menuitem_id
302         );
303         var id2_e = document.getElementById('patron_edit_system_id2type_menulist');
304         if (id2_e) { id2_e.selectedItem = menuitem; }
305 }
306
307 function populate_patron_survey_grid(grid) {
308         mw.sdump('D_LEGACY',arg_dump(arguments));
309         if (typeof(grid) != 'object') {
310                 grid = document.getElementById(grid);
311         }
312         if (!grid) { return; }
313         var rows = grid.lastChild; if (!rows) { return; }
314         try {
315                 empty_widget( rows );
316                 // 'open-ils.circ.survey.required.retrieve',
317                 // 'open-ils.circ.survey.retrieve.all',
318                 var result = mw.user_request(
319                         'open-ils.circ',
320                         'open-ils.circ.survey.retrieve.required',
321                         [ mw.G.auth_ses[0] ]
322                 )[0];
323                 if (typeof(result) != 'object') { throw('survey.retrieve.all did not return an object'); }
324                 var desc_1 = new Object();
325                 var desc_3 = new Object();
326                 var survey_hash = new Object();
327                 for (var i in result) {
328                         var survey = result[i];
329                         survey_hash[ survey.id() ] = survey; 
330                         if ( (survey.required() == '0') && (survey.usr_summary() == '0') ) { continue; }
331                         mw.sdump('D_LEGACY','Survey: ' + survey.id() + ' : ' + survey.name() + '\n');
332                         var row = document.createElement('row');
333                         rows.appendChild(row);
334                         desc_1[ survey.id() ] = document.createElement('description');
335                                 desc_1[ survey.id() ].setAttribute('value', 'Not Taken');
336                         row.appendChild(desc_1[ survey.id() ]);
337                         var desc_2 = document.createElement('label');
338                                 desc_2.setAttribute('class','link');
339                                 desc_2.setAttribute('onclick','survey_test(event,' + survey.id() + ');');
340                         row.appendChild(desc_2);
341                         desc_2.setAttribute('value', survey.name() );
342                         desc_3[ survey.id() ] = document.createElement('description');
343                         row.appendChild(desc_3[ survey.id() ]);
344
345                         if (survey.required() == '1') {
346                                 row.setAttribute('hidden','false');
347                         } else {
348                                 row.setAttribute('hidden','true');
349                         }
350                         mw.sdump('D_LEGACY','creating desc_1: ' + desc_1 + '\n');
351                         var result2 = mw.user_async_request(
352                                 'open-ils.circ',
353                                 'open-ils.circ.survey.response.retrieve',
354                                 [ mw.G.auth_ses[0], survey.id(), PATRON.au.id() ],
355                                 function (request) {
356                                         var result2 = request.getResultObject();
357                                         mw.sdump('D_LEGACY','result2 = ' + js2JSON(result2) + '\n');
358                                         if (result2.length > 0) {
359                                                 var last_response = result2.pop();
360                                                 mw.sdump('D_LEGACY','desc_1 = ' + desc_1[ last_response.survey() ] + '\n');
361                                                 mw.sdump('D_LEGACY','effective_date = [' + last_response.effective_date() + ']  answer_date = [' + last_response.answer_date() + ']\n');
362                                                 var date = last_response.effective_date().substr(0,10);
363                                                 if (!date) { date = last_response.answer_date().substr(0,10); }
364                                                 var first_answer = '';
365                                                 try {
366                                                         first_answer = find_id_object_in_list(
367                                                                 find_id_object_in_list(
368                                                                         survey_hash[ last_response.survey() ].questions(),
369                                                                         last_response.question()
370                                                                 ).answers(),
371                                                                 last_response.answer()
372                                                         ).answer();
373                                                 } catch(E) {
374                                                         mw.sdump('D_LEGACY',js2JSON(E) + '\n');
375                                                 }
376                                                 desc_1[ last_response.survey() ].setAttribute('value', date);
377                                                 desc_3[ last_response.survey() ].setAttribute('value', first_answer);
378                                                 mw.sdump('D_LEGACY','desc_1 = ' + date + '\n');
379                                         }
380                                 }
381                         );
382                 }
383         } catch(E) {
384                 mw.sdump('D_LEGACY','populate_patron_edit_survey_grid ERROR: ' + js2JSON(E) + '\n');
385                 mw.handle_error(E);
386         }
387 }
388
389 function toggle_patron_survey_grid_rows(e,grid) {
390         mw.sdump('D_LEGACY',arg_dump(arguments));
391         var label = e.target.getAttribute('label');
392         var alt_label = e.target.getAttribute('alt_label');
393         e.target.setAttribute('label',alt_label);
394         e.target.setAttribute('alt_label',label);
395         toggle_hidden_grid_rows(grid);  
396 }
397
398 function retrieve_patron_by_barcode(barcode,method) {
399         mw.sdump('D_LEGACY',arg_dump(arguments));
400         if (!barcode) { barcode = PATRON.barcode(); }
401         mw.sdump('D_LEGACY','Entering PATRON.retrieve_patron() with barcode: ' + barcode + '\n');
402         //unregister_patron_window(this);
403         var result;
404         if (!method) method = 'open-ils.actor.user.fleshed.retrieve_by_barcode';
405         try {
406                 if (params._patron) {
407                         result = [ params._patron ];
408                 } else {
409                         result = mw.user_request(
410                                 'open-ils.actor',
411                                 method,
412                                 [ mw.G.auth_ses[0], barcode ]
413                         );
414                 }
415                 if (typeof(result[0]) != 'object') {
416                         mw.sdump('D_LEGACY','unexpected result1 : ' + typeof(result[0]) + ' : ' + js2JSON(result) + '\n');
417                         throw('unexpected result1 : ' + typeof(result[0]) + ' : ' + js2JSON(result) + '\n');
418                 }
419         } catch(E) {
420                 mw.sdump('D_LEGACY','error in search.actor.user.barcode\n' + js2JSON(E) + '\n');
421                 mw.handle_error(E);
422                 return false;
423         }
424         /*for (var i in result[0]) {
425                 var element = result[0][i];
426                 if (typeof(element) != 'function') {
427                         mw.sdump('D_LEGACY','Copying ' + i + ' to PATRON\n');
428                         PATRON[i] = element;
429                 }
430         }*/
431         PATRON.au = result[0];
432         //register_patron_window(this);
433         //PATRON.barcode = find_id_object_in_list(PATRON.au.cards(),PATRON.au.card()).barcode();
434         patron_callback('retrieve_patron');
435         return PATRON.related_refresh(PATRON.au.id());
436 }
437 PATRON.retrieve_patron_by_barcode = retrieve_patron_by_barcode;
438 PATRON.retrieve_patron = retrieve_patron_by_barcode;
439 PATRON.retrieve_via_method = retrieve_patron_by_barcode;
440 PATRON.refresh = retrieve_patron_by_barcode;
441
442 function get_barcode() {
443         mw.sdump('D_LEGACY',arg_dump(arguments));
444         try {
445                 //mw.sdump('D_LEGACY','PATRON.au.array.length = ' + PATRON.au.array.length + '\n');
446                 //mw.sdump('D_LEGACY','get_barcode: PATRON.au = ' + js2JSON(PATRON.au) + '\n.cards() = ' + js2JSON(PATRON.au.cards()) + '\n.card() = ' + js2JSON(PATRON.au.card()) + '\n');
447                 //return find_id_object_in_list(PATRON.au.cards(),PATRON.au.card()).barcode();
448                 return PATRON.au.card().barcode();
449         } catch(E) {
450                 mw.sdump('D_LEGACY','get_barcode() error == ' + js2JSON(E) + '\n');
451                 return '';
452         }
453 }
454 PATRON.barcode = get_barcode;
455
456 function validate_patron() {
457         mw.sdump('D_LEGACY',arg_dump(arguments));
458         //mw.sdump('D_LEGACY','validate_patron: PATRON.au = ' + js2JSON(PATRON.au) + '\nPATRON.au.array.length = ' + PATRON.au.array.length + '\n');
459         var s = '';
460         if ( PATRON.barcode() == 'REQUIRED') {
461                 if (!s) {
462                         var textbox = document.getElementById( 'patron_edit_system_barcode_textbox' );
463                         textbox.select(); textbox.focus();
464                 }
465                 s += ('Barcode required\n');
466         }
467         if ( ! PATRON.au.usrname() ) {
468                 if (!s) {
469                         var textbox = document.getElementById( 'patron_edit_system_usrname_textbox' );
470                         textbox.select(); textbox.focus();
471                 }
472                 s += ('Login Name required\n');
473         }
474
475         if ( ! PATRON.au.family_name() ) {
476                 if (!s) {
477                         var textbox = document.getElementById( 'patron_edit_system_family_name_textbox' );
478                         textbox.select(); textbox.focus();
479                 }
480                 s += ('Family Name required\n');
481         }
482         if ( ! PATRON.au.first_given_name() ) {
483                 if (!s) {
484                         var textbox = document.getElementById( 'patron_edit_system_first_given_name_textbox' );
485                         textbox.select(); textbox.focus();
486                 }
487                 s += ('First Given Name required\n');
488         }
489         if ( ! PATRON.au.ident_value() ) {
490                 if (!s) {
491                         var textbox = document.getElementById( 'patron_edit_system_id1value_textbox' );
492                         textbox.select(); textbox.focus();
493                 }
494                 s += ('Identification required\n');
495         }
496         if ( ! PATRON.au.dob() ) {
497                 if (!s) {
498                         var textbox = document.getElementById( 'patron_edit_system_dob_textbox' );
499                         textbox.select(); textbox.focus();
500                 }
501                 s += ('Date of Birth required\n');
502         } else {
503                 var date = PATRON.au.dob();
504                 var flag = false;
505                 var darray = date.split('-');
506                 var year = darray[0]; if ( (!year) || (year.length != 4) || (!parseInt(year)) ) flag = true;
507                 var month = darray[1]; if ( (!month) || (month.length !=2) || (!parseInt(month)) ) flag = true;
508                 var day = darray[2]; if ( (!day) || (day.length !=2) || (!parseInt(day)) ) flag = true;
509                 if (flag) {
510                         s += ('Date Format is YYYY-MM-DD\n');
511                 }
512                 if (!valid_year_month_day(year,month,day)) {
513                         s += ('Invalid Date\n');
514                 }
515         }
516         if ( ! PATRON.au.mailing_address() ) {
517                 s += ('Mailing Address required\n');
518         }
519         if ( ! PATRON.au.billing_address() ) {
520                 s += ('Billing Address required\n');
521         }
522         if (s) {
523                 mw.sdump('D_LEGACY',s); alert(s); return false;
524         }
525         return true;
526 }
527
528 function backup_patron(P) {
529         mw.sdump('D_LEGACY',arg_dump(arguments));
530         backup_au = P.au.clone();
531 }
532
533 function restore_patron(P) {
534         mw.sdump('D_LEGACY',arg_dump(arguments));
535         P.au = backup_au.clone();
536         //hash_ac[ P.au.card() ] = find_id_object_in_list( P.au.cards(), P.au.card() );
537         hash_ac[ P.au.card().id() ] = P.au.card();
538         hash_au[ P.au.id() ] = P.au;
539 }
540
541 function save_patron() {
542         mw.sdump('D_LEGACY',arg_dump(arguments));
543         mw.sdump('D_LEGACY','Entering PATRON.save()\n\n=-=-=-=-=-=-=-=\n\n');
544         mw.sdump('D_LEGACY','PATRON.au = ' + js2JSON(PATRON.au) + '\n');
545         mw.sdump('D_LEGACY','PATRON.au.a.length = ' + PATRON.au.a.length + '\n\n');
546         //var backup_json = js2JSON(PATRON.au);
547         //mw.sdump('D_LEGACY','backup_json = ' + backup_json + '\n\n');
548         backup_patron(PATRON);
549         mw.sdump('D_LEGACY','\n\n=-=-=-=-=-=-=-=-=-=-\n\n');
550         check_for_new_addresses();
551         check_for_new_stat_cats();
552         if (! validate_patron() ) { 
553                 mw.sdump('D_LEGACY','restoring backup\n');
554                 restore_patron(PATRON);
555                 return false; 
556         }
557         if (! PATRON.au.usrname() ) { 
558                 PATRON.au.usrname( 
559                         PATRON.barcode()
560                 );
561         }
562         PATRON.au.survey_responses( response_list );
563         mw.sdump('D_LEGACY','before PATRON.au = ' + js2JSON(PATRON.au) + '\n');
564         var result;
565         try {
566                 result = mw.user_request(
567                                 'open-ils.actor',
568                                 'open-ils.actor.patron.update',
569                                 [ mw.G.auth_ses[0], PATRON.au ]
570                         );
571                 if (typeof(result[0]) != 'object') {
572                         mw.sdump('D_LEGACY','unexpected result1 : ' + typeof(result[0]) + ' : ' + js2JSON(result) + '\n');
573                         throw('unexpected result1 : ' + typeof(result[0]) + ' : ' + js2JSON(result) + '\n');
574                 }
575         } catch(E) {
576                 mw.sdump('D_LEGACY','error in \n' + js2JSON(E) + '\n');
577                 mw.sdump('D_LEGACY','restoring backup 2\n');
578                 restore_patron(PATRON);
579                 mw.handle_error(E);
580                 //mw.sdump('D_LEGACY','PATRON.au = ' + js2JSON(PATRON.au) + '\nPATRON.au.a.length = ' + PATRON.au.a.length + '\n');
581                 //PATRON.summary_refresh();
582                 return false;
583         }
584         PATRON.au = result[0];
585         if (! PATRON.au) { 
586                 mw.sdump('D_LEGACY','Restoring backup\n'); 
587                 restore_patron(PATRON);
588                 mw.handle_error('Save Failed'); 
589                 return; 
590         }
591         hash_aua = {};
592         response_list = [];
593         mw.sdump('D_LEGACY','after  PATRON.au = ' + js2JSON(PATRON.au) + '\n');
594         //PATRON.barcode = find_id_object_in_list(PATRON.au.cards(),PATRON.au.card()).barcode();
595         PATRON.summary_refresh();
596         patron_callback('save', { 'au' : PATRON.au });
597         var refresh_result = PATRON.related_refresh(PATRON.au.id());
598         alert('Patron successfully updated.');
599         return refresh_result;
600 }
601 PATRON.save = save_patron;
602
603 function check_for_new_addresses() {
604         mw.sdump('D_LEGACY',arg_dump(arguments));
605         for (var id in hash_aua) {
606                 if ( (id < 0) && ( hash_aua[id].ischanged() ) ) {
607                         mw.sdump('D_LEGACY','Pushing new address\n');
608                         if (!PATRON.au.addresses()) { PATRON.au.addresses( [] ); }
609                         PATRON.au.addresses().push( hash_aua[id] );
610                 }
611         }
612 }
613
614 function check_for_new_stat_cats() {
615         mw.sdump('D_LEGACY',arg_dump(arguments));
616         var entries = new Array();
617         var grid = document.getElementById('patron_edit_stat_cat_grid');
618         var nl = grid.getElementsByTagName('menulist');
619         for (var i = 0; i < nl.length; i++) {
620                 var menulist = nl[i];
621                 if (menulist.getAttribute('original') != menulist.value) {
622                         var n_actscecm = new actscecm();
623                         var id = menulist.getAttribute('entry_id')
624                         //alert('check_for_new_stat_cats: id = ' + id );
625                         if (id) {
626                                 n_actscecm.ischanged('1');
627                                 n_actscecm.id( id );
628                         } else {
629                                 n_actscecm.isnew('1');
630                                 n_actscecm.id( new_id-- );
631                         }
632                         n_actscecm.stat_cat( menulist.getAttribute('stat_cat_id') );
633                         n_actscecm.stat_cat_entry( menulist.value );
634                         n_actscecm.target_usr( PATRON.au.id() );
635                         entries.push( n_actscecm );
636                 }
637
638         }
639         //alert( 'entries = ' + js2JSON( entries ) );
640         PATRON.au.stat_cat_entries( entries );
641 }
642
643 function retrieve_patron_related_info(id) {
644         mw.sdump('D_LEGACY',arg_dump(arguments));
645         if (!id) { id = PATRON.au.id(); }
646         mw.sdump('D_LEGACY','Entering PATRON.related_refresh() with id: ' + id + '\n');
647         /*
648         var checkouts = [];
649         if (id > 0) {
650                 try {
651                         checkouts = mw.user_request(
652                                 'open-ils.circ',
653                                 'open-ils.circ.actor.user.checked_out',
654                                 [ mw.G.auth_ses[0], id ]
655                         )[0];
656                 } catch(E) {
657                         mw.handle_error(E);
658                 }
659         }
660         PATRON.checkouts = checkouts;
661         PATRON.nearest_due = '';
662         for (var i in checkouts) {
663                 var checkout = checkouts[i];
664                 mw.sdump('D_LEGACY','checkout = ' + js2JSON(checkout) + '\n');
665         }
666         PATRON.holds = [];
667         PATRON.bills = [];
668         PATRON.summary_refresh();
669         try {
670                 //circ_init_list();
671         } catch(E) {
672                 mw.sdump('D_LEGACY',js2JSON(E) + '\n');
673         }
674         patron_callback('related_refresh');
675         */
676         return true;
677 }
678 PATRON.related_refresh = retrieve_patron_related_info;
679
680 /* patron_scan_overlay functions */
681
682 function patron_callback(s,params) {
683         mw.sdump('D_LEGACY',arg_dump(arguments));
684         try {
685                 switch(s) {
686                         case 'scan_submit' : return patron_scan_submit_callback(params); break;
687                         case 'related_refresh' : return patron_related_refresh_callback(params); break;
688                         case 'retrieve_patron' : return patron_retrieve_patron_callback(params); break;
689                         case 'save' : return patron_save_callback(params); break;
690                         default : return patron_default_callback(s,params); break;
691                 }
692         } catch(E) {
693                 /* assume no callback defined */
694                 return true;
695         }
696 }
697
698 function patron_advanced_button(ev) {
699         mw.sdump('D_LEGACY',arg_dump(arguments));
700         var deck = document.getElementById('patron_scan_deck');
701         if (deck) { deck.setAttribute('selectedIndex','2'); }
702         focus_widget( 'patron_search_family_name_textbox' );
703         deck = document.getElementById('circ_deck_deck');
704         if (deck) { deck.setAttribute('selectedIndex','6'); }
705         //PATRON.search = {};
706 }
707 PATRON.advanced_search = patron_advanced_button;
708
709 function patron_scan_submit(ev) {
710         mw.sdump('D_LEGACY',arg_dump(arguments));
711         mw.sdump('D_LEGACY','Entering PATRON.scan_submit() with target: ' + ev.target + '\n');
712         try {
713                 var rc = PATRON.retrieve_patron( document.getElementById('patron_scan_textbox').value );
714                 if (rc) {
715                         /* the PATRON object should already be updated.  Switch deck if there is one */
716                         var deck = document.getElementById('patron_scan_deck');
717                         if (deck) { deck.setAttribute('selectedIndex','1'); }
718                         patron_callback('scan_submit');
719                         /* enable the scan item widgets if there are some */
720                         enable_widgets(
721                                 'circ_checkout_scan_search_button',
722                                 'circ_checkout_scan_textbox',
723                                 'circ_checkout_scan_submit_button'
724                         );
725                         focus_widget( 'circ_checkout_scan_textbox' );
726                 } else {
727                         throw('retrieve_patron return code == false');
728                 }
729         } catch(E) {
730                 mw.sdump('D_LEGACY','Could not retrieve patron.  Invalid barcode?\n' + js2JSON(E) + '\n');
731                 alert('Could not retrieve patron.  Invalid barcode?\n' + js2JSON(E) + '\n');
732         }
733 }
734 PATRON.scan_submit = patron_scan_submit;
735
736 function patron_scan_search(ev) {
737         mw.sdump('D_LEGACY',arg_dump(arguments));
738         mw.sdump('D_LEGACY','Entering PATRON.scan_search() with target: ' + ev.target + '\n');
739         //mw.sdump('D_LEGACY','PATRON.search = ' + pretty_print(js2JSON(PATRON.search)) + '\n');
740         try {
741                 var result = mw.user_request(
742                         'open-ils.actor',
743                         'open-ils.actor.patron.search.advanced',
744                         [ mw.G.auth_ses[0], PATRON.search ]
745                 )[0];
746                 mw.sdump('D_LEGACY','result = ' + js2JSON(result) + '\n');
747                 PATRON['search_results'] = result;
748                 build_patron_search_result_deck();
749         } catch(E) {
750                 mw.handle_error(E);
751         }
752 }
753 PATRON.scan_search = patron_scan_search;
754
755 /* patron_summary_overlay functions */
756
757 function make_barcode_handler(card_id,user_id) {
758         mw.sdump('D_LEGACY',arg_dump(arguments));
759         return function (ev) {
760                 magic_field_edit(ev,'ac',card_id,'barcode');
761                 PATRON.au.ischanged('1');
762                 magic_field_edit(ev,'au',user_id,'usrname');
763                 var usr_e = document.getElementById('patron_edit_system_usrname_textbox');
764                 if (usr_e) {
765                         usr_e.value = ev.target.value;
766                 }
767                 /*ev.target.removeEventListener(
768                         "change",
769                         this,
770                         false
771                 );*/
772         }
773 }
774
775 function patron_summary_refresh(ev) {
776         mw.sdump('D_LEGACY',arg_dump(arguments));
777         //alert( mw.arg_dump(arguments) );
778         // This function needs to be broken up.. it sets the patron edit section as well
779         if (!PATRON.au) { return; }
780         mw.sdump('D_LEGACY','Entering PATRON.summary_refresh()\n');
781         hash_au[PATRON.au.id()] = PATRON.au;
782         //PATRON.barcode = find_id_object_in_list(PATRON.au.cards(),PATRON.au.card()).barcode();
783         /* just redraw the display with the PATRON object as is */
784         var barcode_e = document.getElementById('patron_edit_system_barcode_textbox');
785         var newcard_e = document.getElementById('patron_new_card_button');
786         if (barcode_e) {
787                 //var barcode_v = find_id_object_in_list(PATRON.au.cards(),PATRON.au.card());
788                 var barcode_v = PATRON.au.card();
789                 if (barcode_v) {
790                         barcode_e.value = barcode_v.barcode();
791                         if (barcode_e.value != 'REQUIRED') {
792                                 barcode_e.disabled = true;
793                                 newcard_e.hidden = false;       
794                                 barcode_e.addEventListener(
795                                         'change',
796                                         function (ev) {
797                                                 PATRON.au.card().barcode( ev.target.value );
798                                         },
799                                         false
800                                 );
801                         } else {
802                                 barcode_e.addEventListener(
803                                         "change",
804                                         make_barcode_handler(barcode_v.id(),PATRON.au.id()),
805                                         false
806                                 );
807                         }
808                 }
809         }
810         var usrname_e = document.getElementById('patron_edit_system_usrname_textbox');
811         if (usrname_e) {
812                 if (PATRON.au.usrname()) usrname_e.value = PATRON.au.usrname();
813                 usrname_e.setAttribute("onchange",
814                         "magic_field_edit(event,'au'," + PATRON.au.id() + ",'usrname');");
815         }
816         var passwd_e = document.getElementById('patron_edit_system_new_passwd_textbox');
817         if (passwd_e) {
818                 passwd_e.setAttribute("onchange",
819                         "magic_field_edit(event,'au'," + PATRON.au.id() + ",'passwd');");
820         }
821         var name_e = document.getElementById('patron_status_caption');
822         if (name_e) {
823                 var name = '';
824                 if (PATRON.au.prefix()) { name += PATRON.au.prefix() + ' '; }
825                 if (PATRON.au.family_name()) { name += PATRON.au.family_name() + ', '; }
826                 if (PATRON.au.first_given_name()) { name += PATRON.au.first_given_name() + ' '; }
827                 if (PATRON.au.second_given_name()) { name += PATRON.au.second_given_name() + ' '; }
828                 if (PATRON.au.suffix()) { name += PATRON.au.suffix(); }
829
830                 name_e.setAttribute('label',name);
831         }
832         name_e = document.getElementById('patron_edit_system_family_name_textbox');
833         if (name_e) {
834                 if (PATRON.au.family_name()) { name_e.value = PATRON.au.family_name(); }
835                 name_e.setAttribute("onchange","magic_field_edit(event,'au'," + PATRON.au.id() + ",'family_name');");
836         }
837         name_e = document.getElementById('patron_edit_system_first_given_name_textbox');
838         if (name_e) {
839                 if (PATRON.au.first_given_name()) { name_e.value = PATRON.au.first_given_name(); }
840                 name_e.setAttribute("onchange","magic_field_edit(event,'au'," + PATRON.au.id() + ",'first_given_name');");
841         }
842         name_e = document.getElementById('patron_edit_system_second_given_name_textbox');
843         if (name_e) {
844                 if (PATRON.au.second_given_name()) { name_e.value = PATRON.au.second_given_name(); }
845                 name_e.setAttribute("onchange","magic_field_edit(event,'au'," + PATRON.au.id() + ",'second_given_name');");
846         }
847         name_e = document.getElementById('patron_edit_system_prefix_menulist');
848         if (name_e) {
849                 if (PATRON.au.prefix()) { name_e.value = PATRON.au.prefix(); }
850                 name_e.setAttribute("oncommand","magic_field_edit(event,'au'," + PATRON.au.id() + ",'prefix');");
851         }
852         name_e = document.getElementById('patron_edit_system_suffix_menulist');
853         if (name_e) {
854                 if (PATRON.au.suffix()) { name_e.value = PATRON.au.suffix(); }
855                 name_e.setAttribute("oncommand","magic_field_edit(event,'au'," + PATRON.au.id() + ",'suffix');");
856         }
857         var disable_e = document.getElementById('patron_edit_system_disable_checkbox');
858         if (disable_e) {
859                 disable_e.checked = (PATRON.au.active() != 1);
860                 disable_e.setAttribute("oncommand","try{PATRON.au.ischanged('1'); if (PATRON.au.active() == 1) {PATRON.au.active('0');} else {PATRON.au.active('1');}}catch(E){alert(E);}");
861         }
862         var profile_e = document.getElementById('patron_status_data_profile');
863         if (profile_e) {
864                 //profile_e.setAttribute('value',find_id_object_in_list(mw.G.ap_list, PATRON.au.profile() ).name() );
865                 profile_e.setAttribute('value',mw.G.ap_hash[ PATRON.au.profile() ].name() );
866         }
867         profile_e = document.getElementById('patron_edit_system_profile_menulist');
868         if (profile_e) {
869                 //hash_ap[PATRON.au.profile().id()] = PATRON.au.profile();
870                 profile_e.setAttribute("oncommand","magic_field_edit(event,'au'," + PATRON.au.id() + ",'profile');");
871                 set_patron_edit_profile_menu();
872         }
873         var email_e = document.getElementById('patron_edit_contact_email_textbox');
874         if (email_e) {
875                 if (PATRON.au.email()) email_e.value = PATRON.au.email();
876                 email_e.setAttribute("onchange","magic_field_edit(event,'au'," + PATRON.au.id() + ",'email');");
877         }
878         var dayphone_e = document.getElementById('patron_contact_dayphone_data');
879         if (dayphone_e) {
880                 if (PATRON.au.day_phone()) dayphone_e.setAttribute('value', PATRON.au.day_phone() );
881         }
882         dayphone_e = document.getElementById('patron_edit_contact_dayphone_textbox');
883         if (dayphone_e) {
884                 if (PATRON.au.day_phone()) dayphone_e.value = PATRON.au.day_phone();
885                 dayphone_e.setAttribute("onchange","magic_field_edit(event,'au'," + PATRON.au.id() + ",'day_phone');");
886         }
887         var eveningphone_e = document.getElementById('patron_contact_eveningphone_data');
888         if (eveningphone_e) {
889                 if (PATRON.au.evening_phone()) eveningphone_e.setAttribute('value', PATRON.au.evening_phone() );
890         }
891         eveningphone_e = document.getElementById('patron_edit_contact_eveningphone_textbox');
892         if (eveningphone_e) {
893                 if (PATRON.au.evening_phone()) eveningphone_e.value = PATRON.au.evening_phone();
894                 eveningphone_e.setAttribute("onchange","magic_field_edit(event,'au'," + PATRON.au.id() + ",'evening_phone');");
895         }
896         var otherphone_e = document.getElementById('patron_contact_otherphone_data');
897         if (otherphone_e) {
898                 if (PATRON.au.other_phone()) otherphone_e.setAttribute('value', PATRON.au.other_phone() );
899         }
900         otherphone_e = document.getElementById('patron_edit_contact_otherphone_textbox');
901         if (otherphone_e) {
902                 if (PATRON.au.other_phone()) otherphone_e.value = PATRON.au.other_phone();
903                 otherphone_e.setAttribute("onchange","magic_field_edit(event,'au'," + PATRON.au.id() + ",'other_phone');");
904         }
905         var standing_e = document.getElementById('patron_status_data_standing');
906         if (standing_e) {
907                 /*var standing = find_id_object_in_list( 
908                                 mw.G.cst_list,
909                                 PATRON.au.standing() 
910                         );*/
911                 var standing = mw.G.cst_hash[ PATRON.au.standing() ];
912                 mw.sdump('D_LEGACY','standing = ' + js2JSON(standing) + '\n');
913                 standing_e.setAttribute( 'value', standing.value() );
914                 if (standing.value() == 'Good') {
915                         addCSSClass(standing_e,'good');
916                 } else {
917                         removeCSSClass(standing_e,'good');      
918                 }
919         }
920         var claims_returned_e = document.getElementById('patron_status_data_claims_returned');
921         if (claims_returned_e) {
922                 claims_returned_e.setAttribute('value',PATRON.au.claims_returned_count());
923         }
924         var credit_e = document.getElementById('patron_status_data_credit');
925         if (credit_e) {
926                 credit_e.setAttribute('value',PATRON.au.credit_forward_balance());
927         }
928         var homelib_e = document.getElementById('patron_status_data_homelib');
929         if (homelib_e) {
930                 homelib_e.setAttribute('value', 
931                         find_ou(
932                                 mw.G.org_tree,
933                                 PATRON.au.home_ou()
934                         ).name()
935                 );
936         }
937         homelib_e = document.getElementById('patron_edit_system_library_menulist');
938         if (homelib_e) {
939                 homelib_e.setAttribute("oncommand","magic_field_edit(event,'au'," + PATRON.au.id() + ",'home_ou',false);");
940                 set_patron_edit_library_menu();
941         }
942         var fees_e = document.getElementById('patron_status_data_fees');
943         if (fees_e) {
944                 fees_e.setAttribute('value',PATRON.bills.length);
945         }
946         var checkouts_e = document.getElementById('patron_status_data_checkouts');
947         if (checkouts_e) {
948                 checkouts_e.setAttribute('value',PATRON.checkouts.length);
949         }
950         var holds_e = document.getElementById('patron_status_data_holds');
951         if (holds_e) {
952                 holds_e.setAttribute('value',PATRON.holds.length);
953         }
954         var nearest_due_e = document.getElementById('patron_status_data_nearest_due');
955         if (nearest_due_e) {
956                 nearest_due_e.setAttribute('value',PATRON.nearest_due);
957         }
958         var id1value_e = document.getElementById('patron_edit_system_id1value_textbox');
959         if (id1value_e) {
960                 if (PATRON.au.ident_value()) id1value_e.setAttribute('value',PATRON.au.ident_value());
961                 id1value_e.setAttribute("onchange","magic_field_edit(event,'au'," + PATRON.au.id() + ",'ident_value');");
962         }
963         var id2value_e = document.getElementById('patron_edit_system_id2value_textbox');
964         if (id2value_e) {
965                 if (PATRON.au.ident_value2()) id2value_e.setAttribute('value',PATRON.au.ident_value2());
966                 id2value_e.setAttribute("onchange","magic_field_edit(event,'au'," + PATRON.au.id() + ",'ident_value2');");
967         }
968         var id1type_e = document.getElementById('patron_edit_system_id1type_menulist');
969         if (id1type_e) {
970                 id1type_e.setAttribute("oncommand","magic_field_edit(event,'au'," + PATRON.au.id() + ",'ident_type');");
971                 set_patron_edit_ident_type_menu(); 
972         }
973         var id2type_e = document.getElementById('patron_edit_system_id2type_menulist');
974         if (id2type_e) {
975                 id2type_e.setAttribute("oncommand","magic_field_edit(event,'au'," + PATRON.au.id() + ",'ident_type2');");
976                 set_patron_edit_ident_type_menu2();
977         }
978         var dob_e = document.getElementById('patron_edit_system_dob_textbox');
979         if (dob_e) {
980                 if (PATRON.au.dob()) dob_e.setAttribute('value',PATRON.au.dob());
981                 dob_e.setAttribute("onchange","magic_field_edit(event,'au'," + PATRON.au.id() + ",'dob',false);");
982         }
983         //populate_patron_survey_grid('patron_survey_grid');
984         populate_patron_survey_grid('patron_edit_survey_grid');
985         populate_patron_edit_surveys();
986
987         var address_rows = document.getElementById('patron_contact_address_rows');
988         if (address_rows) {
989                 empty_widget(address_rows);
990                 var addresses = PATRON.au.addresses();
991                 for (var i in addresses) { 
992                         if (typeof(addresses[i])=='object') {
993                                 var address = addresses[i];
994                                 if (
995                                         (address.id() == PATRON.au.mailing_address()) ||
996                                         (address.id() == PATRON.au.billing_address())
997                                 ) {
998                                         //mw.sdump('D_LEGACY','address dump: ' + i + ' : ' + addresses[i] + '\n');
999                                         rows_append_address( address_rows, address, false );
1000                                 }
1001                         }
1002                 }
1003         }
1004         address_rows = document.getElementById('patron_edit_address_rows');
1005         if (address_rows) {
1006                 empty_widget(address_rows);
1007                 var addresses = PATRON.au.addresses();
1008                 for (var i in addresses) { 
1009                         if (typeof(addresses[i])=='object') {
1010                                 //mw.sdump('D_LEGACY','address dump: ' + i + ' : ' + js2JSON(addresses[i]) + '\n');
1011                                 rows_append_address( address_rows, addresses[i], true );
1012                         }
1013                 }
1014                 var blank = new aua();
1015                 blank.id( new_id-- );
1016                 blank.isnew("1");
1017                 blank.usr( PATRON.au.id() );
1018                 /*if ( addresses.length == 0 ) {*/
1019                 if (hash_aua.length == 0) {
1020                         blank.address_type( 'REQUIRED' );
1021                         blank.ischanged( "1" );
1022                         PATRON.au.mailing_address( blank.id() );
1023                         PATRON.au.billing_address( blank.id() );
1024                 } else {
1025                         blank.address_type( 'NEW' );
1026                 }
1027                 blank.city( 'CITY' );
1028                 blank.country( 'USA' );
1029                 blank.county( 'COUNTY' );
1030                 blank.post_code( 'ZIP' );
1031                 blank.state( 'GA' );
1032                 blank.street1( 'STREET1' );
1033                 blank.street2( 'STREET2' );
1034                 rows_append_address( address_rows, blank , true );
1035         }
1036
1037         var alert_message_e = document.getElementById('patron_edit_system_alert_message_textbox');
1038         if (alert_message_e) {
1039                 if (PATRON.au.alert_message()) alert_message_e.setAttribute('value',PATRON.au.alert_message());
1040                 alert_message_e.setAttribute("onchange","magic_field_edit(event,'au'," + PATRON.au.id() + ",'alert_message',false);");
1041         }
1042         var stat_cats_e = document.getElementById('patron_statcat_rows');
1043         if (stat_cats_e) {
1044                 try {
1045                         empty_widget( stat_cats_e );
1046                         for (var i = 0; i < PATRON.au.stat_cat_entries().length; i++) {
1047
1048                                 // fieldmapper
1049                                 var entry = PATRON.au.stat_cat_entries()[i];
1050                                 var stat_cat = mw.G.actsc_hash[ entry.stat_cat() ];
1051
1052                                 // build XUL
1053                                 var row = document.createElement('row');
1054                                 stat_cats_e.appendChild(row);
1055                                 var sc_label = document.createElement('label');
1056                                 row.appendChild(sc_label);
1057                                 var sce_label = document.createElement('label');
1058                                 row.appendChild(sce_label);
1059
1060                                 // set values
1061                                 sc_label.setAttribute('value', stat_cat.name() );
1062                                 sce_label.setAttribute('value', entry.stat_cat_entry() );
1063                         }
1064                 } catch(E) {
1065                         mw.handle_error(E);
1066                 }
1067         }
1068         stat_cats_e = document.getElementById('patron_edit_stat_cat_grid');
1069         if (stat_cats_e) {
1070                 //alert('stat_cats_e');
1071                 try {
1072                         //alert('in try');
1073                         for (var i = 0; i < PATRON.au.stat_cat_entries().length; i++) {
1074                                 var entry = PATRON.au.stat_cat_entries()[i];
1075                                 //alert('entry = ' + js2JSON(entry) );
1076                                 var stat_cat = entry.stat_cat();
1077                                 if (typeof stat_cat == 'object') stat_cat = stat_cat.id();
1078                                 var menulist = document.getElementById('menulist_stat_cat_' + stat_cat);
1079                                 if (menulist) {
1080
1081                                         //alert('menulist');
1082
1083                                         menulist.value = entry.stat_cat_entry();
1084                                         menulist.setAttribute( 'original', menulist.value );
1085                                         menulist.setAttribute( 'entry_id', entry.id() );
1086                                         //alert('summary_refresh: entry.id() = ' + entry.id() );
1087
1088                                 }
1089                         }
1090                 } catch(E) {
1091                         mw.handle_error(E);
1092                 }
1093         }
1094         /*
1095         if (PATRON.au.alert_message()) {
1096                 snd_bad(); snd_bad();
1097                 s_alert(
1098                         'PATRON ALERT MESSAGE\n\n\n\n' +
1099                         PATRON.au.alert_message() +
1100                         '\n\n\n\nTo remove this alert permanently, Edit the patron and erase the message in "Alert Message".\n\n'
1101                 );
1102         }
1103         */
1104 }
1105 PATRON.summary_refresh = patron_summary_refresh;
1106
1107 function rows_append_address( rows, address, edit ) {
1108         mw.sdump('D_LEGACY',arg_dump(arguments));
1109
1110
1111         // patron_summary
1112
1113         //mw.sdump('D_LEGACY','Entering rows_append_address()\n');
1114         if (typeof(rows) != 'object') {
1115                 rows = document.getElementById(box);
1116         }
1117         if (typeof(rows) != 'object') {
1118                 mw.sdump('D_LEGACY','rows_append_address: could not find ' + rows + '\n');
1119                 alert('rows_append_address: could not find ' + rows + '\n');
1120                 return false;
1121         }
1122         var row = document.createElement('row');
1123                 //row.setAttribute('id','row_address_'+yesno(edit)+address.id());
1124                 hash_aua[ address.id() ] = address;
1125         rows.appendChild(row);
1126         var groupbox = document.createElement('groupbox');
1127                 if (edit) {
1128                         groupbox.setAttribute('id','groupbox_address_'+address.id());
1129                 } else {
1130                         groupbox.setAttribute('id','groupbox_summary_address_'+address.id());
1131                 }
1132                 groupbox.setAttribute('flex','1');
1133         row.appendChild(groupbox);
1134         var caption = document.createElement('caption');
1135                 var caption_string = address.address_type();
1136                 if ( PATRON.au.mailing_address() == address.id() ) {
1137                         caption_string = '(MAILING) ' + caption_string;
1138                 }
1139                 if ( PATRON.au.billing_address() == address.id() ) {
1140                         caption_string = '(PHYSICAL) ' + caption_string;
1141                 }
1142                 caption.setAttribute( 'label',caption_string );
1143         groupbox.appendChild(caption);
1144
1145         // patron_edit
1146
1147         if (edit) {
1148                 var hbox = document.createElement('hbox');
1149                 groupbox.appendChild(hbox);
1150                         var label = document.createElement('label');
1151                                 label.setAttribute('value','Type:');
1152                         hbox.appendChild(label);
1153                         var textbox = document.createElement('textbox');
1154                         textbox.setAttribute('size','20');
1155                         textbox.setAttribute("onchange","magic_field_edit(event,'aua'," + address.id() + ",'address_type');");
1156                         hbox.appendChild(textbox);
1157                                 textbox.value = address.address_type();
1158                         var label2 = document.createElement('label');
1159                                 label2.setAttribute('value','Physical:');
1160                         hbox.appendChild(label2);
1161                         var checkbox_billing = document.createElement('checkbox');
1162                                 checkbox_billing.setAttribute('group','billing');
1163                                 checkbox_billing.setAttribute('oncommand','radio_checkbox(event); PATRON.au.billing_address(' + address.id() + '); PATRON.au.ischanged("1");');
1164                         hbox.appendChild(checkbox_billing);
1165                         if ( PATRON.au.billing_address() == address.id() ) {
1166                                 checkbox_billing.checked = true;
1167                         }
1168                         var label3 = document.createElement('label');
1169                                 label3.setAttribute('value','Mailing:');
1170                         hbox.appendChild(label3);
1171                         var checkbox_mailing = document.createElement('checkbox');
1172                                 checkbox_mailing.setAttribute('group','mailing');
1173                                 checkbox_mailing.setAttribute('oncommand','radio_checkbox(event); PATRON.au.mailing_address(' + address.id() + '); PATRON.au.ischanged("1");');
1174                         hbox.appendChild(checkbox_mailing);
1175                         if ( PATRON.au.mailing_address() == address.id() ) {
1176                                 checkbox_mailing.checked = true;
1177                         }
1178                         var label4 = document.createElement('label');
1179                                 label4.setAttribute('value','Invalid:');
1180                         hbox.appendChild(label4);
1181                         var checkbox_invalid = document.createElement('checkbox');
1182                                 checkbox_invalid.setAttribute('oncommand','invalid_checkbox(event,' + address.id() + ');');
1183                         hbox.appendChild(checkbox_invalid);
1184                         mw.sdump('D_LEGACY','address ' + address.id() + ' valid = ' + address.valid() + '\n');
1185                         if ( address.valid() == '1' || address.valid() == 't' ) {
1186                                 checkbox_invalid.checked = false;
1187                         } else {
1188                                 checkbox_invalid.checked = true;
1189                         }
1190                         var label5 = document.createElement('spacer');
1191                                 label5.setAttribute('flex','1');
1192                         hbox.appendChild(label5);
1193                         if (address.id()>-1) {
1194                                 var label6 = document.createElement('button');
1195                                         label6.setAttribute('label','Delete');
1196                                         label6.setAttribute('alt_label','Un-Delete');
1197                                         label6.setAttribute('toggle','0');
1198                                         label6.setAttribute('oncommand','toggle_address(event,' + address.id() + ');');
1199                                 hbox.appendChild(label6);
1200                         }
1201         }
1202         var street1;
1203                 if (edit) {
1204                         street1 = document.createElement('textbox');
1205                         street1.setAttribute('size','40');
1206                         street1.setAttribute("onchange","magic_field_edit(event,'aua'," + address.id() + ",'street1');");
1207                 } else {
1208                         street1 = document.createElement('label');
1209                 }
1210                 street1.setAttribute( 'value',address.street1() );
1211         groupbox.appendChild(street1);
1212         var street2;
1213                 if (edit) {
1214                         street2 = document.createElement('textbox');
1215                         street2.setAttribute('size','40');
1216                         street2.setAttribute("onchange","magic_field_edit(event,'aua'," + address.id() + ",'street2');");
1217                 } else {
1218                         street2 = document.createElement('label');
1219                 }
1220                 street2.setAttribute( 'value',address.street2() );
1221         groupbox.appendChild(street2);
1222         var hbox = document.createElement('hbox');
1223         groupbox.appendChild(hbox);
1224                 var city;
1225                         if (edit) {
1226                                 city = document.createElement('textbox');
1227                                 city.setAttribute('size','20');
1228                                 city.setAttribute("onchange","magic_field_edit(event,'aua'," + address.id() + ",'city');");
1229                         } else {
1230                                 city = document.createElement('label');
1231                         }
1232                         city.setAttribute( 'value',address.city() );
1233                 hbox.appendChild(city);
1234                 var county;
1235                         if (edit) {
1236                                 county = document.createElement('textbox');
1237                                 county.setAttribute('size','20');
1238                                 county.setAttribute("onchange","magic_field_edit(event,'aua'," + address.id() + ",'county');");
1239                                 county.setAttribute( 'value',address.county() );
1240                         } else {
1241                                 county = document.createElement('label');
1242                                 county.setAttribute( 'value','(' + address.county() + '),');
1243                         }
1244                 hbox.appendChild(county);
1245                 if (!address.county()) {
1246                         if (!edit) {
1247                                 county.setAttribute( 'display', 'none' );
1248                                 city.setAttribute( 'value',address.city() + ',' );
1249                         }
1250                 }
1251                 var state;
1252                         if (edit) {
1253                                 state = document.createElement('textbox');
1254                                 state.setAttribute('size','2');
1255                                 state.setAttribute("onchange","magic_field_edit(event,'aua'," + address.id() + ",'state');");
1256                         } else {
1257                                 state = document.createElement('label');
1258                         }
1259                         state.setAttribute( 'value',address.state() );
1260                 hbox.appendChild(state);
1261                 var country;
1262                         if (edit) {
1263                                 country = document.createElement('textbox');
1264                                 country.setAttribute('size','3');
1265                                 country.setAttribute("onchange","magic_field_edit(event,'aua'," + address.id() + ",'country');");
1266                                 country.setAttribute( 'value',address.country() );
1267                         } else {
1268                                 country = document.createElement('label');
1269                                 country.setAttribute( 'value','(' + address.country() + '),');
1270                         }
1271                 hbox.appendChild(country);
1272                 var zip;
1273                         if (edit) {
1274                                 zip = document.createElement('textbox');
1275                                 zip.setAttribute('size','10');
1276                                 zip.setAttribute("onchange","magic_field_edit(event,'aua'," + address.id() + ",'post_code');");
1277                         } else {
1278                                 zip = document.createElement('label');
1279                         }
1280                         zip.setAttribute( 'value',address.post_code() );
1281                 hbox.appendChild(zip);
1282 }
1283
1284 function invalid_checkbox(e,id) {
1285         mw.sdump('D_LEGACY',arg_dump(arguments));
1286         if (e.target.checked) {
1287                 mw.sdump('D_LEGACY','Marking address ' + id + ' invalid\n');
1288                 mw.sdump('D_LEGACY','\tbefore address: ' + js2JSON(hash_aua[id]) + '\n');
1289                 //hash_aua[id].valid( '0' );
1290                 hash_aua[id].valid( 'f' );
1291                 hash_aua[id].ischanged( '1' );
1292                 mw.sdump('D_LEGACY','\tafter  address: ' + js2JSON(hash_aua[id]) + '\n');
1293         } else {
1294                 mw.sdump('D_LEGACY','Marking address ' + id + ' valid\n');
1295                 mw.sdump('D_LEGACY','\tbefore address: ' + js2JSON(hash_aua[id]) + '\n');
1296                 //hash_aua[id].valid( '1' );
1297                 hash_aua[id].valid( 't' );
1298                 hash_aua[id].ischanged( '1' );
1299                 mw.sdump('D_LEGACY','\tafter  address: ' + js2JSON(hash_aua[id]) + '\n');
1300         }
1301 }
1302
1303 function toggle_address(e,id) {
1304         mw.sdump('D_LEGACY',arg_dump(arguments));
1305         var groupbox = document.getElementById('groupbox_address_' + id);
1306         //var address = find_id_object_in_list( hash_aua, id );
1307         var address = hash_aua[id];
1308         var button = e.target;
1309         var label = button.getAttribute('label');
1310         var alt_label = button.getAttribute('alt_label');
1311         button.setAttribute('label',alt_label);
1312         button.setAttribute('alt_label',label);
1313         var toggle = button.getAttribute('toggle');
1314         if (toggle == '0') {
1315                 button.setAttribute('toggle','1');
1316                 mw.sdump('D_LEGACY','original node = ' + js2JSON(address) + '\n');
1317                 addCSSClass(groupbox,'deleted_address');
1318                 address.isdeleted('1');
1319                 mw.sdump('D_LEGACY','updated  node = ' + js2JSON(address) + '\n');
1320                 mw.sdump('D_LEGACY','PATRON.au.mailing_address() = ' + PATRON.au.mailing_address() + ' address.id() = ' + address.id() + '\n');
1321                 if (PATRON.au.mailing_address() == address.id() ) {
1322                         find_available_address_for('mailing_address');
1323                 }
1324                 mw.sdump('D_LEGACY','PATRON.au.billing_address() = ' + PATRON.au.billing_address() + ' address.id() = ' + address.id() + '\n');
1325                 if (PATRON.au.billing_address() == address.id() ) {
1326                         find_available_address_for('billing_address');
1327                 }
1328         } else {
1329                 button.setAttribute('toggle','0');
1330                 mw.sdump('D_LEGACY','original node = ' + js2JSON(address) + '\n');
1331                 removeCSSClass(groupbox,'deleted_address');
1332                 address.isdeleted('0');
1333                 mw.sdump('D_LEGACY','updated  node = ' + js2JSON(address) + '\n');
1334         }
1335         var nl = groupbox.getElementsByTagName('textbox');
1336                 for (var i in nl) {
1337                         if (typeof(nl[i])=='object') {
1338                                 var t = nl[i];
1339                                 t.disabled = ! t.disabled;
1340                         }
1341                 }
1342 }
1343
1344 function find_available_address_for(which) {
1345         mw.sdump('D_LEGACY',arg_dump(arguments));
1346         mw.sdump('D_LEGACY','entering find_avialable_address_for(' + which + ')\n');
1347         var addresses = PATRON.au.addresses();
1348         mw.sdump('D_LEGACY','considering existing addresses...\n');
1349         for (var i in addresses) {
1350                 var address = addresses[i];
1351                 mw.sdump('D_LEGACY','i = ' + i + ' addresses[i] = ' + js2JSON(address) + '\n');
1352                 if ( address.isdeleted() == '1') { continue; }
1353                 if ( (address.address_type() == 'NEW') && (address.id() < 0) ) {
1354                         address.address_type('REQUIRED');
1355                         address.ischanged( '1' );
1356                 }
1357                 mw.sdump('D_LEGACY','PATRON.au before = ' + js2JSON(PATRON.au) + '\n');
1358                 var command = 'PATRON.au.' + which + "( '" + address.id() + "' );";
1359                 mw.sdump('D_LEGACY', command + '\n' );
1360                 eval( command );
1361                 mw.sdump('D_LEGACY','PATRON.au after  = ' + js2JSON(PATRON.au) + '\n');
1362                 return true;
1363         }
1364         mw.sdump('D_LEGACY','considering old and new addresses...\n');
1365         for (var i in hash_aua) {
1366                 var address = hash_aua[i];
1367                 mw.sdump('D_LEGACY','i = ' + i + ' addresses[i] = ' + js2JSON(address) + '\n');
1368                 if ( address.isdeleted() == '1') { continue; }
1369                 if ( (address.address_type() == 'NEW') && (address.id() < 0) ) {
1370                         address.address_type('REQUIRED');
1371                         address.ischanged( '1' );
1372                 }
1373                 mw.sdump('D_LEGACY','PATRON.au before = ' + js2JSON(PATRON.au) + '\n');
1374                 var command = 'PATRON.au.' + which + "( '" + address.id() + "' );";
1375                 mw.sdump('D_LEGACY', command + '\n' );
1376                 eval( command );
1377                 mw.sdump('D_LEGACY','PATRON.au after  = ' + js2JSON(PATRON.au) + '\n');
1378                 return true;
1379         }
1380 }
1381
1382 function survey_test(ev,survey_id) {
1383         mw.sdump('D_LEGACY',arg_dump(arguments));
1384
1385         document.getElementById('circ_deck_deck').setAttribute('selectedIndex','5');
1386         var vbox = document.getElementById('patron_survey_vbox');
1387         empty_widget( vbox ); 
1388         survey_render_with_results(
1389                 vbox,
1390                 survey_id,
1391                 function (survey) {
1392                         return PATRON.refresh();
1393                 }
1394         );
1395 }
1396
1397 function populate_patron_edit_surveys() {
1398         mw.sdump('D_LEGACY',arg_dump(arguments));
1399         var vbox = document.getElementById('patron_edit_survey_vbox');
1400         if (!vbox) return;
1401
1402         mw.sdump('D_LEGACY','populate_patron_edit_surveys()\n');
1403
1404         empty_widget( vbox ); 
1405
1406         var surveys = [];
1407         try {
1408                 surveys = mw.user_request(
1409                         'open-ils.circ',
1410                         'open-ils.circ.survey.retrieve.required',
1411                         [ mw.G.auth_ses[0] ]
1412                 )[0];
1413         } catch(E) {
1414                 mw.handle_error(E);
1415         }
1416
1417         for (var i = 0; i < surveys.length; i++) {
1418                 var survey = surveys[i];
1419                 survey_render(
1420                         vbox,
1421                         survey.id(),
1422                         populate_patron_edit_surveys_build_callback( survey ),
1423                         null
1424                 );
1425         }
1426 }
1427
1428 function populate_patron_edit_surveys_build_callback( survey ) {
1429         mw.sdump('D_LEGACY',arg_dump(arguments));
1430         return function (responses) {
1431                 for (var i in responses) {
1432                         response_list.push( responses[i] );
1433                 }
1434                 var nframe = document.getElementById('patron_survey_frame_'+survey.id());
1435                 nframe.hidden = true;
1436                 var complete = document.createElement('description');
1437                 complete.setAttribute('value',survey.name() + ' Complete');
1438                 nframe.parentNode.insertBefore( complete, nframe );
1439                 //nframe.contentWindow.document.body.innerHTML = '<h1>' + survey.name() + ' Complete</h1>';
1440                 return true;
1441         };
1442 }
1443
1444
1445 function survey_render(vbox,survey_id,commit_callback,submit_callback) {
1446         sdump('D_LEGACY',arg_dump(arguments));
1447         if (typeof(vbox) != 'object') { vbox = document.getElementById(vbox); }
1448         var frame = document.createElement('vbox');
1449         vbox.appendChild(frame);
1450         //frame.setAttribute('flex','1'); frame.setAttribute('src','data:text/html,<html><head><LINK href="http://spacely.georgialibraries.org/css/box.css" rel="stylesheet" type="text/css"><LINK href="http://spacely.georgialibraries.org/css/survey.css" rel="stylesheet" type="text/css"></head><body></body></html>');
1451         
1452         setTimeout(function(){
1453         /*var doc = frame.contentWindow.document;*/
1454         var doc = document;
1455         window.createAppElement = function (name) { return doc.createElementNS('http://www.w3.org/1999/xhtml','html:'+name); }
1456         window.createTextNode = function (value) { return doc.createTextNode(value); }
1457         Survey.retrieveById( 
1458                 mw.G.auth_ses[0] , 
1459                 survey_id,
1460                 function(sur) { 
1461                         sur.setUser( PATRON.au.id() ); 
1462                         if (submit_callback) sur.setSubmitCallback( submit_callback );
1463                         if (commit_callback) sur.commitCallback = commit_callback;
1464                         mw.sdump('D_LEGACY','survey id: ' + sur.survey.id() + '\n');
1465
1466                         var node = sur.getNode();
1467
1468                         try {
1469                                 dump( super_dump( node ) );
1470                         } catch(E) {
1471                                 dump( js2JSON(E) );
1472                         }
1473
1474                         frame.appendChild( node ); 
1475                         /*frame.setAttribute('style','height: ' + (30+doc.height) + 'px;');*/
1476                         frame.setAttribute('id','patron_survey_frame_' + sur.survey.id());
1477                 } 
1478         );
1479         },0);
1480 }
1481
1482 function survey_render_with_results(vbox,survey_id,callback) {
1483         sdump('D_LEGACY',arg_dump(arguments));
1484         if (typeof(vbox) != 'object') { vbox = document.getElementById(vbox); }
1485         var frame = document.createElement('vbox');
1486         vbox.appendChild(frame);
1487         //frame.setAttribute('id','patron_survey_frame'); 
1488         //frame.setAttribute('flex','1'); frame.setAttribute('src','data:text/html,<html><head><LINK href="http://spacely.georgialibraries.org/css/box.css" rel="stylesheet" type="text/css"><LINK href="http://spacely.georgialibraries.org/css/survey.css" rel="stylesheet" type="text/css"></head><body></body></html>');
1489         //var doc = frame.contentWindow.document;
1490         var doc = document;
1491         //HTMLdoc = doc;
1492         window.createAppElement = function (name) { return doc.createElementNS('http://www.w3.org/1999/xhtml','html:'+name); }
1493         window.createTextNode = function (value) { return doc.createTextNode(value); }
1494         Survey.retrieveById( 
1495                 mw.G.auth_ses[0] , 
1496                 survey_id,
1497                 function(sur) { 
1498                         sur.setUser( PATRON.au.id() ); 
1499                         sur.setSubmitCallback( callback );
1500                         mw.sdump('D_LEGACY','survey id: ' + sur.survey.id() + '\n');
1501                         frame.appendChild( sur.getNode() ); 
1502                         var span = doc.createElement('html:blockquote');
1503                         span.setAttribute('id','survey_response_' + sur.survey.id());
1504                         span.setAttribute('class','survey');
1505                         var warning = doc.createTextNode('Retrieving Responses...');
1506                         span.appendChild(warning);
1507                         frame.appendChild(span);
1508                         mw.user_async_request(
1509                                 'open-ils.circ',
1510                                 'open-ils.circ.survey.response.retrieve',
1511                                 [ mw.G.auth_ses[0], sur.survey.id(), PATRON.au.id() ],
1512                                 function (request) {
1513                                         result = request.getResultObject().reverse();
1514                                         span.removeChild( warning );
1515                                         if (result.length == 0) { return; }
1516                                         //span.appendChild( doc.createTextNode('Previous Responses:') );
1517                                         //span.appendChild( doc.createElement('html:br') );
1518                                         //span.setAttribute('style','border: black solid thin;');
1519                                         var num_of_q = sur.survey.questions().length;
1520                                         var current_q = 0;
1521                                         span.appendChild( doc.createTextNode(
1522                                                 'Previous Responses:'
1523                                         ) );
1524                                         span.appendChild( doc.createElement('html:br') );
1525                                         span.appendChild( doc.createElement('html:br') );
1526                                         var block;
1527                                         for (var i = 0; i < result.length; i++) {
1528                                                 if (++current_q > num_of_q) { current_q = 1; }
1529                                                 mw.sdump('D_LEGACY','current_q = ' + current_q + '  num_of_q = ' + num_of_q + '\n');
1530                                                 if (current_q == 1) {
1531                                                         block = doc.createElement('html:blockquote');
1532                                                         span.appendChild( doc.createTextNode(
1533                                                                 'Answer Date: ' + 
1534                                                                 result[i].answer_date() +
1535                                                                 ', Effective Date: ' + 
1536                                                                 result[i].effective_date()
1537                                                         ) );
1538                                                         span.appendChild( doc.createElement('html:br') );
1539                                                         span.appendChild(block);
1540                                                 }
1541                                                 block.appendChild(
1542                                                         doc.createTextNode(
1543                                                                 current_q + ') ' + 
1544                                                                 find_id_object_in_list(
1545                                                                         find_id_object_in_list(
1546                                                                                 sur.survey.questions(),
1547                                                                                 result[i].question()
1548                                                                         ).answers(),
1549                                                                         result[i].answer()
1550                                                                 ).answer() + ' '
1551                                                         )
1552                                                 );
1553                                         }
1554                                         span.appendChild( doc.createElement('html:br') );
1555                                         /*frame.setAttribute('style','height: ' + (30+doc.height) + 'px;');*/
1556                                 }
1557                         );
1558                 } 
1559         );
1560 }
1561
1562
1563 function handle_patron_search_textbox(ev,group) {
1564         mw.sdump('D_LEGACY',arg_dump(arguments));
1565         var id = ev.target.getAttribute('id');
1566         var field = id.split(/_/).slice(2,-1).join('_');
1567         mw.sdump('D_LEGACY','field = ' + field + ' value = ' + ev.target.value + '\n');
1568         PATRON.search[field] = { 'value' : ev.target.value, 'group' : group };
1569 }
1570
1571 function build_patron_search_result_deck() {
1572         mw.sdump('D_LEGACY',arg_dump(arguments));
1573         var label = document.getElementById('patron_search_results_label');
1574         if (label) {
1575                 var s = 'Found ' + PATRON.search_results.length + ' matches.  ';
1576                 if (PATRON.search_results.length > patron_hits_per_page) {
1577                         s += 'Displaying ' + patron_hits_per_page + ' per page:';
1578                 }
1579                 label.setAttribute('value',s);
1580         }
1581         var deck = document.getElementById('patron_search_results_deck');       
1582         if (!deck) return;
1583
1584         empty_widget(deck);
1585
1586         var patron_ids = PATRON.search_results.slice(0,patron_hits_per_page);
1587         PATRON.search_results = PATRON.search_results.slice(patron_hits_per_page);
1588         build_patron_search_result_page(deck,patron_ids,PATRON.search_results.length);
1589 }
1590
1591 function build_patron_search_result_page(deck,patron_ids,remaining) {
1592         mw.sdump('D_LEGACY',arg_dump(arguments));
1593         mw.sdump('D_LEGACY','build_patron_search_result_page()\n');
1594         if (typeof(deck)!='object') deck = document.getElementById(deck);
1595         if (!deck) return;
1596
1597         if (patron_ids.length == 0) return;
1598
1599         var vbox = document.createElement('vbox');
1600         deck.appendChild(vbox);
1601         vbox.setAttribute('flex','1');
1602
1603         var idx = deck.childNodes.length - 1;
1604         deck.selectedIndex = idx;
1605
1606         var button_box = document.createElement('hbox');
1607         vbox.appendChild(button_box);
1608
1609         var back_button = document.createElement('button');
1610         button_box.appendChild(back_button);
1611         back_button.setAttribute('label','Previous');
1612         back_button.disabled = true;
1613         if (idx > 0) {
1614                 back_button.disabled = false;
1615                 back_button.setAttribute('oncommand',"var deck = document.getElementById('patron_search_results_deck'); deck.selectedIndex = deck.selectedIndex -1;");
1616         }
1617         var forward_button = document.createElement('button');
1618         button_box.appendChild(forward_button);
1619         forward_button.setAttribute('label','Next');
1620         forward_button.disabled = true;
1621         if (remaining > 0) {
1622                 forward_button.disabled = false;
1623                 forward_button.addEventListener(
1624                         'command',
1625                         function (ev) {
1626                                 var fired = ev.target.getAttribute('fired');
1627
1628                                 if (fired) {
1629                                         deck.selectedIndex = idx + 1;
1630                                 } else {
1631                                         var next_ids = PATRON.search_results.slice(0,patron_hits_per_page);
1632                                         PATRON.search_results = PATRON.search_results.slice(patron_hits_per_page);
1633                                         build_patron_search_result_page(deck,next_ids,PATRON.search_results.length);
1634                                         ev.target.setAttribute('fired',true);
1635                                 }
1636                         },
1637                         false
1638                 );
1639         }
1640         var tree = document.createElement('tree');
1641         vbox.appendChild(tree);
1642         tree.setAttribute('flex','1');
1643         tree.setAttribute('enableColumnDrag','true');
1644         tree.addEventListener(
1645                 'select',
1646                 function (ev) {
1647                         var row = get_list_from_tree_selection(ev.target)[0];
1648                         if (row) {
1649                                 var patron_id = row.getAttribute('patron_id');
1650                                 if (patron_id) {
1651                                         PATRON.retrieve_via_method( patron_id, 'open-ils.actor.user.fleshed.retrieve' );
1652                                         circ_init();
1653                                         set_decks( { 'patron_scan_deck' : '1' } );
1654                                         focus_widget( row.parentNode );
1655                                 }
1656                         }
1657                 },
1658                 false
1659         );
1660
1661         var t_columns = document.createElement('treecols');
1662         tree.appendChild(t_columns);
1663
1664         for (var i = 0; i < patron_list_columns.length; i++) {
1665                 var column = patron_list_columns[i];
1666                 var t_column = document.createElement('treecol');
1667                 t_columns.appendChild( t_column );
1668                 t_column.setAttribute('label', column.v);
1669                 if (column.s) {
1670                         t_column.setAttribute('id', 'tc_' + column.s + '_' + column.f);
1671                 } else {
1672                         t_column.setAttribute('id', 'tc_' + column.f);
1673                 }
1674                 t_column.setAttribute('flex', '0');
1675                 try {
1676                         if (column.primary) {
1677                                 t_column.setAttribute('primary','true');
1678                                 t_column.setAttribute('flex','1');
1679                         }
1680                         if (column.hidden) t_column.setAttribute('hidden','true');
1681                 } catch(E) {
1682                         mw.sdump('D_LEGACY',js2JSON(E) + '\n');
1683                 }
1684                 t_column.setAttribute('field',column.f);
1685                 if (i != (patron_list_columns.length - 1) ) {
1686                         var t_splitter = document.createElement('splitter');
1687                         t_columns.appendChild( t_splitter );
1688                         t_splitter.setAttribute('class','tree-splitter');
1689                 }
1690         }
1691         var t_children = document.createElement('treechildren');
1692         tree.appendChild(t_children);
1693
1694         for (var i = 0; i < patron_ids.length; i++) {
1695                 var t_item = document.createElement('treeitem');
1696                 t_children.appendChild( t_item );
1697                 t_item.setAttribute('patron_id',patron_ids[i]);
1698
1699                 var t_row = document.createElement('treerow');
1700                 t_item.appendChild( t_row );
1701                 t_row.setAttribute('patron_id',patron_ids[i]);
1702
1703                 //var t_cell = document.createElement('treecell');
1704                 //t_row.appendChild( t_cell );
1705                 //t_cell.setAttribute('label',patron_ids[i]);
1706
1707                 mw.user_async_request(
1708                         'open-ils.actor',
1709                         'open-ils.actor.user.fleshed.retrieve', 
1710                         [ mw.G.auth_ses[0], patron_ids[i] ],
1711                         build_patron_retrieve_for_search_callback( t_row )
1712                 );
1713                 mw.sdump('D_LEGACY','Making call... count = ' + counter_incr('patron_call') + '\n');
1714         }
1715         
1716 }
1717
1718 function build_patron_retrieve_for_search_callback(treerow) {
1719         mw.sdump('D_LEGACY',arg_dump(arguments));
1720         return function (request) {
1721                 mw.sdump('D_LEGACY','Running callback... count = ' + counter_incr('patron_callback') + '\n');
1722                 var result = request.getResultObject();
1723                 mw.sdump('D_LEGACY','Result = ' + js2JSON(result) + '\n');
1724
1725                 for (var i = 0; i < patron_list_columns.length; i++) {
1726                         var column = patron_list_columns[i];
1727                         var t_cell = document.createElement('treecell');
1728                         treerow.appendChild(t_cell);
1729                         var expression;
1730                         if (column.s) {
1731                                 switch(column.s) {
1732                                         case 'home_ou':
1733 expression = 'mw.G.org_tree_hash[ result.home_ou() ].' + column.f + '()';
1734                                         break;
1735                                         case 'mailing_address':
1736 expression = 'find_id_object_in_list( result.addresses() , result.mailing_address() ).' + column.f + '()';
1737                                         break;
1738                                         case 'billing_address':
1739 expression = 'find_id_object_in_list( result.addresses() , result.billing_address() ).' + column.f + '()';
1740                                         break;
1741                                 }
1742                         } else {
1743                                 expression = 'result.' + column.f + '()';
1744                         }
1745                         //mw.sdump('D_LEGACY','Trying to eval: ' + expression + '\n');
1746                         t_cell.setAttribute(
1747                                 'label',
1748                                 eval( expression )
1749                         );
1750                 }
1751         };
1752 }
1753
1754