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