Fixes for display of cart contents number, other minor markup corrections
[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     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         alert(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,document);
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                 alert(MSG_RECORD_IN_BASKET);
115             }
116         }
117         else {
118             valCookie += val + '/';
119             write = 1;
120             updateBasket(arrayRecords.length,document);
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             alert(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
183     var msg = "";
184     if (nbAdd) {
185         if (i > nbAdd) {
186             msg = nbAdd+" "+MSG_NRECORDS_ADDED+", "+(i-nbAdd)+" "+MSG_NRECORDS_IN_BASKET;
187         }
188         else {
189             msg = nbAdd+" "+MSG_NRECORDS_ADDED;
190         }
191     }
192     else {
193         if (i < 1) {
194             msg = MSG_NO_RECORD_SELECTED;
195         }
196         else {
197             msg = MSG_NO_RECORD_ADDED+" ("+MSG_NRECORDS_IN_BASKET+") !";
198         }
199     }
200     alert(msg);
201 }
202
203
204 function selRecord(num, status) {
205     var str = document.myform.records.value
206     if (status){
207         str += num+"/";
208     }
209     else {
210         str = delRecord(num, str);
211     }
212
213     document.myform.records.value = str;
214 }
215
216 function delSelRecords() {
217     var recordsSel = 0;
218     var end = 0;
219     var nameCookie = "bib_list";
220     var valCookie = readCookie(nameCookie, 1);
221
222     if (valCookie) {
223         var str = document.myform.records.value;
224         if (str.length > 0){
225             recordsSel = 1;
226             var str2 = valCookie;
227             while (!end){
228                 s = str.indexOf("/");
229                 if (s>0){
230                     num = str.substring(0, s)
231                     str = delRecord(num,str);
232                     str2 = delRecord(num,str2);
233                 } else {
234                     end = 1;
235                 }
236             }
237
238             if (str2.length == 0) { // equivalent to emptying the basket
239                 var rep = false;
240                 rep = confirm(MSG_CONFIRM_DEL_BASKET);
241                 if (rep) {
242                     delCookie(nameCookie);
243                     document.location = "about:blank";
244                     updateBasket(0,top.opener.document);
245                     window.close();
246                 } else {
247                     return;
248                 }
249             } else {
250                 writeCookie(nameCookie, str2, 1);
251             }
252         }
253     }
254
255     if (recordsSel) {
256         var strCookie = "";
257         var nameCookie = "bib_list";
258         var valCookie = readCookie(nameCookie, 1);
259         strCookie = nameCookie + "=" + valCookie;
260         var arrayRecords = valCookie.split("/");
261         updateBasket(arrayRecords.length-1,top.opener.document);
262         document.location = CGIBIN + "opac-basket.pl?" + strCookie;
263     }
264     else {
265         alert(MSG_NO_RECORD_SELECTED);
266     }
267 }
268
269 function delRecord (n, s) {
270     var re = /\d/;
271     var aux = s;
272     var found = 0;
273     var pos = -1;
274
275     while (!found) {
276         pos = aux.indexOf(n, pos+1);
277         var charAfter = aux.charAt(pos+n.length); // character right after the researched string
278         if (charAfter.match(re)) { // record number inside another one
279             continue;
280         }
281         else { // good record number
282             aux = s.substring(0, pos)+ s.substring(pos+n.length+1, s.length);
283             s = aux;
284             found = 1;
285         }
286     }
287
288     return s;
289 }
290
291
292 function delBasket() {
293     var nameCookie = "bib_list";
294
295     var rep = false;
296     rep = confirm(MSG_CONFIRM_DEL_BASKET);
297     if (rep) {
298         delCookie(nameCookie);
299         document.location = "about:blank";
300         updateBasket(0,top.opener.document);
301         window.close();
302     }
303 }
304
305
306 function quit() {
307     if (document.myform.records.value) {
308         var rep = false;
309         rep = confirm(MSG_CONFIRM_DEL_RECORDS);
310         if (rep) {
311             delSelRecords();
312         }
313     }
314     updateBasket(arrayRecords.length-1,top.opener.document);
315     window.close();
316 }
317
318 function sendBasket() {
319     var nameCookie = "bib_list";
320     var valCookie = readCookie(nameCookie);
321     var strCookie = nameCookie + "=" + valCookie;
322
323     var loc = CGIBIN + "opac-sendbasket.pl?" + strCookie;
324
325     var optWin="dependant=yes,scrollbars=no,resizable=no,height=300,width=450,top=50,left=100";
326     var win_form = open(loc,"win_form",optWin);
327 }
328
329 function printBasket() {
330     var loc = document.location + "&print=1";
331     document.location = loc;
332 }
333
334 function showMore() {
335     var strCookie = "";
336
337     var nameCookie = "bib_list";
338     var valCookie = readCookie(nameCookie);
339     if (valCookie) {
340         strCookie = nameCookie + "=" + valCookie;
341     }
342     var loc = CGIBIN + "opac-basket.pl?" + strCookie + "&verbose=1";
343     document.location = loc;
344 }
345
346 function showLess() {
347     var strCookie = "";
348
349     var nameCookie = "bib_list";
350     var valCookie = readCookie(nameCookie);
351     if (valCookie) {
352         strCookie = nameCookie + "=" + valCookie;
353     }
354     var loc = CGIBIN + "opac-basket.pl?" + strCookie + "&verbose=0";
355     document.location = loc;
356 }
357
358 function updateBasket(updated_value,target) {
359     if(typeof document.getElementById != "undefined") {
360                 if(target.getElementById('basket')){
361                 target.getElementById('basket').innerHTML = "<div>"+updated_value+"</div>";
362                 }
363                 if(target.getElementById('cartDetails')){
364                         target.getElementById('cartDetails').innerHTML = _("Your cart contains ")+updated_value+_(" items");
365                 }
366     } else if (typeof document.layers != "undefined") {
367         target.layers['basket'].open();
368         target.layers['basket'].write(" ("+updated_value+")");
369         target.layers['basket'].close();
370     } else if(typeof document.all != "undefined" &&  typeof
371 document.getElementById == "undefined") {
372         target.all['basket'].innerHTML = " ("+updated_value+")";
373     }
374         var basketcount = updated_value;
375 }
376
377 function openBiblio(dest,biblionumber) {
378     openerURL=dest+"?biblionumber="+biblionumber;
379     opener.document.location = openerURL;
380     opener.focus();
381 }
382
383 function addSelToShelf() {
384     var items = document.getElementById('records').value;
385     document.location = "/cgi-bin/koha/opac-addbybiblionumber.pl?biblionumber="+items;
386 }
387
388 ///  vShelfAdd()  builds url string for multiple-biblio adds.
389
390 function vShelfAdd() {
391         bibs= new Array;
392         if(document.bookbag_form.biblionumber.length > 0) {
393                 for (var i=0; i < document.bookbag_form.biblionumber.length; i++) {
394                         if (document.bookbag_form.biblionumber[i].checked) {
395                                 bibs.push("biblionumber=" +  document.bookbag_form.biblionumber[i].value);
396                         }
397                 }
398         return bibs.join("&");
399         }
400 }