disabled checkout renewal in the current opac until the code is more normalized ...
[Evergreen.git] / Open-ILS / web / opac / skin / default / js / myopac.js
1
2 attachEvt("common", "run", myOPACInit );
3 attachEvt("common", "loggedIn", myOPACInit );
4
5 var fleshedUser = null;
6
7
8 function clearNodes( node, keepArray ) {
9         if(!node) return;
10         for( var n in keepArray ) node.removeChild(keepArray[n]);
11         removeChildren(node);
12         for( var n in keepArray ) node.appendChild(keepArray[n]);
13 }
14
15 function myOPACInit() {
16         if(!(G.user && G.user.session)) initLogin();
17         else myOPACChangePage( "summary" );
18 }
19
20
21 function myOPACChangePage( page ) {
22         
23         var s = $("myopac_summary_td");
24         var c = $("myopac_checked_td");
25         var f = $("myopac_fines_td");
26         var h = $("myopac_holds_td");
27         var p = $("myopac_prefs_td");
28
29         var ss = $("myopac_summary_div");
30         var cc = $("myopac_checked_div");
31         var ff = $("myopac_fines_div");
32         var hh = $("myopac_holds_div");
33         var pp = $("myopac_prefs_div");
34
35         var cls = "myopac_link";
36         var acls = "myopac_link_active";
37
38         hideMe(ss);
39         hideMe(cc); hideMe(ff);
40         hideMe(hh); hideMe(pp);
41
42         removeCSSClass(s, acls );
43         removeCSSClass(c, acls );
44         removeCSSClass(f, acls );
45         removeCSSClass(h, acls );
46         removeCSSClass(p, acls );
47
48         switch( page ) {
49
50                 case "summary": 
51                         unHideMe(ss);
52                         addCSSClass(s, acls );
53                         myOPACShowSummary();
54                         break;
55
56                 case "checked": 
57                         unHideMe(cc);
58                         addCSSClass(c, acls );
59                         myOPACShowChecked();
60                         break;
61
62                 case "holds": 
63                         unHideMe(hh);
64                         addCSSClass(h, acls );
65                         myOPACShowHolds();
66                         break;
67
68                 case "fines": 
69                         unHideMe(ff);
70                         addCSSClass(f, acls );
71                         myOPACShowFines();
72                         break;
73
74                 case "prefs": 
75                         unHideMe(pp);
76                         addCSSClass(p, acls );
77                         myOPACShowPrefs();
78                         break;
79         }
80
81         /*
82         alert('Classes:\n' + s.className + '\n' + c.className + 
83                 '\n' + h.className + '\n' + f.className + '\n' + p.className);
84                 */
85 }
86
87 function myOPACShowChecked() {
88         var req = new Request(FETCH_CHECKED_OUT_SLIM, G.user.session);  
89         req.callback(myOPACDrawCheckedOutSlim);
90         req.send();
91 }
92
93
94 var checkedRowTemplate;
95 var circsCache = new Array();
96 var checkedDrawn = false;
97
98 function myOPACDrawCheckedOutSlim(r) {
99
100         var checked                     = r.getResultObject();
101         var tbody                       = $("myopac_checked_tbody");
102         var loading                     = $("myopac_checked_loading");
103         var none                                = $("myopac_checked_none");
104
105         if(checkedDrawn) return;
106         checkedDrawn = true;
107         if(!checkedRowTemplate) 
108                 checkedRowTemplate = tbody.removeChild($("myopac_checked_row"));
109
110         clearNodes( tbody, [ loading, none ] );
111
112         hideMe(loading); /* remove all children and start over */
113         if(!(checked && checked[0])) unHideMe(none);
114
115         for( var idx in checked ) {
116
117                 var circ    = checked[idx]
118                 var row = checkedRowTemplate.cloneNode(true);
119                 row.id = 'myopac_checked_row_ ' + circ.id();
120
121                 var due = circ.due_date();
122       due = due.replace(/[0-9][0-9]:.*$/,"");
123
124                 var dlink = $n( row, "myopac_checked_due" );
125                 var rlink = $n( row, "myopac_checked_renewals" );
126                 var rnlink = $n( row, "myopac_checked_renew_link" );
127
128                 dlink.appendChild(text(due));
129                 rlink.appendChild(text(circ.renewal_remaining()));
130                 unHideMe(row);
131                 rnlink.setAttribute('href', 'javascript:myOPACRenewCirc("'+circ.id()+'");');
132                 circsCache.push(circ);
133
134                 tbody.appendChild(row);
135
136                 var req = new Request(FETCH_MODS_FROM_COPY, circ.target_copy() );
137                 req.request.circ = circ.id();
138                 req.callback(myOPACDrawCheckedTitle);
139                 req.send();
140         }
141 }
142
143 function myOPACDrawCheckedTitle(r) {
144         var circid = r.circ;
145         var row = $('myopac_checked_row_ ' + circid);
146         var record = r.getResultObject();
147         var tlink = $n( row, "myopac_checked_title_link" );
148         var alink = $n( row, "myopac_checked_author_link" );
149         buildTitleDetailLink(record, tlink);
150         buildSearchLink(STYPE_AUTHOR, record.author(), alink);
151 }
152
153
154 function myOPACRenewCirc(circid) {
155         alert("Functionality currently under construction");
156         return;
157
158         var circ;
159         for( var i = 0; i != circsCache.length; i++ ) 
160                 if(circsCache[i].id() == circid)
161                         circ = circsCache[i];
162
163         var req = new Request(RENEW_CIRC, G.user.session, circ );
164         req.send(true);
165         var res = req.result();
166         if(res.status) {
167                 alert(res.text);
168                 return;
169         }
170         alert($('myopac_renew_success').innerHTML);     
171         checkedDrawn = false;
172         myOPACShowChecked();
173 }
174
175
176
177 function myOPACShowHolds() {
178         var req = new Request(FETCH_HOLDS, G.user.session, G.user.id());        
179         req.callback(myOPACDrawHolds);
180         req.send();
181 }
182
183 var holdsTemplateRowOrig;
184 var holdsTemplateRow;
185 function myOPACDrawHolds(r) {
186
187         var tbody = $("myopac_holds_tbody");
188         if(holdsTemplateRow) return;
189         if(holdsTemplateRowOrig) {
190                 holdsTemplateRow = holdsTemplateRowOrig;
191                 removeChildren(tbody);
192         } else {
193                 holdsTemplateRow = tbody.removeChild($("myopac_holds_row"));
194                 holdsTemplateRowOrig = holdsTemplateRow;
195         }
196
197         hideMe($('myopac_holds_loading'));
198
199         var holds = r.getResultObject();
200
201         if(!holds || holds.length < 1) unHideMe($('myopac_holds_none'));
202         for( var i = 0; i != holds.length; i++ ) {
203
204                 var h = holds[i];
205                 var row = holdsTemplateRow.cloneNode(true);
206                 row.id = "myopac_holds_row_" + h.id() + '_' + h.target();
207
208                 var formats = (h.holdable_formats()) ? h.holdable_formats() : null;
209                 var form = $n(row, "myopac_holds_formats");
210                 form.id = "myopac_holds_form_" + h.id() + '_' + h.target();
211                 if(formats) form.appendChild(text(formats));
212
213                 $n(row, "myopac_holds_location").
214                         appendChild(text(findOrgUnit(h.pickup_lib()).name()));
215                 $n(row, "myopac_holds_email_link").
216                         appendChild(text(h.email_notify()));
217                 $n(row, "myopac_holds_phone_link").
218                         appendChild(text(h.phone_notify()));
219                 tbody.appendChild(row);
220
221                 $n(row,'myopac_holds_cancel_link').setAttribute(
222                         'href','javascript:myOPACCancelHold("'+ h.id()+'");'); 
223                 unHideMe(row);
224
225                 myOPACDrawHoldTitle(h);
226         }
227 }
228
229 function myOPACCancelHold(holdid) {
230         if( confirm($('myopac_holds_cancel_verify').innerHTML) ) {
231                 holdsCancel(holdid);
232                 holdsTemplateRow = null
233                 myOPACShowHolds();
234         }
235 }
236
237
238
239 function myOPACDrawHoldTitle(hold) {
240         var method;
241         if(hold.hold_type() == "M") method = FETCH_MRMODS;
242         if(hold.hold_type() == "T") method = FETCH_RMODS;
243         var req = new Request(method, hold.target());
244         req.callback(myOPACFleshHoldTitle);
245         req.request.hold = hold.id();
246         req.send();
247 }
248
249 function myOPACFleshHoldTitle(r) {
250
251         var record = r.getResultObject();
252         var row = $("myopac_holds_row_" + r.hold + '_' + record.doc_id());
253         var title_link = $n(row, "myopac_holds_title_link");
254         var author_link = $n(row, "myopac_holds_author_link");
255
256         buildTitleDetailLink(record, title_link);
257         buildSearchLink(STYPE_AUTHOR, record.author(), author_link);
258
259         var form = $("myopac_holds_form_" + r.hold + '_' + record.doc_id());
260
261         if(form) {
262                 var img = elem("img");
263                 img.setAttribute("src", 
264                         buildImageLink('tor/' + record.types_of_resource()[0] + ".jpg"));
265                 addCSSClass(img, "myopac_form_pic");
266                 form.appendChild(img);
267         }
268 }
269
270 var finesShown = false;
271 function myOPACShowFines() {
272         if(finesShown) return; finesShown = true;
273         var req = new Request(FETCH_FINES_SUMMARY, G.user.session, G.user.id() );
274         req.callback(_myOPACShowFines);
275         req.send();
276 }
277
278 function _myOPACShowFines(r) {
279         hideMe($('myopac_fines_summary_loading'));
280         unHideMe($('myopac_fines_summary_row'));
281
282         var summary = r.getResultObject();
283         var total       = "0.00"; /* localization? */
284         var paid                = "0.00";
285         var balance = "0.00";
286         if( instanceOf(summary,mous) ) {
287
288                 total           = _finesFormatNumber(summary.total_owed());
289                 paid            = _finesFormatNumber(summary.total_paid());
290                 balance = _finesFormatNumber(summary.balance_owed());
291
292                 var req = new Request(FETCH_TRANSACTIONS, G.user.session, G.user.id() );
293                 req.callback(myOPACShowTransactions);
294                 req.send();
295         }
296
297         $('myopac_fines_summary_total').appendChild(text(total));
298         $('myopac_fines_summary_paid').appendChild(text(paid));
299         $('myopac_fines_summary_balance').appendChild(text(balance));
300 }
301
302 function _finesFormatNumber(num) {
303         if(isNull(num)) num = 0;
304         num = num + "";
305         if(num.length < 2 || !num.match(/\./)) num += ".00";
306         if(num.match(/\./) && num.charAt(num.length-2) == '.') num += "0";
307         return num;
308 }          
309
310 function _trimSeconds(time) { if(!time) return ""; return time.replace(/\..*/,""); }
311
312
313 var transTemplate;
314 function myOPACShowTransactions(r) {
315
316         if(transTemplate) return;
317         var tbody = $('myopac_fines_tbody');
318         unHideMe($('myopac_trans_table'));
319         transTemplate = tbody.removeChild($('myopac_trans_row'));
320
321         var transactions = r.getResultObject();
322
323         //alert(js2JSON(transactions));
324
325         for( var idx in transactions ) {
326
327                 var trans = transactions[idx].transaction;
328                 var record = transactions[idx].record;
329                 var row = transTemplate.cloneNode(transTemplate);
330
331                 $n(row,'myopac_trans_start').
332                         appendChild(text(_trimSeconds(trans.xact_start())));
333                 $n(row,'myopac_trans_last_bill').
334                         appendChild(text(_trimSeconds(trans.last_billing_ts())));
335                 $n(row,'myopac_trans_last_payment').
336                         appendChild(text(_trimSeconds(trans.last_payment_ts())));
337                 $n(row,'myopac_trans_init_amount').
338                         appendChild(text(_finesFormatNumber(trans.total_owed())));
339                 $n(row,'myopac_trans_total_paid').
340                         appendChild(text(_finesFormatNumber(trans.total_paid())));
341                 $n(row,'myopac_trans_balance').
342                         appendChild(text(_finesFormatNumber(trans.balance_owed())));
343
344                 var extra = "";
345                 var type = trans.xact_type();
346                 $n(row,'myopac_trans_type').appendChild(text(type));
347                 if( type == "circulation" ) extra = record.title();
348                 $n(row, 'myopac_trans_extra').appendChild(text(extra));
349
350                 tbody.appendChild(row);
351         }
352 }
353
354 function myOPACSavePrefs() {
355         G.user.prefs['opac.hits_per_page'] = getSelectorVal($('prefs_hits_per'));
356         if(commitUserPrefs())
357                 alert($('prefs_update_success').innerHTML);
358         else alert($('prefs_update_failure').innerHTML);
359 }
360
361
362 function myOPACShowPrefs() {
363         grabUserPrefs();
364         myOPACShowHitsPer();
365         hideMe($('myopac_prefs_loading'));
366         unHideMe($('myopac_prefs_row'));
367 }
368
369 function myOPACShowHitsPer() {
370         var hits = 10;
371         if(G.user.prefs['opac.hits_per_page'])
372                 hits = G.user.prefs['opac.hits_per_page'];
373         var hitsSel = $('prefs_hits_per');
374         setSelector(hitsSel, hits);
375 }
376
377 var userShown = false;
378 function myOPACShowSummary() {
379         if(userShown) return; userShown = true;
380         var req = new Request(FETCH_FLESHED_USER,G.user.session, G.user.id());
381         req.callback(_myOPACSummaryShowUer);
382         req.send();
383 }
384
385 var addrRowTemplate;
386 function _myOPACSummaryShowUer(r) {
387
388         var user = r.getResultObject();
389         fleshedUser = user;
390
391         appendClear($('myopac_summary_first'),text(user.first_given_name()));
392         appendClear($('myopac_summary_middle'),text(user.second_given_name()));
393         appendClear($('myopac_summary_dayphone'),text(user.day_phone()));
394         appendClear($('myopac_summary_eveningphone'),text(user.evening_phone()));
395         appendClear($('myopac_summary_otherphone'),text(user.other_phone()));
396         appendClear($('myopac_summary_last'),text(user.family_name()));
397         appendClear($('myopac_summary_username'),text(user.usrname()));
398         appendClear($('myopac_summary_email'),text(user.email()));
399         appendClear($('myopac_summary_barcode'),text(user.card().barcode()));
400         appendClear($('myopac_summary_ident1'),text(user.ident_value()));
401         appendClear($('myopac_summary_ident2'),text(user.ident_value2()));
402         appendClear($('myopac_summary_homelib'),text(findOrgUnit(user.home_ou()).name()));
403         appendClear($('myopac_summary_create_date'),text(user.create_date()));
404
405         var tbody = $('myopac_addr_tbody');
406         var template;
407
408         if(addrRowTemplate) { 
409                 template = addrRowTemplate;
410         } else {
411                 template = tbody.removeChild($('myopac_addr_row'));
412                 addrRowTemplate = template;
413         }
414         removeChildren(tbody);
415
416         for( var a in user.addresses() ) {
417                 var row = template.cloneNode(true);
418                 myOPACDrawAddr(row, user.addresses()[a]);
419                 tbody.appendChild(row);
420         }
421 }
422
423 function myOPACDrawAddr(row, addr) {
424
425         appendClear($n(row, 'myopac_addr_type'),text(addr.address_type()));
426         var street = (addr.street2()) ? addr.street1() + ", " + addr.street2() : addr.street1();
427         appendClear($n(row, 'myopac_addr_street'),text(street));
428         appendClear($n(row, 'myopac_addr_city'),text(addr.city()));
429         appendClear($n(row, 'myopac_addr_county'),text(addr.county()));
430         appendClear($n(row, 'myopac_addr_state'),text(addr.state()));
431         appendClear($n(row, 'myopac_addr_zip'),text(addr.post_code()));
432 }
433
434
435 function myOPACUpdateUsername() {
436         var username = $('myopac_new_username').value;
437         if(username == null || username == "") {
438                 alert($('myopac_username_error').innerHTML);
439                 return;
440         }
441         var req = new Request(UPDATE_USERNAME, G.user.session, username );
442         req.send(true);
443         if(req.result()) {
444
445                 var evt;
446                 if(evt = checkILSEvent(req.result())) {
447                         alertILSEvent(evt);
448                         return;
449                 }
450
451                 G.user.usrname(username);
452                 hideMe($('myopac_update_username_row'));
453                 userShown = false;
454                 myOPACShowSummary();
455                 return;
456         }
457
458         alert($('myopac_username_failure').innerHTML);
459 }
460
461 function myOPACUpdateEmail() {
462         var email = $('myopac_new_email').value;
463         if(email == null || email == "") {
464                 alert($('myopac_email_error').innerHTML);
465                 return;
466         }
467
468         var req = new Request(UPDATE_EMAIL, G.user.session, email );
469         req.send(true);
470         if(req.result()) {
471                 G.user.usrname(email);
472                 hideMe($('myopac_update_email_row'));
473                 userShown = false;
474                 myOPACShowSummary();
475                 return;
476         }
477
478         alert($('myopac_email_failure').innerHTML);
479 }
480
481
482 function myOPACUpdatePassword() {
483         var curpassword = $('myopac_current_password').value;
484         var password = $('myopac_new_password').value;
485         var password2 = $('myopac_new_password2').value;
486
487         if(     curpassword == null || curpassword == "" || 
488                         password == null || password == "" || 
489                         password2 == null || password2 == "" || password != password2 ) {
490                 alert($('myopac_password_error').innerHTML);
491                 return;
492         }
493
494         var req = new Request(UPDATE_PASSWORD, G.user.session, password, curpassword );
495         req.send(true);
496         if(req.result()) {
497                 G.user.usrname(password);
498                 hideMe($('myopac_update_password_row'));
499                 userShown = false;
500                 myOPACShowSummary();
501                 return;
502         }
503
504         alert($('myopac_password_failure').innerHTML);
505 }
506
507
508
509