added much control over holds during placement and after the fact
[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_SLIM, G.user.session);  
107         req.callback(myOPACDrawCheckedOutSlim);
108         req.send();
109 }
110
111
112 var checkedRowTemplate;
113 var circsCache = new Array();
114 var checkedDrawn = false;
115
116 function myOPACDrawCheckedOutSlim(r) {
117
118         var checked                     = r.getResultObject();
119         var tbody                       = $("myopac_checked_tbody");
120         var loading                     = $("myopac_checked_loading");
121         var none                                = $("myopac_checked_none");
122
123         if(checkedDrawn) return;
124         checkedDrawn = true;
125         if(!checkedRowTemplate) 
126                 checkedRowTemplate = tbody.removeChild($("myopac_checked_row"));
127
128         clearNodes( tbody, [ loading, none ] );
129
130         hideMe(loading); /* remove all children and start over */
131         if(!(checked && checked[0])) unHideMe(none);
132
133         for( var idx in checked ) {
134
135                 var circ    = checked[idx]
136                 var row = checkedRowTemplate.cloneNode(true);
137                 row.id = 'myopac_checked_row_ ' + circ.id();
138
139                 var due = circ.due_date();
140       due = due.replace(/[0-9][0-9]:.*$/,"");
141
142                 var dlink = $n( row, "myopac_checked_due" );
143                 var rlink = $n( row, "myopac_checked_renewals" );
144                 var rnlink = $n( row, "myopac_checked_renew_link" );
145
146                 dlink.appendChild(text(due));
147                 rlink.appendChild(text(circ.renewal_remaining()));
148                 unHideMe(row);
149                 rnlink.setAttribute('href', 'javascript:myOPACRenewCirc("'+circ.id()+'");');
150                 circsCache.push(circ);
151
152                 tbody.appendChild(row);
153
154                 var req = new Request(FETCH_MODS_FROM_COPY, circ.target_copy() );
155                 req.request.circ = circ.id();
156                 req.request.copy = circ.target_copy();
157                 req.callback(myOPACDrawCheckedTitle);
158                 req.send();
159         }
160 }
161
162 function myOPACDrawCheckedTitle(r) {
163         var record = r.getResultObject();
164         var circid = r.circ;
165
166         if(!record || checkILSEvent(record)) {
167                 var req = new Request( FETCH_COPY, r.copy );
168                 req.request.circ = circid
169                 req.callback(myOPACDrawNonCatalogedItem);
170                 req.send();
171                 return;
172         }
173
174         var row = $('myopac_checked_row_ ' + circid);
175         var tlink = $n( row, "myopac_checked_title_link" );
176         var alink = $n( row, "myopac_checked_author_link" );
177         buildTitleDetailLink(record, tlink);
178         buildSearchLink(STYPE_AUTHOR, record.author(), alink);
179 }
180
181 function myOPACDrawNonCatalogedItem(r) {
182         var copy = r.getResultObject();
183         var circid = r.circ;
184
185         var row = $('myopac_checked_row_ ' + circid);
186         var tlink = $n( row, "myopac_checked_title_link" );
187         var alink = $n( row, "myopac_checked_author_link" );
188
189         tlink.parentNode.appendChild(text(copy.dummy_title()));
190         alink.parentNode.appendChild(text(copy.dummy_author()));
191 }
192
193
194 function myOPACRenewCirc(circid) {
195
196         var circ;
197         for( var i = 0; i != circsCache.length; i++ ) 
198                 if(circsCache[i].id() == circid)
199                         circ = circsCache[i];
200
201         if(!confirm($('myopac_renew_confirm').innerHTML)) return;
202
203         var req = new Request(RENEW_CIRC, G.user.session, 
204                 { patron : G.user.id(), copyid : circ.target_copy() } );
205         req.send(true);
206         var res = req.result();
207
208         if(evt = checkILSEvent(res)) {
209                 if( evt != 0 ) {
210                         alert(evt);
211                         alertILSEvent(evt);
212                         return;
213                 }
214         }
215
216         alert($('myopac_renew_success').innerHTML);     
217         checkedDrawn = false;
218         myOPACShowChecked();
219 }
220
221
222
223 function myOPACShowHolds() {
224         var req = new Request(FETCH_HOLDS, G.user.session, G.user.id());        
225         req.callback(myOPACDrawHolds);
226         req.send();
227 }
228
229 var holdsTemplateRowOrig;
230 var holdsTemplateRow;
231 function myOPACDrawHolds(r) {
232
233         var tbody = $("myopac_holds_tbody");
234         if(holdsTemplateRow) return;
235         if(holdsTemplateRowOrig) {
236                 holdsTemplateRow = holdsTemplateRowOrig;
237                 removeChildren(tbody);
238         } else {
239                 holdsTemplateRow = tbody.removeChild($("myopac_holds_row"));
240                 holdsTemplateRowOrig = holdsTemplateRow;
241         }
242
243         hideMe($('myopac_holds_loading'));
244
245         var holds = r.getResultObject();
246
247         if(!holds || holds.length < 1) unHideMe($('myopac_holds_none'));
248         for( var i = 0; i != holds.length; i++ ) {
249
250                 var h = holds[i];
251                 holdCache[h.id()] = h;
252
253                 var row = holdsTemplateRow.cloneNode(true);
254                 row.id = "myopac_holds_row_" + h.id() + '_' + h.target();
255
256                 var formats = (h.holdable_formats()) ? h.holdable_formats() : null;
257                 var form = $n(row, "myopac_holds_formats");
258                 form.id = "myopac_holds_form_" + h.id() + '_' + h.target();
259                 if(formats) form.appendChild(text(formats));
260
261                 var orglink = $n(row, "myopac_holds_location");
262                 orglink.appendChild(text(findOrgUnit(h.pickup_lib()).name()));
263                 orglink.setAttribute('href', 'javascript:myOPACChangeHoldPickupLib('+h.id()+');');
264
265                 if(h.email_notify()) 
266                         $n(row, "myopac_holds_email_link").checked = true;
267                 else
268                         $n(row, "myopac_holds_email_link").checked = false;
269
270                 var plink = $n(row, "myopac_holds_phone_link");
271
272                 if( h.phone_notify() ) {
273                         plink.appendChild(text(h.phone_notify()));
274                         $n(row, 'myopac_holds_enable_phone').checked = true;
275                 } else {
276                         $n(row, 'myopac_holds_enable_phone').checked = false;
277                 }
278
279                 plink.setAttribute('href', 'javascript:myOPACChangeHoldPhone('+h.id()+');');
280                 tbody.appendChild(row);
281
282                 $n(row,'myopac_holds_cancel_link').setAttribute(
283                         'href','javascript:myOPACCancelHold("'+ h.id()+'");'); 
284                 unHideMe(row);
285
286                 myOPACDrawHoldTitle(h);
287         }
288 }
289
290 var holdsOrgRowTemplate;
291 function myOPACChangeHoldPickupLib(holdid) {
292         var hold = holdCache[holdid];
293         var row = $('myopac_holds_row_' + holdid + '_' + hold.target());
294         if(!holdsOrgRowTemplate)
295                 holdsOrgRowTemplate = $('myopac_holds_org_row').cloneNode(true);
296         var orgrow = holdsOrgRowTemplate;
297         var tbody = row.parentNode;
298         if( row.nextSibling ) tbody.insertBefore(orgrow, row.nextSibling);
299         else tbody.appendChild(orgrow);
300         var selector = $n(orgrow, 'myopac_holds_org_selector');
301         buildOrgSel( selector, globalOrgTree, 0 );
302         setSelector( selector, hold.pickup_lib() );
303
304         for( var i = 0; i != selector.options.length; i++ ) {
305                 var ou = findOrgUnit(selector.options[i].value);
306                 var t = findOrgType(ou.ou_type());
307                 if(!t.can_have_vols()) selector.options[i].disabled = true;
308         }
309
310         unHideMe(orgrow);
311
312         $n(orgrow, 'myopac_hold_org_update_submit').onclick = 
313                 function(){myOPACUpdateHoldPickupLib(tbody, orgrow, hold);} 
314         $n(orgrow, 'myopac_hold_org_update_cancel').onclick = 
315                 function(){tbody.removeChild(orgrow);}
316 }
317
318 function myOPACUpdateHoldPickupLib( tbody, orgrow, hold ) {
319
320         if( hold.capture_time() ) {
321                 alert($('myopac_cannot_change_pickup').textContent);
322                 return;
323         }
324
325         var org = getSelectorVal($n(orgrow, 'myopac_holds_org_selector'));
326         hold.pickup_lib(org);
327         tbody.removeChild(orgrow);
328         myOPACUpdateHold(hold);
329 }
330
331
332 function myOPACUpdateHold(hold) {
333         var req = new Request(UPDATE_HOLD, G.user.session, hold);
334         req.send(true);
335         var x = req.result();
336         holdsTemplateRow = null
337         myOPACShowHolds();
338 }
339
340
341 function myOPACChangeHoldPhone(holdid) {
342         var hold = holdCache[holdid];
343         var phone;
344
345         var origphone = hold.phone_notify();
346         if(!origphone) origphone = G.user.day_phone();
347
348         phone = prompt($('myopac_hold_phone_change').innerHTML, origphone);
349         if(!phone) return;
350         if( phone == hold.phone_notify() ) return;
351         if( !phone.match(REGEX_PHONE) ) {
352                 alert($('myopac_bad_phone').textContent);
353                 myOPACChangeHoldPhone(holdid);
354                 return;
355         }
356
357         hold.phone_notify(phone);
358         myOPACUpdateHold(hold);
359 }
360
361
362 function myopacChangeEmailNotify(node) {
363         var id = node.parentNode.parentNode.id.replace(/myopac_holds_row_/,"").replace(/_\d+$/,"");
364         var hold = holdCache[id];
365
366         if(!confirm($('myopac_hold_email_verify').innerHTML)) {
367                 if( hold.email_notify() ) node.checked = true;
368                 else node.checked = false;
369                 return;
370         }
371
372         if( hold.email_notify() ) {
373                 hold.email_notify(0);
374                 node.checked = false;
375         } else {
376                 hold.email_notify(1);
377                 node.checked = true;
378         }
379
380         myOPACUpdateHold(hold);
381 }
382
383 function myopacChangePhoneNotify(node) {
384         var id = node.parentNode.parentNode.id.replace(/myopac_holds_row_/,"").replace(/_\d+$/,"");
385         var hold = holdCache[id];
386
387         if(!node.checked) {
388                 if(!confirm($('myopac_hold_phone_verify').innerHTML)) {
389                         node.checked = true;
390                         return;
391                 }
392         }
393
394         if( hold.phone_notify() ) hold.phone_notify("");
395         else myOPACChangeHoldPhone(id);
396
397         myOPACUpdateHold(hold);
398 }
399
400 function myOPACCancelHold(holdid) {
401         if( confirm($('myopac_holds_cancel_verify').innerHTML) ) {
402                 holdsCancel(holdid);
403                 holdsTemplateRow = null
404                 myOPACShowHolds();
405         }
406 }
407
408
409
410 function myOPACDrawHoldTitle(hold) {
411         var method;
412         if(hold.hold_type() == "M") method = FETCH_MRMODS;
413         if(hold.hold_type() == "T") method = FETCH_RMODS;
414         var req = new Request(method, hold.target());
415         req.callback(myOPACFleshHoldTitle);
416         req.request.hold = hold.id();
417         req.send();
418 }
419
420 function myOPACFleshHoldTitle(r) {
421
422         var record = r.getResultObject();
423         var row = $("myopac_holds_row_" + r.hold + '_' + record.doc_id());
424         var title_link = $n(row, "myopac_holds_title_link");
425         var author_link = $n(row, "myopac_holds_author_link");
426
427         buildTitleDetailLink(record, title_link);
428         buildSearchLink(STYPE_AUTHOR, record.author(), author_link);
429
430         var form = $("myopac_holds_form_" + r.hold + '_' + record.doc_id());
431
432         if(form) {
433                 var img = elem("img");
434                 img.setAttribute("src", 
435                         buildImageLink('tor/' + record.types_of_resource()[0] + ".jpg"));
436                 addCSSClass(img, "myopac_form_pic");
437                 form.appendChild(img);
438         }
439 }
440
441 var finesShown = false;
442 function myOPACShowFines() {
443         if(finesShown) return; finesShown = true;
444         var req = new Request(FETCH_FINES_SUMMARY, G.user.session, G.user.id() );
445         req.callback(_myOPACShowFines);
446         req.send();
447 }
448
449 function _myOPACShowFines(r) {
450         hideMe($('myopac_fines_summary_loading'));
451         unHideMe($('myopac_fines_summary_row'));
452
453         var summary = r.getResultObject();
454         var total       = "0.00"; /* localization? */
455         var paid                = "0.00";
456         var balance = "0.00";
457         if( instanceOf(summary,mous) ) {
458
459                 total           = _finesFormatNumber(summary.total_owed());
460                 paid            = _finesFormatNumber(summary.total_paid());
461                 balance = _finesFormatNumber(summary.balance_owed());
462
463                 var req = new Request(FETCH_TRANSACTIONS, G.user.session, G.user.id() );
464                 req.callback(myOPACShowTransactions);
465                 req.send();
466         }
467
468         $('myopac_fines_summary_total').appendChild(text(total));
469         $('myopac_fines_summary_paid').appendChild(text(paid));
470         $('myopac_fines_summary_balance').appendChild(text(balance));
471 }
472
473 function _finesFormatNumber(num) {
474         if(isNull(num)) num = 0;
475         num = num + "";
476         if(num.length < 2 || !num.match(/\./)) num += ".00";
477         if(num.match(/\./) && num.charAt(num.length-2) == '.') num += "0";
478         return num;
479 }          
480
481 function _trimTime(time) { if(!time) return ""; return time.replace(/\ .*/,""); }
482
483 function _trimSeconds(time) { if(!time) return ""; return time.replace(/:\d\d\..*$/,""); }
484
485 function myOPACShowTransactions(r) {
486
487         if(myopacGenericTransTemplate || myopacCircTransTemplate) return;
488
489         var transactions = r.getResultObject();
490
491         for( var idx in transactions ) {
492
493                 var trans       = transactions[idx].transaction;
494                 var record      = transactions[idx].record;
495                 var circ                = transactions[idx].circ;
496
497                 if(trans.xact_type() == 'circulation') 
498                         myOPACShowCircTransaction(trans, record, circ);
499
500                 else if(trans.xact_type() == 'grocery' ) 
501                         myopacShowGenericTransaction( trans );
502         }
503 }
504
505 var myopacGenericTransTemplate;
506 function myopacShowGenericTransaction( trans ) {
507         var tbody = $('myopac_trans_tbody');
508
509         if(!myopacGenericTransTemplate) {
510                 myopacGenericTransTemplate = 
511                         tbody.removeChild($('myopac_trans_row'));
512                 removeChildren(tbody);
513         }
514
515         var row = myopacGenericTransTemplate.cloneNode(true);
516
517         $n(row,'myopac_trans_start').appendChild(
518                         text(_trimSeconds(trans.xact_start())));
519
520         $n(row,'myopac_trans_last_payment').appendChild(
521                         text(_trimSeconds(trans.last_payment_ts())));
522
523         $n(row,'myopac_trans_init_amount').appendChild(
524                         text(_finesFormatNumber(trans.total_owed())));
525
526         $n(row,'myopac_trans_total_paid').appendChild(
527                         text(_finesFormatNumber(trans.total_paid())));
528
529         $n(row,'myopac_trans_balance').appendChild(
530                         text(_finesFormatNumber(trans.balance_owed())));
531
532         var req = new Request(FETCH_MONEY_BILLING, G.user.session, trans.id());
533         req.send(true);
534         var bills = req.result();
535         if(bills && bills[0])
536                 $n(row,'myopac_trans_bill_type').appendChild(
537                                 text(bills[0].billing_type()));
538
539         tbody.appendChild(row);
540         unHideMe($('myopac_trans_div'));
541 }
542
543
544
545 /* draws a circulation transaction summary */
546 var myopacCircTransTemplate;
547 function myOPACShowCircTransaction(trans, record, circ) {
548         var tbody = $('myopac_circ_trans_tbody');
549
550         if(!myopacCircTransTemplate) {
551                 myopacCircTransTemplate = tbody.removeChild($('myopac_circ_trans_row'));
552                 removeChildren(tbody);
553         }
554
555         var row = myopacCircTransTemplate.cloneNode(true);
556
557         buildTitleDetailLink(record, $n(row,'myopac_circ_trans_title'));
558
559         $n(row,'myopac_circ_trans_author').appendChild(text(
560                 normalize(truncate(record.author(), 65))));
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         /*$n(row,'myopac_circ_trans_finished').appendChild(text(checkin));*/
570         if(checkin)
571                 appendClear($n(row,'myopac_circ_trans_finished'), text(checkin));
572
573         $n(row,'myopac_circ_trans_balance').
574                 appendChild(text(_finesFormatNumber(trans.balance_owed())));
575
576         tbody.appendChild(row);
577         unHideMe($('myopac_circ_trans_div'));
578 }
579
580
581 function myOPACSavePrefs() {
582         G.user.prefs[PREF_HITS_PER] = getSelectorVal($('prefs_hits_per'));
583         G.user.prefs[PREF_DEF_FONT] = getSelectorVal($('prefs_def_font'));
584         if(commitUserPrefs())
585                 alert($('prefs_update_success').innerHTML);
586         else alert($('prefs_update_failure').innerHTML);
587 }
588
589
590 function myOPACShowDefFont() {
591         var font;
592         if(G.user.prefs[PREF_DEF_FONT])
593                 font = G.user.prefs[PREF_DEF_FONT];
594         else font = "regular";
595         setSelector($('prefs_def_font'), font);
596 }
597
598 function myOPACShowPrefs() {
599         grabUserPrefs();
600         myOPACShowHitsPer();
601         myOPACShowDefFont();
602         hideMe($('myopac_prefs_loading'));
603 }
604
605 function myOPACShowHitsPer() {
606         var hits = 10;
607         if(G.user.prefs[PREF_HITS_PER])
608                 hits = G.user.prefs[PREF_HITS_PER];
609         var hitsSel = $('prefs_hits_per');
610         setSelector(hitsSel, hits);
611 }
612
613 var userShown = false;
614 function myOPACShowSummary() {
615         if(userShown) return; userShown = true;
616         var req = new Request(FETCH_FLESHED_USER,G.user.session, G.user.id());
617         req.callback(_myOPACSummaryShowUer);
618         req.send();
619 }
620
621 var addrRowTemplate;
622 function _myOPACSummaryShowUer(r) {
623
624         var user = r.getResultObject();
625         fleshedUser = user;
626         if(!user) return;
627
628         appendClear($('myopac_summary_first'),text(user.first_given_name()));
629         appendClear($('myopac_summary_middle'),text(user.second_given_name()));
630         appendClear($('myopac_summary_dayphone'),text(user.day_phone()));
631         appendClear($('myopac_summary_eveningphone'),text(user.evening_phone()));
632         appendClear($('myopac_summary_otherphone'),text(user.other_phone()));
633         appendClear($('myopac_summary_last'),text(user.family_name()));
634         appendClear($('myopac_summary_username'),text(user.usrname()));
635         appendClear($('myopac_summary_email'),text(user.email()));
636         appendClear($('myopac_summary_barcode'),text(user.card().barcode()));
637         appendClear($('myopac_summary_ident1'),text(user.ident_value()));
638         appendClear($('myopac_summary_ident2'),text(user.ident_value2()));
639         appendClear($('myopac_summary_homelib'),text(findOrgUnit(user.home_ou()).name()));
640         appendClear($('myopac_summary_create_date'),text(user.create_date()));
641
642         var tbody = $('myopac_addr_tbody');
643         var template;
644
645         if(addrRowTemplate) { 
646                 template = addrRowTemplate;
647         } else {
648                 template = tbody.removeChild($('myopac_addr_row'));
649                 addrRowTemplate = template;
650         }
651         removeChildren(tbody);
652
653         for( var a in user.addresses() ) {
654                 var row = template.cloneNode(true);
655                 myOPACDrawAddr(row, user.addresses()[a]);
656                 tbody.appendChild(row);
657         }
658 }
659
660 function myOPACDrawAddr(row, addr) {
661
662         appendClear($n(row, 'myopac_addr_type'),text(addr.address_type()));
663         var street = (addr.street2()) ? addr.street1() + ", " + addr.street2() : addr.street1();
664         appendClear($n(row, 'myopac_addr_street'),text(street));
665         appendClear($n(row, 'myopac_addr_city'),text(addr.city()));
666         appendClear($n(row, 'myopac_addr_county'),text(addr.county()));
667         appendClear($n(row, 'myopac_addr_state'),text(addr.state()));
668         appendClear($n(row, 'myopac_addr_zip'),text(addr.post_code()));
669 }
670
671
672 function myOPACUpdateUsername() {
673         var username = $('myopac_new_username').value;
674         if(username == null || username == "") {
675                 alert($('myopac_username_error').innerHTML);
676                 return;
677         }
678         var req = new Request(UPDATE_USERNAME, G.user.session, username );
679         req.send(true);
680         if(req.result()) {
681
682                 var evt;
683                 if(evt = checkILSEvent(req.result())) {
684                         alertILSEvent(evt);
685                         return;
686                 }
687
688                 G.user.usrname(username);
689                 hideMe($('myopac_update_username_row'));
690                 userShown = false;
691                 myOPACShowSummary();
692                 return;
693         }
694
695         alert($('myopac_username_failure').innerHTML);
696 }
697
698 function myOPACUpdateEmail() {
699         var email = $('myopac_new_email').value;
700         if(email == null || email == "") {
701                 alert($('myopac_email_error').innerHTML);
702                 return;
703         }
704
705         var req = new Request(UPDATE_EMAIL, G.user.session, email );
706         req.send(true);
707         if(req.result()) {
708                 G.user.usrname(email);
709                 hideMe($('myopac_update_email_row'));
710                 userShown = false;
711                 myOPACShowSummary();
712                 return;
713         }
714
715         alert($('myopac_email_failure').innerHTML);
716 }
717
718
719 function myOPACUpdatePassword() {
720         var curpassword = $('myopac_current_password').value;
721         var password = $('myopac_new_password').value;
722         var password2 = $('myopac_new_password2').value;
723
724         if(     curpassword == null || curpassword == "" || 
725                         password == null || password == "" || 
726                         password2 == null || password2 == "" || password != password2 ) {
727                 alert($('myopac_password_error').innerHTML);
728                 return;
729         }
730
731         var req = new Request(UPDATE_PASSWORD, G.user.session, password, curpassword );
732         req.send(true);
733         if(req.result()) {
734                 G.user.usrname(password);
735                 hideMe($('myopac_update_password_row'));
736                 userShown = false;
737                 myOPACShowSummary();
738                 return;
739         }
740
741         alert($('myopac_password_failure').innerHTML);
742 }
743
744
745
746
747 var containerTemplate;
748 function myOPACShowBookbags(force) {
749
750         var tbody =$('myopac_bookbag_tbody') ;
751
752         if(!containerTemplate) 
753                 containerTemplate = tbody.removeChild($('myopac_bookbag_tr'));
754         else if(!force) return;
755
756         removeChildren(tbody);
757
758         var containers = containerFetchAll();
759
760         var found = false;
761         for( var i in containers ) {
762                 found = true;
763                 var cont = containers[i];
764                 var row = containerTemplate.cloneNode(true);
765                 row.id = 'myopac_bookbag_row_' + cont.id();
766                 var link = $n(row, 'myopac_expand_bookbag');
767                 var dlink = $n(row, 'myopac_container_delete');
768                 link.appendChild( text(cont.name()) );
769                 link.setAttribute('href', 
770                         'javascript:myOPACExpandBookbag("' + cont.id() + '","' + cont.name() + '");');
771                 myOPACFetchBBItems( cont.id(), row );
772                 dlink.setAttribute('href', 'javascript:myOPACDeleteBookbag("'+cont.id()+'");');
773
774                 if( cont.pub() ) {
775                         unHideMe($n(row, 'myopac_bb_published_yes'));
776                         var link = $n(row, 'myopac_bb_published_view');
777                         link.setAttribute('href', buildExtrasLink( 'bbags.xml?bb='+cont.id(), false));  
778                         link.setAttribute('target', '_blank' );
779                         unHideMe(link);
780
781                         link = $n(row, 'myopac_bb_make_unpublished');
782                         link.setAttribute('href', 'javascript:myOPACMakeBBPublished("'+cont.id()+'", true);');
783                         unHideMe(link);
784
785                 } else { 
786                         unHideMe($n(row, 'myopac_bb_published_no')); 
787                         var link = $n(row, 'myopac_bb_make_published');
788                         link.setAttribute('href', 'javascript:myOPACMakeBBPublished("'+cont.id()+'");');
789                         unHideMe(link);
790                 }
791
792                 tbody.appendChild(row); 
793         }
794
795         if(!found) unHideMe($('myopac_bookbags_none'));
796         else unHideMe($('myopac_bookbag_table'));       
797 }
798
799 function myOPACMakeBBPublished(bbid, hideme) {
800
801         var bb = fleshedContainers[bbid];
802
803         if(hideme) {
804                 if(!confirm($('myopac_make_unpublished_confirm').innerHTML)) return;
805                 bb.pub(0);
806         } else {
807                 if(!confirm($('myopac_make_published_confirm').innerHTML)) return;
808                 bb.pub(1);
809         }
810
811         var result = containerUpdate(bb);
812
813         var code = checkILSEvent(result);
814         if(code) { alertILSEvent(code); return; }
815
816         if(result) alert($('myopac_bb_update_success').innerHTML);
817         myOPACShowBookbags(true);
818 }
819
820
821
822 function myOPACDeleteBookbag(id) {
823         if( confirm( $('myopac_delete_bookbag_warn').innerHTML ) ) {
824                 var result = containerDelete(id);
825                 var code = checkILSEvent(result);
826                 if(code) { alertILSEvent(code); return; }
827                 hideMe($('myopac_bookbag_items_table'));
828                 hideMe($('myopac_bookbag_items_name'));
829                 hideMe($('myopac_bookbag_no_items'));
830                 myOPACShowBookbags(true);
831         }
832 }
833
834 function myOPACFetchBBItems( id, row, block ) {
835         if(!block) {
836                 containerFlesh( id, _myOPACSetBBItems, { row: row }  );
837         } else {
838                 var cont = containerFlesh(id);
839                 myOPACSetBBItems( cont, row );
840         }
841 }
842
843 function _myOPACSetBBItems(r) { myOPACSetBBItems( r.getResultObject(), r.args.row ); }
844
845 function myOPACSetBBItems( container, row ) {
846         fleshedContainers[container.id()] = container;
847         var node = $n(row, 'myopac_bookbag_item_count');
848         removeChildren(node);
849         node.appendChild( text(container.items().length) );
850 }
851
852 var BBItemsRow;
853 function myOPACExpandBookbag( id, name ) {
854         
855         var tbody = $('myopac_bookbag_items_tbody');
856         if(!BBItemsRow) BBItemsRow = tbody.removeChild($('myopac_bookbag_items_row'));
857         removeChildren(tbody);
858         removeChildren($('myopac_bookbag_items_name'));
859
860         $('myopac_bookbag_items_name').appendChild(text(name));
861
862         if( fleshedContainers[id] ) {
863                 var len = fleshedContainers[id].items().length;
864
865                 if( len == 0 ) {
866                         unHideMe($('myopac_bookbag_no_items'));
867                         hideMe($('myopac_bookbag_items_table'));
868                         return;
869                 }
870
871                 hideMe($('myopac_bookbag_no_items'));
872                 unHideMe($('myopac_bookbag_items_table'));
873
874                 for( var i = 0; i != len; i++ ) {
875                         var row = BBItemsRow.cloneNode(true);
876                         found = true;
877
878                         var item = fleshedContainers[id].items()[i];
879                         var tlink = $n(row,'myopac_bookbag_items_title');
880                         var alink = $n(row,'myopac_bookbag_items_author');
881
882                         var req = new Request( FETCH_RMODS, item.target_biblio_record_entry() );
883                         req.request.tlink = tlink;
884                         req.request.alink = alink;
885                         req.callback(myOPACShowBBItem);
886                         req.send();
887
888                         var clink = $n(row, 'myopac_bookbag_items_remove');
889                         clink.setAttribute('href', 'javascript:myOPACRemoveBBItem("'+item.id()+'","'+id+'","'+name+'");');
890
891                         tbody.appendChild(row);
892                 }
893         }
894 }
895
896 function myOPACRemoveBBItem( id, containerid, container_name ) {
897         if(!confirm($('myopac_remove_bb_item_confirm').innerHTML)) return;
898         containerRemoveItem( id );
899         myOPACFetchBBItems( containerid, $('myopac_bookbag_row_' + containerid), true);
900         myOPACExpandBookbag( containerid, container_name );
901 }
902
903 function myOPACShowBBItem(r) {
904         var record = r.getResultObject();
905         buildTitleDetailLink(record, r.tlink);
906         buildSearchLink(STYPE_AUTHOR, record.author(), r.alink);
907 }
908
909 function myOPACCreateBookbag() {
910         var name = $('myopac_bookbag_new_name').value;  
911         if(!name) return;
912
913         var exists = false;
914         for( var c in fleshedContainers ) { exists = true; break; }
915
916         /* let them know what they are getting into... */
917         if(!exists) if(!confirm($('bb_create_warning').innerHTML)) return;
918
919         var result = containerCreate( name, $('bb_public_yes').checked );
920         var code = checkILSEvent(result);
921         if(code) { alertILSEvent(code); return; }
922         myOPACShowBookbags(true);
923 }
924
925