moderation filtering added
[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') { // OK
285             var ldate = new Date();
286             console.log(ldate.getTime()+':MESG:'+mesg); // OK
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     var tr, td, date;
462
463     this.time  = item_ar[0];
464     this.usrid = item_ar[1];
465     this.table = item_ar[2];
466     this.name  = item_ar[3];
467     this.cont  = item_ar[4];
468
469     date = new Date();
470     date.setTime(this.time * 1000);
471
472     tr = document.createElement("tr");
473     td = document.createElement("td");
474     // FIXME a more readable date here
475     // td.innerHTML = date.xxxx;
476     td.innerHTML = this.time % 100000;
477     tr.appendChild(td);
478
479     td = document.createElement("td");
480     td.innerHTML = this.table;
481     tr.appendChild(td);
482
483     td = document.createElement("td");
484     td.innerHTML = this.usrid;
485     tr.appendChild(td);
486
487     td = document.createElement("td");
488     td.innerHTML = this.name;
489     tr.appendChild(td);
490
491     td = document.createElement("td");
492     td.innerHTML = this.cont;
493     tr.appendChild(td);
494
495     this.tr = tr;
496 }
497
498 ModerateItem.prototype = {
499     time: 0,
500     usrid: 0,
501     table: -1,
502     name: "",
503     cont: "",
504
505     tr: null,
506     hide: false,
507     sel: false,
508
509     tr_get: function () {
510         return this.tr;
511     },
512
513     sel_get: function () {
514         return this.sel;
515     },
516
517     sel_set: function (v) {
518         if (this.sel != v) {
519             this.sel = v;
520             this.tr.className = (v ? 'selected' : 'normal');
521         }
522     }
523 }
524
525 function Moderate()
526 {
527     this.item = new Array();
528 }
529
530 Moderate.prototype = {
531     win:  null,
532     table: null,
533     enabled: false,
534     item: null,
535
536     room_show: true,
537     table_show: -1,
538
539     cur: -1,
540
541     disable: function () {
542         if (this.tout) {
543             clearTimeout(this.tout);
544             this.tout = 0;
545         }
546         if (this.win) {
547             this.win.onbeforeunload = null;
548             this.win.close();
549             this.win = null;
550         }
551     },
552
553     activate: function (enable) {
554         if (this.enabled == enable) {
555             return true;
556         }
557         if (enable) {
558             this.disable();
559
560             // FIXME: remove scrollbars, only for devel reason
561             // this.win = window.open("moderation.php", "_blank", "width=800,height=600,toolbar=no,location=no,menubar=no,status=no");
562             this.win = window.open("moderation.php", "_blank", "width=800,height=600,toolbar=no,location=no,menubar=no,status=no,scrollbars=yes");
563             if (this.win == null) {
564                 this.disable();
565                 return false;
566             }
567             // to finish initialization we wait for popup page onload event ...
568             this.win_waitonload();
569         }
570         else {
571             this.disable();
572             this.enabled = false;
573         }
574
575     },
576
577     win_waitonload: function () {
578         if (typeof(this.win.is_loaded)  == 'undefined' || this.win.is_loaded != true) {
579             this.tout = setTimeout(function (obj) { obj.win_waitonload(); }, 250, this);
580         }
581         else {
582             this.post_onload();
583         }
584     },
585
586     post_onload: function() {
587         var tr, td, remtr;
588
589         this.win.anc = this;
590         this.table = $(this.win, 'moder_tab');
591
592         // for (i = 0 ; i < 3 ; i++) {
593         //     tr = document.createElement("tr");
594         //     for (e = 0 ; e < 2 ; e++) {
595         //         td = document.createElement("td");
596         //         td.innerHTML = "Content "+((i * 2)+e);
597         //         tr.appendChild(td);
598         //     }
599         //     this.table.appendChild(tr);
600         //     if (i == 0) 
601         //         remtr = tr;
602         // }
603         // this.table.removeChild(remtr);
604
605         this.enabled = true;
606     },
607
608     onunload: function() {
609         act_moderate();
610     },
611
612     is_enabled: function() {
613         return (this.enabled);
614     },
615
616     add: function(item) {
617         var mi;
618
619         mi = new ModerateItem(item);
620         mi.tr.className = 'normal';
621
622         var self;
623         self = this;
624         mi.tr.onclick = function () { self.row_select(mi); };
625
626         this.item.push(mi);
627         this.table.appendChild(mi.tr_get());
628     },
629
630     row_select: function(mi) {
631         for (i = 0 ; i < this.item.length ; i++) {
632             if (this.item[i] == mi) {
633                 this.item[i].sel_set(!this.item[i].sel_get());
634             }
635             else {
636                 this.item[i].sel_set(false);
637             }
638         }
639         // mi.tr.className = "selected";
640     },
641
642     room_show_update: function(obj) {
643         this.tab_update(obj.checked, this.table_show);
644     },
645
646     //
647     table_show_update: function(obj) {
648         this.tab_update(this.room_show, obj.options[obj.selectedIndex].value );
649     },
650
651     tab_update: function(room_new, table_new)
652     {
653         // remove all and add all valid
654         for (i = 0 ; i < this.item.length ; i++) {
655             if (this.item[i].hide)
656                 continue;
657             this.table.removeChild(this.item[i].tr_get());
658             this.item[i].hide = true;
659         }
660
661         for (i = 0 ; i < this.item.length ; i++) {
662             var app = false;
663
664             if (room_new && table_new == -1) {
665                 app = true;
666             }
667             else if (room_new && table_new != -1) {
668                 if (this.item[i].table == table_new || this.item[i].table == -1) {
669                     app = true;
670                 }
671             }
672             else if (!room_new && table_new == -1) {
673                 if (this.item[i].table != -1) {
674                     app = true;
675                 }
676             }
677             else if (!room_new && table_new != -1) {
678                 if (this.item[i].table == table_new) {
679                     app = true;
680                 }
681             }
682             if (app) {
683                 this.table.appendChild(this.item[i].tr_get());
684                 this.item[i].hide = false;
685             }
686         }
687         this.room_show  = room_new;
688         this.table_show = table_new;
689     }
690 }
691
692 // function AddBefore(rowId){
693 //     var target = document.getElementById(rowId);
694 //     var newElement = document.createElement('tr');
695 //     target.parentNode.insertBefore(newElement, target);
696 //     return newElement;
697 // }
698
699 // function AddAfter(rowId){
700 //     var target = document.getElementById(rowId);
701 //     var newElement = document.createElement('tr');
702
703 //     target.parentNode.insertBefore(newElement, target.nextSibling );
704 //     return newElement;
705 // }
706
707 function moderate(enable)
708 {
709     return (g_moder.activate(enable));
710 }
711
712 var g_moder = new Moderate();
713
714 function act_moderate()
715 {
716     send_mesg("moderate|"+(g_moder.is_enabled() ? "false" : "true"));
717 }
718
719
720
721 //         // build table with js
722         
723 //         g_moder.item = new Array;
724 //         g_moder.table = xxx;
725 //     }
726 //     else {
727 //         if (g_moder == null)
728 //             return true;
729
730 //         if (g_moder.win != null) {
731 //             g_moder.win.close();
732 //             g_moder.win = null;
733 //         }
734
735 //         if (g_moder.item != null) {
736 //             // TODO CLEANUP
737 //             ;
738 //         }
739 //         g_moder.cur = -1;
740 //     }
741 // }
742
743
744 function act_reloadroom()
745 {
746     if (g_moder.is_enabled()) {
747         g_moder.disable();
748     }
749     window.onunload = null;
750     window.onbeforeunload = null;
751     document.location.assign("index.php");
752 }
753
754
755 function act_shutdown()
756 {
757     var c = 0;
758
759     send_mesg("shutdown");
760     // while (xhr_wr.readyState != 4)
761     //  c++;
762 }
763
764 function postact_logout()
765 {
766     // alert("postact_logout");
767
768     if (g_moder.is_enabled()) {
769         g_moder.disable();
770     }
771
772     try { 
773         hstm.abort();
774     } catch (e) {}
775
776     // eraseCookie("sess");
777     document.location.assign("index.php");
778 }
779
780 /*
781   function slowimg(img,x1,y1,deltat,free,action,srcend)
782   img    - image to move
783   x1,y1  - destination coords
784   deltat - time for each frame (in msec)
785   free   - when the release the local block for other operations (range: 0 - 1)
786   action - function to run when the image is moved
787   srcend - image to switch when the image is moved
788 */
789
790 function sleep(st, delay)
791 {
792     // alert("LOC_NEW PRE: "+st.st_loc_new);
793
794     st.st_loc_new++;
795
796     setTimeout(function(obj){ if (obj.st_loc_new > obj.st_loc) { obj.st_loc++; }},
797                delay, st);
798 }
799
800 function slowimg(img,x1,y1,deltat,free,action,srcend) {
801     this.img = img;
802
803     // this.x0  = parseInt(document.defaultView.getComputedStyle(this.img, "").getPropertyValue("left"));
804     this.x0 = parseInt(getStyle(this.img,"left", "left"));
805 // alert("img.x0 = "+this.x0);
806     // this.y0  = parseInt(document.defaultView.getComputedStyle(this.img, "").getPropertyValue("top"));
807     this.y0  = parseInt(getStyle(this.img,"top", "top"));
808     this.x1  = x1;
809     this.y1  = y1;
810     this.deltat = deltat;
811     this.free = free;
812     this.action = action;
813     this.srcend = srcend;
814 }
815
816 slowimg.prototype = {
817     img: null, 
818     st: null,
819     x0: 0,
820     y0: 0,
821     x1: 0,
822     y1: 0,
823     dx: 0,
824     dy: 0,
825     free: 0,
826     step_n:    0,
827     step_cur:  0,
828     step_free: 0,
829     time:      0,
830     deltat:   40,
831     tout: 0,
832     action: null,
833     srcend: null,
834     
835     setstart: function(x0,y0)
836     {
837         this.x0 = x0;
838         this.y0 = y0;
839     },
840     
841     setaction: function(act)
842     {
843         this.action = act;
844     },
845     
846
847     settime: function(time) 
848     {
849         this.time = (time < this.deltat ? this.deltat : time);
850         this.step_n = parseInt(this.time / this.deltat);
851         this.dx = (this.x1 - this.x0) / this.step_n;
852         this.dy = (this.y1 - this.y0) / this.step_n;
853         if (this.step_n * this.deltat == this.time) {
854             this.step_n--;
855         }
856         if (this.free < 1) {
857             this.step_free = parseInt(this.step_n * this.free);
858         }
859     },
860     
861     start: function(st)
862     {
863         // $("logz").innerHTML += "               xxxxxxxxxxxxxxxxxxxxxSTART<br>";
864         this.st = st;
865         this.st.st_loc_new++;
866         
867         this.img.style.visibility = "visible";
868         setTimeout(function(obj){ obj.animate(); }, this.deltat, this);
869     },
870     
871     animate: function()
872     {
873         // $("log").innerHTML = "Val " + this.step_cur + " N: " + this.step_n + "<br>";
874         if (this.step_cur == 0) {
875             var date = new Date();
876             // $("logz").innerHTML = "Timestart: " + date + "<br>";
877         }
878         if (this.step_cur <= this.step_n) {
879             this.img.style.left = this.x0 + this.dx * this.step_cur;
880             this.img.style.top  = this.y0 + this.dy * this.step_cur;
881             this.step_cur++;
882             setTimeout(function(obj){ obj.animate(); }, this.deltat, this);
883             if (this.step_cur == this.step_free && this.st != null) {
884                 if (this.st.st_loc < this.st.st_loc_new) {
885                     // alert("QUI1  " + this.step_cur + "  ZZ  "+  this.step_free);
886                     this.st.st_loc++;
887                     this.st = null;
888                 }
889             }
890         }
891         else {
892             this.img.style.left = this.x1;
893             this.img.style.top  = this.y1;
894             // $("logz").innerHTML += "xxxxxxxxxxxxxxxCLEAR<br>";
895             var date = new Date();
896             // $("logz").innerHTML += "Timestop: " + date + "<br>";
897
898             if (this.action != null) {
899                 eval(this.action);
900             }
901
902             if (this.st != null && this.st.st_loc < this.st.st_loc_new) {
903                 // alert("QUI2");
904                 this.st.st_loc++;
905                 this.st = null;
906             }
907             if (this.srcend != null) {
908                 this.img.src = this.srcend;
909             }
910         }
911     }
912 }
913
914 function div_show(div)
915 {
916     div.style.top = parseInt((document.body.clientHeight - parseInt(getStyle(div,"height", "height"))) / 2) + document.body.scrollTop;
917     div.style.visibility = "visible";
918 }
919
920 function notify_ex(st, text, tout, butt, w, h, is_opa, block_time)
921 {
922     var clo, box;
923     var t = this;
924     
925     this.st = st;
926
927     this.ancestor = document.body;
928     
929     this.st.st_loc_new++;
930
931     clo = document.createElement("input");
932     clo.type = "submit";
933     clo.className = "button";
934     clo.style.bottom = "4px";
935     clo.obj = this;
936     if (block_time > 0) {
937         clo.value = "leggere, prego.";
938         this.butt = butt;
939     }
940     else {
941         clo.value = butt;
942         clo.onclick = this.input_hide;
943     }
944
945     clodiv = document.createElement("div");
946     clodiv.className = "notify_clo";
947     this.clo = clo;
948     this.clodiv = clodiv;
949
950     clodiv.appendChild(clo);
951
952     cont = document.createElement("div");
953
954     cont.style.borderBottomStyle = "solid";
955     cont.style.borderBottomWidth = "1px";
956     cont.style.borderBottomColor = "gray";
957     cont.style.height = (h - 30)+"px";
958     cont.style.overflow = "auto";
959     cont.innerHTML = text;
960
961     box =  document.createElement("div");
962     if (is_opa)
963         box.className = "notify_opaque";
964     else
965         box.className = "notify";
966
967     box.style.zIndex = 200;
968     box.style.width  = w+"px";
969     box.style.marginLeft  = -parseInt(w/2)+"px";
970     box.style.height = h+"px";
971     box.style.top = parseInt((document.body.clientHeight - h) / 2) + document.body.scrollTop;
972     box.appendChild(cont);
973     box.appendChild(clodiv);
974     box.style.visibility = "visible";
975
976     this.notitag = box;
977     
978     this.ancestor.appendChild(box);
979     
980     this.toutid = setTimeout(function(obj){ obj.unblock(); }, tout, this);
981
982     if (block_time != 0) {
983         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);
984     }
985     else {
986         formsub_hilite(clo);
987         clo.focus();
988     }
989
990 }
991
992
993 notify_ex.prototype = {
994     ancestor: null,
995     st: null,
996     notitag: null,
997     toutid: null,
998     clo: null,
999     clodiv: null, 
1000     butt: null,
1001     tblkid: null,
1002
1003     unblock: function()
1004     {
1005         if (this.st.st_loc < this.st.st_loc_new) {
1006             this.st.st_loc++;
1007         }
1008     },
1009     
1010     hide: function()
1011     {
1012         clearTimeout(this.toutid);
1013         this.ancestor.removeChild(this.notitag);
1014         this.unblock();
1015     },
1016
1017     input_hide: function()
1018     {
1019         clearTimeout(this.obj.toutid);
1020         this.obj.ancestor.removeChild(this.obj.notitag);
1021         this.obj.unblock();
1022     }
1023 }
1024
1025
1026 notify.prototype = notify_ex.prototype;                // Define sub-class
1027 notify.prototype.constructor = notify;
1028 notify.baseConstructor = notify_ex;
1029 notify.superClass = notify_ex.prototype;
1030
1031 function notify(st, text, tout, butt, w, h)
1032 {
1033     notify_ex.call(this, st, text, tout, butt, w, h, false, 0);
1034 }
1035
1036 function globst() {
1037     this.st = -1;
1038     this.st_loc = -1;
1039     this.st_loc_new = -1;
1040     this.comms  = new Array;
1041 }
1042
1043 globst.prototype = {
1044     st: -1,
1045     st_loc: -1,
1046     st_loc_new: -1,
1047     comms: null,
1048     sleep_hdl: null,
1049
1050     sleep: function(delay) {
1051         st.st_loc_new++;
1052
1053         if (!this.the_end) {
1054             this.sleep_hdl = setTimeout(function(obj){ if (obj.st_loc_new > obj.st_loc) { obj.st_loc++; obj.sleep_hdl = null; }},
1055                                         delay, this);
1056         }
1057     },
1058
1059     abort: function() {
1060         if (this.sleep_hdl != null) {
1061             clearTimeout(this.sleep_hdl);
1062             this.sleep_hdl = null;
1063         }
1064     }
1065 }
1066
1067 function remark_step()
1068 {
1069     var ct = $("remark").l_remct;
1070     
1071     if (ct != 0) {
1072         ct++;
1073         if (ct > 2)
1074             ct = 1;
1075         $("remark").className = "remark"+ct;
1076         $("remark").l_remct = ct;
1077         setTimeout(remark_step,500);
1078     }
1079     else
1080         $("remark").className = "remark0";
1081     
1082     return;
1083 }
1084
1085 function remark_on()
1086 {
1087     if ($("remark").l_remct == 0) {
1088         $("remark").l_remct = 1;
1089         setTimeout(remark_step,500);
1090     }
1091 }
1092
1093 function remark_off()
1094 {
1095     $("remark").l_remct = 0;
1096     $("remark").className = "remark0";
1097 }
1098
1099
1100 function italizer(ga)
1101 {
1102     var pre, pos;
1103     if (ga[0] & 2) 
1104         return "<i>"+ga[1]+"</i>";
1105     else
1106         return ga[1];
1107 }
1108
1109
1110 function exitlock_show(num, islock)
1111 {
1112     g_exitlock = num;
1113
1114     num = (num < 3 ? num : 3);
1115     $("exitlock").src = "img/exitlock"+num+(islock ? "n" : "y")+".png";
1116     // alert("EXITLOCK: "+$("exitlock").src);
1117     $("exitlock").style.visibility = "visible";
1118 }
1119
1120 var fin = 0;
1121
1122 //    exitlock_show(0, true);
1123
1124
1125 var chatt_lines = new Array();
1126 var chatt_lines_n = 0;
1127
1128 var CHATT_MAXLINES = 40;
1129
1130 function user_decorator(user)
1131 {
1132     var name;
1133     var flags = user[BSK_USER_FLAGS];
1134     var flags_vlt = user[BSK_USER_FLGVL];
1135     if ((flags & 0x03) != 0)
1136         name = "<span class='au" + (flags & 0x03) + "'>"+user[BSK_USER_NICK]+"</span>";
1137     else
1138         name = user[BSK_USER_NICK];
1139
1140     return (name);
1141 }
1142
1143 function user_dec_and_state(el)
1144 {
1145     var content = "";
1146     var val_el;
1147
1148     content = user_decorator(el);
1149     content += state_add(el[BSK_USER_FLAGS], el[BSK_USER_FLGVL],
1150                          (typeof(el[BSK_USER_SCOL]) != 'undefined' ? el[BSK_USER_SCOL] : null));
1151     
1152     return (content);
1153 }
1154
1155
1156 /* PRO CHATT */
1157 function chatt_sub(dt,data,str)
1158 {
1159     var must_scroll = false;
1160     var name;
1161     var flags;
1162     var isauth;
1163     var bolder = [ (data[BSK_USER_FLAGS] | 1), data[BSK_USER_FLGVL], data[BSK_USER_NICK] ];
1164     name = user_decorator(bolder);
1165
1166     if ($("txt").scrollTop + parseInt(getStyle($("txt"),"height", "height")) -  $("txt").scrollHeight >= 0)
1167         must_scroll = true;
1168
1169     // alert("ARRIVA NAME: "+ name + "  STR:"+str);
1170     if (chatt_lines_n == CHATT_MAXLINES) {
1171         $("txt").innerHTML = "";
1172         for (i = 0 ; i < (CHATT_MAXLINES - 1) ; i++) {
1173             chatt_lines[i] = chatt_lines[i+1];
1174             $("txt").innerHTML += chatt_lines[i];
1175         }
1176         chatt_lines[i] = dt+name+": "+str+ "<br>";
1177         $("txt").innerHTML += chatt_lines[i];
1178     }
1179     else {
1180         chatt_lines[chatt_lines_n] = dt+name+": "+str+ "<br>";
1181         $("txt").innerHTML += chatt_lines[chatt_lines_n];
1182         chatt_lines_n++;
1183     }
1184     // $("txt").innerHTML;
1185
1186     
1187     if (must_scroll) {
1188         $("txt").scrollTop = 10000000;
1189     }
1190     // alert("scTOP "+$("txt").scrollTop+"  scHEIGHT: "+$("txt").scrollHeight+" HEIGHT: "+getStyle($("txt"),"height", "height") );
1191 }
1192
1193 /*
1194  *  GESTIONE DEI COOKIES
1195  */
1196 function createCookie(name,value,hours,path) {
1197         if (hours) {
1198                 var date = new Date();
1199                 date.setTime(date.getTime()+(hours*60*60*1000));
1200                 var expires = "; expires="+date.toGMTString();
1201         }
1202         else var expires = "";
1203         document.cookie = name+"="+value+expires+"; path="+path;
1204 }
1205
1206 function readCookie(name) {
1207         var nameEQ = name + "=";
1208         var ca = document.cookie.split(';');
1209         for(var i=0;i < ca.length;i++) {
1210                 var c = ca[i];
1211                 while (c.charAt(0)==' ') c = c.substring(1,c.length);
1212                 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
1213         }
1214         return null;
1215 }
1216
1217 function eraseCookie(name) {
1218         createCookie(name,"",-1);
1219 }
1220
1221 function onbeforeunload_cb () {
1222     return("");
1223 }
1224
1225 function onunload_cb () {
1226     
1227     if (typeof(hstm) != "undefined")
1228         hstm.the_end = true; 
1229
1230     act_shutdown();
1231     
1232     return(false);
1233 }
1234
1235 function room_checkspace(emme,tables,inpe)
1236 {
1237     nome = "<b>";
1238     for (i = 0 ; i < emme ; i++) 
1239         nome += "m";
1240     nome += "</b>";
1241
1242     alta = "";
1243     for (i = 0 ; i < 5 ; i++) 
1244         alta += nome+"<br>";
1245
1246     for (i = 0 ; i < tables ; i++) {
1247         $("table"+i).innerHTML = alta;
1248         // MLANG Mi siedo.
1249         $("table_act"+i).innerHTML = "<input type=\"button\" class=\"button\" name=\"xhenter"+i+"\"  value=\""+mlang_commons['btn_sit'][g_lang]+"\" onclick=\"act_sitdown(1);\">";
1250         }
1251
1252     stand = "<table class=\"table_standup\"><tbody><tr>";
1253     for (i = 0 ; i < inpe ; i++) {
1254         stand += "<td>"+nome+"</td>";
1255         if ((i+1) % 4 == 0) {
1256             stand += "</tr><tr>";
1257         }
1258     }
1259     stand += "</tr>";
1260     $("standup").innerHTML = stand;
1261
1262     // VERIFY: what is this button ?
1263     // MLANG Esco.
1264     $("esco").innerHTML = "<input class=\"button\" name=\"logout\" type=\"button\" value=\""+mlang_commons['btn_exit'][g_lang]+"\" onclick=\"act_logout();\" type=\"button\">";
1265 }
1266
1267 function  unescapeHTML(cont) {
1268     var div = document.createElement('div');
1269     var memo = "";
1270     var i;
1271
1272     div.innerHTML = cont;
1273     if (div.childNodes[0]) {
1274         if (div.childNodes.length > 1) {
1275             if (div.childNodes.toArray)
1276                 alert("si puo");
1277             else {
1278                 var length = div.childNodes.length, results = new Array(length);
1279             while (length--)
1280                 results[length] = div.childNodes[length];
1281                 
1282             for (i=0 ; i<results.length ; i++)
1283                 memo = memo + results[i].nodeValue;
1284             }
1285
1286             return (memo);
1287         }
1288         else {
1289             return (div.childNodes[0].nodeValue);
1290         }
1291     }
1292     else {
1293         return ('');
1294     }
1295 }
1296
1297 function playsound(tag, sound) {
1298    // g_withflash is a global var
1299    if (g_withflash) {
1300       $(tag).innerHTML = '<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" '+
1301 'codebase="http://active.macromedia.com/flash2/cabs/swflash.cab#version=4,0,0,0" id="mysound" WIDTH=1 HEIGHT=1>' +
1302 '<PARAM NAME="movie" VALUE="../playsound.swf"><PARAM NAME="PLAY" VALUE="true"><PARAM NAME="LOOP" VALUE="false">' +
1303 '<PARAM NAME=FlashVars VALUE="streamUrl='+sound+'">' +
1304 '<EMBED swliveconnect="true" name="mysound" src="../playsound.swf" FlashVars="streamUrl='+sound+'" PLAY="true" LOOP="false" '+
1305 ' WIDTH=1 HEIGHT=1 TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></OBJECT>';
1306    }
1307 }
1308
1309 function topbanner_init()
1310 {
1311     setInterval(topbanner_cb, 666);
1312 ;
1313 }
1314
1315 function topbanner_cb()
1316 {
1317     var a, b;
1318
1319     a = $('topbanner').style.backgroundColor;
1320     b = $('topbanner').style.borderLeftColor;
1321
1322     $('topbanner').style.backgroundColor = b;
1323     $('topbanner').style.borderColor = a+" "+a+" "+a+" "+a;
1324
1325     // console.log("A: "+a+"  B: "+b);
1326 }
1327
1328 function sidebanner_init()
1329 {
1330     setInterval(sidebanner_cb, 666);
1331 }
1332
1333 function sidebanner2_init()
1334 {
1335     setInterval(sidebanner2_cb, 666);
1336 }
1337
1338 function sidebanner_cb()
1339 {
1340     var a, b;
1341
1342     a = $('sidebanner').style.backgroundColor;
1343     b = $('sidebanner').style.borderLeftColor;
1344
1345     $('sidebanner').style.backgroundColor = b;
1346     $('sidebanner').style.borderColor = a+" "+a+" "+a+" "+a;
1347
1348     // console.log("A: "+a+"  B: "+b);
1349 }
1350
1351 function sidebanner2_cb()
1352 {
1353     var a, b;
1354
1355     a = $('sidebanner2').style.backgroundColor;
1356     b = $('sidebanner2').style.borderLeftColor;
1357
1358     $('sidebanner2').style.backgroundColor = b;
1359     $('sidebanner2').style.borderColor = a+" "+a+" "+a+" "+a;
1360
1361     // console.log("A: "+a+"  B: "+b);
1362 }
1363
1364
1365 function langtolng(lang)
1366 {
1367     if (lang == "en")
1368         return ("-en");
1369     else
1370         return ("");
1371 }
1372
1373 function formtext_hilite(obj)
1374 {
1375     obj.className = 'input_text';
1376     addEvent(obj, "focus", function () { this.className = 'input_text_hi'; });
1377     addEvent(obj, "blur",  function () { this.className = 'input_text'; });
1378 }
1379
1380 function formsub_hilite(obj)
1381 {
1382     obj.className = 'input_sub';
1383     addEvent(obj, "focus", function () { this.className = 'input_sub_hi'; });
1384     addEvent(obj, "blur",  function () { this.className = 'input_sub'; });
1385 }
1386
1387 // return the value of the radio button that is checked
1388 // return an empty string if none are checked, or
1389 // there are no radio buttons
1390 function get_checked_value(radioObj) {
1391         if(!radioObj)
1392                 return "";
1393         var radioLength = radioObj.length;
1394         if(radioLength == undefined)
1395                 if(radioObj.checked)
1396                         return radioObj.value;
1397                 else
1398                         return "";
1399         for(var i = 0; i < radioLength; i++) {
1400                 if(radioObj[i].checked) {
1401                         return radioObj[i].value;
1402                 }
1403         }
1404         return "";
1405 }
1406
1407 // set the radio button with the given value as being checked
1408 // do nothing if there are no radio buttons
1409 // if the given value does not exist, all the radio buttons
1410 // are reset to unchecked
1411 function set_checked_value(radioObj, newValue) {
1412         if(!radioObj)
1413                 return;
1414         var radioLength = radioObj.length;
1415         if(radioLength == undefined) {
1416                 radioObj.checked = (radioObj.value == newValue.toString());
1417                 return;
1418         }
1419         for(var i = 0; i < radioLength; i++) {
1420                 radioObj[i].checked = false;
1421                 if(radioObj[i].value == newValue.toString()) {
1422                         radioObj[i].checked = true;
1423                 }
1424         }
1425 }
1426
1427 function url_append_arg(url, name, value)
1428 {
1429     var pos, sep, pref, rest;
1430
1431     if ((pos = url.indexOf('?'+name+'=')) == -1) {
1432         pos = url.indexOf('&'+name+'=');
1433     }
1434     if (pos == -1) {
1435         if ((pos = url.indexOf('?')) != -1)
1436             sep = '&';
1437         else
1438             sep = '?';
1439
1440         return (url+sep+name+"="+encodeURIComponent(value));
1441     }
1442     else {
1443         pref = url.substring(0, pos+1);
1444         rest = url.substring(pos+1);
1445         // alert("rest: "+rest+"  pos: "+pos);
1446         if ((pos = rest.indexOf('&')) != -1) {
1447             rest = rest.substring(pos);
1448         }
1449         else {
1450             rest = "";
1451         }
1452         return (pref+name+"="+encodeURIComponent(value)+rest);
1453     }
1454 }
1455
1456 function url_append_args(url)
1457 {
1458     var i, ret;
1459
1460     ret = url;
1461     for (i = 1 ; i < arguments.length-1 ; i+= 2) {
1462         ret = url_append_arg(ret, arguments[i], arguments[i+1]);
1463     }
1464
1465     return (ret);
1466 }
1467
1468 function url_complete(parent, url)
1469 {
1470     var p, p2, rest;
1471     var host = "", path = "";
1472
1473     // host extraction
1474     p = parent.indexOf("://");
1475     if (p > -1) {
1476         rest = parent.substring(p+3);
1477         p2 = rest.indexOf("/");
1478         if (p2 > -1) {
1479             host = parent.substring(0, p+3+p2);
1480             rest = parent.substring(p+3+p2);
1481         }
1482         else {
1483             host = rest;
1484             rest = "";
1485         }
1486     }
1487     else {
1488         rest = parent;
1489     }
1490
1491     // path extraction
1492     p = rest.lastIndexOf("/");
1493     if (p > -1) {
1494         path = rest.substring(0, p+1);
1495     }
1496
1497     // alert("host: ["+host+"]  path: ["+path+"]");
1498     if (url.substring(0,6) == 'http:/' || url.substring(0,7) == 'https:/') {
1499         return (url);
1500     }
1501     else if (url.substring(0,1) == '/') {
1502         return (host+url);
1503     }
1504     else {
1505         return (host+path+url);
1506     }
1507 }