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