Warranty form management added, server_request() enhanced, states management added
[brisk.git] / web / room.js
1 /* 
2    data = [ [ flags, name ],  ... ]
3    
4 */
5
6 function state_add(flags)
7 {
8     var content = "";
9     var st, name = "";
10     var tit = "";
11
12     if ((flags & 0xf00) != 0) {
13         st = flags & 0xf00;
14         switch (st) {
15         case 0x100:
16             name = "st_pau.png";
17             tit = "in pausa";
18             break;
19         case 0x200:
20             name = "st_out.png";
21             tit = "fuori";
22             break;
23         case 0x300:
24             name = "st_dog.png";
25             tit = "cane a spasso";
26             break;
27         case 0x400:
28             name = "st_eat.png";
29             tit = "a mangiare";
30             break;
31         case 0x500:
32             name = "st_wrk.png";
33             tit = "a lavoro";
34             break;
35         case 0x600:
36             name = "st_smoke.png";
37             tit = "si sta fumando una sigaretta (e facendosi venire il cancro)";
38             break;
39         default:
40             break;
41         }
42         if (name != "") {
43             content += '<img title="'+tit+'" class="unbo" src="img/'+name+'">';
44         }
45     }
46
47     return content;
48 }
49
50 function j_stand_cont(data)
51 {
52     var i;
53     var content;
54     var st, name = "";
55
56     content = '<table cols="'+(data.length < 4 ? data.length : 4)+'" class="table_standup">';
57     for (i = 0 ; i < data.length ; i++) {
58         if ((i % 4) == 0)
59             content += '<tr>';
60         content += '<td class="room_standup">';
61         if (data[i][0] & 0x01)
62             content += '<b>';
63
64         if (data[i][0] & 0x02)
65             content += '<i>';
66
67         content += data[i][1];
68         
69         if (data[i][0] & 0x02)
70             content += '</i>';
71
72         if (data[i][0] & 0x01)
73             content += '</b>';
74
75         content += state_add(data[i][0]);
76         content += '</td>';
77
78         if ((i % 4) == 3)
79             content += '</tr>';
80     }
81     content += '</tr>';
82
83     $("standup").innerHTML = content;
84
85     $("esco").innerHTML =  '<input class="button" name="logout" value="Esco." onclick="esco_cb();" type="button">';
86 }
87
88 function esco_cb() {
89     window.onbeforeunload = null; 
90     window.onunload = null; 
91     // nonunload = true; 
92     act_logout();
93  };
94
95
96
97 function j_tab_cont(table_idx, data)
98 {
99     var i;
100     var content = '';
101
102     for (i = 0 ; i < data.length ; i++) {
103         if (data[i][0] & 0x01)
104             content += '<b>';
105
106         if (data[i][0] & 0x02)
107             content += '<i>';
108
109         content += data[i][1];
110         
111         if (data[i][0] & 0x02)
112             content += '</i>';
113
114         if (data[i][0] & 0x01)
115             content += '</b>';
116         content += state_add(data[i][0]);
117
118         content += '<br>';
119     }
120     $("table"+table_idx).innerHTML = content;
121 }
122
123 function j_tab_act_cont(idx, act)
124 {
125     if (act == 'sit') {
126         $("table_act"+idx).innerHTML = '<input type="button" class="button" name="xhenter'+idx+'"  value="Mi siedo." onclick="act_sitdown('+idx+');">';
127     }
128     else if (act == 'sitreser') {
129         // <img class="nobo" title="tavolo riservato agli utenti registrati" style="display: inline; margin-right: 80px;" src="img/okauth.png">
130         $("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="Mi siedo." onclick="act_sitdown('+idx+');">';
131     }
132     else if (act == 'wake') {
133         $("table_act"+idx).innerHTML = '<input type="button" class="button" name="xwakeup"  value="Mi alzo." onclick="act_wakeup();">';
134     }
135     else if (act == 'reserved') {
136         $("table_act"+idx).innerHTML = '<img class="nobo" title="tavolo riservato agli utenti registrati" style="margin-right: 20px;" src="img/onlyauth.png">';
137     }
138     else {
139         $("table_act"+idx).innerHTML = '';
140     }
141 }
142
143 function j_login_manager(form)
144 {
145     var token;
146
147     if (form.elements['passid'].value == '')
148         return (true);
149
150     else {
151         // console.log("richiesta token");
152         /* richiede token */
153         token = server_request('mesg', 'getchallenge', 'cli_name', encodeURIComponent(form.elements['nameid'].value));
154         tokens = token.split('|');
155         
156         // console.log('XX token: '+token);
157         // console.log(tokens);
158         if (token == null)
159             return (false);
160
161         token = calcMD5(tokens[1]+calcMD5(form.elements['passid'].value));
162         
163         form.elements['passid_private'].value = token;
164         form.elements['passid'].value = ""; // FIXME da sost con la stessa len di A
165
166         return (true);
167     }
168     
169     return (false);
170 }
171
172 function formtext_hilite(obj)
173 {
174     obj.className = 'input_text';
175     addEvent(obj, "focus", function () { this.className = 'input_text_hi'; });
176     addEvent(obj, "blur",  function () { this.className = 'input_text'; });
177 }
178
179 function formsub_hilite(obj)
180 {
181     obj.className = 'input_sub';
182     addEvent(obj, "focus", function () { this.className = 'input_sub_hi'; });
183     addEvent(obj, "blur",  function () { this.className = 'input_sub'; });
184 }
185
186 function login_formtext_hilite()
187 {
188     formtext_hilite($("nameid"));
189     formtext_hilite($("passid"));
190     formsub_hilite($("sub"));
191 }
192
193 function login_init()
194 {
195     menu_init();
196     login_formtext_hilite();
197 }
198
199 function warrant_formtext_hilite()
200 {
201     formtext_hilite($("nameid"));
202     formtext_hilite($("emailid"));
203     formsub_hilite($("subid"));
204     formsub_hilite($("cloid"));
205 }
206
207
208 function j_check_email(email)
209 {
210     if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email))
211         return (true);
212     return (false);
213 }
214
215 function j_authbox(form)
216 {
217     var no; 
218
219     if (form.elements['realsub'].value == "chiudi") {
220         $('authbox').style.visibility = "hidden";
221         return (false);
222     }
223
224     if (form.elements['name'].value == '' || j_check_email(form.elements['email'].value) == false)
225         no = new notify(gst, "<br>I campi user e/o e-mail non sono validi;</br> correggeteli per favore.", 1, "chiudi", 280, 100); 
226     else {
227         // submit the request
228         token = server_request('mesg', 'warranty', 
229                                'cli_name', encodeURIComponent(form.elements['name'].value),
230                                'cli_email', encodeURIComponent(form.elements['email'].value) );
231         if (token == "1") {
232             $('authbox').style.visibility = "hidden";
233             form.elements['name'].value = "";
234             form.elements['email'].value = "";
235             return (false);
236         }
237     }
238
239     return (false);
240 }
241
242 function authbox(w, h)
243 {
244     var box;
245
246     box = $('authbox');
247
248     box.style.zIndex = 200;
249     box.style.width  = w+"px";
250     box.style.marginLeft  = -parseInt(w/2)+"px";
251     box.style.height = h+"px";
252     box.style.top = parseInt((document.body.clientHeight - h) / 2) + document.body.scrollTop;
253
254     warrant_formtext_hilite();
255
256     box.style.visibility = "visible";
257     $("nameid").focus();
258 }