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