Corrections to make markup more valid (quoting attributes, encoding ampersands, etc.)
[koha.git] / koha-tmpl / js / basket.js
1 //////////////////////////////////////////////////////////////////////////////
2 // BASIC FUNCTIONS FOR COOKIE MANGEMENT //
3 //////////////////////////////////////////////////////////////////////////////
4
5 var CGIBIN = "/cgi-bin/koha/";
6
7 function writeCookie(name, val, wd) {
8         if (wd) {
9                 parent.opener.document.cookie = name + "=" + val;
10         }
11         else {
12                 parent.document.cookie = name + "=" + val;
13         }
14 }
15
16 function readCookieValue (str, val_beg) {
17         var val_end = str.indexOf(";", val_end);
18         if (val_end == -1)
19                 val_end = str.length;
20         return str.substring(val_beg, val_end);
21 }
22
23 function readCookie(name, wd) {
24         var str_name = name + "=";
25         var str_len = str_name.length;
26         var str_cookie = "";
27         if (wd) {
28                 str_cookie = parent.opener.document.cookie;
29         }
30         else {
31                 str_cookie = parent.document.cookie;
32         }
33         var coo_len = str_cookie.length;
34         var i = 0;
35
36         while (i < coo_len) {
37                 var j = i + str_len;
38                 if (str_cookie.substring(i, j) == str_name)
39                         return readCookieValue(str_cookie, j);
40                 i = str_cookie.indexOf(" ", i) + 1;
41                 if (i == 0)
42                         break;
43         }
44
45         return null;
46 }
47
48 function delCookie(name) {
49         var exp = new Date();
50         exp.setTime(exp.getTime()-1);
51         parent.opener.document.cookie = name + "=null; expires=" + exp.toGMTString();
52 }
53
54
55 ///////////////////////////////////////////////////////////////////
56 // SPECIFIC FUNCTIONS USING COOKIES //
57 ///////////////////////////////////////////////////////////////////
58
59 function openBasket() {
60         var strCookie = "";
61
62         var nameCookie = "bib_list";
63         var valCookie = readCookie(nameCookie);
64         if (valCookie) {
65                 strCookie = nameCookie + "=" + valCookie;
66         }
67
68         if (strCookie) {
69 //              alert(strCookie);
70 //              return;
71
72 //              var Wmax = screen.width;
73 //              var Hmax = screen.height;
74
75                 var iW = 650;
76                 var iH = 600;
77
78                 var optWin = "dependant=yes,status=yes,scrollbars=yes,resizable=no,height="+iH+",width="+iW;
79                 var loc = CGIBIN + "opac-basket.pl?" + strCookie;
80                 var basket = open(loc, "basket", optWin);
81         }
82         else {
83                 alert(MSG_BASKET_EMPTY);
84                 //alert("Il n'y a aucune notice !");
85         }
86 }
87
88
89 function addRecord(val, selection) {
90         var nameCookie = "bib_list";
91         var valCookie = readCookie(nameCookie);
92
93         var write = 0;
94
95         if ( ! valCookie ) { // empty basket
96                 valCookie = val + '/';
97                 write = 1;
98         }
99         else {
100                 // is this record already in the basket ?
101                 var found = false;
102                 var arrayRecords = valCookie.split("/");
103                 
104                 for (var i = 0; i < valCookie.length - 1; i++) {
105                         if (val == arrayRecords[i]) {
106                                 found = true;
107                                 break;
108                         }
109                 }
110
111                 if ( found ) {
112                         if (selection) {
113                                 return 0;
114                         }
115                         alert(MSG_RECORD_IN_BASKET);
116                 }
117                 else {
118                         valCookie += val + '/';
119                         write = 1;
120                 }
121         }
122
123         if (write) {
124                 writeCookie(nameCookie, valCookie);
125                 if (selection) { // ajout à partir d'une sélection de notices
126                         return 1;
127                 }
128                 alert(MSG_RECORD_ADDED);
129         }
130 }
131
132
133 function addSelRecords(valSel) { // fonction permettant d'ajouter une sélection de notices
134                                                                         // (à partir d'une page de résultats) au panier
135         var arrayRecords = valSel.split("/");
136         var i = 0;
137         var nbAdd = 0;
138         for (i=0;i<arrayRecords.length;i++) {
139                 if (arrayRecords[i]) {
140                         nbAdd += addRecord(arrayRecords[i], 1);
141                 }
142                 else {
143                         break;
144                 }
145         }
146
147         var msg = "";
148         if (nbAdd) {
149                 if (i > nbAdd) {
150                         msg = nbAdd+" "+MSG_NRECORDS_ADDED+", "+(i-nbAj)+" "+MSG_NRECORDS_IN_BASKET;
151                 }
152                 else {
153                         msg = nbAdd+" "+MSG_NRECORDS_ADDED;
154                 }
155         }
156         else {
157                 if (i < 1) {
158                         msg = MSG_NO_RECORD_SELECTED;   
159                 }
160                 else {
161                         msg = MSG_NO_RECORD_ADDED+" ("+MSG_NRECORDS_IN_BASKET+") !";
162                 }
163         }
164         alert(msg);
165 }
166
167
168 function selRecord(num, status) {
169         var str = document.myform.records.value
170         if (status){
171                 str += num+"/";
172         }
173         else {
174                 str = delRecord(num, str);
175         }
176
177         document.myform.records.value = str;
178 }
179
180
181 function delSelRecords() {
182         var recordsSel = 0;
183         var end = 0;
184         var nameCookie = "bib_list";
185         var valCookie = readCookie(nameCookie, 1);
186
187         if (valCookie) {
188                 var str = document.myform.records.value;
189                 if (str.length > 0){
190                         recordsSel = 1;
191                         var str2 = valCookie;
192                         while (!end){
193                                 s = str.indexOf("/");
194                                 if (s>0){
195                                         num = str.substring(0, s)
196                                         str = delRecord(num,str);
197                                         str2 = delRecord(num,str2);
198                                 }
199                                 else {
200                                         end = 1;
201                                 }
202                         }
203
204                         if (str2.length == 0) { // equivalent to emptying the basket
205                                 var rep = false;
206                                 rep = confirm(MSG_CONFIRM_DEL_BASKET);
207                                 if (rep) { 
208                                         delCookie(nameCookie);
209                                         document.location = "about:blank";
210                                         window.close();
211                                 }
212                                 else {
213                                         return;
214                                 }
215                         }
216                         else {
217                                 writeCookie(nameCookie, str2, 1);
218                         }
219                 }
220         }
221
222         if (recordsSel) {
223                 var strCookie = "";
224                 var nameCookie = "bib_list";
225                 var valCookie = readCookie(nameCookie, 1);
226                 strCookie = nameCookie + "=" + valCookie;
227                 document.location = CGIBIN + "opac-basket.pl?" + strCookie;
228         }
229         else {
230                 alert(MSG_NO_RECORD_SELECTED);
231         }
232 }
233
234
235 function delRecord (n, s) {
236         var re = /\d/;
237         var aux = s;
238         var found = 0;
239         var pos = -1;
240
241         while (!found) {
242                 pos = aux.indexOf(n, pos+1);
243                 var charAfter = aux.charAt(pos+n.length); // character right after the researched string
244                 if (charAfter.match(re)) { // record number inside another one
245                         continue;
246                 }
247                 else { // good record number
248                         aux = s.substring(0, pos)+ s.substring(pos+n.length+1, s.length);
249                         s = aux;
250                         found = 1;
251                 }
252         }
253
254         return s;
255 }
256
257
258 function delBasket() {
259         var nameCookie = "bib_list";
260
261         var rep = false;
262         rep = confirm(MSG_CONFIRM_DEL_BASKET);
263         if (rep) {
264                 delCookie(nameCookie);
265                 document.location = "about:blank";
266                 window.close();
267         }
268 }
269
270
271 function quit() {
272         if (document.myform.records.value) {
273                 var rep = false;
274                 rep = confirm(MSG_CONFIRM_DEL_RECORDS);
275                 if (rep) {
276                         delSelRecords();
277                 }
278         }
279         window.close();
280 }
281
282
283 function sendBasket() {
284         var nameCookie = "bib_list";
285         var valCookie = readCookie(nameCookie);
286         var strCookie = nameCookie + "=" + valCookie;
287
288         var loc = CGIBIN + "opac-sendbasket.pl?" + strCookie;
289
290         var optWin="dependant=yes,scrollbars=no,resizable=no,height=300,width=400,top=50,left=100";
291         var win_form = open(loc,"win_form",optWin);
292 }
293
294
295 function printBasket() {
296         var loc = document.location + "&print=1";
297         document.location = loc;
298 }