adding prefix and suffix to name display in myopac
[Evergreen.git] / Open-ILS / web / opac / skin / default / js / myopac.js
1
2 attachEvt("common", "run", myOPACInit );
3 //attachEvt("common", "loggedIn", myOPACInit );
4 attachEvt('common','locationUpdated', myopacReload );
5
6 var fleshedUser = null;
7 var fleshedContainers = {};
8 var holdCache = {};
9 var holdStatusCache = {};
10
11
12 function clearNodes( node, keepArray ) {
13         if(!node) return;
14         for( var n in keepArray ) node.removeChild(keepArray[n]);
15         removeChildren(node);
16         for( var n in keepArray ) node.appendChild(keepArray[n]);
17 }
18
19 function myOPACInit() {
20         if(!(G.user && G.user.session)) initLogin();
21         else myOPACChangePage( "summary" );
22
23     $('myopac_holds_thaw_date_input').onkeyup = 
24         function(){holdsVerifyThawDateUI('myopac_holds_thaw_date_input'); }
25     $('myopac_holds_thaw_date_input').onchange = 
26         function(){holdsVerifyThawDateUI('myopac_holds_thaw_date_input'); }
27 }
28
29 function myopacReload() {
30         //swapCanvas($('myopac_reloading'));
31         var a = {};
32         a[PARAM_LOCATION] = getNewSearchLocation();
33         a[PARAM_DEPTH] = getNewSearchDepth();
34         hideMe($('canvas_main'));
35         goTo(buildOPACLink(a, true));
36 }
37
38
39 function myOPACChangePage( page ) {
40         showCanvas();
41
42         var s = $("myopac_summary_td");
43         var c = $("myopac_checked_td");
44         var f = $("myopac_fines_td");
45         var h = $("myopac_holds_td");
46         var p = $("myopac_prefs_td");
47         var b = $('myopac_bookbag_td');
48
49         var ss = $("myopac_summary_div");
50         var cc = $("myopac_checked_div");
51         var ff = $("myopac_fines_div");
52         var hh = $("myopac_holds_div");
53         var pp = $("myopac_prefs_div");
54         var bb = $('myopac_bookbag_div');
55
56         var cls = "myopac_link";
57         var acls = "myopac_link_active";
58
59         hideMe(ss);
60         hideMe(cc); hideMe(ff);
61         hideMe(hh); hideMe(pp);
62         hideMe(bb);
63
64         removeCSSClass(s, acls );
65         removeCSSClass(c, acls );
66         removeCSSClass(f, acls );
67         removeCSSClass(h, acls );
68         removeCSSClass(p, acls );
69         removeCSSClass(b, acls );
70
71         switch( page ) {
72
73                 case "summary": 
74                         unHideMe(ss);
75                         addCSSClass(s, acls );
76                         myOPACShowSummary();
77                         break;
78
79                 case "checked": 
80                         unHideMe(cc);
81                         addCSSClass(c, acls );
82                         myOPACShowChecked();
83                         break;
84
85                 case "holds": 
86                         unHideMe(hh);
87                         addCSSClass(h, acls );
88                         myOPACShowHolds();
89                         break;
90
91                 case "fines": 
92                         unHideMe(ff);
93                         addCSSClass(f, acls );
94                         myOPACShowFines();
95                         break;
96
97                 case "prefs": 
98                         unHideMe(pp);
99                         addCSSClass(p, acls );
100                         myOPACShowPrefs();
101                         break;
102
103                 case 'bookbag':
104                         unHideMe(bb);
105                         addCSSClass(b, acls);
106                         myOPACShowBookbags();
107                         break;
108         }
109 }
110
111 function myOPACShowChecked() {
112         if(checkedDrawn) return;
113         var req = new Request(FETCH_CHECKED_OUT_SUM, G.user.session, G.user.id());      
114         req.callback(myOPACDrawCheckedOutSlim);
115         req.send();
116
117         var nreq = new Request(FETCH_NON_CAT_CIRCS, G.user.session);
118         nreq.callback(myOPACDrawNonCatCircs);
119         nreq.send();
120 }
121
122
123 var checkedRowTemplate;
124 var circsCache = new Array();
125 var checkedDrawn = false;
126
127 function moClearCheckedTable() {
128         var tbody                       = $("myopac_checked_tbody");
129         var loading                     = $("myopac_checked_loading");
130         var none                                = $("myopac_checked_none");
131         clearNodes( tbody, [ loading, none ] );
132 }
133
134 var __can_renew_one = false;
135
136 function myOPACDrawCheckedOutSlim(r) {
137
138         var checked                     = r.getResultObject();
139         var tbody                       = $("myopac_checked_tbody");
140         var loading                     = $("myopac_checked_loading");
141         var none                                = $("myopac_checked_none");
142
143    __can_renew_one = false;
144
145         if(checkedDrawn) return;
146         checkedDrawn = true;
147         if(!checkedRowTemplate) 
148                 checkedRowTemplate = tbody.removeChild($("myopac_checked_row"));
149
150    moClearCheckedTable();
151
152         hideMe(loading); /* remove all children and start over */
153         if(!(checked && (checked.out || checked.overdue))) {
154                 unHideMe(none);
155                 return;
156         }
157
158         for( var i = 0; i < checked.overdue.length; i++ ) {
159                 var req = new Request(FETCH_CIRC_BY_ID, G.user.session, checked.overdue[i]);
160                 req.request.tbody = tbody;
161                 req.request.od = true;
162                 req.callback(myOPACDrawCheckedItem);
163                 req.send();
164         }
165
166
167         for( var i = 0; i < checked.out.length; i++ ) {
168                 var req = new Request(FETCH_CIRC_BY_ID, G.user.session, checked.out[i]);
169                 req.request.tbody = tbody;
170                 req.callback(myOPACDrawCheckedItem);
171                 req.send();
172         }
173
174    appendClear($('mo_items_out_count'), 
175       text(new String( parseInt(checked.overdue.length) + parseInt(checked.out.length) )) );
176
177    if( checked.overdue.length > 0 ) {
178       addCSSClass($('mo_items_overdue_count'), 'overdue');
179       appendClear($('mo_items_overdue_count'),
180          text(new String( parseInt(checked.overdue.length) )) );
181    }
182
183 }
184
185
186 function myOPACDrawCheckedItem(r) {
187
188         var circ = r.getResultObject();
189         var tbody = r.tbody;
190         var row = checkedRowTemplate.cloneNode(true);
191         row.id = 'myopac_checked_row_ ' + circ.id();
192    row.setAttribute('circid', circ.id());
193
194         var due = _trimTime(circ.due_date());
195
196         var dlink = $n( row, "myopac_checked_due" );
197         var rlink = $n( row, "myopac_checked_renewals" );
198         //var rnlink = $n( row, "myopac_checked_renew_link" );
199
200         //if( r.od ) due = elem('b', {style:'color:red;font-size:110%'},due);
201         if( r.od ) {
202       due = elem('b', null, due);
203       addCSSClass(due, 'overdue');
204    } else {
205       due = text(due);
206    }
207
208         dlink.appendChild(due);
209         rlink.appendChild(text(circ.renewal_remaining()));
210         unHideMe(row);
211         //rnlink.setAttribute('href', 'javascript:myOPACRenewCirc("'+circ.id()+'");');
212         circsCache.push(circ);
213
214         if( circ.renewal_remaining() < 1 ) {
215       $n(row, 'selectme').disabled = true;
216       if(!__can_renew_one)
217          $('mo_renew_button').disabled = true;
218    } else {
219       __can_renew_one = true;
220       $('mo_renew_button').disabled = false;
221       $n(row, 'selectme').disabled = false;
222    }
223
224         tbody.appendChild(row);
225
226         var req = new Request(FETCH_MODS_FROM_COPY, circ.target_copy() );
227         req.request.alertEvent = false;
228         req.request.circ = circ.id();
229         req.request.copy = circ.target_copy();
230         req.callback(myOPACDrawCheckedTitle);
231         req.send();
232 }
233
234 var __circ_titles = {};
235
236 function myOPACDrawCheckedTitle(r) {
237         var record = r.getResultObject();
238         var circid = r.circ;
239
240         if(!record || checkILSEvent(record)) {
241                 var req = new Request( FETCH_COPY, r.copy );
242                 req.request.circ = circid
243                 req.callback(myOPACDrawNonCatalogedItem);
244                 req.send();
245                 return;
246         }
247
248         var row = $('myopac_checked_row_ ' + circid);
249         var tlink = $n( row, "myopac_checked_title_link" );
250         var alink = $n( row, "myopac_checked_author_link" );
251         buildTitleDetailLink(record, tlink);
252         buildSearchLink(STYPE_AUTHOR, record.author(), alink);
253    __circ_titles[circid] = record.title();
254 }
255
256 function myOPACDrawNonCatalogedItem(r) {
257         var copy = r.getResultObject();
258         var circid = r.circ;
259
260         var row = $('myopac_checked_row_ ' + circid);
261         var tlink = $n( row, "myopac_checked_title_link" );
262         var alink = $n( row, "myopac_checked_author_link" );
263
264         tlink.parentNode.appendChild(text(copy.dummy_title()));
265         alink.parentNode.appendChild(text(copy.dummy_author()));
266    __circ_titles[circid] = copy.dummy_title();
267 }
268
269
270 /*
271 function myOPACRenewCirc(circid) {
272
273         var circ;
274         for( var i = 0; i != circsCache.length; i++ ) 
275                 if(circsCache[i].id() == circid)
276                         circ = circsCache[i];
277
278         if(!confirm($('myopac_renew_confirm').innerHTML)) return;
279
280         var req = new Request(RENEW_CIRC, G.user.session, 
281                 { patron : G.user.id(), copyid : circ.target_copy(), opac_renewal : 1 } );
282         req.request.alertEvent = false;
283         req.send(true);
284         var res = req.result();
285
286         if(checkILSEvent(res) || checkILSEvent(res[0])) {
287                 alertId('myopac_renew_fail');
288                 return;
289         }
290
291         alert($('myopac_renew_success').innerHTML);     
292         checkedDrawn = false;
293         myOPACShowChecked();
294 }
295 */
296
297
298
299 function myOPACShowHolds() {
300         var req = new Request(FETCH_HOLDS, G.user.session, G.user.id());        
301         req.callback(myOPACDrawHolds);
302         req.send();
303     $('myopac_holds_actions_none').selected = true;
304 }
305
306 var holdsTemplateRowOrig;
307 var holdsTemplateRow;
308 var myopacForceHoldsRedraw = false;
309 function myOPACDrawHolds(r) {
310
311         var tbody = $("myopac_holds_tbody");
312         if(holdsTemplateRow && !myopacForceHoldsRedraw) return;
313     myopacForceHoldsRedraw = false;
314
315         if(holdsTemplateRowOrig) {
316                 holdsTemplateRow = holdsTemplateRowOrig;
317                 removeChildren(tbody);
318         } else {
319                 holdsTemplateRow = tbody.removeChild($("myopac_holds_row"));
320                 holdsTemplateRowOrig = holdsTemplateRow;
321         }
322
323         hideMe($('myopac_holds_loading'));
324
325         var holds = r.getResultObject();
326
327         if(!holds || holds.length < 1) unHideMe($('myopac_holds_none'));
328         for( var i = 0; i != holds.length; i++ ) {
329
330                 var h = holds[i];
331                 holdCache[h.id()] = h;
332
333                 var row = holdsTemplateRow.cloneNode(true);
334                 row.id = "myopac_holds_row_" + h.id();
335
336                 var form = $n(row, "myopac_holds_formats");
337                 form.id = "myopac_holds_form_" + h.id();
338
339                 var orglink = $n(row, "myopac_holds_location");
340                 orglink.appendChild(text(findOrgUnit(h.pickup_lib()).name()));
341
342                 tbody.appendChild(row);
343
344                 $n(row,'myopac_holds_edit_link').setAttribute(
345                         'href','javascript:myOPACEditHold("'+ h.id()+'");'); 
346
347         if(isTrue(h.frozen())) {
348             hideMe($n(row, 'myopac_hold_unfrozen_true'))
349             unHideMe($n(row, 'myopac_hold_unfrozen_false'))
350             if(h.thaw_date()) {
351                 var d = dojo.date.stamp.fromISOString(h.thaw_date());
352                 $n(row, 'myopac_holds_frozen_until').appendChild(text(dojo.date.locale.format(d, {selector: 'date', fullYear: true})));
353             }
354         } else {
355             unHideMe($n(row, 'myopac_hold_unfrozen_true'))
356             hideMe($n(row, 'myopac_hold_unfrozen_false'))
357         }
358
359         $n(row, 'myopac_holds_selected_chkbx').checked = false;
360
361         var exp_date = dojo.date.stamp.fromISOString(h.expire_time());
362         $n(row, 'myopac_hold_expire_time').appendChild(
363             text(dojo.date.locale.format(exp_date, {selector:'date'})));
364
365                 unHideMe(row);
366
367         var interval = fetchOrgSettingDefault(G.user.home_ou(), 'circ.hold_expire_alert_interval');
368         if(interval) {
369             secs = interval_to_seconds(interval) * 1000;
370             var diff = exp_date.getTime() - new Date().getTime();
371             if(diff < secs)
372                 addCSSClass($n(row, 'myopac_hold_expire_time'), 'hold_expire_warning');
373         }
374
375         myOPACDrawHoldTitle(h);
376         myOPACDrawHoldStatus(h);
377     }
378 }
379
380 function myOPACEditHold(holdid) {
381         var hold = holdCache[holdid];
382
383         holdsDrawEditor( 
384                 { 
385                         editHold : hold,
386                         onComplete : function(){ 
387                                 holdsTemplateRow = null;
388                                 myOPACShowHolds(); 
389                         }
390                 }
391         );
392 }
393
394
395 function myOPACDrawHoldStatus(hold) {
396         var req = new Request(FETCH_HOLD_STATUS, G.user.session, hold.id() );
397         req.callback(myOShowHoldStatus);
398         req.request.hold = hold;
399         req.send();
400 }
401
402 function myOShowHoldStatus(r) {
403
404         var hold = r.hold;
405         var status = r.getResultObject();
406     holdStatusCache[hold.id()] = status;
407
408         var row = $("myopac_holds_row_" + r.hold.id());
409
410         if( status < 3 )
411                 unHideMe($n(row, 'hold_status_waiting'));
412
413         if( status == 3 )
414                 unHideMe($n(row, 'hold_status_transit'));
415
416         if( status == 4 ) {
417                 unHideMe($n(row, 'hold_status_available'));
418                 hideMe($n(row, 'myopac_holds_cancel_link'));
419         }
420 }
421
422
423 function myOPACDrawHoldTitle(hold) {
424         var method;
425
426         if( hold.hold_type() == 'T' || hold.hold_type() == 'M' ) {
427                 if(hold.hold_type() == "M") method = FETCH_MRMODS;
428                 if(hold.hold_type() == "T") method = FETCH_RMODS;
429                 var req = new Request(method, hold.target());
430                 req.callback(myOPACFleshHoldTitle);
431                 req.request.hold = hold;
432                 req.request.alertEvent = false;
433                 req.send();
434
435         } else {
436                 holdFetchObjects(hold, 
437                         function(a) { _myOPACFleshHoldTitle(hold, a);});
438         }
439 }
440
441 function myOPACFleshHoldTitle(r) {
442         var rec = r.getResultObject();
443         _myOPACFleshHoldTitle(r.hold, {recordObject: rec});
444 }
445
446 function _myOPACFleshHoldTitle(hold, holdObjects) {
447
448         var record = holdObjects.recordObject;
449         var volume      = holdObjects.volumeObject;
450         var copy        = holdObjects.copyObject;
451
452         var row = $("myopac_holds_row_" + hold.id());
453         var title_link = $n(row, "myopac_holds_title_link");
454         var author_link = $n(row, "myopac_holds_author_link");
455
456         if(!record || checkILSEvent(record) ) {
457                 addCSSClass(row, 'invalid_hold');
458                 $n(row, 'myopac_holds_edit_link').setAttribute('href', 'javascript:void(0);');
459                 $n(row, 'myopac_holds_edit_link').onclick = function(){alertId('invalid_hold');};
460                 return;
461         }
462
463         buildTitleDetailLink(record, title_link);
464         buildSearchLink(STYPE_AUTHOR, record.author(), author_link);
465
466         if( volume ) {
467                 $n(row, 'volume').appendChild(text(volume.label()));
468                 unHideMe($n(row, 'vol_copy'));
469                 if(copy) $n(row, 'copy').appendChild(text(copy.barcode()));
470         }
471
472         var form = $("myopac_holds_form_" + hold.id());
473
474         if(form) {
475                 var mods_formats = record.types_of_resource();
476
477                 if( hold.hold_type() == 'M' ) {
478                         var data = holdsParseMRFormats(hold.holdable_formats());
479                         mods_formats = data.mods_formats;
480                 }
481
482                 for( var i = 0; i < mods_formats.length; i++ ) {
483                         var img = elem("img");
484                         setResourcePic(img, mods_formats[i]);
485                         form.appendChild(img);
486                 }
487         }
488 }
489
490 var finesShown = false;
491 function myOPACShowFines() {
492         if(finesShown) return; finesShown = true;
493         var req = new Request(FETCH_FINES_SUMMARY, G.user.session, G.user.id() );
494         req.callback(_myOPACShowFines);
495         req.send();
496 }
497
498 function _myOPACShowFines(r) {
499         hideMe($('myopac_fines_summary_loading'));
500         unHideMe($('myopac_fines_summary_row'));
501
502         var summary = r.getResultObject();
503         var total       = "0.00"; /* localization? */
504         var paid                = "0.00";
505         var balance = "0.00";
506         if( instanceOf(summary,mous) ) {
507
508                 total           = _finesFormatNumber(summary.total_owed());
509                 paid            = _finesFormatNumber(summary.total_paid());
510                 balance = _finesFormatNumber(summary.balance_owed());
511
512                 var req = new Request(FETCH_TRANSACTIONS, G.user.session, G.user.id() );
513                 req.callback(myOPACShowTransactions);
514                 req.send();
515         }
516
517         $('myopac_fines_summary_total').appendChild(text(total));
518         $('myopac_fines_summary_paid').appendChild(text(paid));
519         $('myopac_fines_summary_balance').appendChild(text(balance));
520 }
521
522 function _finesFormatNumber(num) {
523         if(isNull(num)) num = 0;
524         num = num + "";
525         if(num.length < 2 || !num.match(/\./)) num += ".00";
526         if(num.match(/\./) && num.charAt(num.length-2) == '.') num += "0";
527         return num;
528 }          
529
530 //function _trimTime(time) { if(!time) return ""; return time.replace(/\ .*/,""); }
531 function _trimTime(time) { 
532         if(!time) return ""; 
533     var d = dojo.date.stamp.fromISOString(time);
534     if(!d) return ""; /* date parse failed */
535     return d.iso8601Format('YMD');
536 }
537
538 function _trimSeconds(time) { 
539     if(!time) return ""; 
540     var d = dojo.date.stamp.fromISOString(time);
541     if(!d) return ""; /* date parse failed */
542     return d.iso8601Format('YMDHM',null,true,true);
543 }
544
545 function myOPACShowTransactions(r) {
546
547         if(myopacGenericTransTemplate || myopacCircTransTemplate) return;
548
549         var transactions = r.getResultObject();
550
551         for( var idx in transactions ) {
552
553                 var trans       = transactions[idx].transaction;
554                 var record      = transactions[idx].record;
555                 var circ                = transactions[idx].circ;
556
557                 if(trans.xact_type() == 'circulation') 
558                         myOPACShowCircTransaction(trans, record, circ);
559
560                 else if(trans.xact_type() == 'grocery' ) 
561                         myopacShowGenericTransaction( trans );
562         }
563 }
564
565 var myopacGenericTransTemplate;
566 function myopacShowGenericTransaction( trans ) {
567         var tbody = $('myopac_trans_tbody');
568
569         if(!myopacGenericTransTemplate) {
570                 myopacGenericTransTemplate = 
571                         tbody.removeChild($('myopac_trans_row'));
572                 removeChildren(tbody);
573         }
574
575         var row = myopacGenericTransTemplate.cloneNode(true);
576
577         $n(row,'myopac_trans_start').appendChild(
578                         text(_trimSeconds(trans.xact_start())));
579
580         $n(row,'myopac_trans_last_payment').appendChild(
581                         text(_trimSeconds(trans.last_payment_ts())));
582
583         $n(row,'myopac_trans_init_amount').appendChild(
584                         text(_finesFormatNumber(trans.total_owed())));
585
586         $n(row,'myopac_trans_total_paid').appendChild(
587                         text(_finesFormatNumber(trans.total_paid())));
588
589         $n(row,'myopac_trans_balance').appendChild(
590                         text(_finesFormatNumber(trans.balance_owed())));
591
592         var req = new Request(FETCH_MONEY_BILLING, G.user.session, trans.id());
593         req.send(true);
594         var bills = req.result();
595         if(bills && bills[0])
596                 $n(row,'myopac_trans_bill_type').appendChild(
597                                 text(bills[0].billing_type()));
598
599         tbody.appendChild(row);
600         unHideMe($('myopac_trans_div'));
601 }
602
603
604
605 /* draws a circulation transaction summary */
606 var myopacCircTransTemplate;
607 function myOPACShowCircTransaction(trans, record, circ) {
608         var tbody = $('myopac_circ_trans_tbody');
609
610         if(!myopacCircTransTemplate) {
611                 myopacCircTransTemplate = tbody.removeChild($('myopac_circ_trans_row'));
612                 removeChildren(tbody);
613         }
614
615         var row = myopacCircTransTemplate.cloneNode(true);
616
617         if(record) {
618                 buildTitleDetailLink(record, $n(row,'myopac_circ_trans_title'));
619
620                 $n(row,'myopac_circ_trans_author').appendChild(text(
621                         normalize(truncate(record.author(), 65))));
622
623         } else {
624
625                 var req = new Request( FETCH_COPY, circ.target_copy() );
626                 req.alertEvents = false;
627                 req.send(true);
628                 var copy = req.result();
629                 if( copy ) {
630                         $n(row,'myopac_circ_trans_title').appendChild(text(copy.dummy_title()));
631                         $n(row,'myopac_circ_trans_author').appendChild(text(copy.dummy_author()));
632                 }
633         }
634
635
636         $n(row,'myopac_circ_trans_start').
637                 appendChild(text(_trimTime(trans.xact_start())));
638
639    var due = _trimTime(circ.due_date());
640         var checkin = _trimTime(circ.stop_fines_time());
641
642         $n(row,'myopac_circ_trans_due').appendChild(text(due))
643         if(checkin)
644                 appendClear($n(row,'myopac_circ_trans_finished'), text(checkin));
645         if(circ.stop_fines() == 'LOST')
646                 appendClear($n(row,'myopac_circ_trans_finished'), text(circ.stop_fines()));
647         if(circ.stop_fines() == 'CLAIMSRETURNED')
648                 appendClear($n(row,'myopac_circ_trans_finished'), text(""));
649
650
651         $n(row,'myopac_circ_trans_balance').
652                 appendChild(text(_finesFormatNumber(trans.balance_owed())));
653
654         tbody.appendChild(row);
655         unHideMe($('myopac_circ_trans_div'));
656 }
657
658
659 function myOPACSavePrefs() {
660         G.user.prefs[PREF_HITS_PER] = getSelectorVal($('prefs_hits_per'));
661         G.user.prefs[PREF_DEF_FONT] = getSelectorVal($('prefs_def_font'));
662         G.user.prefs[PREF_HOLD_NOTIFY] = getSelectorVal($('prefs_hold_notify'));
663         G.user.prefs[PREF_DEF_DEPTH] = getSelectorVal($('prefs_def_range'));
664
665         if( $('myopac_pref_home_lib').checked == true )
666                 G.user.prefs[PREF_DEF_LOCATION] = null;
667         else
668                 G.user.prefs[PREF_DEF_LOCATION] = getSelectorVal($('prefs_def_location'));
669
670         if(commitUserPrefs())
671                 alert($('prefs_update_success').innerHTML);
672         else alert($('prefs_update_failure').innerHTML);
673 }
674
675
676 function myOPACShowDefFont() {
677         var font;
678         if(G.user.prefs[PREF_DEF_FONT])
679                 font = G.user.prefs[PREF_DEF_FONT];
680         else font = "regular";
681         setSelector($('prefs_def_font'), font);
682 }
683
684 function myOPACShowHoldNotify() {
685         var pref = G.user.prefs[PREF_HOLD_NOTIFY];
686
687         if(pref) {
688                 if(pref.match(/email/i) && pref.match(/phone/i)) {
689                         setSelector($('prefs_hold_notify'), 'phone:email');
690                 } else if( pref.match(/email/i) ) {
691                         setSelector($('prefs_hold_notify'), 'email');
692                 } else if( pref.match(/phone/i) ) {
693                         setSelector($('prefs_hold_notify'), 'phone');
694                 }
695
696         } else {
697                 setSelector($('prefs_hold_notify'), 'phone:email');
698         }
699 }
700
701 function myOPACShowPrefs() {
702         grabUserPrefs();
703         myOPACShowHitsPer();
704         myOPACShowDefFont();
705         myOPACShowHoldNotify();
706         myOPACShowDefLocation();
707         hideMe($('myopac_prefs_loading'));
708 }
709
710 var defSearchLocationDrawn = false;
711 var defDepthIndex = 0;
712 function myOPACShowDefLocation() {
713
714         var selector = $('prefs_def_location');
715         var rsel = $('prefs_def_range');
716
717         if(!defSearchLocationDrawn) {
718
719                 defSearchLocationDrawn = true;
720
721                 var org = G.user.prefs[PREF_DEF_LOCATION];
722
723                 if(!org) {
724                         $('myopac_pref_home_lib').checked = true;
725                         $('prefs_def_location').disabled = true;
726                         org = G.user.home_ou();
727                 }
728
729                 buildOrgSel(selector, globalOrgTree, 0);
730
731                 globalOrgTypes = globalOrgTypes.sort(
732                         function(a, b) {
733                                 if( a.depth() < b.depth() ) return -1;
734                                 return 1;
735                         }
736                 );
737
738                 iterate(globalOrgTypes,
739                         function(t) {
740                                 if( t.depth() <= findOrgDepth(org) ) {
741                                         setSelectorVal(rsel, defDepthIndex++, t.opac_label(), t.depth());
742                                         if( t.depth() == findOrgDepth(org) ) 
743                                                 setSelector(rsel, t.depth());
744                                 }
745                         }
746                 );
747         }
748
749         setSelector(selector, org);
750 }
751
752 function myOPACShowHitsPer() {
753         var hits = 10;
754         if(G.user.prefs[PREF_HITS_PER])
755                 hits = G.user.prefs[PREF_HITS_PER];
756         var hitsSel = $('prefs_hits_per');
757         setSelector(hitsSel, hits);
758 }
759
760 var userShown = false;
761 function myOPACShowSummary() {
762         if(userShown) return; userShown = true;
763         var req = new Request(FETCH_FLESHED_USER,G.user.session, G.user.id());
764         req.callback(_myOPACSummaryShowUer);
765         req.send();
766 }
767
768 var addrRowTemplate;
769 var notesTemplate;
770 function _myOPACSummaryShowUer(r) {
771
772         var user = r.getResultObject();
773         fleshedUser = user;
774         if(!user) return;
775
776     var expireDate = dojo.date.stamp.fromISOString(user.expire_date());
777     if( expireDate < new Date() ) {
778         appendClear($('myopac.expired.date'), expireDate.iso8601Format('YMD'));
779         unHideMe($('myopac.expired.alert'));
780     }
781
782         var iv1 = user.ident_value()+'';
783         if (iv1.length > 4) iv1 = iv1.replace(new RegExp(iv1.substring(0,iv1.length - 4)), '***********');
784
785         appendClear($('myopac_summary_prefix'),text(user.first_given_name()));
786         appendClear($('myopac_summary_first'),text(user.first_given_name()));
787         appendClear($('myopac_summary_middle'),text(user.second_given_name()));
788         appendClear($('myopac_summary_dayphone'),text(user.day_phone()));
789         appendClear($('myopac_summary_eveningphone'),text(user.evening_phone()));
790         appendClear($('myopac_summary_otherphone'),text(user.other_phone()));
791         appendClear($('myopac_summary_last'),text(user.family_name()));
792         if (user.suffix()) appendClear($('myopac_summary_suffix'),text(', ' + user.suffix()));
793         appendClear($('myopac_summary_username'),text(user.usrname()));
794         appendClear($('myopac_summary_email'),text(user.email()));
795         appendClear($('myopac_summary_barcode'),text(user.card().barcode()));
796         appendClear($('myopac_summary_ident1'),text(iv1));
797         appendClear($('myopac_summary_homelib'),text(findOrgUnit(user.home_ou()).name()));
798         appendClear($('myopac_summary_create_date'),text(_trimTime(user.create_date())));
799
800         var req = new Request( 
801                 FETCH_USER_NOTES, G.user.session, {pub:1, patronid:G.user.id()});
802         req.callback(myopacDrawNotes);
803         req.send();
804
805
806         var tbody = $('myopac_addr_tbody');
807         var template;
808
809         if(addrRowTemplate) { 
810                 template = addrRowTemplate;
811         } else {
812                 template = tbody.removeChild($('myopac_addr_row'));
813                 addrRowTemplate = template;
814         }
815         removeChildren(tbody);
816
817         for( var a in user.addresses() ) {
818                 var row = template.cloneNode(true);
819                 myOPACDrawAddr(row, user.addresses()[a]);
820                 tbody.appendChild(row);
821         }
822 }
823
824
825 function myopacDrawNotes(r) {
826         var notes = r.getResultObject();
827         var tbody = $('myopac.notes.tbody');
828         if(!notesTemplate)
829                 notesTemplate = tbody.removeChild($('myopac.notes.tr'));
830         removeChildren(tbody);
831
832         iterate(notes, 
833                 function(note) {
834                         unHideMe($('myopac.notes.div'));
835                         var row = notesTemplate.cloneNode(true);
836                         $n(row, 'title').appendChild(text(note.title()));
837                         $n(row, 'value').appendChild(text(note.value()));
838                         tbody.appendChild(row);
839                 }
840         );
841 }
842
843
844
845 function myOPACDrawAddr(row, addr) {
846
847         appendClear($n(row, 'myopac_addr_type'),text(addr.address_type()));
848         var street = (addr.street2()) ? addr.street1() + ", " + addr.street2() : addr.street1();
849         appendClear($n(row, 'myopac_addr_street'),text(street));
850         appendClear($n(row, 'myopac_addr_city'),text(addr.city()));
851         appendClear($n(row, 'myopac_addr_county'),text(addr.county()));
852         appendClear($n(row, 'myopac_addr_state'),text(addr.state()));
853         appendClear($n(row, 'myopac_addr_zip'),text(addr.post_code()));
854 }
855
856
857 function myOPACUpdateUsername() {
858         var username = $('myopac_new_username').value;
859         if(username == null || username == "") {
860                 alert($('myopac_username_error').innerHTML);
861                 return;
862         }
863
864         if( username.match(/.*\s.*/) ) {
865                 alert($('myopac_invalid_username').innerHTML);
866                 return;
867         }
868
869     r = fetchOrgSettingDefault(globalOrgTree.id(), 'opac.barcode_regex');
870     if(r) REGEX_BARCODE = new RegExp(r);
871
872     if(username.match(REGEX_BARCODE)) {
873         alert($('myopac_invalid_username').innerHTML);
874         return;
875     }
876
877         /* first see if the requested username is taken */
878         var req = new Request(CHECK_USERNAME, G.user.session, username);
879         req.send(true);
880         var res = req.result();
881         /* If the username does not already exist, res will be null;
882          * we can move on to updating the username.
883          * 
884          * If the username does exist, then res will be the user ID.
885          * G.user.id() gives us the currently authenticated user ID.
886          * If res == G.user.id(), we try to update the username anyways.
887          */
888         if( res !== null && res != G.user.id() ) {
889                 alertId('myopac_username_dup');
890                 return;
891         }
892
893         var req = new Request(UPDATE_USERNAME, G.user.session, username );
894         req.send(true);
895         if(req.result()) {
896
897                 var evt;
898                 var res = req.result();
899                 if(evt = checkILSEvent(res)) {
900                         alertILSEvent(res);
901                         return;
902                 }
903
904                 G.user.usrname(username);
905                 hideMe($('myopac_update_username_row'));
906                 userShown = false;
907                 alertId('myopac_username_success');
908                 myOPACShowSummary();
909                 return;
910         }
911
912         alert($('myopac_username_failure').innerHTML);
913 }
914
915 function myOPACUpdateEmail() {
916         var email = $('myopac_new_email').value;
917         if(email == null || email == "") {
918                 alert($('myopac_email_error').innerHTML);
919                 return;
920         }
921
922         var req = new Request(UPDATE_EMAIL, G.user.session, email );
923         req.send(true);
924         if(req.result()) {
925                 G.user.usrname(email);
926                 hideMe($('myopac_update_email_row'));
927                 userShown = false;
928                 alertId('myopac_email_success');
929                 myOPACShowSummary();
930                 return;
931         }
932
933         alert($('myopac_email_failure').innerHTML);
934 }
935
936
937 function myOPACUpdatePassword() {
938         var curpassword = $('myopac_current_password').value;
939         var password = $('myopac_new_password').value;
940         var password2 = $('myopac_new_password2').value;
941
942         if(     curpassword == null || curpassword == "" || 
943                         password == null || password == "" || 
944                         password2 == null || password2 == "" || password != password2 ) {
945                 alert($('myopac_password_error').innerHTML);
946                 return;
947         }
948
949         if(!strongPassword(password, true)) return;
950
951         var req = new Request(UPDATE_PASSWORD, G.user.session, password, curpassword );
952         req.send(true);
953         if(req.result()) {
954                 hideMe($('myopac_update_password_row'));
955                 userShown = false;
956                 alertId('myopac_password_success');
957                 myOPACShowSummary();
958                 return;
959         }
960
961         alert($('myopac_password_failure').innerHTML);
962 }
963
964
965
966
967 var containerTemplate;
968 function myOPACShowBookbags(force) {
969
970         var tbody =$('myopac_bookbag_tbody') ;
971
972         if(!containerTemplate) 
973                 containerTemplate = tbody.removeChild($('myopac_bookbag_tr'));
974         else if(!force) return;
975
976         removeChildren(tbody);
977
978         var containers = containerFetchAll();
979
980         var found = false;
981         for( var i in containers ) {
982                 found = true;
983                 var cont = containers[i];
984                 var row = containerTemplate.cloneNode(true);
985                 row.id = 'myopac_bookbag_row_' + cont.id();
986                 var link = $n(row, 'myopac_expand_bookbag');
987                 var dlink = $n(row, 'myopac_container_delete');
988                 link.appendChild( text(cont.name()) );
989                 link.setAttribute('href', 
990                         'javascript:myOPACExpandBookbag("' + cont.id() + '","' + cont.name() + '");');
991                 myOPACFetchBBItems( cont.id(), row );
992                 dlink.setAttribute('href', 'javascript:myOPACDeleteBookbag("'+cont.id()+'");');
993
994                 if( isTrue(cont.pub()) ) {
995                         unHideMe($n(row, 'myopac_bb_published_yes'));
996                         var link = $n(row, 'myopac_bb_published_view');
997                         link.setAttribute('href', buildExtrasLink( 'feed/bookbag/html-full/'+cont.id(), false));  
998                         link.setAttribute('target', '_blank' );
999                         unHideMe(link);
1000
1001                         link = $n(row, 'myopac_bb_published_atom');
1002                         link.setAttribute('href', buildExtrasLink( 'feed/bookbag/rss2-full/'+cont.id(), false));  
1003                         link.setAttribute('target', '_blank' );
1004                         unHideMe(link);
1005
1006                         link = $n(row, 'myopac_bb_make_unpublished');
1007                         link.setAttribute('href', 'javascript:myOPACMakeBBPublished("'+cont.id()+'", true);');
1008                         unHideMe(link);
1009
1010                 } else { 
1011                         unHideMe($n(row, 'myopac_bb_published_no')); 
1012                         var link = $n(row, 'myopac_bb_make_published');
1013                         link.setAttribute('href', 'javascript:myOPACMakeBBPublished("'+cont.id()+'");');
1014                         unHideMe(link);
1015                 }
1016
1017                 tbody.appendChild(row); 
1018         }
1019
1020         if(!found) unHideMe($('myopac_bookbags_none'));
1021         else unHideMe($('myopac_bookbag_table'));       
1022 }
1023
1024 function myOPACMakeBBPublished(bbid, hideme) {
1025
1026         var bb = fleshedContainers[bbid];
1027
1028         if(hideme) {
1029                 if(!confirm($('myopac_make_unpublished_confirm').innerHTML)) return;
1030                 bb.pub('f');
1031         } else {
1032                 if(!confirm($('myopac_make_published_confirm').innerHTML)) return;
1033                 bb.pub('t');
1034         }
1035
1036         var result = containerUpdate(bb);
1037
1038         var code = checkILSEvent(result);
1039         if(code) { alertILSEvent(result); return; }
1040
1041         alert($('myopac_bb_update_success').innerHTML);
1042         myOPACShowBookbags(true);
1043 }
1044
1045
1046
1047 function myOPACDeleteBookbag(id) {
1048         if( confirm( $('myopac_delete_bookbag_warn').innerHTML ) ) {
1049                 var result = containerDelete(id);
1050                 var code = checkILSEvent(result);
1051                 if(code) { alertILSEvent(result); return; }
1052                 alert($('myopac_bb_update_success').innerHTML);
1053                 hideMe($('myopac_bookbag_items_table'));
1054                 hideMe($('myopac_bookbag_items_name'));
1055                 hideMe($('myopac_bookbag_no_items'));
1056                 myOPACShowBookbags(true);
1057         }
1058 }
1059
1060 function myOPACFetchBBItems( id, row, block ) {
1061         if(!block) {
1062                 containerFlesh( id, _myOPACSetBBItems, { row: row }  );
1063         } else {
1064                 var cont = containerFlesh(id);
1065                 myOPACSetBBItems( cont, row );
1066         }
1067 }
1068
1069 function _myOPACSetBBItems(r) { myOPACSetBBItems( r.getResultObject(), r.args.row ); }
1070
1071 function myOPACSetBBItems( container, row ) {
1072         fleshedContainers[container.id()] = container;
1073         var node = $n(row, 'myopac_bookbag_item_count');
1074         removeChildren(node);
1075         node.appendChild( text(container.items().length) );
1076 }
1077
1078 var BBItemsRow;
1079 function myOPACExpandBookbag( id, name ) {
1080         
1081         var tbody = $('myopac_bookbag_items_tbody');
1082         if(!BBItemsRow) BBItemsRow = tbody.removeChild($('myopac_bookbag_items_row'));
1083         removeChildren(tbody);
1084         removeChildren($('myopac_bookbag_items_name'));
1085
1086         $('myopac_bookbag_items_name').appendChild(text(name));
1087
1088         if( fleshedContainers[id] ) {
1089                 var len = fleshedContainers[id].items().length;
1090
1091                 if( len == 0 ) {
1092                         unHideMe($('myopac_bookbag_no_items'));
1093                         hideMe($('myopac_bookbag_items_table'));
1094                         return;
1095                 }
1096
1097                 hideMe($('myopac_bookbag_no_items'));
1098                 unHideMe($('myopac_bookbag_items_table'));
1099
1100                 for( var i = 0; i != len; i++ ) {
1101                         var row = BBItemsRow.cloneNode(true);
1102                         found = true;
1103
1104                         var item = fleshedContainers[id].items()[i];
1105                         var tlink = $n(row,'myopac_bookbag_items_title');
1106                         var alink = $n(row,'myopac_bookbag_items_author');
1107
1108                         var req = new Request( FETCH_RMODS, item.target_biblio_record_entry() );
1109                         req.request.tlink = tlink;
1110                         req.request.alink = alink;
1111                         req.callback(myOPACShowBBItem);
1112                         req.send();
1113
1114                         var clink = $n(row, 'myopac_bookbag_items_remove');
1115                         clink.setAttribute('href', 'javascript:myOPACRemoveBBItem("'+item.id()+'","'+id+'","'+name+'");');
1116
1117                         tbody.appendChild(row);
1118                 }
1119         }
1120 }
1121
1122 function myOPACRemoveBBItem( id, containerid, container_name ) {
1123         if(!confirm($('myopac_remove_bb_item_confirm').innerHTML)) return;
1124         var stat = containerRemoveItem( id );
1125         if(stat) alert($('myopac_bb_update_success').innerHTML);
1126         myOPACFetchBBItems( containerid, $('myopac_bookbag_row_' + containerid), true);
1127         myOPACExpandBookbag( containerid, container_name );
1128 }
1129
1130 function myOPACShowBBItem(r) {
1131         var record = r.getResultObject();
1132         buildTitleDetailLink(record, r.tlink);
1133         buildSearchLink(STYPE_AUTHOR, record.author(), r.alink);
1134 }
1135
1136 function myOPACCreateBookbag() {
1137         var name = $('myopac_bookbag_new_name').value;  
1138         if(!name) return;
1139
1140         var exists = false;
1141         for( var c in fleshedContainers ) { exists = true; break; }
1142
1143         /* let them know what they are getting into... */
1144         if(!exists) if(!confirm($('bb_create_warning').innerHTML)) return;
1145
1146         var result = containerCreate( name, $('bb_public_yes').checked );
1147         var code = checkILSEvent(result);
1148         if(code) { alertILSEvent(result); return; }
1149         if(result) alert($('myopac_bb_update_success').innerHTML);
1150         myOPACShowBookbags(true);
1151 }
1152
1153
1154 /* ---------------------------------------------------------------------- */
1155 /* Non cat circs */
1156 /* ---------------------------------------------------------------------- */
1157
1158 var nonCatCircIds;
1159 var nonCatTypes;
1160 /* if we have some circs, grab the non-cat types */
1161 function myOPACDrawNonCatCircs(r) {
1162         var ids = r.getResultObject();
1163         if(ids.length == 0) return;
1164         nonCatCircIds = ids;
1165         unHideMe($('non_cat_circs_div'));
1166         var req = new Request(FETCH_NON_CAT_TYPES, G.user.home_ou());
1167         req.callback(myOPACDrawNonCatCircs2);
1168         req.send();
1169 }
1170
1171
1172 /* now we have circs and the types.. draw each one */
1173 var nonCatTbody;
1174 var nonCatRow;
1175 function myOPACDrawNonCatCircs2(r) {
1176         nonCatTypes = r.getResultObject();
1177         nonCatTbody = $('non_cat_circs_tbody');
1178         if(!nonCatRow) nonCatRow = 
1179                 nonCatTbody.removeChild($('non_cat_circs_row'));
1180         removeChildren(nonCatTbody);
1181         for( var i in nonCatCircIds ) {
1182                 var req = new Request(FETCH_NON_CAT_CIRC, G.user.session, nonCatCircIds[i]);
1183                 req.callback(myOPACDrawNonCatCirc);
1184                 req.send();
1185         }
1186 }
1187
1188
1189 /* draw a single circ */
1190 function myOPACDrawNonCatCirc(r) {
1191         var circ = r.getResultObject();
1192
1193
1194         var type = grep(nonCatTypes, 
1195                 function(i){
1196                         return (i.id() == circ.item_type());
1197                 }
1198         )[0];
1199
1200
1201         var row = nonCatTbody.appendChild(nonCatRow.cloneNode(true));
1202         appendClear($n(row, 'circ_lib'), text(findOrgUnit(circ.circ_lib()).name()));
1203         appendClear($n(row, 'item_type'), text(type.name()));
1204
1205         var duration = interval_to_seconds(type.circ_duration());
1206         duration = parseInt(duration + '000');
1207
1208         var dtf = circ.circ_time();
1209     var start = dojo.date.stamp.fromISOString(circ.circ_time());
1210         var due = new Date(  start.getTime() + duration );
1211         appendClear($n(row, 'circ_time'), text(due.iso8601Format('YMDHM', null, true, true)));
1212 }
1213
1214
1215
1216
1217 function myopacSelectAllChecked() {
1218    __myopacSelectChecked(true);
1219 }
1220
1221 function myopacSelectNoneChecked() {
1222    __myopacSelectChecked(false);
1223 }
1224
1225 function __myopacSelectChecked(value) {
1226    var rows = myopacGetCheckedOutRows();
1227    for( var i = 0; i < rows.length; i++ ) {
1228       var row = rows[i];
1229       var box = $n(row, 'selectme');
1230       if( box && ! box.disabled )
1231       box.checked = value;
1232    }
1233 }
1234
1235 function myopacGetCheckedOutRows() {
1236    var rows = [];
1237    var tbody = $('myopac_checked_tbody');
1238    var children = tbody.childNodes;
1239    for( var i = 0; i < children.length; i++ ) {
1240       var child = children[i];
1241       if( child.nodeName.match(/^tr$/i) ) 
1242          if( $n(child, 'selectme') ) 
1243             rows.push(child);
1244    }
1245    return rows;
1246 }
1247
1248 var __renew_circs = [];
1249
1250 /* true if 1 renewal succeeded */
1251 var __success_count = 0;
1252
1253 /* renews all selected circulations */
1254 function myOPACRenewSelected() {
1255    var rows = myopacGetCheckedOutRows();
1256         if(!confirm($('myopac_renew_confirm').innerHTML)) return;
1257    __success_count = 0;
1258
1259    for( var i = 0; i < rows.length; i++ ) {
1260
1261       var row = rows[i];
1262       if( ! $n(row, 'selectme').checked ) continue;
1263       var circ_id = row.getAttribute('circid');
1264
1265            var circ;
1266            for( var j = 0; j != circsCache.length; j++ ) 
1267                    if(circsCache[j].id() == circ_id)
1268                            circ = circsCache[j];
1269
1270       __renew_circs.push(circ);
1271    }
1272
1273     if( __renew_circs.length == 0 ) return;
1274
1275     unHideMe($('my_renewing'));
1276     moClearCheckedTable();
1277
1278     for( var i = 0; i < __renew_circs.length; i++ ) {
1279         var circ = __renew_circs[i];
1280         moRenewCirc( circ.target_copy(), G.user.id(), circ );
1281     }
1282 }
1283
1284
1285 /* renews a single circulation */
1286 function moRenewCirc(copy_id, user_id, circ) {
1287
1288    _debug('renewing circ ' + circ.id() + ' with copy ' + copy_id);
1289    var req = new Request(RENEW_CIRC, G.user.session, 
1290       {  patron : user_id, 
1291          copyid : copy_id, 
1292          opac_renewal : 1 
1293       } 
1294    );
1295
1296    req.request.alertEvent = false;
1297    req.callback(myHandleRenewResponse);
1298    req.request.circ = circ;
1299    req.send();
1300 }
1301
1302
1303
1304 /* handles the circ renew results */
1305 function myHandleRenewResponse(r) {
1306    var res = r.getResultObject();
1307    var circ = r.circ;
1308
1309    /* remove this circ from the list of circs to renew */
1310    __renew_circs = grep(__renew_circs, function(i) { return (i.id() != circ.id()); });
1311
1312    _debug("handling renew result for " + circ.id());
1313
1314    if(checkILSEvent(res) || checkILSEvent(res[0])) 
1315       alertIdText('myopac_renew_fail', __circ_titles[circ.id()]);
1316    else __success_count++;
1317
1318    if(__renew_circs) return; /* more to come */
1319
1320    __renew_circs = [];
1321
1322         if( __success_count > 0 )
1323       alertIdText('myopac_renew_success', __success_count);
1324
1325    hideMe($('my_renewing'));
1326    checkedDrawn = false;
1327     myOPACShowChecked();
1328 }
1329
1330 /** ---- batch hold processing ------------ */
1331
1332
1333 /* myopac_holds_checkbx */
1334 function myopacSelectAllHolds() {
1335     var rows = getTableRows($("myopac_holds_tbody"));
1336     for(var i = 0; i < rows.length; i++) {
1337         cb = $n(rows[i], 'myopac_holds_selected_chkbx');
1338         if(cb) cb.checked = true;
1339     }
1340 }
1341
1342 function myopacSelectNoneHolds() {
1343     var rows = getTableRows($("myopac_holds_tbody"));
1344     for(var i = 0; i < rows.length; i++) {
1345         cb = $n(rows[i], 'myopac_holds_selected_chkbx');
1346         if(cb) cb.checked = false;
1347     }
1348 }
1349
1350 function myopacSelectedHoldsRows() {
1351     var r = [];
1352     var rows = getTableRows($("myopac_holds_tbody"));
1353     for(var i = 0; i < rows.length; i++) {
1354         cb = $n(rows[i], 'myopac_holds_selected_chkbx');
1355         if(cb && cb.checked)
1356             r.push(rows[i]);
1357     }
1358     return r;
1359 }
1360
1361 var myopacProcessedHolds = 0;
1362 var myopacHoldsToProcess = 0;
1363 function myopacDoHoldAction() {
1364
1365     var selectedRows = myopacSelectedHoldsRows();
1366     action = getSelectorVal($('myopac_holds_actions'));
1367     $('myopac_holds_actions_none').selected = true;
1368     if(selectedRows.length == 0) return;
1369
1370     myopacProcessedHolds = 0;
1371
1372     if(!confirmId('myopac.holds.'+action+'.confirm')) return;
1373     myopacSelectNoneHolds(); /* clear the selection */
1374
1375
1376     /* first, let's collect the holds that actually need processing and
1377         collect the full process count while we're at it */
1378     var holds = [];
1379     for(var i = 0; i < selectedRows.length; i++) {
1380         hold = holdCache[myopacHoldIDFromRow(selectedRows[i])];
1381         var status = holdStatusCache[hold.id()];
1382         switch(action) {
1383             case 'cancel':
1384                 holds.push(hold);
1385                 break;
1386             case 'thaw_date':
1387             case 'thaw':
1388                 if(isTrue(hold.frozen()))
1389                     holds.push(hold);
1390                 break;
1391             case 'freeze':
1392                 if(!isTrue(hold.frozen()) && status < 3)
1393                     holds.push(hold);
1394                 break;
1395         }
1396     }
1397     myopacHoldsToProcess = holds;
1398     if(myopacHoldsToProcess.length == 0) return;
1399
1400     if(action == 'thaw_date' || action == 'freeze') 
1401         myopacDrawHoldThawDateForm();
1402     else
1403     myopacProcessHolds(action);
1404 }
1405
1406
1407 function myopacProcessHolds(action, thawDate) {
1408
1409     myopacShowHoldProcessing();
1410     /* now we process them */
1411     for(var i = 0; i < myopacHoldsToProcess.length; i++) {
1412
1413         hold = myopacHoldsToProcess[i];
1414         
1415         var req;
1416         switch(action) { 
1417
1418             case 'cancel':
1419                 req = new Request(CANCEL_HOLD, G.user.session, hold.id());
1420                 break;
1421     
1422             case 'thaw':
1423                 hold.frozen('f');
1424                 hold.thaw_date(null);
1425                 req = new Request(UPDATE_HOLD, G.user.session, hold);
1426                 break;
1427
1428             case 'thaw_date':
1429             case 'freeze':
1430                 hold.frozen('t');
1431                 hold.thaw_date(thawDate); 
1432                 req = new Request(UPDATE_HOLD, G.user.session, hold);
1433                 break;
1434                 //thawDate = prompt($('myopac.holds.freeze.select_thaw').innerHTML);
1435
1436         }
1437
1438         req.callback(myopacBatchHoldCallback);
1439         req.send();
1440         req = null;
1441     }
1442 }
1443
1444 function myopacDrawHoldThawDateForm() {
1445     hideMe($('myopac_holds_main_table'));
1446     unHideMe($('myopac_holds_thaw_date_form'));
1447     $('myopac_holds_thaw_date_input').focus();
1448 }
1449
1450 function myopacApplyThawDate() {
1451     var dateString = dojo.date.stamp.toISOString(dijit.byId('myopac_holds_thaw_date_input').getValue());
1452     if(dateString) {
1453         dateString = holdsVerifyThawDate(dateString);
1454         if(!dateString) return;
1455     } else {
1456         dateString = null;
1457     }
1458     myopacProcessHolds('freeze', dateString);
1459 }
1460
1461 function myopacHoldIDFromRow(row) {
1462     return row.id.replace(/.*_(\d+)$/, '$1');
1463 }
1464
1465 function myopacShowHoldProcessing() {
1466     unHideMe($('myopac_holds_processing'));
1467     hideMe($('myopac_holds_main_table'));
1468 }
1469
1470 function myopacHideHoldProcessing() {
1471     hideMe($('myopac_holds_processing'));
1472     unHideMe($('myopac_holds_main_table'));
1473     hideMe($('myopac_holds_thaw_date_form'));
1474 }
1475
1476 function myopacBatchHoldCallback(r) {
1477     if(r) /* force load any exceptions */
1478         r.getResultObject();
1479     if(++myopacProcessedHolds >= myopacHoldsToProcess.length) {
1480         myopacHideHoldProcessing();
1481         holdCache = {};
1482         holdStatusCache = {};
1483         myopacForceHoldsRedraw = true;
1484         myOPACShowHolds();
1485     }
1486 }
1487