style changes
[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         var req = new Request(RENEW_CIRC, G.user.session, circ );
149         req.send(true);
150         var res = req.result();
151         if(res.status) {
152                 alert(res.text);
153                 return;
154         }
155         alert($('myopac_renew_success').innerHTML);     
156         checkedDrawn = false;
157         myOPACShowChecked();
158 }
159
160
161
162 function myOPACShowHolds() {
163         var req = new Request(FETCH_HOLDS, G.user.session, G.user.id());        
164         req.callback(myOPACDrawHolds);
165         req.send();
166 }
167
168 var holdsTemplateRowOrig;
169 var holdsTemplateRow;
170 function myOPACDrawHolds(r) {
171
172         var tbody = $("myopac_holds_tbody");
173         if(holdsTemplateRow) return;
174         if(holdsTemplateRowOrig) {
175                 holdsTemplateRow = holdsTemplateRowOrig;
176                 removeChildren(tbody);
177         } else {
178                 holdsTemplateRow = tbody.removeChild($("myopac_holds_row"));
179                 holdsTemplateRowOrig = holdsTemplateRow;
180         }
181
182         hideMe($('myopac_holds_loading'));
183
184         var holds = r.getResultObject();
185
186         if(!holds || holds.length < 1) unHideMe($('myopac_holds_none'));
187         for( var i = 0; i != holds.length; i++ ) {
188
189                 var h = holds[i];
190                 var row = holdsTemplateRow.cloneNode(true);
191                 row.id = "myopac_holds_row_" + h.target();
192
193                 var formats = (h.holdable_formats()) ? h.holdable_formats() : null;
194                 var form = $n(row, "myopac_holds_formats");
195                 form.id = "myopac_holds_form_" + h.target();
196                 if(formats) form.appendChild(text(formats));
197
198                 $n(row, "myopac_holds_location").
199                         appendChild(text(findOrgUnit(h.pickup_lib()).name()));
200                 $n(row, "myopac_holds_email_link").
201                         appendChild(text(h.email_notify()));
202                 $n(row, "myopac_holds_phone_link").
203                         appendChild(text(h.phone_notify()));
204                 tbody.appendChild(row);
205
206                 $n(row,'myopac_holds_cancel_link').setAttribute(
207                         'href','javascript:myOPACCancelHold("'+ h.id()+'");'); 
208                 unHideMe(row);
209
210                 myOPACDrawHoldTitle(h);
211         }
212 }
213
214 function myOPACCancelHold(holdid) {
215         if( confirm($('myopac_holds_cancel_verify').innerHTML) ) {
216                 holdsCancel(holdid);
217                 holdsTemplateRow = null
218                 myOPACShowHolds();
219         }
220 }
221
222
223
224 function myOPACDrawHoldTitle(hold) {
225         var method;
226         if(hold.hold_type() == "M") method = FETCH_MRMODS;
227         if(hold.hold_type() == "T") method = FETCH_RMODS;
228         var req = new Request(method, hold.target());
229         req.callback(myOPACFleshHoldTitle);
230         req.send();
231 }
232
233 function myOPACFleshHoldTitle(r) {
234
235         var record = r.getResultObject();
236         var row = $("myopac_holds_row_" + record.doc_id());
237         var title_link = $n(row, "myopac_holds_title_link");
238         var author_link = $n(row, "myopac_holds_author_link");
239
240         buildTitleDetailLink(record, title_link);
241         buildSearchLink(STYPE_AUTHOR, record.author(), author_link);
242
243         var form = $("myopac_holds_form_" + record.doc_id());
244
245         if(form) {
246                 var img = elem("img");
247                 img.setAttribute("src", buildImageLink('tor/' + record.types_of_resource()[0] + ".jpg"));
248                 addCSSClass(img, "myopac_form_pic");
249                 form.appendChild(img);
250         }
251 }
252
253 var finesShown = false;
254 function myOPACShowFines() {
255         if(finesShown) return; finesShown = true;
256         var req = new Request(FETCH_FINES_SUMMARY, G.user.session, G.user.id() );
257         req.callback(_myOPACShowFines);
258         req.send();
259 }
260
261 function _myOPACShowFines(r) {
262         hideMe($('myopac_fines_summary_loading'));
263         unHideMe($('myopac_fines_summary_row'));
264
265         var summary = r.getResultObject();
266         var total       = "0.00"; /* localization? */
267         var paid                = "0.00";
268         var balance = "0.00";
269         if( instanceOf(summary,mous) ) {
270
271                 total           = _finesFormatNumber(summary.total_owed());
272                 paid            = _finesFormatNumber(summary.total_paid());
273                 balance = _finesFormatNumber(summary.balance_owed());
274
275                 var req = new Request(FETCH_TRANSACTIONS, G.user.session, G.user.id() );
276                 req.callback(myOPACShowTransactions);
277                 req.send();
278         }
279
280         $('myopac_fines_summary_total').appendChild(text(total));
281         $('myopac_fines_summary_paid').appendChild(text(paid));
282         $('myopac_fines_summary_balance').appendChild(text(balance));
283 }
284
285 function _finesFormatNumber(num) {
286         if(isNull(num)) num = 0;
287         num = num + "";
288         if(num.length < 2 || !num.match(/\./)) num += ".00";
289         if(num.match(/\./) && num.charAt(num.length-2) == '.') num += "0";
290         return num;
291 }          
292
293 function _trimSeconds(time) { if(!time) return ""; return time.replace(/\..*/,""); }
294
295
296 var transTemplate;
297 function myOPACShowTransactions(r) {
298
299         if(transTemplate) return;
300         var tbody = $('myopac_fines_tbody');
301         unHideMe($('myopac_trans_table'));
302         transTemplate = tbody.removeChild($('myopac_trans_row'));
303
304         var transactions = r.getResultObject();
305
306         //alert(js2JSON(transactions));
307
308         for( var idx in transactions ) {
309
310                 var trans = transactions[idx].transaction;
311                 var record = transactions[idx].record;
312                 var row = transTemplate.cloneNode(transTemplate);
313
314                 $n(row,'myopac_trans_start').
315                         appendChild(text(_trimSeconds(trans.xact_start())));
316                 $n(row,'myopac_trans_last_bill').
317                         appendChild(text(_trimSeconds(trans.last_billing_ts())));
318                 $n(row,'myopac_trans_last_payment').
319                         appendChild(text(_trimSeconds(trans.last_payment_ts())));
320                 $n(row,'myopac_trans_init_amount').
321                         appendChild(text(_finesFormatNumber(trans.total_owed())));
322                 $n(row,'myopac_trans_total_paid').
323                         appendChild(text(_finesFormatNumber(trans.total_paid())));
324                 $n(row,'myopac_trans_balance').
325                         appendChild(text(_finesFormatNumber(trans.balance_owed())));
326
327                 var extra = "";
328                 var type = trans.xact_type();
329                 $n(row,'myopac_trans_type').appendChild(text(type));
330                 if( type == "circulation" ) extra = record.title();
331                 $n(row, 'myopac_trans_extra').appendChild(text(extra));
332
333                 tbody.appendChild(row);
334         }
335 }
336
337 function myOPACSavePrefs() {
338         G.user.prefs['opac.hits_per_page'] = 
339                 $('prefs_hits_per').options[$('prefs_hits_per').selectedIndex].value;
340         if(commitUserPrefs())
341                 alert($('prefs_update_success').innerHTML);
342         else alert($('prefs_update_failure').innerHTML);
343 }
344
345
346 function myOPACShowPrefs() {
347         grabUserPrefs();
348         myOPACShowHitsPer();
349         hideMe($('myopac_prefs_loading'));
350         unHideMe($('myopac_prefs_row'));
351 }
352
353 function myOPACShowHitsPer() {
354         var hits = 10;
355         if(G.user.prefs['opac.hits_per_page'])
356                 hits = G.user.prefs['opac.hits_per_page'];
357
358         var hitsSel = $('prefs_hits_per');
359
360         for( var o in hitsSel.options ) {
361                 var opt = hitsSel.options[o];
362                 if( hits == opt.value ) {
363                         opt.selected = true;
364                         hitsSel.selectedIndex = o;
365                 }
366         }
367 }
368
369 var userShown = false;
370 function myOPACShowSummary() {
371         if(userShown) return; userShown = true;
372         var req = new Request(FETCH_FLESHED_USER,G.user.session, G.user.id());
373         req.callback(_myOPACSummaryShowUer);
374         req.send();
375 }
376
377 var addrRowTemplate;
378 function _myOPACSummaryShowUer(r) {
379
380         var user = r.getResultObject();
381         fleshedUser = user;
382
383         appendClear($('myopac_summary_first'),text(user.first_given_name()));
384         appendClear($('myopac_summary_middle'),text(user.second_given_name()));
385         appendClear($('myopac_summary_dayphone'),text(user.day_phone()));
386         appendClear($('myopac_summary_eveningphone'),text(user.evening_phone()));
387         appendClear($('myopac_summary_otherphone'),text(user.other_phone()));
388         appendClear($('myopac_summary_last'),text(user.family_name()));
389         appendClear($('myopac_summary_username'),text(user.usrname()));
390         appendClear($('myopac_summary_email'),text(user.email()));
391         appendClear($('myopac_summary_barcode'),text(user.card().barcode()));
392         appendClear($('myopac_summary_ident1'),text(user.ident_value()));
393         appendClear($('myopac_summary_ident2'),text(user.ident_value2()));
394         appendClear($('myopac_summary_homelib'),text(findOrgUnit(user.home_ou()).name()));
395         appendClear($('myopac_summary_create_date'),text(user.create_date()));
396
397         var tbody = $('myopac_addr_tbody');
398         var template;
399
400         if(addrRowTemplate) { 
401                 template = addrRowTemplate;
402         } else {
403                 template = tbody.removeChild($('myopac_addr_row'));
404                 addrRowTemplate = template;
405         }
406         removeChildren(tbody);
407
408         for( var a in user.addresses() ) {
409                 var row = template.cloneNode(true);
410                 myOPACDrawAddr(row, user.addresses()[a]);
411                 tbody.appendChild(row);
412         }
413 }
414
415 function myOPACDrawAddr(row, addr) {
416
417         appendClear($n(row, 'myopac_addr_type'),text(addr.address_type()));
418         var street = (addr.street2()) ? addr.street1() + ", " + addr.street2() : addr.street1();
419         appendClear($n(row, 'myopac_addr_street'),text(street));
420         appendClear($n(row, 'myopac_addr_city'),text(addr.city()));
421         appendClear($n(row, 'myopac_addr_county'),text(addr.county()));
422         appendClear($n(row, 'myopac_addr_state'),text(addr.state()));
423         appendClear($n(row, 'myopac_addr_zip'),text(addr.post_code()));
424 }
425
426
427 function myOPACUpdateUsername() {
428         var username = $('myopac_new_username').value;
429         if(username == null || username == "") {
430                 alert($('myopac_username_error').innerHTML);
431                 return;
432         }
433         var req = new Request(UPDATE_USERNAME, G.user.session, username );
434         req.send(true);
435         if(req.result()) {
436                 G.user.usrname(username);
437                 hideMe($('myopac_update_username_row'));
438                 userShown = false;
439                 myOPACShowSummary();
440                 return;
441         }
442
443         alert($('myopac_username_failure').innerHTML);
444 }
445
446 function myOPACUpdateEmail() {
447         var email = $('myopac_new_email').value;
448         if(email == null || email == "") {
449                 alert($('myopac_email_error').innerHTML);
450                 return;
451         }
452
453         var req = new Request(UPDATE_EMAIL, G.user.session, email );
454         req.send(true);
455         if(req.result()) {
456                 G.user.usrname(email);
457                 hideMe($('myopac_update_email_row'));
458                 userShown = false;
459                 myOPACShowSummary();
460                 return;
461         }
462
463         alert($('myopac_email_failure').innerHTML);
464 }
465
466
467 function myOPACUpdatePassword() {
468         var curpassword = $('myopac_current_password').value;
469         var password = $('myopac_new_password').value;
470         var password2 = $('myopac_new_password2').value;
471
472         if(     curpassword == null || curpassword == "" || 
473                         password == null || password == "" || 
474                         password2 == null || password2 == "" || password != password2 ) {
475                 alert($('myopac_password_error').innerHTML);
476                 return;
477         }
478
479         var req = new Request(UPDATE_PASSWORD, G.user.session, password, curpassword );
480         req.send(true);
481         if(req.result()) {
482                 G.user.usrname(password);
483                 hideMe($('myopac_update_password_row'));
484                 userShown = false;
485                 myOPACShowSummary();
486                 return;
487         }
488
489         alert($('myopac_password_failure').innerHTML);
490 }
491
492
493
494