4deca438f1ad95c31d3e19f79df00977212599e4
[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_first'),text(user.first_given_name()));
786         appendClear($('myopac_summary_middle'),text(user.second_given_name()));
787         appendClear($('myopac_summary_dayphone'),text(user.day_phone()));
788         appendClear($('myopac_summary_eveningphone'),text(user.evening_phone()));
789         appendClear($('myopac_summary_otherphone'),text(user.other_phone()));
790         appendClear($('myopac_summary_last'),text(user.family_name()));
791         appendClear($('myopac_summary_username'),text(user.usrname()));
792         appendClear($('myopac_summary_email'),text(user.email()));
793         appendClear($('myopac_summary_barcode'),text(user.card().barcode()));
794         appendClear($('myopac_summary_ident1'),text(iv1));
795         appendClear($('myopac_summary_homelib'),text(findOrgUnit(user.home_ou()).name()));
796         appendClear($('myopac_summary_create_date'),text(_trimTime(user.create_date())));
797
798         var req = new Request( 
799                 FETCH_USER_NOTES, G.user.session, {pub:1, patronid:G.user.id()});
800         req.callback(myopacDrawNotes);
801         req.send();
802
803
804         var tbody = $('myopac_addr_tbody');
805         var template;
806
807         if(addrRowTemplate) { 
808                 template = addrRowTemplate;
809         } else {
810                 template = tbody.removeChild($('myopac_addr_row'));
811                 addrRowTemplate = template;
812         }
813         removeChildren(tbody);
814
815         for( var a in user.addresses() ) {
816                 var row = template.cloneNode(true);
817                 myOPACDrawAddr(row, user.addresses()[a]);
818                 tbody.appendChild(row);
819         }
820 }
821
822
823 function myopacDrawNotes(r) {
824         var notes = r.getResultObject();
825         var tbody = $('myopac.notes.tbody');
826         if(!notesTemplate)
827                 notesTemplate = tbody.removeChild($('myopac.notes.tr'));
828         removeChildren(tbody);
829
830         iterate(notes, 
831                 function(note) {
832                         unHideMe($('myopac.notes.div'));
833                         var row = notesTemplate.cloneNode(true);
834                         $n(row, 'title').appendChild(text(note.title()));
835                         $n(row, 'value').appendChild(text(note.value()));
836                         tbody.appendChild(row);
837                 }
838         );
839 }
840
841
842
843 function myOPACDrawAddr(row, addr) {
844
845         appendClear($n(row, 'myopac_addr_type'),text(addr.address_type()));
846         var street = (addr.street2()) ? addr.street1() + ", " + addr.street2() : addr.street1();
847         appendClear($n(row, 'myopac_addr_street'),text(street));
848         appendClear($n(row, 'myopac_addr_city'),text(addr.city()));
849         appendClear($n(row, 'myopac_addr_county'),text(addr.county()));
850         appendClear($n(row, 'myopac_addr_state'),text(addr.state()));
851         appendClear($n(row, 'myopac_addr_zip'),text(addr.post_code()));
852 }
853
854
855 function myOPACUpdateUsername() {
856         var username = $('myopac_new_username').value;
857         if(username == null || username == "") {
858                 alert($('myopac_username_error').innerHTML);
859                 return;
860         }
861
862         if( username.match(/.*\s.*/) ) {
863                 alert($('myopac_invalid_username').innerHTML);
864                 return;
865         }
866
867     r = fetchOrgSettingDefault(globalOrgTree.id(), 'opac.barcode_regex');
868     if(r) REGEX_BARCODE = new RegExp(r);
869
870     if(username.match(REGEX_BARCODE)) {
871         alert($('myopac_invalid_username').innerHTML);
872         return;
873     }
874
875         /* first see if the requested username is taken */
876         var req = new Request(CHECK_USERNAME, G.user.session, username);
877         req.send(true);
878         var res = req.result();
879         /* If the username does not already exist, res will be null;
880          * we can move on to updating the username.
881          * 
882          * If the username does exist, then res will be the user ID.
883          * G.user.id() gives us the currently authenticated user ID.
884          * If res == G.user.id(), we try to update the username anyways.
885          */
886         if( res !== null && res != G.user.id() ) {
887                 alertId('myopac_username_dup');
888                 return;
889         }
890
891         var req = new Request(UPDATE_USERNAME, G.user.session, username );
892         req.send(true);
893         if(req.result()) {
894
895                 var evt;
896                 var res = req.result();
897                 if(evt = checkILSEvent(res)) {
898                         alertILSEvent(res);
899                         return;
900                 }
901
902                 G.user.usrname(username);
903                 hideMe($('myopac_update_username_row'));
904                 userShown = false;
905                 alertId('myopac_username_success');
906                 myOPACShowSummary();
907                 return;
908         }
909
910         alert($('myopac_username_failure').innerHTML);
911 }
912
913 function myOPACUpdateEmail() {
914         var email = $('myopac_new_email').value;
915         if(email == null || email == "") {
916                 alert($('myopac_email_error').innerHTML);
917                 return;
918         }
919
920         var req = new Request(UPDATE_EMAIL, G.user.session, email );
921         req.send(true);
922         if(req.result()) {
923                 G.user.usrname(email);
924                 hideMe($('myopac_update_email_row'));
925                 userShown = false;
926                 alertId('myopac_email_success');
927                 myOPACShowSummary();
928                 return;
929         }
930
931         alert($('myopac_email_failure').innerHTML);
932 }
933
934
935 function myOPACUpdatePassword() {
936         var curpassword = $('myopac_current_password').value;
937         var password = $('myopac_new_password').value;
938         var password2 = $('myopac_new_password2').value;
939
940         if(     curpassword == null || curpassword == "" || 
941                         password == null || password == "" || 
942                         password2 == null || password2 == "" || password != password2 ) {
943                 alert($('myopac_password_error').innerHTML);
944                 return;
945         }
946
947         if(!strongPassword(password, true)) return;
948
949         var req = new Request(UPDATE_PASSWORD, G.user.session, password, curpassword );
950         req.send(true);
951         if(req.result()) {
952                 hideMe($('myopac_update_password_row'));
953                 userShown = false;
954                 alertId('myopac_password_success');
955                 myOPACShowSummary();
956                 return;
957         }
958
959         alert($('myopac_password_failure').innerHTML);
960 }
961
962
963
964
965 var containerTemplate;
966 function myOPACShowBookbags(force) {
967
968         var tbody =$('myopac_bookbag_tbody') ;
969
970         if(!containerTemplate) 
971                 containerTemplate = tbody.removeChild($('myopac_bookbag_tr'));
972         else if(!force) return;
973
974         removeChildren(tbody);
975
976         var containers = containerFetchAll();
977
978         var found = false;
979         for( var i in containers ) {
980                 found = true;
981                 var cont = containers[i];
982                 var row = containerTemplate.cloneNode(true);
983                 row.id = 'myopac_bookbag_row_' + cont.id();
984                 var link = $n(row, 'myopac_expand_bookbag');
985                 var dlink = $n(row, 'myopac_container_delete');
986                 link.appendChild( text(cont.name()) );
987                 link.setAttribute('href', 
988                         'javascript:myOPACExpandBookbag("' + cont.id() + '","' + cont.name() + '");');
989                 myOPACFetchBBItems( cont.id(), row );
990                 dlink.setAttribute('href', 'javascript:myOPACDeleteBookbag("'+cont.id()+'");');
991
992                 if( isTrue(cont.pub()) ) {
993                         unHideMe($n(row, 'myopac_bb_published_yes'));
994                         var link = $n(row, 'myopac_bb_published_view');
995                         link.setAttribute('href', buildExtrasLink( 'feed/bookbag/html-full/'+cont.id(), false));  
996                         link.setAttribute('target', '_blank' );
997                         unHideMe(link);
998
999                         link = $n(row, 'myopac_bb_published_atom');
1000                         link.setAttribute('href', buildExtrasLink( 'feed/bookbag/rss2-full/'+cont.id(), false));  
1001                         link.setAttribute('target', '_blank' );
1002                         unHideMe(link);
1003
1004                         link = $n(row, 'myopac_bb_make_unpublished');
1005                         link.setAttribute('href', 'javascript:myOPACMakeBBPublished("'+cont.id()+'", true);');
1006                         unHideMe(link);
1007
1008                 } else { 
1009                         unHideMe($n(row, 'myopac_bb_published_no')); 
1010                         var link = $n(row, 'myopac_bb_make_published');
1011                         link.setAttribute('href', 'javascript:myOPACMakeBBPublished("'+cont.id()+'");');
1012                         unHideMe(link);
1013                 }
1014
1015                 tbody.appendChild(row); 
1016         }
1017
1018         if(!found) unHideMe($('myopac_bookbags_none'));
1019         else unHideMe($('myopac_bookbag_table'));       
1020 }
1021
1022 function myOPACMakeBBPublished(bbid, hideme) {
1023
1024         var bb = fleshedContainers[bbid];
1025
1026         if(hideme) {
1027                 if(!confirm($('myopac_make_unpublished_confirm').innerHTML)) return;
1028                 bb.pub('f');
1029         } else {
1030                 if(!confirm($('myopac_make_published_confirm').innerHTML)) return;
1031                 bb.pub('t');
1032         }
1033
1034         var result = containerUpdate(bb);
1035
1036         var code = checkILSEvent(result);
1037         if(code) { alertILSEvent(result); return; }
1038
1039         alert($('myopac_bb_update_success').innerHTML);
1040         myOPACShowBookbags(true);
1041 }
1042
1043
1044
1045 function myOPACDeleteBookbag(id) {
1046         if( confirm( $('myopac_delete_bookbag_warn').innerHTML ) ) {
1047                 var result = containerDelete(id);
1048                 var code = checkILSEvent(result);
1049                 if(code) { alertILSEvent(result); return; }
1050                 alert($('myopac_bb_update_success').innerHTML);
1051                 hideMe($('myopac_bookbag_items_table'));
1052                 hideMe($('myopac_bookbag_items_name'));
1053                 hideMe($('myopac_bookbag_no_items'));
1054                 myOPACShowBookbags(true);
1055         }
1056 }
1057
1058 function myOPACFetchBBItems( id, row, block ) {
1059         if(!block) {
1060                 containerFlesh( id, _myOPACSetBBItems, { row: row }  );
1061         } else {
1062                 var cont = containerFlesh(id);
1063                 myOPACSetBBItems( cont, row );
1064         }
1065 }
1066
1067 function _myOPACSetBBItems(r) { myOPACSetBBItems( r.getResultObject(), r.args.row ); }
1068
1069 function myOPACSetBBItems( container, row ) {
1070         fleshedContainers[container.id()] = container;
1071         var node = $n(row, 'myopac_bookbag_item_count');
1072         removeChildren(node);
1073         node.appendChild( text(container.items().length) );
1074 }
1075
1076 var BBItemsRow;
1077 function myOPACExpandBookbag( id, name ) {
1078         
1079         var tbody = $('myopac_bookbag_items_tbody');
1080         if(!BBItemsRow) BBItemsRow = tbody.removeChild($('myopac_bookbag_items_row'));
1081         removeChildren(tbody);
1082         removeChildren($('myopac_bookbag_items_name'));
1083
1084         $('myopac_bookbag_items_name').appendChild(text(name));
1085
1086         if( fleshedContainers[id] ) {
1087                 var len = fleshedContainers[id].items().length;
1088
1089                 if( len == 0 ) {
1090                         unHideMe($('myopac_bookbag_no_items'));
1091                         hideMe($('myopac_bookbag_items_table'));
1092                         return;
1093                 }
1094
1095                 hideMe($('myopac_bookbag_no_items'));
1096                 unHideMe($('myopac_bookbag_items_table'));
1097
1098                 for( var i = 0; i != len; i++ ) {
1099                         var row = BBItemsRow.cloneNode(true);
1100                         found = true;
1101
1102                         var item = fleshedContainers[id].items()[i];
1103                         var tlink = $n(row,'myopac_bookbag_items_title');
1104                         var alink = $n(row,'myopac_bookbag_items_author');
1105
1106                         var req = new Request( FETCH_RMODS, item.target_biblio_record_entry() );
1107                         req.request.tlink = tlink;
1108                         req.request.alink = alink;
1109                         req.callback(myOPACShowBBItem);
1110                         req.send();
1111
1112                         var clink = $n(row, 'myopac_bookbag_items_remove');
1113                         clink.setAttribute('href', 'javascript:myOPACRemoveBBItem("'+item.id()+'","'+id+'","'+name+'");');
1114
1115                         tbody.appendChild(row);
1116                 }
1117         }
1118 }
1119
1120 function myOPACRemoveBBItem( id, containerid, container_name ) {
1121         if(!confirm($('myopac_remove_bb_item_confirm').innerHTML)) return;
1122         var stat = containerRemoveItem( id );
1123         if(stat) alert($('myopac_bb_update_success').innerHTML);
1124         myOPACFetchBBItems( containerid, $('myopac_bookbag_row_' + containerid), true);
1125         myOPACExpandBookbag( containerid, container_name );
1126 }
1127
1128 function myOPACShowBBItem(r) {
1129         var record = r.getResultObject();
1130         buildTitleDetailLink(record, r.tlink);
1131         buildSearchLink(STYPE_AUTHOR, record.author(), r.alink);
1132 }
1133
1134 function myOPACCreateBookbag() {
1135         var name = $('myopac_bookbag_new_name').value;  
1136         if(!name) return;
1137
1138         var exists = false;
1139         for( var c in fleshedContainers ) { exists = true; break; }
1140
1141         /* let them know what they are getting into... */
1142         if(!exists) if(!confirm($('bb_create_warning').innerHTML)) return;
1143
1144         var result = containerCreate( name, $('bb_public_yes').checked );
1145         var code = checkILSEvent(result);
1146         if(code) { alertILSEvent(result); return; }
1147         if(result) alert($('myopac_bb_update_success').innerHTML);
1148         myOPACShowBookbags(true);
1149 }
1150
1151
1152 /* ---------------------------------------------------------------------- */
1153 /* Non cat circs */
1154 /* ---------------------------------------------------------------------- */
1155
1156 var nonCatCircIds;
1157 var nonCatTypes;
1158 /* if we have some circs, grab the non-cat types */
1159 function myOPACDrawNonCatCircs(r) {
1160         var ids = r.getResultObject();
1161         if(ids.length == 0) return;
1162         nonCatCircIds = ids;
1163         unHideMe($('non_cat_circs_div'));
1164         var req = new Request(FETCH_NON_CAT_TYPES, G.user.home_ou());
1165         req.callback(myOPACDrawNonCatCircs2);
1166         req.send();
1167 }
1168
1169
1170 /* now we have circs and the types.. draw each one */
1171 var nonCatTbody;
1172 var nonCatRow;
1173 function myOPACDrawNonCatCircs2(r) {
1174         nonCatTypes = r.getResultObject();
1175         nonCatTbody = $('non_cat_circs_tbody');
1176         if(!nonCatRow) nonCatRow = 
1177                 nonCatTbody.removeChild($('non_cat_circs_row'));
1178         removeChildren(nonCatTbody);
1179         for( var i in nonCatCircIds ) {
1180                 var req = new Request(FETCH_NON_CAT_CIRC, G.user.session, nonCatCircIds[i]);
1181                 req.callback(myOPACDrawNonCatCirc);
1182                 req.send();
1183         }
1184 }
1185
1186
1187 /* draw a single circ */
1188 function myOPACDrawNonCatCirc(r) {
1189         var circ = r.getResultObject();
1190
1191
1192         var type = grep(nonCatTypes, 
1193                 function(i){
1194                         return (i.id() == circ.item_type());
1195                 }
1196         )[0];
1197
1198
1199         var row = nonCatTbody.appendChild(nonCatRow.cloneNode(true));
1200         appendClear($n(row, 'circ_lib'), text(findOrgUnit(circ.circ_lib()).name()));
1201         appendClear($n(row, 'item_type'), text(type.name()));
1202
1203         var duration = interval_to_seconds(type.circ_duration());
1204         duration = parseInt(duration + '000');
1205
1206         var dtf = circ.circ_time();
1207     var start = dojo.date.stamp.fromISOString(circ.circ_time());
1208         var due = new Date(  start.getTime() + duration );
1209         appendClear($n(row, 'circ_time'), text(due.iso8601Format('YMDHM', null, true, true)));
1210 }
1211
1212
1213
1214
1215 function myopacSelectAllChecked() {
1216    __myopacSelectChecked(true);
1217 }
1218
1219 function myopacSelectNoneChecked() {
1220    __myopacSelectChecked(false);
1221 }
1222
1223 function __myopacSelectChecked(value) {
1224    var rows = myopacGetCheckedOutRows();
1225    for( var i = 0; i < rows.length; i++ ) {
1226       var row = rows[i];
1227       var box = $n(row, 'selectme');
1228       if( box && ! box.disabled )
1229       box.checked = value;
1230    }
1231 }
1232
1233 function myopacGetCheckedOutRows() {
1234    var rows = [];
1235    var tbody = $('myopac_checked_tbody');
1236    var children = tbody.childNodes;
1237    for( var i = 0; i < children.length; i++ ) {
1238       var child = children[i];
1239       if( child.nodeName.match(/^tr$/i) ) 
1240          if( $n(child, 'selectme') ) 
1241             rows.push(child);
1242    }
1243    return rows;
1244 }
1245
1246 var __renew_circs = [];
1247
1248 /* true if 1 renewal succeeded */
1249 var __success_count = 0;
1250
1251 /* renews all selected circulations */
1252 function myOPACRenewSelected() {
1253    var rows = myopacGetCheckedOutRows();
1254         if(!confirm($('myopac_renew_confirm').innerHTML)) return;
1255    __success_count = 0;
1256
1257    for( var i = 0; i < rows.length; i++ ) {
1258
1259       var row = rows[i];
1260       if( ! $n(row, 'selectme').checked ) continue;
1261       var circ_id = row.getAttribute('circid');
1262
1263            var circ;
1264            for( var j = 0; j != circsCache.length; j++ ) 
1265                    if(circsCache[j].id() == circ_id)
1266                            circ = circsCache[j];
1267
1268       __renew_circs.push(circ);
1269    }
1270
1271     if( __renew_circs.length == 0 ) return;
1272
1273     unHideMe($('my_renewing'));
1274     moClearCheckedTable();
1275
1276     for( var i = 0; i < __renew_circs.length; i++ ) {
1277         var circ = __renew_circs[i];
1278         moRenewCirc( circ.target_copy(), G.user.id(), circ );
1279     }
1280 }
1281
1282
1283 /* renews a single circulation */
1284 function moRenewCirc(copy_id, user_id, circ) {
1285
1286    _debug('renewing circ ' + circ.id() + ' with copy ' + copy_id);
1287    var req = new Request(RENEW_CIRC, G.user.session, 
1288       {  patron : user_id, 
1289          copyid : copy_id, 
1290          opac_renewal : 1 
1291       } 
1292    );
1293
1294    req.request.alertEvent = false;
1295    req.callback(myHandleRenewResponse);
1296    req.request.circ = circ;
1297    req.send();
1298 }
1299
1300
1301
1302 /* handles the circ renew results */
1303 function myHandleRenewResponse(r) {
1304    var res = r.getResultObject();
1305    var circ = r.circ;
1306
1307    /* remove this circ from the list of circs to renew */
1308    __renew_circs = grep(__renew_circs, function(i) { return (i.id() != circ.id()); });
1309
1310    _debug("handling renew result for " + circ.id());
1311
1312    if(checkILSEvent(res) || checkILSEvent(res[0])) 
1313       alertIdText('myopac_renew_fail', __circ_titles[circ.id()]);
1314    else __success_count++;
1315
1316    if(__renew_circs) return; /* more to come */
1317
1318    __renew_circs = [];
1319
1320         if( __success_count > 0 )
1321       alertIdText('myopac_renew_success', __success_count);
1322
1323    hideMe($('my_renewing'));
1324    checkedDrawn = false;
1325     myOPACShowChecked();
1326 }
1327
1328 /** ---- batch hold processing ------------ */
1329
1330
1331 /* myopac_holds_checkbx */
1332 function myopacSelectAllHolds() {
1333     var rows = getTableRows($("myopac_holds_tbody"));
1334     for(var i = 0; i < rows.length; i++) {
1335         cb = $n(rows[i], 'myopac_holds_selected_chkbx');
1336         if(cb) cb.checked = true;
1337     }
1338 }
1339
1340 function myopacSelectNoneHolds() {
1341     var rows = getTableRows($("myopac_holds_tbody"));
1342     for(var i = 0; i < rows.length; i++) {
1343         cb = $n(rows[i], 'myopac_holds_selected_chkbx');
1344         if(cb) cb.checked = false;
1345     }
1346 }
1347
1348 function myopacSelectedHoldsRows() {
1349     var r = [];
1350     var rows = getTableRows($("myopac_holds_tbody"));
1351     for(var i = 0; i < rows.length; i++) {
1352         cb = $n(rows[i], 'myopac_holds_selected_chkbx');
1353         if(cb && cb.checked)
1354             r.push(rows[i]);
1355     }
1356     return r;
1357 }
1358
1359 var myopacProcessedHolds = 0;
1360 var myopacHoldsToProcess = 0;
1361 function myopacDoHoldAction() {
1362
1363     var selectedRows = myopacSelectedHoldsRows();
1364     action = getSelectorVal($('myopac_holds_actions'));
1365     $('myopac_holds_actions_none').selected = true;
1366     if(selectedRows.length == 0) return;
1367
1368     myopacProcessedHolds = 0;
1369
1370     if(!confirmId('myopac.holds.'+action+'.confirm')) return;
1371     myopacSelectNoneHolds(); /* clear the selection */
1372
1373
1374     /* first, let's collect the holds that actually need processing and
1375         collect the full process count while we're at it */
1376     var holds = [];
1377     for(var i = 0; i < selectedRows.length; i++) {
1378         hold = holdCache[myopacHoldIDFromRow(selectedRows[i])];
1379         var status = holdStatusCache[hold.id()];
1380         switch(action) {
1381             case 'cancel':
1382                 holds.push(hold);
1383                 break;
1384             case 'thaw_date':
1385             case 'thaw':
1386                 if(isTrue(hold.frozen()))
1387                     holds.push(hold);
1388                 break;
1389             case 'freeze':
1390                 if(!isTrue(hold.frozen()) && status < 3)
1391                     holds.push(hold);
1392                 break;
1393         }
1394     }
1395     myopacHoldsToProcess = holds;
1396     if(myopacHoldsToProcess.length == 0) return;
1397
1398     if(action == 'thaw_date' || action == 'freeze') 
1399         myopacDrawHoldThawDateForm();
1400     else
1401     myopacProcessHolds(action);
1402 }
1403
1404
1405 function myopacProcessHolds(action, thawDate) {
1406
1407     myopacShowHoldProcessing();
1408     /* now we process them */
1409     for(var i = 0; i < myopacHoldsToProcess.length; i++) {
1410
1411         hold = myopacHoldsToProcess[i];
1412         
1413         var req;
1414         switch(action) { 
1415
1416             case 'cancel':
1417                 req = new Request(CANCEL_HOLD, G.user.session, hold.id());
1418                 break;
1419     
1420             case 'thaw':
1421                 hold.frozen('f');
1422                 hold.thaw_date(null);
1423                 req = new Request(UPDATE_HOLD, G.user.session, hold);
1424                 break;
1425
1426             case 'thaw_date':
1427             case 'freeze':
1428                 hold.frozen('t');
1429                 hold.thaw_date(thawDate); 
1430                 req = new Request(UPDATE_HOLD, G.user.session, hold);
1431                 break;
1432                 //thawDate = prompt($('myopac.holds.freeze.select_thaw').innerHTML);
1433
1434         }
1435
1436         req.callback(myopacBatchHoldCallback);
1437         req.send();
1438         req = null;
1439     }
1440 }
1441
1442 function myopacDrawHoldThawDateForm() {
1443     hideMe($('myopac_holds_main_table'));
1444     unHideMe($('myopac_holds_thaw_date_form'));
1445     $('myopac_holds_thaw_date_input').focus();
1446 }
1447
1448 function myopacApplyThawDate() {
1449     var dateString = dojo.date.stamp.toISOString(dijit.byId('myopac_holds_thaw_date_input').getValue());
1450     if(dateString) {
1451         dateString = holdsVerifyThawDate(dateString);
1452         if(!dateString) return;
1453     } else {
1454         dateString = null;
1455     }
1456     myopacProcessHolds('freeze', dateString);
1457 }
1458
1459 function myopacHoldIDFromRow(row) {
1460     return row.id.replace(/.*_(\d+)$/, '$1');
1461 }
1462
1463 function myopacShowHoldProcessing() {
1464     unHideMe($('myopac_holds_processing'));
1465     hideMe($('myopac_holds_main_table'));
1466 }
1467
1468 function myopacHideHoldProcessing() {
1469     hideMe($('myopac_holds_processing'));
1470     unHideMe($('myopac_holds_main_table'));
1471     hideMe($('myopac_holds_thaw_date_form'));
1472 }
1473
1474 function myopacBatchHoldCallback(r) {
1475     if(r) /* force load any exceptions */
1476         r.getResultObject();
1477     if(++myopacProcessedHolds >= myopacHoldsToProcess.length) {
1478         myopacHideHoldProcessing();
1479         holdCache = {};
1480         holdStatusCache = {};
1481         myopacForceHoldsRedraw = true;
1482         myOPACShowHolds();
1483     }
1484 }
1485