allignment pre merge in master
[brisk.git] / web / commons.js
1 /*
2  *  brisk - commons.js
3  *
4  *  Copyright (C) 2006-2008 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  * $Id$
23  *
24  */
25
26 var PLAYERS_N = 3;
27 var EXIT_BAN_TIME = 900;
28 var cookiepath = "/brisk/";
29
30 var mlang_commons = { 'imgload_a' : { 'it' : 'Immagine caricate ', 
31                                       'en' : 'Loaded images ' },
32                       'imgload_b' : { 'it' : '%.', 
33                                       'en' : '%.' },
34                       'gamleav'   : { 'it' : 'Sei sicuro di volere lasciare questa mano?' ,
35                                       'en' : 'Are you sure to leave this game?' },
36                       'brileav'   : { 'it' : '    Vuoi veramente abbandonare la briscola ?\n(clicca annulla o cancel se vuoi ricaricare la briscola)',
37                                       'en' : '    Are you really sure to leave briscola ?\n(click cancel yo reload it)' },
38                       'brireco'   : { 'it' : 'Ripristino della briscola fallito, per non perdere la sessione ricaricare la pagina manualmente.',
39                                       'en' : 'Recovery of briscola failed, to keep the current session reload the page manually.' },
40                       'btn_sit'   : { 'it' : 'Mi siedo.',
41                                       'en' : 'Sit down.' },
42                       'btn_exit'  : { 'it' : 'Esco.',
43                                       'en' : 'Exit.' }
44                       
45                       };
46
47 function $(id) { return document.getElementById(id); }
48
49 function getStyle(x,IEstyleProp, MozStyleProp) 
50 {
51     if (x.currentStyle) {
52         var y = x.currentStyle[IEstyleProp];
53     } else if (window.getComputedStyle) {
54         var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(MozStyleProp);
55     }
56     return y;
57 }
58
59 /* replacement of setInterval on IE */
60 (function(){
61     /*if not IE, do nothing*/
62     if(!document.uniqueID){return;};
63
64     /*Copy the default setInterval behavior*/
65     var nativeSetInterval = window.setInterval;
66     window.setInterval = function(fn,ms) {              
67         var param = [];
68         if(arguments.length <= 2)       {
69             return nativeSetInterval(fn,ms);
70         }
71         else {
72             for(var i=2;i<arguments.length;i+=1) {
73                 param[i-2] =  arguments[i];
74             }   
75         }
76         
77         if(typeof(fn)=='function') {
78             
79             return (function (fn,ms,param) {
80                 var fo = function () {                                                          
81                     fn.apply(window,param);
82                 };                      
83                 return nativeSetInterval(fo,ms); 
84             })(fn,ms,param);
85         }
86         else if(typeof(fn)=='string')
87         {
88             return  nativeSetInterval(fn,ms);
89         }
90         else
91         {
92             throw Error('setInterval Error\nInvalid function type');
93         };
94     };
95
96     /*Copy the default setTimeout behavior*/
97     var nativeSetTimeout = window.setTimeout;
98     window.setTimeout = function(fn,ms) {               
99         var param = [];
100         if(arguments.length <= 2)       {
101             return nativeSetTimeout(fn,ms);
102         }
103         else {
104             for(var i=2;i<arguments.length;i+=1) {
105                 param[i-2] =  arguments[i];
106             }   
107         }
108         
109         if(typeof(fn)=='function') {
110             
111             return (function (fn,ms,param) {
112                 var fo = function () {                                                          
113                     fn.apply(window,param);
114                 };                      
115                 return nativeSetTimeout(fo,ms); 
116             })(fn,ms,param);
117         }
118         else if(typeof(fn)=='string')
119         {
120             return  nativeSetTimeout(fn,ms);
121         }
122         else
123         {
124             throw Error('setTimeout Error\nInvalid function type');
125         };
126     };
127
128 })()
129
130 function addEvent(obj,type,fn)
131 {
132     if (obj.addEventListener) {
133         obj.addEventListener( type, fn, false);
134     }
135     else if (obj.attachEvent) {
136         obj["e"+type+fn] = fn;
137         obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
138         obj.attachEvent( "on"+type, obj[type+fn] );
139     }
140     else
141         throw new Error("Event registration not supported");
142 }
143
144 function removeEvent(obj,type,fn)
145 {
146     if (obj.removeEventListener) {
147         obj.removeEventListener( type, fn, false );
148     }
149     else if (obj.detachEvent) {
150         obj.detachEvent( "on"+type, obj[type+fn] );
151         obj[type+fn] = null;
152         obj["e"+type+fn] = null;
153     }
154 }
155
156     // var card_pos = RANGE 0 <= x < cards_ea_n
157
158 function show_bigpict(obj, act, x, y)
159 {
160    var big, sfx;
161
162    if (arguments.length > 4)
163        sfx = arguments[4];
164    else
165        sfx = '';
166
167    big = $(obj.id+"_big"+sfx);
168    if (act == "over") {
169        big.style.left = obj.offsetLeft + x+"px";
170        big.style.top  = obj.offsetTop  + y+"px";
171        big.style.visibility = "visible";
172        }
173    else {
174        big.style.visibility = "hidden";
175        }
176 }
177
178 function rnd_int(min, max) {
179   return Math.floor(Math.random() * (max - min + 1) + min);
180 }
181
182 function error_images()
183 {
184     // alert("GHESEMU!");
185     setTimeout(preload_images, 2000, g_preload_img_arr, g_imgct-1);
186 }
187
188 function abort_images()
189 {
190     // alert("ABORTAIMAGES");
191     setTimeout(preload_images, 2000, g_preload_img_arr, g_imgct-1);
192 }
193
194 function unload_images()
195 {
196     // alert("ABORTAIMAGES");
197     setTimeout(preload_images, 2000, g_preload_img_arr, g_imgct-1);
198 }
199
200 function reset_images()
201 {
202     // alert("ABORTAIMAGES");
203     setTimeout(preload_images, 2000, g_preload_img_arr, g_imgct-1);
204 }
205
206 function update_images()
207 {
208     // MLANG "Immagine caricate" + g_preload_imgsz_arr[g_imgct] + "%."
209     $("imgct").innerHTML = mlang_commons['imgload_a'][g_lang]+g_preload_imgsz_arr[g_imgct]+"%.";
210     if (g_imgct+1 < g_preload_img_arr.length) {
211         g_imgct++;
212         setTimeout(preload_images, 100, g_preload_img_arr, g_imgct-1);
213     }
214     // $("imgct").innerHTML += "U";
215 }
216
217 function preload_images(arr,idx)
218 {
219     var im = new Image;
220     
221     // $("imgct").innerHTML = "Stiamo caricando "+arr[idx]+"%.<br>";
222     im.onload =   update_images;
223     im.onerror =  error_images;
224     im.onabort =  abort_images;
225     im.onunload = unload_images;
226     im.onreset =  reset_images;
227     im.src =      arr[idx];
228     // $("imgct").innerHTML += "P";
229 }
230
231 function safestatus(a)
232 {
233     try{
234         return (a.status);
235     } catch(b)
236         { return (-1); }
237 }
238
239 function createXMLHttpRequest() {
240     try { return new ActiveXObject("Msxml2.XMLHTTP");    } catch(e) {}
241     try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {}
242     try { return new XMLHttpRequest();                   } catch(e) {}
243     alert("XMLHttpRequest not supported");
244     return null;
245 }
246
247 function send_mesg(mesg)
248 {
249     var xhr_wr = createXMLHttpRequest();
250     var is_conn = (sess == "not_connected" ? false : true);
251     
252     // alert("xhr_wr: "+xhr_wr+"  is_conn: "+is_conn);
253     xhr_wr.open('GET', 'index_wr.php?'+(is_conn ? 'sess='+sess+'&' : '')+'mesg='+mesg, (is_conn ? true : false));
254     xhr_wr.onreadystatechange = function() { return; };
255     if (typeof(console) == 'object') {
256         if (typeof(console.log) == 'function') {
257             var ldate = new Date();
258             console.log(ldate.getTime()+':MESG:'+mesg);
259         }
260     }
261     xhr_wr.send(null);
262
263     if (!is_conn) {
264         if (xhr_wr.responseText != null) {
265             eval(xhr_wr.responseText);
266         }
267     }
268 }
269
270 /*
271   request to server
272   server_request([arg0=arg1[, arg2=arg3[, ...]]])
273  */
274 function server_request()
275 {
276     var xhr_wr = createXMLHttpRequest();
277     var i, collect = "";
278
279     if (arguments.length > 0) {
280         for (i = 0 ; i < arguments.length ; i+= 2) {
281             collect += (i == 0 ? "" : "&") + arguments[i] + "=" + encodeURIComponent(arguments[i+1]);
282         }
283     }
284     // alert("Args: "+arguments.length);
285
286     var is_conn = (sess == "not_connected" ? false : true);
287     
288     // console.log("server_request:preresp: "+xhr_wr.responseText);
289
290     xhr_wr.open('GET', 'index_wr.php?'+(is_conn ? 'sess='+sess+'&' : '')+collect, false);
291     xhr_wr.onreadystatechange = function() { return; };
292     xhr_wr.send(null);
293     
294     if (xhr_wr.responseText != null) {
295         // console.log("server_request:resp: "+xhr_wr.responseText);
296         return (xhr_wr.responseText);
297     } 
298     else
299         return (null);
300 }
301
302 /* Stat: CHAT and TABLE */
303
304 function chatt_checksend(obj,e)
305 {
306     var keynum;
307     var keychar;
308     var numcheck;
309
310     if(window.event) { // IE
311         keynum = e.keyCode;
312     }
313     else if(e.which) { // Netscape/Firefox/Opera
314         keynum = e.which;
315     }
316     // alert("OBJ: "+obj);
317     if (keynum == 13 && obj.value != "") { // Enter
318         act_chatt(obj.value);
319         obj.value = "";
320     }
321 }
322 function act_chatt(value)
323 {
324     send_mesg("chatt|"+encodeURIComponent(value));
325     /*
326     obj.disabled = true;
327     obj.value = "";
328     obj.disabled = false;
329     obj.focus();
330     */
331     return false;
332 }
333
334 /* Stat: ROOM */
335 function act_sitdown(table)
336 {
337     send_mesg("sitdown|"+table);
338 }
339
340 function act_wakeup()
341 {
342     send_mesg("wakeup");
343 }
344
345 function act_splash()
346 {
347     send_mesg("splash");
348 }
349
350 function act_help()
351 {
352     send_mesg("help");
353 }
354
355 function act_passwdhowto()
356 {
357     send_mesg("passwdhowto");
358 }
359
360 function act_mesgtoadm()
361 {
362     send_mesg("mesgtoadm");
363 }
364
365 function act_tav()
366 {
367     act_chatt('/tav '+$('txt_in').value); 
368     $('txt_in').value = '';
369 }
370
371 function act_about()
372 {
373     send_mesg("about");
374 }
375
376 function act_placing()
377 {
378     send_mesg("placing");
379 }
380
381 function act_roadmap()
382 {
383     send_mesg("roadmap");
384 }
385
386 function act_whysupport()
387 {
388     send_mesg("whysupport");
389 }
390
391 function act_lascio()
392 {
393     send_mesg("lascio");
394 }
395
396 function safelascio()
397 {
398     var res;
399     // MLANG "Sei sicuro di volere lasciare questa mano?"
400     res = window.confirm(mlang_commons['gamleav'][g_lang]);
401     if (res)
402         act_lascio();
403 }
404
405 function act_logout(exitlock)
406 {
407     send_mesg("logout|"+exitlock);
408 }
409
410 function act_reloadroom()
411 {
412     window.onunload = null;
413     window.onbeforeunload = null;
414     document.location.assign("index.php");
415 }
416
417 function act_shutdown()
418 {
419     var c = 0;
420
421     send_mesg("shutdown");
422     // while (xhr_wr.readyState != 4)
423     //  c++;
424 }
425
426 function postact_logout()
427 {
428     // alert("postact_logout");
429     try { 
430         xhr_rd.abort();
431     } catch (e) {}
432
433     // eraseCookie("sess");
434     document.location.assign("index.php");
435 }
436
437 /*
438   function slowimg(img,x1,y1,deltat,free,action,srcend)
439   img    - image to move
440   x1,y1  - destination coords
441   deltat - time for each frame (in msec)
442   free   - when the release the local block for other operations (range: 0 - 1)
443   action - function to run when the image is moved
444   srcend - image to switch when the image is moved
445 */
446
447 function sleep(st, delay)
448 {
449     // alert("LOC_NEW PRE: "+st.st_loc_new);
450
451     st.st_loc_new++;
452
453     setTimeout(function(obj){ if (obj.st_loc_new > obj.st_loc) { obj.st_loc++; }},
454                delay, st);
455 }
456
457 function slowimg(img,x1,y1,deltat,free,action,srcend) {
458     this.img = img;
459
460     // this.x0  = parseInt(document.defaultView.getComputedStyle(this.img, "").getPropertyValue("left"));
461     this.x0 = parseInt(getStyle(this.img,"left", "left"));
462 // alert("img.x0 = "+this.x0);
463     // this.y0  = parseInt(document.defaultView.getComputedStyle(this.img, "").getPropertyValue("top"));
464     this.y0  = parseInt(getStyle(this.img,"top", "top"));
465     this.x1  = x1;
466     this.y1  = y1;
467     this.deltat = deltat;
468     this.free = free;
469     this.action = action;
470     this.srcend = srcend;
471 }
472
473 slowimg.prototype = {
474     img: null, 
475     st: null,
476     x0: 0,
477     y0: 0,
478     x1: 0,
479     y1: 0,
480     dx: 0,
481     dy: 0,
482     free: 0,
483     step_n:    0,
484     step_cur:  0,
485     step_free: 0,
486     time:      0,
487     deltat:   40,
488     tout: 0,
489     action: null,
490     srcend: null,
491     
492     setstart: function(x0,y0)
493     {
494         this.x0 = x0;
495         this.y0 = y0;
496     },
497     
498     setaction: function(act)
499     {
500         this.action = act;
501     },
502     
503
504     settime: function(time) 
505     {
506         this.time = (time < this.deltat ? this.deltat : time);
507         this.step_n = parseInt(this.time / this.deltat);
508         this.dx = (this.x1 - this.x0) / this.step_n;
509         this.dy = (this.y1 - this.y0) / this.step_n;
510         if (this.step_n * this.deltat == this.time) {
511             this.step_n--;
512         }
513         if (this.free < 1) {
514             this.step_free = parseInt(this.step_n * this.free);
515         }
516     },
517     
518     start: function(st)
519     {
520         // $("logz").innerHTML += "               xxxxxxxxxxxxxxxxxxxxxSTART<br>";
521         this.st = st;
522         this.st.st_loc_new++;
523         
524         this.img.style.visibility = "visible";
525         setTimeout(function(obj){ obj.animate(); }, this.deltat, this);
526     },
527     
528     animate: function()
529     {
530         // $("log").innerHTML = "Val " + this.step_cur + " N: " + this.step_n + "<br>";
531         if (this.step_cur == 0) {
532             var date = new Date();
533             // $("logz").innerHTML = "Timestart: " + date + "<br>";
534         }
535         if (this.step_cur <= this.step_n) {
536             this.img.style.left = this.x0 + this.dx * this.step_cur;
537             this.img.style.top  = this.y0 + this.dy * this.step_cur;
538             this.step_cur++;
539             setTimeout(function(obj){ obj.animate(); }, this.deltat, this);
540             if (this.step_cur == this.step_free && this.st != null) {
541                 if (this.st.st_loc < this.st.st_loc_new) {
542                     // alert("QUI1  " + this.step_cur + "  ZZ  "+  this.step_free);
543                     this.st.st_loc++;
544                     this.st = null;
545                 }
546             }
547         }
548         else {
549             this.img.style.left = this.x1;
550             this.img.style.top  = this.y1;
551             // $("logz").innerHTML += "xxxxxxxxxxxxxxxCLEAR<br>";
552             var date = new Date();
553             // $("logz").innerHTML += "Timestop: " + date + "<br>";
554
555             if (this.action != null) {
556                 eval(this.action);
557             }
558
559             if (this.st != null && this.st.st_loc < this.st.st_loc_new) {
560                 // alert("QUI2");
561                 this.st.st_loc++;
562                 this.st = null;
563             }
564             if (this.srcend != null) {
565                 this.img.src = this.srcend;
566             }
567         }
568     }
569 }
570
571 function div_show(div)
572 {
573     div.style.top = parseInt((document.body.clientHeight - parseInt(getStyle(div,"height", "height"))) / 2) + document.body.scrollTop;
574     div.style.visibility = "visible";
575 }
576
577 function notify_ex(st, text, tout, butt, w, h, is_opa, block_time)
578 {
579     var clo, box;
580     var t = this;
581     
582     this.st = st;
583
584     this.ancestor = document.body;
585     
586     this.st.st_loc_new++;
587
588     clo = document.createElement("input");
589     clo.type = "submit";
590     clo.className = "button";
591     clo.style.bottom = "4px";
592     clo.obj = this;
593     if (block_time > 0) {
594         clo.value = "leggere, prego.";
595         this.butt = butt;
596     }
597     else {
598         clo.value = butt;
599         clo.onclick = this.input_hide;
600     }
601
602     clodiv = document.createElement("div");
603     clodiv.className = "notify_clo";
604     this.clo = clo;
605     this.clodiv = clodiv;
606
607     clodiv.appendChild(clo);
608
609     cont = document.createElement("div");
610
611     cont.style.borderBottomStyle = "solid";
612     cont.style.borderBottomWidth = "1px";
613     cont.style.borderBottomColor = "gray";
614     cont.style.height = (h - 30)+"px";
615     cont.style.overflow = "auto";
616     cont.innerHTML = text;
617
618     box =  document.createElement("div");
619     if (is_opa)
620         box.className = "notify_opaque";
621     else
622         box.className = "notify";
623
624     box.style.zIndex = 200;
625     box.style.width  = w+"px";
626     box.style.marginLeft  = -parseInt(w/2)+"px";
627     box.style.height = h+"px";
628     box.style.top = parseInt((document.body.clientHeight - h) / 2) + document.body.scrollTop;
629     box.appendChild(cont);
630     box.appendChild(clodiv);
631     box.style.visibility = "visible";
632
633     this.notitag = box;
634     
635     this.ancestor.appendChild(box);
636     
637     this.toutid = setTimeout(function(obj){ obj.unblock(); }, tout, this);
638
639     if (block_time != 0) {
640         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);
641     }
642     else {
643         formsub_hilite(clo);
644         clo.focus();
645     }
646
647 }
648
649
650 notify_ex.prototype = {
651     ancestor: null,
652     st: null,
653     notitag: null,
654     toutid: null,
655     clo: null,
656     clodiv: null, 
657     butt: null,
658     tblkid: null,
659
660     unblock: function()
661     {
662         if (this.st.st_loc < this.st.st_loc_new) {
663             this.st.st_loc++;
664         }
665     },
666     
667     hide: function()
668     {
669         clearTimeout(this.toutid);
670         this.ancestor.removeChild(this.notitag);
671         this.unblock();
672     },
673
674     input_hide: function()
675     {
676         clearTimeout(this.obj.toutid);
677         this.obj.ancestor.removeChild(this.obj.notitag);
678         this.obj.unblock();
679     }
680 }
681
682
683 notify.prototype = notify_ex.prototype;                // Define sub-class
684 notify.prototype.constructor = notify;
685 notify.baseConstructor = notify_ex;
686 notify.superClass = notify_ex.prototype;
687
688 function notify(st, text, tout, butt, w, h)
689 {
690     notify_ex.call(this, st, text, tout, butt, w, h, false, 0);
691 }
692         
693
694 function $(id) { 
695     return document.getElementById(id); 
696 }
697
698 function globst() {
699     this.st = -1;
700     this.st_loc = -1;
701     this.st_loc_new = -1;
702     this.comms  = new Array;
703 }
704
705
706
707 function remark_step()
708 {
709     var ct = $("remark").l_remct;
710     
711     if (ct != 0) {
712         ct++;
713         if (ct > 2)
714             ct = 1;
715         $("remark").className = "remark"+ct;
716         $("remark").l_remct = ct;
717         setTimeout(remark_step,500);
718     }
719     else
720         $("remark").className = "remark0";
721     
722     return;
723 }
724
725 function remark_on()
726 {
727     if ($("remark").l_remct == 0) {
728         $("remark").l_remct = 1;
729         setTimeout(remark_step,500);
730     }
731 }
732
733 function remark_off()
734 {
735     $("remark").l_remct = 0;
736     $("remark").className = "remark0";
737 }
738
739
740 function italizer(ga)
741 {
742     var pre, pos;
743     if (ga[0] & 2) 
744         return "<i>"+ga[1]+"</i>";
745     else
746         return ga[1];
747 }
748
749
750 function exitlock_show(num, islock)
751 {
752     g_exitlock = num;
753
754     num = (num < 3 ? num : 3);
755     $("exitlock").src = "img/exitlock"+num+(islock ? "n" : "y")+".png";
756     // alert("EXITLOCK: "+$("exitlock").src);
757     $("exitlock").style.visibility = "visible";
758 }
759
760 var fin = 0;
761
762 //    exitlock_show(0, true);
763
764
765 var chatt_lines = new Array();
766 var chatt_lines_n = 0;
767
768 var CHATT_MAXLINES = 40;
769
770 /* PRO CHATT */
771 function chatt_sub(dt,data,str)
772 {
773     var must_scroll = false;
774     var name;
775     var flags;
776     var isauth;
777
778     flags = data[0];
779     if (flags & 0x02)
780         name = "<i>"+data[1]+"</i>";
781     else
782         name = data[1];
783     // alert ($("txt").scrollTop + parseInt(getStyle($("txt"),"height", "height")) -  $("txt").scrollHeight);
784
785   if ($("txt").scrollTop + parseInt(getStyle($("txt"),"height", "height")) -  $("txt").scrollHeight >= 0)
786       must_scroll = true;
787
788   // alert("ARRIVA NAME: "+ name + "  STR:"+str);
789   if (chatt_lines_n == CHATT_MAXLINES) {
790     $("txt").innerHTML = "";
791     for (i = 0 ; i < (CHATT_MAXLINES - 1) ; i++) {
792       chatt_lines[i] = chatt_lines[i+1];
793       $("txt").innerHTML += chatt_lines[i];
794     }
795     chatt_lines[i] = dt+"<b>"+name+"</b> "+str+ "<br>";
796     $("txt").innerHTML += chatt_lines[i];
797   }
798   else {
799     chatt_lines[chatt_lines_n] = dt+"<b>"+name+"</b> "+str+ "<br>";
800     $("txt").innerHTML += chatt_lines[chatt_lines_n];
801     chatt_lines_n++;
802   }
803   // $("txt").innerHTML;
804
805
806   if (must_scroll) {
807       $("txt").scrollTop = 10000000;
808   }
809   // alert("scTOP "+$("txt").scrollTop+"  scHEIGHT: "+$("txt").scrollHeight+" HEIGHT: "+getStyle($("txt"),"height", "height") );
810 }
811
812 /*
813  *  GESTIONE DEI COOKIES
814  */
815 function createCookie(name,value,hours,path) {
816         if (hours) {
817                 var date = new Date();
818                 date.setTime(date.getTime()+(hours*60*60*1000));
819                 var expires = "; expires="+date.toGMTString();
820         }
821         else var expires = "";
822         document.cookie = name+"="+value+expires+"; path="+path;
823 }
824
825 function readCookie(name) {
826         var nameEQ = name + "=";
827         var ca = document.cookie.split(';');
828         for(var i=0;i < ca.length;i++) {
829                 var c = ca[i];
830                 while (c.charAt(0)==' ') c = c.substring(1,c.length);
831                 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
832         }
833         return null;
834 }
835
836 function eraseCookie(name) {
837         createCookie(name,"",-1);
838 }
839
840 var onunload_times = 0;
841
842
843 function onbeforeunload_cb () {
844     return("");
845 }
846
847 function onunload_cb_old () {
848     var u = 0;
849     
850     //    if (nonunload == true)
851     //     return true;
852     
853     if (onunload_times == 0) {
854         // MLANG "    Vuoi veramente abbandonare la briscola ?\n(clicca annulla o cancel se vuoi ricaricare la briscola)"
855         var res = window.confirm(mlang_commons['brileav'][g_lang]);
856         if (res == true) {
857             the_end = true; 
858             act_shutdown();
859             // while (1) 
860             //  u++;
861         }
862         else {
863             try {
864                 document.location.href = self.location; //  = self.location;
865                 // alert ("passiamo di qui"+self.location);
866                 return (false);
867             } catch (e) {
868                 // MLANG "Ripristino della briscola fallito, per non perdere la sessione ricaricare la pagina manualmente."
869                 alert(mlang_commons['brireco'][g_lang]);
870             }
871         }
872         onunload_times++;
873     }
874     
875     return(false);
876 }
877
878 function onunload_cb () {
879     
880     the_end = true; 
881
882     act_shutdown();
883     
884     return(false);
885 }
886
887
888 function room_checkspace(emme,tables,inpe)
889 {
890     nome = "<b>";
891     for (i = 0 ; i < emme ; i++) 
892         nome += "m";
893     nome += "</b>";
894
895     alta = "";
896     for (i = 0 ; i < 5 ; i++) 
897         alta += nome+"<br>";
898
899     for (i = 0 ; i < tables ; i++) {
900         $("table"+i).innerHTML = alta;
901         // MLANG Mi siedo.
902         $("table_act"+i).innerHTML = "<input type=\"button\" class=\"button\" name=\"xhenter"+i+"\"  value=\""+mlang_commons['btn_sit'][g_lang]+"\" onclick=\"act_sitdown(1);\">";
903         }
904
905     stand = "<table class=\"table_standup\"><tbody><tr>";
906     for (i = 0 ; i < inpe ; i++) {
907         stand += "<td>"+nome+"</td>";
908         if ((i+1) % 4 == 0) {
909             stand += "</tr><tr>";
910         }
911     }
912     stand += "</tr>";
913     $("standup").innerHTML = stand;
914
915     // VERIFY: what is this button ?
916     // MLANG Esco.
917     $("esco").innerHTML = "<input class=\"button\" name=\"logout\" type=\"button\" value=\""+mlang_commons['btn_exit'][g_lang]+"\" onclick=\"act_logout();\" type=\"button\">";
918 }
919
920 function  unescapeHTML(cont) {
921     var div = document.createElement('div');
922     var memo = "";
923     var i;
924
925     div.innerHTML = cont;
926     if (div.childNodes[0]) {
927         if (div.childNodes.length > 1) {
928             if (div.childNodes.toArray)
929                 alert("si puo");
930             else {
931                 var length = div.childNodes.length, results = new Array(length);
932             while (length--)
933                 results[length] = div.childNodes[length];
934                 
935             for (i=0 ; i<results.length ; i++)
936                 memo = memo + results[i].nodeValue;
937             }
938
939             return (memo);
940         }
941         else {
942             return (div.childNodes[0].nodeValue);
943         }
944     }
945     else {
946         return ('');
947     }
948 }
949
950 function playsound(tag, sound) {
951    // g_withflash is a global var
952    if (g_withflash) {
953       $(tag).innerHTML = '<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" '+
954 'codebase="http://active.macromedia.com/flash2/cabs/swflash.cab#version=4,0,0,0" id="mysound" WIDTH=1 HEIGHT=1>' +
955 '<PARAM NAME="movie" VALUE="../playsound.swf"><PARAM NAME="PLAY" VALUE="true"><PARAM NAME="LOOP" VALUE="false">' +
956 '<PARAM NAME=FlashVars VALUE="streamUrl='+sound+'">' +
957 '<EMBED swliveconnect="true" name="mysound" src="../playsound.swf" FlashVars="streamUrl='+sound+'" PLAY="true" LOOP="false" '+
958 ' WIDTH=1 HEIGHT=1 TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></OBJECT>';
959    }
960 }
961
962 function topbanner_init()
963 {
964     setInterval(topbanner_cb, 666);
965 ;
966 }
967
968 function topbanner_cb()
969 {
970     var a, b;
971
972     a = $('topbanner').style.backgroundColor;
973     b = $('topbanner').style.borderLeftColor;
974
975     $('topbanner').style.backgroundColor = b;
976     $('topbanner').style.borderColor = a+" "+a+" "+a+" "+a;
977
978     // console.log("A: "+a+"  B: "+b);
979 }
980
981 function sidebanner_init()
982 {
983     setInterval(sidebanner_cb, 666);
984 }
985
986 function sidebanner2_init()
987 {
988     setInterval(sidebanner2_cb, 666);
989 }
990
991 function sidebanner_cb()
992 {
993     var a, b;
994
995     a = $('sidebanner').style.backgroundColor;
996     b = $('sidebanner').style.borderLeftColor;
997
998     $('sidebanner').style.backgroundColor = b;
999     $('sidebanner').style.borderColor = a+" "+a+" "+a+" "+a;
1000
1001     // console.log("A: "+a+"  B: "+b);
1002 }
1003
1004 function sidebanner2_cb()
1005 {
1006     var a, b;
1007
1008     a = $('sidebanner2').style.backgroundColor;
1009     b = $('sidebanner2').style.borderLeftColor;
1010
1011     $('sidebanner2').style.backgroundColor = b;
1012     $('sidebanner2').style.borderColor = a+" "+a+" "+a+" "+a;
1013
1014     // console.log("A: "+a+"  B: "+b);
1015 }
1016
1017
1018 function langtolng(lang)
1019 {
1020     if (lang == "en")
1021         return ("-en");
1022     else
1023         return ("");
1024 }
1025
1026 function formtext_hilite(obj)
1027 {
1028     obj.className = 'input_text';
1029     addEvent(obj, "focus", function () { this.className = 'input_text_hi'; });
1030     addEvent(obj, "blur",  function () { this.className = 'input_text'; });
1031 }
1032
1033 function formsub_hilite(obj)
1034 {
1035     obj.className = 'input_sub';
1036     addEvent(obj, "focus", function () { this.className = 'input_sub_hi'; });
1037     addEvent(obj, "blur",  function () { this.className = 'input_sub'; });
1038 }
1039
1040 // return the value of the radio button that is checked
1041 // return an empty string if none are checked, or
1042 // there are no radio buttons
1043 function get_checked_value(radioObj) {
1044         if(!radioObj)
1045                 return "";
1046         var radioLength = radioObj.length;
1047         if(radioLength == undefined)
1048                 if(radioObj.checked)
1049                         return radioObj.value;
1050                 else
1051                         return "";
1052         for(var i = 0; i < radioLength; i++) {
1053                 if(radioObj[i].checked) {
1054                         return radioObj[i].value;
1055                 }
1056         }
1057         return "";
1058 }
1059
1060 // set the radio button with the given value as being checked
1061 // do nothing if there are no radio buttons
1062 // if the given value does not exist, all the radio buttons
1063 // are reset to unchecked
1064 function set_checked_value(radioObj, newValue) {
1065         if(!radioObj)
1066                 return;
1067         var radioLength = radioObj.length;
1068         if(radioLength == undefined) {
1069                 radioObj.checked = (radioObj.value == newValue.toString());
1070                 return;
1071         }
1072         for(var i = 0; i < radioLength; i++) {
1073                 radioObj[i].checked = false;
1074                 if(radioObj[i].value == newValue.toString()) {
1075                         radioObj[i].checked = true;
1076                 }
1077         }
1078 }