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