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