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