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