aggiunta cvs var id.
[brisk.git] / web / commons.js
1 /*
2  *  brisk - commons.js
3  *
4  *  Copyright (C) 2006 matteo.nastasi@milug.org
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * General Public License for more details. You should have received a
15  * copy of the GNU General Public License along with this program; if
16  * not, write to the Free Software Foundation, Inc, 59 Temple Place -
17  * Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * $Id$
20  *
21  */
22
23 var PLAYERS_N = 3;
24
25 function $(id) { return document.getElementById(id); }
26
27 /* replacement of setInterval on IE */
28 (function(){
29     /*if not IE, do nothing*/
30     if(!document.uniqueID){return;};
31
32     /*Copy the default setInterval behavior*/
33     var nativeSetInterval = window.setInterval;
34     window.setInterval = function(fn,ms) {              
35         var param = [];
36         if(arguments.length <= 2)       {
37             return nativeSetInterval(fn,ms);
38         }
39         else {
40             for(var i=2;i<arguments.length;i+=1) {
41                 param[i-2] =  arguments[i];
42             }   
43         }
44         
45         if(typeof(fn)=='function') {
46             
47             return (function (fn,ms,param) {
48                 var fo = function () {                                                          
49                     fn.apply(window,param);
50                 };                      
51                 return nativeSetInterval(fo,ms); 
52             })(fn,ms,param);
53         }
54         else if(typeof(fn)=='string')
55         {
56             return  nativeSetInterval(fn,ms);
57         }
58         else
59         {
60             throw Error('setInterval Error\nInvalid function type');
61         };
62     };
63 })()
64
65     // var card_pos = RANGE 0 <= x < cards_ea_n
66
67 function rnd_int(min, max) {
68   return Math.floor(Math.random() * (max - min + 1) + min);
69 }
70
71 function error_images()
72 {
73     alert("GHESEMU!");
74 }
75
76 function abort_images()
77 {
78     alert("ABORTAIMAGES");
79 }
80
81 function unload_images()
82 {
83     alert("ABORTAIMAGES");
84 }
85
86 function reset_images()
87 {
88     alert("ABORTAIMAGES");
89 }
90
91 function update_images()
92 {
93     $("imgct").innerHTML = "Immagini caricate "+g_preload_imgsz_arr[g_imgct]+"%.";
94     if (g_imgct < g_preload_img_arr.length)
95         setTimeout(preload_images, 100, g_preload_img_arr, g_imgct);
96     g_imgct++;
97     // $("imgct").innerHTML += "U";
98 }
99
100 function preload_images(arr,idx)
101 {
102     var im = new Image;
103     
104     // $("imgct").innerHTML = "Stiamo caricando "+arr[idx]+"%.<br>";
105     im.onload =   update_images;
106     im.onerror =  error_images;
107     im.onabort =  abort_images;
108     im.onunload = unload_images;
109     im.onreset =  reset_images;
110     im.src =      arr[idx];
111     // $("imgct").innerHTML += "P";
112 }
113
114 function safestatus(a)
115 {
116     try{
117         return (a.status);
118     } catch(b)
119         { return (-1); }
120 }
121
122 function createXMLHttpRequest() {
123     try { return new ActiveXObject("Msxml2.XMLHTTP");    } catch(e) {}
124     try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {}
125     try { return new XMLHttpRequest();                   } catch(e) {}
126     alert("XMLHttpRequest not supported");
127     return null;
128 }
129
130 function send_mesg(mesg)
131 {
132     var xhr_wr = createXMLHttpRequest();
133     // xhr_wr.open('GET', 'index_wr.php?sess='+sess+'&mesg='+encodeURIComponent(mesg), true);
134     xhr_wr.open('GET', 'index_wr.php?sess='+sess+'&mesg='+mesg, true);
135     xhr_wr.onreadystatechange = function() { return; };
136     xhr_wr.send(null);
137
138 }
139
140 /* Stat: CHAT and TABLE */
141
142 function chatt_checksend(obj,e)
143 {
144     var keynum;
145     var keychar;
146     var numcheck;
147
148     if(window.event) { // IE
149         keynum = e.keyCode;
150     }
151     else if(e.which) { // Netscape/Firefox/Opera
152         keynum = e.which;
153     }
154     // alert("OBJ: "+obj);
155     if (keynum == 13 && obj.value != "") { // Enter
156         act_chatt(obj.value);
157         obj.value = "";
158     }
159 }
160 function act_chatt(value)
161 {
162     send_mesg("chatt|"+encodeURIComponent(value));
163     /*
164     obj.disabled = true;
165     obj.value = "";
166     obj.disabled = false;
167     obj.focus();
168     */
169     return false;
170 }
171
172 /* Stat: ROOM */
173 function act_sitdown(table)
174 {
175     send_mesg("sitdown|"+table);
176 }
177
178 function act_wakeup()
179 {
180     send_mesg("wakeup");
181 }
182
183 /* Stat: TABLE  Subst: ASTA */
184 function act_asta(card,pnt)
185 {
186     send_mesg("asta|"+card+"|"+pnt);
187 }
188
189 function act_choose(card)
190 {
191     // alert("sitdown");
192     send_mesg("choose|"+card);
193 }
194
195 /* Stat: TABLE  Subst: GAME */
196 function act_play(card,x,y)
197 {
198     // alert("sitdown");
199     send_mesg("play|"+card+"|"+x+"|"+y);
200 }
201
202 function act_tableinfo()
203 {
204     send_mesg("tableinfo");
205 }
206
207 function act_logout()
208 {
209     send_mesg("logout");
210 }
211
212 function act_preout()
213 {
214     act_logout();
215 }
216
217 function postact_logout()
218 {
219     // alert("postact_logout");
220     try { 
221         xhr_rd.abort();
222     } catch (e) {}
223
224     eraseCookie("sess");
225     document.location.assign("index.php");
226 }
227
228 /*
229   function slowimg(img,x1,y1,deltat,free,action,srcend)
230   img    - image to move
231   x1,y1  - destination coords
232   deltat - time for each frame (in msec)
233   free   - when the release the local block for other operations (range: 0 - 1)
234   action - function to run when the image is moved
235   srcend - image to switch when the image is moved
236 */
237
238 function sleep(st, delay)
239 {
240     // alert("LOC_NEW PRE: "+st.st_loc_new);
241
242     st.st_loc_new++;
243
244     setTimeout(function(obj){ if (obj.st_loc_new > obj.st_loc) { obj.st_loc++; }},
245                delay, st);
246 }
247
248 function slowimg(img,x1,y1,deltat,free,action,srcend) {
249     this.img = img;
250
251     this.x0  = parseInt(window.getComputedStyle(this.img, "").getPropertyValue("left"));
252     // alert("img.x0 = "+this.x0);
253     this.y0  = parseInt(window.getComputedStyle(this.img, "").getPropertyValue("top"));
254     this.x1  = x1;
255     this.y1  = y1;
256     this.deltat = deltat;
257     this.free = free;
258     this.action = action;
259     this.srcend = srcend;
260 }
261
262 slowimg.prototype = {
263     img: null, 
264     st: null,
265     x0: 0,
266     y0: 0,
267     x1: 0,
268     y1: 0,
269     dx: 0,
270     dy: 0,
271     free: 0,
272     step_n:    0,
273     step_cur:  0,
274     step_free: 0,
275     time:      0,
276     deltat:   40,
277     tout: 0,
278     action: null,
279     srcend: null,
280     
281     setstart: function(x0,y0)
282     {
283         this.x0 = x0;
284         this.y0 = y0;
285     },
286     
287     setaction: function(act)
288     {
289         this.action = act;
290     },
291     
292     settime: function(time) 
293     {
294         this.time = time;
295         this.step_n = parseInt(time / this.deltat);
296         this.dx = (this.x1 - this.x0) / this.step_n;
297         this.dy = (this.y1 - this.y0) / this.step_n;
298         if (this.step_n * this.deltat == time) {
299             this.step_n--;
300         }
301         this.step_free = parseInt(this.step_n * this.free);
302     },
303     
304     start: function(st)
305     {
306         // $("logz").innerHTML += "               xxxxxxxxxxxxxxxxxxxxxSTART<br>";
307         this.st = st;
308         this.st.st_loc_new++;
309         
310         this.img.style.visibility = "visible";
311         setTimeout(function(obj){ obj.animate(); }, this.deltat, this);
312     },
313     
314     animate: function()
315     {
316         // $("log").innerHTML = "Val " + this.step_cur + " N: " + this.step_n + "<br>";
317         if (this.step_cur == 0) {
318             var date = new Date();
319             // $("logz").innerHTML = "Timestart: " + date + "<br>";
320         }
321         if (this.step_cur <= this.step_n) {
322             this.img.style.left = this.x0 + this.dx * this.step_cur;
323             this.img.style.top  = this.y0 + this.dy * this.step_cur;
324             this.step_cur++;
325             setTimeout(function(obj){ obj.animate(); }, this.deltat, this);
326             if (this.step_cur == this.step_free && this.st != null) {
327                 if (this.st != null && this.st.st_loc < this.st.st_loc_new) {
328                     // alert("QUI1  " + this.step_cur + "  ZZ  "+  this.step_free);
329                     this.st.st_loc++;
330                     this.st = null;
331                 }
332             }
333         }
334         else {
335             this.img.style.left = this.x1;
336             this.img.style.top  = this.y1;
337             // $("logz").innerHTML += "xxxxxxxxxxxxxxxCLEAR<br>";
338             var date = new Date();
339             // $("logz").innerHTML += "Timestop: " + date + "<br>";
340             if (this.st != null && this.st.st_loc < this.st.st_loc_new) {
341                 // alert("QUI2");
342                 this.st.st_loc++;
343                 this.st = null;
344             }
345             if (this.action != null) {
346                 eval(this.action);
347             }
348             if (this.srcend != null) {
349                 this.img.src = this.srcend;
350             }
351         }
352     }
353 }
354
355 var asta_xarr = new Array(0,66,133);
356
357 /* TODO: impostare gli onclick */
358 function dispose_asta(idx, pnt)
359 {
360     var i, btn, pass;
361     
362     var btn;
363     for (i = 0 ; i < 10 ; i++) {
364         btn = $("asta"+i);
365         if (i < idx) {
366             btn.src = "img/astapasso"+(pnt >= 0 ? "" : "_ro")+".png";
367             pass = -1;
368         }
369         else {
370             btn.src = "img/asta"+i+(pnt >= 0 ? "" : "_ro")+".png";
371             pass = i;
372         }
373         if (i < 19)
374             btn.style.left = asta_xarr[i % 3];
375         else
376             btn.style.left = asta_xarr[(i+1) % 3];
377         
378         btn.style.top  = parseInt(i / 3) * 50+1;
379         // btn.style.visibility  = "visible";
380         
381         if (pnt >= 0)
382             eval("btn.onclick = function () { act_asta("+pass+",61); }");
383         else
384             btn.onclick = null;
385     }
386     
387     
388     btn = $("astaptdiv");
389     btn.style.left = asta_xarr[i % 3];
390     btn.style.top = parseInt(i / 3) * 50;
391     // btn.style.visibility  = "visible";
392     
393     btn = $("astapt");
394     var rpnt = (pnt < 0 ? -pnt : pnt);
395     btn.value = (rpnt < 61 ? 61 : (rpnt > 120 ? 120 : rpnt));
396     
397     btn = $("astaptsub");
398     btn.style.left = asta_xarr[i % 3];
399     btn.style.top = 25 + parseInt(i / 3) * 50;;
400     btn.src = "img/astaptsub"+(pnt >= 0 ? "" : "_ro")+".png";
401     // btn.style.visibility  = "visible";
402     if (pnt >= 0)
403         btn.onclick = function () { act_asta(9,$("astapt").value); };
404     else
405         btn.onclick = null;
406     
407     i+=1;
408     btn = $("astapasso2");
409     btn.style.left = asta_xarr[i % 3];
410     btn.style.top = parseInt(i / 3) * 50;;
411     btn.src = "img/astapasso"+(pnt >= 0 ? "" : "_ro")+".png";
412     // btn.style.visibility  = "visible";
413     if (pnt >= 0)
414         btn.onclick = function () { act_asta(-1,0); };
415     else
416         btn.onclick = null;
417     $("asta").style.visibility = "visible";
418 }
419
420 function hide_asta()
421 {
422     $("asta").style.visibility = "hidden"; 
423 }
424
425 function notify(st, ancestor, text, tout, butt)
426 {
427     var clo, box;
428     var t = this;
429     
430     this.st = st;
431     this.ancestor = ancestor;
432     
433     this.st.st_loc_new++;
434
435     clo = document.createElement("input");
436     clo.type = "submit";
437     clo.value = butt;
438     clo.obj = this;
439     clo.onclick = this.input_hide;
440
441     box = document.createElement("div");
442     box.className = "notify";
443     box.innerHTML = text;
444     box.style.zIndex = 200;
445     box.appendChild(clo);
446     box.style.visibility = "visible";
447     
448     this.notitag = box;
449     
450     this.ancestor.appendChild(box);
451     
452     this.toutid = setTimeout(function(obj){ obj.unblock(); }, tout, this);
453 }
454
455 notify.prototype = {
456     ancestor: null,
457     st: null,
458     notitag: null,
459     toutid: null,
460     
461     unblock: function()
462     {
463         if (this.st.st_loc < this.st.st_loc_new) {
464             this.st.st_loc++;
465         }
466     },
467     
468     hide: function()
469     {
470         clearTimeout(this.toutid);
471         this.ancestor.removeChild(this.notitag);
472         this.unblock();
473     },
474
475     input_hide: function()
476     {
477         clearTimeout(this.obj.toutid);
478         this.obj.ancestor.removeChild(this.obj.notitag);
479         this.obj.unblock();
480     }
481 }
482         
483
484 function $(id) { 
485     return document.getElementById(id); 
486 }
487
488 function globst() {
489     this.st = -1;
490     this.st_loc = -1;
491     this.st_loc_new = -1;
492     this.comms  = new Array;
493 }
494
495
496
497 function remark_step()
498 {
499     var ct = $("remark").l_remct;
500     
501     if (ct != 0) {
502         ct++;
503         if (ct > 2)
504             ct = 1;
505         $("remark").className = "remark"+ct;
506         $("remark").l_remct = ct;
507         setTimeout(remark_step,500);
508     }
509     else
510         $("remark").className = "remark0";
511     
512     return;
513 }
514
515 function remark_on()
516 {
517     if ($("remark").l_remct == 0) {
518         $("remark").l_remct = 1;
519         setTimeout(remark_step,500);
520     }
521 }
522
523 function remark_off()
524 {
525     $("remark").l_remct = 0;
526     $("remark").className = "remark0";
527 }
528
529
530 function choose_seed(card)
531 {
532     var i;
533
534     $("chooseed").style.visibility = "visible";
535     for (i = 0 ; i < 4 ; i++) {
536         $("seed"+i).src = "img/"+i+""+card+".png";
537         seed=$("seed"+i);
538         eval("seed.onclick = function () { act_choose("+i+""+card+"); };");
539     }
540 }
541
542 function set_names(so,ea,ne,nw,we)
543 {
544 //    alert("EA: "+ea);
545     $("name").innerHTML = so; 
546     $("name_ea").innerHTML = ea;
547     $("name_ne").innerHTML = ne;
548     $("name_nw").innerHTML = nw;
549     $("name_we").innerHTML = we;
550     return;
551 }
552
553 var astat_suffix = new Array("","_ea","_ne","_nw","_we");
554
555 function show_astat(zer,uno,due,tre,qua)
556 {
557     var astat = new Array(zer,uno,due,tre,qua);
558
559     for (i = 0 ; i < PLAYERS_N ; i++) {
560         idx = (PLAYERS_N + i - table_pos) % PLAYERS_N;
561
562         if (astat[i] == -2) {
563             $("public"+astat_suffix[idx]).style.visibility = "hidden";
564         }
565         else if (astat[i] == -1) {
566             $("public"+astat_suffix[idx]).style.visibility = "visible";
567             $("pubacard"+astat_suffix[idx]).src = "img/astapasso.png";
568             $("pubapnt"+astat_suffix[idx]).innerHTML = "";
569             $("pubapnt"+astat_suffix[idx]).style.visibility = "hidden";
570         }
571         else if (astat[i] <= 10) {
572             $("public"+astat_suffix[idx]).style.visibility = "visible";
573             $("pubacard"+astat_suffix[idx]).src = "img/asta"+astat[i]+".png";
574             $("pubapnt"+astat_suffix[idx]).style.visibility = "hidden";
575         }
576         else if (astat[i] <= 120) {
577             $("public"+astat_suffix[idx]).style.visibility = "visible";
578             $("pubacard"+astat_suffix[idx]).src = "img/asta9.png";
579             $("pubapnt"+astat_suffix[idx]).style.visibility = "inherit"; // XXX VISIBLE
580             $("pubapnt"+astat_suffix[idx]).innerHTML = astat[i];
581         }
582     }
583 }
584
585 var fin = 0;
586
587 function table_init() {
588     var sux = new Array("", "_ea", "_ne", "_nw", "_we");
589
590     remark_off();
591
592     $("asta").style.visibility = "hidden";
593     $("caller").style.visibility = "hidden";
594     show_astat(-2,-2,-2,-2,-2);
595
596     for (i=0 ; i < 8 ; i++) {
597         Drag.init($("card" + i), card_mouseup_cb);
598         for (e = 0 ; e < PLAYERS_N ; e++)
599             $("card"+sux[e]+i).style.visibility = "hidden";
600     }
601     for (i=0 ; i < PLAYERS_N ; i++) {
602         $("takes"+sux[i]).style.visibility = "hidden";
603         }
604
605     for (i = 0 ; i < 8 ; i++) {
606         cards_pos[i] = i;
607         cards_ea_pos[i] = i;
608         cards_ne_pos[i] = i;
609         cards_nw_pos[i] = i;
610         cards_we_pos[i] = i;
611     }
612
613 }
614   
615
616
617 var chatt_lines = new Array();
618 var chatt_lines_n = 0;
619
620 /* PRO CHATT */
621 function chatt_sub(name,str)
622 {
623   // alert("ARRIVA NAME: "+ name + "  STR:"+str);
624   if (chatt_lines_n == 20) {
625     $("txt").innerHTML = "";
626     for (i = 0 ; i < 19 ; i++) {
627       chatt_lines[i] = chatt_lines[i+1];
628       $("txt").innerHTML += chatt_lines[i];
629     }
630     chatt_lines[i] = "<b>"+name+"</b> "+str+ "<br>";
631     $("txt").innerHTML += chatt_lines[i];
632   }
633   else {
634     chatt_lines[chatt_lines_n] = "<b>"+name+"</b> "+str+ "<br>";
635     $("txt").innerHTML += chatt_lines[chatt_lines_n];
636     chatt_lines_n++;
637   }
638   $("txt").innerHTML;
639   $("txt").scrollTop = 10000000;
640 }
641
642 /*
643  *  GESTIONE DEI COOKIES
644  */
645 function createCookie(name,value,hours,path) {
646         if (hours) {
647                 var date = new Date();
648                 date.setTime(date.getTime()+(hours*60*60*1000));
649                 var expires = "; expires="+date.toGMTString();
650         }
651         else var expires = "";
652         document.cookie = name+"="+value+expires+"; path="+path;
653 }
654
655 function readCookie(name) {
656         var nameEQ = name + "=";
657         var ca = document.cookie.split(';');
658         for(var i=0;i < ca.length;i++) {
659                 var c = ca[i];
660                 while (c.charAt(0)==' ') c = c.substring(1,c.length);
661                 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
662         }
663         return null;
664 }
665
666 function eraseCookie(name) {
667         createCookie(name,"",-1);
668 }
669
670
671 /*
672 window.onload = function() {
673     $("log").innerHTML += "            xxxxxxxxxxxxxxxxxxxxxONLOAD<br>";
674
675     // $("imm2").style.left = 600;
676     // $("imm2").style.top  = 400;
677     var zigu = new slowimg($("imm"),300,100,15,"fin");
678     zigu.settime(1000);
679     zigu.start();
680     //     setTimeout(function() { alert("FIN:" + fin); }, 5000);
681 }
682 */