Bug 9016: (follow-up) standardize case of message transport type columns
[koha.git] / koha-tmpl / intranet-tmpl / prog / en / js / basket.js
1 //////////////////////////////////////////////////////////////////////////////
2 // BASIC FUNCTIONS FOR COOKIE MANAGEMENT //
3 //////////////////////////////////////////////////////////////////////////////
4
5 var CGIBIN = "/cgi-bin/koha/";
6
7 var nameCookie = "intranet_bib_list";
8 var nameParam = "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 + "; path=/";
25     }
26     else {
27         parent.document.cookie = name + "=" + val + "; path=/";
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     if(parent.opener){
62         parent.opener.document.cookie = name + "=null; path=/; expires=" + exp.toGMTString();
63     } else {
64         document.cookie = name + "=null; path=/; expires=" + exp.toGMTString();
65     }
66 }
67
68 ///////////////////////////////////////////////////////////////////
69 // SPECIFIC FUNCTIONS USING COOKIES //
70 ///////////////////////////////////////////////////////////////////
71
72 function openBasket() {
73     var strCookie = "";
74     var valCookie = readCookie(nameCookie);
75     if ( valCookie ) {
76         strCookie = nameParam + "=" + valCookie;
77     }
78
79     if ( strCookie ) {
80         var iW = 820;
81         var iH = 450;
82         var optWin = "status=yes,scrollbars=yes,resizable=yes,toolbar=no,location=yes,height="+iH+",width="+iW;
83         var loc = CGIBIN + "basket/basket.pl?" + strCookie;
84         var basket = open(loc, "basket", optWin);
85         if (window.focus) {basket.focus()}
86     }
87     else {
88         showCartUpdate(MSG_BASKET_EMPTY);
89     }
90 }
91
92 function addRecord(val, selection,NoMsgAlert) {
93     var valCookie = readCookie(nameCookie);
94     var write = 0;
95
96     if ( ! valCookie ) { // empty basket
97         valCookie = val + '/';
98         write = 1;
99         updateBasket(1);
100     }
101     else {
102         // is this record already in the basket ?
103         var found = false;
104         var arrayRecords = valCookie.split("/");
105         for (var i = 0; i < valCookie.length - 1; i++) {
106             if (val == arrayRecords[i]) {
107                 found = true;
108                 break;
109             }
110         }
111         if ( found ) {
112             if (selection) {
113                 return 0;
114             }
115             if (! NoMsgAlert ) {
116                 showCartUpdate(MSG_RECORD_IN_BASKET);
117             }
118         }
119         else {
120             valCookie += val + '/';
121             write = 1;
122             updateBasket(arrayRecords.length);
123         }
124     }
125
126     if (write) {
127         writeCookie(nameCookie, valCookie);
128         if (selection) { // when adding a selection of records
129             return 1;
130         }
131         if (! NoMsgAlert ) {
132             showCartUpdate(MSG_RECORD_ADDED);
133         }
134     }
135 }
136
137 function AllAreChecked(s){
138         if (! s.length) { return false;}
139         var l = s.length;
140         for (var i=0; i < l; i++) {
141                 if(! s[i].checked) { return false; }
142         }
143         return true;
144 }
145
146 function SelectAll(){
147     if(document.bookbag_form.biblionumber.length > 0) {
148                 var checky = AllAreChecked(document.bookbag_form.biblionumber);
149                 var l = document.bookbag_form.biblionumber.length;
150         for (var i=0; i < l; i++) {
151             document.bookbag_form.biblionumber[i].checked = (checky) ? false : true;
152         }
153     }
154 }
155
156 function addMultiple(biblist){
157     var c_value = "";
158     if(biblist.length > 0) {
159         for (var i=0; i < biblist.length; i++) {
160             if (biblist[i].checked) {
161                 c_value = c_value + biblist[i].value + "/";
162             }
163         }
164         addSelRecords(c_value);
165     } else {
166         c_value = c_value + biblist.value + "/";
167         addSelRecords(c_value);
168     }
169 }
170
171 function addSelRecords(valSel) { // function for adding a selection of biblios to the basket
172                                                 // from the results list
173     var arrayRecords = valSel.split("/");
174     var i = 0;
175     var nbAdd = 0;
176     for (i=0;i<arrayRecords.length;i++) {
177         if (arrayRecords[i]) {
178             nbAdd += addRecord(arrayRecords[i], 1);
179         }
180         else {
181             break;
182         }
183     }
184     var msg = "";
185     if (nbAdd) {
186         if (i > nbAdd) {
187             msg = nbAdd+" "+MSG_NRECORDS_ADDED+", "+(i-nbAdd)+" "+MSG_NRECORDS_IN_BASKET;
188         }
189         else {
190             msg = nbAdd+" "+MSG_NRECORDS_ADDED;
191         }
192     }
193     else {
194         if (i < 1) {
195             msg = MSG_NO_RECORD_SELECTED;
196         }
197         else {
198             msg = MSG_NO_RECORD_ADDED+" ("+MSG_NRECORDS_IN_BASKET+") !";
199         }
200     }
201         showCartUpdate(msg);
202 }
203
204 function showCartUpdate(msg){
205         // set body of popup window
206         $("#cartDetails").html(msg);
207         showCart();
208         setTimeout("hideCart()",2000);  
209 }
210
211 function showListsUpdate(msg){
212        // set body of popup window
213        alert(msg);
214 }
215
216 function selRecord(num, status) {
217     var str = document.myform.records.value
218     if (status){
219         str += num+"/";
220     }
221     else {
222         str = delRecord(num, str);
223     }
224
225     document.myform.records.value = str;
226 }
227
228 function delSelRecords() {
229     var recordsSel = 0;
230     var end = 0;
231     var valCookie = readCookie(nameCookie, 1);
232
233     if (valCookie) {
234         var str = document.myform.records.value;
235         if (str.length > 0){
236             recordsSel = 1;
237             var str2 = valCookie;
238             while (!end){
239                 s = str.indexOf("/");
240                 if (s>0){
241                     num = str.substring(0, s)
242                     str = delRecord(num,str);
243                     str2 = delRecord(num,str2);
244                 } else {
245                     end = 1;
246                 }
247             }
248
249             if (str2.length == 0) { // equivalent to emptying the basket
250                 var rep = false;
251                 rep = confirm(MSG_CONFIRM_DEL_BASKET);
252                 if (rep) {
253                     delCookie(nameCookie);
254                     document.location = "about:blank";
255                     updateBasket(0,top.opener);
256                     window.close();
257                 } else {
258                     return;
259                 }
260             } else {
261                 writeCookie(nameCookie, str2, 1);
262             }
263         }
264     }
265
266     if (recordsSel) {
267         var strCookie = "";
268         var valCookie = readCookie(nameCookie, 1);
269         strCookie = nameParam + "=" + valCookie;
270         var arrayRecords = valCookie.split("/");
271         updateBasket(arrayRecords.length-1,top.opener);
272         document.location = CGIBIN + "basket/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(context,rep) {
303     if (rep == undefined){
304         rep = confirm(MSG_CONFIRM_DEL_BASKET);
305     }
306     if (rep) {
307         if(context == "popup"){
308             delCookie(nameCookie);
309             document.location = "about:blank";
310             updateBasket(0,top.opener);
311             window.close();
312         } else {
313             delCookie(nameCookie);
314             updateBasket(0,top.opener);
315         }
316     }
317 }
318
319
320 function quit() {
321     if (document.myform.records.value) {
322         var rep = false;
323         rep = confirm(MSG_CONFIRM_DEL_RECORDS);
324         if (rep) {
325             delSelRecords();
326         }
327     }
328     updateBasket(arrayRecords.length-1,top.opener);
329     window.close();
330 }
331
332 function sendBasket() {
333     var valCookie = readCookie(nameCookie);
334     var strCookie = nameParam + "=" + valCookie;
335
336     var loc = CGIBIN + "basket/sendbasket.pl?" + strCookie;
337
338     var optWin="scrollbars=no,resizable=no,height=300,width=450,top=50,left=100";
339     var win_form = open(loc,"win_form",optWin);
340 }
341
342 function downloadBasket() {
343     var valCookie = readCookie(nameCookie);
344     var strCookie = nameParam + "=" + valCookie;
345
346     var loc = CGIBIN + "basket/downloadcart.pl?" + strCookie;
347
348     open(loc,"win_form",'scrollbars=no,resizable=no,height=300,width=450,top=50,left=100');
349 }
350
351 function printBasket() {
352     var loc = document.location + "&print=1";
353     document.location = loc;
354 }
355
356 function showMore() {
357     var strCookie = "";
358
359     var valCookie = readCookie(nameCookie);
360     if (valCookie) {
361         strCookie = nameParam + "=" + valCookie;
362     }
363     var loc = CGIBIN + "basket/basket.pl?" + strCookie + "&verbose=1";
364     document.location = loc;
365 }
366
367 function showLess() {
368     var strCookie = "";
369
370     var valCookie = readCookie(nameCookie);
371     if (valCookie) {
372         strCookie = nameParam + "=" + valCookie;
373     }
374     var loc = CGIBIN + "basket/basket.pl?" + strCookie + "&verbose=0";
375     document.location = loc;
376 }
377
378 function updateBasket(updated_value,target) {
379         if(target){
380         target.$('#basketcount').html(" <span>("+updated_value+")</span>");
381     target.$('#cartDetails').html(MSG_IN_YOUR_CART+updated_value);
382         } else {
383         $('#basketcount').html(" <span>("+updated_value+")</span>");
384     $('#cartDetails').html(MSG_IN_YOUR_CART+updated_value);
385         }
386         var basketcount = updated_value;
387 }
388
389 function openBiblio(dest,biblionumber) {
390     openerURL=dest+"?biblionumber="+biblionumber;
391     opener.document.location = openerURL;
392     opener.focus();
393 }
394
395 function addSelToShelf() {
396     var items = document.getElementById('records').value;
397         if(items){
398     document.location = "/cgi-bin/koha/virtualshelves/addbybiblionumber.pl?biblionumber="+items;
399         } else {
400         alert(MSG_NO_RECORD_SELECTED);
401     }
402 }
403
404 ///  vShelfAdd()  builds url string for multiple-biblio adds.
405
406 function vShelfAdd(biblist) {
407         bibs = new Array();
408         if(biblist.length > 0) {
409                 for (var i=0; i < biblist.length; i++) {
410                         if (biblist[i].checked) {
411                                 bibs.push("biblionumber=" +  biblist[i].value);
412                         }
413                 }
414         if (bibs.length == 0) { showListsUpdate(MSG_NO_RECORD_SELECTED); }
415             return bibs.join("&");
416         } else {
417             if (biblist.checked) {
418                 return "biblionumber=" + biblist.value;
419             }
420         }
421 }
422
423 function showCart(){
424                 var position = $("#cartmenulink").offset();
425         var scrolld = $(window).scrollTop();
426                 var top = position.top + $("#cartmenulink").outerHeight();
427         if( scrolld > top ){
428             top = scrolld + 15;
429         }
430                 var left = position.left
431                 $("#cartDetails").css("position","absolute").css("top",top);
432                 $("#cartDetails").css("position","absolute").css("left",left);
433                 $("#cartDetails").fadeIn("fast");
434 }
435
436 function hideCart(){
437     $("#cartDetails").fadeOut("fast");
438 }
439
440 $(document).ready(function(){
441         $("#cartmenulink").click(function(){ openBasket(); return false; });
442         if(basketcount){ updateBasket(basketcount); }
443 });
444
445