fakeroot usage added to avoid root privileges requirements.
[threegates.git] / raw / root / var / www / onlycrs / room.js
1 /* 
2    data = [ [ flags, name ],  ... ]
3    
4 */
5
6
7 var standup_data_old = null;
8
9 // TODO !!
10 // appendChild , removeChild
11
12 function table_add(curtag, td)
13 {
14     var tbody  = null, tr;
15
16     do {
17         // console.log("wt: "+curtag.tagName);
18
19         if (curtag.tagName.toLowerCase() == "div" || 
20             curtag.tagName.toLowerCase() == "table") {
21             curtag = curtag.firstChild;
22         }
23         else if (curtag.tagName.toLowerCase() == "tbody") {
24             tbody = curtag;
25             break;
26         }
27         else
28             curtag = null;
29     } while (curtag != null);
30     
31     curtag = tbody.firstChild;
32     ct = 0;
33     do {
34         if (curtag.tagName.toLowerCase() == "tr") {
35             if (curtag.firstChild != null) {
36                 curtag = curtag.firstChild;
37                 ct++;
38             }
39             else {
40                 curtag.appendChild(td);
41                 return(true);
42             }
43         }
44         else if (curtag.tagName.toLowerCase() == "td") {
45             if (curtag.nextSibling != null) {
46                 curtag = curtag.nextSibling;
47                 ct++;
48             }
49             else {
50                 if (ct < 4) {
51                     curtag.parentNode.appendChild(td);
52                     return (true);
53                 }
54                 else {
55                     ct = 0;
56                     curtag = curtag.parentNode.nextSibling;
57                 }
58             }
59         }
60         else {
61             curtag = curtag.parentNode;
62         }
63
64     } while (curtag != null);
65
66     tr = document.createElement("tr");
67     tr.appendChild(td);
68     tbody.appendChild(tr);
69
70     return (true);
71 }
72
73 function spcs(c1, c2, n)
74 {
75     var ret = "";
76     var i;
77
78     for (i = 0 ; i < n ; i++) {
79         if ((i % 2) == 0)
80             ret += c1;
81         else
82             ret += c2;
83     }
84
85     return (ret);
86 }
87
88
89 function table_walk(curtag)
90 {
91     do {
92         // console.log("wt: "+curtag.tagName);
93         if (curtag.tagName.toLowerCase() == "div" || 
94             curtag.tagName.toLowerCase() == "table" ||
95             curtag.tagName.toLowerCase() == "tbody") {
96             curtag = curtag.firstChild;
97         }
98         else if (curtag.tagName.toLowerCase() == "tr") {
99             if (curtag.firstChild != null)
100                 curtag = curtag.firstChild;
101             else if (curtag.tagName != '')
102                 curtag = curtag.nextSibling;
103             else
104                 curtag = null;
105         }
106         else if (curtag.tagName.toLowerCase() == "td") {
107             if (curtag.nextSibling != null)
108                 curtag = curtag.nextSibling;
109             else {
110                 if (curtag.parentNode.nextSibling != null && curtag.parentNode.nextSibling.tagName != '')
111                     curtag = curtag.parentNode.nextSibling;
112                 else
113                     curtag = null;
114             }
115         }
116         else
117             curtag = null;
118
119     } while (curtag != null && curtag.tagName.toLowerCase() != "td");
120
121     if (1 == 0) {
122         if (curtag == null)
123             alert("outtag == null"); 
124         else
125             alert("outtag: "+curtag.tagName);
126     }
127     return (curtag);
128 }
129
130 function j_stand_tdcont(el)
131 {
132     var content = "";
133
134     if (el[0] & 0x01)
135         content += '<b>';
136     
137     if (el[0] & 0x02)
138         content += '<i>';
139     
140     content += el[1];
141     
142     if (el[0] & 0x02)
143         content += '</i>';
144     
145     if (el[0] & 0x01)
146         content += '</b>';
147             
148     content += state_add(el[0]);
149     
150     return (content);
151 }
152
153 function j_stand_cont(data)
154 {
155     var i;
156     var content;
157     var st = 0, name = "";
158     var curtag, nextag;
159
160     if (standup_data_old == null || data.length < 4) {
161     // if (standup_data_old == null) {
162         
163         content = '<table cols="'+(data.length < 4 ? data.length : 4)+'" class="table_standup">';
164         for (i = 0 ; i < data.length ; i++) {
165             if ((i % 4) == 0)
166                 content += '<tr>';
167             content += '<td id="'+i+'" class="room_standup">';
168             content += j_stand_tdcont(data[i]);
169             content += '</td>';
170             
171             if ((i % 4) == 3)
172                 content += '</tr>';
173         }
174         content += '</table>';
175         
176         $("standup").innerHTML = content;
177
178         // console.log("inizio");
179         // for (i = 0 , curtag = table_walk($("standup")) ; curtag != null ;  curtag = table_walk(curtag), i++ ) {
180         //     console.log("inloop["+i+"]: "+curtag.tagName+"  ID: "+curtag.id);
181         // }
182         // console.log("fine "+i);
183
184         // walktable($("standup"), nextag);
185         // console.log($("standup").firstChild);
186         // console.log($("standup").firstChild.firstChild.firstChild.firstChild);
187
188         // log_walk($("standup"));
189
190         standup_data_old = data;
191     }
192     else {
193         var idx_del, arr_add, idx_mod, arr_mod;
194         var idx_del_n = 0, idx_add_n = 0, idx_mod_n = 0;
195         var i, e;
196         var i_del, i_mod, i_add;
197         var td;
198
199         idx_del = new Array();
200         arr_add = new Array();
201         map_add = new Array();
202         idx_mod = new Array();
203         arr_mod = new Array();
204         map_cur = new Array();
205         
206         // find removed entries
207         for (i = 0 ; i < standup_data_old.length ; i++) {
208             for (e = 0 ; e < data.length ; e++) {
209                 if (standup_data_old[i][1] == data[e][1]) {
210                     break;
211                 }
212             }
213             if (e == data.length) {
214                 idx_del[idx_del_n++] = i;
215                 map_cur[i] = -1;
216             }
217             else {
218                 /* modified entries */
219                 if (standup_data_old[i][0] != data[e][0]) {
220                     arr_mod[idx_mod_n] = data[e];
221                     idx_mod[idx_mod_n++] = i;
222                 }
223                 map_cur[i] = e;
224             }
225         }
226
227         // find new entries
228         for (e = 0 ; e < data.length ; e++) {
229             for (i = 0 ; i < standup_data_old.length ; i++) {
230                 if (data[e][1] == standup_data_old[i][1] ) {
231                     break;
232                 }
233             }
234             if (i == standup_data_old.length) {
235                 // console.log("ADD: "+data[e][1]);
236                 arr_add[idx_add_n]   = data[e];
237                 map_add[idx_add_n++] = e;
238             }
239         }
240         
241         // TODO: qui travaso add in del
242
243         i_del = 0;
244         // alert("del: ["+j_stand_tdcont(standup_data_old[idx_del[i_del]])+"]");
245         for (i = 0 , i_del = 0, i_mod = 0, i_add = 0, curtag = table_walk($("standup")) ; curtag != null ;  curtag = table_walk(curtag), i++ ) {
246             // console.log("cur.id: "+curtag.id);
247
248             // alert("i: "+i+"  tagname: "+curtag.tagName+"  innerHTML: ["+curtag.innerHTML+"]");
249             // console.log("inloop["+i+"]: "+curtag.tagName+"  ID: "+curtag.id);
250             if (curtag.innerHTML == "") {
251                 // console.log("innerHTML == none");
252                 if (i_add < idx_add_n) {
253                     // console.log("  to be new");
254                     // console.log("  add:   CONT:"+j_stand_tdcont(arr_add[i_add]));
255                     curtag.innerHTML = j_stand_tdcont(arr_add[i_add]);
256                     curtag.id = map_add[i_add];
257                     i_add++
258                 }
259             }
260
261             // else if (i_del < idx_del_n && curtag.innerHTML == j_stand_tdcont(standup_data_old[idx_del[i_del]])) {
262             else if (i_del < idx_del_n && curtag.id == idx_del[i_del]) {
263                 // console.log("to be cancel["+i+"]:  ID: "+curtag.id);
264                 if (i_add < idx_add_n) {
265                     // console.log("  to be new");
266                     // console.log("  add:   CONT:"+j_stand_tdcont(arr_add[i_add]));
267                     curtag.innerHTML = j_stand_tdcont(arr_add[i_add]);
268                     curtag.id = map_add[i_add];
269                     i_add++
270                 }
271                 else {
272                     // console.log("  to be del");
273                     curtag.innerHTML = "";
274                     curtag.id = -1;
275                 }
276                 i_del++;
277             }
278             // else if (i_mod < idx_mod_n && curtag.innerHTML == j_stand_tdcont(standup_data_old[idx_mod[i_mod]])) {
279             else if (i_mod < idx_mod_n && curtag.id == idx_mod[i_mod]) {
280                 // console.log("  to be mod");
281                 // console.log("mod: "+idx_mod[i_mod]+ "  CONT:"+j_stand_tdcont(arr_mod[i_mod]));
282                 curtag.innerHTML = j_stand_tdcont(arr_mod[i_mod]);
283                 curtag.id = map_cur[curtag.id];
284                 i_mod++;
285             }
286             else
287                 curtag.id = map_cur[curtag.id];
288         }
289         // console.log("fineloop");
290
291         for (i ; i_add < idx_add_n ; i_add++, i++) {
292             // console.log("ADD: "+i+" arr_add: "+ arr_add[i_add][1]);
293             td = document.createElement("td");
294             td.className = "room_standup";
295             td.id = map_add[i_add];
296             td.innerHTML = j_stand_tdcont(arr_add[i_add]);
297
298             table_add($("standup"), td);
299         }
300
301         standup_data_old = data;
302         return;
303     }
304     // $("esco").innerHTML =  '<input class="button" name="logout" value="Esco." onclick="esco_cb();" type="button">';
305 }
306
307 function esco_cb() {
308     window.onbeforeunload = null; 
309     window.onunload = null; 
310     // nonunload = true; 
311     act_logout();
312  };
313
314
315
316 function j_selfauth_manager(form, lang)
317 {
318     var token, no;
319     
320     do {
321         if (form.elements['i_userid'].value == '' || j_check_email(form.elements['i_emailid'].value) == false || (form.elements['i_action'] == 'insert' &&form.elements['i_passwordid'].value == '')) {
322             if (lang == 'en') 
323                 no = new notify(false, "<br><b>e-mail</b> and/or <b>user</b> and/or <b>pwd</b> fields are invalid.<br><br>Please fix them.", 1, "close", 360, 100);             
324             else 
325                 no = new notify(false, "<br>I campi <b>e-mail</b> e/o <b>user</b> e/o <b>pwd</b> non sono validi.<br><br>Correggili per favore.", 1, "chiudi", 360, 100);             
326
327             break;
328         }
329         else if (form.elements['i_passwordid'].value != form.elements['i_confpassid'].value) {
330             if (lang == 'en') 
331                 no = new notify(false, "<br>The two passwords don't match. Insert them again.", 1, "close", 360, 100); 
332             else
333                 no = new notify(false, "<br>Le due password che hai inserito non combaciano. Immettile di nuovo.", 1, "chiudi", 360, 100); 
334             break;
335         }
336
337         if (form.elements['i_passwordid'].value != "")
338             token = calcMD5(form.elements['i_passwordid'].value);
339         else
340             token = "";
341
342         form.elements['i_pass_privateid'].value = token;
343         form.elements['i_passwordid'].value = "";
344         form.elements['i_confpassid'].value = ""; 
345
346         return (true);
347     } while (0);
348     
349     return (false);
350 }
351
352 function selfauth_formtext_hilite()
353 {
354     if ($("selfauth_formid").elements['i_actionid'].value != "show") {
355         formtext_hilite($("i_emailid"));
356         formtext_hilite($("i_userid"));
357         formtext_hilite($("i_passwordid"));
358         formtext_hilite($("i_confpassid"));
359         formsub_hilite($("sub"));
360     }
361 }
362
363 function selfauth_init()
364 {
365     selfauth_formtext_hilite();
366 }
367
368 function j_check_email(email)
369 {
370     if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email))
371         return (true);
372     return (false);
373 }
374