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