added some status popups
[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         removeChildren(tbody);
749
750         iterate(notes, 
751                 function(note) {
752                         unHideMe($('myopac.notes.div'));
753                         var row = notesTemplate.cloneNode(true);
754                         $n(row, 'title').appendChild(text(note.title()));
755                         $n(row, 'value').appendChild(text(note.value()));
756                         tbody.appendChild(row);
757                 }
758         );
759 }
760
761
762
763 function myOPACDrawAddr(row, addr) {
764
765         appendClear($n(row, 'myopac_addr_type'),text(addr.address_type()));
766         var street = (addr.street2()) ? addr.street1() + ", " + addr.street2() : addr.street1();
767         appendClear($n(row, 'myopac_addr_street'),text(street));
768         appendClear($n(row, 'myopac_addr_city'),text(addr.city()));
769         appendClear($n(row, 'myopac_addr_county'),text(addr.county()));
770         appendClear($n(row, 'myopac_addr_state'),text(addr.state()));
771         appendClear($n(row, 'myopac_addr_zip'),text(addr.post_code()));
772 }
773
774
775 function myOPACUpdateUsername() {
776         var username = $('myopac_new_username').value;
777         if(username == null || username == "") {
778                 alert($('myopac_username_error').innerHTML);
779                 return;
780         }
781
782         if( username.match(/.*\s.*/) ) {
783                 alert($('myopac_invalid_username').innerHTML);
784                 return;
785         }
786
787         /* first see if the requested username is taken */
788         var req = new Request(CHECK_USERNAME, G.user.session, username);
789         req.send(true);
790         var res = req.result();
791         if( res && res != G.user.id() ) {
792                 alertId('myopac_username_dup');
793                 return;
794         }
795
796         var req = new Request(UPDATE_USERNAME, G.user.session, username );
797         req.send(true);
798         if(req.result()) {
799
800                 var evt;
801                 var res = req.result();
802                 if(evt = checkILSEvent(res)) {
803                         alertILSEvent(res);
804                         return;
805                 }
806
807                 G.user.usrname(username);
808                 hideMe($('myopac_update_username_row'));
809                 userShown = false;
810                 alertId('myopac_username_success');
811                 myOPACShowSummary();
812                 return;
813         }
814
815         alert($('myopac_username_failure').innerHTML);
816 }
817
818 function myOPACUpdateEmail() {
819         var email = $('myopac_new_email').value;
820         if(email == null || email == "") {
821                 alert($('myopac_email_error').innerHTML);
822                 return;
823         }
824
825         var req = new Request(UPDATE_EMAIL, G.user.session, email );
826         req.send(true);
827         if(req.result()) {
828                 G.user.usrname(email);
829                 hideMe($('myopac_update_email_row'));
830                 userShown = false;
831                 alertId('myopac_email_success');
832                 myOPACShowSummary();
833                 return;
834         }
835
836         alert($('myopac_email_failure').innerHTML);
837 }
838
839
840 function myOPACUpdatePassword() {
841         var curpassword = $('myopac_current_password').value;
842         var password = $('myopac_new_password').value;
843         var password2 = $('myopac_new_password2').value;
844
845         if(     curpassword == null || curpassword == "" || 
846                         password == null || password == "" || 
847                         password2 == null || password2 == "" || password != password2 ) {
848                 alert($('myopac_password_error').innerHTML);
849                 return;
850         }
851
852         if(!strongPassword(password, true)) return;
853
854         var req = new Request(UPDATE_PASSWORD, G.user.session, password, curpassword );
855         req.send(true);
856         if(req.result()) {
857                 hideMe($('myopac_update_password_row'));
858                 userShown = false;
859                 alertId('myopac_password_success');
860                 myOPACShowSummary();
861                 return;
862         }
863
864         alert($('myopac_password_failure').innerHTML);
865 }
866
867
868
869
870 var containerTemplate;
871 function myOPACShowBookbags(force) {
872
873         var tbody =$('myopac_bookbag_tbody') ;
874
875         if(!containerTemplate) 
876                 containerTemplate = tbody.removeChild($('myopac_bookbag_tr'));
877         else if(!force) return;
878
879         removeChildren(tbody);
880
881         var containers = containerFetchAll();
882
883         var found = false;
884         for( var i in containers ) {
885                 found = true;
886                 var cont = containers[i];
887                 var row = containerTemplate.cloneNode(true);
888                 row.id = 'myopac_bookbag_row_' + cont.id();
889                 var link = $n(row, 'myopac_expand_bookbag');
890                 var dlink = $n(row, 'myopac_container_delete');
891                 link.appendChild( text(cont.name()) );
892                 link.setAttribute('href', 
893                         'javascript:myOPACExpandBookbag("' + cont.id() + '","' + cont.name() + '");');
894                 myOPACFetchBBItems( cont.id(), row );
895                 dlink.setAttribute('href', 'javascript:myOPACDeleteBookbag("'+cont.id()+'");');
896
897                 if( isTrue(cont.pub()) ) {
898                         unHideMe($n(row, 'myopac_bb_published_yes'));
899                         var link = $n(row, 'myopac_bb_published_view');
900                         link.setAttribute('href', buildExtrasLink( 'feed/bookbag/html-full/'+cont.id(), false));  
901                         link.setAttribute('target', '_blank' );
902                         unHideMe(link);
903
904                         link = $n(row, 'myopac_bb_published_atom');
905                         link.setAttribute('href', buildExtrasLink( 'feed/bookbag/atom-full/'+cont.id(), false));  
906                         link.setAttribute('target', '_blank' );
907                         unHideMe(link);
908
909                         link = $n(row, 'myopac_bb_make_unpublished');
910                         link.setAttribute('href', 'javascript:myOPACMakeBBPublished("'+cont.id()+'", true);');
911                         unHideMe(link);
912
913                 } else { 
914                         unHideMe($n(row, 'myopac_bb_published_no')); 
915                         var link = $n(row, 'myopac_bb_make_published');
916                         link.setAttribute('href', 'javascript:myOPACMakeBBPublished("'+cont.id()+'");');
917                         unHideMe(link);
918                 }
919
920                 tbody.appendChild(row); 
921         }
922
923         if(!found) unHideMe($('myopac_bookbags_none'));
924         else unHideMe($('myopac_bookbag_table'));       
925 }
926
927 function myOPACMakeBBPublished(bbid, hideme) {
928
929         var bb = fleshedContainers[bbid];
930
931         if(hideme) {
932                 if(!confirm($('myopac_make_unpublished_confirm').innerHTML)) return;
933                 bb.pub('f');
934         } else {
935                 if(!confirm($('myopac_make_published_confirm').innerHTML)) return;
936                 bb.pub('t');
937         }
938
939         var result = containerUpdate(bb);
940
941         var code = checkILSEvent(result);
942         if(code) { alertILSEvent(result); return; }
943
944         alert($('myopac_bb_update_success').innerHTML);
945         myOPACShowBookbags(true);
946 }
947
948
949
950 function myOPACDeleteBookbag(id) {
951         if( confirm( $('myopac_delete_bookbag_warn').innerHTML ) ) {
952                 var result = containerDelete(id);
953                 var code = checkILSEvent(result);
954                 if(code) { alertILSEvent(result); return; }
955                 alert($('myopac_bb_update_success').innerHTML);
956                 hideMe($('myopac_bookbag_items_table'));
957                 hideMe($('myopac_bookbag_items_name'));
958                 hideMe($('myopac_bookbag_no_items'));
959                 myOPACShowBookbags(true);
960         }
961 }
962
963 function myOPACFetchBBItems( id, row, block ) {
964         if(!block) {
965                 containerFlesh( id, _myOPACSetBBItems, { row: row }  );
966         } else {
967                 var cont = containerFlesh(id);
968                 myOPACSetBBItems( cont, row );
969         }
970 }
971
972 function _myOPACSetBBItems(r) { myOPACSetBBItems( r.getResultObject(), r.args.row ); }
973
974 function myOPACSetBBItems( container, row ) {
975         fleshedContainers[container.id()] = container;
976         var node = $n(row, 'myopac_bookbag_item_count');
977         removeChildren(node);
978         node.appendChild( text(container.items().length) );
979 }
980
981 var BBItemsRow;
982 function myOPACExpandBookbag( id, name ) {
983         
984         var tbody = $('myopac_bookbag_items_tbody');
985         if(!BBItemsRow) BBItemsRow = tbody.removeChild($('myopac_bookbag_items_row'));
986         removeChildren(tbody);
987         removeChildren($('myopac_bookbag_items_name'));
988
989         $('myopac_bookbag_items_name').appendChild(text(name));
990
991         if( fleshedContainers[id] ) {
992                 var len = fleshedContainers[id].items().length;
993
994                 if( len == 0 ) {
995                         unHideMe($('myopac_bookbag_no_items'));
996                         hideMe($('myopac_bookbag_items_table'));
997                         return;
998                 }
999
1000                 hideMe($('myopac_bookbag_no_items'));
1001                 unHideMe($('myopac_bookbag_items_table'));
1002
1003                 for( var i = 0; i != len; i++ ) {
1004                         var row = BBItemsRow.cloneNode(true);
1005                         found = true;
1006
1007                         var item = fleshedContainers[id].items()[i];
1008                         var tlink = $n(row,'myopac_bookbag_items_title');
1009                         var alink = $n(row,'myopac_bookbag_items_author');
1010
1011                         var req = new Request( FETCH_RMODS, item.target_biblio_record_entry() );
1012                         req.request.tlink = tlink;
1013                         req.request.alink = alink;
1014                         req.callback(myOPACShowBBItem);
1015                         req.send();
1016
1017                         var clink = $n(row, 'myopac_bookbag_items_remove');
1018                         clink.setAttribute('href', 'javascript:myOPACRemoveBBItem("'+item.id()+'","'+id+'","'+name+'");');
1019
1020                         tbody.appendChild(row);
1021                 }
1022         }
1023 }
1024
1025 function myOPACRemoveBBItem( id, containerid, container_name ) {
1026         if(!confirm($('myopac_remove_bb_item_confirm').innerHTML)) return;
1027         var stat = containerRemoveItem( id );
1028         if(stat) alert($('myopac_bb_update_success').innerHTML);
1029         myOPACFetchBBItems( containerid, $('myopac_bookbag_row_' + containerid), true);
1030         myOPACExpandBookbag( containerid, container_name );
1031 }
1032
1033 function myOPACShowBBItem(r) {
1034         var record = r.getResultObject();
1035         buildTitleDetailLink(record, r.tlink);
1036         buildSearchLink(STYPE_AUTHOR, record.author(), r.alink);
1037 }
1038
1039 function myOPACCreateBookbag() {
1040         var name = $('myopac_bookbag_new_name').value;  
1041         if(!name) return;
1042
1043         var exists = false;
1044         for( var c in fleshedContainers ) { exists = true; break; }
1045
1046         /* let them know what they are getting into... */
1047         if(!exists) if(!confirm($('bb_create_warning').innerHTML)) return;
1048
1049         var result = containerCreate( name, $('bb_public_yes').checked );
1050         var code = checkILSEvent(result);
1051         if(code) { alertILSEvent(result); return; }
1052         if(result) alert($('myopac_bb_update_success').innerHTML);
1053         myOPACShowBookbags(true);
1054 }
1055
1056
1057 /* ---------------------------------------------------------------------- */
1058 /* Non cat circs */
1059 /* ---------------------------------------------------------------------- */
1060
1061 var nonCatCircIds;
1062 var nonCatTypes;
1063 /* if we have some circs, grab the non-cat types */
1064 function myOPACDrawNonCatCircs(r) {
1065         var ids = r.getResultObject();
1066         if(ids.length == 0) return;
1067         nonCatCircIds = ids;
1068         unHideMe($('non_cat_circs_div'));
1069         var req = new Request(FETCH_NON_CAT_TYPES, G.user.home_ou());
1070         req.callback(myOPACDrawNonCatCircs2);
1071         req.send();
1072 }
1073
1074
1075 /* now we have circs and the types.. draw each one */
1076 var nonCatTbody;
1077 var nonCatRow;
1078 function myOPACDrawNonCatCircs2(r) {
1079         nonCatTypes = r.getResultObject();
1080         nonCatTbody = $('non_cat_circs_tbody');
1081         if(!nonCatRow) nonCatRow = 
1082                 nonCatTbody.removeChild($('non_cat_circs_row'));
1083         removeChildren(nonCatTbody);
1084         for( var i in nonCatCircIds ) {
1085                 var req = new Request(FETCH_NON_CAT_CIRC, G.user.session, nonCatCircIds[i]);
1086                 req.callback(myOPACDrawNonCatCirc);
1087                 req.send();
1088         }
1089 }
1090
1091
1092 /* draw a single circ */
1093 function myOPACDrawNonCatCirc(r) {
1094         var circ = r.getResultObject();
1095
1096
1097         var type = grep(nonCatTypes, 
1098                 function(i){
1099                         return (i.id() == circ.item_type());
1100                 }
1101         )[0];
1102
1103
1104         var row = nonCatTbody.appendChild(nonCatRow.cloneNode(true));
1105         appendClear($n(row, 'circ_lib'), text(findOrgUnit(circ.circ_lib()).name()));
1106         appendClear($n(row, 'item_type'), text(type.name()));
1107
1108         var duration = interval_to_seconds(type.circ_duration());
1109         duration = parseInt(duration + '000');
1110
1111         var dtf = circ.circ_time();
1112
1113         /*Date.W3CDTF is not happy with the milliseonds, nor is it
1114         happy without minute component of the timezone */
1115         dtf = dtf.replace(/\.\d+/,'');
1116         dtf += ":00"; 
1117
1118         var start = new Date.W3CDTF();
1119         start.setW3CDTF(dtf);
1120         var due = new Date(  start.getTime() + duration );
1121         due = (due+'').replace(/(.*?:\d\d):.*/, '$1');
1122
1123         appendClear($n(row, 'circ_time'), text(due));
1124 }
1125
1126
1127
1128