add_css_class is now addCSSClass
[Evergreen.git] / Evergreen / staff_client / chrome / content / evergreen / stat_cat / stat_cat.js
1 var local_copy_stat_cats = [];
2 var local_copy_stat_cats_hash = {};
3 var local_copy_stat_cat_entries = {};
4 var local_patron_stat_cats = [];
5 var local_patron_stat_cats_hash = {};
6 var local_patron_stat_cat_entries = {};
7 var local_generic_stat_cats = [];
8 var local_generic_stat_cats_hash = {};
9 var local_generic_stat_cat_entries = {};
10 var new_id = -1;
11
12 function copy_stat_cat_editor_init() {
13         sdump('D_LEGACY','entering copy_stat_cat_editor_init for copy_stat_cat.js\n');
14         sdump('D_LEGACY','TESTING: stat_cat.js: ' + mw.G['main_test_variable'] + '\n');
15         populate_local_copy_stat_cats(
16                 'copy_stat_cat_grid',
17                 map_list(mw.G.my_orgs, function (obj) { return obj.id(); })
18         );
19 }
20
21 function patron_stat_cat_editor_init() {
22         sdump('D_LEGACY','entering patron_stat_cat_editor_init for patron_stat_cat.js\n');
23         sdump('D_LEGACY','TESTING: stat_cat.js: ' + mw.G['main_test_variable'] + '\n');
24         populate_local_patron_stat_cats(
25                 'patron_stat_cat_grid',
26                 map_list(mw.G.my_orgs, function (obj) { return obj.id(); })
27         );
28 }
29
30 function new_entry_listener(ev) {
31         if (ev.target.tagName != 'textbox') return;
32         var row = ev.target.parentNode;
33         var rows = row.parentNode;
34         var clone = row.cloneNode(true);
35         if (row.nextSibling) {
36                 rows.insertBefore(clone,row.nextSibling);
37         } else {
38                 rows.appendChild(clone);
39         }
40         clone.getElementsByTagName('textbox')[0].addEventListener('change',new_entry_listener,false);
41         ev.target.removeEventListener('change',new_entry_listener,false);
42
43
44
45 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
46 // COPY
47
48 function populate_local_copy_stat_cats(grid,libs) {
49         sdump('D_LEGACY','populate_local_copy_stat_cats: pertinent libs = ' + js2JSON(libs) + '\n');
50         sdump('D_LEGACY','populate_local_copy_stat_cats: pertinent libs = ' + mw.js2JSON(libs) + '\n');
51         sdump('D_LEGACY','libs.constructor = ' + libs.constructor + '\n');
52
53         try {
54                 local_copy_stat_cats = user_request(
55                         'open-ils.circ',
56                         'open-ils.circ.stat_cat.asset.multirange.union.retrieve',
57                         [ mw.G.auth_ses[0], libs ]
58                 );
59                 //sdump('D_LEGACY','1: local_copy_stat_cats = ' + js2JSON(local_copy_stat_cats) + '\n');
60                 local_copy_stat_cats = local_copy_stat_cats[0];
61                 //sdump('D_LEGACY','2: local_copy_stat_cats = ' + js2JSON(local_copy_stat_cats) + '\n');
62                 local_copy_stat_cats_hash = convert_object_list_to_hash( local_copy_stat_cats );
63         } catch(E) {
64                 mw.handle_error(E);
65         }
66         populate_local_copy_stat_cats_grid(grid);
67 }
68
69 function populate_local_copy_stat_cats_grid(grid) {
70
71         sdump('D_LEGACY','local_copy_stat_cats = ' + pretty_print( js2JSON( local_copy_stat_cats ) ) + '\n');
72
73         if (typeof(grid) != 'object') { grid = document.getElementById(grid); }
74         var rows = grid.getElementsByTagName('rows')[0];
75         empty_widget(rows);
76
77         var row0 = document.createElement('row');
78         rows.appendChild(row0);
79         row0.appendChild( document.createElement('label') );
80         row0.appendChild( document.createElement('label') );
81         var delete_label = document.createElement('label');
82         row0.appendChild( delete_label );
83                 delete_label.setAttribute('value','Delete');
84         var owner_label = document.createElement('label');
85         row0.appendChild( owner_label );
86                 owner_label.setAttribute('value','Owner');
87
88         for (var i in local_copy_stat_cats) {
89
90                 var copy_stat_cat = local_copy_stat_cats[i];
91
92                 var row1 = document.createElement('row'); 
93                 rows.appendChild(row1);
94                 row1.setAttribute('asc_id',copy_stat_cat.id());
95                 row1.setAttribute('object_type','asc');
96                 addCSSClass(row1,'row' + i % 2);
97
98                         var label1 = document.createElement('label'); 
99                         row1.appendChild(label1);
100                         label1.setAttribute('value','Statistical Category:');
101
102                         if ( find_ou( mw.G.user_ou,copy_stat_cat.owner() ) ) {
103                                 var textbox1 = document.createElement('textbox');
104                                 row1.appendChild(textbox1);
105                                 textbox1.value = copy_stat_cat.name();
106                                 textbox1.setAttribute('original',textbox1.value);
107         
108                                 var checkbox1 = document.createElement('checkbox');
109                                 row1.appendChild(checkbox1);
110                                 checkbox1.setAttribute('delete','true');
111
112                                 var menulist1 = document.createElement('menulist');
113                                 row1.appendChild(menulist1);
114                                 menulist1.setAttribute('original',copy_stat_cat.owner());
115         
116                                         var menupopup1 = document.createElement('menupopup');
117                                         menulist1.appendChild(menupopup1);
118                                         //sdump('D_LEGACY','About to populate with copy_stat_cat.owner() = ' + copy_stat_cat.owner() + '\n');
119                                         populate_lib_list_with_branch(menulist1,menupopup1,copy_stat_cat.owner(),mw.G.user_ou);
120                         } else {
121
122                                 var label1a = document.createElement('label');
123                                 row1.appendChild(label1a);
124                                 label1a.setAttribute('value',copy_stat_cat.name() );
125
126                                 row1.appendChild( document.createElement('label') );
127
128                                 var label1c = document.createElement('label');
129                                 row1.appendChild(label1c);
130                                 label1c.setAttribute( 'value',mw.G.org_tree_hash[copy_stat_cat.owner()].name() );
131                         }
132
133                 var row2 = document.createElement('row'); 
134                 rows.appendChild(row2);
135                 row2.setAttribute('asc_id',copy_stat_cat.id());
136                 row2.setAttribute('object_type','asc');
137                 addCSSClass(row2,'row' + i % 2);
138
139                         var label2 = document.createElement('label');
140                         row2.appendChild(label2);
141                         label2.setAttribute('value','OPAC Visible');
142
143                         if ( find_ou( mw.G.user_ou,copy_stat_cat.owner() ) ) {
144                                 var checkbox2 = document.createElement('checkbox');
145                                 row2.appendChild(checkbox2);
146                                 checkbox2.checked = (copy_stat_cat.opac_visible() == '1')
147                                 checkbox2.setAttribute('original',checkbox2.checked);
148                         } else {
149                                 var label2a = document.createElement('label');
150                                 row2.appendChild(label2a);
151                                 label2a.setAttribute('value',yesno( copy_stat_cat.opac_visible() ));
152                         }
153
154                 for (var j in copy_stat_cat.entries() ) {
155
156                         var stat_entry = copy_stat_cat.entries()[j];
157                         local_copy_stat_cat_entries[stat_entry.id()] = stat_entry;
158
159                         var row = document.createElement('row');
160                         rows.appendChild(row);
161                         row.setAttribute('asc_id',copy_stat_cat.id());
162                         row.setAttribute('asce_id',stat_entry.id());
163                         row.setAttribute('object_type','asce');
164                         addCSSClass(row,'row' + i % 2);
165
166                                 var label = document.createElement('label');
167                                 row.appendChild(label);
168                                 label.setAttribute('value','Entry:');
169
170                                 if ( find_ou( mw.G.user_ou,stat_entry.owner() ) ) {
171                                         var textbox = document.createElement('textbox');
172                                         row.appendChild(textbox);
173                                         textbox.value = stat_entry.value();
174                                         textbox.setAttribute('original',textbox.value);
175
176                                         var checkbox = document.createElement('checkbox');
177                                         row.appendChild(checkbox);
178                                         checkbox.setAttribute('delete','true');
179
180                                         var menulist = document.createElement('menulist');
181                                         row.appendChild(menulist);
182                                         menulist.setAttribute('original',stat_entry.owner());
183
184                                                 var menupopup = document.createElement('menupopup');
185                                                 menulist.appendChild(menupopup);
186                                                 //sdump('D_LEGACY','About to populate with stat_entry.owner() = ' + stat_entry.owner() + '\n');
187                                                 populate_lib_list_with_branch(menulist,menupopup,stat_entry.owner(),mw.G.user_ou);
188                                 } else {
189
190                                         var labela = document.createElement('label');
191                                         row.appendChild(labela);
192                                         labela.setAttribute('value',stat_entry.value());
193
194                                         row.appendChild( document.createElement('label') );
195
196                                         var labelc = document.createElement('label');
197                                         row.appendChild(labelc);
198                                         labelc.setAttribute( 'value',mw.G.org_tree_hash[stat_entry.owner()].name() );
199                                 }
200                 }
201
202                 var row3 = document.createElement('row');
203                 rows.appendChild(row3);
204                 row3.setAttribute('asc_id',copy_stat_cat.id());
205                 row3.setAttribute('object_type','asce');
206                 row3.setAttribute('new','true');
207                 addCSSClass(row3,'row' + i % 2);
208
209                         var label3 = document.createElement('label');
210                         row3.appendChild(label3);
211                         label3.setAttribute('value','New Entry:');
212
213                         var textbox3 = document.createElement('textbox');
214                         row3.appendChild(textbox3);
215                         textbox3.setAttribute('original','');
216                         textbox3.addEventListener(
217                                 'change',
218                                 new_entry_listener,
219                                 false
220                         );
221
222                         var checkbox3 = document.createElement('checkbox');
223                         row3.appendChild(checkbox3);
224                         checkbox3.setAttribute('delete','true');
225
226                         var menulist3 = document.createElement('menulist');
227                         row3.appendChild(menulist3);
228                         menulist3.setAttribute('original',mw.G.user_ou.id());
229
230                                 var menupopup3 = document.createElement('menupopup');
231                                 menulist3.appendChild(menupopup3);
232                                 //sdump('D_LEGACY','About to populate with mw.G.user_ou\n');
233                                 populate_lib_list_with_branch(menulist3,menupopup3,mw.G.user_ou,mw.G.user_ou);
234
235                 var row4 = document.createElement('row');
236                 rows.appendChild(row4);
237                 addCSSClass(row4,'row' + i % 2);
238
239                         var label4 = document.createElement('label');
240                         row4.appendChild(label4);
241                         label4.setAttribute('value',' ');
242                         
243                 var row5 = document.createElement('row');
244                 rows.appendChild(row5);
245
246                         var label5 = document.createElement('label');
247                         row5.appendChild(label5);
248                         label5.setAttribute('value',' ');
249
250         }
251
252         var row6 = document.createElement('row');
253         rows.appendChild(row6);
254
255                 var label6 = document.createElement('label');
256                 row6.appendChild(label6);
257                 label6.setAttribute('value',' ');
258         
259         sdump('D_LEGACY','local_copy_stat_cat_entries = ' + pretty_print( js2JSON( local_copy_stat_cat_entries ) ) + '\n');
260 }
261 function save_copy_changes() {
262
263         // XUL
264
265         var nl = document.getElementsByTagName('textbox');
266         for (var i = 0; i < nl.length; i++) {
267                 var t = nl[i];
268                 var row = t.parentNode;
269                 var object_type = row.getAttribute('object_type');
270                 var asc_id = row.getAttribute('asc_id');
271                 var asce_id = row.getAttribute('asce_id');
272                 var new_flag = row.getAttribute('new');
273                 var original = t.getAttribute('original');
274                 sdump('D_LEGACY','Considering textbox: object_type = ' + object_type + ' asc_id = ' + asc_id + ' asce_id = ' + asce_id + ' original = ' + original + ' value = ' + t.value + '\n');
275
276                 if ( (original != t.value) && (t.value != null) && (t.value != undefined) ) {
277                         sdump('D_LEGACY',"\tWe're in...\n");
278                         switch(object_type) {
279                                 case 'asc': 
280                                         local_copy_stat_cats_hash[asc_id].ischanged('1');
281                                         local_copy_stat_cats_hash[asc_id].name( t.value );
282                                 break;
283                                 case 'asce': 
284                                         if (!asce_id) { 
285                                                 asce_id = new_id--;
286                                                 row.setAttribute('asce_id', asce_id);
287                                                 local_copy_stat_cats_hash[ asc_id ].entries().push( new asce() );
288                                                 local_copy_stat_cat_entries[asce_id] = 
289                                                         local_copy_stat_cats_hash[ asc_id].entries()[ local_copy_stat_cats_hash[ asc_id].entries().length -1 ];
290                                                 local_copy_stat_cat_entries[asce_id].id( asce_id );
291                                                 local_copy_stat_cat_entries[asce_id].isnew('1');
292                                                 local_copy_stat_cat_entries[asce_id].stat_cat( asc_id );
293                                                 local_copy_stat_cat_entries[asce_id].owner(
294                                                         mw.G.user_ou.id()
295                                                 );
296                                                 local_copy_stat_cats_hash = convert_object_list_to_hash( local_copy_stat_cats );
297                                         }
298                                         local_copy_stat_cat_entries[asce_id].ischanged('1');
299                                         local_copy_stat_cat_entries[asce_id].value( t.value );
300                                 break;
301                         }
302                 }
303         }
304         var nl = document.getElementsByTagName('menulist');
305         for (var i = 0; i < nl.length; i++) {
306                 var m = nl[i];
307                 var row = m.parentNode;
308                 var object_type = row.getAttribute('object_type');
309                 var asc_id = row.getAttribute('asc_id');
310                 var asce_id = row.getAttribute('asce_id');
311                 var new_flag = row.getAttribute('new');
312                 var original = m.getAttribute('original');
313
314                 sdump('D_LEGACY','Considering menulist: object_type = ' + object_type + ' asc_id = ' + asc_id + ' asce_id = ' + asce_id + ' original = ' + original + ' value = ' + m.value + '\n');
315
316                 if ( (original != m.value) && (m.value != null) && (m.value != undefined) ) {
317                         sdump('D_LEGACY',"\tWe're in...\n");
318                         switch(object_type) {
319                                 case 'asc': 
320                                         local_copy_stat_cats_hash[asc_id].ischanged('1');
321                                         local_copy_stat_cats_hash[asc_id].owner( m.value );
322                                 break;
323                                 case 'asce': 
324                                         if (asce_id) { 
325                                                 local_copy_stat_cat_entries[asce_id].ischanged('1');
326                                                 local_copy_stat_cat_entries[asce_id].owner( m.value );
327                                         } else {
328                                                 sdump('D_LEGACY','\tbut nothing to do.\n');
329                                         }
330                                 break;
331                         }
332                 }
333         }
334
335         var nl = document.getElementsByTagName('checkbox');
336         for (var i = 0; i < nl.length; i++) {
337                 var c = nl[i];
338                 var row = c.parentNode;
339                 var object_type = row.getAttribute('object_type');
340                 var asc_id = row.getAttribute('asc_id');
341                 var asce_id = row.getAttribute('asce_id');
342                 var new_flag = row.getAttribute('new');
343                 var delete_flag = c.getAttribute('delete');
344                 if (delete_flag == 'true') { delete_flag = true; }
345                 else if (delete_flag == 'false') { delete_flag = false; }
346                 var original = c.getAttribute('original');
347                 if (original == 'true') { original = true; }
348                 else if (original == 'false' ) { original = false; }
349
350                 sdump('D_LEGACY','Considering checkbox: object_type = ' + object_type + ' asc_id = ' + asc_id + ' asce_id = ' + asce_id + ' original = ' + original + ' checked = ' + m.checked + ' delete_flag = ' + delete_flag + '\n');
351
352                 if ( (original != c.checked) && (c.checked != null) && (c.checked != undefined) ) {
353                         sdump('D_LEGACY',"\tWe're in...\n");
354                         switch(object_type) {
355                                 case 'asc': 
356                                         local_copy_stat_cats_hash[asc_id].ischanged('1');
357                                         if (delete_flag) {
358                                                 local_copy_stat_cats_hash[asc_id].isdeleted('1');
359                                         } else {
360                                                 local_copy_stat_cats_hash[asc_id].opac_visible( c.checked );
361                                         }
362                                 break;
363                                 case 'asce': 
364                                         if (asce_id) {
365                                                 local_copy_stat_cat_entries[asce_id].ischanged('1');
366                                                 if (delete_flag) {
367                                                         local_copy_stat_cat_entries[asce_id].isdeleted('1');
368                                                 } else {
369                                                         // This actually doesn't exist on the asce object
370                                                         //local_copy_stat_cat_entries[asce_id].opac_visible( c.checked );
371                                                 }
372                                         } else {
373                                                 sdump('D_LEGACY',"\tBut nothing to do.\n");
374                                         }
375                                 break;
376                         }
377                 }
378         }
379
380         sdump('D_LEGACY','local_copy_stat_cats = ' + js2JSON(local_copy_stat_cats) + '\n');
381
382         // fieldmapper
383         sdump('D_LEGACY','Creating, Updating, Deleting, Oh My...\n');
384         for (var i = 0; i < local_copy_stat_cats.length; i++) {
385                 var copy_stat_cat = local_copy_stat_cats[i];
386                 sdump('D_LEGACY','Considering copy_stat_cat = ' + js2JSON(copy_stat_cat) + '\n');
387
388                 if ( (copy_stat_cat.name() == undefined) || (copy_stat_cat.name() == null) 
389                         || (copy_stat_cat.name() == '') ) continue; 
390                 if ( copy_stat_cat.isnew() == '1' ) {
391                 // This will handle copy_stat_cat and and it's entries
392                         try {
393                                 if ( copy_stat_cat.isdeleted() != '1') {
394                                         var r1 = mw.user_request(
395                                                 'open-ils.circ',
396                                                 'open-ils.circ.stat_cat.asset.create',
397                                                 [ mw.G.auth_ses[0], copy_stat_cat ]
398                                         )[0];
399                                         sdump('D_LEGACY','r1 = ' + js2JSON(r1) + '\n');
400                                 }
401                         } catch(E) {
402                                 mw.handle_error(E);
403                         }
404
405                 } else {
406                 // We will also need to loop through the entries
407
408                         if ( (copy_stat_cat.ischanged() == '1') || (copy_stat_cat.isdeleted() == '1') ) {
409                                 try {
410                                         if (copy_stat_cat.isdeleted() == '1') {
411                                                 var r2 = mw.user_request(
412                                                         'open-ils.circ',
413                                                         'open-ils.circ.stat_cat.asset.delete',
414                                                         [ mw.G.auth_ses[0], copy_stat_cat ]
415                                                 )[0];
416                                                 sdump('D_LEGACY','r2 = ' + js2JSON(r2) + '\n');
417                                         } else {
418                                                 var r2 = mw.user_request(
419                                                         'open-ils.circ',
420                                                         'open-ils.circ.stat_cat.asset.update',
421                                                         [ mw.G.auth_ses[0], copy_stat_cat ]
422                                                 )[0];
423                                                 sdump('D_LEGACY','r2 = ' + js2JSON(r2) + '\n');
424                                         }
425                                 } catch(E) {
426                                         mw.handle_error(E);
427                                 }
428                         }
429                         if (copy_stat_cat.isdeleted() != '1')
430                         for (var j = 0; j < copy_stat_cat.entries().length; j++) {
431                                 var stat_entry = copy_stat_cat.entries()[j];
432                                 sdump('D_LEGACY','\tConsidering stat_entry = ' + js2JSON(stat_entry) + '\n');
433
434                                 if (stat_entry.isnew() == '1') {
435                                         if (stat_entry.isdeleted() != '1') {                    
436                                                 var r3 = mw.user_request(
437                                                         'open-ils.circ',
438                                                         'open-ils.circ.stat_cat.asset.entry.create',
439                                                         [ mw.G.auth_ses[0], stat_entry ]
440                                                 );
441                                                 sdump('D_LEGACY','r3 = ' + js2JSON(r3) + '\n');
442                                         }
443                                 } else if (stat_entry.isdeleted() == '1') {
444
445                                         var r3 = mw.user_request(
446                                                 'open-ils.circ',
447                                                 'open-ils.circ.stat_cat.asset.entry.delete',
448                                                 [ mw.G.auth_ses[0], stat_entry ]
449                                         );
450                                         sdump('D_LEGACY','r3 = ' + js2JSON(r3) + '\n');
451
452                                 } else {
453
454                                         var r3 = mw.user_request(
455                                                 'open-ils.circ',
456                                                 'open-ils.circ.stat_cat.asset.entry.update',
457                                                 [ mw.G.auth_ses[0], stat_entry ]
458                                         );
459                                         sdump('D_LEGACY','r3 = ' + js2JSON(r3) + '\n');
460
461                                 }
462
463                         }
464
465                 }
466         }
467
468         sdump('D_LEGACY','\n\n\n=-=-=-=-=-=-=-=-=\n\n\n');
469
470         local_copy_stat_cat_entries = {};
471         local_copy_stat_cats = [];
472         local_copy_stat_cats_hash = {};
473         sdump('D_LEGACY','Successfully updated the Stat Cats\n');
474         alert('Successfully updated the Stat Cats\n');
475         populate_local_copy_stat_cats(
476                 'copy_stat_cat_grid',
477                 map_list(mw.G.my_orgs, function (obj) { return obj.id(); })
478         );
479         document.getElementById('copy_stat_cat_new').disabled = false;
480 }
481
482 function new_copy_stat_cat(ev) {
483         ev.target.disabled = true;
484         var n_asc = new asc();
485         n_asc.isnew( '1' );
486         n_asc.id( new_id-- );
487         n_asc.owner( mw.G.user_ou.id() );
488         n_asc.entries( [] );
489         local_copy_stat_cats.push( n_asc );
490         local_copy_stat_cats_hash = convert_object_list_to_hash( local_copy_stat_cats );
491         populate_local_copy_stat_cats_grid('copy_stat_cat_grid');
492 }
493
494 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
495 // PATRON
496
497 function populate_local_patron_stat_cats(grid,libs) {
498         sdump('D_LEGACY','populate_local_patron_stat_cats: pertinent libs = ' + js2JSON(libs) + '\n');
499
500         try {
501                 local_patron_stat_cats = mw.user_request(
502                         'open-ils.circ',
503                         'open-ils.circ.stat_cat.actor.retrieve.all',
504                         [ mw.G.auth_ses[0] ]
505                 );
506                 //sdump('D_LEGACY','1: local_patron_stat_cats = ' + js2JSON(local_patron_stat_cats) + '\n');
507                 local_patron_stat_cats = local_patron_stat_cats[0];
508                 //sdump('D_LEGACY','2: local_patron_stat_cats = ' + js2JSON(local_patron_stat_cats) + '\n');
509                 local_patron_stat_cats_hash = convert_object_list_to_hash( local_patron_stat_cats );
510         } catch(E) {
511                 mw.handle_error(E);
512         }
513         populate_local_patron_stat_cats_grid(grid);
514 }
515
516 function populate_local_patron_stat_cats_grid(grid) {
517
518         sdump('D_LEGACY','local_patron_stat_cats = ' + pretty_print( js2JSON( local_patron_stat_cats ) ) + '\n');
519
520         if (typeof(grid) != 'object') { grid = document.getElementById(grid); }
521         var rows = grid.getElementsByTagName('rows')[0];
522         empty_widget(rows);
523
524         var row0 = document.createElement('row');
525         rows.appendChild(row0);
526         row0.appendChild( document.createElement('label') );
527         row0.appendChild( document.createElement('label') );
528         var delete_label = document.createElement('label');
529         row0.appendChild( delete_label );
530                 delete_label.setAttribute('value','Delete');
531         var owner_label = document.createElement('label');
532         row0.appendChild( owner_label );
533                 owner_label.setAttribute('value','Owner');
534
535         for (var i in local_patron_stat_cats) {
536                 //sdump('D_LEGACY','local_patron_stat_cats['+i+'] = '+local_patron_stat_cats[i]+' ; typeof = '+typeof(local_patron_stat_cats[i]) + '\n');
537
538                 var patron_stat_cat = local_patron_stat_cats[i];
539
540                 var row1 = document.createElement('row'); 
541                 rows.appendChild(row1);
542                 row1.setAttribute('actsc_id',patron_stat_cat.id());
543                 row1.setAttribute('object_type','actsc');
544                 addCSSClass(row1,'row' + i % 2);
545
546                         var label1 = document.createElement('label'); 
547                         row1.appendChild(label1);
548                         label1.setAttribute('value','Statistical Category:');
549
550                         if ( find_ou( mw.G.user_ou,patron_stat_cat.owner() ) ) {
551                                 var textbox1 = document.createElement('textbox');
552                                 row1.appendChild(textbox1);
553                                 textbox1.value = patron_stat_cat.name();
554                                 textbox1.setAttribute('original',textbox1.value);
555         
556                                 var checkbox1 = document.createElement('checkbox');
557                                 row1.appendChild(checkbox1);
558                                 checkbox1.setAttribute('delete','true');
559
560                                 var menulist1 = document.createElement('menulist');
561                                 row1.appendChild(menulist1);
562                                 menulist1.setAttribute('original',patron_stat_cat.owner());
563         
564                                         var menupopup1 = document.createElement('menupopup');
565                                         menulist1.appendChild(menupopup1);
566                                         //sdump('D_LEGACY','About to populate with patron_stat_cat.owner() = ' + patron_stat_cat.owner() + '\n');
567                                         populate_lib_list_with_branch(menulist1,menupopup1,patron_stat_cat.owner(),mw.G.user_ou);
568                         } else {
569
570                                 var label1a = document.createElement('label');
571                                 row1.appendChild(label1a);
572                                 label1a.setAttribute('value',patron_stat_cat.name() );
573
574                                 row1.appendChild( document.createElement('label') );
575
576                                 var label1c = document.createElement('label');
577                                 row1.appendChild(label1c);
578                                 label1c.setAttribute( 'value',mw.G.org_tree_hash[patron_stat_cat.owner()].name() );
579                         }
580
581                 var row2 = document.createElement('row'); 
582                 rows.appendChild(row2);
583                 row2.setAttribute('actsc_id',patron_stat_cat.id());
584                 row2.setAttribute('object_type','actsc');
585                 addCSSClass(row2,'row' + i % 2);
586
587                         var label2 = document.createElement('label');
588                         row2.appendChild(label2);
589                         label2.setAttribute('value','OPAC Visible');
590
591                         if ( find_ou( mw.G.user_ou,patron_stat_cat.owner() ) ) {
592                                 var checkbox2 = document.createElement('checkbox');
593                                 row2.appendChild(checkbox2);
594                                 checkbox2.checked = (patron_stat_cat.opac_visible() == '1')
595                                 checkbox2.setAttribute('original',checkbox2.checked);
596                         } else {
597                                 var label2a = document.createElement('label');
598                                 row2.appendChild(label2a);
599                                 label2a.setAttribute('value',yesno( patron_stat_cat.opac_visible() ));
600                         }
601
602                 for (var j in patron_stat_cat.entries() ) {
603
604                         var stat_entry = patron_stat_cat.entries()[j];
605                         local_patron_stat_cat_entries[stat_entry.id()] = stat_entry;
606
607                         var row = document.createElement('row');
608                         rows.appendChild(row);
609                         row.setAttribute('actsc_id',patron_stat_cat.id());
610                         row.setAttribute('actsce_id',stat_entry.id());
611                         row.setAttribute('object_type','actsce');
612                         addCSSClass(row,'row' + i % 2);
613
614                                 var label = document.createElement('label');
615                                 row.appendChild(label);
616                                 label.setAttribute('value','Entry:');
617
618                                 if ( find_ou( mw.G.user_ou,stat_entry.owner() ) ) {
619                                         var textbox = document.createElement('textbox');
620                                         row.appendChild(textbox);
621                                         textbox.value = stat_entry.value();
622                                         textbox.setAttribute('original',textbox.value);
623
624                                         var checkbox = document.createElement('checkbox');
625                                         row.appendChild(checkbox);
626                                         checkbox.setAttribute('delete','true');
627
628                                         var menulist = document.createElement('menulist');
629                                         row.appendChild(menulist);
630                                         menulist.setAttribute('original',stat_entry.owner());
631
632                                                 var menupopup = document.createElement('menupopup');
633                                                 menulist.appendChild(menupopup);
634                                                 //sdump('D_LEGACY','About to populate with stat_entry.owner() = ' + stat_entry.owner() + '\n');
635                                                 populate_lib_list_with_branch(menulist,menupopup,stat_entry.owner(),mw.G.user_ou);
636                                 } else {
637
638                                         var labela = document.createElement('label');
639                                         row.appendChild(labela);
640                                         labela.setAttribute('value',stat_entry.value());
641
642                                         row.appendChild( document.createElement('label') );
643
644                                         var labelc = document.createElement('label');
645                                         row.appendChild(labelc);
646                                         labelc.setAttribute( 'value',mw.G.org_tree_hash[stat_entry.owner()].name() );
647                                 }
648                 }
649
650                 var row3 = document.createElement('row');
651                 rows.appendChild(row3);
652                 row3.setAttribute('actsc_id',patron_stat_cat.id());
653                 row3.setAttribute('object_type','actsce');
654                 row3.setAttribute('new','true');
655                 addCSSClass(row3,'row' + i % 2);
656
657                         var label3 = document.createElement('label');
658                         row3.appendChild(label3);
659                         label3.setAttribute('value','New Entry:');
660
661                         var textbox3 = document.createElement('textbox');
662                         row3.appendChild(textbox3);
663                         textbox3.setAttribute('original','');
664                         textbox3.addEventListener(
665                                 'change',
666                                 new_entry_listener,
667                                 false
668                         );
669
670                         var checkbox3 = document.createElement('checkbox');
671                         row3.appendChild(checkbox3);
672                         checkbox3.setAttribute('delete','true');
673
674                         var menulist3 = document.createElement('menulist');
675                         row3.appendChild(menulist3);
676                         menulist3.setAttribute('original',mw.G.user_ou.id());
677
678                                 var menupopup3 = document.createElement('menupopup');
679                                 menulist3.appendChild(menupopup3);
680                                 //sdump('D_LEGACY','About to populate with mw.G.user_ou\n');
681                                 populate_lib_list_with_branch(menulist3,menupopup3,mw.G.user_ou,mw.G.user_ou);
682
683                 var row4 = document.createElement('row');
684                 rows.appendChild(row4);
685                 addCSSClass(row4,'row' + i % 2);
686
687                         var label4 = document.createElement('label');
688                         row4.appendChild(label4);
689                         label4.setAttribute('value',' ');
690                         
691                 var row5 = document.createElement('row');
692                 rows.appendChild(row5);
693
694                         var label5 = document.createElement('label');
695                         row5.appendChild(label5);
696                         label5.setAttribute('value',' ');
697
698         }
699
700         var row6 = document.createElement('row');
701         rows.appendChild(row6);
702
703                 var label6 = document.createElement('label');
704                 row6.appendChild(label6);
705                 label6.setAttribute('value',' ');
706         
707         sdump('D_LEGACY','local_patron_stat_cat_entries = ' + pretty_print( js2JSON( local_patron_stat_cat_entries ) ) + '\n');
708 }
709
710 function save_patron_changes() {
711
712         // XUL
713
714         var nl = document.getElementsByTagName('textbox');
715         for (var i = 0; i < nl.length; i++) {
716                 var t = nl[i];
717                 var row = t.parentNode;
718                 var object_type = row.getAttribute('object_type');
719                 var actsc_id = row.getAttribute('actsc_id');
720                 var actsce_id = row.getAttribute('actsce_id');
721                 var new_flag = row.getAttribute('new');
722                 var original = t.getAttribute('original');
723                 sdump('D_LEGACY','Considering textbox: object_type = ' + object_type + ' actsc_id = ' + actsc_id + ' actsce_id = ' + actsce_id + ' original = ' + original + ' value = ' + t.value + '\n');
724
725                 if ( (original != t.value) && (t.value != null) && (t.value != undefined) ) {
726                         sdump('D_LEGACY',"\tWe're in...\n");
727                         switch(object_type) {
728                                 case 'actsc': 
729                                         local_patron_stat_cats_hash[actsc_id].ischanged('1');
730                                         local_patron_stat_cats_hash[actsc_id].name( t.value );
731                                 break;
732                                 case 'actsce': 
733                                         if (!actsce_id) { 
734                                                 actsce_id = new_id--;
735                                                 row.setAttribute('actsce_id', actsce_id);
736                                                 local_patron_stat_cats_hash[ actsc_id ].entries().push( new actsce() );
737                                                 local_patron_stat_cat_entries[actsce_id] = 
738                                                         local_patron_stat_cats_hash[ actsc_id].entries()[ local_patron_stat_cats_hash[ actsc_id].entries().length -1 ];
739                                                 local_patron_stat_cat_entries[actsce_id].id( actsce_id );
740                                                 local_patron_stat_cat_entries[actsce_id].isnew('1');
741                                                 local_patron_stat_cat_entries[actsce_id].stat_cat( actsc_id );
742                                                 local_patron_stat_cat_entries[actsce_id].owner(
743                                                         mw.G.user_ou.id()
744                                                 );
745                                                 local_patron_stat_cats_hash = convert_object_list_to_hash( local_patron_stat_cats );
746                                         }
747                                         local_patron_stat_cat_entries[actsce_id].ischanged('1');
748                                         local_patron_stat_cat_entries[actsce_id].value( t.value );
749                                 break;
750                         }
751                 }
752         }
753         var nl = document.getElementsByTagName('menulist');
754         for (var i = 0; i < nl.length; i++) {
755                 var m = nl[i];
756                 var row = m.parentNode;
757                 var object_type = row.getAttribute('object_type');
758                 var actsc_id = row.getAttribute('actsc_id');
759                 var actsce_id = row.getAttribute('actsce_id');
760                 var new_flag = row.getAttribute('new');
761                 var original = m.getAttribute('original');
762
763                 sdump('D_LEGACY','Considering menulist: object_type = ' + object_type + ' actsc_id = ' + actsc_id + ' actsce_id = ' + actsce_id + ' original = ' + original + ' value = ' + m.value + '\n');
764
765                 if ( (original != m.value) && (m.value != null) && (m.value != undefined) ) {
766                         sdump('D_LEGACY',"\tWe're in...\n");
767                         switch(object_type) {
768                                 case 'actsc': 
769                                         local_patron_stat_cats_hash[actsc_id].ischanged('1');
770                                         local_patron_stat_cats_hash[actsc_id].owner( m.value );
771                                 break;
772                                 case 'actsce': 
773                                         if (actsce_id) { 
774                                                 local_patron_stat_cat_entries[actsce_id].ischanged('1');
775                                                 local_patron_stat_cat_entries[actsce_id].owner( m.value );
776                                         } else {
777                                                 sdump('D_LEGACY','\tbut nothing to do.\n');
778                                         }
779                                 break;
780                         }
781                 }
782         }
783
784         var nl = document.getElementsByTagName('checkbox');
785         for (var i = 0; i < nl.length; i++) {
786                 var c = nl[i];
787                 var row = c.parentNode;
788                 var object_type = row.getAttribute('object_type');
789                 var actsc_id = row.getAttribute('actsc_id');
790                 var actsce_id = row.getAttribute('actsce_id');
791                 var new_flag = row.getAttribute('new');
792                 var delete_flag = c.getAttribute('delete');
793                 if (delete_flag == 'true') { delete_flag = true; }
794                 else if (delete_flag == 'false') { delete_flag = false; }
795                 var original = c.getAttribute('original');
796                 if (original == 'true') { original = true; }
797                 else if (original == 'false' ) { original = false; }
798
799                 sdump('D_LEGACY','Considering checkbox: object_type = ' + object_type + ' actsc_id = ' + actsc_id + ' actsce_id = ' + actsce_id + ' original = ' + original + ' checked = ' + m.checked + ' delete_flag = ' + delete_flag + '\n');
800
801                 if ( (original != c.checked) && (c.checked != null) && (c.checked != undefined) ) {
802                         sdump('D_LEGACY',"\tWe're in...\n");
803                         switch(object_type) {
804                                 case 'actsc': 
805                                         local_patron_stat_cats_hash[actsc_id].ischanged('1');
806                                         if (delete_flag) {
807                                                 local_patron_stat_cats_hash[actsc_id].isdeleted('1');
808                                         } else {
809                                                 local_patron_stat_cats_hash[actsc_id].opac_visible( c.checked );
810                                         }
811                                 break;
812                                 case 'actsce': 
813                                         if (actsce_id) {
814                                                 local_patron_stat_cat_entries[actsce_id].ischanged('1');
815                                                 if (delete_flag) {
816                                                         local_patron_stat_cat_entries[actsce_id].isdeleted('1');
817                                                 } else {
818                                                         // This actually doesn't exist on the actsce object
819                                                         //local_patron_stat_cat_entries[actsce_id].opac_visible( c.checked );
820                                                 }
821                                         } else {
822                                                 sdump('D_LEGACY',"\tBut nothing to do.\n");
823                                         }
824                                 break;
825                         }
826                 }
827         }
828
829         sdump('D_LEGACY','local_patron_stat_cats = ' + js2JSON(local_patron_stat_cats) + '\n');
830
831         // fieldmapper
832         sdump('D_LEGACY','Creating, Updating, Deleting, Oh My...\n');
833         for (var i = 0; i < local_patron_stat_cats.length; i++) {
834                 var patron_stat_cat = local_patron_stat_cats[i];
835                 sdump('D_LEGACY','Considering patron_stat_cat = ' + js2JSON(patron_stat_cat) + '\n');
836
837                 if ( (patron_stat_cat.name() == undefined) || (patron_stat_cat.name() == null) 
838                         || (patron_stat_cat.name() == '') ) continue; 
839                 if ( patron_stat_cat.isnew() == '1' ) {
840                 // This will handle patron_stat_cat and and it's entries
841                         try {
842                                 if ( patron_stat_cat.isdeleted() != '1' ) {
843                                         var r1 = mw.user_request(
844                                                 'open-ils.circ',
845                                                 'open-ils.circ.stat_cat.actor.create',
846                                                 [ mw.G.auth_ses[0], patron_stat_cat ]
847                                         )[0];
848                                         sdump('D_LEGACY','r1 = ' + js2JSON(r1) + '\n');
849                                 }
850                         } catch(E) {
851                                 mw.handle_error(E);
852                         }
853
854                 } else {
855                 // We will also need to loop through the entries
856
857                         if ( (patron_stat_cat.ischanged() == '1') || (patron_stat_cat.isdeleted() == '1') ) {
858                                 try {
859                                         if (patron_stat_cat.isdeleted() == '1') {
860                                                 var r2 = mw.user_request(
861                                                         'open-ils.circ',
862                                                         'open-ils.circ.stat_cat.actor.delete',
863                                                         [ mw.G.auth_ses[0], patron_stat_cat ]
864                                                 )[0];
865                                                 sdump('D_LEGACY','r2 = ' + js2JSON(r2) + '\n');
866                                         } else {
867                                                 var r2 = mw.user_request(
868                                                         'open-ils.circ',
869                                                         'open-ils.circ.stat_cat.actor.update',
870                                                         [ mw.G.auth_ses[0], patron_stat_cat ]
871                                                 )[0];
872                                                 sdump('D_LEGACY','r2 = ' + js2JSON(r2) + '\n');
873
874                                         }
875                                 } catch(E) {
876                                         mw.handle_error(E);
877                                 }
878                         }
879                         if (patron_stat_cat.isdeleted() != '1')
880                         for (var j = 0; j < patron_stat_cat.entries().length; j++) {
881                                 var stat_entry = patron_stat_cat.entries()[j];
882                                 sdump('D_LEGACY','\tConsidering stat_entry = ' + js2JSON(stat_entry) + '\n');
883
884                                 if (stat_entry.isnew() == '1') {
885                                         if (stat_entry.isdeleted() != '1') {                    
886                                                 var r3 = mw.user_request(
887                                                         'open-ils.circ',
888                                                         'open-ils.circ.stat_cat.actor.entry.create',
889                                                         [ mw.G.auth_ses[0], stat_entry ]
890                                                 );
891                                                 sdump('D_LEGACY','r3 = ' + js2JSON(r3) + '\n');
892                                         }
893                                 } else if (stat_entry.isdeleted() == '1') {
894
895                                         var r3 = mw.user_request(
896                                                 'open-ils.circ',
897                                                 'open-ils.circ.stat_cat.actor.entry.delete',
898                                                 [ mw.G.auth_ses[0], stat_entry ]
899                                         );
900                                         sdump('D_LEGACY','r3 = ' + js2JSON(r3) + '\n');
901                         
902                                 } else {
903
904                                         var r3 = mw.user_request(
905                                                 'open-ils.circ',
906                                                 'open-ils.circ.stat_cat.actor.entry.update',
907                                                 [ mw.G.auth_ses[0], stat_entry ]
908                                         );
909                                         sdump('D_LEGACY','r3 = ' + js2JSON(r3) + '\n');
910
911                                 }
912
913                         }
914
915                 }
916         }
917
918         sdump('D_LEGACY','\n\n\n=-=-=-=-=-=-=-=-=\n\n\n');
919
920         local_patron_stat_cat_entries = {};
921         local_patron_stat_cats = [];
922         local_patron_stat_cats_hash = {};
923         sdump('D_LEGACY','Successfully updated the Stat Cats\n');
924         alert('Successfully updated the Stat Cats\n');
925         populate_local_patron_stat_cats(
926                 'patron_stat_cat_grid',
927                 map_list(mw.G.my_orgs, function (obj) { return obj.id(); })
928         );
929         document.getElementById('patron_stat_cat_new').disabled = false;
930 }
931
932 function new_patron_stat_cat(ev) {
933         ev.target.disabled = true;
934         var n_actsc = new actsc();
935         n_actsc.isnew( '1' );
936         n_actsc.id( new_id-- );
937         n_actsc.owner( mw.G.user_ou.id() );
938         n_actsc.entries( [] );
939         local_patron_stat_cats.push( n_actsc );
940         local_patron_stat_cats_hash = convert_object_list_to_hash( local_patron_stat_cats );
941         populate_local_patron_stat_cats_grid('patron_stat_cat_grid');
942 }
943
944 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
945 // GENERIC