set and get checked value func. management added
[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     xhr_wr.send(null);
256
257     if (!is_conn) {
258         if (xhr_wr.responseText != null) {
259             eval(xhr_wr.responseText);
260         }
261     }
262 }
263
264 /*
265   request to server
266   server_request([arg0=arg1[, arg2=arg3[, ...]]])
267  */
268 function server_request()
269 {
270     var xhr_wr = createXMLHttpRequest();
271     var i, collect = "";
272
273     if (arguments.length > 0) {
274         for (i = 0 ; i < arguments.length ; i+= 2) {
275             collect += (i == 0 ? "" : "&") + arguments[i] + "=" + encodeURIComponent(arguments[i+1]);
276         }
277     }
278     // alert("Args: "+arguments.length);
279
280     var is_conn = (sess == "not_connected" ? false : true);
281     
282     // console.log("server_request:preresp: "+xhr_wr.responseText);
283
284     xhr_wr.open('GET', 'index_wr.php?'+(is_conn ? 'sess='+sess+'&' : '')+collect, false);
285     xhr_wr.onreadystatechange = function() { return; };
286     xhr_wr.send(null);
287     
288     if (xhr_wr.responseText != null) {
289         // console.log("server_request:resp: "+xhr_wr.responseText);
290         return (xhr_wr.responseText);
291     } 
292     else
293         return (null);
294 }
295
296 /* Stat: CHAT and TABLE */
297
298 function chatt_checksend(obj,e)
299 {
300     var keynum;
301     var keychar;
302     var numcheck;
303
304     if(window.event) { // IE
305         keynum = e.keyCode;
306     }
307     else if(e.which) { // Netscape/Firefox/Opera
308         keynum = e.which;
309     }
310     // alert("OBJ: "+obj);
311     if (keynum == 13 && obj.value != "") { // Enter
312         act_chatt(obj.value);
313         obj.value = "";
314     }
315 }
316 function act_chatt(value)
317 {
318     send_mesg("chatt|"+encodeURIComponent(value));
319     /*
320     obj.disabled = true;
321     obj.value = "";
322     obj.disabled = false;
323     obj.focus();
324     */
325     return false;
326 }
327
328 /* Stat: ROOM */
329 function act_sitdown(table)
330 {
331     send_mesg("sitdown|"+table);
332 }
333
334 function act_wakeup()
335 {
336     send_mesg("wakeup");
337 }
338
339 function act_splash()
340 {
341     send_mesg("splash");
342 }
343
344 function act_help()
345 {
346     send_mesg("help");
347 }
348
349 function act_passwdhowto()
350 {
351     send_mesg("passwdhowto");
352 }
353
354 function act_mesgtoadm()
355 {
356     send_mesg("mesgtoadm");
357 }
358
359 function act_tav()
360 {
361     act_chatt('/tav '+$('txt_in').value); 
362     $('txt_in').value = '';
363 }
364
365 function act_about()
366 {
367     send_mesg("about");
368 }
369
370 function act_placing()
371 {
372     send_mesg("placing");
373 }
374
375 function act_roadmap()
376 {
377     send_mesg("roadmap");
378 }
379
380 function act_whysupport()
381 {
382     send_mesg("whysupport");
383 }
384
385 function act_lascio()
386 {
387     send_mesg("lascio");
388 }
389
390 function safelascio()
391 {
392     var res;
393     // MLANG "Sei sicuro di volere lasciare questa mano?"
394     res = window.confirm(mlang_commons['gamleav'][g_lang]);
395     if (res)
396         act_lascio();
397 }
398
399 function act_logout(exitlock)
400 {
401     send_mesg("logout|"+exitlock);
402 }
403
404 function act_reloadroom()
405 {
406     window.onunload = null;
407     window.onbeforeunload = null;
408     document.location.assign("index.php");
409 }
410
411 function act_shutdown()
412 {
413     var c = 0;
414
415     send_mesg("shutdown");
416     // while (xhr_wr.readyState != 4)
417     //  c++;
418 }
419
420 function postact_logout()
421 {
422     // alert("postact_logout");
423     try { 
424         xhr_rd.abort();
425     } catch (e) {}
426
427     // eraseCookie("sess");
428     document.location.assign("index.php");
429 }
430
431 /*
432   function slowimg(img,x1,y1,deltat,free,action,srcend)
433   img    - image to move
434   x1,y1  - destination coords
435   deltat - time for each frame (in msec)
436   free   - when the release the local block for other operations (range: 0 - 1)
437   action - function to run when the image is moved
438   srcend - image to switch when the image is moved
439 */
440
441 function sleep(st, delay)
442 {
443     // alert("LOC_NEW PRE: "+st.st_loc_new);
444
445     st.st_loc_new++;
446
447     setTimeout(function(obj){ if (obj.st_loc_new > obj.st_loc) { obj.st_loc++; }},
448                delay, st);
449 }
450
451 function slowimg(img,x1,y1,deltat,free,action,srcend) {
452     this.img = img;
453
454     // this.x0  = parseInt(document.defaultView.getComputedStyle(this.img, "").getPropertyValue("left"));
455     this.x0 = parseInt(getStyle(this.img,"left", "left"));
456 // alert("img.x0 = "+this.x0);
457     // this.y0  = parseInt(document.defaultView.getComputedStyle(this.img, "").getPropertyValue("top"));
458     this.y0  = parseInt(getStyle(this.img,"top", "top"));
459     this.x1  = x1;
460     this.y1  = y1;
461     this.deltat = deltat;
462     this.free = free;
463     this.action = action;
464     this.srcend = srcend;
465 }
466
467 slowimg.prototype = {
468     img: null, 
469     st: null,
470     x0: 0,
471     y0: 0,
472     x1: 0,
473     y1: 0,
474     dx: 0,
475     dy: 0,
476     free: 0,
477     step_n:    0,
478     step_cur:  0,
479     step_free: 0,
480     time:      0,
481     deltat:   40,
482     tout: 0,
483     action: null,
484     srcend: null,
485     
486     setstart: function(x0,y0)
487     {
488         this.x0 = x0;
489         this.y0 = y0;
490     },
491     
492     setaction: function(act)
493     {
494         this.action = act;
495     },
496     
497
498     settime: function(time) 
499     {
500         this.time = (time < this.deltat ? this.deltat : time);
501         this.step_n = parseInt(this.time / this.deltat);
502         this.dx = (this.x1 - this.x0) / this.step_n;
503         this.dy = (this.y1 - this.y0) / this.step_n;
504         if (this.step_n * this.deltat == this.time) {
505             this.step_n--;
506         }
507         if (this.free < 1) {
508             this.step_free = parseInt(this.step_n * this.free);
509         }
510     },
511     
512     start: function(st)
513     {
514         // $("logz").innerHTML += "               xxxxxxxxxxxxxxxxxxxxxSTART<br>";
515         this.st = st;
516         this.st.st_loc_new++;
517         
518         this.img.style.visibility = "visible";
519         setTimeout(function(obj){ obj.animate(); }, this.deltat, this);
520     },
521     
522     animate: function()
523     {
524         // $("log").innerHTML = "Val " + this.step_cur + " N: " + this.step_n + "<br>";
525         if (this.step_cur == 0) {
526             var date = new Date();
527             // $("logz").innerHTML = "Timestart: " + date + "<br>";
528         }
529         if (this.step_cur <= this.step_n) {
530             this.img.style.left = this.x0 + this.dx * this.step_cur;
531             this.img.style.top  = this.y0 + this.dy * this.step_cur;
532             this.step_cur++;
533             setTimeout(function(obj){ obj.animate(); }, this.deltat, this);
534             if (this.step_cur == this.step_free && this.st != null) {
535                 if (this.st.st_loc < this.st.st_loc_new) {
536                     // alert("QUI1  " + this.step_cur + "  ZZ  "+  this.step_free);
537                     this.st.st_loc++;
538                     this.st = null;
539                 }
540             }
541         }
542         else {
543             this.img.style.left = this.x1;
544             this.img.style.top  = this.y1;
545             // $("logz").innerHTML += "xxxxxxxxxxxxxxxCLEAR<br>";
546             var date = new Date();
547             // $("logz").innerHTML += "Timestop: " + date + "<br>";
548
549             if (this.action != null) {
550                 eval(this.action);
551             }
552
553             if (this.st != null && this.st.st_loc < this.st.st_loc_new) {
554                 // alert("QUI2");
555                 this.st.st_loc++;
556                 this.st = null;
557             }
558             if (this.srcend != null) {
559                 this.img.src = this.srcend;
560             }
561         }
562     }
563 }
564
565 function div_show(div)
566 {
567     div.style.top = parseInt((document.body.clientHeight - parseInt(getStyle(div,"height", "height"))) / 2) + document.body.scrollTop;
568     div.style.visibility = "visible";
569 }
570
571 function notify_ex(st, text, tout, butt, w, h, is_opa, block_time)
572 {
573     var clo, box;
574     var t = this;
575     
576     this.st = st;
577
578     this.ancestor = document.body;
579     
580     this.st.st_loc_new++;
581
582     clo = document.createElement("input");
583     clo.type = "submit";
584     clo.className = "button";
585     clo.style.bottom = "4px";
586     clo.obj = this;
587     if (block_time > 0) {
588         clo.value = "leggere, prego.";
589         this.butt = butt;
590     }
591     else {
592         clo.value = butt;
593         clo.onclick = this.input_hide;
594     }
595
596     clodiv = document.createElement("div");
597     clodiv.className = "notify_clo";
598     this.clo = clo;
599     this.clodiv = clodiv;
600
601     clodiv.appendChild(clo);
602
603     cont = document.createElement("div");
604
605     cont.style.borderBottomStyle = "solid";
606     cont.style.borderBottomWidth = "1px";
607     cont.style.borderBottomColor = "gray";
608     cont.style.height = (h - 30)+"px";
609     cont.style.overflow = "auto";
610     cont.innerHTML = text;
611
612     box =  document.createElement("div");
613     if (is_opa)
614         box.className = "notify_opaque";
615     else
616         box.className = "notify";
617
618     box.style.zIndex = 200;
619     box.style.width  = w+"px";
620     box.style.marginLeft  = -parseInt(w/2)+"px";
621     box.style.height = h+"px";
622     box.style.top = parseInt((document.body.clientHeight - h) / 2) + document.body.scrollTop;
623     box.appendChild(cont);
624     box.appendChild(clodiv);
625     box.style.visibility = "visible";
626
627     this.notitag = box;
628     
629     this.ancestor.appendChild(box);
630     
631     this.toutid = setTimeout(function(obj){ obj.unblock(); }, tout, this);
632
633     if (block_time != 0) {
634         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);
635     }
636     else {
637         formsub_hilite(clo);
638         clo.focus();
639     }
640
641 }
642
643
644 notify_ex.prototype = {
645     ancestor: null,
646     st: null,
647     notitag: null,
648     toutid: null,
649     clo: null,
650     clodiv: null, 
651     butt: null,
652     tblkid: null,
653
654     unblock: function()
655     {
656         if (this.st.st_loc < this.st.st_loc_new) {
657             this.st.st_loc++;
658         }
659     },
660     
661     hide: function()
662     {
663         clearTimeout(this.toutid);
664         this.ancestor.removeChild(this.notitag);
665         this.unblock();
666     },
667
668     input_hide: function()
669     {
670         clearTimeout(this.obj.toutid);
671         this.obj.ancestor.removeChild(this.obj.notitag);
672         this.obj.unblock();
673     }
674 }
675
676
677 notify.prototype = notify_ex.prototype;                // Define sub-class
678 notify.prototype.constructor = notify;
679 notify.baseConstructor = notify_ex;
680 notify.superClass = notify_ex.prototype;
681
682 function notify(st, text, tout, butt, w, h)
683 {
684     notify_ex.call(this, st, text, tout, butt, w, h, false, 0);
685 }
686         
687
688 function $(id) { 
689     return document.getElementById(id); 
690 }
691
692 function globst() {
693     this.st = -1;
694     this.st_loc = -1;
695     this.st_loc_new = -1;
696     this.comms  = new Array;
697 }
698
699
700
701 function remark_step()
702 {
703     var ct = $("remark").l_remct;
704     
705     if (ct != 0) {
706         ct++;
707         if (ct > 2)
708             ct = 1;
709         $("remark").className = "remark"+ct;
710         $("remark").l_remct = ct;
711         setTimeout(remark_step,500);
712     }
713     else
714         $("remark").className = "remark0";
715     
716     return;
717 }
718
719 function remark_on()
720 {
721     if ($("remark").l_remct == 0) {
722         $("remark").l_remct = 1;
723         setTimeout(remark_step,500);
724     }
725 }
726
727 function remark_off()
728 {
729     $("remark").l_remct = 0;
730     $("remark").className = "remark0";
731 }
732
733
734 function italizer(ga)
735 {
736     var pre, pos;
737     if (ga[0] & 2) 
738         return "<i>"+ga[1]+"</i>";
739     else
740         return ga[1];
741 }
742
743
744 function exitlock_show(num, islock)
745 {
746     g_exitlock = num;
747
748     num = (num < 3 ? num : 3);
749     $("exitlock").src = "img/exitlock"+num+(islock ? "n" : "y")+".png";
750     // alert("EXITLOCK: "+$("exitlock").src);
751     $("exitlock").style.visibility = "visible";
752 }
753
754 var fin = 0;
755
756 //    exitlock_show(0, true);
757
758
759 var chatt_lines = new Array();
760 var chatt_lines_n = 0;
761
762 var CHATT_MAXLINES = 40;
763
764 /* PRO CHATT */
765 function chatt_sub(dt,data,str)
766 {
767     var must_scroll = false;
768     var name;
769     var flags;
770     var isauth;
771
772     flags = data[0];
773     if (flags & 0x02)
774         name = "<i>"+data[1]+"</i>";
775     else
776         name = data[1];
777     // alert ($("txt").scrollTop + parseInt(getStyle($("txt"),"height", "height")) -  $("txt").scrollHeight);
778
779   if ($("txt").scrollTop + parseInt(getStyle($("txt"),"height", "height")) -  $("txt").scrollHeight >= 0)
780       must_scroll = true;
781
782   // alert("ARRIVA NAME: "+ name + "  STR:"+str);
783   if (chatt_lines_n == CHATT_MAXLINES) {
784     $("txt").innerHTML = "";
785     for (i = 0 ; i < (CHATT_MAXLINES - 1) ; i++) {
786       chatt_lines[i] = chatt_lines[i+1];
787       $("txt").innerHTML += chatt_lines[i];
788     }
789     chatt_lines[i] = dt+"<b>"+name+"</b> "+str+ "<br>";
790     $("txt").innerHTML += chatt_lines[i];
791   }
792   else {
793     chatt_lines[chatt_lines_n] = dt+"<b>"+name+"</b> "+str+ "<br>";
794     $("txt").innerHTML += chatt_lines[chatt_lines_n];
795     chatt_lines_n++;
796   }
797   // $("txt").innerHTML;
798
799
800   if (must_scroll) {
801       $("txt").scrollTop = 10000000;
802   }
803   // alert("scTOP "+$("txt").scrollTop+"  scHEIGHT: "+$("txt").scrollHeight+" HEIGHT: "+getStyle($("txt"),"height", "height") );
804 }
805
806 /*
807  *  GESTIONE DEI COOKIES
808  */
809 function createCookie(name,value,hours,path) {
810         if (hours) {
811                 var date = new Date();
812                 date.setTime(date.getTime()+(hours*60*60*1000));
813                 var expires = "; expires="+date.toGMTString();
814         }
815         else var expires = "";
816         document.cookie = name+"="+value+expires+"; path="+path;
817 }
818
819 function readCookie(name) {
820         var nameEQ = name + "=";
821         var ca = document.cookie.split(';');
822         for(var i=0;i < ca.length;i++) {
823                 var c = ca[i];
824                 while (c.charAt(0)==' ') c = c.substring(1,c.length);
825                 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
826         }
827         return null;
828 }
829
830 function eraseCookie(name) {
831         createCookie(name,"",-1);
832 }
833
834 var onunload_times = 0;
835
836
837 function onbeforeunload_cb () {
838     return("");
839 }
840
841 function onunload_cb_old () {
842     var u = 0;
843     
844     //    if (nonunload == true)
845     //     return true;
846     
847     if (onunload_times == 0) {
848         // MLANG "    Vuoi veramente abbandonare la briscola ?\n(clicca annulla o cancel se vuoi ricaricare la briscola)"
849         var res = window.confirm(mlang_commons['brileav'][g_lang]);
850         if (res == true) {
851             the_end = true; 
852             act_shutdown();
853             // while (1) 
854             //  u++;
855         }
856         else {
857             try {
858                 document.location.href = self.location; //  = self.location;
859                 // alert ("passiamo di qui"+self.location);
860                 return (false);
861             } catch (e) {
862                 // MLANG "Ripristino della briscola fallito, per non perdere la sessione ricaricare la pagina manualmente."
863                 alert(mlang_commons['brireco'][g_lang]);
864             }
865         }
866         onunload_times++;
867     }
868     
869     return(false);
870 }
871
872 function onunload_cb () {
873     
874     the_end = true; 
875
876     act_shutdown();
877     
878     return(false);
879 }
880
881
882 function room_checkspace(emme,tables,inpe)
883 {
884     nome = "<b>";
885     for (i = 0 ; i < emme ; i++) 
886         nome += "m";
887     nome += "</b>";
888
889     alta = "";
890     for (i = 0 ; i < 5 ; i++) 
891         alta += nome+"<br>";
892
893     for (i = 0 ; i < tables ; i++) {
894         $("table"+i).innerHTML = alta;
895         // MLANG Mi siedo.
896         $("table_act"+i).innerHTML = "<input type=\"button\" class=\"button\" name=\"xhenter"+i+"\"  value=\""+mlang_commons['btn_sit'][g_lang]+"\" onclick=\"act_sitdown(1);\">";
897         }
898
899     stand = "<table class=\"table_standup\"><tbody><tr>";
900     for (i = 0 ; i < inpe ; i++) {
901         stand += "<td>"+nome+"</td>";
902         if ((i+1) % 4 == 0) {
903             stand += "</tr><tr>";
904         }
905     }
906     stand += "</tr>";
907     $("standup").innerHTML = stand;
908
909     // VERIFY: what is this button ?
910     // MLANG Esco.
911     $("esco").innerHTML = "<input class=\"button\" name=\"logout\" type=\"button\" value=\""+mlang_commons['btn_exit'][g_lang]+"\" onclick=\"act_logout();\" type=\"button\">";
912 }
913
914 function  unescapeHTML(cont) {
915     var div = document.createElement('div');
916     var memo = "";
917     var i;
918
919     div.innerHTML = cont;
920     if (div.childNodes[0]) {
921         if (div.childNodes.length > 1) {
922             if (div.childNodes.toArray)
923                 alert("si puo");
924             else {
925                 var length = div.childNodes.length, results = new Array(length);
926             while (length--)
927                 results[length] = div.childNodes[length];
928                 
929             for (i=0 ; i<results.length ; i++)
930                 memo = memo + results[i].nodeValue;
931             }
932
933             return (memo);
934         }
935         else {
936             return (div.childNodes[0].nodeValue);
937         }
938     }
939     else {
940         return ('');
941     }
942 }
943
944 function playsound(tag, sound) {
945    // g_withflash is a global var
946    if (g_withflash) {
947       $(tag).innerHTML = '<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" '+
948 'codebase="http://active.macromedia.com/flash2/cabs/swflash.cab#version=4,0,0,0" id="mysound" WIDTH=1 HEIGHT=1>' +
949 '<PARAM NAME="movie" VALUE="../playsound.swf"><PARAM NAME="PLAY" VALUE="true"><PARAM NAME="LOOP" VALUE="false">' +
950 '<PARAM NAME=FlashVars VALUE="streamUrl='+sound+'">' +
951 '<EMBED swliveconnect="true" name="mysound" src="../playsound.swf" FlashVars="streamUrl='+sound+'" PLAY="true" LOOP="false" '+
952 ' WIDTH=1 HEIGHT=1 TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></OBJECT>';
953    }
954 }
955
956 function topbanner_init()
957 {
958     setInterval(topbanner_cb, 666);
959 ;
960 }
961
962 function topbanner_cb()
963 {
964     var a, b;
965
966     a = $('topbanner').style.backgroundColor;
967     b = $('topbanner').style.borderLeftColor;
968
969     $('topbanner').style.backgroundColor = b;
970     $('topbanner').style.borderColor = a+" "+a+" "+a+" "+a;
971
972     // console.log("A: "+a+"  B: "+b);
973 }
974
975 function sidebanner_init()
976 {
977     setInterval(sidebanner_cb, 666);
978 }
979
980 function sidebanner2_init()
981 {
982     setInterval(sidebanner2_cb, 666);
983 }
984
985 function sidebanner_cb()
986 {
987     var a, b;
988
989     a = $('sidebanner').style.backgroundColor;
990     b = $('sidebanner').style.borderLeftColor;
991
992     $('sidebanner').style.backgroundColor = b;
993     $('sidebanner').style.borderColor = a+" "+a+" "+a+" "+a;
994
995     // console.log("A: "+a+"  B: "+b);
996 }
997
998 function sidebanner2_cb()
999 {
1000     var a, b;
1001
1002     a = $('sidebanner2').style.backgroundColor;
1003     b = $('sidebanner2').style.borderLeftColor;
1004
1005     $('sidebanner2').style.backgroundColor = b;
1006     $('sidebanner2').style.borderColor = a+" "+a+" "+a+" "+a;
1007
1008     // console.log("A: "+a+"  B: "+b);
1009 }
1010
1011
1012 function langtolng(lang)
1013 {
1014     if (lang == "en")
1015         return ("-en");
1016     else
1017         return ("");
1018 }
1019
1020 function formtext_hilite(obj)
1021 {
1022     obj.className = 'input_text';
1023     addEvent(obj, "focus", function () { this.className = 'input_text_hi'; });
1024     addEvent(obj, "blur",  function () { this.className = 'input_text'; });
1025 }
1026
1027 function formsub_hilite(obj)
1028 {
1029     obj.className = 'input_sub';
1030     addEvent(obj, "focus", function () { this.className = 'input_sub_hi'; });
1031     addEvent(obj, "blur",  function () { this.className = 'input_sub'; });
1032 }
1033
1034 // return the value of the radio button that is checked
1035 // return an empty string if none are checked, or
1036 // there are no radio buttons
1037 function get_checked_value(radioObj) {
1038         if(!radioObj)
1039                 return "";
1040         var radioLength = radioObj.length;
1041         if(radioLength == undefined)
1042                 if(radioObj.checked)
1043                         return radioObj.value;
1044                 else
1045                         return "";
1046         for(var i = 0; i < radioLength; i++) {
1047                 if(radioObj[i].checked) {
1048                         return radioObj[i].value;
1049                 }
1050         }
1051         return "";
1052 }
1053
1054 // set the radio button with the given value as being checked
1055 // do nothing if there are no radio buttons
1056 // if the given value does not exist, all the radio buttons
1057 // are reset to unchecked
1058 function set_checked_value(radioObj, newValue) {
1059         if(!radioObj)
1060                 return;
1061         var radioLength = radioObj.length;
1062         if(radioLength == undefined) {
1063                 radioObj.checked = (radioObj.value == newValue.toString());
1064                 return;
1065         }
1066         for(var i = 0; i < radioLength; i++) {
1067                 radioObj[i].checked = false;
1068                 if(radioObj[i].value == newValue.toString()) {
1069                         radioObj[i].checked = true;
1070                 }
1071         }
1072 }