Updates to go with upgrade to YUI 2.5.1
[koha.git] / koha-tmpl / opac-tmpl / prog / en / js / basket.js
1 //////////////////////////////////////////////////////////////////////////////
2 // BASIC FUNCTIONS FOR COOKIE MANGEMENT //
3 //////////////////////////////////////////////////////////////////////////////
4
5 var CGIBIN = "/cgi-bin/koha/";
6
7
8 var nameCookie = "bib_list";
9 var valCookie = readCookie(nameCookie);
10
11 if(valCookie){
12     var arrayRecords = valCookie.split("/");
13     if(arrayRecords.length > 0){
14         var basketcount = arrayRecords.length-1;
15     } else {
16         var basketcount = "";
17     }
18 } else {
19         var basketcount = "";
20 }
21
22 function writeCookie(name, val, wd) {
23     if (wd) {
24         parent.opener.document.cookie = name + "=" + val;
25     }
26     else {
27         parent.document.cookie = name + "=" + val;
28     }
29 }
30
31 function readCookieValue (str, val_beg) {
32     var val_end = str.indexOf(";", val_end);
33     if (val_end == -1)
34         val_end = str.length;
35     return str.substring(val_beg, val_end);
36 }
37
38 function readCookie(name, wd) {
39     var str_name = name + "=";
40     var str_len = str_name.length;
41     var str_cookie = "";
42     if (wd) {
43         str_cookie = parent.opener.document.cookie;
44     }
45     else {
46         str_cookie = parent.document.cookie;
47     }
48         // fixed - getting the part of the basket that is bib_list
49         var cookie_parts = str_cookie.split(";");
50             for(var i=0;i < cookie_parts.length;i++) {
51                     var c = cookie_parts[i];
52                     while (c.charAt(0)==' ') c = c.substring(1,c.length);
53                     if(c.indexOf(str_name) == 0) return c.substring(str_name.length,c.length);
54             }
55     return null;
56 }
57
58 function delCookie(name) {
59     var exp = new Date();
60     exp.setTime(exp.getTime()-1);
61         if(parent.opener){
62     parent.opener.document.cookie = name + "=null; expires=" + exp.toGMTString();
63         } else {
64         document.cookie = name + "=null; expires=" + exp.toGMTString();
65         }
66 }
67
68 ///////////////////////////////////////////////////////////////////
69 // SPECIFIC FUNCTIONS USING COOKIES //
70 ///////////////////////////////////////////////////////////////////
71
72 function openBasket() {
73     var strCookie = "";
74     var nameCookie = "bib_list";
75     var valCookie = readCookie(nameCookie);
76     if ( valCookie ) {
77         strCookie = nameCookie + "=" + valCookie;
78     }
79
80     if ( strCookie ) {
81         var iW = 820;
82         var iH = 450;
83         var optWin = "dependant=yes,status=yes,scrollbars=yes,resizable=yes,toolbar=no,location=yes,height="+iH+",width="+iW;
84         var loc = CGIBIN + "opac-basket.pl?" + strCookie;
85         var basket = open(loc, "basket", optWin);
86         if (window.focus) {basket.focus()}
87     }
88     else {
89         showCartUpdate(MSG_BASKET_EMPTY);
90     }
91 }
92
93 function addRecord(val, selection,NoMsgAlert) {
94     var nameCookie = "bib_list";
95     var valCookie = readCookie(nameCookie);
96     var write = 0;
97
98     if ( ! valCookie ) { // empty basket
99         valCookie = val + '/';
100         write = 1;
101         updateBasket(1);
102     }
103     else {
104         // is this record already in the basket ?
105         var found = false;
106         var arrayRecords = valCookie.split("/");
107         for (var i = 0; i < valCookie.length - 1; i++) {
108             if (val == arrayRecords[i]) {
109                 found = true;
110                 break;
111             }
112         }
113         if ( found ) {
114             if (selection) {
115                 return 0;
116             }
117             if (! NoMsgAlert ) {
118                 showCartUpdate(MSG_RECORD_IN_BASKET);
119             }
120         }
121         else {
122             valCookie += val + '/';
123             write = 1;
124             updateBasket(arrayRecords.length);
125         }
126     }
127
128     if (write) {
129         writeCookie(nameCookie, valCookie);
130         if (selection) { // when adding a selection of records
131             return 1;
132         }
133         if (! NoMsgAlert ) {
134             showCartUpdate(MSG_RECORD_ADDED);
135         }
136     }
137 }
138
139 function AllAreChecked(s){
140         if (! s.length) { return false;}
141         var l = s.length;
142         for (var i=0; i < l; i++) {
143                 if(! s[i].checked) { return false; }
144         }
145         return true;
146 }
147
148 function SelectAll(){
149     if(document.bookbag_form.biblionumber.length > 0) {
150                 var checky = AllAreChecked(document.bookbag_form.biblionumber);
151                 var l = document.bookbag_form.biblionumber.length;
152         for (var i=0; i < l; i++) {
153             document.bookbag_form.biblionumber[i].checked = (checky) ? false : true;
154         }
155     }
156 }
157
158 function addMultiple(){
159     var c_value = "";
160     if(document.bookbag_form.biblionumber.length > 0) {
161         for (var i=0; i < document.bookbag_form.biblionumber.length; i++) {
162             if (document.bookbag_form.biblionumber[i].checked) {
163                 c_value = c_value + document.bookbag_form.biblionumber[i].value + "/";
164             }
165         }
166         addSelRecords(c_value);
167     } else {
168         c_value = c_value + document.bookbag_form.biblionumber.value + "/";
169         addSelRecords(c_value);
170     }
171 }
172
173 function addSelRecords(valSel) { // function for adding a selection of biblios to the basket
174                                                 // from the results list
175     var arrayRecords = valSel.split("/");
176     var i = 0;
177     var nbAdd = 0;
178     for (i=0;i<arrayRecords.length;i++) {
179         if (arrayRecords[i]) {
180             nbAdd += addRecord(arrayRecords[i], 1);
181         }
182         else {
183             break;
184         }
185     }
186     var msg = "";
187     if (nbAdd) {
188         if (i > nbAdd) {
189             msg = nbAdd+" "+MSG_NRECORDS_ADDED+", "+(i-nbAdd)+" "+MSG_NRECORDS_IN_BASKET;
190         }
191         else {
192             msg = nbAdd+" "+MSG_NRECORDS_ADDED;
193         }
194     }
195     else {
196         if (i < 1) {
197             msg = MSG_NO_RECORD_SELECTED;
198         }
199         else {
200             msg = MSG_NO_RECORD_ADDED+" ("+MSG_NRECORDS_IN_BASKET+") !";
201         }
202     }
203         showCartUpdate(msg);
204 }
205
206 function showCartUpdate(msg){
207         cartUpdate.setBody(msg);
208         cartUpdate.render("cc");
209         cartUpdate.show();
210         YAHOO.util.Event.addListener("cartUpdate", "click", cartUpdate.hide, cartUpdate, true);
211         setTimeout("cartUpdate.hide()",5000);   
212 }
213
214 function selRecord(num, status) {
215     var str = document.myform.records.value
216     if (status){
217         str += num+"/";
218     }
219     else {
220         str = delRecord(num, str);
221     }
222
223     document.myform.records.value = str;
224 }
225
226 function delSelRecords() {
227     var recordsSel = 0;
228     var end = 0;
229     var nameCookie = "bib_list";
230     var valCookie = readCookie(nameCookie, 1);
231
232     if (valCookie) {
233         var str = document.myform.records.value;
234         if (str.length > 0){
235             recordsSel = 1;
236             var str2 = valCookie;
237             while (!end){
238                 s = str.indexOf("/");
239                 if (s>0){
240                     num = str.substring(0, s)
241                     str = delRecord(num,str);
242                     str2 = delRecord(num,str2);
243                 } else {
244                     end = 1;
245                 }
246             }
247
248             if (str2.length == 0) { // equivalent to emptying the basket
249                 var rep = false;
250                 rep = confirm(MSG_CONFIRM_DEL_BASKET);
251                 if (rep) {
252                     delCookie(nameCookie);
253                     document.location = "about:blank";
254                     updateBasket(0,top.opener);
255                     window.close();
256                 } else {
257                     return;
258                 }
259             } else {
260                 writeCookie(nameCookie, str2, 1);
261             }
262         }
263     }
264
265     if (recordsSel) {
266         var strCookie = "";
267         var nameCookie = "bib_list";
268         var valCookie = readCookie(nameCookie, 1);
269         strCookie = nameCookie + "=" + valCookie;
270         var arrayRecords = valCookie.split("/");
271         updateBasket(arrayRecords.length-1,top.opener);
272         document.location = CGIBIN + "opac-basket.pl?" + strCookie;
273     }
274     else {
275         alert(MSG_NO_RECORD_SELECTED);
276     }
277 }
278
279 function delRecord (n, s) {
280     var re = /\d/;
281     var aux = s;
282     var found = 0;
283     var pos = -1;
284
285     while (!found) {
286         pos = aux.indexOf(n, pos+1);
287         var charAfter = aux.charAt(pos+n.length); // character right after the researched string
288         if (charAfter.match(re)) { // record number inside another one
289             continue;
290         }
291         else { // good record number
292             aux = s.substring(0, pos)+ s.substring(pos+n.length+1, s.length);
293             s = aux;
294             found = 1;
295         }
296     }
297
298     return s;
299 }
300
301
302 function delBasket() {
303     var nameCookie = "bib_list";
304
305     var rep = false;
306     rep = confirm(MSG_CONFIRM_DEL_BASKET);
307     if (rep) {
308         delCookie(nameCookie);
309         document.location = "about:blank";
310         updateBasket(0,top.opener);
311         window.close();
312     }
313 }
314
315
316 function quit() {
317     if (document.myform.records.value) {
318         var rep = false;
319         rep = confirm(MSG_CONFIRM_DEL_RECORDS);
320         if (rep) {
321             delSelRecords();
322         }
323     }
324     updateBasket(arrayRecords.length-1,top.opener);
325     window.close();
326 }
327
328 function sendBasket() {
329     var nameCookie = "bib_list";
330     var valCookie = readCookie(nameCookie);
331     var strCookie = nameCookie + "=" + valCookie;
332
333     var loc = CGIBIN + "opac-sendbasket.pl?" + strCookie;
334
335     var optWin="dependant=yes,scrollbars=no,resizable=no,height=300,width=450,top=50,left=100";
336     var win_form = open(loc,"win_form",optWin);
337 }
338
339 function printBasket() {
340     var loc = document.location + "&print=1";
341     document.location = loc;
342 }
343
344 function showMore() {
345     var strCookie = "";
346
347     var nameCookie = "bib_list";
348     var valCookie = readCookie(nameCookie);
349     if (valCookie) {
350         strCookie = nameCookie + "=" + valCookie;
351     }
352     var loc = CGIBIN + "opac-basket.pl?" + strCookie + "&verbose=1";
353     document.location = loc;
354 }
355
356 function showLess() {
357     var strCookie = "";
358
359     var nameCookie = "bib_list";
360     var valCookie = readCookie(nameCookie);
361     if (valCookie) {
362         strCookie = nameCookie + "=" + valCookie;
363     }
364     var loc = CGIBIN + "opac-basket.pl?" + strCookie + "&verbose=0";
365     document.location = loc;
366 }
367
368 function updateBasket(updated_value,target) {
369         if(target){
370         target.$('#basket').html("<span>"+updated_value+"</span>");
371         target.$('#cartDetails').html(_("Your cart contains ")+updated_value+_(" items"));
372         } else {
373         $('#basket').html("<span>"+updated_value+"</span>");
374         $('#cartDetails').html(_("Your cart contains ")+updated_value+_(" items"));
375         }
376         var basketcount = updated_value;
377 }
378
379 function openBiblio(dest,biblionumber) {
380     openerURL=dest+"?biblionumber="+biblionumber;
381     opener.document.location = openerURL;
382     opener.focus();
383 }
384
385 function addSelToShelf() {
386     var items = document.getElementById('records').value;
387         if(items){
388     document.location = "/cgi-bin/koha/opac-addbybiblionumber.pl?biblionumber="+items;
389         } else {
390         alert(MSG_NO_RECORD_SELECTED);
391     }
392 }
393
394 ///  vShelfAdd()  builds url string for multiple-biblio adds.
395
396 function vShelfAdd() {
397         bibs= new Array;
398         if(document.bookbag_form.biblionumber.length > 0) {
399                 for (var i=0; i < document.bookbag_form.biblionumber.length; i++) {
400                         if (document.bookbag_form.biblionumber[i].checked) {
401                                 bibs.push("biblionumber=" +  document.bookbag_form.biblionumber[i].value);
402                         }
403                 }
404             return bibs.join("&");
405         } else {
406             if (document.bookbag_form.biblionumber.checked) {
407                 return "biblionumber=" + document.bookbag_form.biblionumber.value;
408             }
409         }
410 }
411
412 YAHOO.util.Event.onContentReady("cartDetails", function () {
413         $("#cartDetails").css("display","block").css("visibility","hidden").after("<div id=\"cc\" style=\"visibility: hidden\"></div>");
414         $("#cmspan").html("<a href=\"#\" id=\"cartmenulink\" class=\"\"><i></i><span><i></i><span></span><img src=\"/opac-tmpl/prog/images/cart.gif\" width=\"14\" height=\"14\" alt=\"\" border=\"0\" /> Cart<span id=\"basket\"></span></span></a>");
415         if(basketcount){ updateBasket(basketcount) }    
416 });
417
418 function cartMenuInit() {
419         $('#cartmenulink').click(function(){
420                 openBasket(); return false;
421         });
422         // Build cartOverlay based on markup
423         cartOverlay = new YAHOO.widget.Overlay("cartDetails", { context:["cartmenulink","tr","br"],                                                                                                                                                                                       visible:false,width:"200px",effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.25} } );
424         cartOverlay.render();
425         YAHOO.util.Event.addListener("cartmenulink", "mouseover", cartOverlay.show, cartOverlay, true);
426         YAHOO.util.Event.addListener("cartmenulink", "mouseout", cartOverlay.hide, cartOverlay, true);
427         YAHOO.util.Event.addListener("cartmenulink", "click", cartOverlay.hide, cartOverlay, true);
428         
429         cartUpdate = new YAHOO.widget.Panel("cartUpdate", { context:["cartmenulink","tr","br"],                                                                                                                                                                                   visible:false,draggable:false, close:false,width:"200px",effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.25} } );
430 }
431 YAHOO.util.Event.addListener(window, "load", cartMenuInit);