Putting this file in includes for easier access. Contains updates to basket functiona...
[koha-ffzg.git] / koha-tmpl / opac-tmpl / css / 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 addMultiple(){
23 var c_value = "";
24 if(document.myform.bibid.length > 0){
25 for (var i=0; i < document.myform.bibid.length; i++)
26    {
27    if (document.myform.bibid[i].checked)
28       {
29       c_value = c_value + document.myform.bibid[i].value + "|";
30       }
31    }
32         var arrayChosen = c_value.split("|");
33         for(i=0;i<(arrayChosen.length-1);i++){
34                 addRecord(arrayChosen[i],"","silent");
35         }
36         alert(i+MSG_NRECORDS_ADDED);
37         } else {
38                 alert(MSG_NO_RECORD_SELECTED);
39         }
40 }
41
42 function writeCookie(name, val, wd) {
43         if (wd) {
44                 parent.opener.document.cookie = name + "=" + val;
45         }
46         else {
47                 parent.document.cookie = name + "=" + val;
48         }
49 }
50
51 function readCookieValue (str, val_beg) {
52         var val_end = str.indexOf(";", val_end);
53         if (val_end == -1)
54                 val_end = str.length;
55         return str.substring(val_beg, val_end);
56 }
57
58 function readCookie(name, wd) {
59         var str_name = name + "=";
60         var str_len = str_name.length;
61         var str_cookie = "";
62         if (wd) {
63                 str_cookie = parent.opener.document.cookie;
64         }
65         else {
66                 str_cookie = parent.document.cookie;
67         }
68         var coo_len = str_cookie.length;
69         var i = 0;
70
71         while (i < coo_len) {
72                 var j = i + str_len;
73                 if (str_cookie.substring(i, j) == str_name)
74                         return readCookieValue(str_cookie, j);
75                 i = str_cookie.indexOf(" ", i) + 1;
76                 if (i == 0)
77                         break;
78         }
79
80         return null;
81 }
82
83 function delCookie(name) {
84         var exp = new Date();
85         exp.setTime(exp.getTime()-1);
86         parent.opener.document.cookie = name + "=null; expires=" + exp.toGMTString();
87 }
88
89
90 ///////////////////////////////////////////////////////////////////
91 // SPECIFIC FUNCTIONS USING COOKIES //
92 ///////////////////////////////////////////////////////////////////
93
94 function openBasket() {
95         var strCookie = "";
96
97         var nameCookie = "bib_list";
98         var valCookie = readCookie(nameCookie);
99         if (valCookie) {
100                 strCookie = nameCookie + "=" + valCookie;
101         }
102
103         if (strCookie) {
104 //              alert(strCookie);
105 //              return;
106
107 //              var Wmax = screen.width;
108 //              var Hmax = screen.height;
109
110                 var iW = 620;
111                 var iH = 450;
112
113                 var optWin = "dependant=yes,status=yes,scrollbars=yes,resizable=yes,toolbar=yes,height="+iH+",width="+iW;
114                 var loc = CGIBIN + "opac-basket.pl?" + strCookie;
115                 var basket = open(loc, "basket", optWin);
116         }
117         else {
118                 alert(MSG_BASKET_EMPTY);
119                 //alert("Il n'y a aucune notice !");
120         }
121 }
122
123
124 function addRecord(val, selection, display) {
125         var nameCookie = "bib_list";
126         var valCookie = readCookie(nameCookie);
127         var write = 0;
128
129         if ( ! valCookie ) { // empty basket
130                 valCookie = val + '/';
131                 write = 1;
132                 updateBasket(1,document);
133         }
134         else {
135                 // is this record already in the basket ?
136                 var found = false;
137                 var arrayRecords = valCookie.split("/");
138                 
139                 for (var i = 0; i < valCookie.length - 1; i++) {
140                         if (val == arrayRecords[i]) {
141                                 found = true;
142                                 break;
143                         }
144                 }
145
146                 if ( found ) {
147                         if (selection) {
148                                 return 0;
149                         }
150                         if( display != "silent" ){
151                         alert(MSG_RECORD_IN_BASKET);
152                         }
153                 }
154                 else {
155                         valCookie += val + '/';
156                         write = 1;
157                         updateBasket(arrayRecords.length,document);
158                 }
159         }
160
161         if (write) {
162                 writeCookie(nameCookie, valCookie);
163                 if (selection) { // ajout à partir d'une sélection de notices
164                         return 1;
165                 }
166                         if( display != "silent" ){
167                 alert(MSG_RECORD_ADDED);
168                 }
169         }
170 }
171
172
173 function addSelRecords(valSel) { // fonction permettant d'ajouter une sélection de notices
174                                                                         // (à partir d'une page de résultats) au panier
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
187         var msg = "";
188         if (nbAdd) {
189                 if (i > nbAdd) {
190                         msg = nbAdd+" "+MSG_NRECORDS_ADDED+", "+(i-nbAj)+" "+MSG_NRECORDS_IN_BASKET;
191                 }
192                 else {
193                         msg = nbAdd+" "+MSG_NRECORDS_ADDED;
194                 }
195         }
196         else {
197                 if (i < 1) {
198                         msg = MSG_NO_RECORD_SELECTED;   
199                 }
200                 else {
201                         msg = MSG_NO_RECORD_ADDED+" ("+MSG_NRECORDS_IN_BASKET+") !";
202                 }
203         }
204         alert(msg);
205 }
206
207
208 function selRecord(num, status) {
209         var str = document.myform.records.value
210         if (status){
211                 str += num+"/";
212         }
213         else {
214                 str = delRecord(num, str);
215         }
216
217         document.myform.records.value = str;
218 }
219
220
221 function delSelRecords() {
222         var recordsSel = 0;
223         var end = 0;
224         var nameCookie = "bib_list";
225         var valCookie = readCookie(nameCookie, 1);
226
227         if (valCookie) {
228                 var str = document.myform.records.value;
229                 if (str.length > 0){
230                         recordsSel = 1;
231                         var str2 = valCookie;
232                         while (!end){
233                                 s = str.indexOf("/");
234                                 if (s>0){
235                                         num = str.substring(0, s)
236                                         str = delRecord(num,str);
237                                         str2 = delRecord(num,str2);
238                                 } else {
239                                         end = 1;
240                                 }
241                         }
242
243                         if (str2.length == 0) { // equivalent to emptying the basket
244                                 var rep = false;
245                                 rep = confirm(MSG_CONFIRM_DEL_BASKET);
246                                 if (rep) { 
247                                         delCookie(nameCookie);
248                                         document.location = "about:blank";
249                                         updateBasket(0,top.opener.document);
250                                         window.close();
251                                 } else {
252                                         return;
253                                 }
254                         } else {
255                                 writeCookie(nameCookie, str2, 1);
256                         }
257                 }
258         }
259
260         if (recordsSel) {
261                 var strCookie = "";
262                 var nameCookie = "bib_list";
263                 var valCookie = readCookie(nameCookie, 1);
264                 strCookie = nameCookie + "=" + valCookie;
265                 var arrayRecords = valCookie.split("/");
266                 updateBasket(arrayRecords.length-1,top.opener.document);
267                 document.location = CGIBIN + "opac-basket.pl?" + strCookie;
268         }
269         else {
270                 alert(MSG_NO_RECORD_SELECTED);
271         }
272 }
273
274
275 function delRecord (n, s) {
276         var re = /\d/;
277         var aux = s;
278         var found = 0;
279         var pos = -1;
280
281         while (!found) {
282                 pos = aux.indexOf(n, pos+1);
283                 var charAfter = aux.charAt(pos+n.length); // character right after the researched string
284                 if (charAfter.match(re)) { // record number inside another one
285                         continue;
286                 }
287                 else { // good record number
288                         aux = s.substring(0, pos)+ s.substring(pos+n.length+1, s.length);
289                         s = aux;
290                         found = 1;
291                 }
292         }
293
294         return s;
295 }
296
297
298 function delBasket() {
299         var nameCookie = "bib_list";
300
301         var rep = false;
302         rep = confirm(MSG_CONFIRM_DEL_BASKET);
303         if (rep) {
304                 delCookie(nameCookie);
305                 document.location = "about:blank";
306                 updateBasket(0,top.opener.document);
307                 window.close();
308         }
309 }
310
311
312 function quit() {
313         if (document.myform.records.value) {
314                 var rep = false;
315                 rep = confirm(MSG_CONFIRM_DEL_RECORDS);
316                 if (rep) {
317                         delSelRecords();
318                 }
319         }
320         updateBasket(arrayRecords.length-1,top.opener.document);
321         window.close();
322 }
323
324 function sendBasket() {
325         var nameCookie = "bib_list";
326         var valCookie = readCookie(nameCookie);
327         var strCookie = nameCookie + "=" + valCookie;
328
329         var loc = CGIBIN + "opac-sendbasket.pl?" + strCookie;
330
331         var optWin="dependant=yes,scrollbars=no,resizable=no,height=300,width=400,top=50,left=100";
332         var win_form = open(loc,"win_form",optWin);
333 }
334
335 function printBasket() {
336         var loc = document.location + "&print=1";
337         document.location = loc;
338 }
339
340 function showMore() {
341         var strCookie = "";
342
343         var nameCookie = "bib_list";
344         var valCookie = readCookie(nameCookie);
345         if (valCookie) {
346                 strCookie = nameCookie + "=" + valCookie;
347         }
348         var loc = CGIBIN + "opac-basket.pl?" + strCookie + "&verbose=1";
349         document.location = loc;
350 }
351
352 function showLess() {
353         var strCookie = "";
354
355         var nameCookie = "bib_list";
356         var valCookie = readCookie(nameCookie);
357         if (valCookie) {
358                 strCookie = nameCookie + "=" + valCookie;
359         }
360         var loc = CGIBIN + "opac-basket.pl?" + strCookie + "&verbose=0";
361         document.location = loc;
362 }
363
364 function updateBasket(updated_value,target) {
365     if(typeof document.getElementById != "undefined") {
366         target.getElementById('basket').innerHTML = " ("+updated_value+")";
367     } else if (typeof document.layers != "undefined") {
368         target.layers['basket'].open();
369         target.layers['basket'].write(" ("+updated_value+")");
370         target.layers['basket'].close();
371     } else if(typeof document.all != "undefined" &&  typeof
372 document.getElementById == "undefined") {
373         target.all['basket'].innerHTML = " ("+updated_value+")";
374     }
375 }