added renewal code to myopac for circulations... needs debugging, permissions fixing...
[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, G.user.session);       
89         req.callback(myOPACDrawCheckedOut);
90         req.send();
91 }
92
93
94 var checkedRowTemplate;
95 var circsCache = new Array();
96 var checkedDrawn = false;
97 function myOPACDrawCheckedOut(r) {
98
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         if(!checkedRowTemplate) 
107                 checkedRowTemplate = tbody.removeChild($("myopac_checked_row"));
108
109         clearNodes( tbody, [ loading, none ] );
110
111         hideMe(loading); /* remove all children and start over */
112         if(!(checked && checked[0])) unHideMe(none);
113
114         for( var idx in checked ) {
115
116                 var row = checkedRowTemplate.cloneNode(true);
117
118                 var circ    = checked[idx].circ;
119       var record  = checked[idx].record;
120       var copy    = checked[idx].copy;
121                 var due = circ.due_date();
122       due = due.replace(/[0-9][0-9]:.*$/,"");
123
124                 var tlink = $n( row, "myopac_checked_title_link" );
125                 var alink = $n( row, "myopac_checked_author_link" );
126                 var dlink = $n( row, "myopac_checked_due" );
127                 var rlink = $n( row, "myopac_checked_renewals" );
128                 var rnlink = $n( row, "myopac_checked_renew_link" );
129
130                 buildTitleDetailLink(record, tlink);
131                 buildSearchLink(STYPE_AUTHOR, record.author(), alink);
132                 dlink.appendChild(text(due));
133                 rlink.appendChild(text(circ.renewal_remaining()));
134                 unHideMe(row);
135                 rnlink.setAttribute('href', 'javascript:myOPACRenewCirc("'+circ.id()+'");');
136                 circsCache.push(circ);
137
138                 tbody.appendChild(row);
139         }
140 }
141
142 function myOPACRenewCirc(circid) {
143         var circ;
144         for( var i = 0; i != circsCache.length; i++ ) 
145                 if(circsCache[i].id() == circid)
146                         circ = circsCache[i];
147
148         alert('renewing ' + circ.id());
149         var req = new Request(RENEW_CIRC, G.user.session, circ );
150         req.send(true);
151         var res = req.result();
152         alert('renew result: ' + js2JSON(res));
153         alert($('myopac_renew_success').innerHTML);     
154         checkedDrawn = false;
155         myOPACShowChecked();
156 }
157
158
159
160 function myOPACShowHolds() {
161         var req = new Request(FETCH_HOLDS, G.user.session, G.user.id());        
162         req.callback(myOPACDrawHolds);
163         req.send();
164 }
165
166 var holdsTemplateRowOrig;
167 var holdsTemplateRow;
168 function myOPACDrawHolds(r) {
169
170         var tbody = $("myopac_holds_tbody");
171         if(holdsTemplateRow) return;
172         if(holdsTemplateRowOrig) {
173                 holdsTemplateRow = holdsTemplateRowOrig;
174                 removeChildren(tbody);
175         } else {
176                 holdsTemplateRow = tbody.removeChild($("myopac_holds_row"));
177                 holdsTemplateRowOrig = holdsTemplateRow;
178         }
179
180         hideMe($('myopac_holds_loading'));
181
182         var holds = r.getResultObject();
183
184         if(!holds || holds.length < 1) unHideMe($('myopac_holds_none'));
185         for( var i = 0; i != holds.length; i++ ) {
186
187                 var h = holds[i];
188                 var row = holdsTemplateRow.cloneNode(true);
189                 row.id = "myopac_holds_row_" + h.target();
190
191                 var formats = (h.holdable_formats()) ? h.holdable_formats() : null;
192                 var form = $n(row, "myopac_holds_formats");
193                 form.id = "myopac_holds_form_" + h.target();
194                 if(formats) form.appendChild(text(formats));
195
196                 $n(row, "myopac_holds_location").
197                         appendChild(text(findOrgUnit(h.pickup_lib()).name()));
198                 $n(row, "myopac_holds_email_link").
199                         appendChild(text(h.email_notify()));
200                 $n(row, "myopac_holds_phone_link").
201                         appendChild(text(h.phone_notify()));
202                 tbody.appendChild(row);
203
204                 $n(row,'myopac_holds_cancel_link').setAttribute(
205                         'href','javascript:myOPACCancelHold("'+ h.id()+'");'); 
206                 unHideMe(row);
207
208                 myOPACDrawHoldTitle(h);
209         }
210 }
211
212 function myOPACCancelHold(holdid) {
213         if( confirm($('myopac_holds_cancel_verify').innerHTML) ) {
214                 holdsCancel(holdid);
215                 holdsTemplateRow = null
216                 myOPACShowHolds();
217         }
218 }
219
220
221
222 function myOPACDrawHoldTitle(hold) {
223         var method;
224         if(hold.hold_type() == "M") method = FETCH_MRMODS;
225         if(hold.hold_type() == "T") method = FETCH_RMODS;
226         var req = new Request(method, hold.target());
227         req.callback(myOPACFleshHoldTitle);
228         req.send();
229 }
230
231 function myOPACFleshHoldTitle(r) {
232
233         var record = r.getResultObject();
234         var row = $("myopac_holds_row_" + record.doc_id());
235         var title_link = $n(row, "myopac_holds_title_link");
236         var author_link = $n(row, "myopac_holds_author_link");
237
238         buildTitleDetailLink(record, title_link);
239         buildSearchLink(STYPE_AUTHOR, record.author(), author_link);
240
241         var form = $("myopac_holds_form_" + record.doc_id());
242
243         if(form) {
244                 var img = elem("img");
245                 img.setAttribute("src", buildImageLink('tor/' + record.types_of_resource()[0] + ".jpg"));
246                 addCSSClass(img, "myopac_form_pic");
247                 form.appendChild(img);
248         }
249 }
250
251 var finesShown = false;
252 function myOPACShowFines() {
253         if(finesShown) return; finesShown = true;
254         var req = new Request(FETCH_FINES_SUMMARY, G.user.session, G.user.id() );
255         req.callback(_myOPACShowFines);
256         req.send();
257 }
258
259 function _myOPACShowFines(r) {
260         hideMe($('myopac_fines_summary_loading'));
261         unHideMe($('myopac_fines_summary_row'));
262
263         var summary = r.getResultObject();
264         var total       = "0.00"; /* localization? */
265         var paid                = "0.00";
266         var balance = "0.00";
267         if( instanceOf(summary,mus) ) {
268
269                 total           = _finesFormatNumber(summary.total_owed());
270                 paid            = _finesFormatNumber(summary.total_paid());
271                 balance = _finesFormatNumber(summary.balance_owed());
272
273                 var req = new Request(FETCH_TRANSACTIONS, G.user.session, G.user.id() );
274                 req.callback(myOPACShowTransactions);
275                 req.send();
276         }
277
278         $('myopac_fines_summary_total').appendChild(text(total));
279         $('myopac_fines_summary_paid').appendChild(text(paid));
280         $('myopac_fines_summary_balance').appendChild(text(balance));
281 }
282
283 function _finesFormatNumber(num) {
284         if(isNull(num)) num = 0;
285         num = num + "";
286         if(num.length < 2 || !num.match(/\./)) num += ".00";
287         if(num.match(/\./) && num.charAt(num.length-2) == '.') num += "0";
288         return num;
289 }          
290
291 function _trimSeconds(time) { if(!time) return ""; return time.replace(/\..*/,""); }
292
293
294 var transTemplate;
295 function myOPACShowTransactions(r) {
296
297         if(transTemplate) return;
298         var tbody = $('myopac_fines_tbody');
299         unHideMe($('myopac_trans_table'));
300         transTemplate = tbody.removeChild($('myopac_trans_row'));
301
302         var transactions = r.getResultObject();
303
304         //alert(js2JSON(transactions));
305
306         for( var idx in transactions ) {
307
308                 var trans = transactions[idx].transaction;
309                 var record = transactions[idx].record;
310                 var row = transTemplate.cloneNode(transTemplate);
311
312                 $n(row,'myopac_trans_start').
313                         appendChild(text(_trimSeconds(trans.xact_start())));
314                 $n(row,'myopac_trans_last_bill').
315                         appendChild(text(_trimSeconds(trans.last_billing_ts())));
316                 $n(row,'myopac_trans_last_payment').
317                         appendChild(text(_trimSeconds(trans.last_payment_ts())));
318                 $n(row,'myopac_trans_init_amount').
319                         appendChild(text(_finesFormatNumber(trans.total_owed())));
320                 $n(row,'myopac_trans_total_paid').
321                         appendChild(text(_finesFormatNumber(trans.total_paid())));
322                 $n(row,'myopac_trans_balance').
323                         appendChild(text(_finesFormatNumber(trans.balance_owed())));
324
325                 var extra = "";
326                 var type = trans.xact_type();
327                 $n(row,'myopac_trans_type').appendChild(text(type));
328                 if( type == "circulation" ) extra = record.title();
329                 $n(row, 'myopac_trans_extra').appendChild(text(extra));
330
331                 tbody.appendChild(row);
332         }
333 }
334
335
336 function myOPACShowPrefs() {
337 }
338
339 var userShown = false;
340 function myOPACShowSummary() {
341         if(userShown) return; userShown = true;
342         var req = new Request(FETCH_FLESHED_USER,G.user.session, G.user.id());
343         req.callback(_myOPACSummaryShowUer);
344         req.send();
345 }
346
347 var addrRowTemplate;
348 function _myOPACSummaryShowUer(r) {
349
350         var user = r.getResultObject();
351         fleshedUser = user;
352
353         appendClear($('myopac_summary_first'),text(user.first_given_name()));
354         appendClear($('myopac_summary_middle'),text(user.second_given_name()));
355         appendClear($('myopac_summary_dayphone'),text(user.day_phone()));
356         appendClear($('myopac_summary_eveningphone'),text(user.evening_phone()));
357         appendClear($('myopac_summary_otherphone'),text(user.other_phone()));
358         appendClear($('myopac_summary_last'),text(user.family_name()));
359         appendClear($('myopac_summary_username'),text(user.usrname()));
360         appendClear($('myopac_summary_email'),text(user.email()));
361         appendClear($('myopac_summary_barcode'),text(user.card().barcode()));
362         appendClear($('myopac_summary_ident1'),text(user.ident_value()));
363         appendClear($('myopac_summary_ident2'),text(user.ident_value2()));
364         appendClear($('myopac_summary_homelib'),text(findOrgUnit(user.home_ou()).name()));
365         appendClear($('myopac_summary_create_date'),text(user.create_date()));
366
367         var tbody = $('myopac_addr_tbody');
368         var template;
369
370         if(addrRowTemplate) { 
371                 template = addrRowTemplate;
372         } else {
373                 template = tbody.removeChild($('myopac_addr_row'));
374                 addrRowTemplate = template;
375         }
376
377         for( var a in user.addresses() ) {
378                 var row = template.cloneNode(true);
379                 myOPACDrawAddr(row, user.addresses()[a]);
380                 tbody.appendChild(row);
381         }
382 }
383
384 function myOPACDrawAddr(row, addr) {
385
386         appendClear($n(row, 'myopac_addr_type'),text(addr.address_type()));
387         var street = (addr.street2()) ? addr.street1() + ", " + addr.street2() : addr.street1();
388         appendClear($n(row, 'myopac_addr_street'),text(street));
389         appendClear($n(row, 'myopac_addr_city'),text(addr.city()));
390         appendClear($n(row, 'myopac_addr_county'),text(addr.county()));
391         appendClear($n(row, 'myopac_addr_state'),text(addr.state()));
392         appendClear($n(row, 'myopac_addr_zip'),text(addr.post_code()));
393 }
394
395
396 function myOPACUpdateUsername() {
397         var username = $('myopac_new_username').value;
398         if(username == null || username == "") {
399                 alert($('myopac_username_error').innerHTML);
400                 return;
401         }
402         var req = new Request(UPDATE_USERNAME, G.user.session, username );
403         req.send(true);
404         if(req.result()) {
405                 G.user.usrname(username);
406                 hideMe($('myopac_update_username_row'));
407                 userShown = false;
408                 myOPACShowSummary();
409                 return;
410         }
411
412         alert($('myopac_username_failure').innerHTML);
413 }
414
415 function myOPACUpdateEmail() {
416         var email = $('myopac_new_email').value;
417         if(email == null || email == "") {
418                 alert($('myopac_email_error').innerHTML);
419                 return;
420         }
421
422         var req = new Request(UPDATE_EMAIL, G.user.session, email );
423         req.send(true);
424         if(req.result()) {
425                 G.user.usrname(email);
426                 hideMe($('myopac_update_email_row'));
427                 userShown = false;
428                 myOPACShowSummary();
429                 return;
430         }
431
432         alert($('myopac_email_failure').innerHTML);
433 }
434
435
436 function myOPACUpdatePassword() {
437         var curpassword = $('myopac_current_password').value;
438         var password = $('myopac_new_password').value;
439         var password2 = $('myopac_new_password2').value;
440
441         if(     curpassword == null || curpassword == "" || 
442                         password == null || password == "" || 
443                         password2 == null || password2 == "" || password != password2 ) {
444                 alert($('myopac_password_error').innerHTML);
445                 return;
446         }
447
448         var req = new Request(UPDATE_PASSWORD, G.user.session, password, curpassword );
449         req.send(true);
450         if(req.result()) {
451                 G.user.usrname(password);
452                 hideMe($('myopac_update_password_row'));
453                 userShown = false;
454                 myOPACShowSummary();
455                 return;
456         }
457
458         alert($('myopac_password_failure').innerHTML);
459 }
460
461
462
463