multi lang
[brisk.git] / web / room.js
1 /*
2  *  brisk - room.js
3  *
4  *  Copyright (C) 2006-2009 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  */
23
24
25 /* 
26    data = [ [ flags, name ],  ... ]
27    
28 */
29
30 function state_add(flags)
31 {
32     var content = "";
33     var st, name = "";
34     var tit = "";
35
36     if ((flags & 0xf00) != 0) {
37         st = flags & 0xf00;
38         // MLANG 4,12,16,20,24,28
39         switch (st) {
40         case 0x100:
41             name = "st_pau.png";
42             tit = (g_lang == 'en' ? "I'm doing a break" : "sono in pausa");
43             break;
44         case 0x200:
45             name = "st_out.png";
46             tit = (g_lang == 'en' ? "I'm away" : "sono fuori");
47             break;
48         case 0x300:
49             name = "st_dog.png";
50             tit = (g_lang == 'en' ? "Dog time" : "sono a spasso col cane");
51             break;
52         case 0x400:
53             name = "st_eat.png";
54             tit = (g_lang == 'en' ? "I'm eating" : "sto mangiando");
55             break;
56         case 0x500:
57             name = "st_wrk.png";
58             tit = (g_lang == 'en' ? "I'm working" : "sono a lavoro");
59             break;
60         case 0x600:
61             name = "st_smk.png";
62             tit = (g_lang == 'en' ? "I'm smoking a sigarett (and keeping a cancer)" : "sto fumando una sigaretta (e facendomi venire il cancro)");
63             break;
64         case 0x700:
65             name = "st_eye.png";
66             tit = (g_lang == 'en' ? "I'm here!" : "sono presente!");
67             break;
68         default:
69             break;
70         }
71         if (name != "") {
72             content += '&nbsp;<img title="'+tit+'" class="unbo" src="img/'+name+'">';
73         }
74     }
75
76     return content;
77 }
78
79 var standup_data_old = null;
80
81 // TODO !!
82 // appendChild , removeChild
83
84 function table_add(curtag, td)
85 {
86     var tbody  = null, tr;
87
88     do {
89         // console.log("wt: "+curtag.tagName);
90
91         if (curtag.tagName.toLowerCase() == "div" || 
92             curtag.tagName.toLowerCase() == "table") {
93             curtag = curtag.firstChild;
94         }
95         else if (curtag.tagName.toLowerCase() == "tbody") {
96             tbody = curtag;
97             break;
98         }
99         else
100             curtag = null;
101     } while (curtag != null);
102     
103     curtag = tbody.firstChild;
104     ct = 0;
105     do {
106         if (curtag.tagName.toLowerCase() == "tr") {
107             if (curtag.firstChild != null) {
108                 curtag = curtag.firstChild;
109                 ct++;
110             }
111             else {
112                 curtag.appendChild(td);
113                 return(true);
114             }
115         }
116         else if (curtag.tagName.toLowerCase() == "td") {
117             if (curtag.nextSibling != null) {
118                 curtag = curtag.nextSibling;
119                 ct++;
120             }
121             else {
122                 if (ct < 4) {
123                     curtag.parentNode.appendChild(td);
124                     return (true);
125                 }
126                 else {
127                     ct = 0;
128                     curtag = curtag.parentNode.nextSibling;
129                 }
130             }
131         }
132         else {
133             curtag = curtag.parentNode;
134         }
135
136     } while (curtag != null);
137
138     tr = document.createElement("tr");
139     tr.appendChild(td);
140     tbody.appendChild(tr);
141
142     return (true);
143 }
144
145 function spcs(c1, c2, n)
146 {
147     var ret = "";
148     var i;
149
150     for (i = 0 ; i < n ; i++) {
151         if ((i % 2) == 0)
152             ret += c1;
153         else
154             ret += c2;
155     }
156
157     return (ret);
158 }
159
160
161 function table_walk(curtag)
162 {
163     do {
164         // console.log("wt: "+curtag.tagName);
165         if (curtag.tagName.toLowerCase() == "div" || 
166             curtag.tagName.toLowerCase() == "table" ||
167             curtag.tagName.toLowerCase() == "tbody") {
168             curtag = curtag.firstChild;
169         }
170         else if (curtag.tagName.toLowerCase() == "tr") {
171             if (curtag.firstChild != null)
172                 curtag = curtag.firstChild;
173             else if (curtag.tagName != '')
174                 curtag = curtag.nextSibling;
175             else
176                 curtag = null;
177         }
178         else if (curtag.tagName.toLowerCase() == "td") {
179             if (curtag.nextSibling != null)
180                 curtag = curtag.nextSibling;
181             else {
182                 if (curtag.parentNode.nextSibling != null && curtag.parentNode.nextSibling.tagName != '')
183                     curtag = curtag.parentNode.nextSibling;
184                 else
185                     curtag = null;
186             }
187         }
188         else
189             curtag = null;
190
191     } while (curtag != null && curtag.tagName.toLowerCase() != "td");
192
193     if (1 == 0) {
194         if (curtag == null)
195             alert("outtag == null"); 
196         else
197             alert("outtag: "+curtag.tagName);
198     }
199     return (curtag);
200 }
201
202 function j_stand_tdcont(el)
203 {
204     var content = "";
205
206     if (el[0] & 0x01)
207         content += '<b>';
208     
209     if (el[0] & 0x02)
210         content += '<i>';
211     
212     content += el[1];
213     
214     if (el[0] & 0x02)
215         content += '</i>';
216     
217     if (el[0] & 0x01)
218         content += '</b>';
219             
220     content += state_add(el[0]);
221     
222     return (content);
223 }
224
225 function j_stand_cont(data)
226 {
227     var i;
228     var content;
229     var st = 0, name = "";
230     var curtag, nextag;
231
232     if (standup_data_old == null || data.length < 4) {
233     // if (standup_data_old == null) {
234         
235         content = '<table cols="'+(data.length < 4 ? data.length : 4)+'" class="table_standup">';
236         for (i = 0 ; i < data.length ; i++) {
237             if ((i % 4) == 0)
238                 content += '<tr>';
239             content += '<td id="'+i+'" class="room_standup">';
240             content += j_stand_tdcont(data[i]);
241             content += '</td>';
242             
243             if ((i % 4) == 3)
244                 content += '</tr>';
245         }
246         content += '</table>';
247         
248         $("standup").innerHTML = content;
249
250         // console.log("inizio");
251         // for (i = 0 , curtag = table_walk($("standup")) ; curtag != null ;  curtag = table_walk(curtag), i++ ) {
252         //     console.log("inloop["+i+"]: "+curtag.tagName+"  ID: "+curtag.id);
253         // }
254         // console.log("fine "+i);
255
256         // walktable($("standup"), nextag);
257         // console.log($("standup").firstChild);
258         // console.log($("standup").firstChild.firstChild.firstChild.firstChild);
259
260         // log_walk($("standup"));
261
262         standup_data_old = data;
263     }
264     else {
265         var idx_del, arr_add, idx_mod, arr_mod;
266         var idx_del_n = 0, idx_add_n = 0, idx_mod_n = 0;
267         var i, e;
268         var i_del, i_mod, i_add;
269         var td;
270
271         idx_del = new Array();
272         arr_add = new Array();
273         map_add = new Array();
274         idx_mod = new Array();
275         arr_mod = new Array();
276         map_cur = new Array();
277         
278         // find removed entries
279         for (i = 0 ; i < standup_data_old.length ; i++) {
280             for (e = 0 ; e < data.length ; e++) {
281                 if (standup_data_old[i][1] == data[e][1]) {
282                     break;
283                 }
284             }
285             if (e == data.length) {
286                 idx_del[idx_del_n++] = i;
287                 map_cur[i] = -1;
288             }
289             else {
290                 /* modified entries */
291                 if (standup_data_old[i][0] != data[e][0]) {
292                     arr_mod[idx_mod_n] = data[e];
293                     idx_mod[idx_mod_n++] = i;
294                 }
295                 map_cur[i] = e;
296             }
297         }
298
299         // find new entries
300         for (e = 0 ; e < data.length ; e++) {
301             for (i = 0 ; i < standup_data_old.length ; i++) {
302                 if (data[e][1] == standup_data_old[i][1] ) {
303                     break;
304                 }
305             }
306             if (i == standup_data_old.length) {
307                 // console.log("ADD: "+data[e][1]);
308                 arr_add[idx_add_n]   = data[e];
309                 map_add[idx_add_n++] = e;
310             }
311         }
312         
313         // TODO: qui travaso add in del
314
315         i_del = 0;
316         // alert("del: ["+j_stand_tdcont(standup_data_old[idx_del[i_del]])+"]");
317         for (i = 0 , i_del = 0, i_mod = 0, i_add = 0, curtag = table_walk($("standup")) ; curtag != null ;  curtag = table_walk(curtag), i++ ) {
318             // console.log("cur.id: "+curtag.id);
319
320             // alert("i: "+i+"  tagname: "+curtag.tagName+"  innerHTML: ["+curtag.innerHTML+"]");
321             // console.log("inloop["+i+"]: "+curtag.tagName+"  ID: "+curtag.id);
322             if (curtag.innerHTML == "") {
323                 // console.log("innerHTML == none");
324                 if (i_add < idx_add_n) {
325                     // console.log("  to be new");
326                     // console.log("  add:   CONT:"+j_stand_tdcont(arr_add[i_add]));
327                     curtag.innerHTML = j_stand_tdcont(arr_add[i_add]);
328                     curtag.id = map_add[i_add];
329                     i_add++
330                 }
331             }
332
333             // else if (i_del < idx_del_n && curtag.innerHTML == j_stand_tdcont(standup_data_old[idx_del[i_del]])) {
334             else if (i_del < idx_del_n && curtag.id == idx_del[i_del]) {
335                 // console.log("to be cancel["+i+"]:  ID: "+curtag.id);
336                 if (i_add < idx_add_n) {
337                     // console.log("  to be new");
338                     // console.log("  add:   CONT:"+j_stand_tdcont(arr_add[i_add]));
339                     curtag.innerHTML = j_stand_tdcont(arr_add[i_add]);
340                     curtag.id = map_add[i_add];
341                     i_add++
342                 }
343                 else {
344                     // console.log("  to be del");
345                     curtag.innerHTML = "";
346                     curtag.id = -1;
347                 }
348                 i_del++;
349             }
350             // else if (i_mod < idx_mod_n && curtag.innerHTML == j_stand_tdcont(standup_data_old[idx_mod[i_mod]])) {
351             else if (i_mod < idx_mod_n && curtag.id == idx_mod[i_mod]) {
352                 // console.log("  to be mod");
353                 // console.log("mod: "+idx_mod[i_mod]+ "  CONT:"+j_stand_tdcont(arr_mod[i_mod]));
354                 curtag.innerHTML = j_stand_tdcont(arr_mod[i_mod]);
355                 curtag.id = map_cur[curtag.id];
356                 i_mod++;
357             }
358             else
359                 curtag.id = map_cur[curtag.id];
360         }
361         // console.log("fineloop");
362
363         for (i ; i_add < idx_add_n ; i_add++, i++) {
364             // console.log("ADD: "+i+" arr_add: "+ arr_add[i_add][1]);
365             td = document.createElement("td");
366             td.className = "room_standup";
367             td.id = map_add[i_add];
368             td.innerHTML = j_stand_tdcont(arr_add[i_add]);
369
370             table_add($("standup"), td);
371         }
372
373         standup_data_old = data;
374         return;
375     }
376     // $("esco").innerHTML =  '<input class="button" name="logout" value="Esco." onclick="esco_cb();" type="button">';
377 }
378
379 function esco_cb() {
380     window.onbeforeunload = null; 
381     window.onunload = null; 
382     // nonunload = true; 
383     act_logout();
384  };
385
386
387
388 function j_tab_cont(table_idx, data)
389 {
390     var i;
391     var content = '';
392
393     for (i = 0 ; i < data.length ; i++) {
394         if (data[i][0] & 0x01)
395             content += '<b>';
396
397         if (data[i][0] & 0x02)
398             content += '<i>';
399
400         content += data[i][1];
401         
402         if (data[i][0] & 0x02)
403             content += '</i>';
404
405         if (data[i][0] & 0x01)
406             content += '</b>';
407         content += state_add(data[i][0]);
408
409         content += '<br>';
410     }
411     $("table"+table_idx).innerHTML = content;
412 }
413
414 function j_tab_act_cont(idx, act)
415 {
416     if (act == 'sit') {
417         // MLANG 1
418         $("table_act"+idx).innerHTML = '<input type="button" class="button" name="xhenter'+idx+'"  value="'+(g_lang == 'en' ? "Sit down." : "Mi siedo.")+'" onclick="act_sitdown('+idx+');">';
419     }
420     else if (act == 'sitreser') {
421         // <img class="nobo" title="tavolo riservato agli utenti registrati" style="display: inline; margin-right: 80px;" src="img/okauth.png">
422         // MLANG 1
423         $("table_act"+idx).innerHTML = '<input type="button" style="background-repeat: no-repeat; background-position: center; background-image: url(\'img/okauth.png\');" class="button" name="xhenter'+idx+'"  value="'+(g_lang == 'en' ? "Sit down." : "Mi siedo.")+'" onclick="act_sitdown('+idx+');">';
424     }
425     else if (act == 'wake') {
426         // MLANG 1
427         $("table_act"+idx).innerHTML = '<input type="button" class="button" name="xwakeup"  value="'+(g_lang == 'en' ? "Wake up." : "Mi alzo.")+'" onclick="act_wakeup();">';
428     }
429     else if (act == 'reserved') {
430         // MLANG 1
431         $("table_act"+idx).innerHTML = '<img class="nobo" title="'+(g_lang == 'en' ? "reserved table for authenticated users only" : "tavolo riservato agli utenti registrati")+'" style="margin-right: 20px;" src="img/onlyauth.png">';
432     }
433     else {
434         $("table_act"+idx).innerHTML = '';
435     }
436 }
437
438 function j_login_manager(form)
439 {
440     var token;
441
442     if (form.elements['passid'].value == '')
443         return (true);
444
445     else {
446         // console.log("richiesta token");
447         /* richiede token */
448         token = server_request('mesg', 'getchallenge', 'cli_name', encodeURIComponent(form.elements['nameid'].value));
449         tokens = token.split('|');
450         
451         // console.log('XX token: '+token);
452         // console.log(tokens);
453         if (token == null)
454             return (false);
455
456         token = calcMD5(tokens[1]+calcMD5(form.elements['passid'].value));
457         
458         form.elements['passid_private'].value = token;
459         form.elements['passid'].value = ""; // FIXME da sost con la stessa len di A
460
461         return (true);
462     }
463     
464     return (false);
465 }
466
467 function login_formtext_hilite()
468 {
469     formtext_hilite($("nameid"));
470     formtext_hilite($("passid"));
471     formsub_hilite($("sub"));
472 }
473
474 function login_init()
475 {
476     menu_init();
477     login_formtext_hilite();
478 }
479
480 function warrant_formtext_hilite(form)
481 {
482     /*
483     formtext_hilite($("nameid"));
484     formtext_hilite($("emailid"));
485     formsub_hilite($("subid"));
486     formsub_hilite($("cloid"));
487     */
488     formtext_hilite(form.elements['name']);
489     formtext_hilite(form.elements['email']);
490     formsub_hilite(form.elements['sub']);
491     formsub_hilite(form.elements['clo']);
492 }
493
494 function mesgtoadm_formtext_hilite(form)
495 {
496     /*
497     formtext_hilite($("subjid"));
498     formtext_hilite($("mesgid"));
499     formsub_hilite($("subid"));
500     formsub_hilite($("cloid"));
501     */
502     formtext_hilite(form.elements['subj']);
503     formtext_hilite(form.elements['mesg']);
504     formsub_hilite(form.elements['sub']);
505     formsub_hilite(form.elements['clo']);
506 }
507
508
509 function j_check_email(email)
510 {
511     if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email))
512         return (true);
513     return (false);
514 }
515
516 function j_authbox(form)
517 {
518     var no; 
519
520     do {
521         if (form.elements['realsub'].value == "chiudi") {
522             $('authbox').style.visibility = "hidden";
523             break;
524         }
525
526         if (form.elements['name'].value == '' || j_check_email(form.elements['email'].value) == false) {
527             // MLANG 2-4
528             no = new notify(gst, 
529                             (g_lang == 'en' ? "<br><b>nickname</b> and/or <b>e-mail</b> fields are invalid;<br>please, fix them." :
530                              "<br>I campi <b>nickname</b> e/o <b>e-mail</b> non sono validi;<br> correggeteli per favore."),
531                             1, (g_lang == 'en' ? "close" : "chiudi"), 280, 100); 
532             break;
533         }
534
535         // submit the request
536         token = server_request('mesg', 'warranty', 
537                                'cli_name', encodeURIComponent(form.elements['name'].value),
538                                'cli_email', encodeURIComponent(form.elements['email'].value) );
539         if (token == "1") {
540             $('authbox').style.visibility = "hidden";
541             form.elements['name'].value = "";
542             form.elements['email'].value = "";
543             break;
544         }
545     } while (0);
546
547     return (false);
548 }
549
550 function authbox(w, h)
551 {
552     var box;
553
554     box = $('authbox');
555
556     box.style.zIndex = 200;
557     box.style.width  = w+"px";
558     box.style.marginLeft  = -parseInt(w/2)+"px";
559     box.style.height = h+"px";
560     box.style.top = parseInt((document.body.clientHeight - h) / 2) + document.body.scrollTop;
561
562     warrant_formtext_hilite($('auth_form'));
563
564     box.style.visibility = "visible";
565     $("nameid").focus();
566 }
567
568 function j_mesgtoadmbox(form)
569 {
570     var no; 
571
572     do {
573         if (form.elements['realsub'].value == "chiudi") {
574             $('mesgtoadmbox').style.visibility = "hidden";
575             break;
576         }
577
578         if (form.elements['mesg'].value == '' || form.elements['subj'].value == '') {
579             // MLANG 1-3
580             no = new notify(gst, (g_lang == 'en' ? "<br><b>subject</b> and the <b>message</b> cannot be void;<br>please, fix them." :
581                                   "<br>Il <b>soggetto</b> e il <b>messaggo</b> non possono essere vuoti;<br>correggeteli per favore."), 1, 
582                                   (g_lang == 'en' ? "close" : "chiudi"), 280, 100); 
583             break;
584         }
585                 
586         // submit the request
587         token = server_request('mesg', 'mesgtoadm', 
588                                'cli_subj', encodeURIComponent(form.elements['subj'].value),
589                                'cli_mesg', encodeURIComponent(form.elements['mesg'].value) );
590         if (token == "1") {
591             $('mesgtoadmbox').style.visibility = "hidden";
592             form.elements['subj'].value = "";
593             form.elements['mesg'].value = "";
594             break;
595         }
596     } while (0);
597
598     return (false);
599 }
600
601 function mesgtoadmbox(w, h)
602 {
603     var box;
604
605     box = $('mesgtoadmbox');
606
607     box.style.zIndex = 200;
608     box.style.width  = w+"px";
609     box.style.marginLeft  = -parseInt(w/2)+"px";
610     box.style.height = h+"px";
611     box.style.top = parseInt((document.body.clientHeight - h) / 2) + document.body.scrollTop;
612
613     mesgtoadm_formtext_hilite($('mesgtoadm_form'));
614
615     box.style.visibility = "visible";
616     $('mesgtoadm_form').elements['subj'].focus();
617 }
618
619 function j_pollbox(form)
620 {
621     var no, i, choose; 
622
623     do {
624         // submit the request
625         
626         for (i = 0 ; i < form.elements.length ; i++) {
627             if (form.elements[i].checked == true)
628                 break;
629         }
630         if (i == form.elements.length) {
631             // MLANG 1-3
632             no = new notify(gst, (g_lang == 'en' ? "<br>You must choose ah item;<br> please, fix it." :
633                                   "<br>Non hai espresso nessuna preferenza;<br> correggi per favore."), 1, 
634                             (g_lang == 'en' ? "close" : "chiudi"), 280, 100); 
635             return false;
636         }
637         else
638             choose = form.elements[i].value;
639
640         token = server_request('mesg', 'poll', 
641                                'cli_choose', encodeURIComponent(choose) );
642
643         if (token == "1") {
644             // TODO: mesg to user
645             // $('mesgtoadmbox').style.visibility = "hidden";
646             break;
647         }
648     } while (0);
649
650     return (false);
651 }
652
653
654
655
656 function list_set(what, setco, info)
657 {
658     if (what == 'auth') {
659         $('list_auth').style.color = 'red';
660         $('list_all').style.color = 'black';
661     }
662     else {
663         $('list_auth').style.color = 'black';
664         $('list_all').style.color = 'red';
665     }
666     $('list_info').innerHTML = info;
667     if (setco) {
668         createCookie("CO_list", what, 24*365, cookiepath);
669     }
670 }
671
672 function sideslide(domobj, height, step)
673 {
674     this.st = 'wait';
675     this.twait = 5000;
676
677     this.domobj = domobj;
678     this.height = height;
679     this.step = step;
680
681     this.start();
682 }
683
684 sideslide.prototype = {
685     id: null,
686     st: 'wait',
687     twait: 0,
688     scroll: 0,
689     countdown: 0,
690
691     domobj: null,
692     height: 0,
693     step: 0,
694
695     start: function() {
696         var instant = this;
697         
698         this.st = 'wait';
699         this.id = setTimeout(function () { instant.sideslide_cb(); }, this.twait);
700     },
701
702     sideslide_cb: function() {
703         var instant = this;
704
705         if (this.st == 'wait') {
706             this.st = 'scroll';
707             this.countdown = 10;
708             this.id = setInterval(function () { instant.sideslide_cb(); }, 100);
709         }
710         else if (this.st == 'scroll') {
711             this.scroll += (this.step / 10);
712             if (this.scroll >= this.height - this.step) {
713                 this.scroll = 0;
714             }
715             this.domobj.scrollTop = this.scroll;
716             this.countdown--;
717             if (this.countdown == 0) {
718                 this.stop();
719                 this.st = 'wait';
720                 this.id = setTimeout(function () { instant.sideslide_cb(); }, this.twait);
721             }
722         }
723     },
724
725
726     stop: function() {
727         if (this.id != null) {
728             clearInterval(this.id);
729             this.id = null;
730         }
731     }
732
733 }