table_idx via cookie
[brisk.git] / web / commons.js
1 /*
2  *  brisk - commons.js
3  *
4  *  Copyright (C) 2006 matteo.nastasi@milug.org
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * General Public License for more details. You should have received a
15  * copy of the GNU General Public License along with this program; if
16  * not, write to the Free Software Foundation, Inc, 59 Temple Place -
17  * Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * $Id$
20  *
21  */
22
23 var PLAYERS_N = 3;
24 var EXIT_BAN_TIME = 900;
25 var cookiepath = "/brisk/";
26
27 function $(id) { return document.getElementById(id); }
28
29 function getStyle(x,IEstyleProp, MozStyleProp) 
30 {
31     if (x.currentStyle) {
32         var y = x.currentStyle[IEstyleProp];
33     } else if (window.getComputedStyle) {
34         var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(MozStyleProp);
35     }
36     return y;
37 }
38
39 /* replacement of setInterval on IE */
40 (function(){
41     /*if not IE, do nothing*/
42     if(!document.uniqueID){return;};
43
44     /*Copy the default setInterval behavior*/
45     var nativeSetInterval = window.setInterval;
46     window.setInterval = function(fn,ms) {              
47         var param = [];
48         if(arguments.length <= 2)       {
49             return nativeSetInterval(fn,ms);
50         }
51         else {
52             for(var i=2;i<arguments.length;i+=1) {
53                 param[i-2] =  arguments[i];
54             }   
55         }
56         
57         if(typeof(fn)=='function') {
58             
59             return (function (fn,ms,param) {
60                 var fo = function () {                                                          
61                     fn.apply(window,param);
62                 };                      
63                 return nativeSetInterval(fo,ms); 
64             })(fn,ms,param);
65         }
66         else if(typeof(fn)=='string')
67         {
68             return  nativeSetInterval(fn,ms);
69         }
70         else
71         {
72             throw Error('setInterval Error\nInvalid function type');
73         };
74     };
75
76     /*Copy the default setTimeout behavior*/
77     var nativeSetTimeout = window.setTimeout;
78     window.setTimeout = function(fn,ms) {               
79         var param = [];
80         if(arguments.length <= 2)       {
81             return nativeSetTimeout(fn,ms);
82         }
83         else {
84             for(var i=2;i<arguments.length;i+=1) {
85                 param[i-2] =  arguments[i];
86             }   
87         }
88         
89         if(typeof(fn)=='function') {
90             
91             return (function (fn,ms,param) {
92                 var fo = function () {                                                          
93                     fn.apply(window,param);
94                 };                      
95                 return nativeSetTimeout(fo,ms); 
96             })(fn,ms,param);
97         }
98         else if(typeof(fn)=='string')
99         {
100             return  nativeSetTimeout(fn,ms);
101         }
102         else
103         {
104             throw Error('setTimeout Error\nInvalid function type');
105         };
106     };
107
108 })()
109
110     // var card_pos = RANGE 0 <= x < cards_ea_n
111
112 function rnd_int(min, max) {
113   return Math.floor(Math.random() * (max - min + 1) + min);
114 }
115
116 function error_images()
117 {
118     alert("GHESEMU!");
119 }
120
121 function abort_images()
122 {
123     alert("ABORTAIMAGES");
124 }
125
126 function unload_images()
127 {
128     alert("ABORTAIMAGES");
129 }
130
131 function reset_images()
132 {
133     alert("ABORTAIMAGES");
134 }
135
136 function update_images()
137 {
138     $("imgct").innerHTML = "Immagini caricate "+g_preload_imgsz_arr[g_imgct]+"%.";
139     if (g_imgct < g_preload_img_arr.length)
140         setTimeout(preload_images, 100, g_preload_img_arr, g_imgct);
141     g_imgct++;
142     // $("imgct").innerHTML += "U";
143 }
144
145 function preload_images(arr,idx)
146 {
147     var im = new Image;
148     
149     // $("imgct").innerHTML = "Stiamo caricando "+arr[idx]+"%.<br>";
150     im.onload =   update_images;
151     im.onerror =  error_images;
152     im.onabort =  abort_images;
153     im.onunload = unload_images;
154     im.onreset =  reset_images;
155     im.src =      arr[idx];
156     // $("imgct").innerHTML += "P";
157 }
158
159 function safestatus(a)
160 {
161     try{
162         return (a.status);
163     } catch(b)
164         { return (-1); }
165 }
166
167 function createXMLHttpRequest() {
168     try { return new ActiveXObject("Msxml2.XMLHTTP");    } catch(e) {}
169     try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {}
170     try { return new XMLHttpRequest();                   } catch(e) {}
171     alert("XMLHttpRequest not supported");
172     return null;
173 }
174
175 function send_mesg(mesg)
176 {
177     var xhr_wr = createXMLHttpRequest();
178
179     
180     xhr_wr.open('GET', 'index_wr.php?sess='+sess+'&mesg='+encodeURIComponent(mesg), true);
181     xhr_wr.onreadystatechange = function() { return; };
182     xhr_wr.send(null);
183
184 }
185
186 /* Stat: CHAT and TABLE */
187
188 function chatt_checksend(obj,e)
189 {
190     var keynum;
191     var keychar;
192     var numcheck;
193
194     if(window.event) { // IE
195         keynum = e.keyCode;
196     }
197     else if(e.which) { // Netscape/Firefox/Opera
198         keynum = e.which;
199     }
200     // alert("OBJ: "+obj);
201     if (keynum == 13 && obj.value != "") { // Enter
202         act_chatt(obj.value);
203         obj.value = "";
204     }
205 }
206 function act_chatt(value)
207 {
208     send_mesg("chatt|"+encodeURIComponent(value));
209     /*
210     obj.disabled = true;
211     obj.value = "";
212     obj.disabled = false;
213     obj.focus();
214     */
215     return false;
216 }
217
218 /* Stat: ROOM */
219 function act_sitdown(table)
220 {
221     send_mesg("sitdown|"+table);
222 }
223
224 function act_wakeup()
225 {
226     send_mesg("wakeup");
227 }
228
229 /* Stat: TABLE  Subst: ASTA */
230 function act_asta(card,pnt)
231 {
232     send_mesg("asta|"+card+"|"+pnt);
233 }
234
235 function act_choose(card)
236 {
237     // alert("sitdown");
238     send_mesg("choose|"+card);
239 }
240
241 /* Stat: TABLE  Subst: GAME */
242 function act_play(card,x,y)
243 {
244     // alert("sitdown");
245     send_mesg("play|"+card+"|"+x+"|"+y);
246 }
247
248 function act_tableinfo()
249 {
250     send_mesg("tableinfo");
251 }
252
253 function act_help()
254 {
255     send_mesg("help");
256 }
257
258 function act_about()
259 {
260     send_mesg("about");
261 }
262
263 function act_exitlock()
264 {
265     send_mesg("exitlock");
266 }
267
268 function safelogout()
269 {
270     var res;
271     
272     if (g_exitlock < 3) 
273         res = window.confirm("Sei sicuro di volere abbandonare la partita?\nATTENZIONE: se esci adesso senza il consenso degli altri giocatori non potrai sederti ai tavoli per "+(Math.floor(EXIT_BAN_TIME/60))+" minuti.");    
274     else 
275         res = window.confirm("Sei sicuro di volere abbandonare la partita?");
276     if (res)
277         act_logout(g_exitlock);
278 }
279
280 function act_lascio()
281 {
282     send_mesg("lascio");
283 }
284
285 function safelascio()
286 {
287     var res;
288
289     res = window.confirm("Sei sicuro di volere lasciare questa mano?");
290     if (res)
291         act_lascio();
292 }
293
294 function act_logout(exitlock)
295 {
296     send_mesg("logout|"+exitlock);
297 }
298
299 function act_reload()
300 {
301     window.onunload = null;
302     document.location.reload();
303 }
304
305 function act_shutdown()
306 {
307     var c = 0;
308
309     send_mesg("shutdown");
310     while (xhr_wr.readyState != 4)
311         c++;
312 }
313
314 function postact_logout()
315 {
316     // alert("postact_logout");
317     try { 
318         xhr_rd.abort();
319     } catch (e) {}
320
321     // eraseCookie("sess");
322     document.location.assign("index.php");
323 }
324
325 /*
326   function slowimg(img,x1,y1,deltat,free,action,srcend)
327   img    - image to move
328   x1,y1  - destination coords
329   deltat - time for each frame (in msec)
330   free   - when the release the local block for other operations (range: 0 - 1)
331   action - function to run when the image is moved
332   srcend - image to switch when the image is moved
333 */
334
335 function sleep(st, delay)
336 {
337     // alert("LOC_NEW PRE: "+st.st_loc_new);
338
339     st.st_loc_new++;
340
341     setTimeout(function(obj){ if (obj.st_loc_new > obj.st_loc) { obj.st_loc++; }},
342                delay, st);
343 }
344
345 function slowimg(img,x1,y1,deltat,free,action,srcend) {
346     this.img = img;
347
348     // this.x0  = parseInt(document.defaultView.getComputedStyle(this.img, "").getPropertyValue("left"));
349     this.x0 = parseInt(getStyle(this.img,"left", "left"));
350 // alert("img.x0 = "+this.x0);
351     // this.y0  = parseInt(document.defaultView.getComputedStyle(this.img, "").getPropertyValue("top"));
352     this.y0  = parseInt(getStyle(this.img,"top", "top"));
353     this.x1  = x1;
354     this.y1  = y1;
355     this.deltat = deltat;
356     this.free = free;
357     this.action = action;
358     this.srcend = srcend;
359 }
360
361 slowimg.prototype = {
362     img: null, 
363     st: null,
364     x0: 0,
365     y0: 0,
366     x1: 0,
367     y1: 0,
368     dx: 0,
369     dy: 0,
370     free: 0,
371     step_n:    0,
372     step_cur:  0,
373     step_free: 0,
374     time:      0,
375     deltat:   40,
376     tout: 0,
377     action: null,
378     srcend: null,
379     
380     setstart: function(x0,y0)
381     {
382         this.x0 = x0;
383         this.y0 = y0;
384     },
385     
386     setaction: function(act)
387     {
388         this.action = act;
389     },
390     
391
392     settime: function(time) 
393     {
394         this.time = (time < this.deltat ? this.deltat : time);
395         this.step_n = parseInt(this.time / this.deltat);
396         this.dx = (this.x1 - this.x0) / this.step_n;
397         this.dy = (this.y1 - this.y0) / this.step_n;
398         if (this.step_n * this.deltat == this.time) {
399             this.step_n--;
400         }
401         this.step_free = parseInt(this.step_n * this.free);
402     },
403     
404     start: function(st)
405     {
406         // $("logz").innerHTML += "               xxxxxxxxxxxxxxxxxxxxxSTART<br>";
407         this.st = st;
408         this.st.st_loc_new++;
409         
410         this.img.style.visibility = "visible";
411         setTimeout(function(obj){ obj.animate(); }, this.deltat, this);
412     },
413     
414     animate: function()
415     {
416         // $("log").innerHTML = "Val " + this.step_cur + " N: " + this.step_n + "<br>";
417         if (this.step_cur == 0) {
418             var date = new Date();
419             // $("logz").innerHTML = "Timestart: " + date + "<br>";
420         }
421         if (this.step_cur <= this.step_n) {
422             this.img.style.left = this.x0 + this.dx * this.step_cur;
423             this.img.style.top  = this.y0 + this.dy * this.step_cur;
424             this.step_cur++;
425             setTimeout(function(obj){ obj.animate(); }, this.deltat, this);
426             if (this.step_cur == this.step_free && this.st != null) {
427                 if (this.st != null && this.st.st_loc < this.st.st_loc_new) {
428                     // alert("QUI1  " + this.step_cur + "  ZZ  "+  this.step_free);
429                     this.st.st_loc++;
430                     this.st = null;
431                 }
432             }
433         }
434         else {
435             this.img.style.left = this.x1;
436             this.img.style.top  = this.y1;
437             // $("logz").innerHTML += "xxxxxxxxxxxxxxxCLEAR<br>";
438             var date = new Date();
439             // $("logz").innerHTML += "Timestop: " + date + "<br>";
440             if (this.st != null && this.st.st_loc < this.st.st_loc_new) {
441                 // alert("QUI2");
442                 this.st.st_loc++;
443                 this.st = null;
444             }
445             if (this.action != null) {
446                 eval(this.action);
447             }
448             if (this.srcend != null) {
449                 this.img.src = this.srcend;
450             }
451         }
452     }
453 }
454
455 var asta_xarr = new Array(0,66,132);
456
457 /* TODO: impostare gli onclick */
458 function dispose_asta(idx, pnt, nopoint)
459 {
460     var i, btn, pass;
461     var btn;
462
463     for (i = 0 ; i < 10 ; i++) {
464         btn = $("asta"+i);
465         if (i < idx) {
466             btn.src = "img/astapasso"+(pnt >= 0 ? "" : "_ro")+".png";
467             btn.style.cursor = (pnt >= 0 ? "pointer" : "default");
468             pass = -1;
469         }
470         else {
471             btn.src = "img/asta"+i+(pnt >= 0 ? "" : "_ro")+".png";
472             btn.style.cursor = (pnt >= 0 ? "pointer" : "default");
473             pass = i;
474         }
475         if (i < 19)
476             btn.style.left = asta_xarr[i % 3];
477         else
478             btn.style.left = asta_xarr[(i+1) % 3];
479         
480         btn.style.top  = parseInt(i / 3) * 50 + (i == 9 ? 0 : 1);
481
482         if (pnt >= 0) {
483             eval("btn.onclick = function () { act_asta("+pass+",61); }");
484             btn.style.cursor = "pointer";
485         }
486         else {
487             btn.onclick = null;
488             btn.style.cursor = "default";
489         }
490     }
491     
492     
493     btn = $("astaptdiv");
494     btn.style.left = asta_xarr[i % 3];
495     btn.style.top = parseInt(i / 3) * 50 - 2;
496     // btn.style.visibility  = "visible";
497     
498     btn = $("astapt");
499     var rpnt = (pnt < 0 ? -pnt : pnt);
500     btn.value = (rpnt < 61 ? 61 : (rpnt > 120 ? 120 : rpnt));
501     
502     btn = $("astaptsub");
503     btn.style.left = asta_xarr[i % 3];
504     btn.style.top = 25 + parseInt(i / 3) * 50 - 1;
505     btn.src = "img/astaptsub"+(pnt >= 0 ? "" : "_ro")+".png";
506     btn.style.cursor = (pnt >= 0 ? "pointer" : "default");
507     if (pnt >= 0) {
508         btn.onclick = function () { act_asta(9,$("astapt").value); };
509         btn.style.cursor = "pointer";
510     }
511     else {
512         btn.onclick = null;
513         btn.style.cursor = "default";
514     }
515     
516     i+=1;
517     if (nopoint) {
518         btn = $("astapasso");
519         btn.style.left = asta_xarr[i % 3];
520         btn.style.top = parseInt(i / 3) * 50;
521         btn.src = "img/astapashalf"+(pnt >= 0 ? "" : "_ro")+".png";
522         btn.style.cursor = (pnt >= 0 ? "pointer" : "default");
523         if (pnt >= 0) {
524             btn.onclick = function () { act_asta(-1,0); };
525         }
526         else {          
527             btn.onclick = null;
528         }
529
530         btn = $("astalascio");
531         btn.style.left = asta_xarr[i % 3];
532         btn.style.top = parseInt(i / 3) * 50 + 24;
533         btn.src = "img/astalascio.png";
534         btn.style.visibility = "visible";
535         btn.onclick = function () { safelascio(); };
536         }
537     else {
538         btn = $("astapasso");
539         btn.style.left = asta_xarr[i % 3];
540         btn.style.top = parseInt(i / 3) * 50;;
541         btn.src = "img/astapasso"+(pnt >= 0 ? "" : "_ro")+".png";
542         btn.style.cursor = (pnt >= 0 ? "pointer" : "default");
543         if (pnt >= 0) {
544             btn.onclick = function () { act_asta(-1,0); };
545         }
546         else {
547             btn.onclick = null;
548         }
549
550         btn = $("astalascio");
551         btn.style.visibility = "hidden";
552         btn.onclick = null;
553     }
554     // btn.style.visibility  = "visible";
555     $("asta").style.visibility = "visible";
556 }
557
558 function asta_pnt_set(pnt)
559 {
560     btn = $("astapt");
561     var rpnt = (pnt < 0 ? -pnt : pnt);
562     btn.value = (rpnt < 61 ? 61 : (rpnt > 120 ? 120 : rpnt));
563 }
564
565 function hide_asta()
566 {
567     $("asta").style.visibility = "hidden"; 
568 }
569
570
571 function notify(st, text, tout, butt, w, h)
572 {
573     var clo, box;
574     var t = this;
575     
576     this.st = st;
577
578     this.ancestor = document.body;
579     
580     this.st.st_loc_new++;
581
582     clo = document.createElement("input");
583     clo.type = "submit";
584     clo.className = "button";
585     clo.style.bottom = "4px";
586     clo.value = butt;
587     clo.obj = this;
588     clo.onclick = this.input_hide;
589     
590     clodiv = document.createElement("div");
591     clodiv.className = "notify_clo";
592     clodiv.appendChild(clo);
593
594     box = document.createElement("div");
595     box.className = "notify";
596     box.innerHTML = text;
597     box.style.zIndex = 200;
598     box.style.width  = w+"px";
599     box.style.marginLeft  = -parseInt(w/2)+"px";
600     box.style.height = h+"px";
601     box.appendChild(clodiv);
602     box.style.visibility = "visible";
603
604     this.notitag = box;
605     
606     this.ancestor.appendChild(box);
607     
608     this.toutid = setTimeout(function(obj){ obj.unblock(); }, tout, this);
609 }
610
611 notify.prototype = {
612     ancestor: null,
613     st: null,
614     notitag: null,
615     toutid: null,
616     
617     unblock: function()
618     {
619         if (this.st.st_loc < this.st.st_loc_new) {
620             this.st.st_loc++;
621         }
622     },
623     
624     hide: function()
625     {
626         clearTimeout(this.toutid);
627         this.ancestor.removeChild(this.notitag);
628         this.unblock();
629     },
630
631     input_hide: function()
632     {
633         clearTimeout(this.obj.toutid);
634         this.obj.ancestor.removeChild(this.obj.notitag);
635         this.obj.unblock();
636     }
637 }
638         
639
640 function $(id) { 
641     return document.getElementById(id); 
642 }
643
644 function globst() {
645     this.st = -1;
646     this.st_loc = -1;
647     this.st_loc_new = -1;
648     this.comms  = new Array;
649 }
650
651
652
653 function remark_step()
654 {
655     var ct = $("remark").l_remct;
656     
657     if (ct != 0) {
658         ct++;
659         if (ct > 2)
660             ct = 1;
661         $("remark").className = "remark"+ct;
662         $("remark").l_remct = ct;
663         setTimeout(remark_step,500);
664     }
665     else
666         $("remark").className = "remark0";
667     
668     return;
669 }
670
671 function remark_on()
672 {
673     if ($("remark").l_remct == 0) {
674         $("remark").l_remct = 1;
675         setTimeout(remark_step,500);
676     }
677 }
678
679 function remark_off()
680 {
681     $("remark").l_remct = 0;
682     $("remark").className = "remark0";
683 }
684
685
686 function choose_seed(card)
687 {
688     var i;
689
690     $("chooseed").style.visibility = "visible";
691     for (i = 0 ; i < 4 ; i++) {
692         $("seed"+i).src = "img/"+i+""+card+".png";
693         seed=$("seed"+i);
694         eval("seed.onclick = function () { act_choose("+i+""+card+"); };");
695     }
696 }
697
698 function set_names(so,ea,ne,nw,we)
699 {
700 //    alert("EA: "+ea);
701     $("name").innerHTML = so; 
702     $("name").title = so; 
703     $("name_ea").innerHTML = ea;
704     $("name_ea").title = ea;
705     $("name_ne").innerHTML = ne;
706     $("name_ne").title = ne;
707     $("name_nw").innerHTML = nw;
708     $("name_nw").title = nw;
709     $("name_we").innerHTML = we;
710     $("name_we").title = we;
711
712     return;
713 }
714
715 var astat_suffix = new Array("","_ea","_ne","_nw","_we");
716
717 function show_astat(zer,uno,due,tre,qua)
718 {
719     var astat = new Array(zer,uno,due,tre,qua);
720
721     for (i = 0 ; i < PLAYERS_N ; i++) {
722         idx = (PLAYERS_N + i - table_pos) % PLAYERS_N;
723
724         if (astat[i] == -2) {
725             $("public"+astat_suffix[idx]).style.visibility = "hidden";
726         }
727         else if (astat[i] == -1) {
728             $("public"+astat_suffix[idx]).style.visibility = "visible";
729             $("pubacard"+astat_suffix[idx]).src = "img/astapasso.png";
730             $("pubapnt"+astat_suffix[idx]).innerHTML = "";
731             $("pubapnt"+astat_suffix[idx]).style.visibility = "hidden";
732         }
733         else if (astat[i] <= 10) {
734             $("public"+astat_suffix[idx]).style.visibility = "visible";
735             $("pubacard"+astat_suffix[idx]).src = "img/asta"+astat[i]+".png";
736             $("pubapnt"+astat_suffix[idx]).style.visibility = "hidden";
737         }
738         else if (astat[i] <= 120) {
739             $("public"+astat_suffix[idx]).style.visibility = "visible";
740             $("pubacard"+astat_suffix[idx]).src = "img/asta9.png";
741             $("pubapnt"+astat_suffix[idx]).style.visibility = "inherit"; // XXX VISIBLE
742             $("pubapnt"+astat_suffix[idx]).innerHTML = astat[i];
743         }
744     }
745 }
746
747 function exitlock_show(num, islock)
748 {
749     g_exitlock = num;
750
751     num = (num < 3 ? num : 3);
752     $("exitlock").src = "img/exitlock"+num+(islock ? "n" : "y")+".png";
753     // alert("EXITLOCK: "+$("exitlock").src);
754     $("exitlock").style.visibility = "visible";
755 }
756
757 var fin = 0;
758
759 //    exitlock_show(0, true);
760
761 function table_init() {
762     var sux = new Array("", "_ea", "_ne", "_nw", "_we");
763
764     remark_off();
765     $("asta").style.visibility = "hidden";
766     $("caller").style.visibility = "hidden";
767     show_astat(-2,-2,-2,-2,-2);
768     for (i=0 ; i < 8 ; i++) {
769         Drag.init($("card" + i), card_mouseup_cb);
770         for (e = 0 ; e < PLAYERS_N ; e++)
771             $("card"+sux[e]+i).style.visibility = "hidden";
772     }
773     for (i=0 ; i < PLAYERS_N ; i++) {
774         $("takes"+sux[i]).style.visibility = "hidden";
775         }
776
777     for (i = 0 ; i < 8 ; i++) {
778         cards_pos[i] = i;
779         cards_ea_pos[i] = i;
780         cards_ne_pos[i] = i;
781         cards_nw_pos[i] = i;
782         cards_we_pos[i] = i;
783     }
784
785 }
786   
787
788
789 var chatt_lines = new Array();
790 var chatt_lines_n = 0;
791
792 var CHATT_MAXLINES = 40;
793
794 /* PRO CHATT */
795 function chatt_sub(name,str)
796 {
797   // alert("ARRIVA NAME: "+ name + "  STR:"+str);
798   if (chatt_lines_n == CHATT_MAXLINES) {
799     $("txt").innerHTML = "";
800     for (i = 0 ; i < (CHATT_MAXLINES - 1) ; i++) {
801       chatt_lines[i] = chatt_lines[i+1];
802       $("txt").innerHTML += chatt_lines[i];
803     }
804     chatt_lines[i] = "<b>"+name+"</b> "+str+ "<br>";
805     $("txt").innerHTML += chatt_lines[i];
806   }
807   else {
808     chatt_lines[chatt_lines_n] = "<b>"+name+"</b> "+str+ "<br>";
809     $("txt").innerHTML += chatt_lines[chatt_lines_n];
810     chatt_lines_n++;
811   }
812   $("txt").innerHTML;
813   $("txt").scrollTop = 10000000;
814 }
815
816 /*
817  *  GESTIONE DEI COOKIES
818  */
819 function createCookie(name,value,hours,path) {
820         if (hours) {
821                 var date = new Date();
822                 date.setTime(date.getTime()+(hours*60*60*1000));
823                 var expires = "; expires="+date.toGMTString();
824         }
825         else var expires = "";
826         document.cookie = name+"="+value+expires+"; path="+path;
827 }
828
829 function readCookie(name) {
830         var nameEQ = name + "=";
831         var ca = document.cookie.split(';');
832         for(var i=0;i < ca.length;i++) {
833                 var c = ca[i];
834                 while (c.charAt(0)==' ') c = c.substring(1,c.length);
835                 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
836         }
837         return null;
838 }
839
840 function eraseCookie(name) {
841         createCookie(name,"",-1);
842 }
843
844 var onunload_times = 0;
845
846
847 function onunload_cb () {
848     var u = 0;
849     if (onunload_times == 0) {
850         var res = window.confirm("    Vuoi veramente abbandonare la briscola ?\n(clicca annulla o cancel se vuoi ricaricare la briscola)");
851         if (res == true) {
852             the_end = true; 
853             act_shutdown();
854             while (1) 
855                 u++;
856         }
857         else {
858             try {
859                 location = self.location;
860             } catch (e) {
861                 alert("Ripristino della briscola fallito, per non perdere la sessione ricaricare la pagina manualmente.");
862             }
863         }
864         onunload_times++;
865     }
866     
867     return(false);
868 }
869
870
871 function room_checkspace(emme,tables,inpe)
872 {
873     nome = "<b>";
874     for (i = 0 ; i < emme ; i++) 
875         nome += "m";
876     nome += "</b>";
877
878     alta = "";
879     for (i = 0 ; i < 5 ; i++) 
880         alta += nome+"<br>";
881
882     for (i = 0 ; i < tables ; i++) {
883         $("table"+i).innerHTML = alta;
884         $("table_act"+i).innerHTML = "<input type=\"button\" class=\"button\" name=\"xhenter"+i+"\"  value=\"Mi siedo.\" onclick=\"act_sitdown(1);\">";
885         }
886
887     stand = "<table class=\"table_standup\"><tbody><tr>";
888     for (i = 0 ; i < inpe ; i++) {
889         stand += "<td>"+nome+"</td>";
890         if ((i+1) % 4 == 0) {
891             stand += "</tr><tr>";
892         }
893     }
894     stand += "</tr>";
895     $("standup").innerHTML = stand;
896
897     $("esco").innerHTML = "<input class=\"button\" name=\"logout\" type=\"button\" value=\"Esco.\" onclick=\"window.onunload = null; act_logout();\" type=\"button\">";
898 }
899
900 function playsound(tag, sound) {
901    // g_withflash is a global var
902    if (g_withflash) {
903       $(tag).innerHTML = '<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" '+
904 'codebase="http://active.macromedia.com/flash2/cabs/swflash.cab#version=4,0,0,0" id="mysound" WIDTH=1 HEIGHT=1>' +
905 '<PARAM NAME="movie" VALUE="playsound.swf"><PARAM NAME="PLAY" VALUE="true"><PARAM NAME="LOOP" VALUE="false">' +
906 '<PARAM NAME=FlashVars VALUE="streamUrl='+sound+'">' +
907 '<EMBED swliveconnect="true" name="mysound" src="playsound.swf" FlashVars="streamUrl='+sound+'" PLAY="true" LOOP="false" '+
908 ' WIDTH=1 HEIGHT=1 TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></OBJECT>';
909    }
910 }