js refact and first part of md5 login
[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     xhr_wr.open('GET', 'index_wr.php?'+(is_conn ? 'sess='+sess+'&' : '')+'mesg='+mesg, (is_conn ? true : false));
232     xhr_wr.onreadystatechange = function() { return; };
233     xhr_wr.send(null);
234
235     if (!is_conn) {
236         if (xhr_wr.responseText != null) {
237             eval(xhr_wr.responseText);
238         }
239     }
240 }
241
242 function server_request(mesg)
243 {
244     var xhr_wr = createXMLHttpRequest();
245     
246     var is_conn = (sess == "not_connected" ? false : true);
247     
248     console.log("server_request:preresp: "+xhr_wr.responseText);
249
250     xhr_wr.open('GET', 'index_wr.php?'+(is_conn ? 'sess='+sess+'&' : '')+'mesg='+mesg, false);
251     xhr_wr.onreadystatechange = function() { return; };
252     xhr_wr.send(null);
253     
254     if (xhr_wr.responseText != null) {
255         console.log("server_request:resp: "+xhr_wr.responseText);
256         return (xhr_wr.responseText);
257     } 
258     else
259         return (null);
260 }
261
262 /* Stat: CHAT and TABLE */
263
264 function chatt_checksend(obj,e)
265 {
266     var keynum;
267     var keychar;
268     var numcheck;
269
270     if(window.event) { // IE
271         keynum = e.keyCode;
272     }
273     else if(e.which) { // Netscape/Firefox/Opera
274         keynum = e.which;
275     }
276     // alert("OBJ: "+obj);
277     if (keynum == 13 && obj.value != "") { // Enter
278         act_chatt(obj.value);
279         obj.value = "";
280     }
281 }
282 function act_chatt(value)
283 {
284     send_mesg("chatt|"+encodeURIComponent(value));
285     /*
286     obj.disabled = true;
287     obj.value = "";
288     obj.disabled = false;
289     obj.focus();
290     */
291     return false;
292 }
293
294 /* Stat: ROOM */
295 function act_sitdown(table)
296 {
297     send_mesg("sitdown|"+table);
298 }
299
300 function act_wakeup()
301 {
302     send_mesg("wakeup");
303 }
304
305 /* Stat: TABLE  Subst: ASTA */
306 function act_asta(card,pnt)
307 {
308     send_mesg("asta|"+card+"|"+pnt);
309 }
310
311 function act_choose(card)
312 {
313     // alert("sitdown");
314     send_mesg("choose|"+card);
315 }
316
317 /* Stat: TABLE  Subst: GAME */
318 function act_play(card,x,y)
319 {
320     // alert("sitdown");
321     send_mesg("play|"+card+"|"+x+"|"+y);
322 }
323
324 function act_tableinfo()
325 {
326     send_mesg("tableinfo");
327 }
328
329 function act_help()
330 {
331     send_mesg("help");
332 }
333
334 function act_tav()
335 {
336     act_chatt('/tav '+$('txt_in').value); 
337     $('txt_in').value = '';
338 }
339
340 function act_about()
341 {
342     send_mesg("about");
343 }
344
345 function act_roadmap()
346 {
347     send_mesg("roadmap");
348 }
349
350 function act_whysupport()
351 {
352     send_mesg("whysupport");
353 }
354
355 function act_exitlock()
356 {
357     send_mesg("exitlock");
358 }
359
360 function safelogout()
361 {
362     var res;
363     
364     if (g_exitlock < 2) 
365         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.");    
366     else 
367         res = window.confirm("Sei sicuro di volere abbandonare la partita?");
368     if (res)
369         act_logout(g_exitlock);
370 }
371
372 function act_lascio()
373 {
374     send_mesg("lascio");
375 }
376
377 function safelascio()
378 {
379     var res;
380
381     res = window.confirm("Sei sicuro di volere lasciare questa mano?");
382     if (res)
383         act_lascio();
384 }
385
386 function act_logout(exitlock)
387 {
388     send_mesg("logout|"+exitlock);
389 }
390
391 function act_reload()
392 {
393     window.onunload = null;
394     document.location.reload();
395 }
396
397 function act_shutdown()
398 {
399     var c = 0;
400
401     send_mesg("shutdown");
402     while (xhr_wr.readyState != 4)
403         c++;
404 }
405
406 function postact_logout()
407 {
408     // alert("postact_logout");
409     try { 
410         xhr_rd.abort();
411     } catch (e) {}
412
413     // eraseCookie("sess");
414     document.location.assign("index.php");
415 }
416
417 /*
418   function slowimg(img,x1,y1,deltat,free,action,srcend)
419   img    - image to move
420   x1,y1  - destination coords
421   deltat - time for each frame (in msec)
422   free   - when the release the local block for other operations (range: 0 - 1)
423   action - function to run when the image is moved
424   srcend - image to switch when the image is moved
425 */
426
427 function sleep(st, delay)
428 {
429     // alert("LOC_NEW PRE: "+st.st_loc_new);
430
431     st.st_loc_new++;
432
433     setTimeout(function(obj){ if (obj.st_loc_new > obj.st_loc) { obj.st_loc++; }},
434                delay, st);
435 }
436
437 function slowimg(img,x1,y1,deltat,free,action,srcend) {
438     this.img = img;
439
440     // this.x0  = parseInt(document.defaultView.getComputedStyle(this.img, "").getPropertyValue("left"));
441     this.x0 = parseInt(getStyle(this.img,"left", "left"));
442 // alert("img.x0 = "+this.x0);
443     // this.y0  = parseInt(document.defaultView.getComputedStyle(this.img, "").getPropertyValue("top"));
444     this.y0  = parseInt(getStyle(this.img,"top", "top"));
445     this.x1  = x1;
446     this.y1  = y1;
447     this.deltat = deltat;
448     this.free = free;
449     this.action = action;
450     this.srcend = srcend;
451 }
452
453 slowimg.prototype = {
454     img: null, 
455     st: null,
456     x0: 0,
457     y0: 0,
458     x1: 0,
459     y1: 0,
460     dx: 0,
461     dy: 0,
462     free: 0,
463     step_n:    0,
464     step_cur:  0,
465     step_free: 0,
466     time:      0,
467     deltat:   40,
468     tout: 0,
469     action: null,
470     srcend: null,
471     
472     setstart: function(x0,y0)
473     {
474         this.x0 = x0;
475         this.y0 = y0;
476     },
477     
478     setaction: function(act)
479     {
480         this.action = act;
481     },
482     
483
484     settime: function(time) 
485     {
486         this.time = (time < this.deltat ? this.deltat : time);
487         this.step_n = parseInt(this.time / this.deltat);
488         this.dx = (this.x1 - this.x0) / this.step_n;
489         this.dy = (this.y1 - this.y0) / this.step_n;
490         if (this.step_n * this.deltat == this.time) {
491             this.step_n--;
492         }
493         if (this.free < 1) {
494             this.step_free = parseInt(this.step_n * this.free);
495         }
496     },
497     
498     start: function(st)
499     {
500         // $("logz").innerHTML += "               xxxxxxxxxxxxxxxxxxxxxSTART<br>";
501         this.st = st;
502         this.st.st_loc_new++;
503         
504         this.img.style.visibility = "visible";
505         setTimeout(function(obj){ obj.animate(); }, this.deltat, this);
506     },
507     
508     animate: function()
509     {
510         // $("log").innerHTML = "Val " + this.step_cur + " N: " + this.step_n + "<br>";
511         if (this.step_cur == 0) {
512             var date = new Date();
513             // $("logz").innerHTML = "Timestart: " + date + "<br>";
514         }
515         if (this.step_cur <= this.step_n) {
516             this.img.style.left = this.x0 + this.dx * this.step_cur;
517             this.img.style.top  = this.y0 + this.dy * this.step_cur;
518             this.step_cur++;
519             setTimeout(function(obj){ obj.animate(); }, this.deltat, this);
520             if (this.step_cur == this.step_free && this.st != null) {
521                 if (this.st.st_loc < this.st.st_loc_new) {
522                     // alert("QUI1  " + this.step_cur + "  ZZ  "+  this.step_free);
523                     this.st.st_loc++;
524                     this.st = null;
525                 }
526             }
527         }
528         else {
529             this.img.style.left = this.x1;
530             this.img.style.top  = this.y1;
531             // $("logz").innerHTML += "xxxxxxxxxxxxxxxCLEAR<br>";
532             var date = new Date();
533             // $("logz").innerHTML += "Timestop: " + date + "<br>";
534
535             if (this.action != null) {
536                 eval(this.action);
537             }
538
539             if (this.st != null && this.st.st_loc < this.st.st_loc_new) {
540                 // alert("QUI2");
541                 this.st.st_loc++;
542                 this.st = null;
543             }
544             if (this.srcend != null) {
545                 this.img.src = this.srcend;
546             }
547         }
548     }
549 }
550
551 var asta_xarr = new Array(0,66,132);
552
553 /* TODO: impostare gli onclick */
554 function dispose_asta(idx, pnt, nopoint)
555 {
556     var i, btn, pass;
557     var btn;
558
559     for (i = 0 ; i < 10 ; i++) {
560         btn = $("asta"+i);
561         if (i < idx) {
562             btn.src = "img/astapasso"+(pnt >= 0 ? "" : "_ro")+".png";
563             btn.style.cursor = (pnt >= 0 ? "pointer" : "default");
564             pass = -1;
565         }
566         else {
567             btn.src = "img/asta"+i+(pnt >= 0 ? "" : "_ro")+".png";
568             btn.style.cursor = (pnt >= 0 ? "pointer" : "default");
569             pass = i;
570         }
571         if (i < 19)
572             btn.style.left = asta_xarr[i % 3];
573         else
574             btn.style.left = asta_xarr[(i+1) % 3];
575         
576         btn.style.top  = parseInt(i / 3) * 50 + (i == 9 ? 0 : 1);
577
578         if (pnt >= 0) {
579             eval("btn.onclick = function () { act_asta("+pass+",61); }");
580             btn.style.cursor = "pointer";
581         }
582         else {
583             btn.onclick = null;
584             btn.style.cursor = "default";
585         }
586     }
587     
588     
589     btn = $("astaptdiv");
590     btn.style.left = asta_xarr[i % 3];
591     btn.style.top = parseInt(i / 3) * 50 - 2;
592     // btn.style.visibility  = "visible";
593     
594     btn = $("astapt");
595     var rpnt = (pnt < 0 ? -pnt : pnt);
596     btn.value = (rpnt < 61 ? 61 : (rpnt > 120 ? 120 : rpnt));
597     
598     btn = $("astaptsub");
599     btn.style.left = asta_xarr[i % 3];
600     btn.style.top = 25 + parseInt(i / 3) * 50 - 1;
601     btn.src = "img/astaptsub"+(pnt >= 0 ? "" : "_ro")+".png";
602     btn.style.cursor = (pnt >= 0 ? "pointer" : "default");
603     if (pnt >= 0) {
604         btn.onclick = function () { act_asta(9,$("astapt").value); };
605         btn.style.cursor = "pointer";
606     }
607     else {
608         btn.onclick = null;
609         btn.style.cursor = "default";
610     }
611     
612     i+=1;
613     if (nopoint) {
614         btn = $("astapasso");
615         btn.style.left = asta_xarr[i % 3];
616         btn.style.top = parseInt(i / 3) * 50;
617         btn.src = "img/astapashalf"+(pnt >= 0 ? "" : "_ro")+".png";
618         btn.style.cursor = (pnt >= 0 ? "pointer" : "default");
619         if (pnt >= 0) {
620             btn.onclick = function () { act_asta(-1,0); };
621         }
622         else {          
623             btn.onclick = null;
624         }
625
626         btn = $("astalascio");
627         btn.style.left = asta_xarr[i % 3];
628         btn.style.top = parseInt(i / 3) * 50 + 24;
629         btn.src = "img/astalascio.png";
630         btn.style.visibility = "visible";
631         btn.onclick = function () { safelascio(); };
632         }
633     else {
634         btn = $("astapasso");
635         btn.style.left = asta_xarr[i % 3];
636         btn.style.top = parseInt(i / 3) * 50;;
637         btn.src = "img/astapasso"+(pnt >= 0 ? "" : "_ro")+".png";
638         btn.style.cursor = (pnt >= 0 ? "pointer" : "default");
639         if (pnt >= 0) {
640             btn.onclick = function () { act_asta(-1,0); };
641         }
642         else {
643             btn.onclick = null;
644         }
645
646         btn = $("astalascio");
647         btn.style.visibility = "hidden";
648         btn.onclick = null;
649     }
650     // btn.style.visibility  = "visible";
651     $("asta").style.visibility = "visible";
652 }
653
654 function asta_pnt_set(pnt)
655 {
656     btn = $("astapt");
657     var rpnt = (pnt < 0 ? -pnt : pnt);
658     btn.value = (rpnt < 61 ? 61 : (rpnt > 120 ? 120 : rpnt));
659 }
660
661 function hide_asta()
662 {
663     $("asta").style.visibility = "hidden"; 
664 }
665
666
667 function notify(st, text, tout, butt, w, h)
668 {
669     var clo, box;
670     var t = this;
671     
672     this.st = st;
673
674     this.ancestor = document.body;
675     
676     this.st.st_loc_new++;
677
678     clo = document.createElement("input");
679     clo.type = "submit";
680     clo.className = "button";
681     clo.style.bottom = "4px";
682     clo.value = butt;
683     clo.obj = this;
684     clo.onclick = this.input_hide;
685     
686     clodiv = document.createElement("div");
687     clodiv.className = "notify_clo";
688     clodiv.appendChild(clo);
689
690     box = document.createElement("div");
691     box.className = "notify";
692     box.innerHTML = text;
693     box.style.zIndex = 200;
694     box.style.width  = w+"px";
695     box.style.marginLeft  = -parseInt(w/2)+"px";
696     box.style.height = h+"px";
697     box.appendChild(clodiv);
698     box.style.visibility = "visible";
699
700     this.notitag = box;
701     
702     this.ancestor.appendChild(box);
703     
704     this.toutid = setTimeout(function(obj){ obj.unblock(); }, tout, this);
705 }
706
707 notify.prototype = {
708     ancestor: null,
709     st: null,
710     notitag: null,
711     toutid: null,
712     
713     unblock: function()
714     {
715         if (this.st.st_loc < this.st.st_loc_new) {
716             this.st.st_loc++;
717         }
718     },
719     
720     hide: function()
721     {
722         clearTimeout(this.toutid);
723         this.ancestor.removeChild(this.notitag);
724         this.unblock();
725     },
726
727     input_hide: function()
728     {
729         clearTimeout(this.obj.toutid);
730         this.obj.ancestor.removeChild(this.obj.notitag);
731         this.obj.unblock();
732     }
733 }
734         
735
736 function $(id) { 
737     return document.getElementById(id); 
738 }
739
740 function globst() {
741     this.st = -1;
742     this.st_loc = -1;
743     this.st_loc_new = -1;
744     this.comms  = new Array;
745 }
746
747
748
749 function remark_step()
750 {
751     var ct = $("remark").l_remct;
752     
753     if (ct != 0) {
754         ct++;
755         if (ct > 2)
756             ct = 1;
757         $("remark").className = "remark"+ct;
758         $("remark").l_remct = ct;
759         setTimeout(remark_step,500);
760     }
761     else
762         $("remark").className = "remark0";
763     
764     return;
765 }
766
767 function remark_on()
768 {
769     if ($("remark").l_remct == 0) {
770         $("remark").l_remct = 1;
771         setTimeout(remark_step,500);
772     }
773 }
774
775 function remark_off()
776 {
777     $("remark").l_remct = 0;
778     $("remark").className = "remark0";
779 }
780
781
782 function choose_seed(card)
783 {
784     var i;
785
786     $("chooseed").style.visibility = "visible";
787     for (i = 0 ; i < 4 ; i++) {
788         $("seed"+i).src = "img/"+i+""+card+".png";
789         seed=$("seed"+i);
790         eval("seed.onclick = function () { act_choose("+i+""+card+"); };");
791     }
792 }
793
794 function set_names(so,ea,ne,nw,we)
795 {
796 //    alert("EA: "+ea);
797     $("name").innerHTML = so; 
798     $("name").title = unescapeHTML(so); 
799     $("name_ea").innerHTML = ea;
800     $("name_ea").title = unescapeHTML(ea);
801     $("name_ne").innerHTML = ne;
802     $("name_ne").title = unescapeHTML(ne);
803     $("name_nw").innerHTML = nw;
804     $("name_nw").title = unescapeHTML(nw);
805     $("name_we").innerHTML = we;
806     $("name_we").title = unescapeHTML(we);
807
808     return;
809 }
810
811 var astat_suffix = new Array("","_ea","_ne","_nw","_we");
812
813 function show_astat(zer,uno,due,tre,qua)
814 {
815     var astat = new Array(zer,uno,due,tre,qua);
816
817     for (i = 0 ; i < PLAYERS_N ; i++) {
818         idx = (PLAYERS_N + i - table_pos) % PLAYERS_N;
819
820         if (astat[i] == -2) {
821             $("public"+astat_suffix[idx]).style.visibility = "hidden";
822         }
823         else if (astat[i] == -1) {
824             $("public"+astat_suffix[idx]).style.visibility = "visible";
825             $("pubacard"+astat_suffix[idx]).src = "img/astapasso.png";
826             $("pubapnt"+astat_suffix[idx]).innerHTML = "";
827             $("pubapnt"+astat_suffix[idx]).style.visibility = "hidden";
828         }
829         else if (astat[i] <= 10) {
830             $("public"+astat_suffix[idx]).style.visibility = "visible";
831             $("pubacard"+astat_suffix[idx]).src = "img/asta"+astat[i]+".png";
832             $("pubapnt"+astat_suffix[idx]).style.visibility = "hidden";
833         }
834         else if (astat[i] <= 120) {
835             $("public"+astat_suffix[idx]).style.visibility = "visible";
836             $("pubacard"+astat_suffix[idx]).src = "img/asta9.png";
837             $("pubapnt"+astat_suffix[idx]).style.visibility = "inherit"; // XXX VISIBLE
838             $("pubapnt"+astat_suffix[idx]).innerHTML = astat[i];
839         }
840     }
841 }
842
843 function exitlock_show(num, islock)
844 {
845     g_exitlock = num;
846
847     num = (num < 3 ? num : 3);
848     $("exitlock").src = "img/exitlock"+num+(islock ? "n" : "y")+".png";
849     // alert("EXITLOCK: "+$("exitlock").src);
850     $("exitlock").style.visibility = "visible";
851 }
852
853 var fin = 0;
854
855 //    exitlock_show(0, true);
856
857 function table_init() {
858     var sux = new Array("", "_ea", "_ne", "_nw", "_we");
859
860     // console.log("table_init");
861
862     remark_off();
863     $("asta").style.visibility = "hidden";
864     $("caller").style.visibility = "hidden";
865     show_astat(-2,-2,-2,-2,-2);
866     for (i=0 ; i < 8 ; i++) {
867         Drag.init($("card" + i), card_mouseup_cb);
868         for (e = 0 ; e < PLAYERS_N ; e++)
869             $("card"+sux[e]+i).style.visibility = "hidden";
870     }
871     for (i=0 ; i < PLAYERS_N ; i++) {
872         // console.log("shut: "+"takes"+sux[i]);
873         $("takes"+sux[i]).style.visibility = "hidden";
874         }
875
876     for (i = 0 ; i < 8 ; i++) {
877         cards_pos[i] = i;
878         cards_ea_pos[i] = i;
879         cards_ne_pos[i] = i;
880         cards_nw_pos[i] = i;
881         cards_we_pos[i] = i;
882     }
883
884 }
885   
886
887
888 var chatt_lines = new Array();
889 var chatt_lines_n = 0;
890
891 var CHATT_MAXLINES = 40;
892
893 /* PRO CHATT */
894 function chatt_sub(name,str)
895 {
896     var must_scroll = false;
897
898     // alert ($("txt").scrollTop + parseInt(getStyle($("txt"),"height", "height")) -  $("txt").scrollHeight);
899
900   if ($("txt").scrollTop + parseInt(getStyle($("txt"),"height", "height")) -  $("txt").scrollHeight >= 0)
901       must_scroll = true;
902
903   // alert("ARRIVA NAME: "+ name + "  STR:"+str);
904   if (chatt_lines_n == CHATT_MAXLINES) {
905     $("txt").innerHTML = "";
906     for (i = 0 ; i < (CHATT_MAXLINES - 1) ; i++) {
907       chatt_lines[i] = chatt_lines[i+1];
908       $("txt").innerHTML += chatt_lines[i];
909     }
910     chatt_lines[i] = "<b>"+name+"</b> "+str+ "<br>";
911     $("txt").innerHTML += chatt_lines[i];
912   }
913   else {
914     chatt_lines[chatt_lines_n] = "<b>"+name+"</b> "+str+ "<br>";
915     $("txt").innerHTML += chatt_lines[chatt_lines_n];
916     chatt_lines_n++;
917   }
918   // $("txt").innerHTML;
919
920
921   if (must_scroll) {
922       $("txt").scrollTop = 10000000;
923   }
924   // alert("scTOP "+$("txt").scrollTop+"  scHEIGHT: "+$("txt").scrollHeight+" HEIGHT: "+getStyle($("txt"),"height", "height") );
925 }
926
927 /*
928  *  GESTIONE DEI COOKIES
929  */
930 function createCookie(name,value,hours,path) {
931         if (hours) {
932                 var date = new Date();
933                 date.setTime(date.getTime()+(hours*60*60*1000));
934                 var expires = "; expires="+date.toGMTString();
935         }
936         else var expires = "";
937         document.cookie = name+"="+value+expires+"; path="+path;
938 }
939
940 function readCookie(name) {
941         var nameEQ = name + "=";
942         var ca = document.cookie.split(';');
943         for(var i=0;i < ca.length;i++) {
944                 var c = ca[i];
945                 while (c.charAt(0)==' ') c = c.substring(1,c.length);
946                 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
947         }
948         return null;
949 }
950
951 function eraseCookie(name) {
952         createCookie(name,"",-1);
953 }
954
955 var onunload_times = 0;
956
957
958 function onunload_cb () {
959     var u = 0;
960     if (onunload_times == 0) {
961         var res = window.confirm("    Vuoi veramente abbandonare la briscola ?\n(clicca annulla o cancel se vuoi ricaricare la briscola)");
962         if (res == true) {
963             the_end = true; 
964             act_shutdown();
965             while (1) 
966                 u++;
967         }
968         else {
969             try {
970                 location = self.location;
971             } catch (e) {
972                 alert("Ripristino della briscola fallito, per non perdere la sessione ricaricare la pagina manualmente.");
973             }
974         }
975         onunload_times++;
976     }
977     
978     return(false);
979 }
980
981
982 function room_checkspace(emme,tables,inpe)
983 {
984     nome = "<b>";
985     for (i = 0 ; i < emme ; i++) 
986         nome += "m";
987     nome += "</b>";
988
989     alta = "";
990     for (i = 0 ; i < 5 ; i++) 
991         alta += nome+"<br>";
992
993     for (i = 0 ; i < tables ; i++) {
994         $("table"+i).innerHTML = alta;
995         $("table_act"+i).innerHTML = "<input type=\"button\" class=\"button\" name=\"xhenter"+i+"\"  value=\"Mi siedo.\" onclick=\"act_sitdown(1);\">";
996         }
997
998     stand = "<table class=\"table_standup\"><tbody><tr>";
999     for (i = 0 ; i < inpe ; i++) {
1000         stand += "<td>"+nome+"</td>";
1001         if ((i+1) % 4 == 0) {
1002             stand += "</tr><tr>";
1003         }
1004     }
1005     stand += "</tr>";
1006     $("standup").innerHTML = stand;
1007
1008     $("esco").innerHTML = "<input class=\"button\" name=\"logout\" type=\"button\" value=\"Esco.\" onclick=\"window.onunload = null; act_logout();\" type=\"button\">";
1009 }
1010
1011 function  unescapeHTML(cont) {
1012     var div = document.createElement('div');
1013     var memo = "";
1014     var i;
1015
1016     div.innerHTML = cont;
1017     if (div.childNodes[0]) {
1018         if (div.childNodes.length > 1) {
1019             if (div.childNodes.toArray)
1020                 alert("si puo");
1021             else {
1022                 var length = div.childNodes.length, results = new Array(length);
1023             while (length--)
1024                 results[length] = div.childNodes[length];
1025                 
1026             for (i=0 ; i<results.length ; i++)
1027                 memo = memo + results[i].nodeValue;
1028             }
1029
1030             return (memo);
1031         }
1032         else {
1033             return (div.childNodes[0].nodeValue);
1034         }
1035     }
1036     else {
1037         return ('');
1038     }
1039 }
1040
1041 function playsound(tag, sound) {
1042    // g_withflash is a global var
1043    if (g_withflash) {
1044       $(tag).innerHTML = '<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" '+
1045 'codebase="http://active.macromedia.com/flash2/cabs/swflash.cab#version=4,0,0,0" id="mysound" WIDTH=1 HEIGHT=1>' +
1046 '<PARAM NAME="movie" VALUE="../playsound.swf"><PARAM NAME="PLAY" VALUE="true"><PARAM NAME="LOOP" VALUE="false">' +
1047 '<PARAM NAME=FlashVars VALUE="streamUrl='+sound+'">' +
1048 '<EMBED swliveconnect="true" name="mysound" src="../playsound.swf" FlashVars="streamUrl='+sound+'" PLAY="true" LOOP="false" '+
1049 ' WIDTH=1 HEIGHT=1 TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></OBJECT>';
1050    }
1051 }
1052
1053 function topbanner_init()
1054 {
1055 //    setInterval(topbanner_cb, 666);
1056 ;
1057 }
1058
1059 function topbanner_cb()
1060 {
1061     var a, b;
1062
1063     a = $('topbanner').style.backgroundColor;
1064     b = $('topbanner').style.borderLeftColor;
1065
1066     $('topbanner').style.backgroundColor = b;
1067     $('topbanner').style.borderColor = a+" "+a+" "+a+" "+a;
1068
1069     // console.log("A: "+a+"  B: "+b);
1070 }