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