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