Add PLACE_UNFILLABLE_HOLD permission
[working/Evergreen.git] / Open-ILS / web / opac / skin / default / js / holds.js
1 var holdsOrgSelectorBuilt = false;
2 var holdArgs;
3
4 /* 
5 note: metarecord holds have a holdable_formats field that contains
6 item_type(s)-item_forms(s)-language
7 item_form and language are optional - if language exist and no 
8 item_form is specified, use item_type(s)--language
9 */
10
11 var noEmailMessage;
12 var noEmailMessageXUL;
13
14 var holdTargetTypeMap = {
15     M : 'metarecord',
16     T : 'record',
17     V : 'volume',
18     I : 'issuance',
19     C : 'copy',
20     P : 'part'
21 };
22
23
24
25 function holdsHandleStaff() {
26
27     // if we know the recipient's barcode, use it
28     if(xulG.patron_barcode) return _holdsHandleStaff();
29
30         swapCanvas($('xulholds_box'));
31         $('xul_recipient_barcode').focus();
32         $('xul_recipient_barcode').onkeypress = function(evt) 
33                 {if(userPressedEnter(evt)) { _holdsHandleStaff(); } };
34         $('xul_recipient_barcode_submit').onclick = _holdsHandleStaff;
35         $('xul_recipient_me').onclick = _holdsHandleStaffMe;
36
37         $('xul_recipient_barcode').onkeyup = function(evt) {
38         if($('xul_recipient_barcode').value == '') 
39             $('xul_recipient_me').disabled = false;
40         else
41             $('xul_recipient_me').disabled = true;
42     };
43 }
44
45 $('holds_frozen_thaw_input').onchange = 
46         function(){holdsVerifyThawDateUI('holds_frozen_thaw_input');}
47 $('holds_frozen_thaw_input').onkeyup = 
48         function(){holdsVerifyThawDateUI('holds_frozen_thaw_input');}
49
50 function _holdsHandleStaffMe() {
51         holdArgs.recipient = G.user;
52         holdsDrawEditor();
53 }
54
55 function _holdsHandleStaff() {
56         var barcode = xulG.patron_barcode;
57     if(!barcode) {
58         barcode = $('xul_recipient_barcode').value;
59         if(xulG.get_barcode) {
60             // We have a "complete the barcode" function, call it (actor = users only)
61             var new_barcode = xulG.get_barcode(window, 'actor', barcode);
62             // If we got a result (boolean false is "no result") check it
63             if(new_barcode) {
64                 // user_false string means they picked "None of the above"
65                 // Abort before any other events can fire
66                 if(new_barcode == "user_false") return;
67                 // No error means we have a (hopefully valid) completed barcode to use.
68                 // Otherwise, fall through to other methods of checking
69                 if(typeof new_barcode.ilsevent == 'undefined')
70                     barcode = new_barcode.barcode;
71             }
72         }
73     }
74         var user = grabUserByBarcode( G.user.session, barcode );
75
76         var evt;
77         if(evt = checkILSEvent(user)) {
78                 alertILSEvent(user);
79                 return;
80         }
81
82         if(!barcode || !user) {
83                 alertId('holds_invalid_recipient', barcode);
84                 return
85         }
86
87         grabUserPrefs(user);
88
89         holdArgs.recipient = user;
90         holdsDrawEditor();
91 }
92
93
94
95 /** args:
96   * record, volume, copy (ids)
97   * request, recipient, editHold (objects)
98   */
99
100 function holdsDrawEditor(args) {
101
102         holdArgs = (args) ? args : holdArgs;
103
104     if(!noEmailMessage)
105         noEmailMessage = $('holds_email').removeChild($('holds.no_email'));
106
107     if(!noEmailMessageXUL)
108         noEmailMessageXUL = $('holds_email').removeChild($('holds.no_email.xul'));
109
110         if(isXUL() && holdArgs.recipient == null 
111                         && holdArgs.editHold == null) {
112                 holdsHandleStaff();
113                 return;
114         }
115
116         if(!holdArgs.recipient) holdArgs.recipient = G.user;
117         if(!holdArgs.requestor) holdArgs.requestor = G.user;
118
119         if(!(holdArgs.requestor && holdArgs.requestor.session)) {
120                 detachAllEvt('common','locationChanged');
121                 attachEvt('common','loggedIn', holdsDrawEditor)
122                 initLogin();
123                 return;
124         }
125
126         if(holdArgs.editHold) // flesh the args with the existing hold 
127                 holdArgsFromHold(holdArgs.editHold, holdArgs);
128
129      removeCSSClass($('holds_parts_selector'), 'parts-warning');
130     holdArgs.partsSuggestionMade = false;
131
132         holdsDrawWindow();
133 }
134
135
136 // updates the edit window with the existing hold's data 
137 function _holdsUpdateEditHold() {
138
139         var hold = holdArgs.editHold;
140         var qstats = holdArgs.status;
141
142         var orgsel = $('holds_org_selector');
143     var frozenbox = $('holds_frozen_chkbox');
144
145         setSelector(orgsel, hold.pickup_lib());
146
147         if( hold.capture_time() || qstats.status > 2 ) {
148         frozenbox.disabled = true;
149         $('holds_frozen_thaw_input').disabled = true;
150         if(qstats.status == 3) {
151             // no pickup lib changes while in-transit
152                     orgsel.disabled = true;
153         } else {
154             var orgs = fetchPermOrgs('UPDATE_PICKUP_LIB_FROM_HOLDS_SHELF');
155             if(orgs[0] == -1)
156                         orgsel.disabled = true;
157         }
158     } else {
159                 orgsel.disabled = false;
160         frozenbox.disabled = false;
161     }
162
163
164         $('holds_submit').onclick = holdsEditHold;
165         $('holds_update').onclick = holdsEditHold;
166
167         if(hold.phone_notify()) {
168                 $('holds_enable_phone').checked = true;
169                 $('holds_phone').value = hold.phone_notify();
170
171         } else {
172                 $('holds_phone').disabled = true;
173                 $('holds_enable_phone').checked = false;
174         }
175
176         if(isTrue(hold.email_notify())) {
177                 $('holds_enable_email').checked = true;
178
179         } else {
180                 $('holds_enable_email').checked = false;
181         }
182
183     dijit.byId('holds_expire_time').setValue(dojo.date.stamp.fromISOString(hold.expire_time()));
184
185     /* populate the hold freezing info */
186     if(!frozenbox.disabled && isTrue(hold.frozen())) {
187         frozenbox.checked = true;
188         unHideMe($('hold_frozen_thaw_row'));
189         if(hold.thaw_date()) {
190             dijit.byId('holds_frozen_thaw_input').setValue(dojo.date.stamp.fromISOString(hold.thaw_date()));
191         } else {
192             dijit.byId('holds_frozen_thaw_input').setValue('');
193         }
194     } else {
195         frozenbox.checked = false;
196         dijit.byId('holds_frozen_thaw_input').setValue('');
197         hideMe($('hold_frozen_thaw_row'));
198     }
199 }
200
201 function holdsEditHold() {
202         var hold = holdsBuildHoldFromWindow();
203         if(!hold) return;
204         holdsUpdate(hold);
205         showCanvas();
206         if(holdArgs.onComplete)
207                 holdArgs.onComplete(hold);
208 }
209
210 function holdArgsFromHold(hold, oargs) {
211         var args = (oargs) ? oargs : {};
212         args.type = hold.hold_type();
213         var target = hold.target();
214     args[holdTargetTypeMap[args.type]] = target;
215         return args;
216 }
217
218 function holdFetchObjects(hold, doneCallback) {
219
220         var args = (hold) ? holdArgsFromHold(hold) : holdArgs;
221
222         var type = args.type;
223
224         if( type == 'C' ) {
225
226                 if( args.copyObject ) {
227
228                         args.copy = args.copyObject.id();
229                         args.volume = args.copyObject.call_number();
230                         _h_set_vol(args, doneCallback);
231
232                 } else {
233                         var creq = new Request(FETCH_COPY, args.copy);
234
235                         creq.callback(
236                                 function(r) {
237                                         var cp = r.getResultObject();
238                                         args.copyObject = cp;
239                                         args.volume = args.copyObject.call_number();
240                                         _h_set_vol(args, doneCallback);
241                                 }
242                         );
243                         creq.send();
244                 }
245         } else {
246                 if( type == 'V' ) {
247                         _h_set_vol(args, doneCallback);
248
249         } else if( type == 'I' ) {
250             _h_set_issuance(args, doneCallback);
251
252         } else if( type == 'P' ) {
253             _h_set_parts(args, doneCallback);
254
255                 } else {
256                         if( type == 'T') {
257                                 _h_set_rec(args, doneCallback);
258                         } else {
259                                 _h_set_rec_descriptors(args, doneCallback);
260                         }
261                 }
262         }
263
264         return args;
265 }
266
267 function _h_set_parts(args, doneCallback) {
268
269     var preq = new Request(
270         'open-ils.fielder:open-ils.fielder.bmp.atomic',
271         {"cache":1, "fields":["label", "record"],"query": {"id":args.part}}
272     );
273
274     preq.callback(
275         function(r) {
276             var part = r.getResultObject()[0];
277             args.record = part.record;
278             args.partObject = part;
279             _h_set_rec(args, doneCallback);
280         }
281     );
282
283     preq.send();
284 }
285
286 function _h_set_vol(args, doneCallback) {
287
288         if( args.volumeObject ) {
289                 args.volume = args.volumeObject.id();
290                 args.record = args.volumeObject.record();
291                 _h_set_rec(args, doneCallback);
292
293         } else {
294
295                 var vreq = new Request(FETCH_VOLUME, args.volume);
296                 vreq.callback(
297                         function(r) {
298                                 var vol = r.getResultObject();
299                                 args.volumeObject = vol;
300                                 args.record = vol.record();
301                                 _h_set_rec(args, doneCallback);
302                         }
303                 );
304                 vreq.send();
305         }
306 }
307
308 function _h_set_issuance(args, doneCallback) {
309
310         if( args.issuanceObject ) {
311                 args.issuance = args.issuanceObject.id();
312                 args.record = args.issuanceObject.subscription().record_entry();
313                 _h_set_rec(args, doneCallback);
314
315         } else {
316
317                 var vreq = new Request(FETCH_ISSUANCE, [args.issuance]);
318                 vreq.callback(
319                         function(r) {
320                                 var issuance = r.getResultObject()[0];
321                                 args.issuanceObject = issuance;
322                                 args.record = issuance.subscription().record_entry();
323                                 _h_set_rec(args, doneCallback);
324                         }
325                 );
326                 vreq.send();
327         }
328 }
329
330 function _h_set_rec(args, doneCallback) {
331
332         if(args.recordObject) 
333                 args.record = args.recordObject.doc_id();
334         else 
335                 args.recordObject = findRecord( args.record, 'T' );
336         
337         if( args.type == 'T' || args.type == 'M' )  {
338                 _h_set_rec_descriptors(args, doneCallback);
339         //} else if(args.type == 'P') {
340         //_h_get_parts(args, doneCallback);
341     } else {
342                 if(doneCallback) doneCallback(args);
343     }
344 }
345
346
347 function _h_set_rec_descriptors(args, doneCallback) {
348
349     if( ! args.pickup_lib )
350         args.pickup_lib = getSelectorVal($('holds_org_selector'));
351
352     if(args.pickup_lib === null)
353         args.pickup_lib = args.recipient.home_ou();
354
355         // grab the list of record desciptors attached to this records metarecord 
356         if( ! args.recordDescriptors )  {
357                 var params = { pickup_lib: args.pickup_lib };
358
359         if (args.type == 'M') {
360                 if( !args.metarecord && args.record) {
361                 params.metarecord = args.metarecord = args.record;
362                 delete(args.record);
363                 } else {
364                                 params.metarecord = args.metarecordObject.doc_id();
365                 }
366         } else {
367                 params.record = args.record;
368         }
369
370                 if( ! args.record ) {
371                         if( args.metarecord )
372                                 params.metarecord = args.metarecord;
373                         else 
374                                 params.metarecord = args.metarecordObject.doc_id();
375                 }
376
377                 var req = new Request(FETCH_MR_DESCRIPTORS, params );
378                 req.callback(
379                         function(r) {
380                                 var data = r.getResultObject();
381                                 args.recordDescriptors = args.recordDescriptors = data.descriptors;
382                                 args.metarecord = args.metarecord = data.metarecord;
383                                 if( args.type == 'M' && ! args.metarecordObject) 
384                                         args.metarecordObject = args.metarecordObject = findRecord(args.metarecord, 'M');       
385
386                 _h_get_parts(args, doneCallback);
387                         }
388                 );
389                 req.send();
390
391         } else {
392         _h_get_parts(args, doneCallback);
393         }
394
395         return args;
396 }
397
398 function _h_get_parts(args, doneCallback) {
399
400     if(args.type == 'M' || args.editHold || args.holdParts) {
401         if(doneCallback) 
402             doneCallback(args);
403
404     } else {
405
406                 var req = new Request(
407             'open-ils.search:open-ils.search.biblio.record_hold_parts', 
408                     {pickup_lib: args.pickup_lib, record: args.record}
409         );
410
411                 req.callback(
412                         function(r) {
413                                 args.recordParts = r.getResultObject();
414                 if(doneCallback)
415                     doneCallback(args);
416                         }
417                 );
418                 req.send();
419     }
420 }
421
422
423
424 function holdsDrawWindow() {
425         swapCanvas($('holds_box'));
426         $('holds_cancel').onclick = function(){ runEvt('common', 'holdUpdateCanceled'), showCanvas() };
427         $('holds_submit').onclick = function(){holdsPlaceHold(holdsBuildHoldFromWindow())};
428         $('holds_update').onclick = function(){holdsPlaceHold(holdsBuildHoldFromWindow())};
429         holdFetchObjects(null, 
430                 function(){
431                         __holdsDrawWindow();
432
433                         if(holdArgs.editHold) {
434                                 hideMe($('holds_submit'));
435                                 unHideMe($('holds_update'));
436                                 var req = new Request(FETCH_HOLD_STATUS, 
437                                         G.user.session, holdArgs.editHold.id());
438                                 req.send(true);
439                                 holdArgs.status = req.result();
440                                 _holdsUpdateEditHold();
441                         }  
442                 }
443         );
444 }
445
446 function __holdsDrawWindow() {
447
448         var rec = holdArgs.recordObject;
449         var vol = holdArgs.volumeObject;
450         var copy = holdArgs.copyObject;
451         var mr = holdArgs.metarecordObject;
452
453         rec = (rec) ? rec : mr;
454
455         if(!holdsOrgSelectorBuilt) {
456                 holdsBuildOrgSelector(null,0);
457                 holdsOrgSelectorBuilt = true;
458                 var selector = $('holds_org_selector');
459
460                 /*
461                 var o_loc = findOrgUnit(getOrigLocation());
462                 var t = findOrgType(o_loc.ou_type());
463                 if( t.can_have_users() ) 
464                         setSelector(selector, o_loc.id());
465                 else 
466                 */
467
468                 setSelector(selector, holdArgs.recipient.home_ou());
469         
470         }
471
472         /*
473         if(isXUL()) {
474                 var dsel = $('holds_depth_selector');
475                 unHideMe($('holds_depth_selector_row'));
476                 if(dsel.getElementsByTagName('option').length == 0) {
477                         var types = globalOrgTypes;
478                         var depth = findOrgDepth(G.user.ws_ou());
479                         iterate(types, 
480                                 function(t) {
481                                         if(t.depth() > depth) return;
482                                         insertSelectorVal(dsel, -1, t.opac_label(), t.depth());
483                                 }
484                         );
485                 }
486         }
487         */
488
489         appendClear($('holds_recipient'), text(
490                 holdArgs.recipient.family_name() + ', ' +  
491                         holdArgs.recipient.first_given_name()));
492         appendClear($('holds_title'), text(rec.title()));
493         appendClear($('holds_author'), text(rec.author()));
494
495     if( holdArgs.type == 'I' ) {
496                 unHideMe($('holds_type_row'));
497         unHideMe($('holds_is_issuance'));
498         unHideMe($('holds_issuance_row'));
499         appendClear($('holds_issuance_label'), text(holdArgs.issuanceObject.label()));
500
501     } else if( holdArgs.type == 'V' || holdArgs.type == 'C' ) {
502
503                 unHideMe($('holds_type_row'));
504                 unHideMe($('holds_cn_row'));
505                 appendClear($('holds_cn'), text(holdArgs.volumeObject.label()));
506
507                 if( holdArgs.type == 'V'  ) {
508                         unHideMe($('holds_is_cn'));
509                         hideMe($('holds_is_copy'));
510
511                 } else {
512                         hideMe($('holds_is_cn'));
513                         unHideMe($('holds_is_copy'));
514                         unHideMe($('holds_copy_row'));
515                         appendClear($('holds_copy'), text(holdArgs.copyObject.barcode()));
516                 }
517
518         } else {
519                 hideMe($('holds_type_row'));
520                 hideMe($('holds_copy_row'));
521                 hideMe($('holds_cn_row'));
522                 hideMe($('holds_issuance_row'));
523         }
524
525     if(holdArgs.recordParts && holdArgs.recordParts.length) {
526         var selector = $('holds_parts_selector');
527         unHideMe($('holds_parts_row'));
528         unHideMe(selector);
529
530         var nodeList = [];
531         dojo.forEach(selector.options, 
532             function(node) { if(node.value != '') nodeList.push(node) } );
533
534         dojo.forEach(nodeList, function(node) { selector.removeChild(node); });
535
536         dojo.forEach(
537             holdArgs.recordParts, 
538             function(part) {
539                 insertSelectorVal(selector, -1, part.label, part.id);
540             }
541         );
542
543     } else if(holdArgs.type == 'P') {
544         unHideMe($('holds_parts_row'));
545         unHideMe($('holds_parts_label'));
546             appendClear( $('holds_parts_label'), text(holdArgs.partObject.label));
547     }
548
549         removeChildren($('holds_format'));
550
551         var mods_formats = rec.types_of_resource();
552         var formats;
553
554         if (holdArgs.recordDescriptors && holdArgs.recordDescriptors.length)
555                 formats = holdArgs.recordDescriptors[0].item_type();
556
557         if( holdArgs.type == 'T' ) {
558                 var desc = grep( holdArgs.recordDescriptors,
559                         function(i) {
560                                 return (i.record() == holdArgs.record); 
561                         }
562                 );
563                 if (desc) {
564                         formats = desc[0].item_type();
565                 }
566         }
567
568         if( holdArgs.type == 'M' ) {
569                 var mr_formats;
570                 if(holdArgs.editHold){
571                         mr_formats = holdArgs.editHold.holdable_formats();
572                 }else{
573                         mr_formats = ''; // collect the item_type()s from all holdArgs.recordDescriptors
574                         for(var desc in holdArgs.recordDescriptors){
575                 if (!holdArgs.recordDescriptors[desc].item_type()) continue;
576                                 mr_formats += holdArgs.recordDescriptors[desc].item_type();
577                         }
578
579             var first_form = 1;
580                         for(var desc in holdArgs.recordDescriptors){
581                 if (!holdArgs.recordDescriptors[desc].item_form()) continue;
582                 if (first_form) {
583                     mr_formats += '-';
584                     first_form = 0;
585                 }
586                                 mr_formats += holdArgs.recordDescriptors[desc].item_form();
587                         }
588
589
590                 }
591                 
592                 var data = holdsParseMRFormats(mr_formats);
593                 mods_formats = data.mods_formats;
594                 formats = data.formats;
595         }
596
597
598         for( var i in mods_formats ) {
599                 var res = mods_formats[i];
600                 var img = elem("img");
601                 setResourcePic(img, res);
602                 $('holds_format').appendChild(img);
603                 if(formats)
604                         $('holds_format').appendChild(text(' '+ MARCTypeToFriendly(formats[i]) +' '));
605                 else
606                         $('holds_format').appendChild(text(' '+ mods_formats[i] +' '));
607                 $('holds_format').appendChild(elem('br'));
608         }
609
610
611         $('holds_phone').value = holdArgs.recipient.day_phone();
612         appendClear( $('holds_email'), text(holdArgs.recipient.email()));
613
614         var pref = holdArgs.recipient.prefs[PREF_HOLD_NOTIFY];
615
616         if(pref) {
617                 if( ! pref.match(/email/i) ) {
618                         $('holds_enable_email').checked = false;
619                 } else {
620                         $('holds_enable_email').checked = true;
621                 }
622
623                 if( ! pref.match(/phone/i) ) {
624                         $('holds_phone').disabled = true;
625                         $('holds_enable_phone').checked = false;
626                 } else {
627                         $('holds_phone').disabled = false;
628                         $('holds_enable_phone').checked = true;
629                 }
630         }
631
632     if(!holdArgs.recipient.email()) {
633                 $('holds_enable_email').checked = false;        
634                 $('holds_enable_email').disabled = true;
635         var message;
636         if(isXUL()) {
637             message = noEmailMessageXUL.cloneNode(true);
638                 appendClear($('holds_email'), message);
639         } else {
640             message = noEmailMessage.cloneNode(true);
641                 appendClear($('holds_email'), message);
642             $('holds.no_email.my_account').setAttribute('href', buildOPACLink({page:MYOPAC},null,true));
643         }
644         unHideMe(message);
645     }
646
647         if(!$('holds_phone').value) 
648                 $('holds_enable_phone').checked = false;        
649
650         appendClear($('holds_physical_desc'), text(rec.physical_description()));
651
652         if(holdArgs.type == 'M') hideMe($('hold_physical_desc_row'));
653
654         holdsSetFormatSelector();
655
656     $('holds_frozen_chkbox').checked = false;
657     hideMe($('hold_frozen_thaw_row'));
658
659     var interval = fetchOrgSettingDefault(holdArgs.recipient.home_ou(), 'circ.hold_expire_interval');
660     var secs = 0;
661     if(interval) {
662         secs = interval_to_seconds(interval);
663         var expire = new Date();
664         expire.setTime(expire.getTime() + Number(secs + '000'));
665         dijit.byId('holds_expire_time').setValue(expire);
666     }
667 }
668
669 function holdsParseMRFormats(str) {
670         var data = str.split(/-/);      
671
672         var formats = [];
673         var mods_formats = [];
674
675         for( var i = 0; i < data[0].length; i++ ) {
676                 formats.push( data[0].charAt(i) );
677                 mods_formats.push( MARCFormatToMods( formats[i] ) );
678         }
679         
680         formats = uniquify(formats);
681         mods_formats = uniquify(mods_formats);
682
683         return {
684                 formats                 : formats,
685                 mods_formats    : mods_formats,
686                 lang                            : data[2],
687                 largeprint              : data[1]
688         };
689 }
690
691
692 function holdsSetFormatSelector() {
693         var type = holdArgs.type;
694         if( type == 'C' || type == 'V' || type == "I" || holdArgs.editHold ) return;
695
696         var data                                = holdsGetFormats();
697         var avail_formats       = data.avail_formats;
698         var sel_formats = data.sel_formats;
699         holdArgs.language = data.lang;
700         if( type=='M'){         
701                 hideMe($('holds_alt_formats_row_extras'));
702                 unHideMe($('holds_alt_formats_row'));   
703         }else{
704                 unHideMe($('holds_alt_formats_row_extras'));
705         }
706
707         var selector = $('hold_alt_form_selector');
708
709     for( var i = 0; i < selector.options.length; i++ ) {
710         if (selector.options[i].className.indexOf('hide_me') == -1)
711             hideMe(selector.options[i]);
712         selector.options[i].disabled = true;
713     }
714
715         for( var i = 0; i < avail_formats.length; i++ ) {
716                 var form = avail_formats[i];
717                 var opt = findFormatSelectorOptByParts(selector,form);
718         if (!opt) continue;
719                 if(type=='M') opt.selected=true;
720                 unHideMe(opt);
721         opt.disabled = false;
722         }
723
724     // If the user selects a format, P-type holds are no longer an option
725     // disable and reset the P-type form control
726     selector.onchange = function() {
727         var partsSel = $('holds_parts_selector');
728         for(var i = 0; i < selector.options.length; i++) {
729             if(selector.options[i].selected) {
730                 partsSel.selectedIndex = 0; // none selected
731                 partsSel.disabled = true;
732                 return;
733             }
734         }
735         partsSel.disabled = false;
736     }
737 }
738
739 function findFormatSelectorOptByParts( sel, val ) {
740     var parts = val.split('-');
741     for( var i = 0; i < sel.options.length; i++ ) {
742         var opt = sel.options[i];
743         var oval = opt.value;
744         var oparts = oval.split('-');
745         if( oparts[0].indexOf(parts[0]) > -1 && (!parts[1] || oparts[1].indexOf(parts[1]) > -1) ) return opt;
746     }
747     return null;
748 }
749
750 function holdsGetFormats() {
751
752         var lang;
753         var formats = [];
754         var sformats = []; // selected formats 
755
756         var type = holdArgs.type;
757         var desc = holdArgs.recordDescriptors;
758         var rec = holdArgs.record;
759         var mrec = holdArgs.metarecord;
760
761
762         if( type == 'T') {
763
764                 for( var i = 0; i < desc.length; i++ ) {
765                         var d = desc[i];
766                         if( d.record() == holdArgs.record ) {
767                                 lang = d.item_lang();
768                                 holdArgs.myFormat =  _t_f_2_format(d.item_type(), d.item_form());
769                                 sformats.push(holdArgs.myFormat);
770                                 break;
771                         }
772                 }
773
774         for( var i = 0; i < desc.length; i++ ) {
775                 var d = desc[i];
776                     if( type == 'T' && d.item_lang() != lang ) continue;
777                 formats.push( _t_f_2_format(d.item_type(), d.item_form()));
778             }
779
780         } else if( type =='M') {
781
782         // All available formats are selected by default in MR holds
783         for( var i = 0; i < desc.length; i++ ) {
784                 var d = desc[i];
785                     var _tmp_f = _t_f_2_format(d.item_type(), d.item_form());
786                 formats.push( _tmp_f );
787                 sformats.push( _tmp_f );
788         }
789         }
790
791         formats = uniquify(formats);
792         sformats = uniquify(sformats);
793
794         return {
795                 lang : lang,
796                 avail_formats : formats, 
797                 sel_formats : sformats
798         };
799 }
800
801
802
803 function _t_f_2_format(type, form) {
804         if( (type == 'a' || type == 't') && form == 's' ) return 'at-s';
805         if( form == 'd' ) return 'at-d';
806         return (type == 'a' || type == 't') ? 'at' : type;
807 }
808
809 function holdsSetSelectedFormats() {
810
811         var cn = $('holds_alt_formats_row').className;
812         if( cn && cn.match(/hide_me/) ) return;
813
814         var selector = $('hold_alt_form_selector');
815         var vals = getSelectedList(selector);
816
817         if(vals.length == 0) return;
818
819         var fstring = "";
820
821         if( contains(vals, 'at-d') || contains(vals, 'at-s') || contains(vals, 'at')) {
822                 if( contains(vals, 'at') ) {
823                         fstring = 'at';
824                 } else if (contains(vals, 'at-s') && contains(vals, 'at-d')) {
825                         fstring = 'at-sd';
826                 } else if (!contains(vals, 'at-s')) {
827                         fstring = 'at-d';
828                 } else {
829                         fstring = 'at-s';
830                 }
831         }
832
833         for( var i = 0; i < vals.length; i++ ) {
834                 var val = vals[i];
835                 if( !val.match(/at/) ) fstring = val + fstring;
836         }
837
838         if( holdArgs.language ) {
839                 if( fstring.match(/-/) )
840                         fstring = fstring + '-' + holdArgs.language;
841                 else
842                         fstring = fstring + '--' + holdArgs.language;
843         }
844
845
846         return fstring;
847 }
848
849
850 function holdsCheckPossibility(pickuplib, hold, recurse) {
851
852         var args = { 
853                 titleid : holdArgs.record,
854                 mrid : holdArgs.metarecord,
855                 volume_id : holdArgs.volume,
856                 issuanceid : holdArgs.issuance,
857                 copy_id : holdArgs.copy,
858                 hold_type : holdArgs.type,
859                 holdable_formats : holdArgs.holdable_formats,
860                 patronid : holdArgs.recipient.id(),
861                 depth : 0, 
862                 pickup_lib : pickuplib,
863         partid : holdArgs.part
864         };
865
866         if(recurse) {
867                 /* if we're calling create again (recursing), 
868                         we know that the hold possibility check already succeeded */
869                 holdHandleCreateResponse({_recurse:true, _hold:hold}, true );
870
871         } else {
872                 _debug("hold possible args = "+js2JSON(args));
873         
874                 var req = new Request(CHECK_HOLD_POSSIBLE, G.user.session, args );
875         
876                 req.request.alertEvent = false;
877                 req.request._hold = hold;
878                 req.request._recurse = recurse;
879                 req.callback(holdHandleCreateResponse);
880                 req.send();
881         }
882 }
883
884
885 function holdsBuildOrgSelector(node) {
886
887         if(!node) node = globalOrgTree;
888         if(!isTrue(node.opac_visible()) && !isXUL()) return;
889
890         var render_this_org = true;
891         var orgHiding = checkOrgHiding(); // value here is cached so not too painful with the recursion
892         if (orgHiding) {
893                 if (node.id() == globalOrgTree.id()) {
894                         node = orgHiding.org; // top of tree = org hiding context org
895                 }
896                 if ( ! orgIsMine( orgHiding.org, node, orgHiding.depth ) ) {
897                         render_this_org = false;
898                 }
899         }
900
901         if (render_this_org) {
902                 var selector = $('holds_org_selector');
903                 var index = selector.options.length;
904
905                 var type = findOrgType(node.ou_type());
906                 var indent = type.depth() - 1;
907
908                 var opt = setSelectorVal( selector, index, node.name(), node.id(), null, indent );
909                 if(!type.can_have_users()) {
910                         opt.disabled = true;
911                         addCSSClass(opt, 'disabled_option');
912                 }
913         }
914         
915         for( var i in node.children() ) {
916                 var child = node.children()[i];
917                 if(child) holdsBuildOrgSelector(child);
918         }
919 }
920
921 function holdsBuildHoldFromWindow() {
922
923         var org = getSelectorVal($('holds_org_selector'));
924         var node = findOrgUnit(org);
925         var ntype = findOrgType(node.ou_type());
926         if(!ntype.can_have_users()) {
927                 alertId('holds_pick_good_org');
928                 return;
929         }
930
931     fieldmapper.IDL.load(['ahr']);
932         var hold = new ahr();
933         if(holdArgs.editHold) {
934                 hold = holdArgs.editHold;
935                 holdArgs.editHold = null;
936         }
937
938         if( $('holds_enable_phone').checked ) {
939                 var phone = $('holds_phone').value;
940                 if( !phone || !phone.match(REGEX_PHONE) ) {
941                         alert($('holds_bad_phone').innerHTML);
942                         return null;
943                 }
944                 hold.phone_notify(phone);
945
946         } else {
947                 hold.phone_notify("");
948         }
949
950         if( $('holds_enable_email').checked ) 
951                 hold.email_notify(1);
952         else
953                 hold.email_notify(0);
954
955     var part = getSelectorVal($('holds_parts_selector'));
956     if(part) {
957         holdArgs.type = 'P';
958         holdArgs.part = part;
959     }
960
961         var target = holdArgs[holdTargetTypeMap[holdArgs.type]];
962
963     // a mono part is selected
964
965         hold.pickup_lib(org); 
966         //hold.request_lib(org); 
967         hold.requestor(holdArgs.requestor.id());
968         hold.usr(holdArgs.recipient.id());
969         hold.target(target);
970         hold.hold_type(holdArgs.type);
971
972     var expDate = dijit.byId('holds_expire_time').getValue();
973     if(expDate) {
974         var expireDate = dojo.date.stamp.toISOString(expDate);
975         expireDate = holdsVerifyThawDate(expireDate); 
976         if(expireDate)
977             hold.expire_time(expireDate);
978         else 
979             return;
980     }
981
982     // see if this hold should be frozen and for how long
983     if($('holds_frozen_chkbox').checked) {
984         hold.frozen('t');
985         unHideMe($('hold_frozen_thaw_row'));
986         var thawDate = dijit.byId('holds_frozen_thaw_input').attr('value');
987         if(thawDate) {
988             thawDate = dojo.date.stamp.toISOString(thawDate);
989             thawDate = holdsVerifyThawDate(thawDate); 
990             if(thawDate) 
991                 hold.thaw_date(thawDate);
992             else
993                 return;
994         } else {
995             hold.thaw_date(null);
996         }
997     } else {
998         hold.frozen('f');
999         hold.thaw_date(null);
1000     }
1001
1002         //check for alternate hold formats 
1003         var fstring = holdsSetSelectedFormats();
1004         if(fstring) { 
1005                 hold.hold_type('M'); 
1006                 hold.holdable_formats(fstring);
1007                 if (fstring)
1008                         holdArgs.holdable_formats = fstring;
1009                 hold.target(holdArgs.metarecord);
1010         }
1011         return hold;
1012 }
1013         
1014 function holdsPlaceHold(hold, recurse) {
1015         if(!hold) return;
1016         swapCanvas($('check_holds_box'));
1017         holdsCheckPossibility(hold.pickup_lib(), hold, recurse);
1018 }
1019
1020
1021 function holdHandleCreateResponse(r, recurse) {
1022
1023         if(!recurse) {
1024                 var res = r.getResultObject();
1025         var place_anyway = false;
1026                 if(checkILSEvent(res) || res.success != 1) {
1027             if(res.success != 1) {
1028                 if(res.age_protected_copy == 1) {
1029                     // There is at least one copy that *could* fill the hold, if it were not age-protected.
1030                     if( confirm($('hold_age_protected_override').innerHTML) ) {
1031                         place_anyway = true;
1032                     } else {
1033                                 swapCanvas($('holds_box'));
1034                                 return;
1035                     }
1036                 } else if(res.place_unfillable == 1) {
1037                     if( confirm($('hold_place_unfillable_override').innerHTML) ) {
1038                         place_anyway = true;
1039                     } else {
1040                                 swapCanvas($('holds_box'));
1041                                 return;
1042                     }
1043                 }
1044             }
1045             if(!place_anyway) {
1046                         if(res.success != 1) {
1047
1048                     if(!holdArgs.partsSuggestionMade && holdArgs.recordParts && 
1049                             holdArgs.recordParts.length && holdArgs.type == 'T') {
1050                         // T holds on records that have parts are OK, but if the record has no non-part
1051                         // copies, the hold will ultimately fail.  Suggest selecting a part to the user.
1052                         addCSSClass($('holds_parts_selector'), 'parts-warning');
1053                         holdArgs.partsSuggestionMade = true;
1054                         alert($('hold_has_parts').innerHTML);
1055                     } else {
1056                                     alert($('hold_not_allowed').innerHTML);
1057                     }
1058                         } else {
1059                                 if( res.textcode == 'PATRON_BARRED' ) {
1060                                         alertId('hold_failed_patron_barred');
1061                             } else {
1062                                         alert($('hold_not_allowed').innerHTML);
1063                                 }
1064                         }
1065                         swapCanvas($('holds_box'));
1066                             return;
1067             }
1068                 }
1069         r._hold.selection_depth(res.depth);
1070         }       
1071
1072         holdCreateHold(r._recurse, r._hold);
1073 }
1074
1075
1076 function holdCreateHold( recurse, hold ) {
1077         var method = CREATE_HOLD;
1078         if(recurse) method = CREATE_HOLD_OVERRIDE;
1079         var req = new Request( method, holdArgs.requestor.session, hold );
1080         req.request.alertEvent = false;
1081         req.send(true);
1082         var res = req.result();
1083         holdProcessResult(hold, res, recurse);
1084         
1085         showCanvas();
1086
1087         runEvt('common', 'holdUpdated');
1088 }
1089
1090
1091 function holdProcessResult( hold, res, recurse ) {
1092
1093         if( res && res > -1 ) {
1094                 alert($('holds_success').innerHTML);
1095                 holdArgs = null;
1096         if(isXUL() && typeof xulG.opac_hold_placed == 'function')
1097             xulG.opac_hold_placed(res);
1098
1099         } else {
1100
1101                 if( recurse ) {
1102                         alert($('holds_failure').innerHTML);
1103                         return;
1104                 }
1105
1106                 if( grep(res, function(e) { return (e.textcode == 'HOLD_EXISTS'); }) ) {
1107                         if( fetchPermOrgs('HOLD_EXISTS.override')[0] != -1 ) {
1108                                 if( confirm($('hold_dup_exists_override').innerHTML) ) {
1109                                         return holdsPlaceHold(hold, true);
1110                                 }
1111                 return;
1112
1113                         } else {
1114                                 return alert($('hold_dup_exists').innerHTML);
1115                         }
1116                 }
1117
1118                 if( grep(res, function(e) { return (e.textcode == 'HOLD_ITEM_CHECKED_OUT'); }) ) {
1119                         if( fetchPermOrgs('HOLD_ITEM_CHECKED_OUT.override')[0] != -1 ) {
1120                                 if( confirm($('hold_checked_out_override').innerHTML) ) {
1121                                         return holdsPlaceHold(hold, true);
1122                                 }
1123                 return;
1124
1125                         } else {
1126                                 return alert($('hold_checked_out').innerHTML);
1127                         }
1128                 }
1129
1130
1131                 alert($('holds_failure').innerHTML);
1132         }
1133 }
1134
1135
1136 function holdsCancel(holdid, user) {
1137         if(!user) user = G.user;
1138         var req = new Request(CANCEL_HOLD, user.session, holdid, /* Patron via OPAC */ 6);
1139         req.send(true);
1140         return req.result();
1141         runEvt('common', 'holdUpdated');
1142 }
1143
1144 function holdsUpdate(hold, user) {
1145         if(!user) user = G.user;
1146         var req = new Request(UPDATE_HOLD, user.session, hold);
1147         req.send(true);
1148         var x = req.result(); // cause an exception if there is one 
1149         runEvt('common', 'holdUpdated');
1150 }
1151
1152 /* verify that the thaw date is valid and after today */
1153 function holdsVerifyThawDate(dateString, isGreater) {
1154     thawDate = dojo.date.stamp.fromISOString(dateString);
1155     if(thawDate) {
1156         if(isGreater) {
1157             if(dojo.date.compare(thawDate) > 0) {
1158                 return dojo.date.stamp.toISOString(thawDate);
1159             }
1160         } else {
1161             return dojo.date.stamp.toISOString(thawDate);
1162         }
1163     }
1164     return null;
1165 }
1166
1167 function holdsVerifyThawDateUI(element) {
1168     value = dojo.date.stamp.toISOString(dijit.byId(element).getValue());
1169
1170     if(!value) {
1171         removeCSSClass($(element), 'invalid_field');
1172         return;
1173     }
1174
1175     if(!holdsVerifyThawDate(value, true)) {
1176         addCSSClass($(element), 'invalid_field');
1177     } else {
1178         removeCSSClass($(element), 'invalid_field');
1179     }
1180 }
1181