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