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