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