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