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