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