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