block_time extension added to notify_ex
[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 var mlang_commons = { 'imgload_a' : { 'it' : 'Immagine caricate ', 
31                                       'en' : 'Loaded images ' },
32                       'imgload_b' : { 'it' : '%.', 
33                                       'en' : '%.' },
34                       'gamleav'   : { 'it' : 'Sei sicuro di volere lasciare questa mano?' ,
35                                       'en' : 'Are you sure to leave this game?' },
36                       'brileav'   : { 'it' : '    Vuoi veramente abbandonare la briscola ?\n(clicca annulla o cancel se vuoi ricaricare la briscola)',
37                                       'en' : '    Are you really sure to leave briscola ?\n(click cancel yo reload it)' },
38                       'brireco'   : { 'it' : 'Ripristino della briscola fallito, per non perdere la sessione ricaricare la pagina manualmente.',
39                                       'en' : 'Recovery of briscola failed, to keep the current session reload the page manually.' },
40                       'btn_sit'   : { 'it' : 'Mi siedo.',
41                                       'en' : 'Sit down.' },
42                       'btn_exit'  : { 'it' : 'Esco.',
43                                       'en' : 'Exit.' }
44                       
45                       };
46
47 function $(id) { return document.getElementById(id); }
48
49 function getStyle(x,IEstyleProp, MozStyleProp) 
50 {
51     if (x.currentStyle) {
52         var y = x.currentStyle[IEstyleProp];
53     } else if (window.getComputedStyle) {
54         var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(MozStyleProp);
55     }
56     return y;
57 }
58
59 /* replacement of setInterval on IE */
60 (function(){
61     /*if not IE, do nothing*/
62     if(!document.uniqueID){return;};
63
64     /*Copy the default setInterval behavior*/
65     var nativeSetInterval = window.setInterval;
66     window.setInterval = function(fn,ms) {              
67         var param = [];
68         if(arguments.length <= 2)       {
69             return nativeSetInterval(fn,ms);
70         }
71         else {
72             for(var i=2;i<arguments.length;i+=1) {
73                 param[i-2] =  arguments[i];
74             }   
75         }
76         
77         if(typeof(fn)=='function') {
78             
79             return (function (fn,ms,param) {
80                 var fo = function () {                                                          
81                     fn.apply(window,param);
82                 };                      
83                 return nativeSetInterval(fo,ms); 
84             })(fn,ms,param);
85         }
86         else if(typeof(fn)=='string')
87         {
88             return  nativeSetInterval(fn,ms);
89         }
90         else
91         {
92             throw Error('setInterval Error\nInvalid function type');
93         };
94     };
95
96     /*Copy the default setTimeout behavior*/
97     var nativeSetTimeout = window.setTimeout;
98     window.setTimeout = function(fn,ms) {               
99         var param = [];
100         if(arguments.length <= 2)       {
101             return nativeSetTimeout(fn,ms);
102         }
103         else {
104             for(var i=2;i<arguments.length;i+=1) {
105                 param[i-2] =  arguments[i];
106             }   
107         }
108         
109         if(typeof(fn)=='function') {
110             
111             return (function (fn,ms,param) {
112                 var fo = function () {                                                          
113                     fn.apply(window,param);
114                 };                      
115                 return nativeSetTimeout(fo,ms); 
116             })(fn,ms,param);
117         }
118         else if(typeof(fn)=='string')
119         {
120             return  nativeSetTimeout(fn,ms);
121         }
122         else
123         {
124             throw Error('setTimeout Error\nInvalid function type');
125         };
126     };
127
128 })()
129
130 function addEvent(obj,type,fn)
131 {
132     if (obj.addEventListener) {
133         obj.addEventListener( type, fn, false);
134     }
135     else if (obj.attachEvent) {
136         obj["e"+type+fn] = fn;
137         obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
138         obj.attachEvent( "on"+type, obj[type+fn] );
139     }
140     else
141         throw new Error("Event registration not supported");
142 }
143
144 function removeEvent(obj,type,fn)
145 {
146     if (obj.removeEventListener) {
147         obj.removeEventListener( type, fn, false );
148     }
149     else if (obj.detachEvent) {
150         obj.detachEvent( "on"+type, obj[type+fn] );
151         obj[type+fn] = null;
152         obj["e"+type+fn] = null;
153     }
154 }
155
156     // var card_pos = RANGE 0 <= x < cards_ea_n
157
158 function show_bigpict(obj, act, x, y)
159 {
160    var big, sfx;
161
162    if (arguments.length > 4)
163        sfx = arguments[4];
164    else
165        sfx = '';
166
167    big = $(obj.id+"_big"+sfx);
168    if (act == "over") {
169        big.style.left = obj.offsetLeft + x+"px";
170        big.style.top  = obj.offsetTop  + y+"px";
171        big.style.visibility = "visible";
172        }
173    else {
174        big.style.visibility = "hidden";
175        }
176 }
177
178 function rnd_int(min, max) {
179   return Math.floor(Math.random() * (max - min + 1) + min);
180 }
181
182 function error_images()
183 {
184     // alert("GHESEMU!");
185     setTimeout(preload_images, 2000, g_preload_img_arr, g_imgct-1);
186 }
187
188 function abort_images()
189 {
190     // alert("ABORTAIMAGES");
191     setTimeout(preload_images, 2000, g_preload_img_arr, g_imgct-1);
192 }
193
194 function unload_images()
195 {
196     // alert("ABORTAIMAGES");
197     setTimeout(preload_images, 2000, g_preload_img_arr, g_imgct-1);
198 }
199
200 function reset_images()
201 {
202     // alert("ABORTAIMAGES");
203     setTimeout(preload_images, 2000, g_preload_img_arr, g_imgct-1);
204 }
205
206 function update_images()
207 {
208     // MLANG "Immagine caricate" + g_preload_imgsz_arr[g_imgct] + "%."
209     $("imgct").innerHTML = mlang_commons['imgload_a'][g_lang]+g_preload_imgsz_arr[g_imgct]+"%.";
210     if (g_imgct+1 < g_preload_img_arr.length) {
211         g_imgct++;
212         setTimeout(preload_images, 100, g_preload_img_arr, g_imgct-1);
213     }
214     // $("imgct").innerHTML += "U";
215 }
216
217 function preload_images(arr,idx)
218 {
219     var im = new Image;
220     
221     // $("imgct").innerHTML = "Stiamo caricando "+arr[idx]+"%.<br>";
222     im.onload =   update_images;
223     im.onerror =  error_images;
224     im.onabort =  abort_images;
225     im.onunload = unload_images;
226     im.onreset =  reset_images;
227     im.src =      arr[idx];
228     // $("imgct").innerHTML += "P";
229 }
230
231 function safestatus(a)
232 {
233     try{
234         return (a.status);
235     } catch(b)
236         { return (-1); }
237 }
238
239 function createXMLHttpRequest() {
240     try { return new ActiveXObject("Msxml2.XMLHTTP");    } catch(e) {}
241     try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {}
242     try { return new XMLHttpRequest();                   } catch(e) {}
243     alert("XMLHttpRequest not supported");
244     return null;
245 }
246
247 function send_mesg(mesg)
248 {
249     var xhr_wr = createXMLHttpRequest();
250     var is_conn = (sess == "not_connected" ? false : true);
251     
252     // alert("xhr_wr: "+xhr_wr+"  is_conn: "+is_conn);
253     xhr_wr.open('GET', 'index_wr.php?'+(is_conn ? 'sess='+sess+'&' : '')+'mesg='+mesg, (is_conn ? true : false));
254     xhr_wr.onreadystatechange = function() { return; };
255     xhr_wr.send(null);
256
257     if (!is_conn) {
258         if (xhr_wr.responseText != null) {
259             eval(xhr_wr.responseText);
260         }
261     }
262 }
263
264 function server_request()
265 {
266     var xhr_wr = createXMLHttpRequest();
267     var i, collect = "";
268
269     if (arguments.length > 0) {
270         for (i = 0 ; i < arguments.length ; i+= 2) {
271             collect += (i == 0 ? "" : "&") + arguments[i] + "=" + encodeURIComponent(arguments[i+1]);
272         }
273     }
274     // alert("Args: "+arguments.length);
275
276     var is_conn = (sess == "not_connected" ? false : true);
277     
278     // console.log("server_request:preresp: "+xhr_wr.responseText);
279
280     xhr_wr.open('GET', 'index_wr.php?'+(is_conn ? 'sess='+sess+'&' : '')+collect, false);
281     xhr_wr.onreadystatechange = function() { return; };
282     xhr_wr.send(null);
283     
284     if (xhr_wr.responseText != null) {
285         // console.log("server_request:resp: "+xhr_wr.responseText);
286         return (xhr_wr.responseText);
287     } 
288     else
289         return (null);
290 }
291
292 /* Stat: CHAT and TABLE */
293
294 function chatt_checksend(obj,e)
295 {
296     var keynum;
297     var keychar;
298     var numcheck;
299
300     if(window.event) { // IE
301         keynum = e.keyCode;
302     }
303     else if(e.which) { // Netscape/Firefox/Opera
304         keynum = e.which;
305     }
306     // alert("OBJ: "+obj);
307     if (keynum == 13 && obj.value != "") { // Enter
308         act_chatt(obj.value);
309         obj.value = "";
310     }
311 }
312 function act_chatt(value)
313 {
314     send_mesg("chatt|"+encodeURIComponent(value));
315     /*
316     obj.disabled = true;
317     obj.value = "";
318     obj.disabled = false;
319     obj.focus();
320     */
321     return false;
322 }
323
324 /* Stat: ROOM */
325 function act_sitdown(table)
326 {
327     send_mesg("sitdown|"+table);
328 }
329
330 function act_wakeup()
331 {
332     send_mesg("wakeup");
333 }
334
335 function act_splash(date)
336 {
337     send_mesg("splash|"+date);
338 }
339
340 function act_help()
341 {
342     send_mesg("help");
343 }
344
345 function act_passwdhowto()
346 {
347     send_mesg("passwdhowto");
348 }
349
350 function act_mesgtoadm()
351 {
352     send_mesg("mesgtoadm");
353 }
354
355 function act_tav()
356 {
357     act_chatt('/tav '+$('txt_in').value); 
358     $('txt_in').value = '';
359 }
360
361 function act_about()
362 {
363     send_mesg("about");
364 }
365
366 function act_placing()
367 {
368     send_mesg("placing");
369 }
370
371 function act_roadmap()
372 {
373     send_mesg("roadmap");
374 }
375
376 function act_whysupport()
377 {
378     send_mesg("whysupport");
379 }
380
381 function act_lascio()
382 {
383     send_mesg("lascio");
384 }
385
386 function safelascio()
387 {
388     var res;
389     // MLANG "Sei sicuro di volere lasciare questa mano?"
390     res = window.confirm(mlang_commons['gamleav'][g_lang]);
391     if (res)
392         act_lascio();
393 }
394
395 function act_logout(exitlock)
396 {
397     send_mesg("logout|"+exitlock);
398 }
399
400 function act_reloadroom()
401 {
402     window.onunload = null;
403     window.onbeforeunload = null;
404     document.location.assign("index.php");
405 }
406
407 function act_shutdown()
408 {
409     var c = 0;
410
411     send_mesg("shutdown");
412     // while (xhr_wr.readyState != 4)
413     //  c++;
414 }
415
416 function postact_logout()
417 {
418     // alert("postact_logout");
419     try { 
420         xhr_rd.abort();
421     } catch (e) {}
422
423     // eraseCookie("sess");
424     document.location.assign("index.php");
425 }
426
427 /*
428   function slowimg(img,x1,y1,deltat,free,action,srcend)
429   img    - image to move
430   x1,y1  - destination coords
431   deltat - time for each frame (in msec)
432   free   - when the release the local block for other operations (range: 0 - 1)
433   action - function to run when the image is moved
434   srcend - image to switch when the image is moved
435 */
436
437 function sleep(st, delay)
438 {
439     // alert("LOC_NEW PRE: "+st.st_loc_new);
440
441     st.st_loc_new++;
442
443     setTimeout(function(obj){ if (obj.st_loc_new > obj.st_loc) { obj.st_loc++; }},
444                delay, st);
445 }
446
447 function slowimg(img,x1,y1,deltat,free,action,srcend) {
448     this.img = img;
449
450     // this.x0  = parseInt(document.defaultView.getComputedStyle(this.img, "").getPropertyValue("left"));
451     this.x0 = parseInt(getStyle(this.img,"left", "left"));
452 // alert("img.x0 = "+this.x0);
453     // this.y0  = parseInt(document.defaultView.getComputedStyle(this.img, "").getPropertyValue("top"));
454     this.y0  = parseInt(getStyle(this.img,"top", "top"));
455     this.x1  = x1;
456     this.y1  = y1;
457     this.deltat = deltat;
458     this.free = free;
459     this.action = action;
460     this.srcend = srcend;
461 }
462
463 slowimg.prototype = {
464     img: null, 
465     st: null,
466     x0: 0,
467     y0: 0,
468     x1: 0,
469     y1: 0,
470     dx: 0,
471     dy: 0,
472     free: 0,
473     step_n:    0,
474     step_cur:  0,
475     step_free: 0,
476     time:      0,
477     deltat:   40,
478     tout: 0,
479     action: null,
480     srcend: null,
481     
482     setstart: function(x0,y0)
483     {
484         this.x0 = x0;
485         this.y0 = y0;
486     },
487     
488     setaction: function(act)
489     {
490         this.action = act;
491     },
492     
493
494     settime: function(time) 
495     {
496         this.time = (time < this.deltat ? this.deltat : time);
497         this.step_n = parseInt(this.time / this.deltat);
498         this.dx = (this.x1 - this.x0) / this.step_n;
499         this.dy = (this.y1 - this.y0) / this.step_n;
500         if (this.step_n * this.deltat == this.time) {
501             this.step_n--;
502         }
503         if (this.free < 1) {
504             this.step_free = parseInt(this.step_n * this.free);
505         }
506     },
507     
508     start: function(st)
509     {
510         // $("logz").innerHTML += "               xxxxxxxxxxxxxxxxxxxxxSTART<br>";
511         this.st = st;
512         this.st.st_loc_new++;
513         
514         this.img.style.visibility = "visible";
515         setTimeout(function(obj){ obj.animate(); }, this.deltat, this);
516     },
517     
518     animate: function()
519     {
520         // $("log").innerHTML = "Val " + this.step_cur + " N: " + this.step_n + "<br>";
521         if (this.step_cur == 0) {
522             var date = new Date();
523             // $("logz").innerHTML = "Timestart: " + date + "<br>";
524         }
525         if (this.step_cur <= this.step_n) {
526             this.img.style.left = this.x0 + this.dx * this.step_cur;
527             this.img.style.top  = this.y0 + this.dy * this.step_cur;
528             this.step_cur++;
529             setTimeout(function(obj){ obj.animate(); }, this.deltat, this);
530             if (this.step_cur == this.step_free && this.st != null) {
531                 if (this.st.st_loc < this.st.st_loc_new) {
532                     // alert("QUI1  " + this.step_cur + "  ZZ  "+  this.step_free);
533                     this.st.st_loc++;
534                     this.st = null;
535                 }
536             }
537         }
538         else {
539             this.img.style.left = this.x1;
540             this.img.style.top  = this.y1;
541             // $("logz").innerHTML += "xxxxxxxxxxxxxxxCLEAR<br>";
542             var date = new Date();
543             // $("logz").innerHTML += "Timestop: " + date + "<br>";
544
545             if (this.action != null) {
546                 eval(this.action);
547             }
548
549             if (this.st != null && this.st.st_loc < this.st.st_loc_new) {
550                 // alert("QUI2");
551                 this.st.st_loc++;
552                 this.st = null;
553             }
554             if (this.srcend != null) {
555                 this.img.src = this.srcend;
556             }
557         }
558     }
559 }
560
561
562 function notify_ex(st, text, tout, butt, w, h, is_opa, block_time)
563 {
564     var clo, box;
565     var t = this;
566     
567     this.st = st;
568
569     this.ancestor = document.body;
570     
571     this.st.st_loc_new++;
572
573     clo = document.createElement("input");
574     clo.type = "submit";
575     clo.className = "button";
576     clo.style.bottom = "4px";
577     clo.obj = this;
578     if (block_time > 0) {
579         clo.value = "leggere, prego.";
580         this.butt = butt;
581     }
582     else {
583         clo.value = butt;
584         clo.onclick = this.input_hide;
585     }
586
587     clodiv = document.createElement("div");
588     clodiv.className = "notify_clo";
589     this.clo = clo;
590     this.clodiv = clodiv;
591
592     clodiv.appendChild(clo);
593
594     cont = document.createElement("div");
595
596     cont.style.borderBottomStyle = "solid";
597     cont.style.borderBottomWidth = "1px";
598     cont.style.borderBottomColor = "gray";
599     cont.style.height = (h - 30)+"px";
600     cont.style.overflow = "auto";
601     cont.innerHTML = text;
602
603     box =  document.createElement("div");
604     if (is_opa)
605         box.className = "notify_opaque";
606     else
607         box.className = "notify";
608
609     box.style.zIndex = 200;
610     box.style.width  = w+"px";
611     box.style.marginLeft  = -parseInt(w/2)+"px";
612     box.style.height = h+"px";
613     box.style.top = parseInt((document.body.clientHeight - h) / 2) + document.body.scrollTop;
614     box.appendChild(cont);
615     box.appendChild(clodiv);
616     box.style.visibility = "visible";
617
618     this.notitag = box;
619     
620     this.ancestor.appendChild(box);
621     
622     this.toutid = setTimeout(function(obj){ obj.unblock(); }, tout, this);
623
624     if (block_time != 0) {
625         this.tblkid = setTimeout(function(obj){ obj.clo.value = obj.butt; obj.clo.onclick = obj.input_hide; formsub_hilite(obj.clo); obj.clo.focus(); }, block_time, this);
626     }
627     else {
628         formsub_hilite(clo);
629         clo.focus();
630     }
631
632 }
633
634
635 notify_ex.prototype = {
636     ancestor: null,
637     st: null,
638     notitag: null,
639     toutid: null,
640     clo: null,
641     clodiv: null, 
642     butt: null,
643     tblkid: null, 
644
645     unblock: function()
646     {
647         if (this.st.st_loc < this.st.st_loc_new) {
648             this.st.st_loc++;
649         }
650     },
651     
652     hide: function()
653     {
654         clearTimeout(this.toutid);
655         this.ancestor.removeChild(this.notitag);
656         this.unblock();
657     },
658
659     input_hide: function()
660     {
661         clearTimeout(this.obj.toutid);
662         this.obj.ancestor.removeChild(this.obj.notitag);
663         this.obj.unblock();
664     }
665 }
666
667
668 notify.prototype = notify_ex.prototype;                // Define sub-class
669 notify.prototype.constructor = notify;
670 notify.baseConstructor = notify_ex;
671 notify.superClass = notify_ex.prototype;
672
673 function notify(st, text, tout, butt, w, h)
674 {
675     notify_ex.call(this, st, text, tout, butt, w, h, false, 0);
676 }
677         
678
679 function $(id) { 
680     return document.getElementById(id); 
681 }
682
683 function globst() {
684     this.st = -1;
685     this.st_loc = -1;
686     this.st_loc_new = -1;
687     this.comms  = new Array;
688 }
689
690
691
692 function remark_step()
693 {
694     var ct = $("remark").l_remct;
695     
696     if (ct != 0) {
697         ct++;
698         if (ct > 2)
699             ct = 1;
700         $("remark").className = "remark"+ct;
701         $("remark").l_remct = ct;
702         setTimeout(remark_step,500);
703     }
704     else
705         $("remark").className = "remark0";
706     
707     return;
708 }
709
710 function remark_on()
711 {
712     if ($("remark").l_remct == 0) {
713         $("remark").l_remct = 1;
714         setTimeout(remark_step,500);
715     }
716 }
717
718 function remark_off()
719 {
720     $("remark").l_remct = 0;
721     $("remark").className = "remark0";
722 }
723
724
725 function italizer(ga)
726 {
727     var pre, pos;
728     if (ga[0] & 2) 
729         return "<i>"+ga[1]+"</i>";
730     else
731         return ga[1];
732 }
733
734
735 function exitlock_show(num, islock)
736 {
737     g_exitlock = num;
738
739     num = (num < 3 ? num : 3);
740     $("exitlock").src = "img/exitlock"+num+(islock ? "n" : "y")+".png";
741     // alert("EXITLOCK: "+$("exitlock").src);
742     $("exitlock").style.visibility = "visible";
743 }
744
745 var fin = 0;
746
747 //    exitlock_show(0, true);
748
749
750 var chatt_lines = new Array();
751 var chatt_lines_n = 0;
752
753 var CHATT_MAXLINES = 40;
754
755 /* PRO CHATT */
756 function chatt_sub(dt,data,str)
757 {
758     var must_scroll = false;
759     var name;
760     var flags;
761     var isauth;
762
763     flags = data[0];
764     if (flags & 0x02)
765         name = "<i>"+data[1]+"</i>";
766     else
767         name = data[1];
768     // alert ($("txt").scrollTop + parseInt(getStyle($("txt"),"height", "height")) -  $("txt").scrollHeight);
769
770   if ($("txt").scrollTop + parseInt(getStyle($("txt"),"height", "height")) -  $("txt").scrollHeight >= 0)
771       must_scroll = true;
772
773   // alert("ARRIVA NAME: "+ name + "  STR:"+str);
774   if (chatt_lines_n == CHATT_MAXLINES) {
775     $("txt").innerHTML = "";
776     for (i = 0 ; i < (CHATT_MAXLINES - 1) ; i++) {
777       chatt_lines[i] = chatt_lines[i+1];
778       $("txt").innerHTML += chatt_lines[i];
779     }
780     chatt_lines[i] = dt+"<b>"+name+"</b> "+str+ "<br>";
781     $("txt").innerHTML += chatt_lines[i];
782   }
783   else {
784     chatt_lines[chatt_lines_n] = dt+"<b>"+name+"</b> "+str+ "<br>";
785     $("txt").innerHTML += chatt_lines[chatt_lines_n];
786     chatt_lines_n++;
787   }
788   // $("txt").innerHTML;
789
790
791   if (must_scroll) {
792       $("txt").scrollTop = 10000000;
793   }
794   // alert("scTOP "+$("txt").scrollTop+"  scHEIGHT: "+$("txt").scrollHeight+" HEIGHT: "+getStyle($("txt"),"height", "height") );
795 }
796
797 /*
798  *  GESTIONE DEI COOKIES
799  */
800 function createCookie(name,value,hours,path) {
801         if (hours) {
802                 var date = new Date();
803                 date.setTime(date.getTime()+(hours*60*60*1000));
804                 var expires = "; expires="+date.toGMTString();
805         }
806         else var expires = "";
807         document.cookie = name+"="+value+expires+"; path="+path;
808 }
809
810 function readCookie(name) {
811         var nameEQ = name + "=";
812         var ca = document.cookie.split(';');
813         for(var i=0;i < ca.length;i++) {
814                 var c = ca[i];
815                 while (c.charAt(0)==' ') c = c.substring(1,c.length);
816                 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
817         }
818         return null;
819 }
820
821 function eraseCookie(name) {
822         createCookie(name,"",-1);
823 }
824
825 var onunload_times = 0;
826
827
828 function onbeforeunload_cb () {
829     return("");
830 }
831
832 function onunload_cb_old () {
833     var u = 0;
834     
835     //    if (nonunload == true)
836     //     return true;
837     
838     if (onunload_times == 0) {
839         // MLANG "    Vuoi veramente abbandonare la briscola ?\n(clicca annulla o cancel se vuoi ricaricare la briscola)"
840         var res = window.confirm(mlang_commons['brileav'][g_lang]);
841         if (res == true) {
842             the_end = true; 
843             act_shutdown();
844             // while (1) 
845             //  u++;
846         }
847         else {
848             try {
849                 document.location.href = self.location; //  = self.location;
850                 // alert ("passiamo di qui"+self.location);
851                 return (false);
852             } catch (e) {
853                 // MLANG "Ripristino della briscola fallito, per non perdere la sessione ricaricare la pagina manualmente."
854                 alert(mlang_commons['brireco'][g_lang]);
855             }
856         }
857         onunload_times++;
858     }
859     
860     return(false);
861 }
862
863 function onunload_cb () {
864     
865     the_end = true; 
866
867     act_shutdown();
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         // MLANG Mi siedo.
887         $("table_act"+i).innerHTML = "<input type=\"button\" class=\"button\" name=\"xhenter"+i+"\"  value=\""+mlang_commons['btn_sit'][g_lang]+"\" onclick=\"act_sitdown(1);\">";
888         }
889
890     stand = "<table class=\"table_standup\"><tbody><tr>";
891     for (i = 0 ; i < inpe ; i++) {
892         stand += "<td>"+nome+"</td>";
893         if ((i+1) % 4 == 0) {
894             stand += "</tr><tr>";
895         }
896     }
897     stand += "</tr>";
898     $("standup").innerHTML = stand;
899
900     // VERIFY: what is this button ?
901     // MLANG Esco.
902     $("esco").innerHTML = "<input class=\"button\" name=\"logout\" type=\"button\" value=\""+mlang_commons['btn_exit'][g_lang]+"\" onclick=\"act_logout();\" type=\"button\">";
903 }
904
905 function  unescapeHTML(cont) {
906     var div = document.createElement('div');
907     var memo = "";
908     var i;
909
910     div.innerHTML = cont;
911     if (div.childNodes[0]) {
912         if (div.childNodes.length > 1) {
913             if (div.childNodes.toArray)
914                 alert("si puo");
915             else {
916                 var length = div.childNodes.length, results = new Array(length);
917             while (length--)
918                 results[length] = div.childNodes[length];
919                 
920             for (i=0 ; i<results.length ; i++)
921                 memo = memo + results[i].nodeValue;
922             }
923
924             return (memo);
925         }
926         else {
927             return (div.childNodes[0].nodeValue);
928         }
929     }
930     else {
931         return ('');
932     }
933 }
934
935 function playsound(tag, sound) {
936    // g_withflash is a global var
937    if (g_withflash) {
938       $(tag).innerHTML = '<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" '+
939 'codebase="http://active.macromedia.com/flash2/cabs/swflash.cab#version=4,0,0,0" id="mysound" WIDTH=1 HEIGHT=1>' +
940 '<PARAM NAME="movie" VALUE="../playsound.swf"><PARAM NAME="PLAY" VALUE="true"><PARAM NAME="LOOP" VALUE="false">' +
941 '<PARAM NAME=FlashVars VALUE="streamUrl='+sound+'">' +
942 '<EMBED swliveconnect="true" name="mysound" src="../playsound.swf" FlashVars="streamUrl='+sound+'" PLAY="true" LOOP="false" '+
943 ' WIDTH=1 HEIGHT=1 TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></OBJECT>';
944    }
945 }
946
947 function topbanner_init()
948 {
949     setInterval(topbanner_cb, 666);
950 ;
951 }
952
953 function topbanner_cb()
954 {
955     var a, b;
956
957     a = $('topbanner').style.backgroundColor;
958     b = $('topbanner').style.borderLeftColor;
959
960     $('topbanner').style.backgroundColor = b;
961     $('topbanner').style.borderColor = a+" "+a+" "+a+" "+a;
962
963     // console.log("A: "+a+"  B: "+b);
964 }
965
966 function sidebanner_init()
967 {
968     setInterval(sidebanner_cb, 666);
969 }
970
971 function sidebanner2_init()
972 {
973     setInterval(sidebanner2_cb, 666);
974 }
975
976 function sidebanner_cb()
977 {
978     var a, b;
979
980     a = $('sidebanner').style.backgroundColor;
981     b = $('sidebanner').style.borderLeftColor;
982
983     $('sidebanner').style.backgroundColor = b;
984     $('sidebanner').style.borderColor = a+" "+a+" "+a+" "+a;
985
986     // console.log("A: "+a+"  B: "+b);
987 }
988
989 function sidebanner2_cb()
990 {
991     var a, b;
992
993     a = $('sidebanner2').style.backgroundColor;
994     b = $('sidebanner2').style.borderLeftColor;
995
996     $('sidebanner2').style.backgroundColor = b;
997     $('sidebanner2').style.borderColor = a+" "+a+" "+a+" "+a;
998
999     // console.log("A: "+a+"  B: "+b);
1000 }
1001
1002
1003 function langtolng(lang)
1004 {
1005     if (lang == "en")
1006         return ("-en");
1007     else
1008         return ("");
1009 }
1010
1011 function formtext_hilite(obj)
1012 {
1013     obj.className = 'input_text';
1014     addEvent(obj, "focus", function () { this.className = 'input_text_hi'; });
1015     addEvent(obj, "blur",  function () { this.className = 'input_text'; });
1016 }
1017
1018 function formsub_hilite(obj)
1019 {
1020     obj.className = 'input_sub';
1021     addEvent(obj, "focus", function () { this.className = 'input_sub_hi'; });
1022     addEvent(obj, "blur",  function () { this.className = 'input_sub'; });
1023 }
1024