TomatoAnon project
[tomato.git] / release / src / router / www / tomato.js
blob22f117b9632224a7afc7d8588023ac66e6dec8ac
1 /*
2         Tomato GUI
3         Copyright (C) 2006-2010 Jonathan Zarate
4         http://www.polarcloud.com/tomato/
6         For use with Tomato Firmware only.
7         No part of this file may be used without permission.
8 */
10 // -----------------------------------------------------------------------------
12 Array.prototype.find = function(v) {
13         for (var i = 0; i < this.length; ++i)
14                 if (this[i] == v) return i;
15         return -1;
18 Array.prototype.remove = function(v) {
19         for (var i = 0; i < this.length; ++i) {
20                 if (this[i] == v) {
21                         this.splice(i, 1);
22                         return true;
23                 }
24         }
25         return false;
28 // -----------------------------------------------------------------------------
30 String.prototype.trim = function() {
31         return this.replace(/^\s+/, '').replace(/\s+$/, '');
34 // -----------------------------------------------------------------------------
36 Number.prototype.pad = function(min) {
37         var s = this.toString();
38         while (s.length < min) s = '0' + s;
39         return s;
42 Number.prototype.hex = function(min)
44         var h = '0123456789ABCDEF';
45         var n = this;
46         var s = '';
47         do {
48                 s = h.charAt(n & 15) + s;
49                 n = n >>> 4;
50         } while ((--min > 0) || (n > 0));
51         return s;
54 // -----------------------------------------------------------------------------
56 // ---- Element.protoype. doesn't work with all browsers
58 var elem = {
59         getOffset: function(e) {
60                 var r = { x: 0, y: 0 };
61                 e = E(e);
62                 while (e.offsetParent) {
63                         r.x += e.offsetLeft;
64                         r.y += e.offsetTop;
65                         e = e.offsetParent;
66                 }
67                 return r;
68         },
70         addClass: function(e, name) {
71                 if ((e = E(e)) == null) return;
72                 var a = e.className.split(/\s+/);
73                 var k = 0;
74                 for (var i = 1; i < arguments.length; ++i) {
75                         if (a.find(arguments[i]) == -1) {
76                                 a.push(arguments[i]);
77                                 k = 1;
78                         }
79                 }
80                 if (k) e.className = a.join(' ');
81         },
83         removeClass: function(e, name) {
84                 if ((e = E(e)) == null) return;
85                 var a = e.className.split(/\s+/);
86                 var k = 0;
87                 for (var i = 1; i < arguments.length; ++i)
88                         k |= a.remove(arguments[i]);
89                 if (k) e.className = a.join(' ');
90         },
92         remove: function(e) {
93                  if ((e = E(e)) != null) e.parentNode.removeChild(e);
94         },
96     parentElem: function(e, tagName) {
97                 e = E(e);
98                 tagName = tagName.toUpperCase();
99                 while (e.parentNode) {
100                         e = e.parentNode;
101                         if (e.tagName == tagName) return e;
102                 }
103                 return null;
104         },
106         display: function() {
107                 var enable = arguments[arguments.length - 1];
108                 for (var i = 0; i < arguments.length - 1; ++i) {
109                         E(arguments[i]).style.display = enable ? '' : 'none';
110                 }
111         },
113         isVisible: function(e) {
114                 e = E(e);
115                 while (e) {
116                         if ((e.style.visibility != 'visible') || (e.style.display == 'none')) return false;
117                         e = e.parentNode;
118                 }
119                 return true;
120         },
122         setInnerHTML: function(e, html) {
123                  e = E(e);
124                  if (e.innerHTML != html) e.innerHTML = html;   // reduce flickering
125         }
128 // -----------------------------------------------------------------------------
130 var docu = {
131         getViewSize: function() {
132                 if (window.innerHeight) {
133                         return { width: window.innerWidth, height: window.innerHeight };
134                 }
135                 else if (document.documentElement && document.documentElement.clientHeight) {
136                         return { width: document.documentElement.clientWidth, height: document.documentElement.clientHeight };
137                 }
138                 return { width: document.body.clientWidth, height: document.body.clientHeight };
139         },
141         getPageOffset: function()
142         {
143                 if (typeof(window.pageYOffset) != 'undefined') {
144                         return { x: window.pageXOffset, y: window.pageYOffset };
145                 }
146                 else if ((document.documentElement) && (typeof(document.documentElement.scrollTop) != 'undefined')) {
147                         return { x: document.documentElement.scrollLeft, y: document.documentElement.scrollTop };
148                 }
149                 return { x: document.body.scrollLeft, y: document.body.scrollTop };
150         }
153 // -----------------------------------------------------------------------------
155 var fields = {
156         getAll: function(e) {
157                 var a = [];
158                 switch (e.tagName) {
159                 case 'INPUT':
160                 case 'SELECT':
161                         a.push(e);
162                         break;
163                 default:
164                         if (e.childNodes) {
165                                 for (var i = 0; i < e.childNodes.length; ++i) {
166                                         a = a.concat(fields.getAll(e.childNodes[i]));
167                                 }
168                         }
169                 }
170                 return a;
171         },
172         disableAll: function(e, d) {
173                 var i;
175                 if ((typeof(e.tagName) == 'undefined') && (typeof(e) != 'string')) {
176                         for (i = e.length - 1; i >= 0; --i) {
177                                 e[i].disabled = d;
178                         }
179                 }
180                 else {
181                         var a = this.getAll(E(e));
182                         for (var i = a.length - 1; i >= 0; --i) {
183                                 a[i].disabled = d;
184                         }
185                 }
186         },
187         radio: {
188                 selected: function(e) {
189                         for (var i = 0; i < e.length; ++i) {
190                                 if (e[i].checked) return e[i];
191                         }
192                         return null;
193                 },
194                 find: function(e, value) {
195                         for (var i = 0; i < e.length; ++i) {
196                                 if (e[i].value == value) return e[i];
197                         }
198                         return null;
199                 }
200         }
203 // -----------------------------------------------------------------------------
205 var form = {
206         submitHidden: function(url, fields) {
207                 var fom, body;
209                 fom = document.createElement('FORM');
210                 fom.action = url;
211                 fom.method = 'post';
212                 for (var f in fields) {
213                         var e = document.createElement('INPUT');
214                         e.type = 'hidden';
215                         e.name = f;
216                         e.value = fields[f];
217                         fom.appendChild(e);
218                 }
219                 body = document.getElementsByTagName('body')[0];
220                 fom = body.appendChild(fom);
221                 this.submit(fom);
222                 body.removeChild(fom);
223         },
225         submit: function(fom, async, url) {
226                 var e, v, f, i, wait, msg, sb, cb;
228                 fom = E(fom);
230                 if (isLocal()) {
231                         this.dump(fom, async, url);
232                         return;
233                 }
235                 if (this.xhttp) return;
237                 if ((sb = E('save-button')) != null) sb.disabled = 1;
238                 if ((cb = E('cancel-button')) != null) cb.disabled = 1;
240                 if ((!async) || (!useAjax())) {
241                         this.addId(fom);
242                         if (url) fom.action = url;
243                         fom.submit();
244                         return;
245                 }
247                 v = ['_ajax=1'];
248                 wait = 5;
249                 for (var i = 0; i < fom.elements.length; ++i) {
250                         f = fom.elements[i];
251                         if ((f.disabled) || (f.name == '') || (f.name.substr(0, 2) == 'f_')) continue;
252                         if ((f.tagName == 'INPUT') && ((f.type == 'CHECKBOX') || (f.type == 'RADIO')) && (!f.checked)) continue;
253                         if (f.name == '_nextwait') {
254                                 wait = f.value * 1;
255                                 if (isNaN(wait)) wait = 5;
256                                         else wait = Math.abs(wait);
257                         }
258                         v.push(escapeCGI(f.name) + '=' + escapeCGI(f.value));
259                 }
261                 if ((msg = E('footer-msg')) != null) {
262                         msg.innerHTML = 'Saving...';
263                         msg.style.visibility = 'visible';
264                 }
266                 this.xhttp = new XmlHttp();
267                 this.xhttp.onCompleted = function(text, xml) {
268                         if (msg) {
269                                 if (text.match(/@msg:(.+)/)) msg.innerHTML = escapeHTML(RegExp.$1);
270                                         else msg.innerHTML = 'Saved';
271                         }
272                         setTimeout(
273                                 function() {
274                                         if (sb) sb.disabled = 0;
275                                         if (cb) cb.disabled = 0;
276                                         if (msg) msg.style.visibility = 'hidden';
277                                         if (typeof(submit_complete) != 'undefined') submit_complete();
278                                 }, wait * 1100);
279                         form.xhttp = null;
280                 }
281                 this.xhttp.onError = function(x) {
282                         if (url) fom.action = url;
283                         fom.submit();
284                 }
286                 this.xhttp.post(url ? url : fom.action, v.join('&'));
287         },
289         addId: function(fom) {
290                 var e;
292                 if (typeof(fom._http_id) == 'undefined') {
293                         e = document.createElement('INPUT');
294                         e.type = 'hidden';
295                         e.name = '_http_id';
296                         e.value = nvram.http_id;
297                         fom.appendChild(e);
298                 }
299                 else {
300                         fom._http_id.value = nvram.http_id;
301                 }
302         },
304         addIdAction: function(fom) {
305                 if (fom.action.indexOf('?') != -1) fom.action += '&_http_id=' + nvram.http_id;
306                         else fom.action += '?_http_id=' + nvram.http_id;
307         },
309         dump: function(fom, async, url) {
310         }
313 // -----------------------------------------------------------------------------
315 var ferror = {
316         set: function(e, message, quiet) {
317                 if ((e = E(e)) == null) return;
318                 e._error_msg = message;
319                 e._error_org = e.title;
320                 e.title = message;
321                 elem.addClass(e, 'error');
322                 if (!quiet) this.show(e);
323         },
325         clear: function(e) {
326                 if ((e = E(e)) == null) return;
327                 e.title = e._error_org || '';
328                 elem.removeClass(e, 'error');
329                 e._error_msg = null;
330                 e._error_org = null;
331         },
333         clearAll: function(e) {
334                 for (var i = 0; i < e.length; ++i)
335                         this.clear(e[i]);
336         },
338         show: function(e) {
339                 if ((e = E(e)) == null) return;
340                 if (!e._error_msg) return;
341                 elem.addClass(e, 'error-focused');
342                 e.focus();
343                 alert(e._error_msg);
344                 elem.removeClass(e, 'error-focused');
345         },
347         ok: function(e) {
348                 if ((e = E(e)) == null) return 0;
349         return !e._error_msg;
350         }
353 // -----------------------------------------------------------------------------
355 function fixFile(name)
357         var i;
358         if (((i = name.lastIndexOf('/')) > 0) || ((i = name.lastIndexOf('\\')) > 0))
359                 name = name.substring(i + 1, name.length);
360         return name;
363 function _v_range(e, quiet, min, max, name)
365         if ((e = E(e)) == null) return 0;
366         var v = e.value;
367         if ((!v.match(/^ *[-\+]?\d+ *$/)) || (v < min) || (v > max)) {
368                 ferror.set(e, 'Invalid ' + name + '. Valid range: ' + min + '-' + max, quiet);
369                 return 0;
370         }
371         e.value = v * 1;
372         ferror.clear(e);
373         return 1;
376 function v_range(e, quiet, min, max)
378         return _v_range(e, quiet, min, max, 'number');
381 function v_port(e, quiet)
383         return _v_range(e, quiet, 1, 0xFFFF, 'port');
386 function v_octet(e, quiet)
388         return _v_range(e, quiet, 1, 254, 'address');
391 function v_mins(e, quiet, min, max)
393         var v, m;
395         if ((e = E(e)) == null) return 0;
396         if (e.value.match(/^\s*(.+?)([mhd])?\s*$/)) {
397                 m = 1;
398                 if (RegExp.$2 == 'h') m = 60;
399                         else if (RegExp.$2 == 'd') m = 60 * 24;
400                 v = Math.round(RegExp.$1 * m);
401                 if (!isNaN(v)) {
402                         e.value = v;
403                         return _v_range(e, quiet, min, max, 'minutes');
404                 }
405         }
406         ferror.set(e, 'Invalid number of minutes.', quiet);
407         return 0;
410 function v_macip(e, quiet, bok, lan_ipaddr, lan_netmask)
412         var s, a, b, c, d, i;
413         var ipp, temp;
415         temp = lan_ipaddr.split('.');
416         ipp = temp[0]+'.'+temp[1]+'.'+temp[2]+'.';
418         if ((e = E(e)) == null) return 0;
419         s = e.value.replace(/\s+/g, '');
421         if ((a = fixMAC(s)) != null) {
422                 if (isMAC0(a)) {
423                         if (bok) {
424                                 e.value = '';
425                         }
426                         else {
427                                 ferror.set(e, 'Invalid MAC or IP address');
428                                 return false;
429                         }
430                 }
431                 else e.value = a;
432                 ferror.clear(e);
433                 return true;
434         }
436         a = s.split('-');
437     
438         if (a.length > 2) {
439                 ferror.set(e, 'Invalid IP address range', quiet);
440                 return false;
441         }
442         
443         if (a[0].match(/^\d+$/)){
444                 a[0]=ipp+a[0];
445                 if ((a.length == 2) && (a[1].match(/^\d+$/)))
446                         a[1]=ipp+a[1];
447         }
448         else{
449                 if ((a.length == 2) && (a[1].match(/^\d+$/))){
450                         temp=a[0].split('.');
451                         a[1]=temp[0]+'.'+temp[1]+'.'+temp[2]+'.'+a[1];
452                 }
453         }
454         for (i = 0; i < a.length; ++i) {
455                 b = a[i];    
456                 b = fixIP(b);
457                 if (!b) {
458                         ferror.set(e, 'Invalid IP address', quiet);
459                         return false;
460                 }
462                 if ((aton(b) & aton(lan_netmask))!=(aton(lan_ipaddr) & aton(lan_netmask))) {
463                         ferror.set(e, 'IP address outside of LAN', quiet);
464                         return false;
465                 }
467                 d = (b.split('.'))[3];
468                 if (parseInt(d) <= parseInt(c)) {
469                         ferror.set(e, 'Invalid IP address range', quiet);
470                         return false;
471                 }
473                 a[i] = c = d;
474         }
475         e.value = b.split('.')[0] + '.' + b.split('.')[1] + '.' + b.split('.')[2] + '.' + a.join('-');
476         return true;
479 function fixIP(ip, x)
481         var a, n, i;
483         a = ip.split('.');
484         if (a.length != 4) return null;
485         for (i = 0; i < 4; ++i) {
486                 n = a[i] * 1;
487                 if ((isNaN(n)) || (n < 0) || (n > 255)) return null;
488                 a[i] = n;
489         }
490         if ((x) && ((a[3] == 0) || (a[3] == 255))) return null;
491         return a.join('.');
494 function v_ip(e, quiet, x)
496         var ip;
498         if ((e = E(e)) == null) return 0;
499         ip = fixIP(e.value, x);
500         if (!ip) {
501                 ferror.set(e, 'Invalid IP address', quiet);
502                 return false;
503         }
504         e.value = ip;
505         ferror.clear(e);
506         return true;
509 function v_ipz(e, quiet)
511         if ((e = E(e)) == null) return 0;
512         if (e.value == '') e.value = '0.0.0.0';
513         return v_ip(e, quiet);
516 function v_dns(e, quiet)
518         if ((e = E(e)) == null) return 0;       
519         if (e.value == '') {
520                 e.value = '0.0.0.0';
521         }
522         else {
523                 var s = e.value.split(':');
524                 if (s.length == 1) {
525                         s.push(53);
526                 }
527                 else if (s.length != 2) {
528                         ferror.set(e, 'Invalid IP address or port', quiet);
529                         return false;
530                 }
531                 
532                 if ((s[0] = fixIP(s[0])) == null) {
533                         ferror.set(e, 'Invalid IP address', quiet);
534                         return false;
535                 }
537                 if ((s[1] = fixPort(s[1], -1)) == -1) {
538                         ferror.set(e, 'Invalid port', quiet);
539                         return false;
540                 }
541         
542                 if (s[1] == 53) {
543                         e.value = s[0];
544                 }
545                 else {
546                         e.value = s.join(':');
547                 }
548         }
550         ferror.clear(e);
551         return true;
554 function aton(ip)
556         var o, x, i;
558         // ---- this is goofy because << mangles numbers as signed
559         o = ip.split('.');
560         x = '';
561         for (i = 0; i < 4; ++i) x += (o[i] * 1).hex(2);
562         return parseInt(x, 16);
565 function ntoa(ip)
567         return ((ip >> 24) & 255) + '.' + ((ip >> 16) & 255) + '.' + ((ip >> 8) & 255) + '.' + (ip & 255);
571 // ---- 1.2.3.4, 1.2.3.4/24, 1.2.3.4/255.255.255.0, 1.2.3.4-1.2.3.5
572 function _v_iptip(e, ip, quiet)
574         var ma, x, y, z, oip;
575         var a, b;
577         oip = ip;
579         // x.x.x.x - y.y.y.y
580         if (ip.match(/^(.*)-(.*)$/)) {
581                 a = fixIP(RegExp.$1);
582                 b = fixIP(RegExp.$2);
583                 if ((a == null) || (b == null)) {
584                         ferror.set(e, oip + ' - invalid IP address range', quiet);
585                         return null;
586                 }
587                 ferror.clear(e);
589                 if (aton(a) > aton(b)) return b + '-' + a;
590                 return a + '-' + b;
591         }
593         ma = '';
595         // x.x.x.x/nn
596         // x.x.x.x/y.y.y.y
597         if (ip.match(/^(.*)\/(.*)$/)) {
598                 ip = RegExp.$1;
599                 b = RegExp.$2;
601                 ma = b * 1;
602                 if (isNaN(ma)) {
603                         ma = fixIP(b);
604                         if ((ma == null) || (!_v_netmask(ma))) {
605                                 ferror.set(e, oip + ' - invalid netmask', quiet);
606                                 return null;
607                         }
608                 }
609                 else {
610                         if ((ma < 0) || (ma > 32)) {
611                                 ferror.set(e, oip + ' - invalid netmask', quiet);
612                                 return null;
613                         }
614                 }
615         }
617         ip = fixIP(ip);
618         if (!ip) {
619                 ferror.set(e, oip + ' - invalid IP address', quiet);
620                 return null;
621         }
623         ferror.clear(e);
624         return ip + ((ma != '') ? ('/' + ma) : '');
627 function v_iptip(e, quiet, multi)
629         var v, i;
631         if ((e = E(e)) == null) return 0;
632         v = e.value.split(',');
633         if (multi) {
634                 if (v.length > multi) {
635                         ferror.set(e, 'Too many IP addresses', quiet);
636                         return 0;
637                 }
638         }
639         else {
640                 if (v.length > 1) {
641                         ferror.set(e, 'Invalid IP address', quiet);
642                         return 0;
643                 }
644         }
645         for (i = 0; i < v.length; ++i) {
646                 if ((v[i] = _v_iptip(e, v[i], quiet)) == null) return 0;
647         }
648         e.value = v.join(', ');
649         return 1;
652 function _v_domain(e, dom, quiet)
654         var s;
656         s = dom.replace(/\s+/g, ' ').trim();
657         if (s.length > 0) {
658                 s = _v_hostname(e, s, 1, 1, 7, '.', true);
659                 if (s == null) {
660                         ferror.set(e, "Invalid name. Only characters \"A-Z 0-9 . -\" are allowed.", quiet);
661                         return null;
662                 }
663         }
664         ferror.clear(e);
665         return s;
668 function v_domain(e, quiet)
670         var v;
672         if ((e = E(e)) == null) return 0;
673         if ((v = _v_domain(e, e.value, quiet)) == null) return 0;
675         e.value = v;
676         return 1;
679 /* IPV6-BEGIN */
680 function ExpandIPv6Address(ip)
682         var a, pre, n, i, fill, post;
684         ip = ip.toLowerCase();
685         if (!ip.match(/^(::)?([a-f0-9]{1,4}::?){0,7}([a-f0-9]{1,4})(::)?$/)) return null;
687         a = ip.split('::');
688         switch (a.length) {
689         case 1:
690                 if (a[0] == '') return null;
691                 pre = a[0].split(':');
692                 if (pre.length != 8) return null;
693                 ip = pre.join(':');
694                 break;
695         case 2:
696                 pre = a[0].split(':');
697                 post = a[1].split(':');
698                 n = 8 - pre.length - post.length;
699                 for (i=0; i<2; i++) {
700                         if (a[i]=='') n++;
701                 }
702                 if (n < 0) return null;
703                 fill = '';
704                 while (n-- > 0) fill += ':0';
705                 ip = pre.join(':') + fill + ':' + post.join(':');
706                 ip = ip.replace(/^:/, '').replace(/:$/, '');
707                 break;
708         default:
709                 return null;
710         }
711         
712         ip = ip.replace(/([a-f0-9]{1,4})/ig, '000$1');
713         ip = ip.replace(/0{0,3}([a-f0-9]{4})/ig, '$1');
714         return ip;
717 function CompressIPv6Address(ip)
719         var a, segments;
720         
721         ip = ExpandIPv6Address(ip);
722         if (!ip) return null;
723         
724         // if (ip.match(/(?:^00)|(?:^fe[8-9a-b])|(?:^ff)/)) return null; // not valid routable unicast address
726         ip = ip.replace(/(^|:)0{1,3}/g, '$1');
727         ip = ip.replace(/(:0)+$/, '::');
728         ip = ip.replace(/(?:(?:^|:)0){2,}(?!.*(?:::|(?::0){3,}))/, ':');
729         return ip;
732 function ZeroIPv6PrefixBits(ip, prefix_length)
734         var b, c, m, n;
735         ip = ExpandIPv6Address(ip);
736         ip = ip.replace(/:/g,'');
737         n = Math.floor(prefix_length/4);
738         m = 32 - Math.ceil(prefix_length/4);
739         b = prefix_length % 4;
740         if (b != 0) 
741                 c = (parseInt(ip.charAt(n), 16) & (0xf << 4-b)).toString(16);
742         else
743                 c = '';
744         
745         ip = ip.substring(0, n) + c + Array((m%4)+1).join('0') + (m>=4 ? '::' : '');
746         ip = ip.replace(/([a-f0-9]{4})(?=[a-f0-9])/g,'$1:');
747         ip = ip.replace(/(^|:)0{1,3}/g, '$1');
748         return ip;
751 function ipv6ton(ip)
753         var o, x, i;
755         ip = ExpandIPv6Address(ip);
756         if (!ip) return 0;
758         o = ip.split(':');
759         x = '';
760         for (i = 0; i < 8; ++i) x += (('0x' + o[i]) * 1).hex(4);
761         return parseInt(x, 16);
764 function _v_ipv6_addr(e, ip, ipt, quiet)
766         var oip;
767         var a, b;
769         oip = ip;
771         // ip range
772         if ((ipt) && ip.match(/^(.*)-(.*)$/)) {
773                 a = RegExp.$1;
774                 b = RegExp.$2;
775                 a = CompressIPv6Address(a);
776                 b = CompressIPv6Address(b);
777                 if ((a == null) || (b == null)) {
778                         ferror.set(e, oip + ' - invalid IPv6 address range', quiet);
779                         return null;
780                 }
781                 ferror.clear(e);
783                 if (ipv6ton(a) > ipv6ton(b)) return b + '-' + a;
784                 return a + '-' + b;
785         }
786         
787         if ((ipt) && ip.match(/^([A-Fa-f0-9:]+)\/(\d+)$/)) {
788                 a = RegExp.$1;
789                 b = parseInt(RegExp.$2, 10);
790                 a = ExpandIPv6Address(a);
791                 if ((a == null) || (b == null)) {
792                         ferror.set(e, oip + ' - invalid IPv6 address', quiet);
793                         return null;
794                 }
795                 if (b < 0 || b > 128) {
796                         ferror.set(e, oip + ' - invalid CIDR notation on IPv6 address', quiet);
797                         return null;
798                 }
799                 ferror.clear(e);
801                 ip = ZeroIPv6PrefixBits(a, b);
802                 return ip + '/' + b.toString(10);
803         }
805         ip = CompressIPv6Address(oip);
806         if (!ip) {
807                 ferror.set(e, oip + ' - invalid IPv6 address', quiet);
808                 return null;
809         }
811         ferror.clear(e);
812         return ip;
815 function v_ipv6_addr(e, quiet)
817         if ((e = E(e)) == null) return 0;
819         ip = _v_ipv6_addr(e, e.value, false, quiet);
820         if (ip) e.value = ip;
821         return (ip != null);
823 /* IPV6-END */
825 function fixPort(p, def)
827         if (def == null) def = -1;
828         if (p == null) return def;
829         p *= 1;
830         if ((isNaN(p) || (p < 1) || (p > 65535) || (('' + p).indexOf('.') != -1))) return def;
831         return p;
834 function _v_portrange(e, quiet, v)
836         if (v.match(/^(.*)[-:](.*)$/)) {
837                 var x = RegExp.$1;
838                 var y = RegExp.$2;
840                 x = fixPort(x, -1);
841                 y = fixPort(y, -1);
842                 if ((x == -1) || (y == -1)) {
843                         ferror.set(e, 'Invalid port range: ' + v, quiet);
844                         return null;
845                 }
846                 if (x > y) {
847                         v = x;
848                         x = y;
849                         y = v;
850                 }
851                 ferror.clear(e);
852                 if (x == y) return x;
853                 return x + '-' + y;
854         }
856         v = fixPort(v, -1);
857         if (v == -1) {
858                 ferror.set(e, 'Invalid port', quiet);
859                 return null;
860         }
862         ferror.clear(e);
863         return v;
866 function v_portrange(e, quiet)
868         var v;
870         if ((e = E(e)) == null) return 0;
871         v = _v_portrange(e, quiet, e.value);
872         if (v == null) return 0;
873         e.value = v;
874         return 1;
877 function v_iptport(e, quiet)
879         var a, i, v, q;
881         if ((e = E(e)) == null) return 0;
883         a = e.value.split(/[,\.]/);
885         if (a.length == 0) {
886                 ferror.set(e, 'Expecting a list of ports or port range.', quiet);
887                 return 0;
888         }
889         if (a.length > 10) {
890                 ferror.set(e, 'Only 10 ports/range sets are allowed.', quiet);
891                 return 0;
892         }
894         q = [];
895         for (i = 0; i < a.length; ++i) {
896                 v = _v_portrange(e, quiet, a[i]);
897                 if (v == null) return 0;
898                 q.push(v);
899         }
901         e.value = q.join(',');
902         ferror.clear(e);
903         return 1;
906 function _v_netmask(mask)
908         var v = aton(mask) ^ 0xFFFFFFFF;
909         return (((v + 1) & v) == 0);
912 function v_netmask(e, quiet)
914         var n, b;
916         if ((e = E(e)) == null) return 0;
917         n = fixIP(e.value);
918         if (n) {
919                 if (_v_netmask(n)) {
920                         e.value = n;
921                         ferror.clear(e);
922                         return 1;
923                 }
924         }
925         else if (e.value.match(/^\s*\/\s*(\d+)\s*$/)) {
926                 b = RegExp.$1 * 1;
927                 if ((b >= 1) && (b <= 32)) {
928                         if (b == 32) n = 0xFFFFFFFF;    // js quirk
929                                 else n = (0xFFFFFFFF >>> b) ^ 0xFFFFFFFF;
930                         e.value = (n >>> 24) + '.' + ((n >>> 16) & 0xFF) + '.' + ((n >>> 8) & 0xFF) + '.' + (n & 0xFF);
931                         ferror.clear(e);
932                         return 1;
933                 }
934         }
935         ferror.set(e, 'Invalid netmask', quiet);
936         return 0;
939 function fixMAC(mac)
941         var t, i;
943         mac = mac.replace(/\s+/g, '').toUpperCase();
944         if (mac.length == 0) {
945                 mac = [0,0,0,0,0,0];
946         }
947         else if (mac.length == 12) {
948                 mac = mac.match(/../g);
949         }
950         else {
951                 mac = mac.split(/[:\-]/);
952                 if (mac.length != 6) return null;
953         }
954         for (i = 0; i < 6; ++i) {
955                 t = '' + mac[i];
956                 if (t.search(/^[0-9A-F]+$/) == -1) return null;
957                 if ((t = parseInt(t, 16)) > 255) return null;
958                 mac[i] = t.hex(2);
959         }
960         return mac.join(':');
963 function v_mac(e, quiet)
965         var mac;
967         if ((e = E(e)) == null) return 0;
968         mac = fixMAC(e.value);
969         if ((!mac) || (isMAC0(mac))) {
970                 ferror.set(e, 'Invalid MAC address', quiet);
971                 return 0;
972         }
973         e.value = mac;
974         ferror.clear(e);
975         return 1;
978 function v_macz(e, quiet)
980         var mac;
982         if ((e = E(e)) == null) return 0;
983         mac = fixMAC(e.value);
984         if (!mac) {
985                 ferror.set(e, 'Invalid MAC address', quiet);
986                 return false;
987         }
988         e.value = mac;
989         ferror.clear(e);
990         return true;
993 function v_length(e, quiet, min, max)
995         var s, n;
997         if ((e = E(e)) == null) return 0;
998         s = e.value.trim();
999         n = s.length;
1000         if (min == undefined) min = 1;
1001         if (n < min) {
1002                 ferror.set(e, 'Invalid length. Please enter at least ' + min + ' character' + (min == 1 ? '.' : 's.'), quiet);
1003                 return 0;
1004         }
1005         max = max || e.maxlength;
1006         if (n > max) {
1007                 ferror.set(e, 'Invalid length. Please reduce the length to ' + max + ' characters or less.', quiet);
1008                 return 0;
1009         }
1010         e.value = s;
1011         ferror.clear(e);
1012         return 1;
1015 function _v_iptaddr(e, quiet, multi, ipv4, ipv6)
1017         var v, t, i;
1019         if ((e = E(e)) == null) return 0;
1020         v = e.value.split(',');
1021         if (multi) {
1022                 if (v.length > multi) {
1023                         ferror.set(e, 'Too many addresses', quiet);
1024                         return 0;
1025                 }
1026         }
1027         else {
1028                 if (v.length > 1) {
1029                         ferror.set(e, 'Invalid domain name or IP address', quiet);
1030                         return 0;
1031                 }
1032         }
1034         for (i = 0; i < v.length; ++i) {
1035                 if ((t = _v_domain(e, v[i], 1)) == null) {
1036 /* IPV6-BEGIN */
1037                         if ((!ipv6) && (!ipv4)) {
1038                                 if (!quiet) ferror.show(e);
1039                                 return 0;
1040                         }
1041                         if ((!ipv6) || ((t = _v_ipv6_addr(e, v[i], 1, 1)) == null)) {
1042 /* IPV6-END */
1043                                 if (!ipv4) {
1044                                         if (!quiet) ferror.show(e);
1045                                         return 0;
1046                                 }
1047                                 if ((t = _v_iptip(e, v[i], 1)) == null) {
1048                                         ferror.set(e, e._error_msg + ', or invalid domain name', quiet);
1049                                         return 0;
1050                                 }
1051 /* IPV6-BEGIN */
1052                         }
1053 /* IPV6-END */
1054                 }
1055                 v[i] = t;
1056         }
1058         e.value = v.join(', ');
1059         ferror.clear(e);
1060         return 1;
1063 function v_iptaddr(e, quiet, multi)
1065         return _v_iptaddr(e, quiet, multi, 1, 0);
1068 function _v_hostname(e, h, quiet, required, multi, delim, cidr)
1070         var s;
1071         var v, i;
1072         var re;
1074         v = (typeof(delim) == 'undefined') ? h.split(/\s+/) : h.split(delim);
1076         if (multi) {
1077                 if (v.length > multi) {
1078                         ferror.set(e, 'Too many hostnames.', quiet);
1079                         return null;
1080                 }
1081         }
1082         else {
1083                 if (v.length > 1) {
1084                         ferror.set(e, 'Invalid hostname.', quiet);
1085                         return null;
1086                 }
1087         }
1089         re = /^[a-zA-Z0-9](([a-zA-Z0-9\-]{0,61})[a-zA-Z0-9]){0,1}$/;
1091         for (i = 0; i < v.length; ++i) {
1092                 s = v[i].replace(/_+/g, '-').replace(/\s+/g, '-');
1093                 if (s.length > 0) {
1094                         if (cidr && i == v.length-1)
1095                                 re = /^[a-zA-Z0-9](([a-zA-Z0-9\-]{0,61})[a-zA-Z0-9]){0,1}(\/\d{1,3})?$/;
1096                         if (s.search(re) == -1 || s.search(/^\d+$/) != -1) {
1097                                 ferror.set(e, 'Invalid hostname. Only "A-Z 0-9" and "-" in the middle are allowed (up to 63 characters).', quiet);
1098                                 return null;
1099                         }
1100                 } else if (required) {
1101                         ferror.set(e, 'Invalid hostname.', quiet);
1102                         return null;
1103                 }
1104                 v[i] = s;
1105         }
1107         ferror.clear(e);
1108         return v.join((typeof(delim) == 'undefined') ? ' ' : delim);
1111 function v_hostname(e, quiet, multi, delim)
1113         var v;
1115         if ((e = E(e)) == null) return 0;
1117         v = _v_hostname(e, e.value, quiet, 0, multi, delim, false);
1119         if (v == null) return 0;
1121         e.value = v;
1122         return 1;
1125 function v_nodelim(e, quiet, name, checklist)
1127         if ((e = E(e)) == null) return 0;
1129         e.value = e.value.trim();
1130         if (e.value.indexOf('<') != -1 ||
1131            (checklist && e.value.indexOf('>') != -1)) {
1132                 ferror.set(e, 'Invalid ' + name + ': \"<\" ' + (checklist ? 'or \">\" are' : 'is') + ' not allowed.', quiet);
1133                 return 0;
1134         }
1135         ferror.clear(e);
1136         return 1;
1139 function v_path(e, quiet, required)
1141         if ((e = E(e)) == null) return 0;
1142         if (required && !v_length(e, quiet, 1)) return 0;
1144         if (!required && e.value.trim().length == 0) {
1145                 ferror.clear(e);
1146                 return 1;
1147         }
1148         if (e.value.substr(0, 1) != '/') {
1149                 ferror.set(e, 'Please start at the / root directory.', quiet);
1150                 return 0;
1151         }
1152         ferror.clear(e);
1153         return 1;
1156 function isMAC0(mac)
1158         return (mac == '00:00:00:00:00:00');
1161 // -----------------------------------------------------------------------------
1163 function cmpIP(a, b)
1165         if ((a = fixIP(a)) == null) a = '255.255.255.255';
1166         if ((b = fixIP(b)) == null) b = '255.255.255.255';
1167         return aton(a) - aton(b);
1170 function cmpText(a, b)
1172         if (a == '') a = '\xff';
1173         if (b == '') b = '\xff';
1174         return (a < b) ? -1 : ((a > b) ? 1 : 0);
1177 function cmpInt(a, b)
1179         a = parseInt(a, 10);
1180         b = parseInt(b, 10);
1181         return ((isNaN(a)) ? -0x7FFFFFFF : a) - ((isNaN(b)) ? -0x7FFFFFFF : b);
1184 function cmpFloat(a, b)
1186         a = parseFloat(a);
1187         b = parseFloat(b);
1188         return ((isNaN(a)) ? -Number.MAX_VALUE : a) - ((isNaN(b)) ? -Number.MAX_VALUE : b);
1191 function cmpDate(a, b)
1193         return b.getTime() - a.getTime();
1196 // -----------------------------------------------------------------------------
1198 // ---- todo: cleanup this mess
1200 function TGO(e)
1202         return elem.parentElem(e, 'TABLE').gridObj;
1205 function tgHideIcons()
1207         var e;
1208         while ((e = document.getElementById('tg-row-panel')) != null) e.parentNode.removeChild(e);
1211 // ---- options = sort, move, delete
1212 function TomatoGrid(tb, options, maxAdd, editorFields)
1214         this.init(tb, options, maxAdd, editorFields);
1215         return this;
1218 TomatoGrid.prototype = {
1219         init: function(tb, options, maxAdd, editorFields) {
1220                 if (tb) {
1221                         this.tb = E(tb);
1222                         this.tb.gridObj = this;
1223                 }
1224                 else {
1225                         this.tb = null;
1226                 }
1227                 if (!options) options = '';
1228                 this.header = null;
1229                 this.footer = null;
1230                 this.editor = null;
1231                 this.canSort = options.indexOf('sort') != -1;
1232                 this.canMove = options.indexOf('move') != -1;
1233                 this.maxAdd = maxAdd || 500;
1234                 this.canEdit = (editorFields != null);
1235                 this.canDelete = this.canEdit || (options.indexOf('delete') != -1);
1236                 this.editorFields = editorFields;
1237                 this.sortColumn = -1;
1238                 this.sortAscending = true;
1239         },
1241         _insert: function(at, cells, escCells) {
1242                 var tr, td, c;
1243                 var i, t;
1245                 tr = this.tb.insertRow(at);
1246                 for (i = 0; i < cells.length; ++i) {
1247                         c = cells[i];
1248                         if (typeof(c) == 'string') {
1249                                 td = tr.insertCell(i);
1250                                 td.className = 'co' + (i + 1);
1251                                 if (escCells) td.appendChild(document.createTextNode(c));
1252                                         else td.innerHTML = c;
1253                         }
1254                         else {
1255                                 tr.appendChild(c);
1256                         }
1257                 }
1258                 return tr;
1259         },
1261         // ---- header
1263         headerClick: function(cell) {
1264                 if (this.canSort) {
1265                         this.sort(cell.cellN);
1266                 }
1267         },
1269         headerSet: function(cells, escCells) {
1270                 var e, i;
1272                 elem.remove(this.header);
1273                 this.header = e = this._insert(0, cells, escCells);
1274                 e.className = 'header';
1276                 for (i = 0; i < e.cells.length; ++i) {
1277                         e.cells[i].cellN = i;   // cellIndex broken in Safari
1278                         e.cells[i].onclick = function() { return TGO(this).headerClick(this); };
1279                 }
1280                 return e;
1281         },
1283         // ---- footer
1285         footerClick: function(cell) {
1286         },
1288         footerSet: function(cells, escCells) {
1289                 var e, i;
1291                 elem.remove(this.footer);
1292                 this.footer = e = this._insert(-1, cells, escCells);
1293                 e.className = 'footer';
1294                 for (i = 0; i < e.cells.length; ++i) {
1295                         e.cells[i].cellN = i;
1296                         e.cells[i].onclick = function() { TGO(this).footerClick(this) };
1297                 }
1298                 return e;
1299         },
1301         // ----
1303         rpUp: function(e) {
1304                 var i;
1306                 e = PR(e);
1307                 TGO(e).moving = null;
1308                 i = e.previousSibling;
1309                 if (i == this.header) return;
1310                 e.parentNode.removeChild(e);
1311                 i.parentNode.insertBefore(e, i);
1313                 this.recolor();
1314                 this.rpHide();
1315         },
1317         rpDn: function(e) {
1318                 var i;
1320                 e = PR(e);
1321                 TGO(e).moving = null;
1322                 i = e.nextSibling;
1323                 if (i == this.footer) return;
1324                 e.parentNode.removeChild(e);
1325                 i.parentNode.insertBefore(e, i.nextSibling);
1327                 this.recolor();
1328                 this.rpHide();
1329         },
1331         rpMo: function(img, e) {
1332                 var me;
1334                 e = PR(e);
1335                 me = TGO(e);
1336                 if (me.moving == e) {
1337                         me.moving = null;
1338                         this.rpHide();
1339                         return;
1340                 }
1341                 me.moving = e;
1342                 img.style.border = "1px dotted red";
1343         },
1345         rpDel: function(e) {
1346                 e = PR(e);
1347                 TGO(e).moving = null;
1348                 e.parentNode.removeChild(e);
1349                 this.recolor();
1350                 this.rpHide();
1351         },
1353         rpMouIn: function(evt) {
1354                 var e, x, ofs, me, s, n;
1356                 if ((evt = checkEvent(evt)) == null) return;
1358                 me = TGO(evt.target);
1359                 if (me.isEditing()) return;
1360                 if (me.moving) return;
1362                 me.rpHide();
1363                 e = document.createElement('div');
1364                 e.tgo = me;
1365                 e.ref = evt.target;
1366                 e.setAttribute('id', 'tg-row-panel');
1368                 n = 0;
1369                 s = '';
1370                 if (me.canMove) {
1371                         s = '<img src="rpu.gif" onclick="this.parentNode.tgo.rpUp(this.parentNode.ref)" title="Move Up"><img src="rpd.gif" onclick="this.parentNode.tgo.rpDn(this.parentNode.ref)" title="Move Down"><img src="rpm.gif" onclick="this.parentNode.tgo.rpMo(this,this.parentNode.ref)" title="Move">';
1372                         n += 3;
1373                 }
1374                 if (me.canDelete) {
1375                         s += '<img src="rpx.gif" onclick="this.parentNode.tgo.rpDel(this.parentNode.ref)" title="Delete">';
1376                         ++n;
1377                 }
1378                 x = PR(evt.target);
1379                 x = x.cells[x.cells.length - 1];
1380                 ofs = elem.getOffset(x);
1381                 n *= 18;
1382                 e.style.left = (ofs.x + x.offsetWidth - n) + 'px';
1383                 e.style.top = ofs.y + 'px';
1384                 e.style.width = n + 'px';
1385                 e.innerHTML = s;
1387                 document.body.appendChild(e);
1388         },
1390         rpHide: tgHideIcons,
1392         // ----
1394         onClick: function(cell) {
1395                 if (this.canEdit) {
1396                         if (this.moving) {
1397                                 var p = this.moving.parentNode;
1398                                 var q = PR(cell);
1399                                 if (this.moving != q) {
1400                                         var v = this.moving.rowIndex > q.rowIndex;
1401                                         p.removeChild(this.moving);
1402                                         if (v) p.insertBefore(this.moving, q);
1403                                                 else p.insertBefore(this.moving, q.nextSibling);
1404                                         this.recolor();
1405                                 }
1406                                 this.moving = null;
1407                                 this.rpHide();
1408                                 return;
1409                         }
1410                         this.edit(cell);
1411                 }
1412         },
1414         insert: function(at, data, cells, escCells) {
1415                 var e, i;
1417                 if ((this.footer) && (at == -1)) at = this.footer.rowIndex;
1418                 e = this._insert(at, cells, escCells);
1419                 e.className = (e.rowIndex & 1) ? 'even' : 'odd';
1421                 for (i = 0; i < e.cells.length; ++i) {
1422                         e.cells[i].onclick = function() { return TGO(this).onClick(this); };
1423                 }
1425                 e._data = data;
1426                 e.getRowData = function() { return this._data; }
1427                 e.setRowData = function(data) { this._data = data; }
1429                 if ((this.canMove) || (this.canEdit) || (this.canDelete)) {
1430                         e.onmouseover = this.rpMouIn;
1431 // ----                 e.onmouseout = this.rpMouOut;
1432                         if (this.canEdit) e.title = 'Click to edit';
1433                 }
1435                 return e;
1436         },
1438         // ----
1440         insertData: function(at, data) {
1441                 return this.insert(at, data, this.dataToView(data), false);
1442         },
1444         dataToView: function(data) {
1445                 var v = [];
1446                 for (var i = 0; i < data.length; ++i) {
1447                         var s = escapeHTML('' + data[i]);
1448                         if (this.editorFields && this.editorFields.length > i) {
1449                                 var ef = this.editorFields[i].multi;
1450                                 if (!ef) ef = [this.editorFields[i]];
1451                                 var f = (ef && ef.length > 0 ? ef[0] : null);
1452                                 if (f && f.type == 'password') {
1453                                         if (!f.peekaboo || get_config('web_pb', '1') != '0')
1454                                                 s = s.replace(/./g, '&#x25CF;');
1455                                 }
1456                         }
1457                         v.push(s);
1458                 }
1459                 return v;
1460         },
1462         dataToFieldValues: function(data) {
1463                 return data;
1464         },
1466         fieldValuesToData: function(row) {
1467                 var e, i, data;
1469                 data = [];
1470                 e = fields.getAll(row);
1471                 for (i = 0; i < e.length; ++i) data.push(e[i].value);
1472                 return data;
1473         },
1475         // ----
1477         edit: function(cell) {
1478                 var sr, er, e, c;
1480                 if (this.isEditing()) return;
1482                 sr = PR(cell);
1483                 sr.style.display = 'none';
1484                 elem.removeClass(sr, 'hover');
1485                 this.source = sr;
1487                 er = this.createEditor('edit', sr.rowIndex, sr);
1488                 er.className = 'editor';
1489                 this.editor = er;
1491                 c = er.cells[cell.cellIndex || 0];
1492                 e = c.getElementsByTagName('input');
1493                 if ((e) && (e.length > 0)) {
1494                         try {   // IE quirk
1495                                 e[0].focus();
1496                         }
1497                         catch (ex) {
1498                         }
1499                 }
1501                 this.controls = this.createControls('edit', sr.rowIndex);
1503                 this.disableNewEditor(true);
1504                 this.rpHide();
1505                 this.verifyFields(this.editor, true);
1506         },
1508         createEditor: function(which, rowIndex, source) {
1509                 var values;
1511                 if (which == 'edit') values = this.dataToFieldValues(source.getRowData());
1513                 var row = this.tb.insertRow(rowIndex);
1514                 row.className = 'editor';
1516                 var common = ' onkeypress="return TGO(this).onKey(\'' + which + '\', event)" onchange="TGO(this).onChange(\'' + which + '\', this)"';
1518                 var vi = 0;
1519                 for (var i = 0; i < this.editorFields.length; ++i) {
1520                         var s = '';
1521                         var ef = this.editorFields[i].multi;
1522                         if (!ef) ef = [this.editorFields[i]];
1524                         for (var j = 0; j < ef.length; ++j) {
1525                                 var f = ef[j];
1527                                 if (f.prefix) s += f.prefix;
1528                                 var attrib = ' class="fi' + (vi + 1) + '" ' + (f.attrib || '');
1529                                 var id = (this.tb ? ('_' + this.tb + '_' + (vi + 1)) : null);
1530                                 if (id) attrib += ' id="' + id + '"';
1531                                 switch (f.type) {
1532                                 case 'password':
1533                                         if (f.peekaboo) {
1534                                                 switch (get_config('web_pb', '1')) {
1535                                                 case '0':
1536                                                         f.type = 'text';
1537                                                 case '2':
1538                                                         f.peekaboo = 0;
1539                                                         break;
1540                                                 }
1541                                         }
1542                                         attrib += ' autocomplete="off"';
1543                                         if (f.peekaboo && id) attrib += ' onfocus=\'peekaboo("' + id + '",1)\'';
1544                                         // drop
1545                                 case 'text':
1546                                         s += '<input type="' + f.type + '" maxlength=' + f.maxlen + common + attrib;
1547                                         if (which == 'edit') s += ' value="' + escapeHTML('' + values[vi]) + '">';
1548                                                 else s += '>';
1549                                         break;
1550                                 case 'select':
1551                                         s += '<select' + common + attrib + '>';
1552                                         for (var k = 0; k < f.options.length; ++k) {
1553                                                 a = f.options[k];
1554                                                 if (which == 'edit') {
1555                                                         s += '<option value="' + a[0] + '"' + ((a[0] == values[vi]) ? ' selected>' : '>') + a[1] + '</option>';
1556                                                 }
1557                                                 else {
1558                                                         s += '<option value="' + a[0] + '">' + a[1] + '</option>';
1559                                                 }
1560                                         }
1561                                         s += '</select>';
1562                                         break;
1563                                 case 'checkbox':
1564                                         s += '<input type="checkbox"' + common + attrib;
1565                                         if ((which == 'edit') && (values[vi])) s += ' checked';
1566                                         s += '>';
1567                                         break;
1568                                 default:
1569                                         s += f.custom.replace(/\$which\$/g, which);
1570                                 }
1571                                 if (f.suffix) s += f.suffix;
1573                                 ++vi;
1574                         }
1575                         var c = row.insertCell(i);
1576                         c.innerHTML = s;
1577                         if (this.editorFields[i].vtop) c.vAlign = 'top';
1578                 }
1580                 return row;
1581         },
1583         createControls: function(which, rowIndex) {
1584                 var r, c;
1586                 r = this.tb.insertRow(rowIndex);
1587                 r.className = 'controls';
1589                 c = r.insertCell(0);
1590                 c.colSpan = this.header.cells.length;
1591                 if (which == 'edit') {
1592                         c.innerHTML =
1593                                 '<input type=button value="Delete" onclick="TGO(this).onDelete()"> &nbsp; ' +
1594                                 '<input type=button value="OK" onclick="TGO(this).onOK()"> ' +
1595                                 '<input type=button value="Cancel" onclick="TGO(this).onCancel()">';
1596                 }
1597                 else {
1598                         c.innerHTML =
1599                                 '<input type=button value="Add" onclick="TGO(this).onAdd()">';
1600                 }
1601                 return r;
1602         },
1604         removeEditor: function() {
1605                 if (this.editor) {
1607                         elem.remove(this.editor);
1608                         this.editor = null;
1609                 }
1610                 if (this.controls) {
1611                         elem.remove(this.controls);
1612                         this.controls = null;
1613                 }
1614         },
1616         showSource: function() {
1617                 if (this.source) {
1618                         this.source.style.display = '';
1619                         this.source = null;
1620                 }
1621         },
1623         onChange: function(which, cell) {
1624                 return this.verifyFields((which == 'new') ? this.newEditor : this.editor, true);
1625         },
1627         onKey: function(which, ev) {
1628                 switch (ev.keyCode) {
1629                 case 27:
1630                         if (which == 'edit') this.onCancel();
1631                         return false;
1632                 case 13:
1633                         if (((ev.srcElement) && (ev.srcElement.tagName == 'SELECT')) ||
1634                                 ((ev.target) && (ev.target.tagName == 'SELECT'))) return true;
1635                         if (which == 'edit') this.onOK();
1636                                 else this.onAdd();
1637                         return false;
1638                 }
1639                 return true;
1640         },
1642         onDelete: function() {
1643                 this.removeEditor();
1644                 elem.remove(this.source);
1645                 this.source = null;
1646                 this.disableNewEditor(false);
1647         },
1649         onCancel: function() {
1650                 this.removeEditor();
1651                 this.showSource();
1652                 this.disableNewEditor(false);
1653         },
1655         onOK: function() {
1656                 var i, data, view;
1658                 if (!this.verifyFields(this.editor, false)) return;
1660                 data = this.fieldValuesToData(this.editor);
1661                 view = this.dataToView(data);
1663                 this.source.setRowData(data);
1664                 for (i = 0; i < this.source.cells.length; ++i) {
1665                         this.source.cells[i].innerHTML = view[i];
1666                 }
1668                 this.removeEditor();
1669                 this.showSource();
1670                 this.disableNewEditor(false);
1671         },
1673         onAdd: function() {
1674                 var data;
1676                 this.moving = null;
1677                 this.rpHide();
1679                 if (!this.verifyFields(this.newEditor, false)) return;
1681                 data = this.fieldValuesToData(this.newEditor);
1682                 this.insertData(-1, data);
1684                 this.disableNewEditor(false);
1685                 this.resetNewEditor();
1686         },
1688         verifyFields: function(row, quiet) {
1689                 return true;
1690         },
1692         showNewEditor: function() {
1693                 var r;
1695                 r = this.createEditor('new', -1, null);
1696                 this.footer = this.newEditor = r;
1698                 r = this.createControls('new', -1);
1699                 this.newControls = r;
1701                 this.disableNewEditor(false);
1702         },
1704         disableNewEditor: function(disable) {
1705                 if (this.getDataCount() >= this.maxAdd) disable = true;
1706                 if (this.newEditor) fields.disableAll(this.newEditor, disable);
1707                 if (this.newControls) fields.disableAll(this.newControls, disable);
1708         },
1710         resetNewEditor: function() {
1711                 var i, e;
1713                 e = fields.getAll(this.newEditor);
1714                 ferror.clearAll(e);
1715                 for (i = 0; i < e.length; ++i) {
1716                         var f = e[i];
1717                         if (f.selectedIndex) f.selectedIndex = 0;
1718                                 else f.value = '';
1719                 }
1720                 try { if (e.length) e[0].focus(); } catch (er) { }
1721         },
1723         getDataCount: function() {
1724                 var n;
1725                 n = this.tb.rows.length;
1726                 if (this.footer) n = this.footer.rowIndex;
1727                 if (this.header) n -= this.header.rowIndex + 1;
1728                 return n;
1729         },
1731         sortCompare: function(a, b) {
1732                 var obj = TGO(a);
1733                 var col = obj.sortColumn;
1734                 var r = cmpText(a.cells[col].innerHTML, b.cells[col].innerHTML);
1735                 return obj.sortAscending ? r : -r;
1736         },
1738         sort: function(column) {
1739                 if (this.editor) return;
1741                 if (this.sortColumn >= 0) {
1742                         elem.removeClass(this.header.cells[this.sortColumn], 'sortasc', 'sortdes');
1743                 }
1744                 if (column == this.sortColumn) {
1745                         this.sortAscending = !this.sortAscending;
1746                 }
1747                 else {
1748                         this.sortAscending = true;
1749                         this.sortColumn = column;
1750                 }
1751                 elem.addClass(this.header.cells[column], this.sortAscending ? 'sortasc' : 'sortdes');
1753                 this.resort();
1754         },
1756         resort: function() {
1757                 if ((this.sortColumn < 0) || (this.getDataCount() == 0) || (this.editor)) return;
1759                 var p = this.header.parentNode;
1760                 var a = [];
1761                 var i, j, max, e, p;
1762                 var top;
1764                 this.moving = null;
1766                 top = this.header ? this.header.rowIndex + 1 : 0;
1767                 max = this.footer ? this.footer.rowIndex : this.tb.rows.length;
1768                 for (i = top; i < max; ++i) a.push(p.rows[i]);
1769                 a.sort(THIS(this, this.sortCompare));
1770                 this.removeAllData();
1771                 j = top;
1772                 for (i = 0; i < a.length; ++i) {
1773                         e = p.insertBefore(a[i], this.footer);
1774                         e.className = (j & 1) ? 'even' : 'odd';
1775                         ++j;
1776                 }
1777         },
1779         recolor: function() {
1780                  var i, e, o;
1782                  i = this.header ? this.header.rowIndex + 1 : 0;
1783                  e = this.footer ? this.footer.rowIndex : this.tb.rows.length;
1784                  for (; i < e; ++i) {
1785                          o = this.tb.rows[i];
1786                          o.className = (o.rowIndex & 1) ? 'even' : 'odd';
1787                  }
1788         },
1790         removeAllData: function() {
1791                 var i, count;
1793                 i = this.header ? this.header.rowIndex + 1 : 0;
1794                 count = (this.footer ? this.footer.rowIndex : this.tb.rows.length) - i;
1795                 while (count-- > 0) elem.remove(this.tb.rows[i]);
1796         },
1798         getAllData: function() {
1799                 var i, max, data, r;
1801                 data = [];
1802                 max = this.footer ? this.footer.rowIndex : this.tb.rows.length;
1803                 for (i = this.header ? this.header.rowIndex + 1 : 0; i < max; ++i) {
1804                         r = this.tb.rows[i];
1805                         if ((r.style.display != 'none') && (r._data)) data.push(r._data);
1806                 }
1807                 return data;
1808         },
1810         isEditing: function() {
1811                 return (this.editor != null);
1812         }
1816 // -----------------------------------------------------------------------------
1819 function xmlHttpObj()
1821         var ob;
1822         try {
1823                 ob = new XMLHttpRequest();
1824                 if (ob) return ob;
1825         }
1826         catch (ex) { }
1827         try {
1828                 ob = new ActiveXObject('Microsoft.XMLHTTP');
1829                 if (ob) return ob;
1830         }
1831         catch (ex) { }
1832         return null;
1835 var _useAjax = -1;
1836 var _holdAjax = null;
1838 function useAjax()
1840         if (_useAjax == -1) _useAjax = ((_holdAjax = xmlHttpObj()) != null);
1841         return _useAjax;
1844 function XmlHttp()
1846         if ((!useAjax()) || ((this.xob = xmlHttpObj()) == null)) return null;
1847         return this;
1850 XmlHttp.prototype = {
1851         addId: function(vars) {
1852                 if (vars) vars += '&';
1853                         else vars = '';
1854                 vars += '_http_id=' + escapeCGI(nvram.http_id);
1855                 return vars;
1856         },
1858         get: function(url, vars) {
1859                 try {
1860                         vars = this.addId(vars);
1861                         url += '?' + vars;
1863                         this.xob.onreadystatechange = THIS(this, this.onReadyStateChange);
1864                         this.xob.open('GET', url, true);
1865                         this.xob.send(null);
1866                 }
1867                 catch (ex) {
1868                         this.onError(ex);
1869                 }
1870         },
1872         post: function(url, vars) {
1873                 try {
1874                         vars = this.addId(vars);
1876                         this.xob.onreadystatechange = THIS(this, this.onReadyStateChange);
1877                         this.xob.open('POST', url, true);
1878                         this.xob.send(vars);
1879                 }
1880                 catch (ex) {
1881                         this.onError(ex);
1882                 }
1883         },
1885         abort: function() {
1886                 try {
1887                         this.xob.onreadystatechange = function () { }
1888                         this.xob.abort();
1889                 }
1890                 catch (ex) {
1891                 }
1892         },
1894         onReadyStateChange: function() {
1895                 try {
1896                         if (typeof(E) == 'undefined') return;   // oddly late? testing for bug...
1898                         if (this.xob.readyState == 4) {
1899                                 if (this.xob.status == 200) {
1900                                         this.onCompleted(this.xob.responseText, this.xob.responseXML);
1901                                 }
1902                                 else {
1903                                         this.onError('' + (this.xob.status || 'unknown'));
1904                                 }
1905                         }
1906                 }
1907                 catch (ex) {
1908                         this.onError(ex);
1909                 }
1910         },
1912         onCompleted: function(text, xml) { },
1913         onError: function(ex) { }
1917 // -----------------------------------------------------------------------------
1920 function TomatoTimer(func, ms)
1922         this.tid = null;
1923         this.onTimer = func;
1924         if (ms) this.start(ms);
1925         return this;
1928 TomatoTimer.prototype = {
1929         start: function(ms) {
1930                 this.stop();
1931                 this.tid = setTimeout(THIS(this, this._onTimer), ms);
1932         },
1933         stop: function() {
1934                 if (this.tid) {
1935                         clearTimeout(this.tid);
1936                         this.tid = null;
1937                 }
1938         },
1940         isRunning: function() {
1941                 return (this.tid != null);
1942         },
1944         _onTimer: function() {
1945                 this.tid = null;
1946                 this.onTimer();
1947         },
1949         onTimer: function() {
1950         }
1954 // -----------------------------------------------------------------------------
1957 function TomatoRefresh(actionURL, postData, refreshTime, cookieTag)
1959         this.setup(actionURL, postData, refreshTime, cookieTag);
1960         this.timer = new TomatoTimer(THIS(this, this.start));
1963 TomatoRefresh.prototype = {
1964         running: 0,
1966         setup: function(actionURL, postData, refreshTime, cookieTag) {
1967                 var e, v;
1969                 this.actionURL = actionURL;
1970                 this.postData = postData;
1971                 this.refreshTime = refreshTime * 1000;
1972                 this.cookieTag = cookieTag;
1973         },
1975         start: function() {
1976                 var e;
1978                 if ((e = E('refresh-time')) != null) {
1979                         if (this.cookieTag) cookie.set(this.cookieTag, e.value);
1980                         this.refreshTime = e.value * 1000;
1981                 }
1982                 e = undefined;
1984                 this.updateUI('start');
1986                 this.running = 1;
1987                 if ((this.http = new XmlHttp()) == null) {
1988                         reloadPage();
1989                         return;
1990                 }
1992                 this.http.parent = this;
1994                 this.http.onCompleted = function(text, xml) {
1995                         var p = this.parent;
1997                         if (p.cookieTag) cookie.unset(p.cookieTag + '-error');
1998                         if (!p.running) {
1999                                 p.stop();
2000                                 return;
2001                         }
2003                         p.refresh(text);
2005                         if ((p.refreshTime > 0) && (!p.once)) {
2006                                 p.updateUI('wait');
2007                                 p.timer.start(Math.round(p.refreshTime));
2008                         }
2009                         else {
2010                                 p.stop();
2011                         }
2013                         p.errors = 0;
2014                 }
2016                 this.http.onError = function(ex) {
2017                         var p = this.parent;
2018                         if ((!p) || (!p.running)) return;
2020                         p.timer.stop();
2022                         if (++p.errors <= 3) {
2023                                 p.updateUI('wait');
2024                                 p.timer.start(3000);
2025                                 return;
2026                         }
2028                         if (p.cookieTag) {
2029                                 var e = cookie.get(p.cookieTag + '-error') * 1;
2030                                 if (isNaN(e)) e = 0;
2031                                         else ++e;
2032                                 cookie.unset(p.cookieTag);
2033                                 cookie.set(p.cookieTag + '-error', e, 1);
2034                                 if (e >= 3) {
2035                                         alert('XMLHTTP: ' + ex);
2036                                         return;
2037                                 }
2038                         }
2040                         setTimeout(reloadPage, 2000);
2041                 }
2043                 this.errors = 0;
2044                 this.http.post(this.actionURL, this.postData);
2045         },
2047         stop: function() {
2048                 if (this.cookieTag) cookie.set(this.cookieTag, -(this.refreshTime / 1000));
2049                 this.running = 0;
2050                 this.updateUI('stop');
2051                 this.timer.stop();
2052                 this.http = null;
2053                 this.once = undefined;
2054         },
2056         toggle: function(delay) {
2057                 if (this.running) this.stop();
2058                         else this.start(delay);
2059         },
2061         updateUI: function(mode) {
2062                 var e, b;
2064                 if (typeof(E) == 'undefined') return;   // for a bizzare bug...
2066                 b = (mode != 'stop') && (this.refreshTime > 0);
2067                 if ((e = E('refresh-button')) != null) {
2068                         e.value = b ? 'Stop' : 'Refresh';
2069                         e.disabled = ((mode == 'start') && (!b));
2070                 }
2071                 if ((e = E('refresh-time')) != null) e.disabled = b;
2072                 if ((e = E('refresh-spinner')) != null) e.style.visibility = b ? 'visible' : 'hidden';
2073         },
2075         initPage: function(delay, def) {
2076                 var e, v;
2078                 e = E('refresh-time');
2079                 if (((this.cookieTag) && (e != null)) &&
2080                         ((v = cookie.get(this.cookieTag)) != null) && (!isNaN(v *= 1))) {
2081                         e.value = Math.abs(v);
2082                         if (v > 0) v = (v * 1000) + (delay || 0);
2083                 }
2084                 else if (def) {
2085                         v = def;
2086                         if (e) e.value = def;
2087                 }
2088                 else v = 0;
2090                 if (delay < 0) {
2091                         v = -delay;
2092                         this.once = 1;
2093                 }
2095                 if (v > 0) {
2096                         this.running = 1;
2097                         this.refreshTime = v;
2098                         this.timer.start(v);
2099                         this.updateUI('wait');
2100                 }
2101         }
2104 function genStdTimeList(id, zero, min)
2106         var b = [];
2107         var t = [3,4,5,10,15,30,60,120,180,240,300,10*60,15*60,20*60,30*60];
2108         var i, v;
2110         if (min >= 0) {
2111                 b.push('<select id="' + id + '"><option value=0>' + zero);
2112                 for (i = 0; i < t.length; ++i) {
2113                         v = t[i];
2114                         if (v < min) continue;
2115                         b.push('<option value=' + v + '>');
2116                         if (v == 60) b.push('1 minute');
2117                                 else if (v > 60) b.push((v / 60) + ' minutes');
2118                                 else b.push(v + ' seconds');
2119                 }
2120                 b.push('</select> ');
2121         }
2122         document.write(b.join(''));
2125 function genStdRefresh(spin, min, exec)
2127         W('<div style="text-align:right">');
2128         if (spin) W('<img src="spin.gif" id="refresh-spinner"> ');
2129         genStdTimeList('refresh-time', 'Auto Refresh', min);
2130         W('<input type="button" value="Refresh" onclick="' + (exec ? exec : 'refreshClick()') + '" id="refresh-button"></div>');
2134 // -----------------------------------------------------------------------------
2137 function _tabCreate(tabs)
2139         var buf = [];
2140         buf.push('<ul id="tabs">');
2141         for (var i = 0; i < arguments.length; ++i)
2142                 buf.push('<li><a href="javascript:tabSelect(\'' + arguments[i][0] + '\')" id="' + arguments[i][0] + '">' + arguments[i][1] + '</a>');
2143         buf.push('</ul><div id="tabs-bottom"></div>');
2144         return buf.join('');
2147 function tabCreate(tabs)
2149         document.write(_tabCreate.apply(this, arguments));
2152 function tabHigh(id)
2154         var a = E('tabs').getElementsByTagName('A');
2155         for (var i = 0; i < a.length; ++i) {
2156                 if (id != a[i].id) elem.removeClass(a[i], 'active');
2157         }
2158         elem.addClass(id, 'active');
2161 // -----------------------------------------------------------------------------
2163 var cookie = {
2164         set: function(key, value, days) {
2165                 document.cookie = 'tomato_' + key + '=' + value + '; expires=' +
2166                         (new Date(new Date().getTime() + ((days ? days : 14) * 86400000))).toUTCString() + '; path=/';
2167         },
2169         get: function(key) {
2170                 var r = ('; ' + document.cookie + ';').match('; tomato_' + key + '=(.*?);');
2171                 return r ? r[1] : null;
2172         },
2174         unset: function(key) {
2175                 document.cookie = 'tomato_' + key + '=; expires=' +
2176                         (new Date(1)).toUTCString() + '; path=/';
2177         }
2180 // -----------------------------------------------------------------------------
2182 function checkEvent(evt)
2184         if (typeof(evt) == 'undefined') {
2185                 // ---- IE
2186                 evt = event;
2187                 evt.target = evt.srcElement;
2188                 evt.relatedTarget = evt.toElement;
2189         }
2190         return evt;
2193 function W(s)
2195         document.write(s);
2198 function E(e)
2200         return (typeof(e) == 'string') ? document.getElementById(e) : e;
2203 function PR(e)
2205         return elem.parentElem(e, 'TR');
2208 function THIS(obj, func)
2210         return function() { return func.apply(obj, arguments); }
2213 function UT(v)
2215         return (typeof(v) == 'undefined') ? '' : '' + v;
2218 function escapeHTML(s)
2220         function esc(c) {
2221                 return '&#' + c.charCodeAt(0) + ';';
2222         }
2223         return s.replace(/[&"'<>\r\n]/g, esc);
2226 function escapeCGI(s)
2228         return escape(s).replace(/\+/g, '%2B'); // escape() doesn't handle +
2231 function escapeD(s)
2233         function esc(c) {
2234                 return '%' + c.charCodeAt(0).hex(2);
2235         }
2236         return s.replace(/[<>|%]/g, esc);
2239 function ellipsis(s, max) {
2240         return (s.length <= max) ? s : s.substr(0, max - 3) + '...';
2243 function MIN(a, b)
2245         return a < b ? a : b;
2248 function MAX(a, b)
2250         return a > b ? a : b;
2253 function fixInt(n, min, max, def)
2255         if (n === null) return def;
2256         n *= 1;
2257         if (isNaN(n)) return def;
2258         if (n < min) return min;
2259         if (n > max) return max;
2260         return n;
2263 function comma(n)
2265         n = '' + n;
2266         var p = n;
2267         while ((n = n.replace(/(\d+)(\d{3})/g, '$1,$2')) != p) p = n;
2268         return n;
2271 function doScaleSize(n, sm)
2273         if (isNaN(n *= 1)) return '-';
2274         if (n <= 9999) return '' + n;
2275         var s = -1;
2276         do {
2277                 n /= 1024;
2278                 ++s;
2279         } while ((n > 9999) && (s < 2));
2280         return comma(n.toFixed(2)) + (sm ? '<small> ' : ' ') + (['KB', 'MB', 'GB'])[s] + (sm ? '</small>' : '');
2283 function scaleSize(n)
2285         return doScaleSize(n, 1);
2288 function timeString(mins)
2290         var h = Math.floor(mins / 60);
2291         if ((new Date(2000, 0, 1, 23, 0, 0, 0)).toLocaleString().indexOf('23') != -1)
2292                 return h + ':' + (mins % 60).pad(2);
2293         return ((h == 0) ? 12 : ((h > 12) ? h - 12 : h)) + ':' + (mins % 60).pad(2) + ((h >= 12) ? ' PM' : ' AM');
2296 function features(s)
2298         var features = ['ses','brau','aoss','wham','hpamp','!nve','11n','1000et'];
2299         var i;
2301         for (i = features.length - 1; i >= 0; --i) {
2302                 if (features[i] == s) return (parseInt(nvram.t_features) & (1 << i)) != 0;
2303         }
2304         return 0;
2307 function get_config(name, def)
2309         return ((typeof(nvram) != 'undefined') && (typeof(nvram[name]) != 'undefined')) ? nvram[name] : def;
2312 function nothing()
2316 // -----------------------------------------------------------------------------
2318 function show_notice1(s)
2320 // ---- !!TB - USB Support: multi-line notices
2321         if (s.length) document.write('<div id="notice1">' + s.replace(/\n/g, '<br>') + '</div><br style="clear:both">');
2324 // -----------------------------------------------------------------------------
2326 function myName()
2328         var name, i;
2330         name = document.location.pathname;
2331         name = name.replace(/\\/g, '/');        // IE local testing
2332         if ((i = name.lastIndexOf('/')) != -1) name = name.substring(i + 1, name.length);
2333         if (name == '') name = 'status-overview.asp';
2334         return name;
2337 function navi()
2339         var menu = [
2340                 ['Status',                      'status', 0, [
2341                         ['Overview',                    'overview.asp'],
2342                         ['Device List',                 'devices.asp'],
2343                         ['Web Usage',                   'webmon.asp'],
2344                         ['Logs',                        'log.asp'] ] ],
2345                 ['Bandwidth',                   'bwm', 0, [
2346                         ['Real-Time',                   'realtime.asp'],
2347                         ['Last 24 Hours',               '24.asp'],
2348                         ['Daily',                       'daily.asp'],
2349                         ['Weekly',                      'weekly.asp'],
2350                         ['Monthly',                     'monthly.asp']
2351                         ] ],
2352                 ['IP Traffic',                  'ipt', 0, [
2353                         ['Real-Time',                   'realtime.asp'],
2354                         ['Last 24 Hours',               '24.asp'],
2355                         ['View Graphs',                 'graphs.asp'],
2356                         ['Transfer Rates',              'details.asp'],
2357                         ['Daily',                       'daily.asp'],
2358                         ['Monthly',                     'monthly.asp']
2359                         ] ],
2360                 ['Tools',                       'tools', 0, [
2361                         ['Ping',                        'ping.asp'],
2362                         ['Trace',                       'trace.asp'],
2363                         ['System',                      'shell.asp'],
2364                         ['Wireless Survey',             'survey.asp'],
2365                         ['WOL',                         'wol.asp'] ] ],
2366                 null,
2367                 ['Basic',                       'basic', 0, [
2368                         ['Network',                     'network.asp'],
2369 /* IPV6-BEGIN */
2370                         ['IPv6',                        'ipv6.asp'],
2371 /* IPV6-END */
2372                         ['Identification',              'ident.asp'],
2373                         ['Time',                        'time.asp'],
2374                         ['DDNS',                        'ddns.asp'],
2375                         ['Static DHCP/ARP/IPT',         'static.asp'],
2376                         ['Wireless Filter',             'wfilter.asp'] ] ],
2377                 ['Advanced',                    'advanced', 0, [
2378                         ['Conntrack/Netfilter',         'ctnf.asp'],
2379                         ['DHCP/DNS',                    'dhcpdns.asp'],
2380                         ['Firewall',                    'firewall.asp'],
2381                         ['MAC Address',                 'mac.asp'],
2382                         ['Miscellaneous',               'misc.asp'],
2383                         ['Routing',                     'routing.asp'],
2384 /* TOR-BEGIN */
2385                         ['TOR Project',                 'tor.asp'],
2386 /* TOR-END */
2387                         ['VLAN',                        'vlan.asp'],
2388                         ['LAN Access',                  'access.asp'],
2389                         ['Virtual Wireless',            'wlanvifs.asp'],
2390                         ['Wireless',                    'wireless.asp'] ] ],
2391                 ['Port Forwarding',             'forward', 0, [
2392                         ['Basic',                       'basic.asp'],
2393 /* IPV6-BEGIN */
2394                         ['Basic IPv6',                  'basic-ipv6.asp'],
2395 /* IPV6-END */
2396                         ['DMZ',                         'dmz.asp'],
2397                         ['Triggered',                   'triggered.asp'],
2398                         ['UPnP/NAT-PMP',                'upnp.asp'] ] ],
2399                 ['Access Restriction',          'restrict.asp'],
2400                 ['QoS',                         'qos', 0, [
2401                         ['Basic Settings',              'settings.asp'],
2402                         ['Classification',              'classify.asp'],
2403                         ['View Graphs',                 'graphs.asp'],
2404                         ['View Details',                'detailed.asp'],
2405                         ['Transfer Rates',              'ctrate.asp']
2406                         ] ],
2407                 ['Bandwidth Limiter',           'bwlimit.asp'],
2408                 null,
2409 /* NOCAT-BEGIN */
2410                 ['Captive Portal',              'splashd.asp'],
2411 /* NOCAT-END */
2412 /* REMOVE-BEGIN
2413                 ['Scripts',                             'sc', 0, [
2414                         ['Startup',             'startup.asp'],
2415                         ['Shutdown',            'shutdown.asp'],
2416                         ['Firewall',            'firewall.asp'],
2417                         ['WAN Up',              'wanup.asp']
2418                         ] ],
2419 REMOVE-END */
2420 /* USB-BEGIN */
2421 // ---- !!TB - USB, FTP, Samba, Media Server
2422                 ['USB and NAS',                 'nas', 0, [
2423                         ['USB Support',                 'usb.asp']
2424 /* FTP-BEGIN */
2425                         ,['FTP Server',                 'ftp.asp']
2426 /* FTP-END */
2427 /* SAMBA-BEGIN */
2428                         ,['File Sharing',               'samba.asp']
2429 /* SAMBA-END */
2430 /* MEDIA-SRV-BEGIN */
2431                         ,['Media Server',               'media.asp']
2432 /* MEDIA-SRV-END */
2433 /* UPS-BEGIN */
2434                         ,['UPS Monitor',                'ups.asp']
2435 /* UPS-END */
2436 /* BT-BEGIN */
2437                         ,['BitTorrent Client',          'bittorrent.asp']
2438 /* BT-END */
2439                         ] ],
2440 /* USB-END */
2441 /* VPN-BEGIN */
2442                 ['VPN Tunneling',                       'vpn', 0, [
2443 /* OPENVPN-BEGIN */
2444                         ['OpenVPN Server',              'server.asp'],
2445                         ['OpenVPN Client',              'client.asp'],
2446 /* OPENVPN-END */
2447 /* PPTPD-BEGIN */
2448                         ['PPTP Server',                 'pptp-server.asp'],
2449                         ['PPTP Online',                 'pptp-online.asp'],
2450                         ['PPTP Client',                 'pptp.asp']
2451 /* PPTPD-END */
2452                 ] ],
2453 /* VPN-END */
2454                 null,
2455                 ['Administration',              'admin', 0, [
2456                         ['Admin Access',                'access.asp'],
2457                         ['TomatoAnon',                  'tomatoanon.asp'],
2458                         ['Bandwidth Monitoring',        'bwm.asp'],
2459                         ['IP Traffic Monitoring',       'iptraffic.asp'],
2460                         ['Buttons/LED',                 'buttons.asp'],
2461 /* CIFS-BEGIN */
2462                         ['CIFS Client',                 'cifs.asp'],
2463 /* CIFS-END */
2464 /* SDHC-BEGIN */
2465                         ['SDHC/MMC',                    'sdhc.asp'],
2466 /* SDHC-END */
2467                         ['Configuration',               'config.asp'],
2468                         ['Debugging',                   'debug.asp'],
2469 /* JFFS2-BEGIN */
2470                         ['JFFS',                        'jffs2.asp'],
2471 /* JFFS2-END */
2472 /* NFS-BEGIN */
2473                         ['NFS Server',                  'nfs.asp'],
2474 /* NFS-END */
2475 /* SNMP-BEGIN */
2476                         ['SNMP',                        'snmp.asp'],
2477 /* SNMP-END */
2478                         ['Logging',                     'log.asp'],
2479                         ['Scheduler',                   'sched.asp'],
2480                         ['Scripts',                     'scripts.asp'],
2481                         ['Upgrade',                     'upgrade.asp'] ] ],
2482                 null,
2483                 ['About',                       'about.asp'],
2484                 ['Reboot...',                   'javascript:reboot()'],
2485                 ['Shutdown...',                 'javascript:shutdown()'],
2486                 ['Logout',                      'javascript:logout()']
2487         ];
2488         var name, base;
2489         var i, j;
2490         var buf = [];
2491         var sm;
2492         var a, b, c;
2493         var on1;
2494         var cexp = get_config('web_mx', '').toLowerCase();
2496         name = myName();
2497         if (name == 'restrict-edit.asp') name = 'restrict.asp';
2498         if ((i = name.indexOf('-')) != -1) {
2499                 base = name.substring(0, i);
2500                 name = name.substring(i + 1, name.length);
2501         }
2502         else base = '';
2504         for (i = 0; i < menu.length; ++i) {
2505                 var m = menu[i];
2506                 if (!m) {
2507                         buf.push("<br>");
2508                         continue;
2509                 }
2510                 if (m.length == 2) {
2511                         buf.push('<a href="' + m[1] + '" class="indent1' + (((base == '') && (name == m[1])) ? ' active' : '') + '">' + m[0] + '</a>');
2512                 }
2513                 else {
2514                         if (base == m[1]) {
2515                                 b = name;
2516                         }
2517                         else {
2518                                 a = cookie.get('menu_' + m[1]);
2519                                 b = m[3][0][1];
2520                                 for (j = 0; j < m[3].length; ++j) {
2521                                         if (m[3][j][1] == a) {
2522                                                 b = a;
2523                                                 break;
2524                                         }
2525                                 }
2526                         }
2527                         a = m[1] + '-' + b;
2528                         if (a == 'status-overview.asp') a = '/';
2529                         on1 = (base == m[1]);
2530                         buf.push('<a href="' + a + '" class="indent1' + (on1 ? ' active' : '') + '">' + m[0] + '</a>');
2531                         if ((!on1) && (m[2] == 0) && (cexp.indexOf(m[1]) == -1)) continue;
2533                         for (j = 0; j < m[3].length; ++j) {
2534                                 sm = m[3][j];
2535                                 a = m[1] + '-' + sm[1];
2536                                 if (a == 'status-overview.asp') a = '/';
2537                                 buf.push('<a href="' + a + '" class="indent2' + (((on1) && (name == sm[1])) ? ' active' : '') + '">' + sm[0] + '</a>');
2538                         }
2539                 }
2540         }
2541         document.write(buf.join(''));
2543         if (base.length) {
2544                 if ((base == 'qos') && (name == 'detailed.asp')) name = 'view.asp';
2545                 cookie.set('menu_' + base, name);
2546         }
2549 function createFieldTable(flags, desc)
2551         var common;
2552         var i, n;
2553         var name;
2554         var id;
2555         var fields;
2556         var f;
2557         var a;
2558         var buf = [];
2559         var buf2;
2560         var id1;
2561         var tr;
2563         if ((flags.indexOf('noopen') == -1)) buf.push('<table class="fields">');
2564         for (desci = 0; desci < desc.length; ++desci) {
2565                 var v = desc[desci];
2567                 if (!v) {
2568                         buf.push('<tr><td colspan=2 class="spacer">&nbsp;</td></tr>');
2569                         continue;
2570                 }
2572                 if (v.ignore) continue;
2574                 buf.push('<tr');
2575                 if (v.rid) buf.push(' id="' + v.rid + '"');
2576                 if (v.hidden) buf.push(' style="display:none"');
2577                 buf.push('>');
2579                 if (v.text) {
2580                         if (v.title) {
2581                                 buf.push('<td class="title indent' + (v.indent || 1) + '">' + v.title + '</td><td class="content">' + v.text + '</td></tr>');
2582                         }
2583                         else {
2584                                 buf.push('<td colspan=2>' + v.text + '</td></tr>');
2585                         }
2586                         continue;
2587                 }
2589                 id1 = '';
2590                 buf2 = [];
2591                 buf2.push('<td class="content">');
2593                 if (v.multi) fields = v.multi;
2594                         else fields = [v];
2596                 for (n = 0; n < fields.length; ++n) {
2597                         f = fields[n];
2598                         if (f.prefix) buf2.push(f.prefix);
2600                         if ((f.type == 'radio') && (!f.id)) id = '_' + f.name + '_' + i;
2601                                 else id = (f.id ? f.id : ('_' + f.name));
2603                         if (id1 == '') id1 = id;
2605                         common = ' onchange="verifyFields(this, 1)" id="' + id + '"';
2606                         if (f.attrib) common += ' ' + f.attrib;
2607                         name = f.name ? (' name="' + f.name + '"') : '';
2609                         switch (f.type) {
2610                         case 'checkbox':
2611                                 buf2.push('<input type="checkbox"' + name + (f.value ? ' checked' : '') + ' onclick="verifyFields(this, 1)"' + common + '>');
2612                                 break;
2613                         case 'radio':
2614                                 buf2.push('<input type="radio"' + name + (f.value ? ' checked' : '') + ' onclick="verifyFields(this, 1)"' + common + '>');
2615                                 break;
2616                         case 'password':
2617                                 if (f.peekaboo) {
2618                                         switch (get_config('web_pb', '1')) {
2619                                         case '0':
2620                                                 f.type = 'text';
2621                                         case '2':
2622                                                 f.peekaboo = 0;
2623                                                 break;
2624                                         }
2625                                 }
2626                                 if (f.type == 'password') {
2627                                         common += ' autocomplete="off"';
2628                                         if (f.peekaboo) common += ' onfocus=\'peekaboo("' + id + '",1)\'';
2629                                 }
2630                                 // drop
2631                         case 'text':
2632                                 buf2.push('<input type="' + f.type + '"' + name + ' value="' + escapeHTML(UT(f.value)) + '" maxlength=' + f.maxlen + (f.size ? (' size=' + f.size) : '') + common + '>');
2633                                 break;
2634                         case 'select':
2635                                 buf2.push('<select' + name + common + '>');
2636                                 for (i = 0; i < f.options.length; ++i) {
2637                                         a = f.options[i];
2638                                         if (a.length == 1) a.push(a[0]);
2639                                         buf2.push('<option value="' + a[0] + '"' + ((a[0] == f.value) ? ' selected' : '') + '>' + a[1] + '</option>');
2640                                 }
2641                                 buf2.push('</select>');
2642                                 break;
2643                         case 'textarea':
2644                                 buf2.push('<textarea' + name + common + (f.wrap ? (' wrap=' + f.wrap) : '') + '>' + escapeHTML(UT(f.value)) + '</textarea>');
2645                                 break;
2646                         default:
2647                                 if (f.custom) buf2.push(f.custom);
2648                                 break;
2649                         }
2650                         if (f.suffix) buf2.push(f.suffix);
2651                 }
2652                 buf2.push('</td>');
2654                 buf.push('<td class="title indent' + (v.indent ? v.indent : 1) + '">');
2655                 if (id1 != '') buf.push('<label for="' + id + '">' + v.title + '</label></td>');
2656                         else buf.push(+ v.title + '</td>');
2658                 buf.push(buf2.join(''));
2659                 buf.push('</tr>');
2660         }
2661         if ((!flags) || (flags.indexOf('noclose') == -1)) buf.push('</table>');
2662         document.write(buf.join(''));
2665 function peekaboo(id, show)
2667         try {
2668                 var o = document.createElement('INPUT');
2669                 var e = E(id);
2670                 var name = e.name;
2671                 o.type = show ? 'text' : 'password';
2672                 o.value = e.value;
2673                 o.size = e.size;
2674                 o.maxLength = e.maxLength;
2675                 o.autocomplete = e.autocomplete;
2676                 o.title = e.title;
2677                 o.disabled = e.disabled;
2678                 o.onchange = e.onchange;
2679                 e.parentNode.replaceChild(o, e);
2680                 e = null;
2681                 o.id = id;
2682                 o.name = name;
2684                 if (show) {
2685                         o.onblur = function(ev) { setTimeout('peekaboo("' + this.id + '", 0)', 0) };
2686                         setTimeout('try { E("' + id + '").focus() } catch (ex) { }', 0)
2687                 }
2688                 else {
2689                         o.onfocus = function(ev) { peekaboo(this.id, 1); };
2690                 }
2691         }
2692         catch (ex) {
2693 //              alert(ex);
2694         }
2696 /* REMOVE-BEGIN
2697 notes:
2698  - e.type= doesn't work in IE, ok in FF
2699  - may mess keyboard tabing (bad: IE; ok: FF, Opera)... setTimeout() delay seems to help a little.
2700 REMOVE-END */
2703 // -----------------------------------------------------------------------------
2705 function reloadPage()
2707         document.location.reload(1);
2710 function reboot()
2712         if (confirm("Reboot?")) form.submitHidden('tomato.cgi', { _reboot: 1, _commit: 0, _nvset: 0 });
2715 function shutdown()
2717         if (confirm("Shutdown?")) form.submitHidden('shutdown.cgi', { });
2720 function logout()
2722         form.submitHidden('logout.asp', { });
2725 // -----------------------------------------------------------------------------
2729 // ---- debug
2731 function isLocal()
2733         return location.href.search('file://') == 0;
2736 function console(s)