just all or authenticated users prefs options
[brisk.git] / web / commons.js
1 /*
2  *  brisk - commons.js
3  *
4  *  Copyright (C) 2006-2015 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 = 900;
26 var cookiepath = "/brisk/";
27
28 var mlang_commons = { 'imgload_a' : { 'it' : 'Immagini 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' : '(autenticati)',
45                                                'en' : '(authenticated)' } },
46                       'tos_refu'  : { 'it' : 'Rifiutando di sottoscrivere i nuovi termini del servizio non ti sarà più possibile accedere come utente registrato al sito, sei proprio sicuro di voler rifiutare le nuove condizioni d\'uso ?',
47                                       'en' : 'EN Rifiutando di sottoscrivere i nuovi termini del servizio non ti sarà più possibile accedere come utente registrato al sito, sei proprio sicuro di voler rifiutare le nuove condizioni d\'uso ?'
48                                     }
49                     };
50
51 function $(id) { return document.getElementById(id); }
52
53 function dec2hex(d, padding)
54 {
55     var hex = Number(d).toString(16);
56     padding = typeof (padding) === "undefined" || padding === null ? padding = 2 : padding;
57
58     while (hex.length < padding) {
59         hex = "0" + hex;
60     }
61
62     return hex;
63 }
64
65 function getStyle(x,IEstyleProp, MozStyleProp)
66 {
67     if (x.currentStyle) {
68         var y = x.currentStyle[IEstyleProp];
69     } else if (window.getComputedStyle) {
70         var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(MozStyleProp);
71     }
72     return y;
73 }
74
75 /* replacement of setInterval on IE */
76 (function(){
77     /*if not IE, do nothing*/
78     if(!document.uniqueID){return;};
79
80     /*Copy the default setInterval behavior*/
81     var nativeSetInterval = window.setInterval;
82     window.setInterval = function(fn,ms) {              
83         var param = [];
84         if(arguments.length <= 2)       {
85             return nativeSetInterval(fn,ms);
86         }
87         else {
88             for(var i=2;i<arguments.length;i+=1) {
89                 param[i-2] =  arguments[i];
90             }   
91         }
92
93         if(typeof(fn)=='function') {
94
95             return (function (fn,ms,param) {
96                 var fo = function () {                                                          
97                     fn.apply(window,param);
98                 };                      
99                 return nativeSetInterval(fo,ms);
100             })(fn,ms,param);
101         }
102         else if(typeof(fn)=='string')
103         {
104             return  nativeSetInterval(fn,ms);
105         }
106         else
107         {
108             throw Error('setInterval Error\nInvalid function type');
109         };
110     };
111
112     /*Copy the default setTimeout behavior*/
113     var nativeSetTimeout = window.setTimeout;
114     window.setTimeout = function(fn,ms) {               
115         var param = [];
116         if(arguments.length <= 2)       {
117             return nativeSetTimeout(fn,ms);
118         }
119         else {
120             for(var i=2;i<arguments.length;i+=1) {
121                 param[i-2] =  arguments[i];
122             }   
123         }
124
125         if(typeof(fn)=='function') {
126
127             return (function (fn,ms,param) {
128                 var fo = function () {                                                          
129                     fn.apply(window,param);
130                 };                      
131                 return nativeSetTimeout(fo,ms);
132             })(fn,ms,param);
133         }
134         else if(typeof(fn)=='string')
135         {
136             return  nativeSetTimeout(fn,ms);
137         }
138         else
139         {
140             throw Error('setTimeout Error\nInvalid function type');
141         };
142     };
143
144 })()
145
146 function addEvent(obj, type, fn)
147 {
148     if (obj.addEventListener) {
149         obj.addEventListener( type, fn, false);
150     }
151     else if (obj.attachEvent) {
152         obj["e"+type+fn] = fn;
153         obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
154         obj.attachEvent( "on"+type, obj[type+fn] );
155     }
156     else
157         throw new Error("Event registration not supported");
158 }
159
160 function removeEvent(obj,type,fn)
161 {
162     if (obj.removeEventListener) {
163         obj.removeEventListener( type, fn, false );
164     }
165     else if (obj.detachEvent) {
166         obj.detachEvent( "on"+type, obj[type+fn] );
167         obj[type+fn] = null;
168         obj["e"+type+fn] = null;
169     }
170 }
171
172     // var card_pos = RANGE 0 <= x < cards_ea_n
173
174 function show_bigpict(obj, act, x, y)
175 {
176    var big, sfx;
177
178    if (arguments.length > 4)
179        sfx = arguments[4];
180    else
181        sfx = '';
182
183    big = $(obj.id+"_big"+sfx);
184    if (act == "over") {
185        big.style.left = obj.offsetLeft + x+"px";
186        big.style.top  = obj.offsetTop  + y+"px";
187        big.style.visibility = "visible";
188        }
189    else {
190        big.style.visibility = "hidden";
191        }
192 }
193
194 function rnd_int(min, max) {
195   return Math.floor(Math.random() * (max - min + 1) + min);
196 }
197
198 function error_images()
199 {
200     // alert("GHESEMU!");
201     setTimeout(preload_images, 2000, g_preload_img_arr, g_imgct-1);
202 }
203
204 function abort_images()
205 {
206     // alert("ABORTAIMAGES");
207     setTimeout(preload_images, 2000, g_preload_img_arr, g_imgct-1);
208 }
209
210 function unload_images()
211 {
212     // alert("ABORTAIMAGES");
213     setTimeout(preload_images, 2000, g_preload_img_arr, g_imgct-1);
214 }
215
216 function reset_images()
217 {
218     // alert("ABORTAIMAGES");
219     setTimeout(preload_images, 2000, g_preload_img_arr, g_imgct-1);
220 }
221
222 function update_images()
223 {
224     // MLANG "Immagine caricate" + g_preload_imgsz_arr[g_imgct] + "%."
225     $("imgct").innerHTML = mlang_commons['imgload_a'][g_lang]+g_preload_imgsz_arr[g_imgct]+"%.";
226     if (g_imgct+1 < g_preload_img_arr.length) {
227         g_imgct++;
228         setTimeout(preload_images, 100, g_preload_img_arr, g_imgct-1);
229     }
230     // $("imgct").innerHTML += "U";
231 }
232
233 function preload_images(arr,idx)
234 {
235     var im = new Image;
236
237     // $("imgct").innerHTML = "Stiamo caricando "+arr[idx]+"%.<br>";
238     im.onload =   update_images;
239     im.onerror =  error_images;
240     im.onabort =  abort_images;
241     im.onunload = unload_images;
242     im.onreset =  reset_images;
243     im.src =      arr[idx];
244     // $("imgct").innerHTML += "P";
245 }
246
247 function safestatus(a)
248 {
249     try{
250         return (a.status);
251     } catch(b)
252         { return (-1); }
253 }
254
255 function createXMLHttpRequest() {
256     if (typeof(ActiveXObject) != 'undefined') { // Konqueror complain as unknown object
257         try { return new ActiveXObject("Msxml2.XMLHTTP");    } catch(e) {}
258         try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {}
259     }
260     try { return new XMLHttpRequest();                   } catch(e) {}
261     alert("XMLHttpRequest not supported");
262     return null;
263 }
264
265 function send_mesg(mesg, content)
266 {
267     var is_conn = (sess == "not_connected" ? false : true);
268
269     if (is_conn && xstm && xstm.transp_type.startsWith('websocket')) {
270         if (typeof(content) != 'undefined') {
271             mesg = mesg + content;
272         }
273         var target = window.location.href.substring(0,
274             window.location.href.lastIndexOf('/') + 1) + 'index_wr.php';
275         var ws_msg = JSON.stringify({
276             target: target,
277             mesg: mesg,
278             stp:gst.st,
279             sess:sess,
280             table_idx: readCookie("table_idx"),
281             table_token: readCookie("table_token"),
282             lang: readCookie("lang")
283             });
284         // console.log(ws_msg);
285         xstm.send(ws_msg);
286         /*
287         if (xstm.transp.ws.readyState == 1) {
288             xstm.transp.ws.send(ws_msg);
289         }
290         else {
291             xstm.transp.out_queue.push(ws_msg);
292         }
293         */
294
295     }
296     else {
297     var xhr_wr = createXMLHttpRequest();
298
299     if (typeof(content) != 'undefined') {
300         mesg = mesg + encodeURIComponent(content);
301     }
302     // alert("xhr_wr: "+xhr_wr+"  is_conn: "+is_conn);
303     xhr_wr.open('GET', 'index_wr.php?&'+(is_conn ? 'sess='+sess : '')+'&stp='+gst.st+'&mesg='+mesg, (is_conn ? true : false));
304     xhr_wr.setRequestHeader("If-Modified-Since", new Date().toUTCString());
305     xhr_wr.onreadystatechange = function() { return; };
306     if (typeof(g_debug) == 'number' && g_debug > 0
307         && typeof(console) == 'object' && typeof(console.log) == 'function') {
308             var ldate = new Date();
309             console.log(ldate.getTime()+':MESG:'+mesg);
310     }
311     xhr_wr.send(null);
312
313     if (!is_conn) {
314         if (xhr_wr.responseText != null) {
315             eval(xhr_wr.responseText);
316         }
317     }
318     }
319 }
320
321 /*
322   sync request to server
323   server_request([arg0=arg1[, arg2=arg3[, ...]]])
324   if var name == '__POST__' than all other vars will be managed as POST content
325                                  and the call will be a POST
326  */
327 function server_request()
328 {
329     var xhr_wr = createXMLHttpRequest();
330     var i, collect = "", post_collect = null, is_post = false;
331
332     if (arguments.length > 0) {
333         for (i = 0 ; i < arguments.length ; i+= 2) {
334             if (arguments[i] == "__POST__") {
335                 is_post = true;
336                 post_collect = "";
337                 i -= 1;
338                 continue;
339             }
340             if (is_post)
341                 post_collect += (post_collect == "" ? "" : "&") + arguments[i] + "=" + encodeURIComponent(arguments[i+1]);
342             else
343                 collect += (i == 0 ? "" : "&") + arguments[i] + "=" + encodeURIComponent(arguments[i+1]);
344         }
345     }
346     // alert("Args: "+arguments.length);
347
348     var is_conn = (sess == "not_connected" ? false : true);
349
350     // console.log("server_request:preresp: "+xhr_wr.responseText);
351
352     if (is_post) {
353         xhr_wr.open('POST', 'index_wr.php?'+(is_conn ? 'sess='+sess+'&' : '')+collect, false);
354         xhr_wr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
355     }
356     else {
357         xhr_wr.open('GET', 'index_wr.php?'+(is_conn ? 'sess='+sess+'&' : '')+collect, false);
358     }
359     xhr_wr.onreadystatechange = function() { return; };
360     xhr_wr.send(post_collect);
361
362     if (xhr_wr.responseText != null) {
363         // console.log("server_request:resp: "+xhr_wr.responseText);
364         return (xhr_wr.responseText);
365     }
366     else
367         return (null);
368 }
369
370 /* Stat: CHAT and TABLE */
371
372 function chatt_checksend(obj,e)
373 {
374     var keynum;
375     var keychar;
376     var numcheck;
377
378     if(window.event) { // IE
379         keynum = e.keyCode;
380     }
381     else if(e.which) { // Netscape/Firefox/Opera
382         keynum = e.which;
383     }
384     // alert("OBJ: "+obj);
385     if (keynum == 13 && obj.value != "") { // Enter
386         act_chatt(obj.value);
387         obj.value = "";
388     }
389 }
390 function act_chatt(value)
391 {
392     if (value.substring(0, 6) == "/info ") {
393         info_show(value.substring(6));
394     }
395     else {
396         send_mesg("chatt|", value);
397     }
398     /*
399     obj.disabled = true;
400     obj.value = "";
401     obj.disabled = false;
402     obj.focus();
403     */
404     return false;
405 }
406
407 /* Stat: ROOM */
408 function act_ping()
409 {
410     send_mesg("ping");
411 }
412
413 function act_sitdown(table)
414 {
415     send_mesg("sitdown|"+table);
416 }
417
418 function act_wakeup()
419 {
420     send_mesg("wakeup");
421 }
422
423 function act_splash()
424 {
425     send_mesg("splash");
426 }
427
428 function act_help()
429 {
430     send_mesg("help");
431 }
432
433 function act_passwdhowto()
434 {
435     send_mesg("passwdhowto");
436 }
437
438 function act_mesgtoadm()
439 {
440     send_mesg("mesgtoadm");
441 }
442
443 function act_tav()
444 {
445     act_chatt('/tav '+$('txt_in').value);
446     $('txt_in').value = '';
447 }
448
449 function act_about()
450 {
451     send_mesg("about");
452 }
453
454 function act_placing()
455 {
456     send_mesg("placing");
457 }
458
459 function act_roadmap()
460 {
461     send_mesg("roadmap");
462 }
463
464 function act_lascio()
465 {
466     send_mesg("lascio");
467 }
468
469 function safelascio()
470 {
471     var res;
472     // MLANG "Sei sicuro di volere lasciare questa mano?"
473     res = window.confirm(mlang_commons['gamleav'][g_lang]);
474     if (res)
475         act_lascio();
476 }
477
478 function act_logout(exitlock)
479 {
480     send_mesg("logout|"+exitlock);
481 }
482
483 function act_reloadroom()
484 {
485     window.onunload = null;
486     window.onbeforeunload = null;
487     document.location.assign("index.php");
488 }
489
490 function act_shutdown()
491 {
492     var c = 0;
493
494     send_mesg("shutdown");
495     // while (xhr_wr.readyState != 4)
496     //  c++;
497 }
498
499 function postact_logout()
500 {
501     // alert("postact_logout");
502     try {
503         xstm.abort();
504     } catch (e) {}
505
506     // eraseCookie("sess");
507     document.location.assign("index.php");
508 }
509
510 /*
511   type - 'hard' or 'soft'
512   code - if soft: accept (0), refuse (1), download (2), later (3)
513          if hard: accept (0), refuse (1), download (2)
514  */
515 function act_tosmgr(type, code, tos_curr, tos_vers)
516 {
517     if (type != "soft" && type != "hard") {
518         return false;
519     }
520     switch (code) {
521     case 0:
522     case 1:
523         send_mesg("tosmgr|"+type+"|"+code+"|"+tos_curr+"|"+tos_vers);
524         break;
525     case 2:
526         break;
527     default:
528         break;
529     }
530
531     return true;
532 }
533
534 function tos_confirm(val, url)
535 {
536     var dlm;
537
538     switch (val) {
539     case 1:
540         return (window.confirm(mlang_commons['tos_refu'][g_lang]));
541         break;
542     case 2:
543         dlm = new download_mgr(url);
544         return false;
545         break;
546     default:
547         return true;
548         break;
549     }
550 }
551
552 /*
553   function slowimg(img,x1,y1,deltat,free,action,srcend)
554   img    - image to move
555   x1,y1  - destination coords
556   deltat - time for each frame (in msec)
557   free   - when the release the local block for other operations (range: 0 - 1)
558   action - function to run when the image is moved
559   srcend - image to switch when the image is moved
560 */
561
562 function sleep(st, delay)
563 {
564     // alert("LOC_NEW PRE: "+st.st_loc_new);
565
566     st.st_loc_new++;
567
568     setTimeout(function(obj){ if (obj.st_loc_new > obj.st_loc) { obj.st_loc++; }},
569                delay, st);
570 }
571
572 function slowimg(img,x1,y1,deltat,free,action,srcend) {
573     this.img = img;
574
575     // this.x0  = parseInt(document.defaultView.getComputedStyle(this.img, "").getPropertyValue("left"));
576     this.x0 = parseInt(getStyle(this.img,"left", "left"));
577 // alert("img.x0 = "+this.x0);
578     // this.y0  = parseInt(document.defaultView.getComputedStyle(this.img, "").getPropertyValue("top"));
579     this.y0  = parseInt(getStyle(this.img,"top", "top"));
580     this.x1  = x1;
581     this.y1  = y1;
582     this.deltat = deltat;
583     this.free = free;
584     this.action = action;
585     this.srcend = srcend;
586 }
587
588 slowimg.prototype = {
589     img: null,
590     st: null,
591     x0: 0,
592     y0: 0,
593     x1: 0,
594     y1: 0,
595     dx: 0,
596     dy: 0,
597     free: 0,
598     step_n:    0,
599     step_cur:  0,
600     step_free: 0,
601     time:      0,
602     deltat:   40,
603     tout: 0,
604     action: null,
605     srcend: null,
606
607     setstart: function(x0,y0)
608     {
609         this.x0 = x0;
610         this.y0 = y0;
611     },
612
613     setaction: function(act)
614     {
615         this.action = act;
616     },
617
618
619     settime: function(time)
620     {
621         this.time = (time < this.deltat ? this.deltat : time);
622         this.step_n = parseInt(this.time / this.deltat);
623         this.dx = (this.x1 - this.x0) / this.step_n;
624         this.dy = (this.y1 - this.y0) / this.step_n;
625         if (this.step_n * this.deltat == this.time) {
626             this.step_n--;
627         }
628         if (this.free < 1) {
629             this.step_free = parseInt(this.step_n * this.free);
630         }
631     },
632
633     start: function(st)
634     {
635         // $("logz").innerHTML += "               xxxxxxxxxxxxxxxxxxxxxSTART<br>";
636         this.st = st;
637         this.st.st_loc_new++;
638
639         this.img.style.visibility = "visible";
640         setTimeout(function(obj){ obj.animate(); }, this.deltat, this);
641     },
642
643     animate: function()
644     {
645         // $("log").innerHTML = "Val " + this.step_cur + " N: " + this.step_n + "<br>";
646         if (this.step_cur == 0) {
647             var date = new Date();
648             // $("logz").innerHTML = "Timestart: " + date + "<br>";
649         }
650         if (this.step_cur <= this.step_n) {
651             this.img.style.left = this.x0 + this.dx * this.step_cur;
652             this.img.style.top  = this.y0 + this.dy * this.step_cur;
653             this.step_cur++;
654             setTimeout(function(obj){ obj.animate(); }, this.deltat, this);
655             if (this.step_cur == this.step_free && this.st != null) {
656                 if (this.st.st_loc < this.st.st_loc_new) {
657                     // alert("QUI1  " + this.step_cur + "  ZZ  "+  this.step_free);
658                     this.st.st_loc++;
659                     this.st = null;
660                 }
661             }
662         }
663         else {
664             this.img.style.left = this.x1;
665             this.img.style.top  = this.y1;
666             // $("logz").innerHTML += "xxxxxxxxxxxxxxxCLEAR<br>";
667             var date = new Date();
668             // $("logz").innerHTML += "Timestop: " + date + "<br>";
669
670             if (this.action != null) {
671                 eval(this.action);
672             }
673
674             if (this.st != null && this.st.st_loc < this.st.st_loc_new) {
675                 // alert("QUI2");
676                 this.st.st_loc++;
677                 this.st = null;
678             }
679             if (this.srcend != null) {
680                 this.img.src = this.srcend;
681             }
682         }
683     }
684 }
685
686 function div_show(div)
687 {
688     div.style.top = parseInt((document.body.clientHeight - parseInt(getStyle(div,"height", "height"))) / 2) + document.body.scrollTop;
689     div.style.visibility = "visible";
690 }
691
692 /*
693   st
694   text
695   tout: if < 0 => infinite
696   butt: [ strings ]
697   w:
698   h:
699   is_opa:
700   block_time:
701   */
702
703 function notify_document(st, text, tout, butt, confirm_func, confirm_func_args, w, h, is_opa, block_time)
704 {
705     var i, clo, clodiv_ctx, clodiv_wai, box;
706
707     this.st = st;
708
709     this.ancestor = document.body;
710     this.confirm_func = confirm_func;
711     this.confirm_func_args = confirm_func_args;
712     this.st.st_loc_new++;
713
714     clodiv_ctx = document.createElement("div");
715     clodiv_ctx.className = "notify_clo";
716
717     for (i = 0 ; i < butt.length ; i++) {
718         this.input_add(butt[i], i, this.hide, clodiv_ctx);
719     }
720
721     if (block_time > 0) {
722         clodiv_wai = document.createElement("div");
723         clodiv_wai.className = "notify_clo";
724
725         this.input_add("leggere, prego.", 0, null, clodiv_wai);
726         this.clodiv = clodiv_wai;
727         this.clodiv_pkg = clodiv_ctx;
728         clodiv_ctx.style.display = 'none';
729     }
730     else {
731         this.clodiv = clodiv_ctx;
732     }
733
734     cont = document.createElement("div");
735
736     cont.style.borderBottomStyle = "solid";
737     cont.style.borderBottomWidth = "1px";
738     cont.style.borderBottomColor = "gray";
739     cont.style.height = (h - 50)+"px";
740     cont.style.overflow = "auto";
741     cont.style.textAlign = "left";
742     cont.style.padding = "8px";
743     cont.style.fontFamily = "monospace";
744     cont.innerHTML = text;
745
746     box =  document.createElement("div");
747     if (is_opa)
748         box.className = "notify_opaque";
749     else
750         box.className = "notify";
751
752     box.style.zIndex = 200;
753     box.style.width  = w+"px";
754     box.style.marginLeft  = -parseInt(w/2)+"px";
755     box.style.height = h+"px";
756     box.style.top = parseInt((document.body.clientHeight - h) / 2) + document.body.scrollTop;
757     box.appendChild(cont);
758     box.appendChild(this.clodiv);
759     box.style.visibility = "visible";
760
761     this.notitag = box;
762
763     this.ancestor.appendChild(box);
764
765     if (tout > 0) {
766         this.toutid = setTimeout(function(obj){ obj.unblock(); }, tout, this);
767     }
768
769     if (block_time != 0) {
770         this.tblkid = setTimeout(function(obj){ obj.notitag.removeChild(obj.clodiv); obj.clodiv = obj.clodiv_pkg; obj.clodiv.style.display = '';  obj.notitag.appendChild(obj.clodiv); }, block_time, this);
771     }
772 }
773
774 notify_document.prototype = {
775     ancestor: null,
776     st: null,
777     notitag: null,
778     toutid: null,
779     clo: null,
780
781     clodiv: null,
782     clodiv_pkg: null,
783
784     butt: null,
785     tblkid: null,
786
787     confirm_func: null,
788     confirm_func_args: [],
789
790     ret: -1,
791
792     /*
793       s:          button string
794       idx:        button index
795       onclick_cb: name of the onclick callback (with signature f(idx) ) or null
796       anc:        parent dom object
797
798       return new button dom object
799       */
800     input_add: function(s, idx, onclick_cb, anc)
801     {
802         var clo;
803
804         clo = document.createElement("input");
805         clo.type    = "submit";
806         clo.className = "button";
807         clo.style.bottom = "4px";
808         clo.style.margin = "2px";
809         clo.obj     = this;
810         clo.obj_idx = idx;
811         clo.value   = s;
812         if (onclick_cb)
813             clo.onclick = function () { onclick_cb.call(this.obj, this.obj_idx); };
814
815         formsub_hilite(clo);
816         anc.appendChild(clo);
817
818         return (clo);
819     },
820
821     ret_get: function()
822     {
823         // alert("quiz: "+this.rett);
824         return this.ret;
825     },
826
827     unblock: function()
828     {
829         if (this.st.st_loc < this.st.st_loc_new) {
830             this.st.st_loc++;
831         }
832     },
833
834     hide: function(val)
835     {
836         if (this.confirm_func != null) {
837             var args;
838
839             args = [ val ].concat(this.confirm_func_args);
840
841             if (this.confirm_func.apply(null, args) == false) {
842                 return false;
843             }
844         }
845         this.ret = val;
846         clearTimeout(this.toutid);
847         this.ancestor.removeChild(this.notitag);
848         this.unblock();
849     }
850 }
851
852
853
854
855 function notify_ex(st, text, tout, butt, w, h, is_opa, block_time)
856 {
857     var clo, box;
858     var t = this;
859
860     this.st = st;
861
862     this.ancestor = document.body;
863
864     this.st.st_loc_new++;
865
866     clo = document.createElement("input");
867     clo.type = "submit";
868     clo.className = "button";
869     clo.style.bottom = "4px";
870     clo.obj = this;
871     if (block_time > 0) {
872         clo.value = "leggere, prego.";
873         this.butt = butt;
874     }
875     else {
876         clo.value = butt;
877         clo.onclick = function () { this.obj.hide() };
878     }
879
880     clodiv = document.createElement("div");
881     clodiv.className = "notify_clo";
882     this.clo = clo;
883     this.clodiv = clodiv;
884
885     clodiv.appendChild(clo);
886
887     cont = document.createElement("div");
888
889     cont.style.borderBottomStyle = "solid";
890     cont.style.borderBottomWidth = "1px";
891     cont.style.borderBottomColor = "gray";
892     cont.style.height = (h - 30)+"px";
893     cont.style.overflow = "auto";
894     cont.innerHTML = text;
895
896     box =  document.createElement("div");
897     if (is_opa)
898         box.className = "notify_opaque";
899     else
900         box.className = "notify";
901
902     box.style.zIndex = 200;
903     box.style.width  = w+"px";
904     box.style.marginLeft  = -parseInt(w/2)+"px";
905     box.style.height = h+"px";
906     box.style.top = parseInt((document.body.clientHeight - h) / 2) + document.body.scrollTop;
907     box.appendChild(cont);
908     box.appendChild(clodiv);
909     box.style.visibility = "visible";
910
911     this.notitag = box;
912
913     this.ancestor.appendChild(box);
914
915     this.toutid = setTimeout(function(obj){ obj.unblock(); }, tout, this);
916
917     if (block_time != 0) {
918         this.tblkid = setTimeout(function(obj){ obj.clo.value = obj.butt; obj.clo.onclick = function () { this.obj.hide() }; formsub_hilite(obj.clo); obj.clo.focus(); }, block_time, this);
919     }
920     else {
921         formsub_hilite(clo);
922         clo.focus();
923     }
924
925 }
926
927
928 notify_ex.prototype = {
929     ancestor: null,
930     st: null,
931     notitag: null,
932     toutid: null,
933     clo: null,
934     clodiv: null,
935     butt: null,
936     tblkid: null,
937
938     unblock: function()
939     {
940         if (this.st.st_loc < this.st.st_loc_new) {
941             this.st.st_loc++;
942         }
943     },
944
945     hide: function()
946     {
947         clearTimeout(this.toutid);
948         this.ancestor.removeChild(this.notitag);
949         this.unblock();
950     }
951 }
952
953
954 notify.prototype = notify_ex.prototype;                // Define sub-class
955 notify.prototype.constructor = notify;
956 notify.baseConstructor = notify_ex;
957 notify.superClass = notify_ex.prototype;
958
959 function notify(st, text, tout, butt, w, h)
960 {
961     notify_ex.call(this, st, text, tout, butt, w, h, false, 0);
962 }
963
964 function globst() {
965     this.st = -1;
966     this.st_loc = -1;
967     this.st_loc_new = -1;
968     this.comms  = new Array;
969 }
970
971 globst.prototype = {
972     st: -1,
973     st_loc: -1,
974     st_loc_new: -1,
975     comms: null,
976     sleep_hdl: null,
977
978     sleep: function(delay) {
979         st.st_loc_new++;
980
981         if (!this.the_end) {
982             this.sleep_hdl = setTimeout(function(obj){ if (obj.st_loc_new > obj.st_loc) { obj.st_loc++; obj.sleep_hdl = null; }},
983                                         delay, this);
984         }
985     },
986
987     abort: function() {
988         if (this.sleep_hdl != null) {
989             clearTimeout(this.sleep_hdl);
990             this.sleep_hdl = null;
991         }
992     }
993 }
994
995 function remark_step()
996 {
997     var ct = $("remark").l_remct;
998
999     if (ct != 0) {
1000         ct++;
1001         if (ct > 2)
1002             ct = 1;
1003         $("remark").className = "remark"+ct;
1004         $("remark").l_remct = ct;
1005         setTimeout(remark_step,500);
1006     }
1007     else
1008         $("remark").className = "remark0";
1009
1010     return;
1011 }
1012
1013 function remark_on()
1014 {
1015     if ($("remark").l_remct == 0) {
1016         $("remark").l_remct = 1;
1017         setTimeout(remark_step,500);
1018     }
1019 }
1020
1021 function remark_off()
1022 {
1023     $("remark").l_remct = 0;
1024     $("remark").className = "remark0";
1025 }
1026
1027
1028 function italizer(ga)
1029 {
1030     var pre, pos;
1031     if (ga[0] & 2)
1032         return "<i>"+ga[1]+"</i>";
1033     else
1034         return ga[1];
1035 }
1036
1037
1038 function exitlock_show(num, islock)
1039 {
1040     g_exitlock = num;
1041
1042     num = (num < 3 ? num : 3);
1043     $("exitlock").src = "img/exitlock"+num+(islock ? "n" : "y")+".png";
1044     // alert("EXITLOCK: "+$("exitlock").src);
1045     $("exitlock").style.visibility = "visible";
1046 }
1047
1048 var fin = 0;
1049
1050 //    exitlock_show(0, true);
1051
1052
1053 var chatt_lines = new Array();
1054 var chatt_lines_n = 0;
1055
1056 var CHATT_MAXLINES = 40;
1057
1058 function user_decorator(user, is_real)
1059 {
1060     var name, i, sp = "", cl = "";
1061     var flags = user[0] & 0x03 | ((user[0] & 0x0c0000) >> 16);
1062
1063     // console.log(user[1]+" FLAGS: "+flags);
1064
1065     for (i = 0 ; i < 4 ; i++) {
1066         if (flags & (1 << i)) {
1067             cl += sp + "au" + i + (is_real ? "" : "_off");
1068             sp = " ";
1069         }
1070     }
1071
1072     if (flags != 0) {
1073         name = "<span class='" + cl + "'><span class='" +
1074         (is_real && (flags & 0xfffffe && ((flags & 0x01) == 0)) ? "id_usr" : "") +
1075         "'>" + user[1] + "</span></span>";
1076     }
1077     else {
1078         name = user[1];
1079     }
1080
1081     return (name);
1082 }
1083
1084 function user_dec_and_state(el)
1085 {
1086     var content = "";
1087     var val_el;
1088
1089     content = user_decorator(el, true);
1090     content += state_add(el[0],(typeof(el[2]) != 'undefined' ? el[2] : null));
1091
1092     return (content);
1093 }
1094
1095
1096 /* PRO CHATT */
1097 function chatt_sub(dt,data,str)
1098 {
1099     var must_scroll = false;
1100     var name;
1101     var flags;
1102     var isauth;
1103     var bolder = [ (data[0] | 1), data[1] ];
1104     name = user_decorator(bolder, false);
1105
1106     if ($("txt").scrollTop + parseInt(getStyle($("txt"),"height", "height")) -  $("txt").scrollHeight >= 0)
1107         must_scroll = true;
1108
1109     // alert("ARRIVA NAME: "+ name + "  STR:"+str);
1110     if (chatt_lines_n == CHATT_MAXLINES) {
1111         $("txt").innerHTML = "";
1112         for (i = 0 ; i < (CHATT_MAXLINES - 1) ; i++) {
1113             chatt_lines[i] = chatt_lines[i+1];
1114             $("txt").innerHTML += chatt_lines[i];
1115         }
1116         chatt_lines[i] = dt+name+": "+str+ "<br>";
1117         $("txt").innerHTML += chatt_lines[i];
1118     }
1119     else {
1120         chatt_lines[chatt_lines_n] = dt+name+": "+str+ "<br>";
1121         $("txt").innerHTML += chatt_lines[chatt_lines_n];
1122         chatt_lines_n++;
1123     }
1124     // $("txt").innerHTML;
1125
1126
1127     if (must_scroll) {
1128         $("txt").scrollTop = 10000000;
1129     }
1130     // alert("scTOP "+$("txt").scrollTop+"  scHEIGHT: "+$("txt").scrollHeight+" HEIGHT: "+getStyle($("txt"),"height", "height") );
1131 }
1132
1133 /*
1134  *  GESTIONE DEI COOKIES
1135  */
1136 function createCookie(name,value,hours,path) {
1137         if (hours) {
1138                 var date = new Date();
1139                 date.setTime(date.getTime()+(hours*60*60*1000));
1140                 var expires = "; expires="+date.toGMTString();
1141         }
1142         else var expires = "";
1143         document.cookie = name+"="+value+expires+"; path="+path;
1144 }
1145
1146 function readCookie(name) {
1147         var nameEQ = name + "=";
1148         var ca = document.cookie.split(';');
1149         for(var i=0;i < ca.length;i++) {
1150                 var c = ca[i];
1151                 while (c.charAt(0)==' ')
1152                     c = c.substring(1,c.length);
1153                 if (c.indexOf(nameEQ) == 0)
1154                     return c.substring(nameEQ.length,c.length);
1155         }
1156         return null;
1157 }
1158
1159 function eraseCookie(name) {
1160         createCookie(name,"",-1);
1161 }
1162
1163 function onbeforeunload_cb () {
1164     return("");
1165 }
1166
1167 function onunload_cb () {
1168
1169     if (typeof(xstm) != "undefined")
1170         xstm.the_end = true;
1171
1172     act_shutdown();
1173
1174     return(false);
1175 }
1176
1177 function room_checkspace(emme,tables,inpe)
1178 {
1179     nome = "<b>";
1180     for (i = 0 ; i < emme ; i++)
1181         nome += "m";
1182     nome += "</b>";
1183
1184     alta = "";
1185     for (i = 0 ; i < 5 ; i++)
1186         alta += nome+"<br>";
1187
1188     for (i = 0 ; i < tables ; i++) {
1189         $("table"+i).innerHTML = alta;
1190         // MLANG Mi siedo.
1191         $("table_act"+i).innerHTML = "<input type=\"button\" class=\"button\" name=\"xhenter"+i+"\"  value=\""+mlang_commons['btn_sit'][g_lang]+"\" onclick=\"act_sitdown(1);\">";
1192         }
1193
1194     stand = "<table class=\"table_standup\"><tbody><tr>";
1195     for (i = 0 ; i < inpe ; i++) {
1196         stand += "<td>"+nome+"</td>";
1197         if ((i+1) % 4 == 0) {
1198             stand += "</tr><tr>";
1199         }
1200     }
1201     stand += "</tr>";
1202     $("standup").innerHTML = stand;
1203
1204     // VERIFY: what is this button ?
1205     // MLANG Esco.
1206     $("esco").innerHTML = "<input class=\"button\" name=\"logout\" type=\"button\" value=\""+mlang_commons['btn_exit'][g_lang]+"\" onclick=\"act_logout();\" type=\"button\">";
1207 }
1208
1209 function  unescapeHTML(cont) {
1210     var div = document.createElement('div');
1211     var memo = "";
1212     var i;
1213
1214     div.innerHTML = cont;
1215     if (div.childNodes[0]) {
1216         if (div.childNodes.length > 1) {
1217             if (div.childNodes.toArray)
1218                 alert("si puo");
1219             else {
1220                 var length = div.childNodes.length, results = new Array(length);
1221             while (length--)
1222                 results[length] = div.childNodes[length];
1223
1224             for (i=0 ; i<results.length ; i++)
1225                 memo = memo + results[i].nodeValue;
1226             }
1227
1228             return (memo);
1229         }
1230         else {
1231             return (div.childNodes[0].nodeValue);
1232         }
1233     }
1234     else {
1235         return ('');
1236     }
1237 }
1238
1239 /*
1240    samples = [{'name': <name>, 'file': <file>}, ... ]
1241 */
1242 function jukebox(samples)
1243 {
1244     var source, a;
1245     this.enable = false;
1246     this.audio = {};
1247
1248     var pro_audio_el = document.createElement('audio');
1249     this.enable = !!(pro_audio_el.canPlayType && pro_audio_el.canPlayType('audio/mpeg;').replace(/no/, ''));
1250     if (this.enable) {
1251         for (i in samples) {
1252             sample = samples[i];
1253
1254             this.audio[sample['name']] = a = document.createElement('audio');
1255
1256             source = document.createElement('source');
1257             source.setAttribute('src', sample['file']);
1258             source.setAttribute('type', 'audio/mpeg');
1259             a.appendChild(source);
1260             a.load();
1261         }
1262     }
1263 }
1264
1265 jukebox.prototype = {
1266     is_enabled: function() {
1267         return this.enable;
1268     },
1269
1270     play: function(name) {
1271         var a;
1272         if (! this.enable)
1273             return;
1274
1275         if (!(name in this.audio)) {
1276             return false;
1277         }
1278         a = this.audio[name];
1279         a.currentTime = 0;
1280         a.play();
1281     }
1282 }
1283
1284 function topbanner_init()
1285 {
1286     setInterval(topbanner_cb, 666);
1287 ;
1288 }
1289
1290 function topbanner_cb()
1291 {
1292     var a, b;
1293
1294     a = $('topbanner').style.backgroundColor;
1295     b = $('topbanner').style.borderLeftColor;
1296
1297     $('topbanner').style.backgroundColor = b;
1298     $('topbanner').style.borderColor = a+" "+a+" "+a+" "+a;
1299
1300     // console.log("A: "+a+"  B: "+b);
1301 }
1302
1303 function sidebanner_init(idx)
1304 {
1305     setInterval(function () { sidebanner_cb(idx); }, 666);
1306 }
1307
1308 function sidebanner_cb(idx)
1309 {
1310     var a, b;
1311
1312     a = $('sidebanner'+idx).style.backgroundColor;
1313     b = $('sidebanner'+idx).style.borderLeftColor;
1314
1315     $('sidebanner'+idx).style.backgroundColor = b;
1316     $('sidebanner'+idx).style.borderColor = a+" "+a+" "+a+" "+a;
1317
1318     // console.log("A: "+a+"  B: "+b);
1319 }
1320
1321
1322 function langtolng(lang)
1323 {
1324     if (lang == "en")
1325         return ("-en");
1326     else
1327         return ("");
1328 }
1329
1330 function formtext_hilite(obj)
1331 {
1332     obj.className = 'input_text';
1333     addEvent(obj, "focus", function () { this.className = 'input_text_hi'; });
1334     addEvent(obj, "blur",  function () { this.className = 'input_text'; });
1335 }
1336
1337 function formsub_hilite(obj)
1338 {
1339     obj.className = 'input_sub';
1340     addEvent(obj, "focus", function () { this.className = 'input_sub_hi'; });
1341     addEvent(obj, "blur",  function () { this.className = 'input_sub'; });
1342 }
1343
1344 // return the value of the radio button that is checked
1345 // return an empty string if none are checked, or
1346 // there are no radio buttons
1347 function get_checked_value(radioObj) {
1348         if(!radioObj)
1349                 return "";
1350         var radioLength = radioObj.length;
1351         if(radioLength == undefined)
1352                 if(radioObj.checked)
1353                         return radioObj.value;
1354                 else
1355                         return "";
1356         for(var i = 0; i < radioLength; i++) {
1357                 if(radioObj[i].checked) {
1358                         return radioObj[i].value;
1359                 }
1360         }
1361         return "";
1362 }
1363
1364 // set the radio button with the given value as being checked
1365 // do nothing if there are no radio buttons
1366 // if the given value does not exist, all the radio buttons
1367 // are reset to unchecked
1368 function set_checked_value(radioObj, newValue) {
1369         if(!radioObj)
1370                 return;
1371         var radioLength = radioObj.length;
1372         if(radioLength == undefined) {
1373                 radioObj.checked = (radioObj.value == newValue.toString());
1374                 return;
1375         }
1376         for(var i = 0; i < radioLength; i++) {
1377                 radioObj[i].checked = false;
1378                 if(radioObj[i].value == newValue.toString()) {
1379                         radioObj[i].checked = true;
1380                 }
1381         }
1382 }
1383
1384 function url_append_arg(url, name, value)
1385 {
1386     var pos, sep, pref, rest;
1387
1388     if ((pos = url.indexOf('?'+name+'=')) == -1) {
1389         pos = url.indexOf('&'+name+'=');
1390     }
1391     if (pos == -1) {
1392         if ((pos = url.indexOf('?')) != -1)
1393             sep = '&';
1394         else
1395             sep = '?';
1396
1397         return (url+sep+name+"="+encodeURIComponent(value));
1398     }
1399     else {
1400         pref = url.substring(0, pos+1);
1401         rest = url.substring(pos+1);
1402         // alert("rest: "+rest+"  pos: "+pos);
1403         if ((pos = rest.indexOf('&')) != -1) {
1404             rest = rest.substring(pos);
1405         }
1406         else {
1407             rest = "";
1408         }
1409         return (pref+name+"="+encodeURIComponent(value)+rest);
1410     }
1411 }
1412
1413 function url_append_args(url)
1414 {
1415     var i, ret;
1416
1417     ret = url;
1418     for (i = 1 ; i < arguments.length-1 ; i+= 2) {
1419         ret = url_append_arg(ret, arguments[i], arguments[i+1]);
1420     }
1421
1422     return (ret);
1423 }
1424
1425 function url_complete(parent, url)
1426 {
1427     var p, p2, rest;
1428     var host = "", path = "";
1429
1430     // host extraction
1431     p = parent.indexOf("://");
1432     if (p > -1) {
1433         rest = parent.substring(p+3);
1434         p2 = rest.indexOf("/");
1435         if (p2 > -1) {
1436             host = parent.substring(0, p+3+p2);
1437             rest = parent.substring(p+3+p2);
1438         }
1439         else {
1440             host = rest;
1441             rest = "";
1442         }
1443     }
1444     else {
1445         rest = parent;
1446     }
1447
1448     // path extraction
1449     p = rest.lastIndexOf("/");
1450     if (p > -1) {
1451         path = rest.substring(0, p+1);
1452     }
1453
1454     // alert("host: ["+host+"]  path: ["+path+"]");
1455     if (url.substring(0,6) == 'http:/' || url.substring(0,7) == 'https:/' || url.substring(0,4) == 'ws:/' || url.substring(0,5) == 'wss:/') {
1456         return (url);
1457     }
1458     else if (url.substring(0,1) == '/') {
1459         return (host+url);
1460     }
1461     else {
1462         return (host+path+url);
1463     }
1464 }
1465
1466 function download_mgr(url)
1467 {
1468     var ifra;
1469
1470     if ((ifra = $('the_downloader')) == null) {
1471         ifra = document.createElement("iframe");
1472         ifra.style.display = "none";
1473         ifra.id = 'the_downloader';
1474         document.body.appendChild(ifra);
1475     }
1476
1477     ifra.contentWindow.location.href = url;
1478
1479     this.ifra = ifra;
1480 }
1481
1482 download_mgr.prototype = {
1483     ifra: null
1484 }
1485
1486 function submit_click(obj)
1487 {
1488     obj.form.elements['realsub'].value = obj.id;
1489 }