new virtual shelves changes, keyed by biblionumber
[koha.git] / koha-tmpl / opac-tmpl / prog / en / includes / 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 SelectAll(){
136     if(document.bookbag_form.biblionumber.length > 0) {
137         for (var i=0; i < document.bookbag_form.biblionumber.length; i++) {
138             if (document.bookbag_form.select_all.checked) {
139                 document.bookbag_form.biblionumber[i].checked=true;
140             } else {
141                 document.bookbag_form.biblionumber[i].checked=false;
142             }
143         }
144     }
145 }
146
147 function addMultiple(){
148     var c_value = "";
149     if(document.bookbag_form.biblionumber.length > 0) {
150         for (var i=0; i < document.bookbag_form.biblionumber.length; i++) {
151             if (document.bookbag_form.biblionumber[i].checked) {
152                 c_value = c_value + document.bookbag_form.biblionumber[i].value + "/";
153             }
154         }
155         addSelRecords(c_value);
156     } else {
157         c_value = c_value + document.bookbag_form.biblionumber.value + "/";
158         addSelRecords(c_value);
159     }
160 }
161
162 function addSelToShelf() {
163     var items = document.getElementById('records').value;
164     document.location = "/cgi-bin/koha/opac-addbybiblionumber.pl?biblionumber="+items;
165 }
166
167 function addSelRecords(valSel) { // function for adding a selection of biblios to the basket
168                                                 // from the results list
169     var arrayRecords = valSel.split("/");
170     var i = 0;
171     var nbAdd = 0;
172     for (i=0;i<arrayRecords.length;i++) {
173         if (arrayRecords[i]) {
174             nbAdd += addRecord(arrayRecords[i], 1);
175         }
176         else {
177             break;
178         }
179     }
180
181     var msg = "";
182     if (nbAdd) {
183         if (i > nbAdd) {
184             msg = nbAdd+" "+MSG_NRECORDS_ADDED+", "+(i-nbAdd)+" "+MSG_NRECORDS_IN_BASKET;
185         }
186         else {
187             msg = nbAdd+" "+MSG_NRECORDS_ADDED;
188         }
189     }
190     else {
191         if (i < 1) {
192             msg = MSG_NO_RECORD_SELECTED;
193         }
194         else {
195             msg = MSG_NO_RECORD_ADDED+" ("+MSG_NRECORDS_IN_BASKET+") !";
196         }
197     }
198     alert(msg);
199 }
200
201
202 function selRecord(num, status) {
203     var str = document.myform.records.value
204     if (status){
205         str += num+"/";
206     }
207     else {
208         str = delRecord(num, str);
209     }
210
211     document.myform.records.value = str;
212 }
213
214 function delSelRecords() {
215     var recordsSel = 0;
216     var end = 0;
217     var nameCookie = "bib_list";
218     var valCookie = readCookie(nameCookie, 1);
219
220     if (valCookie) {
221         var str = document.myform.records.value;
222         if (str.length > 0){
223             recordsSel = 1;
224             var str2 = valCookie;
225             while (!end){
226                 s = str.indexOf("/");
227                 if (s>0){
228                     num = str.substring(0, s)
229                     str = delRecord(num,str);
230                     str2 = delRecord(num,str2);
231                 } else {
232                     end = 1;
233                 }
234             }
235
236             if (str2.length == 0) { // equivalent to emptying the basket
237                 var rep = false;
238                 rep = confirm(MSG_CONFIRM_DEL_BASKET);
239                 if (rep) {
240                     delCookie(nameCookie);
241                     document.location = "about:blank";
242                     updateBasket(0,top.opener.document);
243                     window.close();
244                 } else {
245                     return;
246                 }
247             } else {
248                 writeCookie(nameCookie, str2, 1);
249             }
250         }
251     }
252
253     if (recordsSel) {
254         var strCookie = "";
255         var nameCookie = "bib_list";
256         var valCookie = readCookie(nameCookie, 1);
257         strCookie = nameCookie + "=" + valCookie;
258         var arrayRecords = valCookie.split("/");
259         updateBasket(arrayRecords.length-1,top.opener.document);
260         document.location = CGIBIN + "opac-basket.pl?" + strCookie;
261     }
262     else {
263         alert(MSG_NO_RECORD_SELECTED);
264     }
265 }
266
267 function delRecord (n, s) {
268     var re = /\d/;
269     var aux = s;
270     var found = 0;
271     var pos = -1;
272
273     while (!found) {
274         pos = aux.indexOf(n, pos+1);
275         var charAfter = aux.charAt(pos+n.length); // character right after the researched string
276         if (charAfter.match(re)) { // record number inside another one
277             continue;
278         }
279         else { // good record number
280             aux = s.substring(0, pos)+ s.substring(pos+n.length+1, s.length);
281             s = aux;
282             found = 1;
283         }
284     }
285
286     return s;
287 }
288
289
290 function delBasket() {
291     var nameCookie = "bib_list";
292
293     var rep = false;
294     rep = confirm(MSG_CONFIRM_DEL_BASKET);
295     if (rep) {
296         delCookie(nameCookie);
297         document.location = "about:blank";
298         updateBasket(0,top.opener.document);
299         window.close();
300     }
301 }
302
303
304 function quit() {
305     if (document.myform.records.value) {
306         var rep = false;
307         rep = confirm(MSG_CONFIRM_DEL_RECORDS);
308         if (rep) {
309             delSelRecords();
310         }
311     }
312     updateBasket(arrayRecords.length-1,top.opener.document);
313     window.close();
314 }
315
316 function sendBasket() {
317     var nameCookie = "bib_list";
318     var valCookie = readCookie(nameCookie);
319     var strCookie = nameCookie + "=" + valCookie;
320
321     var loc = CGIBIN + "opac-sendbasket.pl?" + strCookie;
322
323     var optWin="dependant=yes,scrollbars=no,resizable=no,height=300,width=400,top=50,left=100";
324     var win_form = open(loc,"win_form",optWin);
325 }
326
327 function printBasket() {
328     var loc = document.location + "&print=1";
329     document.location = loc;
330 }
331
332 function showMore() {
333     var strCookie = "";
334
335     var nameCookie = "bib_list";
336     var valCookie = readCookie(nameCookie);
337     if (valCookie) {
338         strCookie = nameCookie + "=" + valCookie;
339     }
340     var loc = CGIBIN + "opac-basket.pl?" + strCookie + "&verbose=1";
341     document.location = loc;
342 }
343
344 function showLess() {
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=0";
353     document.location = loc;
354 }
355
356 function updateBasket(updated_value,target) {
357     if(typeof document.getElementById != "undefined") {
358         target.getElementById('basket').innerHTML = " ("+updated_value+")";
359     } else if (typeof document.layers != "undefined") {
360         target.layers['basket'].open();
361         target.layers['basket'].write(" ("+updated_value+")");
362         target.layers['basket'].close();
363     } else if(typeof document.all != "undefined" &&  typeof
364 document.getElementById == "undefined") {
365         target.all['basket'].innerHTML = " ("+updated_value+")";
366     }
367 }
368
369 function openBiblio(dest,biblionumber) {
370     openerURL=dest+"?biblionumber="+biblionumber;
371     opener.document.location = openerURL;
372     opener.focus();
373 }