]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/actor/user/register.js
plugged in details on replaced addr for approval of changes process
[working/Evergreen.git] / Open-ILS / web / js / ui / default / actor / user / register.js
1 dojo.require('dojo.data.ItemFileReadStore');
2 dojo.require('dijit.form.Textarea');
3 dojo.require('dijit.form.FilteringSelect');
4 dojo.require('dijit.form.ComboBox');
5 dojo.require('dijit.form.NumberSpinner');
6 dojo.require('fieldmapper.IDL');
7 dojo.require('openils.PermaCrud');
8 dojo.require('openils.widget.AutoGrid');
9 dojo.require('openils.widget.AutoFieldWidget');
10 dojo.require('dijit.form.CheckBox');
11 dojo.require('dijit.form.Button');
12 dojo.require('dojo.date');
13 dojo.require('openils.CGI');
14 dojo.require('openils.XUL');
15 dojo.require('openils.Util');
16 dojo.require('openils.Event');
17
18 dojo.requireLocalization('openils.actor', 'register');
19 var localeStrings = dojo.i18n.getLocalization('openils.actor', 'register');
20
21
22 var pcrud;
23 var fmClasses = ['au', 'ac', 'aua', 'actsc', 'asv', 'asvq', 'asva'];
24 var fieldDoc = {};
25 var statCats;
26 var statCatTempate;
27 var surveys;
28 var staff;
29 var patron;
30 var uEditUsePhonePw = false;
31 var widgetPile = [];
32 var uEditCardVirtId = -1;
33 var uEditAddrVirtId = -1;
34 var orgSettings = {};
35 var userSettings = {};
36 var userSettingsToUpdate = {};
37 var tbody;
38 var addrTemplateRows;
39 var cgi;
40 var cloneUser;
41 var cloneUserObj;
42
43
44 if(!window.xulG) var xulG = null;
45
46
47 function load() {
48     staff = new openils.User().user;
49     pcrud = new openils.PermaCrud();
50     cgi = new openils.CGI();
51     cloneUser = cgi.param('clone');
52     var userId = cgi.param('usr');
53
54     if(xulG) {
55             if(xulG.ses) openils.User.authtoken = xulG.ses;
56             if(xulG.clone !== null) cloneUser = xulG.clone;
57         if(xulG.usr !== null) userId = xulG.usr
58         if(xulG.params) {
59             var parms = xulG.params;
60                 if(parms.ses) 
61                 openils.User.authtoken = parms.ses;
62                 if(parms.clone) 
63                 cloneUser = parms.clone;
64             if(parms.usr !== null)
65                 userId = parms.usr
66         }
67     }
68
69     orgSettings = fieldmapper.aou.fetchOrgSettingBatch(staff.ws_ou(), [
70         'global.juvenile_age_threshold',
71         'patron.password.use_phone',
72         'ui.patron.default_inet_access_level',
73         'circ.holds.behind_desk_pickup_supported'
74     ]);
75     for(k in orgSettings)
76         if(orgSettings[k])
77             orgSettings[k] = orgSettings[k].value;
78
79     uEditFetchUserSettings(userId);
80
81     if(userId) {
82         patron = uEditLoadUser(userId);
83     } else {
84         patron = uEditNewPatron();
85         if(cloneUser) 
86             uEditCopyCloneData(patron);
87     }
88
89
90     var list = pcrud.search('fdoc', {fm_class:fmClasses});
91     for(var i in list) {
92         var doc = list[i];
93         if(!fieldDoc[doc.fm_class()])
94             fieldDoc[doc.fm_class()] = {};
95         fieldDoc[doc.fm_class()][doc.field()] = doc;
96     }
97
98     tbody = dojo.byId('uedit-tbody');
99
100     addrTemplateRows = dojo.query('tr[type=addr-template]', tbody);
101     dojo.forEach(addrTemplateRows, function(row) { row.parentNode.removeChild(row); } );
102     statCatTemplate = tbody.removeChild(dojo.byId('stat-cat-row-template'));
103     surveyTemplate = tbody.removeChild(dojo.byId('survey-row-template'));
104     surveyQuestionTemplate = tbody.removeChild(dojo.byId('survey-question-row-template'));
105
106     loadStaticFields();
107     if(patron.isnew() && patron.addresses().length == 0) 
108         uEditNewAddr(null, uEditAddrVirtId, true);
109     else loadAllAddrs();
110     loadStatCats();
111     loadSurveys();
112     checkClaimsReturnCountPerm();
113     checkClaimsNoCheckoutCountPerm();
114 }
115
116 /*
117  * clone the home org, phone numbers, and billing/mailing address
118  */
119 function uEditCopyCloneData(patron) {
120     cloneUserObj = uEditLoadUser(cloneUser);
121
122     dojo.forEach( [
123         'home_ou', 
124         'day_phone', 
125         'evening_phone', 
126         'other_phone',
127         'billing_address',
128         'mailing_address' ], 
129         function(field) {
130             patron[field](cloneUserObj[field]());
131         }
132     );
133
134     // don't grab all addresses().  the only ones we can link to are billing/mailing
135     if(patron.billing_address())
136         patron.addresses().push(patron.billing_address());
137
138     if(patron.mailing_address() && (
139             patron.addresses().length == 0 || 
140             patron.mailing_address().id() != patron.billing_address().id()) )
141         patron.addresses().push(patron.mailing_address());
142 }
143
144
145 function uEditFetchUserSettings(userId) {
146     userSettings = fieldmapper.standardRequest(
147         ['open-ils.actor', 'open-ils.actor.patron.settings.retrieve'],
148         {params : [openils.User.authtoken, userId, ['circ.holds_behind_desk']]});
149 }
150
151
152 function uEditLoadUser(userId) {
153     var patron = fieldmapper.standardRequest(
154         ['open-ils.actor', 'open-ils.actor.user.fleshed.retrieve'],
155         {params : [openils.User.authtoken, userId]}
156     );
157     openils.Event.parse_and_raise(patron);
158     return patron;
159 }
160
161 function loadStaticFields() {
162     for(var idx = 0; tbody.childNodes[idx]; idx++) {
163         var row = tbody.childNodes[idx];
164         if(row.nodeType != row.ELEMENT_NODE) continue;
165         var fmcls = row.getAttribute('fmclass');
166         if(fmcls) {
167             fleshFMRow(row, fmcls);
168         } else {
169             if(row.getAttribute('user_setting'))
170                 fleshUserSettingRow(row, row.getAttribute('user_setting'))
171         }
172     }
173 }
174
175 function fleshUserSettingRow(row, userSetting) {
176     switch(userSetting) {
177         case 'circ.holds_behind_desk':
178             if(orgSettings['circ.holds.behind_desk_pickup_supported']) {
179                 openils.Util.show('uedit-settings-divider', 'table-row');
180                 openils.Util.show(row, 'table-row');
181                 if(userSettings[userSetting]) 
182                     holdsBehindShelfBox.attr('checked', true);
183
184                 // if the setting changes, add it to the list of settings that need updating
185                 dojo.connect(
186                     holdsBehindShelfBox, 
187                     'onChange', 
188                     function(newVal) { userSettingsToUpdate['circ.holds_behind_desk'] = newVal; }
189                 );
190             } 
191     }
192 }
193
194 function uEditUpdateUserSettings(userId) {
195     return fieldmapper.standardRequest(
196         ['open-ils.actor', 'open-ils.actor.patron.settings.update'],
197         {params : [openils.User.authtoken, userId, userSettingsToUpdate]});
198 }
199
200 function loadAllAddrs() {
201     dojo.forEach(patron.addresses(),
202         function(addr) {
203             uEditNewAddr(null, addr.id());
204         }
205     );
206 }
207
208 function loadStatCats() {
209
210     statCats = fieldmapper.standardRequest(
211         ['open-ils.circ', 'open-ils.circ.stat_cat.actor.retrieve.all'],
212         {params : [openils.User.authtoken, staff.ws_ou()]}
213     );
214
215     // draw stat cats
216     for(var idx in statCats) {
217         var stat = statCats[idx];
218         var row = statCatTemplate.cloneNode(true);
219         row.id = 'stat-cat-row-' + idx;
220         tbody.appendChild(row);
221         getByName(row, 'name').innerHTML = stat.name();
222         var valtd = getByName(row, 'widget');
223         var span = valtd.appendChild(document.createElement('span'));
224         var store = new dojo.data.ItemFileReadStore(
225                 {data:fieldmapper.actsc.toStoreData(stat.entries())});
226         var comboBox = new dijit.form.ComboBox({store:store}, span);
227         comboBox.labelAttr = 'value';
228         comboBox.searchAttr = 'value';
229
230         comboBox._wtype = 'statcat';
231         comboBox._statcat = stat.id();
232         widgetPile.push(comboBox); 
233
234         // populate existing cats
235         var map = patron.stat_cat_entries().filter(
236             function(mp) { return (mp.stat_cat() == stat.id()) })[0];
237         if(map) comboBox.attr('value', map.stat_cat_entry()); 
238
239     }
240 }
241
242 function loadSurveys() {
243
244     surveys = fieldmapper.standardRequest(
245         ['open-ils.circ', 'open-ils.circ.survey.retrieve.all'],
246         {params : [openils.User.authtoken]}
247     );
248
249     // draw surveys
250     for(var idx in surveys) {
251         var survey = surveys[idx];
252         var srow = surveyTemplate.cloneNode(true);
253         tbody.appendChild(srow);
254         getByName(srow, 'name').innerHTML = survey.name();
255
256         for(var q in survey.questions()) {
257             var quest = survey.questions()[q];
258             var qrow = surveyQuestionTemplate.cloneNode(true);
259             tbody.appendChild(qrow);
260             getByName(qrow, 'question').innerHTML = quest.question();
261
262             var span = getByName(qrow, 'answers').appendChild(document.createElement('span'));
263             var store = new dojo.data.ItemFileReadStore(
264                 {data:fieldmapper.asva.toStoreData(quest.answers())});
265             var select = new dijit.form.FilteringSelect({store:store}, span);
266             select.labelAttr = 'answer';
267             select.searchAttr = 'answer';
268
269             select._wtype = 'survey';
270             select._survey = survey.id();
271             select._question = quest.id();
272             widgetPile.push(select); 
273         }
274     }
275 }
276
277
278 function fleshFMRow(row, fmcls, args) {
279     var fmfield = row.getAttribute('fmfield');
280     var wclass = row.getAttribute('wclass');
281     var wstyle = row.getAttribute('wstyle');
282     var wconstraints = row.getAttribute('wconstraints');
283     var fieldIdl = fieldmapper.IDL.fmclasses[fmcls].field_map[fmfield];
284     if(!args) args = {};
285
286     var existing = dojo.query('td', row);
287     var htd = existing[0] || row.appendChild(document.createElement('td'));
288     var ltd = existing[1] || row.appendChild(document.createElement('td'));
289     var wtd = existing[2] || row.appendChild(document.createElement('td'));
290
291     openils.Util.addCSSClass(htd, 'uedit-help');
292     if(fieldDoc[fmcls] && fieldDoc[fmcls][fmfield]) {
293         var link = dojo.byId('uedit-help-template').cloneNode(true);
294         link.id = '';
295         link.onclick = function() { ueLoadContextHelp(fmcls, fmfield) };
296         openils.Util.removeCSSClass(link, 'hidden');
297         htd.appendChild(link);
298     }
299
300     if(!ltd.textContent) {
301         var span = document.createElement('span');
302         ltd.appendChild(document.createTextNode(fieldIdl.label));
303     }
304
305     span = document.createElement('span');
306     wtd.appendChild(span);
307
308     var fmObject = null;
309     var disabled = false;
310     switch(fmcls) {
311         case 'au' : fmObject = patron; break;
312         case 'ac' : fmObject = patron.card(); break;
313         case 'aua' : 
314             fmObject = patron.addresses().filter(
315                 function(i) { return (i.id() == args.addr) })[0];
316             if(fmObject && fmObject.usr() != patron.id())
317                 disabled = true;
318             break;
319     }
320
321     var dijitArgs = {
322         style: wstyle, 
323         required : required,
324         constraints : (wconstraints) ? eval('('+wconstraints+')') : {}, // the ()'s prevent Invalid Label errors with eval
325         disabled : disabled
326     };
327
328     var value = row.getAttribute('wvalue');
329     if(value !== null)
330         dijitArgs.value = value;
331
332     var required = row.getAttribute('required') == 'required';
333     var widget = new openils.widget.AutoFieldWidget({
334         idlField : fieldIdl,
335         fmObject : fmObject,
336         fmClass : fmcls,
337         parentNode : span,
338         widgetClass : wclass,
339         dijitArgs : dijitArgs,
340         orgLimitPerms : ['UPDATE_USER'],
341     });
342
343     widget.build();
344
345     widget._wtype = fmcls;
346     widget._fmfield = fmfield;
347     widget._addr = args.addr;
348     widgetPile.push(widget);
349     attachWidgetEvents(fmcls, fmfield, widget);
350     return widget;
351 }
352
353 function findWidget(wtype, fmfield, callback) {
354     return widgetPile.filter(
355         function(i){
356             if(i._wtype == wtype && i._fmfield == fmfield) {
357                 if(callback) return callback(i);
358                 return true;
359             }
360         }
361     ).pop();
362 }
363
364 /**
365  * if the user does not have the UPDATE_PATRON_CLAIM_RETURN_COUNT, 
366  * they are not allowed to directly alter the claim return count. 
367  * This function checks the perm and disable/enables the widget.
368  */
369 function checkClaimsReturnCountPerm() {
370     new openils.User().getPermOrgList(
371         'UPDATE_PATRON_CLAIM_RETURN_COUNT',
372         function(orgList) { 
373             var cr = findWidget('au', 'claims_returned_count');
374             if(orgList.indexOf(patron.home_ou()) == -1) 
375                 cr.widget.attr('disabled', true);
376             else
377                 cr.widget.attr('disabled', false);
378         },
379         true, 
380         true
381     );
382 }
383
384
385 function checkClaimsNoCheckoutCountPerm() {
386     new openils.User().getPermOrgList(
387         'UPDATE_PATRON_CLAIM_NEVER_CHECKED_OUT_COUNT',
388         function(orgList) { 
389             var cr = findWidget('au', 'claims_never_checked_out_count');
390             if(orgList.indexOf(patron.home_ou()) == -1) 
391                 cr.widget.attr('disabled', true);
392             else
393                 cr.widget.attr('disabled', false);
394         },
395         true, 
396         true
397     );
398 }
399
400
401 function attachWidgetEvents(fmcls, fmfield, widget) {
402
403     if(fmcls == 'ac') {
404         if(fmfield == 'barcode') {
405             dojo.connect(widget.widget, 'onChange',
406                 function() {
407                     var un = findWidget('au', 'usrname');
408                     if(!un.widget.attr('value'))
409                         un.widget.attr('value', this.attr('value'));
410                 }
411             );
412             return;
413         }
414     }
415
416     if(fmcls == 'au') {
417         switch(fmfield) {
418
419             case 'profile': // when the profile changes, update the expire date
420                 dojo.connect(widget.widget, 'onChange', 
421                     function() {
422                         var self = this;
423                         var expireWidget = findWidget('au', 'expire_date');
424                         function found(items) {
425                             if(items.length == 0) return;
426                             var item = items[0];
427                             var interval = self.store.getValue(item, 'perm_interval');
428                             expireWidget.widget.attr('value', dojo.date.add(new Date(), 
429                                 'second', openils.Util.intervalToSeconds(interval)));
430                         }
431                         this.store.fetch({onComplete:found, query:{id:this.attr('value')}});
432                     }
433                 );
434                 return;
435
436             case 'dob':
437                 dojo.connect(widget.widget, 'onChange',
438                     function(newDob) {
439                         if(!newDob) return;
440                         var oldDob = patron.dob();
441                         if(dojo.date.stamp.fromISOString(oldDob) == newDob) return;
442
443                         var juvInterval = orgSettings['global.juvenile_age_threshold'] || '18 years';
444                         var juvWidget = findWidget('au', 'juvenile');
445                         var base = new Date();
446                         base.setTime(base.getTime() - Number(openils.Util.intervalToSeconds(juvInterval) + '000'));
447
448                         if(newDob <= base) // older than global.juvenile_age_threshold
449                             juvWidget.widget.attr('value', false);
450                         else
451                             juvWidget.widget.attr('value', true);
452                     }
453                 );
454                 return;
455
456             case 'first_given_name':
457             case 'family_name':
458                 dojo.connect(widget.widget, 'onChange',
459                     function(newVal) { uEditDupeSearch('name', newVal); });
460                 return;
461
462             case 'email':
463                 dojo.connect(widget.widget, 'onChange',
464                     function(newVal) { uEditDupeSearch('email', newVal); });
465                 return;
466
467             case 'ident_value':
468             case 'ident_value2':
469                 dojo.connect(widget.widget, 'onChange',
470                     function(newVal) { uEditDupeSearch('ident', newVal); });
471                 return;
472
473             case 'day_phone':
474             case 'evening_phone':
475             case 'other_phone':
476                 dojo.connect(widget.widget, 'onChange',
477                     function(newVal) { uEditDupeSearch('phone', newVal); });
478                 return;
479
480             case 'home_ou':
481                 dojo.connect(widget.widget, 'onChange',
482                     function(newVal) { 
483                         checkClaimsReturnCountPerm(); 
484                         checkClaimsNoCheckoutCountPerm();
485                     }
486                 );
487                 return;
488
489         }
490     }
491
492     if(fmclass = 'aua') {
493         switch(fmfield) {
494             case 'post_code':
495                 dojo.connect(widget.widget, 'onChange',
496                     function(e) { 
497                         fieldmapper.standardRequest(
498                             ['open-ils.search', 'open-ils.search.zip'],
499                             {   async: true,
500                                 params: [e],
501                                 oncomplete : function(r) {
502                                     var res = openils.Util.readResponse(r);
503                                     if(!res) return;
504                                     var callback = function(w) { return w._addr == widget._addr; };
505                                     if(res.city) findWidget('aua', 'city', callback).widget.attr('value', res.city);
506                                     if(res.state) findWidget('aua', 'state', callback).widget.attr('value', res.state);
507                                     if(res.county) findWidget('aua', 'county', callback).widget.attr('value', res.county);
508                                     if(res.alert) alert(res.alert);
509                                 }
510                             }
511                         );
512                     }
513                 );
514                 return;
515
516             case 'street1':
517             case 'street2':
518             case 'city':
519                 dojo.connect(widget.widget, 'onChange',
520                     function(e) {
521                         var callback = function(w) { return w._addr == widget._addr; };
522                         var args = {
523                             street1 : findWidget('aua', 'street1', callback).widget.attr('value'),
524                             street2 : findWidget('aua', 'street2', callback).widget.attr('value'),
525                             city : findWidget('aua', 'city', callback).widget.attr('value'),
526                             post_code : findWidget('aua', 'post_code', callback).widget.attr('value')
527                         };
528                         if(args.street1 && args.city && args.post_code)
529                             uEditDupeSearch('address', args); 
530                     }
531                 );
532                 return;
533         }
534     }
535 }
536
537 function uEditDupeSearch(type, value) {
538     var search;
539     switch(type) {
540
541         case 'name':
542             openils.Util.hide('uedit-dupe-names-link');
543             var fname = findWidget('au', 'first_given_name').widget.attr('value');
544             var lname = findWidget('au', 'family_name').widget.attr('value');
545             if( !(fname && lname) ) return;
546             search = {
547                 first_given_name : {value : fname, group : 0},
548                 family_name : {value : lname, group : 0},
549             };
550             break;
551
552         case 'email':
553             openils.Util.hide('uedit-dupe-email-link');
554             search = {email : {value : value, group : 0}};
555             break;
556
557         case 'ident':
558             openils.Util.hide('uedit-dupe-ident-link');
559             search = {ident : {value : value, group : 2}};
560             break;
561
562         case 'phone':
563             openils.Util.hide('uedit-dupe-phone-link');
564             search = {phone : {value : value, group : 2}};
565             break;
566
567         case 'address':
568             openils.Util.hide('uedit-dupe-address-link');
569             search = {};
570             dojo.forEach(['street1', 'street2', 'city', 'post_code'],
571                 function(field) {
572                     if(value[field])
573                         search[field] = {value : value[field], group: 1};
574                 }
575             );
576             break;
577     }
578
579     // find possible duplicate patrons
580     fieldmapper.standardRequest(
581         ['open-ils.actor', 'open-ils.actor.patron.search.advanced'],
582         {   async: true,
583             params: [openils.User.authtoken, search],
584             oncomplete : function(r) {
585                 var resp = openils.Util.readResponse(r);
586                 resp = resp.filter(function(id) { return (id != patron.id()); });
587
588                 if(resp && resp.length > 0) {
589
590                     openils.Util.hide('uedit-help-div');
591                     openils.Util.show('uedit-dupe-div');
592                     var link;
593
594                     switch(type) {
595                         case 'name':
596                             link = dojo.byId('uedit-dupe-names-link');
597                             link.innerHTML = dojo.string.substitute(localeStrings.DUPE_PATRON_NAME, [resp.length]);
598                             break;
599                         case 'email':
600                             link = dojo.byId('uedit-dupe-email-link');
601                             link.innerHTML = dojo.string.substitute(localeStrings.DUPE_PATRON_EMAIL, [resp.length]);
602                             break;
603                         case 'ident':
604                             link = dojo.byId('uedit-dupe-ident-link');
605                             link.innerHTML = dojo.string.substitute(localeStrings.DUPE_PATRON_IDENT, [resp.length]);
606                             break;
607                         case 'phone':
608                             link = dojo.byId('uedit-dupe-phone-link');
609                             link.innerHTML = dojo.string.substitute(localeStrings.DUPE_PATRON_PHONE, [resp.length]);
610                             break;
611                         case 'address':
612                             link = dojo.byId('uedit-dupe-address-link');
613                             link.innerHTML = dojo.string.substitute(localeStrings.DUPE_PATRON_ADDR, [resp.length]);
614                             break;
615                     }
616
617                     openils.Util.show(link);
618                     link.onclick = function() {
619                         search.search_sort = js2JSON(["penalties", "family_name", "first_given_name"]);
620                         if(window.xulG)
621                             window.xulG.spawn_search(search);
622                         else
623                             console.log("running XUL patron search " + js2JSON(search));
624                     }
625                 }
626             }
627         }
628     );
629 }
630
631 function getByName(node, name) {
632     return dojo.query('[name='+name+']', node)[0];
633 }
634
635
636 function ueLoadContextHelp(fmcls, fmfield) {
637     openils.Util.hide('uedit-dupe-div');
638     openils.Util.show('uedit-help-div');
639     dojo.byId('uedit-help-field').innerHTML = fieldmapper.IDL.fmclasses[fmcls].field_map[fmfield].label;
640     dojo.byId('uedit-help-text').innerHTML = fieldDoc[fmcls][fmfield].string();
641 }
642
643
644 /* creates a new patron object with card attached */
645 function uEditNewPatron() {
646     patron = new au();
647     patron.isnew(1);
648     patron.id(-1);
649     card = new ac();
650     card.id(uEditCardVirtId);
651     card.isnew(1);
652     patron.active(1);
653     patron.card(card);
654     patron.cards([card]);
655     patron.net_access_level(orgSettings['ui.patron.default_inet_access_level'] || 1);
656     patron.stat_cat_entries([]);
657     patron.survey_responses([]);
658     patron.addresses([]);
659     uEditMakeRandomPw(patron);
660     return patron;
661 }
662
663 function uEditMakeRandomPw(patron) {
664     if(uEditUsePhonePw) return;
665     var rand  = Math.random();
666     rand = parseInt(rand * 10000) + '';
667     while(rand.length < 4) rand += '0';
668 /*
669     appendClear($('ue_password_plain'),text(rand));
670     unHideMe($('ue_password_gen'));
671 */
672     patron.passwd(rand);
673     return rand;
674 }
675
676 function uEditWidgetVal(w) {
677     var val = (w.getFormattedValue) ? w.getFormattedValue() : w.attr('value');
678     if(val === '') val = null;
679     return val;
680 }
681
682 function uEditSave() { _uEditSave(); }
683 function uEditSaveClone() { _uEditSave(true); }
684
685 function _uEditSave(doClone) {
686
687     for(var idx in widgetPile) {
688         var w = widgetPile[idx];
689         var val = uEditWidgetVal(w);
690
691         switch(w._wtype) {
692             case 'au':
693                 patron[w._fmfield](val);
694                 break;
695
696             case 'ac':
697                 patron.card()[w._fmfield](val);
698                 break;
699
700             case 'aua':
701                 var addr = patron.addresses().filter(function(i){return (i.id() == w._addr)})[0];
702                 if(!addr) {
703                     addr = new fieldmapper.aua();
704                     addr.id(w._addr);
705                     addr.isnew(1);
706                     addr.usr(patron.id());
707                     patron.addresses().push(addr);
708                 } else {
709                     if(addr[w._fmfield]() != val)
710                         addr.ischanged(1);
711                 }
712                 addr[w._fmfield](val);
713
714                 if(dojo.byId('uedit-billing-address-' + addr.id()).checked) 
715                     patron.billing_address(addr.id());
716
717                 if(dojo.byId('uedit-mailing-address-' + addr.id()).checked)
718                     patron.mailing_address(addr.id());
719
720                 break;
721
722             case 'survey':
723                 if(val == null) break;
724                 var resp = new fieldmapper.asvr();
725                 resp.isnew(1);
726                 resp.survey(w._survey)
727                 resp.usr(patron.id());
728                 resp.question(w._question)
729                 resp.answer(val);
730                 patron.survey_responses().push(resp);
731                 break;
732
733             case 'statcat':
734                 if(val == null) break;
735
736                 var map = patron.stat_cat_entries().filter(
737                     function(m){
738                         return (m.stat_cat() == w._statcat) })[0];
739
740                 if(map) {
741                     if(map.stat_cat_entry() == val) 
742                         break;
743                     map.ischanged(1);
744                 } else {
745                     map = new fieldmapper.actscecm();
746                     map.isnew(1);
747                 }
748
749                 map.stat_cat(w._statcat);
750                 map.stat_cat_entry(val);
751                 map.target_usr(patron.id());
752                 patron.stat_cat_entries().push(map);
753                 break;
754         }
755     }
756
757     patron.ischanged(1);
758     fieldmapper.standardRequest(
759         ['open-ils.actor', 'open-ils.actor.patron.update'],
760         {   async: true,
761             params: [openils.User.authtoken, patron],
762             oncomplete: function(r) {
763                 newPatron = openils.Util.readResponse(r);
764                 if(newPatron) {
765                     uEditUpdateUserSettings(newPatron.id());
766                     uEditFinishSave(newPatron, doClone);
767                 }
768             }
769         }
770     );
771 }
772
773 function uEditFinishSave(newPatron, doClone) {
774
775     if(doClone && cloneUser == null)
776         cloneUser = newPatron.id();
777
778         if( doClone ) {
779
780                 if(xulG && typeof xulG.spawn_editor == 'function' && !patron.isnew() ) {
781             window.xulG.spawn_editor({ses:openils.User.authtoken,clone:cloneUser});
782             uEditRefresh();
783
784                 } else {
785                         location.href = location.href.replace(/\?.*/, '') + '?clone=' + cloneUser;
786                 }
787
788         } else {
789
790                 uEditRefresh();
791         }
792
793         uEditRefreshXUL(newPatron);
794 }
795
796 function uEditRefresh() {
797     var usr = cgi.param('usr');
798     var href = location.href.replace(/\?.*/, '');
799     href += ((usr) ? '?usr=' + usr : '');
800     location.href = href;
801 }
802
803 function uEditRefreshXUL(newuser) {
804         if (window.xulG && typeof window.xulG.on_save == 'function') 
805                 window.xulG.on_save(newuser);
806 }
807
808
809 /**
810  * Create a new address and insert it into the DOM
811  * @param evt ignored
812  * @param id The address id
813  * @param mkLinks If true, set the new address as the 
814  *  mailing/billing address for the user
815  */
816 function uEditNewAddr(evt, id, mkLinks) {
817
818     if(id == null) 
819         id = --uEditAddrVirtId; // new address
820
821     var addr =  patron.addresses().filter(
822         function(i) { return (i.id() == id) })[0];
823
824     dojo.forEach(addrTemplateRows, 
825         function(row) {
826
827             row = tbody.insertBefore(row.cloneNode(true), dojo.byId('new-addr-row'));
828             row.setAttribute('type', '');
829             row.setAttribute('addr', id+'');
830
831             if(row.getAttribute('fmclass')) {
832                 var widget = fleshFMRow(row, 'aua', {addr:id});
833
834                 // make new addresses valid by default
835                 if(id < 0 && row.getAttribute('fmfield') == 'valid') 
836                     widget.widget.attr('value', true); 
837
838             } else if(row.getAttribute('name') == 'uedit-addr-pending-row') {
839
840                 // if it's a pending address, show the 'approve' button
841                 if(addr && openils.Util.isTrue(addr.pending())) {
842                     openils.Util.show(row, 'table-row');
843                     dojo.query('[name=approve-button]', row)[0].onclick = 
844                         function() { uEditApproveAddress(addr); };
845
846                     if(addr.replaces()) {
847                         var div = dojo.query('[name=replaced-addr]', row)[0]
848                         var replaced =  patron.addresses().filter(
849                             function(i) { return (i.id() == addr.replaces()) })[0];
850
851                         div.innerHTML = dojo.string.substitute(localeStrings.REPLACED_ADDRESS, [
852                             replaced.address_type() || '',
853                             replaced.street1() || '',
854                             replaced.street2() || '',
855                             replaced.city() || '',
856                             replaced.state() || '',
857                             replaced.post_code() || ''
858                         ]);
859
860                     } else {
861                         openils.Util.hide(dojo.query('[name=replaced-addr-div]', row)[0]);
862                     }
863                 }
864
865             } else if(row.getAttribute('name') == 'uedit-addr-owner-row') {
866                 // address is owned by someone else.  provide option to load the
867                 // user in a different tab
868                 
869                 if(addr && addr.usr() != patron.id()) {
870                     openils.Util.show(row, 'table-row');
871                     var link = getByName(row, 'addr-owner');
872
873                     // fetch the linked user so we can present their name in the UI
874                     var addrUser;
875                     if(cloneUserObj && cloneUserObj.id() == addr.usr()) {
876                         addrUser = [
877                             cloneUserObj.first_given_name(), 
878                             cloneUserObj.second_given_name(), 
879                             cloneUserObj.family_name()
880                         ];
881                     } else {
882                         addrUser = fieldmapper.standardRequest(
883                             ['open-ils.actor', 'open-ils.actor.user.retrieve.parts'],
884                             {params: [
885                                 openils.User.authtoken, 
886                                 addr.usr(), 
887                                 ['first_given_name', 'second_given_name', 'family_name']
888                             ]}
889                         );
890                     }
891
892                     link.innerHTML = (addrUser.map(function(name) { return (name) ? name+' ' : '' })+'').replace(/,/g,''); // TODO i18n
893                     link.onclick = function() {
894                         if(openils.XUL.isXUL()) { 
895                             window.xulG.spawn_editor({ses:openils.User.authtoken, usr:addr.usr()})
896                         } else {
897                             parent.location.href = location.href.replace(/clone=\d+/, 'usr=' + addr.usr());
898                         }
899                     }
900                 }
901
902             } else if(row.getAttribute('name') == 'uedit-addr-divider') {
903                 // link up the billing/mailing address and give the inputs IDs so we can acces the later
904                 
905                 // billing address
906                 var ba = getByName(row, 'billing_address');
907                 ba.id = 'uedit-billing-address-' + id;
908                 if(mkLinks || (patron.billing_address() && patron.billing_address().id() == id))
909                     ba.checked = true;
910
911                 // mailing address
912                 var ma = getByName(row, 'mailing_address');
913                 ma.id = 'uedit-mailing-address-' + id;
914                 if(mkLinks || (patron.mailing_address() && patron.mailing_address().id() == id))
915                     ma.checked = true;
916
917             } else {
918                 var btn = dojo.query('[name=delete-button]', row)[0];
919                 if(btn) btn.onclick = function(){ uEditDeleteAddr(id) };
920             }
921         }
922     );
923 }
924
925 function uEditApproveAddress(addr) {
926     fieldmapper.standardRequest(
927         ['open-ils.actor', 'open-ils.actor.user.pending_address.approve'],
928         {   async: true,
929             params:  [openils.User.authtoken, addr],
930
931             oncomplete : function(r) {
932                 var oldId = openils.Util.readResponse(r);
933                     
934                 // remove addrs from UI
935                 dojo.forEach(
936                     patron.addresses(), 
937                     function(addr) { uEditDeleteAddr(addr.id(), true); }
938                 );
939
940                 if(oldId != null) {
941                     
942                     // remove the replaced address 
943                     if(oldId != addr.id()) {
944                                 patron.addresses(
945                             patron.addresses().filter(
946                                                 function(i) { return (i.id() != oldId); }
947                                         )
948                                 );
949                     }
950                     
951                     // fix the the new address
952                     addr.id(oldId);
953                     addr.replaces(null);
954                     addr.pending('f');
955
956                 }
957
958                 // redraw addrs
959                 loadAllAddrs();
960             }
961         }
962     );
963 }
964
965
966 function uEditDeleteAddr(id, noAlert) {
967     if(!noAlert) {
968         if(!confirm('Delete address ' + id)) return; /* XXX i18n */
969     }
970     var rows = dojo.query('tr[addr='+id+']', tbody);
971     for(var i = 0; i < rows.length; i++)
972         rows[i].parentNode.removeChild(rows[i]);
973     widgetPile = widgetPile.filter(function(w){return (w._addr != id)});
974 }
975
976 function uEditToggleRequired() {
977     if((tbody.className +'').match(/hide-non-required/)) 
978         openils.Util.removeCSSClass(tbody, 'hide-non-required');
979     else
980         openils.Util.addCSSClass(tbody, 'hide-non-required');
981     openils.Util.toggle('uedit-show-required');
982     openils.Util.toggle('uedit-show-all');
983 }
984
985
986
987 openils.Util.addOnLoad(load);