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