date validation for patron edit/register
[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');
511                 }
512         }
513         if ( ! PATRON.au.mailing_address() ) {
514                 s += ('Mailing Address required\n');
515         }
516         if ( ! PATRON.au.billing_address() ) {
517                 s += ('Billing Address required\n');
518         }
519         if (s) {
520                 mw.sdump('D_LEGACY',s); alert(s); return false;
521         }
522         return true;
523 }
524
525 function backup_patron(P) {
526         mw.sdump('D_LEGACY',arg_dump(arguments));
527         backup_au = P.au.clone();
528 }
529
530 function restore_patron(P) {
531         mw.sdump('D_LEGACY',arg_dump(arguments));
532         P.au = backup_au.clone();
533         //hash_ac[ P.au.card() ] = find_id_object_in_list( P.au.cards(), P.au.card() );
534         hash_ac[ P.au.card().id() ] = P.au.card();
535         hash_au[ P.au.id() ] = P.au;
536 }
537
538 function save_patron() {
539         mw.sdump('D_LEGACY',arg_dump(arguments));
540         mw.sdump('D_LEGACY','Entering PATRON.save()\n\n=-=-=-=-=-=-=-=\n\n');
541         mw.sdump('D_LEGACY','PATRON.au = ' + js2JSON(PATRON.au) + '\n');
542         mw.sdump('D_LEGACY','PATRON.au.a.length = ' + PATRON.au.a.length + '\n\n');
543         //var backup_json = js2JSON(PATRON.au);
544         //mw.sdump('D_LEGACY','backup_json = ' + backup_json + '\n\n');
545         backup_patron(PATRON);
546         mw.sdump('D_LEGACY','\n\n=-=-=-=-=-=-=-=-=-=-\n\n');
547         check_for_new_addresses();
548         check_for_new_stat_cats();
549         if (! validate_patron() ) { 
550                 mw.sdump('D_LEGACY','restoring backup\n');
551                 restore_patron(PATRON);
552                 return false; 
553         }
554         if (! PATRON.au.usrname() ) { 
555                 PATRON.au.usrname( 
556                         PATRON.barcode()
557                 );
558         }
559         PATRON.au.survey_responses( response_list );
560         mw.sdump('D_LEGACY','before PATRON.au = ' + js2JSON(PATRON.au) + '\n');
561         var result;
562         try {
563                 result = mw.user_request(
564                                 'open-ils.actor',
565                                 'open-ils.actor.patron.update',
566                                 [ mw.G.auth_ses[0], PATRON.au ]
567                         );
568                 if (typeof(result[0]) != 'object') {
569                         mw.sdump('D_LEGACY','unexpected result1 : ' + typeof(result[0]) + ' : ' + js2JSON(result) + '\n');
570                         throw('unexpected result1 : ' + typeof(result[0]) + ' : ' + js2JSON(result) + '\n');
571                 }
572         } catch(E) {
573                 mw.sdump('D_LEGACY','error in \n' + js2JSON(E) + '\n');
574                 mw.sdump('D_LEGACY','restoring backup 2\n');
575                 restore_patron(PATRON);
576                 mw.handle_error(E);
577                 //mw.sdump('D_LEGACY','PATRON.au = ' + js2JSON(PATRON.au) + '\nPATRON.au.a.length = ' + PATRON.au.a.length + '\n');
578                 //PATRON.summary_refresh();
579                 return false;
580         }
581         PATRON.au = result[0];
582         if (! PATRON.au) { 
583                 mw.sdump('D_LEGACY','Restoring backup\n'); 
584                 restore_patron(PATRON);
585                 mw.handle_error('Save Failed'); 
586                 return; 
587         }
588         hash_aua = {};
589         response_list = [];
590         mw.sdump('D_LEGACY','after  PATRON.au = ' + js2JSON(PATRON.au) + '\n');
591         //PATRON.barcode = find_id_object_in_list(PATRON.au.cards(),PATRON.au.card()).barcode();
592         PATRON.summary_refresh();
593         patron_callback('save', { 'au' : PATRON.au });
594         var refresh_result = PATRON.related_refresh(PATRON.au.id());
595         alert('Patron successfully updated.');
596         return refresh_result;
597 }
598 PATRON.save = save_patron;
599
600 function check_for_new_addresses() {
601         mw.sdump('D_LEGACY',arg_dump(arguments));
602         for (var id in hash_aua) {
603                 if ( (id < 0) && ( hash_aua[id].ischanged() ) ) {
604                         mw.sdump('D_LEGACY','Pushing new address\n');
605                         if (!PATRON.au.addresses()) { PATRON.au.addresses( [] ); }
606                         PATRON.au.addresses().push( hash_aua[id] );
607                 }
608         }
609 }
610
611 function check_for_new_stat_cats() {
612         mw.sdump('D_LEGACY',arg_dump(arguments));
613         var entries = new Array();
614         var grid = document.getElementById('patron_edit_stat_cat_grid');
615         var nl = grid.getElementsByTagName('menulist');
616         for (var i = 0; i < nl.length; i++) {
617                 var menulist = nl[i];
618                 if (menulist.getAttribute('original') != menulist.value) {
619                         var n_actscecm = new actscecm();
620                         var id = menulist.getAttribute('entry_id')
621                         //alert('check_for_new_stat_cats: id = ' + id );
622                         if (id) {
623                                 n_actscecm.ischanged('1');
624                                 n_actscecm.id( id );
625                         } else {
626                                 n_actscecm.isnew('1');
627                                 n_actscecm.id( new_id-- );
628                         }
629                         n_actscecm.stat_cat( menulist.getAttribute('stat_cat_id') );
630                         n_actscecm.stat_cat_entry( menulist.value );
631                         n_actscecm.target_usr( PATRON.au.id() );
632                         entries.push( n_actscecm );
633                 }
634
635         }
636         //alert( 'entries = ' + js2JSON( entries ) );
637         PATRON.au.stat_cat_entries( entries );
638 }
639
640 function retrieve_patron_related_info(id) {
641         mw.sdump('D_LEGACY',arg_dump(arguments));
642         if (!id) { id = PATRON.au.id(); }
643         mw.sdump('D_LEGACY','Entering PATRON.related_refresh() with id: ' + id + '\n');
644         /*
645         var checkouts = [];
646         if (id > 0) {
647                 try {
648                         checkouts = mw.user_request(
649                                 'open-ils.circ',
650                                 'open-ils.circ.actor.user.checked_out',
651                                 [ mw.G.auth_ses[0], id ]
652                         )[0];
653                 } catch(E) {
654                         mw.handle_error(E);
655                 }
656         }
657         PATRON.checkouts = checkouts;
658         PATRON.nearest_due = '';
659         for (var i in checkouts) {
660                 var checkout = checkouts[i];
661                 mw.sdump('D_LEGACY','checkout = ' + js2JSON(checkout) + '\n');
662         }
663         PATRON.holds = [];
664         PATRON.bills = [];
665         PATRON.summary_refresh();
666         try {
667                 //circ_init_list();
668         } catch(E) {
669                 mw.sdump('D_LEGACY',js2JSON(E) + '\n');
670         }
671         patron_callback('related_refresh');
672         */
673         return true;
674 }
675 PATRON.related_refresh = retrieve_patron_related_info;
676
677 /* patron_scan_overlay functions */
678
679 function patron_callback(s,params) {
680         mw.sdump('D_LEGACY',arg_dump(arguments));
681         try {
682                 switch(s) {
683                         case 'scan_submit' : return patron_scan_submit_callback(params); break;
684                         case 'related_refresh' : return patron_related_refresh_callback(params); break;
685                         case 'retrieve_patron' : return patron_retrieve_patron_callback(params); break;
686                         case 'save' : return patron_save_callback(params); break;
687                         default : return patron_default_callback(s,params); break;
688                 }
689         } catch(E) {
690                 /* assume no callback defined */
691                 return true;
692         }
693 }
694
695 function patron_advanced_button(ev) {
696         mw.sdump('D_LEGACY',arg_dump(arguments));
697         var deck = document.getElementById('patron_scan_deck');
698         if (deck) { deck.setAttribute('selectedIndex','2'); }
699         focus_widget( 'patron_search_family_name_textbox' );
700         deck = document.getElementById('circ_deck_deck');
701         if (deck) { deck.setAttribute('selectedIndex','6'); }
702         //PATRON.search = {};
703 }
704 PATRON.advanced_search = patron_advanced_button;
705
706 function patron_scan_submit(ev) {
707         mw.sdump('D_LEGACY',arg_dump(arguments));
708         mw.sdump('D_LEGACY','Entering PATRON.scan_submit() with target: ' + ev.target + '\n');
709         try {
710                 var rc = PATRON.retrieve_patron( document.getElementById('patron_scan_textbox').value );
711                 if (rc) {
712                         /* the PATRON object should already be updated.  Switch deck if there is one */
713                         var deck = document.getElementById('patron_scan_deck');
714                         if (deck) { deck.setAttribute('selectedIndex','1'); }
715                         patron_callback('scan_submit');
716                         /* enable the scan item widgets if there are some */
717                         enable_widgets(
718                                 'circ_checkout_scan_search_button',
719                                 'circ_checkout_scan_textbox',
720                                 'circ_checkout_scan_submit_button'
721                         );
722                         focus_widget( 'circ_checkout_scan_textbox' );
723                 } else {
724                         throw('retrieve_patron return code == false');
725                 }
726         } catch(E) {
727                 mw.sdump('D_LEGACY','Could not retrieve patron.  Invalid barcode?\n' + js2JSON(E) + '\n');
728                 alert('Could not retrieve patron.  Invalid barcode?\n' + js2JSON(E) + '\n');
729         }
730 }
731 PATRON.scan_submit = patron_scan_submit;
732
733 function patron_scan_search(ev) {
734         mw.sdump('D_LEGACY',arg_dump(arguments));
735         mw.sdump('D_LEGACY','Entering PATRON.scan_search() with target: ' + ev.target + '\n');
736         //mw.sdump('D_LEGACY','PATRON.search = ' + pretty_print(js2JSON(PATRON.search)) + '\n');
737         try {
738                 var result = mw.user_request(
739                         'open-ils.actor',
740                         'open-ils.actor.patron.search.advanced',
741                         [ mw.G.auth_ses[0], PATRON.search ]
742                 )[0];
743                 mw.sdump('D_LEGACY','result = ' + js2JSON(result) + '\n');
744                 PATRON['search_results'] = result;
745                 build_patron_search_result_deck();
746         } catch(E) {
747                 mw.handle_error(E);
748         }
749 }
750 PATRON.scan_search = patron_scan_search;
751
752 /* patron_summary_overlay functions */
753
754 function make_barcode_handler(card_id,user_id) {
755         mw.sdump('D_LEGACY',arg_dump(arguments));
756         return function (ev) {
757                 magic_field_edit(ev,'ac',card_id,'barcode');
758                 PATRON.au.ischanged('1');
759                 magic_field_edit(ev,'au',user_id,'usrname');
760                 var usr_e = document.getElementById('patron_edit_system_usrname_textbox');
761                 if (usr_e) {
762                         usr_e.value = ev.target.value;
763                 }
764                 /*ev.target.removeEventListener(
765                         "change",
766                         this,
767                         false
768                 );*/
769         }
770 }
771
772 function patron_summary_refresh(ev) {
773         mw.sdump('D_LEGACY',arg_dump(arguments));
774         //alert( mw.arg_dump(arguments) );
775         // This function needs to be broken up.. it sets the patron edit section as well
776         if (!PATRON.au) { return; }
777         mw.sdump('D_LEGACY','Entering PATRON.summary_refresh()\n');
778         hash_au[PATRON.au.id()] = PATRON.au;
779         //PATRON.barcode = find_id_object_in_list(PATRON.au.cards(),PATRON.au.card()).barcode();
780         /* just redraw the display with the PATRON object as is */
781         var barcode_e = document.getElementById('patron_edit_system_barcode_textbox');
782         var newcard_e = document.getElementById('patron_new_card_button');
783         if (barcode_e) {
784                 //var barcode_v = find_id_object_in_list(PATRON.au.cards(),PATRON.au.card());
785                 var barcode_v = PATRON.au.card();
786                 if (barcode_v) {
787                         barcode_e.value = barcode_v.barcode();
788                         if (barcode_e.value != 'REQUIRED') {
789                                 barcode_e.disabled = true;
790                                 newcard_e.hidden = false;       
791                                 barcode_e.addEventListener(
792                                         'change',
793                                         function (ev) {
794                                                 PATRON.au.card().barcode( ev.target.value );
795                                         },
796                                         false
797                                 );
798                         } else {
799                                 barcode_e.addEventListener(
800                                         "change",
801                                         make_barcode_handler(barcode_v.id(),PATRON.au.id()),
802                                         false
803                                 );
804                         }
805                 }
806         }
807         var usrname_e = document.getElementById('patron_edit_system_usrname_textbox');
808         if (usrname_e) {
809                 if (PATRON.au.usrname()) usrname_e.value = PATRON.au.usrname();
810                 usrname_e.setAttribute("onchange",
811                         "magic_field_edit(event,'au'," + PATRON.au.id() + ",'usrname');");
812         }
813         var passwd_e = document.getElementById('patron_edit_system_new_passwd_textbox');
814         if (passwd_e) {
815                 passwd_e.setAttribute("onchange",
816                         "magic_field_edit(event,'au'," + PATRON.au.id() + ",'passwd');");
817         }
818         var name_e = document.getElementById('patron_status_caption');
819         if (name_e) {
820                 var name = '';
821                 if (PATRON.au.prefix()) { name += PATRON.au.prefix() + ' '; }
822                 if (PATRON.au.family_name()) { name += PATRON.au.family_name() + ', '; }
823                 if (PATRON.au.first_given_name()) { name += PATRON.au.first_given_name() + ' '; }
824                 if (PATRON.au.second_given_name()) { name += PATRON.au.second_given_name() + ' '; }
825                 if (PATRON.au.suffix()) { name += PATRON.au.suffix(); }
826
827                 name_e.setAttribute('label',name);
828         }
829         name_e = document.getElementById('patron_edit_system_family_name_textbox');
830         if (name_e) {
831                 if (PATRON.au.family_name()) { name_e.value = PATRON.au.family_name(); }
832                 name_e.setAttribute("onchange","magic_field_edit(event,'au'," + PATRON.au.id() + ",'family_name');");
833         }
834         name_e = document.getElementById('patron_edit_system_first_given_name_textbox');
835         if (name_e) {
836                 if (PATRON.au.first_given_name()) { name_e.value = PATRON.au.first_given_name(); }
837                 name_e.setAttribute("onchange","magic_field_edit(event,'au'," + PATRON.au.id() + ",'first_given_name');");
838         }
839         name_e = document.getElementById('patron_edit_system_second_given_name_textbox');
840         if (name_e) {
841                 if (PATRON.au.second_given_name()) { name_e.value = PATRON.au.second_given_name(); }
842                 name_e.setAttribute("onchange","magic_field_edit(event,'au'," + PATRON.au.id() + ",'second_given_name');");
843         }
844         name_e = document.getElementById('patron_edit_system_prefix_menulist');
845         if (name_e) {
846                 if (PATRON.au.prefix()) { name_e.value = PATRON.au.prefix(); }
847                 name_e.setAttribute("oncommand","magic_field_edit(event,'au'," + PATRON.au.id() + ",'prefix');");
848         }
849         name_e = document.getElementById('patron_edit_system_suffix_menulist');
850         if (name_e) {
851                 if (PATRON.au.suffix()) { name_e.value = PATRON.au.suffix(); }
852                 name_e.setAttribute("oncommand","magic_field_edit(event,'au'," + PATRON.au.id() + ",'suffix');");
853         }
854         var disable_e = document.getElementById('patron_edit_system_disable_checkbox');
855         if (disable_e) {
856                 disable_e.checked = (PATRON.au.active() != 1);
857                 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);}");
858         }
859         var profile_e = document.getElementById('patron_status_data_profile');
860         if (profile_e) {
861                 //profile_e.setAttribute('value',find_id_object_in_list(mw.G.ap_list, PATRON.au.profile() ).name() );
862                 profile_e.setAttribute('value',mw.G.ap_hash[ PATRON.au.profile() ].name() );
863         }
864         profile_e = document.getElementById('patron_edit_system_profile_menulist');
865         if (profile_e) {
866                 //hash_ap[PATRON.au.profile().id()] = PATRON.au.profile();
867                 profile_e.setAttribute("oncommand","magic_field_edit(event,'au'," + PATRON.au.id() + ",'profile');");
868                 set_patron_edit_profile_menu();
869         }
870         var email_e = document.getElementById('patron_edit_contact_email_textbox');
871         if (email_e) {
872                 if (PATRON.au.email()) email_e.value = PATRON.au.email();
873                 email_e.setAttribute("onchange","magic_field_edit(event,'au'," + PATRON.au.id() + ",'email');");
874         }
875         var dayphone_e = document.getElementById('patron_contact_dayphone_data');
876         if (dayphone_e) {
877                 if (PATRON.au.day_phone()) dayphone_e.setAttribute('value', PATRON.au.day_phone() );
878         }
879         dayphone_e = document.getElementById('patron_edit_contact_dayphone_textbox');
880         if (dayphone_e) {
881                 if (PATRON.au.day_phone()) dayphone_e.value = PATRON.au.day_phone();
882                 dayphone_e.setAttribute("onchange","magic_field_edit(event,'au'," + PATRON.au.id() + ",'day_phone');");
883         }
884         var eveningphone_e = document.getElementById('patron_contact_eveningphone_data');
885         if (eveningphone_e) {
886                 if (PATRON.au.evening_phone()) eveningphone_e.setAttribute('value', PATRON.au.evening_phone() );
887         }
888         eveningphone_e = document.getElementById('patron_edit_contact_eveningphone_textbox');
889         if (eveningphone_e) {
890                 if (PATRON.au.evening_phone()) eveningphone_e.value = PATRON.au.evening_phone();
891                 eveningphone_e.setAttribute("onchange","magic_field_edit(event,'au'," + PATRON.au.id() + ",'evening_phone');");
892         }
893         var otherphone_e = document.getElementById('patron_contact_otherphone_data');
894         if (otherphone_e) {
895                 if (PATRON.au.other_phone()) otherphone_e.setAttribute('value', PATRON.au.other_phone() );
896         }
897         otherphone_e = document.getElementById('patron_edit_contact_otherphone_textbox');
898         if (otherphone_e) {
899                 if (PATRON.au.other_phone()) otherphone_e.value = PATRON.au.other_phone();
900                 otherphone_e.setAttribute("onchange","magic_field_edit(event,'au'," + PATRON.au.id() + ",'other_phone');");
901         }
902         var standing_e = document.getElementById('patron_status_data_standing');
903         if (standing_e) {
904                 /*var standing = find_id_object_in_list( 
905                                 mw.G.cst_list,
906                                 PATRON.au.standing() 
907                         );*/
908                 var standing = mw.G.cst_hash[ PATRON.au.standing() ];
909                 mw.sdump('D_LEGACY','standing = ' + js2JSON(standing) + '\n');
910                 standing_e.setAttribute( 'value', standing.value() );
911                 if (standing.value() == 'Good') {
912                         add_css_class(standing_e,'good');
913                 } else {
914                         remove_css_class(standing_e,'good');    
915                 }
916         }
917         var claims_returned_e = document.getElementById('patron_status_data_claims_returned');
918         if (claims_returned_e) {
919                 claims_returned_e.setAttribute('value',PATRON.au.claims_returned_count());
920         }
921         var credit_e = document.getElementById('patron_status_data_credit');
922         if (credit_e) {
923                 credit_e.setAttribute('value',PATRON.au.credit_forward_balance());
924         }
925         var homelib_e = document.getElementById('patron_status_data_homelib');
926         if (homelib_e) {
927                 homelib_e.setAttribute('value', 
928                         find_ou(
929                                 mw.G.org_tree,
930                                 PATRON.au.home_ou()
931                         ).name()
932                 );
933         }
934         homelib_e = document.getElementById('patron_edit_system_library_menulist');
935         if (homelib_e) {
936                 homelib_e.setAttribute("oncommand","magic_field_edit(event,'au'," + PATRON.au.id() + ",'home_ou',false);");
937                 set_patron_edit_library_menu();
938         }
939         var fees_e = document.getElementById('patron_status_data_fees');
940         if (fees_e) {
941                 fees_e.setAttribute('value',PATRON.bills.length);
942         }
943         var checkouts_e = document.getElementById('patron_status_data_checkouts');
944         if (checkouts_e) {
945                 checkouts_e.setAttribute('value',PATRON.checkouts.length);
946         }
947         var holds_e = document.getElementById('patron_status_data_holds');
948         if (holds_e) {
949                 holds_e.setAttribute('value',PATRON.holds.length);
950         }
951         var nearest_due_e = document.getElementById('patron_status_data_nearest_due');
952         if (nearest_due_e) {
953                 nearest_due_e.setAttribute('value',PATRON.nearest_due);
954         }
955         var id1value_e = document.getElementById('patron_edit_system_id1value_textbox');
956         if (id1value_e) {
957                 if (PATRON.au.ident_value()) id1value_e.setAttribute('value',PATRON.au.ident_value());
958                 id1value_e.setAttribute("onchange","magic_field_edit(event,'au'," + PATRON.au.id() + ",'ident_value');");
959         }
960         var id2value_e = document.getElementById('patron_edit_system_id2value_textbox');
961         if (id2value_e) {
962                 if (PATRON.au.ident_value2()) id2value_e.setAttribute('value',PATRON.au.ident_value2());
963                 id2value_e.setAttribute("onchange","magic_field_edit(event,'au'," + PATRON.au.id() + ",'ident_value2');");
964         }
965         var id1type_e = document.getElementById('patron_edit_system_id1type_menulist');
966         if (id1type_e) {
967                 id1type_e.setAttribute("oncommand","magic_field_edit(event,'au'," + PATRON.au.id() + ",'ident_type');");
968                 set_patron_edit_ident_type_menu(); 
969         }
970         var id2type_e = document.getElementById('patron_edit_system_id2type_menulist');
971         if (id2type_e) {
972                 id2type_e.setAttribute("oncommand","magic_field_edit(event,'au'," + PATRON.au.id() + ",'ident_type2');");
973                 set_patron_edit_ident_type_menu2();
974         }
975         var dob_e = document.getElementById('patron_edit_system_dob_textbox');
976         if (dob_e) {
977                 if (PATRON.au.dob()) dob_e.setAttribute('value',PATRON.au.dob());
978                 dob_e.setAttribute("onchange","magic_field_edit(event,'au'," + PATRON.au.id() + ",'dob',false);");
979         }
980         //populate_patron_survey_grid('patron_survey_grid');
981         populate_patron_survey_grid('patron_edit_survey_grid');
982         populate_patron_edit_surveys();
983
984         var address_rows = document.getElementById('patron_contact_address_rows');
985         if (address_rows) {
986                 empty_widget(address_rows);
987                 var addresses = PATRON.au.addresses();
988                 for (var i in addresses) { 
989                         if (typeof(addresses[i])=='object') {
990                                 var address = addresses[i];
991                                 if (
992                                         (address.id() == PATRON.au.mailing_address()) ||
993                                         (address.id() == PATRON.au.billing_address())
994                                 ) {
995                                         //mw.sdump('D_LEGACY','address dump: ' + i + ' : ' + addresses[i] + '\n');
996                                         rows_append_address( address_rows, address, false );
997                                 }
998                         }
999                 }
1000         }
1001         address_rows = document.getElementById('patron_edit_address_rows');
1002         if (address_rows) {
1003                 empty_widget(address_rows);
1004                 var addresses = PATRON.au.addresses();
1005                 for (var i in addresses) { 
1006                         if (typeof(addresses[i])=='object') {
1007                                 //mw.sdump('D_LEGACY','address dump: ' + i + ' : ' + js2JSON(addresses[i]) + '\n');
1008                                 rows_append_address( address_rows, addresses[i], true );
1009                         }
1010                 }
1011                 var blank = new aua();
1012                 blank.id( new_id-- );
1013                 blank.isnew("1");
1014                 blank.usr( PATRON.au.id() );
1015                 /*if ( addresses.length == 0 ) {*/
1016                 if (hash_aua.length == 0) {
1017                         blank.address_type( 'REQUIRED' );
1018                         blank.ischanged( "1" );
1019                         PATRON.au.mailing_address( blank.id() );
1020                         PATRON.au.billing_address( blank.id() );
1021                 } else {
1022                         blank.address_type( 'NEW' );
1023                 }
1024                 blank.city( 'CITY' );
1025                 blank.country( 'USA' );
1026                 blank.county( 'COUNTY' );
1027                 blank.post_code( 'ZIP' );
1028                 blank.state( 'GA' );
1029                 blank.street1( 'STREET1' );
1030                 blank.street2( 'STREET2' );
1031                 rows_append_address( address_rows, blank , true );
1032         }
1033
1034         var alert_message_e = document.getElementById('patron_edit_system_alert_message_textbox');
1035         if (alert_message_e) {
1036                 if (PATRON.au.alert_message()) alert_message_e.setAttribute('value',PATRON.au.alert_message());
1037                 alert_message_e.setAttribute("onchange","magic_field_edit(event,'au'," + PATRON.au.id() + ",'alert_message',false);");
1038         }
1039         var stat_cats_e = document.getElementById('patron_statcat_rows');
1040         if (stat_cats_e) {
1041                 try {
1042                         empty_widget( stat_cats_e );
1043                         for (var i = 0; i < PATRON.au.stat_cat_entries().length; i++) {
1044
1045                                 // fieldmapper
1046                                 var entry = PATRON.au.stat_cat_entries()[i];
1047                                 var stat_cat = mw.G.actsc_hash[ entry.stat_cat() ];
1048
1049                                 // build XUL
1050                                 var row = document.createElement('row');
1051                                 stat_cats_e.appendChild(row);
1052                                 var sc_label = document.createElement('label');
1053                                 row.appendChild(sc_label);
1054                                 var sce_label = document.createElement('label');
1055                                 row.appendChild(sce_label);
1056
1057                                 // set values
1058                                 sc_label.setAttribute('value', stat_cat.name() );
1059                                 sce_label.setAttribute('value', entry.stat_cat_entry() );
1060                         }
1061                 } catch(E) {
1062                         mw.handle_error(E);
1063                 }
1064         }
1065         stat_cats_e = document.getElementById('patron_edit_stat_cat_grid');
1066         if (stat_cats_e) {
1067                 //alert('stat_cats_e');
1068                 try {
1069                         //alert('in try');
1070                         for (var i = 0; i < PATRON.au.stat_cat_entries().length; i++) {
1071                                 var entry = PATRON.au.stat_cat_entries()[i];
1072                                 //alert('entry = ' + js2JSON(entry) );
1073                                 var stat_cat = entry.stat_cat();
1074                                 if (typeof stat_cat == 'object') stat_cat = stat_cat.id();
1075                                 var menulist = document.getElementById('menulist_stat_cat_' + stat_cat);
1076                                 if (menulist) {
1077
1078                                         //alert('menulist');
1079
1080                                         menulist.value = entry.stat_cat_entry();
1081                                         menulist.setAttribute( 'original', menulist.value );
1082                                         menulist.setAttribute( 'entry_id', entry.id() );
1083                                         //alert('summary_refresh: entry.id() = ' + entry.id() );
1084
1085                                 }
1086                         }
1087                 } catch(E) {
1088                         mw.handle_error(E);
1089                 }
1090         }
1091         /*
1092         if (PATRON.au.alert_message()) {
1093                 snd_bad(); snd_bad();
1094                 s_alert(
1095                         'PATRON ALERT MESSAGE\n\n\n\n' +
1096                         PATRON.au.alert_message() +
1097                         '\n\n\n\nTo remove this alert permanently, Edit the patron and erase the message in "Alert Message".\n\n'
1098                 );
1099         }
1100         */
1101 }
1102 PATRON.summary_refresh = patron_summary_refresh;
1103
1104 function rows_append_address( rows, address, edit ) {
1105         mw.sdump('D_LEGACY',arg_dump(arguments));
1106
1107
1108         // patron_summary
1109
1110         //mw.sdump('D_LEGACY','Entering rows_append_address()\n');
1111         if (typeof(rows) != 'object') {
1112                 rows = document.getElementById(box);
1113         }
1114         if (typeof(rows) != 'object') {
1115                 mw.sdump('D_LEGACY','rows_append_address: could not find ' + rows + '\n');
1116                 alert('rows_append_address: could not find ' + rows + '\n');
1117                 return false;
1118         }
1119         var row = document.createElement('row');
1120                 //row.setAttribute('id','row_address_'+yesno(edit)+address.id());
1121                 hash_aua[ address.id() ] = address;
1122         rows.appendChild(row);
1123         var groupbox = document.createElement('groupbox');
1124                 if (edit) {
1125                         groupbox.setAttribute('id','groupbox_address_'+address.id());
1126                 } else {
1127                         groupbox.setAttribute('id','groupbox_summary_address_'+address.id());
1128                 }
1129                 groupbox.setAttribute('flex','1');
1130         row.appendChild(groupbox);
1131         var caption = document.createElement('caption');
1132                 var caption_string = address.address_type();
1133                 if ( PATRON.au.mailing_address() == address.id() ) {
1134                         caption_string = '(MAILING) ' + caption_string;
1135                 }
1136                 if ( PATRON.au.billing_address() == address.id() ) {
1137                         caption_string = '(PHYSICAL) ' + caption_string;
1138                 }
1139                 caption.setAttribute( 'label',caption_string );
1140         groupbox.appendChild(caption);
1141
1142         // patron_edit
1143
1144         if (edit) {
1145                 var hbox = document.createElement('hbox');
1146                 groupbox.appendChild(hbox);
1147                         var label = document.createElement('label');
1148                                 label.setAttribute('value','Type:');
1149                         hbox.appendChild(label);
1150                         var textbox = document.createElement('textbox');
1151                         textbox.setAttribute('size','20');
1152                         textbox.setAttribute("onchange","magic_field_edit(event,'aua'," + address.id() + ",'address_type');");
1153                         hbox.appendChild(textbox);
1154                                 textbox.value = address.address_type();
1155                         var label2 = document.createElement('label');
1156                                 label2.setAttribute('value','Physical:');
1157                         hbox.appendChild(label2);
1158                         var checkbox_billing = document.createElement('checkbox');
1159                                 checkbox_billing.setAttribute('group','billing');
1160                                 checkbox_billing.setAttribute('oncommand','radio_checkbox(event); PATRON.au.billing_address(' + address.id() + '); PATRON.au.ischanged("1");');
1161                         hbox.appendChild(checkbox_billing);
1162                         if ( PATRON.au.billing_address() == address.id() ) {
1163                                 checkbox_billing.checked = true;
1164                         }
1165                         var label3 = document.createElement('label');
1166                                 label3.setAttribute('value','Mailing:');
1167                         hbox.appendChild(label3);
1168                         var checkbox_mailing = document.createElement('checkbox');
1169                                 checkbox_mailing.setAttribute('group','mailing');
1170                                 checkbox_mailing.setAttribute('oncommand','radio_checkbox(event); PATRON.au.mailing_address(' + address.id() + '); PATRON.au.ischanged("1");');
1171                         hbox.appendChild(checkbox_mailing);
1172                         if ( PATRON.au.mailing_address() == address.id() ) {
1173                                 checkbox_mailing.checked = true;
1174                         }
1175                         var label4 = document.createElement('label');
1176                                 label4.setAttribute('value','Invalid:');
1177                         hbox.appendChild(label4);
1178                         var checkbox_invalid = document.createElement('checkbox');
1179                                 checkbox_invalid.setAttribute('oncommand','invalid_checkbox(event,' + address.id() + ');');
1180                         hbox.appendChild(checkbox_invalid);
1181                         mw.sdump('D_LEGACY','address ' + address.id() + ' valid = ' + address.valid() + '\n');
1182                         if ( address.valid() == '1' ) {
1183                                 checkbox_invalid.checked = false;
1184                         } else {
1185                                 checkbox_invalid.checked = true;
1186                         }
1187                         var label5 = document.createElement('spacer');
1188                                 label5.setAttribute('flex','1');
1189                         hbox.appendChild(label5);
1190                         if (address.id()>-1) {
1191                                 var label6 = document.createElement('button');
1192                                         label6.setAttribute('label','Delete');
1193                                         label6.setAttribute('alt_label','Un-Delete');
1194                                         label6.setAttribute('toggle','0');
1195                                         label6.setAttribute('oncommand','toggle_address(event,' + address.id() + ');');
1196                                 hbox.appendChild(label6);
1197                         }
1198         }
1199         var street1;
1200                 if (edit) {
1201                         street1 = document.createElement('textbox');
1202                         street1.setAttribute('size','40');
1203                         street1.setAttribute("onchange","magic_field_edit(event,'aua'," + address.id() + ",'street1');");
1204                 } else {
1205                         street1 = document.createElement('label');
1206                 }
1207                 street1.setAttribute( 'value',address.street1() );
1208         groupbox.appendChild(street1);
1209         var street2;
1210                 if (edit) {
1211                         street2 = document.createElement('textbox');
1212                         street2.setAttribute('size','40');
1213                         street2.setAttribute("onchange","magic_field_edit(event,'aua'," + address.id() + ",'street2');");
1214                 } else {
1215                         street2 = document.createElement('label');
1216                 }
1217                 street2.setAttribute( 'value',address.street2() );
1218         groupbox.appendChild(street2);
1219         var hbox = document.createElement('hbox');
1220         groupbox.appendChild(hbox);
1221                 var city;
1222                         if (edit) {
1223                                 city = document.createElement('textbox');
1224                                 city.setAttribute('size','20');
1225                                 city.setAttribute("onchange","magic_field_edit(event,'aua'," + address.id() + ",'city');");
1226                         } else {
1227                                 city = document.createElement('label');
1228                         }
1229                         city.setAttribute( 'value',address.city() );
1230                 hbox.appendChild(city);
1231                 var county;
1232                         if (edit) {
1233                                 county = document.createElement('textbox');
1234                                 county.setAttribute('size','20');
1235                                 county.setAttribute("onchange","magic_field_edit(event,'aua'," + address.id() + ",'county');");
1236                                 county.setAttribute( 'value',address.county() );
1237                         } else {
1238                                 county = document.createElement('label');
1239                                 county.setAttribute( 'value','(' + address.county() + '),');
1240                         }
1241                 hbox.appendChild(county);
1242                 if (!address.county()) {
1243                         if (!edit) {
1244                                 county.setAttribute( 'display', 'none' );
1245                                 city.setAttribute( 'value',address.city() + ',' );
1246                         }
1247                 }
1248                 var state;
1249                         if (edit) {
1250                                 state = document.createElement('textbox');
1251                                 state.setAttribute('size','2');
1252                                 state.setAttribute("onchange","magic_field_edit(event,'aua'," + address.id() + ",'state');");
1253                         } else {
1254                                 state = document.createElement('label');
1255                         }
1256                         state.setAttribute( 'value',address.state() );
1257                 hbox.appendChild(state);
1258                 var country;
1259                         if (edit) {
1260                                 country = document.createElement('textbox');
1261                                 country.setAttribute('size','3');
1262                                 country.setAttribute("onchange","magic_field_edit(event,'aua'," + address.id() + ",'country');");
1263                                 country.setAttribute( 'value',address.country() );
1264                         } else {
1265                                 country = document.createElement('label');
1266                                 country.setAttribute( 'value','(' + address.country() + '),');
1267                         }
1268                 hbox.appendChild(country);
1269                 var zip;
1270                         if (edit) {
1271                                 zip = document.createElement('textbox');
1272                                 zip.setAttribute('size','10');
1273                                 zip.setAttribute("onchange","magic_field_edit(event,'aua'," + address.id() + ",'post_code');");
1274                         } else {
1275                                 zip = document.createElement('label');
1276                         }
1277                         zip.setAttribute( 'value',address.post_code() );
1278                 hbox.appendChild(zip);
1279 }
1280
1281 function invalid_checkbox(e,id) {
1282         mw.sdump('D_LEGACY',arg_dump(arguments));
1283         if (e.target.checked) {
1284                 mw.sdump('D_LEGACY','Marking address ' + id + ' invalid\n');
1285                 mw.sdump('D_LEGACY','\tbefore address: ' + js2JSON(hash_aua[id]) + '\n');
1286                 hash_aua[id].valid( '0' );
1287                 hash_aua[id].ischanged( '1' );
1288                 mw.sdump('D_LEGACY','\tafter  address: ' + js2JSON(hash_aua[id]) + '\n');
1289         } else {
1290                 mw.sdump('D_LEGACY','Marking address ' + id + ' valid\n');
1291                 mw.sdump('D_LEGACY','\tbefore address: ' + js2JSON(hash_aua[id]) + '\n');
1292                 hash_aua[id].valid( '1' );
1293                 hash_aua[id].ischanged( '1' );
1294                 mw.sdump('D_LEGACY','\tafter  address: ' + js2JSON(hash_aua[id]) + '\n');
1295         }
1296 }
1297
1298 function toggle_address(e,id) {
1299         mw.sdump('D_LEGACY',arg_dump(arguments));
1300         var groupbox = document.getElementById('groupbox_address_' + id);
1301         //var address = find_id_object_in_list( hash_aua, id );
1302         var address = hash_aua[id];
1303         var button = e.target;
1304         var label = button.getAttribute('label');
1305         var alt_label = button.getAttribute('alt_label');
1306         button.setAttribute('label',alt_label);
1307         button.setAttribute('alt_label',label);
1308         var toggle = button.getAttribute('toggle');
1309         if (toggle == '0') {
1310                 button.setAttribute('toggle','1');
1311                 mw.sdump('D_LEGACY','original node = ' + js2JSON(address) + '\n');
1312                 add_css_class(groupbox,'deleted_address');
1313                 address.isdeleted('1');
1314                 mw.sdump('D_LEGACY','updated  node = ' + js2JSON(address) + '\n');
1315                 mw.sdump('D_LEGACY','PATRON.au.mailing_address() = ' + PATRON.au.mailing_address() + ' address.id() = ' + address.id() + '\n');
1316                 if (PATRON.au.mailing_address() == address.id() ) {
1317                         find_available_address_for('mailing_address');
1318                 }
1319                 mw.sdump('D_LEGACY','PATRON.au.billing_address() = ' + PATRON.au.billing_address() + ' address.id() = ' + address.id() + '\n');
1320                 if (PATRON.au.billing_address() == address.id() ) {
1321                         find_available_address_for('billing_address');
1322                 }
1323         } else {
1324                 button.setAttribute('toggle','0');
1325                 mw.sdump('D_LEGACY','original node = ' + js2JSON(address) + '\n');
1326                 remove_css_class(groupbox,'deleted_address');
1327                 address.isdeleted('0');
1328                 mw.sdump('D_LEGACY','updated  node = ' + js2JSON(address) + '\n');
1329         }
1330         var nl = groupbox.getElementsByTagName('textbox');
1331                 for (var i in nl) {
1332                         if (typeof(nl[i])=='object') {
1333                                 var t = nl[i];
1334                                 t.disabled = ! t.disabled;
1335                         }
1336                 }
1337 }
1338
1339 function find_available_address_for(which) {
1340         mw.sdump('D_LEGACY',arg_dump(arguments));
1341         mw.sdump('D_LEGACY','entering find_avialable_address_for(' + which + ')\n');
1342         var addresses = PATRON.au.addresses();
1343         mw.sdump('D_LEGACY','considering existing addresses...\n');
1344         for (var i in addresses) {
1345                 var address = addresses[i];
1346                 mw.sdump('D_LEGACY','i = ' + i + ' addresses[i] = ' + js2JSON(address) + '\n');
1347                 if ( address.isdeleted() == '1') { continue; }
1348                 if ( (address.address_type() == 'NEW') && (address.id() < 0) ) {
1349                         address.address_type('REQUIRED');
1350                         address.ischanged( '1' );
1351                 }
1352                 mw.sdump('D_LEGACY','PATRON.au before = ' + js2JSON(PATRON.au) + '\n');
1353                 var command = 'PATRON.au.' + which + "( '" + address.id() + "' );";
1354                 mw.sdump('D_LEGACY', command + '\n' );
1355                 eval( command );
1356                 mw.sdump('D_LEGACY','PATRON.au after  = ' + js2JSON(PATRON.au) + '\n');
1357                 return true;
1358         }
1359         mw.sdump('D_LEGACY','considering old and new addresses...\n');
1360         for (var i in hash_aua) {
1361                 var address = hash_aua[i];
1362                 mw.sdump('D_LEGACY','i = ' + i + ' addresses[i] = ' + js2JSON(address) + '\n');
1363                 if ( address.isdeleted() == '1') { continue; }
1364                 if ( (address.address_type() == 'NEW') && (address.id() < 0) ) {
1365                         address.address_type('REQUIRED');
1366                         address.ischanged( '1' );
1367                 }
1368                 mw.sdump('D_LEGACY','PATRON.au before = ' + js2JSON(PATRON.au) + '\n');
1369                 var command = 'PATRON.au.' + which + "( '" + address.id() + "' );";
1370                 mw.sdump('D_LEGACY', command + '\n' );
1371                 eval( command );
1372                 mw.sdump('D_LEGACY','PATRON.au after  = ' + js2JSON(PATRON.au) + '\n');
1373                 return true;
1374         }
1375 }
1376
1377 function survey_test(ev,survey_id) {
1378         mw.sdump('D_LEGACY',arg_dump(arguments));
1379
1380         document.getElementById('circ_deck_deck').setAttribute('selectedIndex','5');
1381         var vbox = document.getElementById('patron_survey_vbox');
1382         empty_widget( vbox ); 
1383         survey_render_with_results(
1384                 vbox,
1385                 survey_id,
1386                 function (survey) {
1387                         return PATRON.refresh();
1388                 }
1389         );
1390 }
1391
1392 function populate_patron_edit_surveys() {
1393         mw.sdump('D_LEGACY',arg_dump(arguments));
1394         var vbox = document.getElementById('patron_edit_survey_vbox');
1395         if (!vbox) return;
1396
1397         mw.sdump('D_LEGACY','populate_patron_edit_surveys()\n');
1398
1399         empty_widget( vbox ); 
1400
1401         var surveys = [];
1402         try {
1403                 surveys = mw.user_request(
1404                         'open-ils.circ',
1405                         'open-ils.circ.survey.retrieve.required',
1406                         [ mw.G.auth_ses[0] ]
1407                 )[0];
1408         } catch(E) {
1409                 mw.handle_error(E);
1410         }
1411
1412         for (var i = 0; i < surveys.length; i++) {
1413                 var survey = surveys[i];
1414                 survey_render(
1415                         vbox,
1416                         survey.id(),
1417                         populate_patron_edit_surveys_build_callback( survey ),
1418                         null
1419                 );
1420         }
1421 }
1422
1423 function populate_patron_edit_surveys_build_callback( survey ) {
1424         mw.sdump('D_LEGACY',arg_dump(arguments));
1425         return function (responses) {
1426                 for (var i in responses) {
1427                         response_list.push( responses[i] );
1428                 }
1429                 var nframe = document.getElementById('patron_survey_frame_'+survey.id());
1430                 nframe.hidden = true;
1431                 var complete = document.createElement('description');
1432                 complete.setAttribute('value',survey.name() + ' Complete');
1433                 nframe.parentNode.insertBefore( complete, nframe );
1434                 //nframe.contentWindow.document.body.innerHTML = '<h1>' + survey.name() + ' Complete</h1>';
1435                 return true;
1436         };
1437 }
1438
1439
1440 function survey_render(vbox,survey_id,commit_callback,submit_callback) {
1441         sdump('D_LEGACY',arg_dump(arguments));
1442         if (typeof(vbox) != 'object') { vbox = document.getElementById(vbox); }
1443         var frame = document.createElement('vbox');
1444         vbox.appendChild(frame);
1445         //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>');
1446         
1447         setTimeout(function(){
1448         /*var doc = frame.contentWindow.document;*/
1449         var doc = document;
1450         window.createAppElement = function (name) { return doc.createElementNS('http://www.w3.org/1999/xhtml','html:'+name); }
1451         window.createTextNode = function (value) { return doc.createTextNode(value); }
1452         Survey.retrieveById( 
1453                 mw.G.auth_ses[0] , 
1454                 survey_id,
1455                 function(sur) { 
1456                         sur.setUser( PATRON.au.id() ); 
1457                         if (submit_callback) sur.setSubmitCallback( submit_callback );
1458                         if (commit_callback) sur.commitCallback = commit_callback;
1459                         mw.sdump('D_LEGACY','survey id: ' + sur.survey.id() + '\n');
1460
1461                         var node = sur.getNode();
1462
1463                         try {
1464                                 dump( super_dump( node ) );
1465                         } catch(E) {
1466                                 dump( js2JSON(E) );
1467                         }
1468
1469                         frame.appendChild( node ); 
1470                         /*frame.setAttribute('style','height: ' + (30+doc.height) + 'px;');*/
1471                         frame.setAttribute('id','patron_survey_frame_' + sur.survey.id());
1472                 } 
1473         );
1474         },0);
1475 }
1476
1477 function survey_render_with_results(vbox,survey_id,callback) {
1478         sdump('D_LEGACY',arg_dump(arguments));
1479         if (typeof(vbox) != 'object') { vbox = document.getElementById(vbox); }
1480         var frame = document.createElement('vbox');
1481         vbox.appendChild(frame);
1482         //frame.setAttribute('id','patron_survey_frame'); 
1483         //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>');
1484         //var doc = frame.contentWindow.document;
1485         var doc = document;
1486         //HTMLdoc = doc;
1487         window.createAppElement = function (name) { return doc.createElementNS('http://www.w3.org/1999/xhtml','html:'+name); }
1488         window.createTextNode = function (value) { return doc.createTextNode(value); }
1489         Survey.retrieveById( 
1490                 mw.G.auth_ses[0] , 
1491                 survey_id,
1492                 function(sur) { 
1493                         sur.setUser( PATRON.au.id() ); 
1494                         sur.setSubmitCallback( callback );
1495                         mw.sdump('D_LEGACY','survey id: ' + sur.survey.id() + '\n');
1496                         frame.appendChild( sur.getNode() ); 
1497                         var span = doc.createElement('html:blockquote');
1498                         span.setAttribute('id','survey_response_' + sur.survey.id());
1499                         span.setAttribute('class','survey');
1500                         var warning = doc.createTextNode('Retrieving Responses...');
1501                         span.appendChild(warning);
1502                         frame.appendChild(span);
1503                         mw.user_async_request(
1504                                 'open-ils.circ',
1505                                 'open-ils.circ.survey.response.retrieve',
1506                                 [ mw.G.auth_ses[0], sur.survey.id(), PATRON.au.id() ],
1507                                 function (request) {
1508                                         result = request.getResultObject().reverse();
1509                                         span.removeChild( warning );
1510                                         if (result.length == 0) { return; }
1511                                         //span.appendChild( doc.createTextNode('Previous Responses:') );
1512                                         //span.appendChild( doc.createElement('html:br') );
1513                                         //span.setAttribute('style','border: black solid thin;');
1514                                         var num_of_q = sur.survey.questions().length;
1515                                         var current_q = 0;
1516                                         span.appendChild( doc.createTextNode(
1517                                                 'Previous Responses:'
1518                                         ) );
1519                                         span.appendChild( doc.createElement('html:br') );
1520                                         span.appendChild( doc.createElement('html:br') );
1521                                         var block;
1522                                         for (var i = 0; i < result.length; i++) {
1523                                                 if (++current_q > num_of_q) { current_q = 1; }
1524                                                 mw.sdump('D_LEGACY','current_q = ' + current_q + '  num_of_q = ' + num_of_q + '\n');
1525                                                 if (current_q == 1) {
1526                                                         block = doc.createElement('html:blockquote');
1527                                                         span.appendChild( doc.createTextNode(
1528                                                                 'Answer Date: ' + 
1529                                                                 result[i].answer_date() +
1530                                                                 ', Effective Date: ' + 
1531                                                                 result[i].effective_date()
1532                                                         ) );
1533                                                         span.appendChild( doc.createElement('html:br') );
1534                                                         span.appendChild(block);
1535                                                 }
1536                                                 block.appendChild(
1537                                                         doc.createTextNode(
1538                                                                 current_q + ') ' + 
1539                                                                 find_id_object_in_list(
1540                                                                         find_id_object_in_list(
1541                                                                                 sur.survey.questions(),
1542                                                                                 result[i].question()
1543                                                                         ).answers(),
1544                                                                         result[i].answer()
1545                                                                 ).answer() + ' '
1546                                                         )
1547                                                 );
1548                                         }
1549                                         span.appendChild( doc.createElement('html:br') );
1550                                         /*frame.setAttribute('style','height: ' + (30+doc.height) + 'px;');*/
1551                                 }
1552                         );
1553                 } 
1554         );
1555 }
1556
1557
1558 function handle_patron_search_textbox(ev,group) {
1559         mw.sdump('D_LEGACY',arg_dump(arguments));
1560         var id = ev.target.getAttribute('id');
1561         var field = id.split(/_/).slice(2,-1).join('_');
1562         mw.sdump('D_LEGACY','field = ' + field + ' value = ' + ev.target.value + '\n');
1563         PATRON.search[field] = { 'value' : ev.target.value, 'group' : group };
1564 }
1565
1566 function build_patron_search_result_deck() {
1567         mw.sdump('D_LEGACY',arg_dump(arguments));
1568         var label = document.getElementById('patron_search_results_label');
1569         if (label) {
1570                 var s = 'Found ' + PATRON.search_results.length + ' matches.  ';
1571                 if (PATRON.search_results.length > patron_hits_per_page) {
1572                         s += 'Displaying ' + patron_hits_per_page + ' per page:';
1573                 }
1574                 label.setAttribute('value',s);
1575         }
1576         var deck = document.getElementById('patron_search_results_deck');       
1577         if (!deck) return;
1578
1579         empty_widget(deck);
1580
1581         var patron_ids = PATRON.search_results.slice(0,patron_hits_per_page);
1582         PATRON.search_results = PATRON.search_results.slice(patron_hits_per_page);
1583         build_patron_search_result_page(deck,patron_ids,PATRON.search_results.length);
1584 }
1585
1586 function build_patron_search_result_page(deck,patron_ids,remaining) {
1587         mw.sdump('D_LEGACY',arg_dump(arguments));
1588         mw.sdump('D_LEGACY','build_patron_search_result_page()\n');
1589         if (typeof(deck)!='object') deck = document.getElementById(deck);
1590         if (!deck) return;
1591
1592         if (patron_ids.length == 0) return;
1593
1594         var vbox = document.createElement('vbox');
1595         deck.appendChild(vbox);
1596         vbox.setAttribute('flex','1');
1597
1598         var idx = deck.childNodes.length - 1;
1599         deck.selectedIndex = idx;
1600
1601         var button_box = document.createElement('hbox');
1602         vbox.appendChild(button_box);
1603
1604         var back_button = document.createElement('button');
1605         button_box.appendChild(back_button);
1606         back_button.setAttribute('label','Previous');
1607         back_button.disabled = true;
1608         if (idx > 0) {
1609                 back_button.disabled = false;
1610                 back_button.setAttribute('oncommand',"var deck = document.getElementById('patron_search_results_deck'); deck.selectedIndex = deck.selectedIndex -1;");
1611         }
1612         var forward_button = document.createElement('button');
1613         button_box.appendChild(forward_button);
1614         forward_button.setAttribute('label','Next');
1615         forward_button.disabled = true;
1616         if (remaining > 0) {
1617                 forward_button.disabled = false;
1618                 forward_button.addEventListener(
1619                         'command',
1620                         function (ev) {
1621                                 var fired = ev.target.getAttribute('fired');
1622
1623                                 if (fired) {
1624                                         deck.selectedIndex = idx + 1;
1625                                 } else {
1626                                         var next_ids = PATRON.search_results.slice(0,patron_hits_per_page);
1627                                         PATRON.search_results = PATRON.search_results.slice(patron_hits_per_page);
1628                                         build_patron_search_result_page(deck,next_ids,PATRON.search_results.length);
1629                                         ev.target.setAttribute('fired',true);
1630                                 }
1631                         },
1632                         false
1633                 );
1634         }
1635         var tree = document.createElement('tree');
1636         vbox.appendChild(tree);
1637         tree.setAttribute('flex','1');
1638         tree.setAttribute('enableColumnDrag','true');
1639         tree.addEventListener(
1640                 'select',
1641                 function (ev) {
1642                         var row = get_list_from_tree_selection(ev.target)[0];
1643                         if (row) {
1644                                 var patron_id = row.getAttribute('patron_id');
1645                                 if (patron_id) {
1646                                         PATRON.retrieve_via_method( patron_id, 'open-ils.actor.user.fleshed.retrieve' );
1647                                         circ_init();
1648                                         set_decks( { 'patron_scan_deck' : '1' } );
1649                                         focus_widget( row.parentNode );
1650                                 }
1651                         }
1652                 },
1653                 false
1654         );
1655
1656         var t_columns = document.createElement('treecols');
1657         tree.appendChild(t_columns);
1658
1659         for (var i = 0; i < patron_list_columns.length; i++) {
1660                 var column = patron_list_columns[i];
1661                 var t_column = document.createElement('treecol');
1662                 t_columns.appendChild( t_column );
1663                 t_column.setAttribute('label', column.v);
1664                 if (column.s) {
1665                         t_column.setAttribute('id', 'tc_' + column.s + '_' + column.f);
1666                 } else {
1667                         t_column.setAttribute('id', 'tc_' + column.f);
1668                 }
1669                 t_column.setAttribute('flex', '0');
1670                 try {
1671                         if (column.primary) {
1672                                 t_column.setAttribute('primary','true');
1673                                 t_column.setAttribute('flex','1');
1674                         }
1675                         if (column.hidden) t_column.setAttribute('hidden','true');
1676                 } catch(E) {
1677                         mw.sdump('D_LEGACY',js2JSON(E) + '\n');
1678                 }
1679                 t_column.setAttribute('field',column.f);
1680                 if (i != (patron_list_columns.length - 1) ) {
1681                         var t_splitter = document.createElement('splitter');
1682                         t_columns.appendChild( t_splitter );
1683                         t_splitter.setAttribute('class','tree-splitter');
1684                 }
1685         }
1686         var t_children = document.createElement('treechildren');
1687         tree.appendChild(t_children);
1688
1689         for (var i = 0; i < patron_ids.length; i++) {
1690                 var t_item = document.createElement('treeitem');
1691                 t_children.appendChild( t_item );
1692                 t_item.setAttribute('patron_id',patron_ids[i]);
1693
1694                 var t_row = document.createElement('treerow');
1695                 t_item.appendChild( t_row );
1696                 t_row.setAttribute('patron_id',patron_ids[i]);
1697
1698                 //var t_cell = document.createElement('treecell');
1699                 //t_row.appendChild( t_cell );
1700                 //t_cell.setAttribute('label',patron_ids[i]);
1701
1702                 mw.user_async_request(
1703                         'open-ils.actor',
1704                         'open-ils.actor.user.fleshed.retrieve', 
1705                         [ mw.G.auth_ses[0], patron_ids[i] ],
1706                         build_patron_retrieve_for_search_callback( t_row )
1707                 );
1708                 mw.sdump('D_LEGACY','Making call... count = ' + counter_incr('patron_call') + '\n');
1709         }
1710         
1711 }
1712
1713 function build_patron_retrieve_for_search_callback(treerow) {
1714         mw.sdump('D_LEGACY',arg_dump(arguments));
1715         return function (request) {
1716                 mw.sdump('D_LEGACY','Running callback... count = ' + counter_incr('patron_callback') + '\n');
1717                 var result = request.getResultObject();
1718                 mw.sdump('D_LEGACY','Result = ' + js2JSON(result) + '\n');
1719
1720                 for (var i = 0; i < patron_list_columns.length; i++) {
1721                         var column = patron_list_columns[i];
1722                         var t_cell = document.createElement('treecell');
1723                         treerow.appendChild(t_cell);
1724                         var expression;
1725                         if (column.s) {
1726                                 switch(column.s) {
1727                                         case 'home_ou':
1728 expression = 'mw.G.org_tree_hash[ result.home_ou() ].' + column.f + '()';
1729                                         break;
1730                                         case 'mailing_address':
1731 expression = 'find_id_object_in_list( result.addresses() , result.mailing_address() ).' + column.f + '()';
1732                                         break;
1733                                         case 'billing_address':
1734 expression = 'find_id_object_in_list( result.addresses() , result.billing_address() ).' + column.f + '()';
1735                                         break;
1736                                 }
1737                         } else {
1738                                 expression = 'result.' + column.f + '()';
1739                         }
1740                         //mw.sdump('D_LEGACY','Trying to eval: ' + expression + '\n');
1741                         t_cell.setAttribute(
1742                                 'label',
1743                                 eval( expression )
1744                         );
1745                 }
1746         };
1747 }
1748
1749