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