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