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