Trying to fancy up the process of adding items to cart. Unfinished.
[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 function getBasketCount(){
12 if(valCookie){
13     var arrayRecords = valCookie.split("/");
14     if(arrayRecords.length > 0){
15         var basketcount = arrayRecords.length-1;
16     } else {
17         var basketcount = "";
18     }
19 } else {
20         var basketcount = "";
21 }
22 return basketcount;
23 }
24 var bCount = getBasketCount();
25
26 function writeCookie(name, val, wd) {
27     if (wd) {
28         parent.opener.document.cookie = name + "=" + val;
29     }
30     else {
31         parent.document.cookie = name + "=" + val;
32     }
33 }
34
35 function readCookieValue (str, val_beg) {
36     var val_end = str.indexOf(";", val_end);
37     if (val_end == -1)
38         val_end = str.length;
39     return str.substring(val_beg, val_end);
40 }
41
42 function readCookie(name, wd) {
43     var str_name = name + "=";
44     var str_len = str_name.length;
45     var str_cookie = "";
46     if (wd) {
47         str_cookie = parent.opener.document.cookie;
48     }
49     else {
50         str_cookie = parent.document.cookie;
51     }
52         // fixed - getting the part of the basket that is bib_list
53         var cookie_parts = str_cookie.split(";");
54             for(var i=0;i < cookie_parts.length;i++) {
55                     var c = cookie_parts[i];
56                     while (c.charAt(0)==' ') c = c.substring(1,c.length);
57                     if(c.indexOf(str_name) == 0) return c.substring(str_name.length,c.length);
58             }
59     return null;
60 }
61
62 function delCookie(name) {
63     var exp = new Date();
64     exp.setTime(exp.getTime()-1);
65     parent.opener.document.cookie = name + "=null; expires=" + exp.toGMTString();
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,adressbar=no,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         alert(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                 alert(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             alert(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         $("#cartDetails").html(msg);
204         cartOverlay.show();
205         alert(nbAdd);
206         newtotal = nbAdd + Number($('#basket span').html());
207         setTimeout("cartOverlay.hide(updateCart("+newtotal+"))",5000);
208 }
209
210 function updateCart(newtotal){
211 $('#cartDetails').html(_("Your cart contains ")+newtotal+_(" items"));
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 bCount = 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     document.location = "/cgi-bin/koha/opac-addbybiblionumber.pl?biblionumber="+items;
388 }
389
390 ///  vShelfAdd()  builds url string for multiple-biblio adds.
391
392 function vShelfAdd() {
393         bibs= new Array;
394         if(document.bookbag_form.biblionumber.length > 0) {
395                 for (var i=0; i < document.bookbag_form.biblionumber.length; i++) {
396                         if (document.bookbag_form.biblionumber[i].checked) {
397                                 bibs.push("biblionumber=" +  document.bookbag_form.biblionumber[i].value);
398                         }
399                 }
400             return bibs.join("&");
401         } else {
402             if (document.bookbag_form.biblionumber.checked) {
403                 return "biblionumber=" + document.bookbag_form.biblionumber.value;
404             }
405         }
406 }
407
408 YAHOO.util.Event.onContentReady("cartDetails", function () {
409         $("#cartDetails").css("display","block").css("visibility","hidden");
410         $("#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>");
411         if(bCount){ updateBasket(bCount) }      
412 });
413
414 function cartMenuInit() {
415         $('#cartmenulink').click(function(){
416                 openBasket(); return false;
417         });
418         // Build cartOverlay based on markup
419         cartOverlay = new YAHOO.widget.Overlay("cartDetails", { context:["cartmenulink","tr","br"],                                                                                                                                                                                       visible:false,width:"200px",effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.25} } );
420         cartOverlay.render();
421         YAHOO.util.Event.addListener("cartmenulink", "mouseover", cartOverlay.show, cartOverlay, true);
422         YAHOO.util.Event.addListener("cartmenulink", "mouseout", cartOverlay.hide, cartOverlay, true);
423         YAHOO.util.Event.addListener("cartmenulink", "click", cartOverlay.hide, cartOverlay, true);
424 }
425 YAHOO.util.Event.addListener(window, "load", cartMenuInit);