aggiunte richieste di conferma
[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 safelogout()
208 {
209     var res;
210
211     res = window.confirm("Sei sicuro di volere abbandonare la partita?");
212     if (res)
213         act_logout();
214 }
215 function act_logout()
216 {
217     send_mesg("logout");
218 }
219
220 function act_reload()
221 {
222     window.onunload = null;
223     document.location.reload();
224 }
225
226 function act_shutdown()
227 {
228     var c = 0;
229
230     send_mesg("shutdown");
231     while (xhr_wr.readyState != 4)
232         c++;
233 }
234
235 function act_preout()
236 {
237     act_logout();
238 }
239
240 function postact_logout()
241 {
242     // alert("postact_logout");
243     try { 
244         xhr_rd.abort();
245     } catch (e) {}
246
247     eraseCookie("sess");
248     document.location.assign("index.php");
249 }
250
251 /*
252   function slowimg(img,x1,y1,deltat,free,action,srcend)
253   img    - image to move
254   x1,y1  - destination coords
255   deltat - time for each frame (in msec)
256   free   - when the release the local block for other operations (range: 0 - 1)
257   action - function to run when the image is moved
258   srcend - image to switch when the image is moved
259 */
260
261 function sleep(st, delay)
262 {
263     // alert("LOC_NEW PRE: "+st.st_loc_new);
264
265     st.st_loc_new++;
266
267     setTimeout(function(obj){ if (obj.st_loc_new > obj.st_loc) { obj.st_loc++; }},
268                delay, st);
269 }
270
271 function slowimg(img,x1,y1,deltat,free,action,srcend) {
272     this.img = img;
273
274     this.x0  = parseInt(document.defaultView.getComputedStyle(this.img, "").getPropertyValue("left"));
275     // alert("img.x0 = "+this.x0);
276     this.y0  = parseInt(document.defaultView.getComputedStyle(this.img, "").getPropertyValue("top"));
277     this.x1  = x1;
278     this.y1  = y1;
279     this.deltat = deltat;
280     this.free = free;
281     this.action = action;
282     this.srcend = srcend;
283 }
284
285 slowimg.prototype = {
286     img: null, 
287     st: null,
288     x0: 0,
289     y0: 0,
290     x1: 0,
291     y1: 0,
292     dx: 0,
293     dy: 0,
294     free: 0,
295     step_n:    0,
296     step_cur:  0,
297     step_free: 0,
298     time:      0,
299     deltat:   40,
300     tout: 0,
301     action: null,
302     srcend: null,
303     
304     setstart: function(x0,y0)
305     {
306         this.x0 = x0;
307         this.y0 = y0;
308     },
309     
310     setaction: function(act)
311     {
312         this.action = act;
313     },
314     
315     settime: function(time) 
316     {
317         this.time = time;
318         this.step_n = parseInt(time / this.deltat);
319         this.dx = (this.x1 - this.x0) / this.step_n;
320         this.dy = (this.y1 - this.y0) / this.step_n;
321         if (this.step_n * this.deltat == time) {
322             this.step_n--;
323         }
324         this.step_free = parseInt(this.step_n * this.free);
325     },
326     
327     start: function(st)
328     {
329         // $("logz").innerHTML += "               xxxxxxxxxxxxxxxxxxxxxSTART<br>";
330         this.st = st;
331         this.st.st_loc_new++;
332         
333         this.img.style.visibility = "visible";
334         setTimeout(function(obj){ obj.animate(); }, this.deltat, this);
335     },
336     
337     animate: function()
338     {
339         // $("log").innerHTML = "Val " + this.step_cur + " N: " + this.step_n + "<br>";
340         if (this.step_cur == 0) {
341             var date = new Date();
342             // $("logz").innerHTML = "Timestart: " + date + "<br>";
343         }
344         if (this.step_cur <= this.step_n) {
345             this.img.style.left = this.x0 + this.dx * this.step_cur;
346             this.img.style.top  = this.y0 + this.dy * this.step_cur;
347             this.step_cur++;
348             setTimeout(function(obj){ obj.animate(); }, this.deltat, this);
349             if (this.step_cur == this.step_free && this.st != null) {
350                 if (this.st != null && this.st.st_loc < this.st.st_loc_new) {
351                     // alert("QUI1  " + this.step_cur + "  ZZ  "+  this.step_free);
352                     this.st.st_loc++;
353                     this.st = null;
354                 }
355             }
356         }
357         else {
358             this.img.style.left = this.x1;
359             this.img.style.top  = this.y1;
360             // $("logz").innerHTML += "xxxxxxxxxxxxxxxCLEAR<br>";
361             var date = new Date();
362             // $("logz").innerHTML += "Timestop: " + date + "<br>";
363             if (this.st != null && this.st.st_loc < this.st.st_loc_new) {
364                 // alert("QUI2");
365                 this.st.st_loc++;
366                 this.st = null;
367             }
368             if (this.action != null) {
369                 eval(this.action);
370             }
371             if (this.srcend != null) {
372                 this.img.src = this.srcend;
373             }
374         }
375     }
376 }
377
378 var asta_xarr = new Array(0,66,133);
379
380 /* TODO: impostare gli onclick */
381 function dispose_asta(idx, pnt)
382 {
383     var i, btn, pass;
384     
385     var btn;
386     for (i = 0 ; i < 10 ; i++) {
387         btn = $("asta"+i);
388         if (i < idx) {
389             btn.src = "img/astapasso"+(pnt >= 0 ? "" : "_ro")+".png";
390             pass = -1;
391         }
392         else {
393             btn.src = "img/asta"+i+(pnt >= 0 ? "" : "_ro")+".png";
394             pass = i;
395         }
396         if (i < 19)
397             btn.style.left = asta_xarr[i % 3];
398         else
399             btn.style.left = asta_xarr[(i+1) % 3];
400         
401         btn.style.top  = parseInt(i / 3) * 50+1;
402         // btn.style.visibility  = "visible";
403         
404         if (pnt >= 0)
405             eval("btn.onclick = function () { act_asta("+pass+",61); }");
406         else
407             btn.onclick = null;
408     }
409     
410     
411     btn = $("astaptdiv");
412     btn.style.left = asta_xarr[i % 3];
413     btn.style.top = parseInt(i / 3) * 50;
414     // btn.style.visibility  = "visible";
415     
416     btn = $("astapt");
417     var rpnt = (pnt < 0 ? -pnt : pnt);
418     btn.value = (rpnt < 61 ? 61 : (rpnt > 120 ? 120 : rpnt));
419     
420     btn = $("astaptsub");
421     btn.style.left = asta_xarr[i % 3];
422     btn.style.top = 25 + parseInt(i / 3) * 50;;
423     btn.src = "img/astaptsub"+(pnt >= 0 ? "" : "_ro")+".png";
424     // btn.style.visibility  = "visible";
425     if (pnt >= 0)
426         btn.onclick = function () { act_asta(9,$("astapt").value); };
427     else
428         btn.onclick = null;
429     
430     i+=1;
431     btn = $("astapasso2");
432     btn.style.left = asta_xarr[i % 3];
433     btn.style.top = parseInt(i / 3) * 50;;
434     btn.src = "img/astapasso"+(pnt >= 0 ? "" : "_ro")+".png";
435     // btn.style.visibility  = "visible";
436     if (pnt >= 0)
437         btn.onclick = function () { act_asta(-1,0); };
438     else
439         btn.onclick = null;
440     $("asta").style.visibility = "visible";
441 }
442
443 function hide_asta()
444 {
445     $("asta").style.visibility = "hidden"; 
446 }
447
448 function notify(st, ancestor, text, tout, butt)
449 {
450     var clo, box;
451     var t = this;
452     
453     this.st = st;
454     this.ancestor = ancestor;
455     
456     this.st.st_loc_new++;
457
458     clo = document.createElement("input");
459     clo.type = "submit";
460     clo.value = butt;
461     clo.obj = this;
462     clo.onclick = this.input_hide;
463
464     box = document.createElement("div");
465     box.className = "notify";
466     box.innerHTML = text;
467     box.style.zIndex = 200;
468     box.appendChild(clo);
469     box.style.visibility = "visible";
470     
471     this.notitag = box;
472     
473     this.ancestor.appendChild(box);
474     
475     this.toutid = setTimeout(function(obj){ obj.unblock(); }, tout, this);
476 }
477
478 notify.prototype = {
479     ancestor: null,
480     st: null,
481     notitag: null,
482     toutid: null,
483     
484     unblock: function()
485     {
486         if (this.st.st_loc < this.st.st_loc_new) {
487             this.st.st_loc++;
488         }
489     },
490     
491     hide: function()
492     {
493         clearTimeout(this.toutid);
494         this.ancestor.removeChild(this.notitag);
495         this.unblock();
496     },
497
498     input_hide: function()
499     {
500         clearTimeout(this.obj.toutid);
501         this.obj.ancestor.removeChild(this.obj.notitag);
502         this.obj.unblock();
503     }
504 }
505         
506
507 function $(id) { 
508     return document.getElementById(id); 
509 }
510
511 function globst() {
512     this.st = -1;
513     this.st_loc = -1;
514     this.st_loc_new = -1;
515     this.comms  = new Array;
516 }
517
518
519
520 function remark_step()
521 {
522     var ct = $("remark").l_remct;
523     
524     if (ct != 0) {
525         ct++;
526         if (ct > 2)
527             ct = 1;
528         $("remark").className = "remark"+ct;
529         $("remark").l_remct = ct;
530         setTimeout(remark_step,500);
531     }
532     else
533         $("remark").className = "remark0";
534     
535     return;
536 }
537
538 function remark_on()
539 {
540     if ($("remark").l_remct == 0) {
541         $("remark").l_remct = 1;
542         setTimeout(remark_step,500);
543     }
544 }
545
546 function remark_off()
547 {
548     $("remark").l_remct = 0;
549     $("remark").className = "remark0";
550 }
551
552
553 function choose_seed(card)
554 {
555     var i;
556
557     $("chooseed").style.visibility = "visible";
558     for (i = 0 ; i < 4 ; i++) {
559         $("seed"+i).src = "img/"+i+""+card+".png";
560         seed=$("seed"+i);
561         eval("seed.onclick = function () { act_choose("+i+""+card+"); };");
562     }
563 }
564
565 function set_names(so,ea,ne,nw,we)
566 {
567 //    alert("EA: "+ea);
568     $("name").innerHTML = so; 
569     $("name_ea").innerHTML = ea;
570     $("name_ne").innerHTML = ne;
571     $("name_nw").innerHTML = nw;
572     $("name_we").innerHTML = we;
573     return;
574 }
575
576 var astat_suffix = new Array("","_ea","_ne","_nw","_we");
577
578 function show_astat(zer,uno,due,tre,qua)
579 {
580     var astat = new Array(zer,uno,due,tre,qua);
581
582     for (i = 0 ; i < PLAYERS_N ; i++) {
583         idx = (PLAYERS_N + i - table_pos) % PLAYERS_N;
584
585         if (astat[i] == -2) {
586             $("public"+astat_suffix[idx]).style.visibility = "hidden";
587         }
588         else if (astat[i] == -1) {
589             $("public"+astat_suffix[idx]).style.visibility = "visible";
590             $("pubacard"+astat_suffix[idx]).src = "img/astapasso.png";
591             $("pubapnt"+astat_suffix[idx]).innerHTML = "";
592             $("pubapnt"+astat_suffix[idx]).style.visibility = "hidden";
593         }
594         else if (astat[i] <= 10) {
595             $("public"+astat_suffix[idx]).style.visibility = "visible";
596             $("pubacard"+astat_suffix[idx]).src = "img/asta"+astat[i]+".png";
597             $("pubapnt"+astat_suffix[idx]).style.visibility = "hidden";
598         }
599         else if (astat[i] <= 120) {
600             $("public"+astat_suffix[idx]).style.visibility = "visible";
601             $("pubacard"+astat_suffix[idx]).src = "img/asta9.png";
602             $("pubapnt"+astat_suffix[idx]).style.visibility = "inherit"; // XXX VISIBLE
603             $("pubapnt"+astat_suffix[idx]).innerHTML = astat[i];
604         }
605     }
606 }
607
608 var fin = 0;
609
610 function table_init() {
611     var sux = new Array("", "_ea", "_ne", "_nw", "_we");
612
613     remark_off();
614
615     $("asta").style.visibility = "hidden";
616     $("caller").style.visibility = "hidden";
617     show_astat(-2,-2,-2,-2,-2);
618
619     for (i=0 ; i < 8 ; i++) {
620         Drag.init($("card" + i), card_mouseup_cb);
621         for (e = 0 ; e < PLAYERS_N ; e++)
622             $("card"+sux[e]+i).style.visibility = "hidden";
623     }
624     for (i=0 ; i < PLAYERS_N ; i++) {
625         $("takes"+sux[i]).style.visibility = "hidden";
626         }
627
628     for (i = 0 ; i < 8 ; i++) {
629         cards_pos[i] = i;
630         cards_ea_pos[i] = i;
631         cards_ne_pos[i] = i;
632         cards_nw_pos[i] = i;
633         cards_we_pos[i] = i;
634     }
635
636 }
637   
638
639
640 var chatt_lines = new Array();
641 var chatt_lines_n = 0;
642
643 /* PRO CHATT */
644 function chatt_sub(name,str)
645 {
646   // alert("ARRIVA NAME: "+ name + "  STR:"+str);
647   if (chatt_lines_n == 20) {
648     $("txt").innerHTML = "";
649     for (i = 0 ; i < 19 ; i++) {
650       chatt_lines[i] = chatt_lines[i+1];
651       $("txt").innerHTML += chatt_lines[i];
652     }
653     chatt_lines[i] = "<b>"+name+"</b> "+str+ "<br>";
654     $("txt").innerHTML += chatt_lines[i];
655   }
656   else {
657     chatt_lines[chatt_lines_n] = "<b>"+name+"</b> "+str+ "<br>";
658     $("txt").innerHTML += chatt_lines[chatt_lines_n];
659     chatt_lines_n++;
660   }
661   $("txt").innerHTML;
662   $("txt").scrollTop = 10000000;
663 }
664
665 /*
666  *  GESTIONE DEI COOKIES
667  */
668 function createCookie(name,value,hours,path) {
669         if (hours) {
670                 var date = new Date();
671                 date.setTime(date.getTime()+(hours*60*60*1000));
672                 var expires = "; expires="+date.toGMTString();
673         }
674         else var expires = "";
675         document.cookie = name+"="+value+expires+"; path="+path;
676 }
677
678 function readCookie(name) {
679         var nameEQ = name + "=";
680         var ca = document.cookie.split(';');
681         for(var i=0;i < ca.length;i++) {
682                 var c = ca[i];
683                 while (c.charAt(0)==' ') c = c.substring(1,c.length);
684                 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
685         }
686         return null;
687 }
688
689 function eraseCookie(name) {
690         createCookie(name,"",-1);
691 }
692
693 var onunload_times = 0;
694
695
696 function onunload_cb () {
697     var u = 0;
698     if (onunload_times == 0) {
699         var res = window.confirm("    Vuoi veramente abbandonare la briscola ?\n(clicca annulla o cancel se vuoi ricaricare la briscola)");
700         if (res == true) {
701             the_end = true; 
702             act_shutdown();
703             for (i = 0 ; i < 1000000 ; i++)
704                 u++;
705         }
706         else {
707             try {
708                 location = self.location;
709             } catch (e) {
710                 alert("Ripristino della briscola fallito, per non perdere la sessione ricaricare la pagina manualmente.");
711             }
712         }
713         onunload_times++;
714     }
715     
716     return(false);
717 }
718