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