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