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