attivato cambio di pointer nell'asta e aggiunta la funzione per risettare il valore...
[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 postact_logout()
313 {
314     // alert("postact_logout");
315     try { 
316         xhr_rd.abort();
317     } catch (e) {}
318
319     // eraseCookie("sess");
320     document.location.assign("index.php");
321 }
322
323 /*
324   function slowimg(img,x1,y1,deltat,free,action,srcend)
325   img    - image to move
326   x1,y1  - destination coords
327   deltat - time for each frame (in msec)
328   free   - when the release the local block for other operations (range: 0 - 1)
329   action - function to run when the image is moved
330   srcend - image to switch when the image is moved
331 */
332
333 function sleep(st, delay)
334 {
335     // alert("LOC_NEW PRE: "+st.st_loc_new);
336
337     st.st_loc_new++;
338
339     setTimeout(function(obj){ if (obj.st_loc_new > obj.st_loc) { obj.st_loc++; }},
340                delay, st);
341 }
342
343 function slowimg(img,x1,y1,deltat,free,action,srcend) {
344     this.img = img;
345
346     // this.x0  = parseInt(document.defaultView.getComputedStyle(this.img, "").getPropertyValue("left"));
347     this.x0 = parseInt(getStyle(this.img,"left", "left"));
348 // alert("img.x0 = "+this.x0);
349     // this.y0  = parseInt(document.defaultView.getComputedStyle(this.img, "").getPropertyValue("top"));
350     this.y0  = parseInt(getStyle(this.img,"top", "top"));
351     this.x1  = x1;
352     this.y1  = y1;
353     this.deltat = deltat;
354     this.free = free;
355     this.action = action;
356     this.srcend = srcend;
357 }
358
359 slowimg.prototype = {
360     img: null, 
361     st: null,
362     x0: 0,
363     y0: 0,
364     x1: 0,
365     y1: 0,
366     dx: 0,
367     dy: 0,
368     free: 0,
369     step_n:    0,
370     step_cur:  0,
371     step_free: 0,
372     time:      0,
373     deltat:   40,
374     tout: 0,
375     action: null,
376     srcend: null,
377     
378     setstart: function(x0,y0)
379     {
380         this.x0 = x0;
381         this.y0 = y0;
382     },
383     
384     setaction: function(act)
385     {
386         this.action = act;
387     },
388     
389
390     settime: function(time) 
391     {
392         this.time = (time < this.deltat ? this.deltat : time);
393         this.step_n = parseInt(this.time / this.deltat);
394         this.dx = (this.x1 - this.x0) / this.step_n;
395         this.dy = (this.y1 - this.y0) / this.step_n;
396         if (this.step_n * this.deltat == this.time) {
397             this.step_n--;
398         }
399         this.step_free = parseInt(this.step_n * this.free);
400     },
401     
402     start: function(st)
403     {
404         // $("logz").innerHTML += "               xxxxxxxxxxxxxxxxxxxxxSTART<br>";
405         this.st = st;
406         this.st.st_loc_new++;
407         
408         this.img.style.visibility = "visible";
409         setTimeout(function(obj){ obj.animate(); }, this.deltat, this);
410     },
411     
412     animate: function()
413     {
414         // $("log").innerHTML = "Val " + this.step_cur + " N: " + this.step_n + "<br>";
415         if (this.step_cur == 0) {
416             var date = new Date();
417             // $("logz").innerHTML = "Timestart: " + date + "<br>";
418         }
419         if (this.step_cur <= this.step_n) {
420             this.img.style.left = this.x0 + this.dx * this.step_cur;
421             this.img.style.top  = this.y0 + this.dy * this.step_cur;
422             this.step_cur++;
423             setTimeout(function(obj){ obj.animate(); }, this.deltat, this);
424             if (this.step_cur == this.step_free && this.st != null) {
425                 if (this.st != null && this.st.st_loc < this.st.st_loc_new) {
426                     // alert("QUI1  " + this.step_cur + "  ZZ  "+  this.step_free);
427                     this.st.st_loc++;
428                     this.st = null;
429                 }
430             }
431         }
432         else {
433             this.img.style.left = this.x1;
434             this.img.style.top  = this.y1;
435             // $("logz").innerHTML += "xxxxxxxxxxxxxxxCLEAR<br>";
436             var date = new Date();
437             // $("logz").innerHTML += "Timestop: " + date + "<br>";
438             if (this.st != null && this.st.st_loc < this.st.st_loc_new) {
439                 // alert("QUI2");
440                 this.st.st_loc++;
441                 this.st = null;
442             }
443             if (this.action != null) {
444                 eval(this.action);
445             }
446             if (this.srcend != null) {
447                 this.img.src = this.srcend;
448             }
449         }
450     }
451 }
452
453 var asta_xarr = new Array(0,66,132);
454
455 /* TODO: impostare gli onclick */
456 function dispose_asta(idx, pnt, nopoint)
457 {
458     var i, btn, pass;
459     var btn;
460
461     for (i = 0 ; i < 10 ; i++) {
462         btn = $("asta"+i);
463         if (i < idx) {
464             btn.src = "img/astapasso"+(pnt >= 0 ? "" : "_ro")+".png";
465             btn.style.cursor = (pnt >= 0 ? "pointer" : "default");
466             pass = -1;
467         }
468         else {
469             btn.src = "img/asta"+i+(pnt >= 0 ? "" : "_ro")+".png";
470             btn.style.cursor = (pnt >= 0 ? "pointer" : "default");
471             pass = i;
472         }
473         if (i < 19)
474             btn.style.left = asta_xarr[i % 3];
475         else
476             btn.style.left = asta_xarr[(i+1) % 3];
477         
478         btn.style.top  = parseInt(i / 3) * 50 + (i == 9 ? 0 : 1);
479
480         if (pnt >= 0) {
481             eval("btn.onclick = function () { act_asta("+pass+",61); }");
482             btn.style.cursor = "pointer";
483         }
484         else {
485             btn.onclick = null;
486             btn.style.cursor = "default";
487         }
488     }
489     
490     
491     btn = $("astaptdiv");
492     btn.style.left = asta_xarr[i % 3];
493     btn.style.top = parseInt(i / 3) * 50 - 2;
494     // btn.style.visibility  = "visible";
495     
496     btn = $("astapt");
497     var rpnt = (pnt < 0 ? -pnt : pnt);
498     btn.value = (rpnt < 61 ? 61 : (rpnt > 120 ? 120 : rpnt));
499     
500     btn = $("astaptsub");
501     btn.style.left = asta_xarr[i % 3];
502     btn.style.top = 25 + parseInt(i / 3) * 50 - 1;
503     btn.src = "img/astaptsub"+(pnt >= 0 ? "" : "_ro")+".png";
504     btn.style.cursor = (pnt >= 0 ? "pointer" : "default");
505     if (pnt >= 0) {
506         btn.onclick = function () { act_asta(9,$("astapt").value); };
507         btn.style.cursor = "pointer";
508     }
509     else {
510         btn.onclick = null;
511         btn.style.cursor = "default";
512     }
513     
514     i+=1;
515     if (nopoint) {
516         btn = $("astapasso");
517         btn.style.left = asta_xarr[i % 3];
518         btn.style.top = parseInt(i / 3) * 50;
519         btn.src = "img/astapashalf"+(pnt >= 0 ? "" : "_ro")+".png";
520         btn.style.cursor = (pnt >= 0 ? "pointer" : "default");
521         if (pnt >= 0) {
522             btn.onclick = function () { act_asta(-1,0); };
523         }
524         else {          
525             btn.onclick = null;
526         }
527
528         btn = $("astalascio");
529         btn.style.left = asta_xarr[i % 3];
530         btn.style.top = parseInt(i / 3) * 50 + 24;
531         btn.src = "img/astalascio.png";
532         btn.style.visibility = "visible";
533         btn.onclick = function () { safelascio(); };
534         }
535     else {
536         btn = $("astapasso");
537         btn.style.left = asta_xarr[i % 3];
538         btn.style.top = parseInt(i / 3) * 50;;
539         btn.src = "img/astapasso"+(pnt >= 0 ? "" : "_ro")+".png";
540         btn.style.cursor = (pnt >= 0 ? "pointer" : "default");
541         if (pnt >= 0) {
542             btn.onclick = function () { act_asta(-1,0); };
543         }
544         else {
545             btn.onclick = null;
546         }
547
548         btn = $("astalascio");
549         btn.style.visibility = "hidden";
550         btn.onclick = null;
551     }
552     // btn.style.visibility  = "visible";
553     $("asta").style.visibility = "visible";
554 }
555
556 function asta_pnt_set(pnt)
557 {
558     btn = $("astapt");
559     var rpnt = (pnt < 0 ? -pnt : pnt);
560     btn.value = (rpnt < 61 ? 61 : (rpnt > 120 ? 120 : rpnt));
561 }
562
563 function hide_asta()
564 {
565     $("asta").style.visibility = "hidden"; 
566 }
567
568
569 function notify(st, text, tout, butt, w, h)
570 {
571     var clo, box;
572     var t = this;
573     
574     this.st = st;
575
576     this.ancestor = document.body;
577     
578     this.st.st_loc_new++;
579
580     clo = document.createElement("input");
581     clo.type = "submit";
582     clo.className = "button";
583     clo.style.bottom = "4px";
584     clo.value = butt;
585     clo.obj = this;
586     clo.onclick = this.input_hide;
587     
588     clodiv = document.createElement("div");
589     clodiv.className = "notify_clo";
590     clodiv.appendChild(clo);
591
592     box = document.createElement("div");
593     box.className = "notify";
594     box.innerHTML = text;
595     box.style.zIndex = 200;
596     box.style.width  = w+"px";
597     box.style.marginLeft  = -parseInt(w/2)+"px";
598     box.style.height = h+"px";
599     box.appendChild(clodiv);
600     box.style.visibility = "visible";
601
602     this.notitag = box;
603     
604     this.ancestor.appendChild(box);
605     
606     this.toutid = setTimeout(function(obj){ obj.unblock(); }, tout, this);
607 }
608
609 notify.prototype = {
610     ancestor: null,
611     st: null,
612     notitag: null,
613     toutid: null,
614     
615     unblock: function()
616     {
617         if (this.st.st_loc < this.st.st_loc_new) {
618             this.st.st_loc++;
619         }
620     },
621     
622     hide: function()
623     {
624         clearTimeout(this.toutid);
625         this.ancestor.removeChild(this.notitag);
626         this.unblock();
627     },
628
629     input_hide: function()
630     {
631         clearTimeout(this.obj.toutid);
632         this.obj.ancestor.removeChild(this.obj.notitag);
633         this.obj.unblock();
634     }
635 }
636         
637
638 function $(id) { 
639     return document.getElementById(id); 
640 }
641
642 function globst() {
643     this.st = -1;
644     this.st_loc = -1;
645     this.st_loc_new = -1;
646     this.comms  = new Array;
647 }
648
649
650
651 function remark_step()
652 {
653     var ct = $("remark").l_remct;
654     
655     if (ct != 0) {
656         ct++;
657         if (ct > 2)
658             ct = 1;
659         $("remark").className = "remark"+ct;
660         $("remark").l_remct = ct;
661         setTimeout(remark_step,500);
662     }
663     else
664         $("remark").className = "remark0";
665     
666     return;
667 }
668
669 function remark_on()
670 {
671     if ($("remark").l_remct == 0) {
672         $("remark").l_remct = 1;
673         setTimeout(remark_step,500);
674     }
675 }
676
677 function remark_off()
678 {
679     $("remark").l_remct = 0;
680     $("remark").className = "remark0";
681 }
682
683
684 function choose_seed(card)
685 {
686     var i;
687
688     $("chooseed").style.visibility = "visible";
689     for (i = 0 ; i < 4 ; i++) {
690         $("seed"+i).src = "img/"+i+""+card+".png";
691         seed=$("seed"+i);
692         eval("seed.onclick = function () { act_choose("+i+""+card+"); };");
693     }
694 }
695
696 function set_names(so,ea,ne,nw,we)
697 {
698 //    alert("EA: "+ea);
699     $("name").innerHTML = so; 
700     $("name").title = so; 
701     $("name_ea").innerHTML = ea;
702     $("name_ea").title = ea;
703     $("name_ne").innerHTML = ne;
704     $("name_ne").title = ne;
705     $("name_nw").innerHTML = nw;
706     $("name_nw").title = nw;
707     $("name_we").innerHTML = we;
708     $("name_we").title = we;
709
710     return;
711 }
712
713 var astat_suffix = new Array("","_ea","_ne","_nw","_we");
714
715 function show_astat(zer,uno,due,tre,qua)
716 {
717     var astat = new Array(zer,uno,due,tre,qua);
718
719     for (i = 0 ; i < PLAYERS_N ; i++) {
720         idx = (PLAYERS_N + i - table_pos) % PLAYERS_N;
721
722         if (astat[i] == -2) {
723             $("public"+astat_suffix[idx]).style.visibility = "hidden";
724         }
725         else if (astat[i] == -1) {
726             $("public"+astat_suffix[idx]).style.visibility = "visible";
727             $("pubacard"+astat_suffix[idx]).src = "img/astapasso.png";
728             $("pubapnt"+astat_suffix[idx]).innerHTML = "";
729             $("pubapnt"+astat_suffix[idx]).style.visibility = "hidden";
730         }
731         else if (astat[i] <= 10) {
732             $("public"+astat_suffix[idx]).style.visibility = "visible";
733             $("pubacard"+astat_suffix[idx]).src = "img/asta"+astat[i]+".png";
734             $("pubapnt"+astat_suffix[idx]).style.visibility = "hidden";
735         }
736         else if (astat[i] <= 120) {
737             $("public"+astat_suffix[idx]).style.visibility = "visible";
738             $("pubacard"+astat_suffix[idx]).src = "img/asta9.png";
739             $("pubapnt"+astat_suffix[idx]).style.visibility = "inherit"; // XXX VISIBLE
740             $("pubapnt"+astat_suffix[idx]).innerHTML = astat[i];
741         }
742     }
743 }
744
745 function exitlock_show(num, islock)
746 {
747     g_exitlock = num;
748
749     num = (num < 3 ? num : 3);
750     $("exitlock").src = "img/exitlock"+num+(islock ? "n" : "y")+".png";
751     // alert("EXITLOCK: "+$("exitlock").src);
752     $("exitlock").style.visibility = "visible";
753 }
754
755 var fin = 0;
756
757 //    exitlock_show(0, true);
758
759 function table_init() {
760     var sux = new Array("", "_ea", "_ne", "_nw", "_we");
761
762     remark_off();
763     $("asta").style.visibility = "hidden";
764     $("caller").style.visibility = "hidden";
765     show_astat(-2,-2,-2,-2,-2);
766     for (i=0 ; i < 8 ; i++) {
767         Drag.init($("card" + i), card_mouseup_cb);
768         for (e = 0 ; e < PLAYERS_N ; e++)
769             $("card"+sux[e]+i).style.visibility = "hidden";
770     }
771     for (i=0 ; i < PLAYERS_N ; i++) {
772         $("takes"+sux[i]).style.visibility = "hidden";
773         }
774
775     for (i = 0 ; i < 8 ; i++) {
776         cards_pos[i] = i;
777         cards_ea_pos[i] = i;
778         cards_ne_pos[i] = i;
779         cards_nw_pos[i] = i;
780         cards_we_pos[i] = i;
781     }
782
783 }
784   
785
786
787 var chatt_lines = new Array();
788 var chatt_lines_n = 0;
789
790 var CHATT_MAXLINES = 40;
791
792 /* PRO CHATT */
793 function chatt_sub(name,str)
794 {
795   // alert("ARRIVA NAME: "+ name + "  STR:"+str);
796   if (chatt_lines_n == CHATT_MAXLINES) {
797     $("txt").innerHTML = "";
798     for (i = 0 ; i < (CHATT_MAXLINES - 1) ; i++) {
799       chatt_lines[i] = chatt_lines[i+1];
800       $("txt").innerHTML += chatt_lines[i];
801     }
802     chatt_lines[i] = "<b>"+name+"</b> "+str+ "<br>";
803     $("txt").innerHTML += chatt_lines[i];
804   }
805   else {
806     chatt_lines[chatt_lines_n] = "<b>"+name+"</b> "+str+ "<br>";
807     $("txt").innerHTML += chatt_lines[chatt_lines_n];
808     chatt_lines_n++;
809   }
810   $("txt").innerHTML;
811   $("txt").scrollTop = 10000000;
812 }
813
814 /*
815  *  GESTIONE DEI COOKIES
816  */
817 function createCookie(name,value,hours,path) {
818         if (hours) {
819                 var date = new Date();
820                 date.setTime(date.getTime()+(hours*60*60*1000));
821                 var expires = "; expires="+date.toGMTString();
822         }
823         else var expires = "";
824         document.cookie = name+"="+value+expires+"; path="+path;
825 }
826
827 function readCookie(name) {
828         var nameEQ = name + "=";
829         var ca = document.cookie.split(';');
830         for(var i=0;i < ca.length;i++) {
831                 var c = ca[i];
832                 while (c.charAt(0)==' ') c = c.substring(1,c.length);
833                 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
834         }
835         return null;
836 }
837
838 function eraseCookie(name) {
839         createCookie(name,"",-1);
840 }
841
842 var onunload_times = 0;
843
844
845 function onunload_cb () {
846     var u = 0;
847     if (onunload_times == 0) {
848         var res = window.confirm("    Vuoi veramente abbandonare la briscola ?\n(clicca annulla o cancel se vuoi ricaricare la briscola)");
849         if (res == true) {
850             the_end = true; 
851             act_shutdown();
852             while (1) 
853                 u++;
854         }
855         else {
856             try {
857                 location = self.location;
858             } catch (e) {
859                 alert("Ripristino della briscola fallito, per non perdere la sessione ricaricare la pagina manualmente.");
860             }
861         }
862         onunload_times++;
863     }
864     
865     return(false);
866 }
867
868
869 function room_checkspace(emme,tables,inpe)
870 {
871     nome = "<b>";
872     for (i = 0 ; i < emme ; i++) 
873         nome += "m";
874     nome += "</b>";
875
876     alta = "";
877     for (i = 0 ; i < 5 ; i++) 
878         alta += nome+"<br>";
879
880     for (i = 0 ; i < tables ; i++) {
881         $("table"+i).innerHTML = alta;
882         $("table_act"+i).innerHTML = "<input type=\"button\" class=\"button\" name=\"xhenter"+i+"\"  value=\"Mi siedo.\" onclick=\"act_sitdown(1);\">";
883         }
884
885     stand = "<table class=\"table_standup\"><tbody><tr>";
886     for (i = 0 ; i < inpe ; i++) {
887         stand += "<td>"+nome+"</td>";
888         if ((i+1) % 4 == 0) {
889             stand += "</tr><tr>";
890         }
891     }
892     stand += "</tr>";
893     $("standup").innerHTML = stand;
894
895     $("esco").innerHTML = "<input class=\"button\" name=\"logout\" type=\"button\" value=\"Esco.\" onclick=\"window.onunload = null; act_logout();\" type=\"button\">";
896 }
897
898 function playsound(tag, sound) {
899    // g_withflash is a global var
900    if (g_withflash) {
901       $(tag).innerHTML = '<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" '+
902 'codebase="http://active.macromedia.com/flash2/cabs/swflash.cab#version=4,0,0,0" id="mysound" WIDTH=1 HEIGHT=1>' +
903 '<PARAM NAME="movie" VALUE="playsound.swf"><PARAM NAME="PLAY" VALUE="true"><PARAM NAME="LOOP" VALUE="false">' +
904 '<PARAM NAME=FlashVars VALUE="streamUrl='+sound+'">' +
905 '<EMBED swliveconnect="true" name="mysound" src="playsound.swf" FlashVars="streamUrl='+sound+'" PLAY="true" LOOP="false" '+
906 ' WIDTH=1 HEIGHT=1 TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></OBJECT>';
907    }
908 }