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