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