be79c65ce8e560833f92c5ea0e85a0c845936797
[Evergreen.git] / Open-ILS / web / opac / skin / default / js / myopac.js
1
2 attachEvt("common", "run", myOPACInit );
3 attachEvt("common", "loggedIn", myOPACInit );
4
5 attachEvt('common','locationUpdated', myopacReload );
6
7 var fleshedUser = null;
8 var fleshedContainers = {};
9 var holdCache = {};
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
24 function myopacReload() {
25         swapCanvas($('myopac_reloading'));
26         var a = {};
27         a[PARAM_LOCATION] = getNewSearchLocation();
28         a[PARAM_DEPTH] = getNewSearchDepth();
29         goTo(buildOPACLink(a, true));
30 }
31
32
33 function myOPACChangePage( page ) {
34         
35         var s = $("myopac_summary_td");
36         var c = $("myopac_checked_td");
37         var f = $("myopac_fines_td");
38         var h = $("myopac_holds_td");
39         var p = $("myopac_prefs_td");
40         var b = $('myopac_bookbag_td');
41
42         var ss = $("myopac_summary_div");
43         var cc = $("myopac_checked_div");
44         var ff = $("myopac_fines_div");
45         var hh = $("myopac_holds_div");
46         var pp = $("myopac_prefs_div");
47         var bb = $('myopac_bookbag_div');
48
49         var cls = "myopac_link";
50         var acls = "myopac_link_active";
51
52         hideMe(ss);
53         hideMe(cc); hideMe(ff);
54         hideMe(hh); hideMe(pp);
55         hideMe(bb);
56
57         removeCSSClass(s, acls );
58         removeCSSClass(c, acls );
59         removeCSSClass(f, acls );
60         removeCSSClass(h, acls );
61         removeCSSClass(p, acls );
62         removeCSSClass(b, acls );
63
64         switch( page ) {
65
66                 case "summary": 
67                         unHideMe(ss);
68                         addCSSClass(s, acls );
69                         myOPACShowSummary();
70                         break;
71
72                 case "checked": 
73                         unHideMe(cc);
74                         addCSSClass(c, acls );
75                         myOPACShowChecked();
76                         break;
77
78                 case "holds": 
79                         unHideMe(hh);
80                         addCSSClass(h, acls );
81                         myOPACShowHolds();
82                         break;
83
84                 case "fines": 
85                         unHideMe(ff);
86                         addCSSClass(f, acls );
87                         myOPACShowFines();
88                         break;
89
90                 case "prefs": 
91                         unHideMe(pp);
92                         addCSSClass(p, acls );
93                         myOPACShowPrefs();
94                         break;
95
96                 case 'bookbag':
97                         unHideMe(bb);
98                         addCSSClass(b, acls);
99                         myOPACShowBookbags();
100                         break;
101         }
102 }
103
104 function myOPACShowChecked() {
105         if(checkedDrawn) return;
106         var req = new Request(FETCH_CHECKED_OUT_SUM, G.user.session, G.user.id());      
107         req.callback(myOPACDrawCheckedOutSlim);
108         req.send();
109
110         var nreq = new Request(FETCH_NON_CAT_CIRCS, G.user.session);
111         nreq.callback(myOPACDrawNonCatCircs);
112         nreq.send();
113 }
114
115
116 var checkedRowTemplate;
117 var circsCache = new Array();
118 var checkedDrawn = false;
119
120 function myOPACDrawCheckedOutSlim(r) {
121
122         var checked                     = r.getResultObject();
123         var tbody                       = $("myopac_checked_tbody");
124         var loading                     = $("myopac_checked_loading");
125         var none                                = $("myopac_checked_none");
126
127         if(checkedDrawn) return;
128         checkedDrawn = true;
129         if(!checkedRowTemplate) 
130                 checkedRowTemplate = tbody.removeChild($("myopac_checked_row"));
131
132         clearNodes( tbody, [ loading, none ] );
133
134         hideMe(loading); /* remove all children and start over */
135         if(!(checked && (checked.out || checked.overdue))) {
136                 unHideMe(none);
137                 return;
138         }
139
140         for( var i = 0; i < checked.overdue.length; i++ ) {
141                 var req = new Request(FETCH_CIRC_BY_ID, G.user.session, checked.overdue[i]);
142                 req.request.tbody = tbody;
143                 req.request.od = true;
144                 req.callback(myOPACDrawCheckedItem);
145                 req.send();
146         }
147
148
149         for( var i = 0; i < checked.out.length; i++ ) {
150                 var req = new Request(FETCH_CIRC_BY_ID, G.user.session, checked.out[i]);
151                 req.request.tbody = tbody;
152                 req.callback(myOPACDrawCheckedItem);
153                 req.send();
154         }
155
156 }
157
158 function myOPACDrawCheckedItem(r) {
159
160         var circ = r.getResultObject();
161         var tbody = r.tbody;
162         var row = checkedRowTemplate.cloneNode(true);
163         row.id = 'myopac_checked_row_ ' + circ.id();
164
165         var due = _trimTime(circ.due_date());
166
167         var dlink = $n( row, "myopac_checked_due" );
168         var rlink = $n( row, "myopac_checked_renewals" );
169         var rnlink = $n( row, "myopac_checked_renew_link" );
170
171         if( r.od ) due = elem('b', {style:'color:red;font-size:110%'},due);
172         else due = text(due);
173
174         dlink.appendChild(due);
175         rlink.appendChild(text(circ.renewal_remaining()));
176         unHideMe(row);
177         rnlink.setAttribute('href', 'javascript:myOPACRenewCirc("'+circ.id()+'");');
178         circsCache.push(circ);
179
180         if( circ.renewal_remaining() < 1 ) hideMe(rnlink);
181
182         tbody.appendChild(row);
183
184         var req = new Request(FETCH_MODS_FROM_COPY, circ.target_copy() );
185         req.request.alertEvent = false;
186         req.request.circ = circ.id();
187         req.request.copy = circ.target_copy();
188         req.callback(myOPACDrawCheckedTitle);
189         req.send();
190 }
191
192 function myOPACDrawCheckedTitle(r) {
193         var record = r.getResultObject();
194         var circid = r.circ;
195
196         if(!record || checkILSEvent(record)) {
197                 var req = new Request( FETCH_COPY, r.copy );
198                 req.request.circ = circid
199                 req.callback(myOPACDrawNonCatalogedItem);
200                 req.send();
201                 return;
202         }
203
204         var row = $('myopac_checked_row_ ' + circid);
205         var tlink = $n( row, "myopac_checked_title_link" );
206         var alink = $n( row, "myopac_checked_author_link" );
207         buildTitleDetailLink(record, tlink);
208         buildSearchLink(STYPE_AUTHOR, record.author(), alink);
209 }
210
211 function myOPACDrawNonCatalogedItem(r) {
212         var copy = r.getResultObject();
213         var circid = r.circ;
214
215         var row = $('myopac_checked_row_ ' + circid);
216         var tlink = $n( row, "myopac_checked_title_link" );
217         var alink = $n( row, "myopac_checked_author_link" );
218
219         tlink.parentNode.appendChild(text(copy.dummy_title()));
220         alink.parentNode.appendChild(text(copy.dummy_author()));
221 }
222
223
224 function myOPACRenewCirc(circid) {
225
226         var circ;
227         for( var i = 0; i != circsCache.length; i++ ) 
228                 if(circsCache[i].id() == circid)
229                         circ = circsCache[i];
230
231         if(!confirm($('myopac_renew_confirm').innerHTML)) return;
232
233         var req = new Request(RENEW_CIRC, G.user.session, 
234                 { patron : G.user.id(), copyid : circ.target_copy() } );
235         req.request.alertEvent = false;
236         req.send(true);
237         var res = req.result();
238
239         if(checkILSEvent(res) || checkILSEvent(res[0])) {
240                 alertId('myopac_renew_fail');
241                 return;
242         }
243
244         alert($('myopac_renew_success').innerHTML);     
245         checkedDrawn = false;
246         myOPACShowChecked();
247 }
248
249
250
251 function myOPACShowHolds() {
252         var req = new Request(FETCH_HOLDS, G.user.session, G.user.id());        
253         req.callback(myOPACDrawHolds);
254         req.send();
255 }
256
257 var holdsTemplateRowOrig;
258 var holdsTemplateRow;
259 function myOPACDrawHolds(r) {
260
261         var tbody = $("myopac_holds_tbody");
262         if(holdsTemplateRow) return;
263         if(holdsTemplateRowOrig) {
264                 holdsTemplateRow = holdsTemplateRowOrig;
265                 removeChildren(tbody);
266         } else {
267                 holdsTemplateRow = tbody.removeChild($("myopac_holds_row"));
268                 holdsTemplateRowOrig = holdsTemplateRow;
269         }
270
271         hideMe($('myopac_holds_loading'));
272
273         var holds = r.getResultObject();
274
275         if(!holds || holds.length < 1) unHideMe($('myopac_holds_none'));
276         for( var i = 0; i != holds.length; i++ ) {
277
278                 var h = holds[i];
279                 holdCache[h.id()] = h;
280
281                 var row = holdsTemplateRow.cloneNode(true);
282                 row.id = "myopac_holds_row_" + h.id();
283
284                 var form = $n(row, "myopac_holds_formats");
285                 form.id = "myopac_holds_form_" + h.id();
286
287                 var orglink = $n(row, "myopac_holds_location");
288                 orglink.appendChild(text(findOrgUnit(h.pickup_lib()).name()));
289
290                 tbody.appendChild(row);
291
292                 $n(row,'myopac_holds_cancel_link').setAttribute(
293                         'href','javascript:myOPACCancelHold("'+ h.id()+'");'); 
294
295                 $n(row,'myopac_holds_edit_link').setAttribute(
296                         'href','javascript:myOPACEditHold("'+ h.id()+'");'); 
297
298                 unHideMe(row);
299
300                 myOPACDrawHoldTitle(h);
301                 myOPACDrawHoldStatus(h);
302         }
303 }
304
305 function myOPACEditHold(holdid) {
306         var hold = holdCache[holdid];
307
308         holdsDrawEditor( 
309                 { 
310                         editHold : hold,
311                         onComplete : function(){ 
312                                 holdsTemplateRow = null;
313                                 myOPACShowHolds(); 
314                         }
315                 }
316         );
317 }
318
319
320 function myOPACCancelHold(holdid) {
321         if( confirm($('myopac_holds_cancel_verify').innerHTML) ) {
322                 holdsCancel(holdid);
323                 holdsTemplateRow = null
324                 myOPACShowHolds();
325         }
326 }
327
328
329 function myOPACDrawHoldStatus(hold) {
330         var req = new Request(FETCH_HOLD_STATUS, G.user.session, hold.id() );
331         req.callback(myOShowHoldStatus);
332         req.request.hold = hold;
333         req.send();
334 }
335
336 function myOShowHoldStatus(r) {
337
338         var hold = r.hold;
339         var status = r.getResultObject();
340         var row = $("myopac_holds_row_" + r.hold.id());
341
342         if( status < 3 )
343                 unHideMe($n(row, 'hold_status_waiting'));
344
345         if( status == 3 )
346                 unHideMe($n(row, 'hold_status_transit'));
347
348         if( status == 4 ) {
349                 unHideMe($n(row, 'hold_status_available'));
350                 hideMe($n(row, 'myopac_holds_cancel_link'));
351         }
352 }
353
354
355 function myOPACDrawHoldTitle(hold) {
356         var method;
357
358         if( hold.hold_type() == 'T' || hold.hold_type() == 'M' ) {
359                 if(hold.hold_type() == "M") method = FETCH_MRMODS;
360                 if(hold.hold_type() == "T") method = FETCH_RMODS;
361                 var req = new Request(method, hold.target());
362                 req.callback(myOPACFleshHoldTitle);
363                 req.request.hold = hold;
364                 req.request.alertEvent = false;
365                 req.send();
366
367         } else {
368                 holdFetchObjects(hold, 
369                         function(a) { _myOPACFleshHoldTitle(hold, a);});
370         }
371 }
372
373 function myOPACFleshHoldTitle(r) {
374         var rec = r.getResultObject();
375         _myOPACFleshHoldTitle(r.hold, {recordObject: rec});
376 }
377
378 function _myOPACFleshHoldTitle(hold, holdObjects) {
379
380         var record = holdObjects.recordObject;
381         var volume      = holdObjects.volumeObject;
382         var copy        = holdObjects.copyObject;
383
384         var row = $("myopac_holds_row_" + hold.id());
385         var title_link = $n(row, "myopac_holds_title_link");
386         var author_link = $n(row, "myopac_holds_author_link");
387
388         if(!record || checkILSEvent(record) ) {
389                 addCSSClass(row, 'invalid_hold');
390                 $n(row, 'myopac_holds_edit_link').setAttribute('href', 'javascript:void(0);');
391                 $n(row, 'myopac_holds_edit_link').onclick = function(){alertId('invalid_hold');};
392                 return;
393         }
394
395         buildTitleDetailLink(record, title_link);
396         buildSearchLink(STYPE_AUTHOR, record.author(), author_link);
397
398         if( volume ) {
399                 $n(row, 'volume').appendChild(text(volume.label()));
400                 unHideMe($n(row, 'vol_copy'));
401                 if(copy) $n(row, 'copy').appendChild(text(copy.barcode()));
402         }
403
404         /*var form = $("myopac_holds_form_" + hold.id() + '_' + record.doc_id());*/
405         var form = $("myopac_holds_form_" + hold.id());
406
407         if(form) {
408                 var mods_formats = record.types_of_resource();
409
410                 if( hold.hold_type() == 'M' ) {
411                         var data = holdsParseMRFormats(hold.holdable_formats());
412                         mods_formats = data.mods_formats;
413                 }
414
415                 for( var i = 0; i < mods_formats.length; i++ ) {
416                         var img = elem("img");
417                         setResourcePic(img, mods_formats[i]);
418                         form.appendChild(img);
419                 }
420         }
421 }
422
423 var finesShown = false;
424 function myOPACShowFines() {
425         if(finesShown) return; finesShown = true;
426         var req = new Request(FETCH_FINES_SUMMARY, G.user.session, G.user.id() );
427         req.callback(_myOPACShowFines);
428         req.send();
429 }
430
431 function _myOPACShowFines(r) {
432         hideMe($('myopac_fines_summary_loading'));
433         unHideMe($('myopac_fines_summary_row'));
434
435         var summary = r.getResultObject();
436         var total       = "0.00"; /* localization? */
437         var paid                = "0.00";
438         var balance = "0.00";
439         if( instanceOf(summary,mous) ) {
440
441                 total           = _finesFormatNumber(summary.total_owed());
442                 paid            = _finesFormatNumber(summary.total_paid());
443                 balance = _finesFormatNumber(summary.balance_owed());
444
445                 var req = new Request(FETCH_TRANSACTIONS, G.user.session, G.user.id() );
446                 req.callback(myOPACShowTransactions);
447                 req.send();
448         }
449
450         $('myopac_fines_summary_total').appendChild(text(total));
451         $('myopac_fines_summary_paid').appendChild(text(paid));
452         $('myopac_fines_summary_balance').appendChild(text(balance));
453 }
454
455 function _finesFormatNumber(num) {
456         if(isNull(num)) num = 0;
457         num = num + "";
458         if(num.length < 2 || !num.match(/\./)) num += ".00";
459         if(num.match(/\./) && num.charAt(num.length-2) == '.') num += "0";
460         return num;
461 }          
462
463 //function _trimTime(time) { if(!time) return ""; return time.replace(/\ .*/,""); }
464 function _trimTime(time) { 
465         if(!time) return ""; 
466         return time.replace(/T.*/,""); 
467 }
468
469 function _trimSeconds(time) { if(!time) return ""; return time.replace(/:\d\d\..*$/,""); }
470
471 function myOPACShowTransactions(r) {
472
473         if(myopacGenericTransTemplate || myopacCircTransTemplate) return;
474
475         var transactions = r.getResultObject();
476
477         for( var idx in transactions ) {
478
479                 var trans       = transactions[idx].transaction;
480                 var record      = transactions[idx].record;
481                 var circ                = transactions[idx].circ;
482
483                 if(trans.xact_type() == 'circulation') 
484                         myOPACShowCircTransaction(trans, record, circ);
485
486                 else if(trans.xact_type() == 'grocery' ) 
487                         myopacShowGenericTransaction( trans );
488         }
489 }
490
491 var myopacGenericTransTemplate;
492 function myopacShowGenericTransaction( trans ) {
493         var tbody = $('myopac_trans_tbody');
494
495         if(!myopacGenericTransTemplate) {
496                 myopacGenericTransTemplate = 
497                         tbody.removeChild($('myopac_trans_row'));
498                 removeChildren(tbody);
499         }
500
501         var row = myopacGenericTransTemplate.cloneNode(true);
502
503         $n(row,'myopac_trans_start').appendChild(
504                         text(_trimSeconds(trans.xact_start())));
505
506         $n(row,'myopac_trans_last_payment').appendChild(
507                         text(_trimSeconds(trans.last_payment_ts())));
508
509         $n(row,'myopac_trans_init_amount').appendChild(
510                         text(_finesFormatNumber(trans.total_owed())));
511
512         $n(row,'myopac_trans_total_paid').appendChild(
513                         text(_finesFormatNumber(trans.total_paid())));
514
515         $n(row,'myopac_trans_balance').appendChild(
516                         text(_finesFormatNumber(trans.balance_owed())));
517
518         var req = new Request(FETCH_MONEY_BILLING, G.user.session, trans.id());
519         req.send(true);
520         var bills = req.result();
521         if(bills && bills[0])
522                 $n(row,'myopac_trans_bill_type').appendChild(
523                                 text(bills[0].billing_type()));
524
525         tbody.appendChild(row);
526         unHideMe($('myopac_trans_div'));
527 }
528
529
530
531 /* draws a circulation transaction summary */
532 var myopacCircTransTemplate;
533 function myOPACShowCircTransaction(trans, record, circ) {
534         var tbody = $('myopac_circ_trans_tbody');
535
536         if(!myopacCircTransTemplate) {
537                 myopacCircTransTemplate = tbody.removeChild($('myopac_circ_trans_row'));
538                 removeChildren(tbody);
539         }
540
541         var row = myopacCircTransTemplate.cloneNode(true);
542
543         if(record) {
544                 buildTitleDetailLink(record, $n(row,'myopac_circ_trans_title'));
545
546                 $n(row,'myopac_circ_trans_author').appendChild(text(
547                         normalize(truncate(record.author(), 65))));
548
549         } else {
550
551                 var req = new Request( FETCH_COPY, circ.target_copy() );
552                 req.alertEvents = false;
553                 req.send(true);
554                 var copy = req.result();
555                 if( copy ) {
556                         $n(row,'myopac_circ_trans_title').appendChild(text(copy.dummy_title()));
557                         $n(row,'myopac_circ_trans_author').appendChild(text(copy.dummy_author()));
558                 }
559         }
560
561
562         $n(row,'myopac_circ_trans_start').
563                 appendChild(text(_trimTime(trans.xact_start())));
564
565    var due = _trimTime(circ.due_date());
566         var checkin = _trimTime(circ.stop_fines_time());
567
568         $n(row,'myopac_circ_trans_due').appendChild(text(due))
569         if(checkin)
570                 appendClear($n(row,'myopac_circ_trans_finished'), text(checkin));
571         if(circ.stop_fines() == 'LOST')
572                 appendClear($n(row,'myopac_circ_trans_finished'), text(circ.stop_fines()));
573         if(circ.stop_fines() == 'CLAIMSRETURNED')
574                 appendClear($n(row,'myopac_circ_trans_finished'), text(""));
575
576
577         $n(row,'myopac_circ_trans_balance').
578                 appendChild(text(_finesFormatNumber(trans.balance_owed())));
579
580         tbody.appendChild(row);
581         unHideMe($('myopac_circ_trans_div'));
582 }
583
584
585 function myOPACSavePrefs() {
586         G.user.prefs[PREF_HITS_PER] = getSelectorVal($('prefs_hits_per'));
587         G.user.prefs[PREF_DEF_FONT] = getSelectorVal($('prefs_def_font'));
588         G.user.prefs[PREF_HOLD_NOTIFY] = getSelectorVal($('prefs_hold_notify'));
589         G.user.prefs[PREF_DEF_DEPTH] = getSelectorVal($('prefs_def_range'));
590
591         if( $('myopac_pref_home_lib').checked == true )
592                 G.user.prefs[PREF_DEF_LOCATION] = null;
593         else
594                 G.user.prefs[PREF_DEF_LOCATION] = getSelectorVal($('prefs_def_location'));
595
596         if(commitUserPrefs())
597                 alert($('prefs_update_success').innerHTML);
598         else alert($('prefs_update_failure').innerHTML);
599 }
600
601
602 function myOPACShowDefFont() {
603         var font;
604         if(G.user.prefs[PREF_DEF_FONT])
605                 font = G.user.prefs[PREF_DEF_FONT];
606         else font = "regular";
607         setSelector($('prefs_def_font'), font);
608 }
609
610 function myOPACShowHoldNotify() {
611         var pref = G.user.prefs[PREF_HOLD_NOTIFY];
612
613         if(pref) {
614                 if(pref.match(/email/i) && pref.match(/phone/i)) {
615                         setSelector($('prefs_hold_notify'), 'phone:email');
616                 } else if( pref.match(/email/i) ) {
617                         setSelector($('prefs_hold_notify'), 'email');
618                 } else if( pref.match(/phone/i) ) {
619                         setSelector($('prefs_hold_notify'), 'phone');
620                 }
621
622         } else {
623                 setSelector($('prefs_hold_notify'), 'phone:email');
624         }
625 }
626
627 function myOPACShowPrefs() {
628         grabUserPrefs();
629         myOPACShowHitsPer();
630         myOPACShowDefFont();
631         myOPACShowHoldNotify();
632         myOPACShowDefLocation();
633         hideMe($('myopac_prefs_loading'));
634 }
635
636 var defSearchLocationDrawn = false;
637 var defDepthIndex = 0;
638 function myOPACShowDefLocation() {
639
640         var selector = $('prefs_def_location');
641         var rsel = $('prefs_def_range');
642
643         if(!defSearchLocationDrawn) {
644
645                 defSearchLocationDrawn = true;
646
647                 var org = G.user.prefs[PREF_DEF_LOCATION];
648
649                 if(!org) {
650                         $('myopac_pref_home_lib').checked = true;
651                         $('prefs_def_location').disabled = true;
652                         org = G.user.home_ou();
653                 }
654
655                 buildOrgSel(selector, globalOrgTree, 0);
656
657                 globalOrgTypes = globalOrgTypes.sort(
658                         function(a, b) {
659                                 if( a.depth() < b.depth() ) return -1;
660                                 return 1;
661                         }
662                 );
663
664                 iterate(globalOrgTypes,
665                         function(t) {
666                                 if( t.depth() <= findOrgDepth(org) ) {
667                                         setSelectorVal(rsel, defDepthIndex++, t.opac_label(), t.depth());
668                                         if( t.depth() == findOrgDepth(org) ) 
669                                                 setSelector(rsel, t.depth());
670                                 }
671                         }
672                 );
673         }
674
675         setSelector(selector, org);
676 }
677
678 function myOPACShowHitsPer() {
679         var hits = 10;
680         if(G.user.prefs[PREF_HITS_PER])
681                 hits = G.user.prefs[PREF_HITS_PER];
682         var hitsSel = $('prefs_hits_per');
683         setSelector(hitsSel, hits);
684 }
685
686 var userShown = false;
687 function myOPACShowSummary() {
688         if(userShown) return; userShown = true;
689         var req = new Request(FETCH_FLESHED_USER,G.user.session, G.user.id());
690         req.callback(_myOPACSummaryShowUer);
691         req.send();
692 }
693
694 var addrRowTemplate;
695 var notesTemplate;
696 function _myOPACSummaryShowUer(r) {
697
698         var user = r.getResultObject();
699         fleshedUser = user;
700         if(!user) return;
701
702         var iv1 = user.ident_value()+'';
703         if (iv1.length > 4) iv1 = iv1.replace(new RegExp(iv1.substring(0,iv1.length - 4)), '***********');
704
705         appendClear($('myopac_summary_first'),text(user.first_given_name()));
706         appendClear($('myopac_summary_middle'),text(user.second_given_name()));
707         appendClear($('myopac_summary_dayphone'),text(user.day_phone()));
708         appendClear($('myopac_summary_eveningphone'),text(user.evening_phone()));
709         appendClear($('myopac_summary_otherphone'),text(user.other_phone()));
710         appendClear($('myopac_summary_last'),text(user.family_name()));
711         appendClear($('myopac_summary_username'),text(user.usrname()));
712         appendClear($('myopac_summary_email'),text(user.email()));
713         appendClear($('myopac_summary_barcode'),text(user.card().barcode()));
714         appendClear($('myopac_summary_ident1'),text(iv1));
715         appendClear($('myopac_summary_homelib'),text(findOrgUnit(user.home_ou()).name()));
716         appendClear($('myopac_summary_create_date'),text(_trimTime(user.create_date())));
717
718         var req = new Request( 
719                 FETCH_USER_NOTES, G.user.session, {pub:1, patronid:G.user.id()});
720         req.callback(myopacDrawNotes);
721         req.send();
722
723
724         var tbody = $('myopac_addr_tbody');
725         var template;
726
727         if(addrRowTemplate) { 
728                 template = addrRowTemplate;
729         } else {
730                 template = tbody.removeChild($('myopac_addr_row'));
731                 addrRowTemplate = template;
732         }
733         removeChildren(tbody);
734
735         for( var a in user.addresses() ) {
736                 var row = template.cloneNode(true);
737                 myOPACDrawAddr(row, user.addresses()[a]);
738                 tbody.appendChild(row);
739         }
740 }
741
742
743 function myopacDrawNotes(r) {
744         var notes = r.getResultObject();
745         var tbody = $('myopac.notes.tbody');
746         if(!notesTemplate)
747                 notesTemplate = tbody.removeChild($('myopac.notes.tr'));
748
749         iterate(notes, 
750                 function(note) {
751                         unHideMe($('myopac.notes.div'));
752                         var row = notesTemplate.cloneNode(true);
753                         $n(row, 'title').appendChild(text(note.title()));
754                         $n(row, 'value').appendChild(text(note.value()));
755                         tbody.appendChild(row);
756                 }
757         );
758 }
759
760
761
762 function myOPACDrawAddr(row, addr) {
763
764         appendClear($n(row, 'myopac_addr_type'),text(addr.address_type()));
765         var street = (addr.street2()) ? addr.street1() + ", " + addr.street2() : addr.street1();
766         appendClear($n(row, 'myopac_addr_street'),text(street));
767         appendClear($n(row, 'myopac_addr_city'),text(addr.city()));
768         appendClear($n(row, 'myopac_addr_county'),text(addr.county()));
769         appendClear($n(row, 'myopac_addr_state'),text(addr.state()));
770         appendClear($n(row, 'myopac_addr_zip'),text(addr.post_code()));
771 }
772
773
774 function myOPACUpdateUsername() {
775         var username = $('myopac_new_username').value;
776         if(username == null || username == "") {
777                 alert($('myopac_username_error').innerHTML);
778                 return;
779         }
780
781         if( username.match(/.*\s.*/) ) {
782                 alert($('myopac_invalid_username').innerHTML);
783                 return;
784         }
785
786         /* first see if the requested username is taken */
787         var req = new Request(CHECK_USERNAME, G.user.session, username);
788         req.send(true);
789         var res = req.result();
790         if( res && res != G.user.id() ) {
791                 alertId('myopac_username_dup');
792                 return;
793         }
794
795         var req = new Request(UPDATE_USERNAME, G.user.session, username );
796         req.send(true);
797         if(req.result()) {
798
799                 var evt;
800                 var res = req.result();
801                 if(evt = checkILSEvent(res)) {
802                         alertILSEvent(res);
803                         return;
804                 }
805
806                 G.user.usrname(username);
807                 hideMe($('myopac_update_username_row'));
808                 userShown = false;
809                 myOPACShowSummary();
810                 return;
811         }
812
813         alert($('myopac_username_failure').innerHTML);
814 }
815
816 function myOPACUpdateEmail() {
817         var email = $('myopac_new_email').value;
818         if(email == null || email == "") {
819                 alert($('myopac_email_error').innerHTML);
820                 return;
821         }
822
823         var req = new Request(UPDATE_EMAIL, G.user.session, email );
824         req.send(true);
825         if(req.result()) {
826                 G.user.usrname(email);
827                 hideMe($('myopac_update_email_row'));
828                 userShown = false;
829                 myOPACShowSummary();
830                 return;
831         }
832
833         alert($('myopac_email_failure').innerHTML);
834 }
835
836
837 function myOPACUpdatePassword() {
838         var curpassword = $('myopac_current_password').value;
839         var password = $('myopac_new_password').value;
840         var password2 = $('myopac_new_password2').value;
841
842         if(     curpassword == null || curpassword == "" || 
843                         password == null || password == "" || 
844                         password2 == null || password2 == "" || password != password2 ) {
845                 alert($('myopac_password_error').innerHTML);
846                 return;
847         }
848
849         if(!strongPassword(password, true)) return;
850
851         var req = new Request(UPDATE_PASSWORD, G.user.session, password, curpassword );
852         req.send(true);
853         if(req.result()) {
854                 hideMe($('myopac_update_password_row'));
855                 userShown = false;
856                 myOPACShowSummary();
857                 alert($('pw_update_successful').innerHTML);
858                 return;
859         }
860
861         alert($('myopac_password_failure').innerHTML);
862 }
863
864
865
866
867 var containerTemplate;
868 function myOPACShowBookbags(force) {
869
870         var tbody =$('myopac_bookbag_tbody') ;
871
872         if(!containerTemplate) 
873                 containerTemplate = tbody.removeChild($('myopac_bookbag_tr'));
874         else if(!force) return;
875
876         removeChildren(tbody);
877
878         var containers = containerFetchAll();
879
880         var found = false;
881         for( var i in containers ) {
882                 found = true;
883                 var cont = containers[i];
884                 var row = containerTemplate.cloneNode(true);
885                 row.id = 'myopac_bookbag_row_' + cont.id();
886                 var link = $n(row, 'myopac_expand_bookbag');
887                 var dlink = $n(row, 'myopac_container_delete');
888                 link.appendChild( text(cont.name()) );
889                 link.setAttribute('href', 
890                         'javascript:myOPACExpandBookbag("' + cont.id() + '","' + cont.name() + '");');
891                 myOPACFetchBBItems( cont.id(), row );
892                 dlink.setAttribute('href', 'javascript:myOPACDeleteBookbag("'+cont.id()+'");');
893
894                 if( isTrue(cont.pub()) ) {
895                         unHideMe($n(row, 'myopac_bb_published_yes'));
896                         var link = $n(row, 'myopac_bb_published_view');
897                         link.setAttribute('href', buildExtrasLink( 'feed/bookbag/html-full/'+cont.id(), false));  
898                         link.setAttribute('target', '_blank' );
899                         unHideMe(link);
900
901                         link = $n(row, 'myopac_bb_published_atom');
902                         link.setAttribute('href', buildExtrasLink( 'feed/bookbag/atom-full/'+cont.id(), false));  
903                         link.setAttribute('target', '_blank' );
904                         unHideMe(link);
905
906                         link = $n(row, 'myopac_bb_make_unpublished');
907                         link.setAttribute('href', 'javascript:myOPACMakeBBPublished("'+cont.id()+'", true);');
908                         unHideMe(link);
909
910                 } else { 
911                         unHideMe($n(row, 'myopac_bb_published_no')); 
912                         var link = $n(row, 'myopac_bb_make_published');
913                         link.setAttribute('href', 'javascript:myOPACMakeBBPublished("'+cont.id()+'");');
914                         unHideMe(link);
915                 }
916
917                 tbody.appendChild(row); 
918         }
919
920         if(!found) unHideMe($('myopac_bookbags_none'));
921         else unHideMe($('myopac_bookbag_table'));       
922 }
923
924 function myOPACMakeBBPublished(bbid, hideme) {
925
926         var bb = fleshedContainers[bbid];
927
928         if(hideme) {
929                 if(!confirm($('myopac_make_unpublished_confirm').innerHTML)) return;
930                 bb.pub('f');
931         } else {
932                 if(!confirm($('myopac_make_published_confirm').innerHTML)) return;
933                 bb.pub('t');
934         }
935
936         var result = containerUpdate(bb);
937
938         var code = checkILSEvent(result);
939         if(code) { alertILSEvent(result); return; }
940
941         alert($('myopac_bb_update_success').innerHTML);
942         myOPACShowBookbags(true);
943 }
944
945
946
947 function myOPACDeleteBookbag(id) {
948         if( confirm( $('myopac_delete_bookbag_warn').innerHTML ) ) {
949                 var result = containerDelete(id);
950                 var code = checkILSEvent(result);
951                 if(code) { alertILSEvent(result); return; }
952                 alert($('myopac_bb_update_success').innerHTML);
953                 hideMe($('myopac_bookbag_items_table'));
954                 hideMe($('myopac_bookbag_items_name'));
955                 hideMe($('myopac_bookbag_no_items'));
956                 myOPACShowBookbags(true);
957         }
958 }
959
960 function myOPACFetchBBItems( id, row, block ) {
961         if(!block) {
962                 containerFlesh( id, _myOPACSetBBItems, { row: row }  );
963         } else {
964                 var cont = containerFlesh(id);
965                 myOPACSetBBItems( cont, row );
966         }
967 }
968
969 function _myOPACSetBBItems(r) { myOPACSetBBItems( r.getResultObject(), r.args.row ); }
970
971 function myOPACSetBBItems( container, row ) {
972         fleshedContainers[container.id()] = container;
973         var node = $n(row, 'myopac_bookbag_item_count');
974         removeChildren(node);
975         node.appendChild( text(container.items().length) );
976 }
977
978 var BBItemsRow;
979 function myOPACExpandBookbag( id, name ) {
980         
981         var tbody = $('myopac_bookbag_items_tbody');
982         if(!BBItemsRow) BBItemsRow = tbody.removeChild($('myopac_bookbag_items_row'));
983         removeChildren(tbody);
984         removeChildren($('myopac_bookbag_items_name'));
985
986         $('myopac_bookbag_items_name').appendChild(text(name));
987
988         if( fleshedContainers[id] ) {
989                 var len = fleshedContainers[id].items().length;
990
991                 if( len == 0 ) {
992                         unHideMe($('myopac_bookbag_no_items'));
993                         hideMe($('myopac_bookbag_items_table'));
994                         return;
995                 }
996
997                 hideMe($('myopac_bookbag_no_items'));
998                 unHideMe($('myopac_bookbag_items_table'));
999
1000                 for( var i = 0; i != len; i++ ) {
1001                         var row = BBItemsRow.cloneNode(true);
1002                         found = true;
1003
1004                         var item = fleshedContainers[id].items()[i];
1005                         var tlink = $n(row,'myopac_bookbag_items_title');
1006                         var alink = $n(row,'myopac_bookbag_items_author');
1007
1008                         var req = new Request( FETCH_RMODS, item.target_biblio_record_entry() );
1009                         req.request.tlink = tlink;
1010                         req.request.alink = alink;
1011                         req.callback(myOPACShowBBItem);
1012                         req.send();
1013
1014                         var clink = $n(row, 'myopac_bookbag_items_remove');
1015                         clink.setAttribute('href', 'javascript:myOPACRemoveBBItem("'+item.id()+'","'+id+'","'+name+'");');
1016
1017                         tbody.appendChild(row);
1018                 }
1019         }
1020 }
1021
1022 function myOPACRemoveBBItem( id, containerid, container_name ) {
1023         if(!confirm($('myopac_remove_bb_item_confirm').innerHTML)) return;
1024         var stat = containerRemoveItem( id );
1025         if(stat) alert($('myopac_bb_update_success').innerHTML);
1026         myOPACFetchBBItems( containerid, $('myopac_bookbag_row_' + containerid), true);
1027         myOPACExpandBookbag( containerid, container_name );
1028 }
1029
1030 function myOPACShowBBItem(r) {
1031         var record = r.getResultObject();
1032         buildTitleDetailLink(record, r.tlink);
1033         buildSearchLink(STYPE_AUTHOR, record.author(), r.alink);
1034 }
1035
1036 function myOPACCreateBookbag() {
1037         var name = $('myopac_bookbag_new_name').value;  
1038         if(!name) return;
1039
1040         var exists = false;
1041         for( var c in fleshedContainers ) { exists = true; break; }
1042
1043         /* let them know what they are getting into... */
1044         if(!exists) if(!confirm($('bb_create_warning').innerHTML)) return;
1045
1046         var result = containerCreate( name, $('bb_public_yes').checked );
1047         var code = checkILSEvent(result);
1048         if(code) { alertILSEvent(result); return; }
1049         if(result) alert($('myopac_bb_update_success').innerHTML);
1050         myOPACShowBookbags(true);
1051 }
1052
1053
1054 /* ---------------------------------------------------------------------- */
1055 /* Non cat circs */
1056 /* ---------------------------------------------------------------------- */
1057
1058 var nonCatCircIds;
1059 var nonCatTypes;
1060 /* if we have some circs, grab the non-cat types */
1061 function myOPACDrawNonCatCircs(r) {
1062         var ids = r.getResultObject();
1063         if(ids.length == 0) return;
1064         nonCatCircIds = ids;
1065         unHideMe($('non_cat_circs_div'));
1066         var req = new Request(FETCH_NON_CAT_TYPES, G.user.home_ou());
1067         req.callback(myOPACDrawNonCatCircs2);
1068         req.send();
1069 }
1070
1071
1072 /* now we have circs and the types.. draw each one */
1073 var nonCatTbody;
1074 var nonCatRow;
1075 function myOPACDrawNonCatCircs2(r) {
1076         nonCatTypes = r.getResultObject();
1077         nonCatTbody = $('non_cat_circs_tbody');
1078         if(!nonCatRow) nonCatRow = 
1079                 nonCatTbody.removeChild($('non_cat_circs_row'));
1080         removeChildren(nonCatTbody);
1081         for( var i in nonCatCircIds ) {
1082                 var req = new Request(FETCH_NON_CAT_CIRC, G.user.session, nonCatCircIds[i]);
1083                 req.callback(myOPACDrawNonCatCirc);
1084                 req.send();
1085         }
1086 }
1087
1088
1089 /* draw a single circ */
1090 function myOPACDrawNonCatCirc(r) {
1091         var circ = r.getResultObject();
1092
1093
1094         var type = grep(nonCatTypes, 
1095                 function(i){
1096                         return (i.id() == circ.item_type());
1097                 }
1098         )[0];
1099
1100
1101         var row = nonCatTbody.appendChild(nonCatRow.cloneNode(true));
1102         appendClear($n(row, 'circ_lib'), text(findOrgUnit(circ.circ_lib()).name()));
1103         appendClear($n(row, 'item_type'), text(type.name()));
1104
1105         var duration = interval_to_seconds(type.circ_duration());
1106         duration = parseInt(duration + '000');
1107
1108         var dtf = circ.circ_time();
1109
1110         /*Date.W3CDTF is not happy with the milliseonds, nor is it
1111         happy without minute component of the timezone */
1112         dtf = dtf.replace(/\.\d+/,'');
1113         dtf += ":00"; 
1114
1115         var start = new Date.W3CDTF();
1116         start.setW3CDTF(dtf);
1117         var due = new Date(  start.getTime() + duration );
1118         due = (due+'').replace(/(.*?:\d\d):.*/, '$1');
1119
1120         appendClear($n(row, 'circ_time'), text(due));
1121 }
1122
1123
1124
1125