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