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