2 LuCI - Lua Configuration Interface
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008-2012 Jo-Philipp Wich <jow@openwrt.org>
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
11 http://www.apache.org/licenses/LICENSE-2.0
16 var cbi_strings = { path: {}, label: {} };
19 return (/^-?\d+$/.test(x) ? +x : NaN);
23 return (/^-?\d+(?:\.\d+)?$/.test(x) ? +x : NaN);
27 if (!x.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/))
30 if (RegExp.$1 > 255 || RegExp.$2 > 255 || RegExp.$3 > 255 || RegExp.$4 > 255)
33 return [ +RegExp.$1, +RegExp.$2, +RegExp.$3, +RegExp.$4 ];
37 if (x.match(/^([a-fA-F0-9:]+):(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/)) {
38 var v6 = RegExp.$1, v4 = IPv4(RegExp.$2);
43 x = v6 + ':' + (v4[0] * 256 + v4[1]).toString(16)
44 + ':' + (v4[2] * 256 + v4[3]).toString(16);
47 if (!x.match(/^[a-fA-F0-9:]+$/))
50 var prefix_suffix = x.split(/::/);
52 if (prefix_suffix.length > 2)
55 var prefix = (prefix_suffix[0] || '0').split(/:/);
56 var suffix = prefix_suffix.length > 1 ? (prefix_suffix[1] || '0').split(/:/) : [];
58 if (suffix.length ? (prefix.length + suffix.length > 7) : (prefix.length > 8))
64 for (i = 0, word = parseInt(prefix[0], 16); i < prefix.length; word = parseInt(prefix[++i], 16))
65 if (prefix[i].length <= 4 && !isNaN(word) && word <= 0xFFFF)
70 for (i = 0; i < (8 - prefix.length - suffix.length); i++)
73 for (i = 0, word = parseInt(suffix[0], 16); i < suffix.length; word = parseInt(suffix[++i], 16))
74 if (suffix[i].length <= 4 && !isNaN(word) && word <= 0xFFFF)
82 var cbi_validators = {
89 'uinteger': function()
91 return (Int(this) >= 0);
101 return (Dec(this) >= 0);
106 return cbi_validators.ip4addr.apply(this) ||
107 cbi_validators.ip6addr.apply(this);
110 'ip4addr': function()
112 var m = this.match(/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(?:\/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})|\/(\d{1,2}))?$/);
113 return !!(m && IPv4(m[1]) && (m[2] ? IPv4(m[2]) : (m[3] ? cbi_validators.ip4prefix.apply(m[3]) : true)));
116 'ip6addr': function()
118 var m = this.match(/^([0-9a-fA-F:.]+)(?:\/(\d{1,3}))?$/);
119 return !!(m && IPv6(m[1]) && (m[2] ? cbi_validators.ip6prefix.apply(m[2]) : true));
122 'ip4prefix': function()
124 return !isNaN(this) && this >= 0 && this <= 32;
127 'ip6prefix': function()
129 return !isNaN(this) && this >= 0 && this <= 128;
134 return cbi_validators.cidr4.apply(this) ||
135 cbi_validators.cidr6.apply(this);
140 var m = this.match(/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\/(\d{1,2})$/);
141 return !!(m && IPv4(m[1]) && cbi_validators.ip4prefix.apply(m[2]));
146 var m = this.match(/^([0-9a-fA-F:.]+)\/(\d{1,3})$/);
147 return !!(m && IPv6(m[1]) && cbi_validators.ip6prefix.apply(m[2]));
152 var m = this.match(/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/);
153 return !!(m && IPv4(m[1]) && IPv4(m[2]));
158 var m = this.match(/^([0-9a-fA-F:.]+)\/([0-9a-fA-F:.]+)$/);
159 return !!(m && IPv6(m[1]) && IPv6(m[2]));
162 'ip6hostid': function()
164 if (this == "eui64" || this == "random")
168 return !(!v6 || v6[0] || v6[1] || v6[2] || v6[3]);
173 return cbi_validators.ipmask4.apply(this) ||
174 cbi_validators.ipmask6.apply(this);
177 'ipmask4': function()
179 return cbi_validators.cidr4.apply(this) ||
180 cbi_validators.ipnet4.apply(this) ||
181 cbi_validators.ip4addr.apply(this);
184 'ipmask6': function()
186 return cbi_validators.cidr6.apply(this) ||
187 cbi_validators.ipnet6.apply(this) ||
188 cbi_validators.ip6addr.apply(this);
194 return (p >= 0 && p <= 65535);
197 'portrange': function()
199 if (this.match(/^(\d+)-(\d+)$/))
203 return (p1 <= p2 && p2 <= 65535);
206 return cbi_validators.port.apply(this);
209 'macaddr': function()
211 return (this.match(/^([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}$/) != null);
214 'host': function(ipv4only)
216 return cbi_validators.hostname.apply(this) ||
217 ((ipv4only != 1) && cbi_validators.ipaddr.apply(this)) ||
218 ((ipv4only == 1) && cbi_validators.ip4addr.apply(this));
221 'hostname': function()
223 if (this.length <= 253)
224 return (this.match(/^[a-zA-Z0-9]+$/) != null ||
225 (this.match(/^[a-zA-Z0-9_][a-zA-Z0-9_\-.]*[a-zA-Z0-9]$/) &&
226 this.match(/[^0-9.]/)));
231 'network': function()
233 return cbi_validators.uciname.apply(this) ||
234 cbi_validators.host.apply(this);
237 'hostport': function(ipv4only)
239 var hp = this.split(/:/);
242 return (cbi_validators.host.apply(hp[0], ipv4only) &&
243 cbi_validators.port.apply(hp[1]));
248 'ip4addrport': function()
250 var hp = this.split(/:/);
253 return (cbi_validators.ipaddr.apply(hp[0]) &&
254 cbi_validators.port.apply(hp[1]));
258 'ipaddrport': function(bracket)
260 if (this.match(/^([^\[\]:]+):([^:]+)$/)) {
263 return (cbi_validators.ip4addr.apply(addr) &&
264 cbi_validators.port.apply(port));
265 } else if ((bracket == 1) && (this.match(/^\[(.+)\]:([^:]+)$/))) {
268 return (cbi_validators.ip6addr.apply(addr) &&
269 cbi_validators.port.apply(port));
270 } else if ((bracket != 1) && (this.match(/^([^\[\]]+):([^:]+)$/))) {
273 return (cbi_validators.ip6addr.apply(addr) &&
274 cbi_validators.port.apply(port));
285 return (v.match(/^[a-fA-F0-9]{64}$/) != null);
287 return (v.length >= 8) && (v.length <= 63);
294 if ( v.substr(0,2) == 's:' )
297 if( (v.length == 10) || (v.length == 26) )
298 return (v.match(/^[a-fA-F0-9]{10,26}$/) != null);
300 return (v.length == 5) || (v.length == 13);
303 'uciname': function()
305 return (this.match(/^[a-zA-Z0-9_]+$/) != null);
308 'range': function(min, max)
311 return (val >= +min && val <= +max);
316 return (Dec(this) >= +min);
321 return (Dec(this) <= +max);
324 'rangelength': function(min, max)
327 return ((val.length >= +min) && (val.length <= +max));
330 'minlength': function(min)
332 return ((''+this).length >= +min);
335 'maxlength': function(max)
337 return ((''+this).length <= +max);
342 for (var i = 0; i < arguments.length; i += 2)
344 if (typeof arguments[i] != 'function')
346 if (arguments[i] == this)
350 else if (arguments[i].apply(this, arguments[i+1]))
360 for (var i = 0; i < arguments.length; i += 2)
362 if (typeof arguments[i] != 'function')
364 if (arguments[i] != this)
368 else if (!arguments[i].apply(this, arguments[i+1]))
378 return cbi_validators.or.apply(
379 this.replace(/^[ \t]*![ \t]*/, ''), arguments);
382 'list': function(subvalidator, subargs)
384 if (typeof subvalidator != 'function')
387 var tokens = this.match(/[^ \t]+/g);
388 for (var i = 0; i < tokens.length; i++)
389 if (!subvalidator.apply(tokens[i], subargs))
394 'phonedigit': function()
396 return (this.match(/^[0-9\*#!\.]+$/) != null);
398 'timehhmmss': function()
400 return (this.match(/^[0-6][0-9]:[0-6][0-9]:[0-6][0-9]$/) != null);
402 'dateyyyymmdd': function()
407 if (this.match(/^(\d\d\d\d)-(\d\d)-(\d\d)/)) {
408 var year = RegExp.$1;
409 var month = RegExp.$2;
412 var days_in_month = [ 31, 28, 31, 30, 31, 30, 31, 31, 30 , 31, 30, 31 ];
413 function is_leap_year(year) {
414 return ((year % 4) == 0) && ((year % 100) != 0) || ((year % 400) == 0);
416 function get_days_in_month(month, year) {
417 if ((month == 2) && is_leap_year(year)) {
420 return days_in_month[month];
423 /* Firewall rules in the past don't make sense */
427 if ((month <= 0) || (month > 12)) {
430 if ((day <= 0) || (day > get_days_in_month(month, year))) {
442 function cbi_d_add(field, dep, index) {
443 var obj = (typeof(field) === 'string') ? document.getElementById(field) : field;
446 for (var i=0; i<cbi_d.length; i++) {
447 if (cbi_d[i].id == obj.id) {
456 "parent": obj.parentNode.id,
460 cbi_d.unshift(entry);
466 function cbi_d_checkvalue(target, ref) {
467 var t = document.getElementById(target);
471 var tl = document.getElementsByName(target);
473 if( tl.length > 0 && (tl[0].type == 'radio' || tl[0].type == 'checkbox'))
474 for( var i = 0; i < tl.length; i++ )
475 if( tl[i].checked ) {
480 value = value ? value : "";
481 } else if (!t.value) {
486 if (t.type == "checkbox") {
487 value = t.checked ? value : "";
491 return (value == ref)
494 function cbi_d_check(deps) {
497 for (var i=0; i<deps.length; i++) {
500 for (var j in deps[i]) {
501 if (j == "!reverse") {
503 } else if (j == "!default") {
507 istat = (istat && cbi_d_checkvalue(j, deps[i][j]))
511 if (istat ^ reverse) {
518 function cbi_d_update() {
520 for (var i=0; i<cbi_d.length; i++) {
521 var entry = cbi_d[i];
522 var node = document.getElementById(entry.id);
523 var parent = document.getElementById(entry.parent);
525 if (node && node.parentNode && !cbi_d_check(entry.deps)) {
526 node.parentNode.removeChild(node);
528 } else if (parent && (!node || !node.parentNode) && cbi_d_check(entry.deps)) {
529 var next = undefined;
531 for (next = parent.firstChild; next; next = next.nextSibling) {
532 if (next.getAttribute && parseInt(next.getAttribute('data-index'), 10) > entry.index) {
538 parent.appendChild(entry.node);
540 parent.insertBefore(entry.node, next);
546 // hide optionals widget if no choices remaining
547 if (parent && parent.parentNode && parent.getAttribute('data-optionals'))
548 parent.parentNode.style.display = (parent.options.length <= 1) ? 'none' : '';
551 if (entry && entry.parent) {
553 cbi_tag_last(parent);
561 function cbi_init() {
564 nodes = document.querySelectorAll('[data-strings]');
566 for (var i = 0, node; (node = nodes[i]) !== undefined; i++) {
567 var str = JSON.parse(node.getAttribute('data-strings'));
568 for (var key in str) {
569 for (var key2 in str[key]) {
570 var dst = cbi_strings[key] || (cbi_strings[key] = { });
571 dst[key2] = str[key][key2];
576 nodes = document.querySelectorAll('[data-depends]');
578 for (var i = 0, node; (node = nodes[i]) !== undefined; i++) {
579 var index = parseInt(node.getAttribute('data-index'), 10);
580 var depends = JSON.parse(node.getAttribute('data-depends'));
581 if (!isNaN(index) && depends.length > 0) {
582 for (var alt = 0; alt < depends.length; alt++) {
583 cbi_d_add(node, depends[alt], index);
588 nodes = document.querySelectorAll('[data-update]');
590 for (var i = 0, node; (node = nodes[i]) !== undefined; i++) {
591 var events = node.getAttribute('data-update').split(' ');
592 for (var j = 0, event; (event = events[j]) !== undefined; j++) {
593 cbi_bind(node, event, cbi_d_update);
597 nodes = document.querySelectorAll('[data-choices]');
599 for (var i = 0, node; (node = nodes[i]) !== undefined; i++) {
600 var choices = JSON.parse(node.getAttribute('data-choices'));
603 for (var j = 0; j < choices[0].length; j++)
604 options[choices[0][j]] = choices[1][j];
606 var def = (node.getAttribute('data-optional') === 'true')
607 ? node.placeholder || '' : null;
609 cbi_combobox_init(node, options, def,
610 node.getAttribute('data-manual'));
613 nodes = document.querySelectorAll('[data-dynlist]');
615 for (var i = 0, node; (node = nodes[i]) !== undefined; i++) {
616 var choices = JSON.parse(node.getAttribute('data-dynlist'));
619 if (choices[0] && choices[0].length) {
622 for (var j = 0; j < choices[0].length; j++)
623 options[choices[0][j]] = choices[1][j];
626 cbi_dynlist_init(node, choices[2], choices[3], options);
629 nodes = document.querySelectorAll('[data-type]');
631 for (var i = 0, node; (node = nodes[i]) !== undefined; i++) {
632 cbi_validate_field(node, node.getAttribute('data-optional') === 'true',
633 node.getAttribute('data-type'));
639 function cbi_bind(obj, type, callback, mode) {
640 if (!obj.addEventListener) {
641 obj.attachEvent('on' + type,
643 var e = window.event;
645 if (!e.target && e.srcElement)
646 e.target = e.srcElement;
648 return !!callback(e);
652 obj.addEventListener(type, callback, !!mode);
657 function cbi_combobox(id, values, def, man, focus) {
658 var selid = "cbi.combobox." + id;
659 if (document.getElementById(selid)) {
663 var obj = document.getElementById(id)
664 var sel = document.createElement("select");
666 sel.index = obj.index;
667 sel.className = obj.className.replace(/cbi-input-text/, 'cbi-input-select');
669 if (obj.nextSibling) {
670 obj.parentNode.insertBefore(sel, obj.nextSibling);
672 obj.parentNode.appendChild(sel);
675 var dt = obj.getAttribute('cbi_datatype');
676 var op = obj.getAttribute('cbi_optional');
678 if (!values[obj.value]) {
679 if (obj.value == "") {
680 var optdef = document.createElement("option");
682 optdef.appendChild(document.createTextNode(typeof(def) === 'string' ? def : cbi_strings.label.choose));
683 sel.appendChild(optdef);
685 var opt = document.createElement("option");
686 opt.value = obj.value;
687 opt.selected = "selected";
688 opt.appendChild(document.createTextNode(obj.value));
689 sel.appendChild(opt);
693 for (var i in values) {
694 var opt = document.createElement("option");
697 if (obj.value == i) {
698 opt.selected = "selected";
701 opt.appendChild(document.createTextNode(values[i]));
702 sel.appendChild(opt);
705 var optman = document.createElement("option");
707 optman.appendChild(document.createTextNode(typeof(man) === 'string' ? man : cbi_strings.label.custom));
708 sel.appendChild(optman);
710 obj.style.display = "none";
713 cbi_validate_field(sel, op == 'true', dt);
715 cbi_bind(sel, "change", function() {
716 if (sel.selectedIndex == sel.options.length - 1) {
717 obj.style.display = "inline";
719 sel.parentNode.removeChild(sel);
722 obj.value = sel.options[sel.selectedIndex].value;
732 // Retrigger validation in select
739 function cbi_combobox_init(id, values, def, man) {
740 var obj = (typeof(id) === 'string') ? document.getElementById(id) : id;
741 cbi_bind(obj, "blur", function() {
742 cbi_combobox(obj.id, values, def, man, true);
744 cbi_combobox(obj.id, values, def, man, false);
747 function cbi_filebrowser(id, defpath) {
748 var field = document.getElementById(id);
749 var browser = window.open(
750 cbi_strings.path.browser + ( field.value || defpath || '' ) + '?field=' + id,
751 "luci_filebrowser", "width=300,height=400,left=100,top=200,scrollbars=yes"
757 function cbi_browser_init(id, resource, defpath)
759 function cbi_browser_btnclick(e) {
760 cbi_filebrowser(id, defpath);
764 var field = document.getElementById(id);
766 var btn = document.createElement('img');
767 btn.className = 'cbi-image-button';
768 btn.src = (resource || cbi_strings.path.resource) + '/cbi/folder.gif';
769 field.parentNode.insertBefore(btn, field.nextSibling);
771 cbi_bind(btn, 'click', cbi_browser_btnclick);
774 function cbi_dynlist_init(parent, datatype, optional, choices)
776 var prefix = parent.getAttribute('data-prefix');
777 var holder = parent.getAttribute('data-placeholder');
781 function cbi_dynlist_redraw(focus, add, del)
785 while (parent.firstChild)
787 var n = parent.firstChild;
792 if (n.nodeName.toLowerCase() == 'input')
793 values.push(n.value || '');
794 else if (n.nodeName.toLowerCase() == 'select')
795 values[values.length-1] = n.options[n.selectedIndex].value;
798 parent.removeChild(n);
804 values.splice(focus, 0, '');
806 else if (values.length == 0)
812 for (var i = 0; i < values.length; i++)
814 var t = document.createElement('input');
815 t.id = prefix + '.' + (i+1);
820 t.className = 'cbi-input-text';
822 if (i == 0 && holder)
824 t.placeholder = holder;
827 var b = document.createElement('img');
828 b.src = cbi_strings.path.resource + ((i+1) < values.length ? '/cbi/remove.gif' : '/cbi/add.gif');
829 b.className = 'cbi-image-button';
831 parent.appendChild(t);
832 parent.appendChild(b);
833 if (datatype == 'file')
835 cbi_browser_init(t.id, null, parent.getAttribute('data-browser-path'));
838 parent.appendChild(document.createElement('br'));
842 cbi_validate_field(t.id, ((i+1) == values.length) || optional, datatype);
847 cbi_combobox_init(t.id, choices, '', cbi_strings.label.custom);
850 cbi_bind(b, 'keydown', cbi_dynlist_keydown);
851 cbi_bind(b, 'keypress', cbi_dynlist_keypress);
853 if (i == focus || -i == focus)
858 cbi_bind(t, 'keydown', cbi_dynlist_keydown);
859 cbi_bind(t, 'keypress', cbi_dynlist_keypress);
865 else if (-i == focus)
869 /* force cursor to end */
876 cbi_bind(b, 'click', cbi_dynlist_btnclick);
880 function cbi_dynlist_keypress(ev)
882 ev = ev ? ev : window.event;
884 var se = ev.target ? ev.target : ev.srcElement;
886 if (se.nodeType == 3)
891 /* backspace, delete */
894 if (se.value.length == 0)
896 if (ev.preventDefault)
904 /* enter, arrow up, arrow down */
908 if (ev.preventDefault)
917 function cbi_dynlist_keydown(ev)
919 ev = ev ? ev : window.event;
921 var se = ev.target ? ev.target : ev.srcElement;
923 if (se.nodeType == 3)
926 var prev = se.previousSibling;
927 while (prev && prev.name != prefix)
928 prev = prev.previousSibling;
930 var next = se.nextSibling;
931 while (next && next.name != prefix)
932 next = next.nextSibling;
934 /* advance one further in combobox case */
935 if (next && next.nextSibling.name == prefix)
936 next = next.nextSibling;
940 /* backspace, delete */
943 var del = (se.nodeName.toLowerCase() == 'select')
944 ? true : (se.value.length == 0);
948 if (ev.preventDefault)
951 var focus = se.index;
955 cbi_dynlist_redraw(focus, -1, se.index);
964 cbi_dynlist_redraw(-1, se.index, -1);
985 function cbi_dynlist_btnclick(ev)
987 ev = ev ? ev : window.event;
989 var se = ev.target ? ev.target : ev.srcElement;
990 var input = se.previousSibling;
991 while (input && input.name != prefix) {
992 input = input.previousSibling;
995 if (se.src.indexOf('remove') > -1)
999 cbi_dynlist_keydown({
1006 cbi_dynlist_keydown({
1015 cbi_dynlist_redraw(NaN, -1, -1);
1019 function cbi_t_add(section, tab) {
1020 var t = document.getElementById('tab.' + section + '.' + tab);
1021 var c = document.getElementById('container.' + section + '.' + tab);
1024 cbi_t[section] = (cbi_t[section] || [ ]);
1025 cbi_t[section][tab] = { 'tab': t, 'container': c, 'cid': c.id };
1029 function cbi_t_switch(section, tab) {
1030 if( cbi_t[section] && cbi_t[section][tab] ) {
1031 var o = cbi_t[section][tab];
1032 var h = document.getElementById('tab.' + section);
1033 for( var tid in cbi_t[section] ) {
1034 var o2 = cbi_t[section][tid];
1035 if( o.tab.id != o2.tab.id ) {
1036 o2.tab.className = o2.tab.className.replace(/(^| )cbi-tab( |$)/, " cbi-tab-disabled ");
1037 o2.container.style.display = 'none';
1040 if(h) h.value = tab;
1041 o2.tab.className = o2.tab.className.replace(/(^| )cbi-tab-disabled( |$)/, " cbi-tab ");
1042 o2.container.style.display = 'block';
1049 function cbi_t_update() {
1051 var updated = false;
1053 for( var sid in cbi_t )
1054 for( var tid in cbi_t[sid] )
1056 var t = cbi_t[sid][tid].tab;
1057 var c = cbi_t[sid][tid].container;
1059 if (!c.firstElementChild) {
1060 t.style.display = 'none';
1062 else if (t.style.display == 'none') {
1063 t.style.display = '';
1064 t.className += ' cbi-tab-highlighted';
1072 if (hl_tabs.length > 0)
1073 window.setTimeout(function() {
1074 for( var i = 0; i < hl_tabs.length; i++ )
1075 hl_tabs[i].className = hl_tabs[i].className.replace(/ cbi-tab-highlighted/g, '');
1082 function cbi_validate_form(form, errmsg)
1084 /* if triggered by a section removal or addition, don't validate */
1085 if( form.cbi_state == 'add-section' || form.cbi_state == 'del-section' )
1088 if( form.cbi_validators )
1090 for( var i = 0; i < form.cbi_validators.length; i++ )
1092 var validator = form.cbi_validators[i];
1093 if( !validator() && errmsg )
1104 function cbi_validate_reset(form)
1107 function() { cbi_validate_form(form, null) }, 100
1113 function cbi_validate_compile(code)
1122 for (var i = 0; i < code.length; i++)
1130 switch (code.charCodeAt(i))
1142 var label = code.substring(pos, i);
1143 label = label.replace(/\\(.)/g, '$1');
1144 label = label.replace(/^[ \t]+/g, '');
1145 label = label.replace(/[ \t]+$/g, '');
1147 if (label && !isNaN(label))
1149 stack.push(parseFloat(label));
1151 else if (label.match(/^(['"]).*\1$/))
1153 stack.push(label.replace(/^(['"])(.*)\1$/, '$2'));
1155 else if (typeof cbi_validators[label] == 'function')
1157 stack.push(cbi_validators[label]);
1162 throw "Syntax error, unhandled token '"+label+"'";
1167 depth += (code.charCodeAt(i) == 40);
1173 if (typeof stack[stack.length-2] != 'function')
1174 throw "Syntax error, argument list follows non-function";
1176 stack[stack.length-1] =
1177 arguments.callee(code.substring(pos, i));
1188 function cbi_validate_field(cbid, optional, type)
1190 var field = (typeof cbid == "string") ? document.getElementById(cbid) : cbid;
1191 var vstack; try { vstack = cbi_validate_compile(type); } catch(e) { };
1193 if (field && vstack && typeof vstack[0] == "function")
1195 var validator = function()
1200 field.className = field.className.replace(/ cbi-input-invalid/g, '');
1203 var value = (field.options && field.options.selectedIndex > -1)
1204 ? field.options[field.options.selectedIndex].value : field.value;
1206 if (!(((value.length == 0) && optional) || vstack[0].apply(value, vstack[1])))
1209 field.className += ' cbi-input-invalid';
1217 if( ! field.form.cbi_validators )
1218 field.form.cbi_validators = [ ];
1220 field.form.cbi_validators.push(validator);
1222 cbi_bind(field, "blur", validator);
1223 cbi_bind(field, "keyup", validator);
1225 if (field.nodeName == 'SELECT')
1227 cbi_bind(field, "change", validator);
1228 cbi_bind(field, "click", validator);
1231 field.setAttribute("cbi_validate", validator);
1232 field.setAttribute("cbi_datatype", type);
1233 field.setAttribute("cbi_optional", (!!optional).toString());
1237 var fcbox = document.getElementById('cbi.combobox.' + field.id);
1239 cbi_validate_field(fcbox, optional, type);
1243 function cbi_row_swap(elem, up, store)
1245 var tr = elem.parentNode;
1246 while (tr && tr.nodeName.toLowerCase() != 'tr')
1252 var table = tr.parentNode;
1253 while (table && table.nodeName.toLowerCase() != 'table')
1254 table = table.parentNode;
1260 var e = up ? table.rows.length : table.rows.length - 1;
1262 for (var idx = s; idx < e; idx++)
1264 if (table.rows[idx] == tr)
1267 tr.parentNode.insertBefore(table.rows[idx], table.rows[idx-1]);
1269 tr.parentNode.insertBefore(table.rows[idx+1], table.rows[idx]);
1276 for (idx = 2; idx < table.rows.length; idx++)
1278 table.rows[idx].className = table.rows[idx].className.replace(
1279 /cbi-rowstyle-[12]/, 'cbi-rowstyle-' + (1 + (idx % 2))
1282 if (table.rows[idx].id && table.rows[idx].id.match(/-([^\-]+)$/) )
1283 ids.push(RegExp.$1);
1286 var input = document.getElementById(store);
1288 input.value = ids.join(' ');
1293 function cbi_tag_last(container)
1297 for (var i = 0; i < container.childNodes.length; i++)
1299 var c = container.childNodes[i];
1300 if (c.nodeType == 1 && c.nodeName.toLowerCase() == 'div')
1302 c.className = c.className.replace(/ cbi-value-last$/, '');
1309 last.className += ' cbi-value-last';
1313 String.prototype.serialize = function()
1330 for( var i = 0; i < o.length; i++ )
1331 s += (s ? ', ' : '') + String.serialize(o[i]);
1333 return '[ ' + s + ' ]';
1342 s += (s ? ', ' : '') + k + ': ' + String.serialize(o[k]);
1344 return '{ ' + s + ' }';
1351 if( o.match(/[^a-zA-Z0-9_,.: -]/) )
1352 return 'decodeURIComponent("' + encodeURIComponent(o) + '")';
1356 return '"' + o + '"';
1361 return o.toString();
1365 String.prototype.format = function()
1370 var html_esc = [/&/g, '&', /"/g, '"', /'/g, ''', /</g, '<', />/g, '>'];
1371 var quot_esc = [/"/g, '"', /'/g, '''];
1373 function esc(s, r) {
1374 if (typeof(s) !== 'string' && !(s instanceof String))
1377 for( var i = 0; i < r.length; i += 2 )
1378 s = s.replace(r[i], r[i+1]);
1384 var re = /^(([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X|q|h|j|t|m))/;
1385 var a = b = [], numSubstitutions = 0, numMatches = 0;
1387 while (a = re.exec(str))
1390 var leftpart = a[2], pPad = a[3], pJustify = a[4], pMinLength = a[5];
1391 var pPrecision = a[6], pType = a[7];
1401 if (numSubstitutions < arguments.length)
1403 var param = arguments[numSubstitutions++];
1406 if (pPad && pPad.substr(0,1) == "'")
1407 pad = leftpart.substr(1,1);
1413 var justifyRight = true;
1414 if (pJustify && pJustify === "-")
1415 justifyRight = false;
1419 minLength = +pMinLength;
1422 if (pPrecision && pType == 'f')
1423 precision = +pPrecision.substring(1);
1430 subst = (+param || 0).toString(2);
1434 subst = String.fromCharCode(+param || 0);
1438 subst = ~~(+param || 0);
1442 subst = ~~Math.abs(+param || 0);
1446 subst = (precision > -1)
1447 ? ((+param || 0.0)).toFixed(precision)
1452 subst = (+param || 0).toString(8);
1460 subst = ('' + (+param || 0).toString(16)).toLowerCase();
1464 subst = ('' + (+param || 0).toString(16)).toUpperCase();
1468 subst = esc(param, html_esc);
1472 subst = esc(param, quot_esc);
1476 subst = String.serialize(param);
1483 var ts = (param || 0);
1486 tm = Math.floor(ts / 60);
1491 th = Math.floor(tm / 60);
1496 td = Math.floor(th / 24);
1501 ? String.format('%dd %dh %dm %ds', td, th, tm, ts)
1502 : String.format('%dh %dm %ds', th, tm, ts);
1507 var mf = pMinLength ? +pMinLength : 1000;
1508 var pr = pPrecision ? ~~(10 * +('0' + pPrecision)) : 2;
1511 var val = (+param || 0);
1512 var units = [ ' ', ' K', ' M', ' G', ' T', ' P', ' E' ];
1514 for (i = 0; (i < units.length) && (val > mf); i++)
1517 subst = (i ? val.toFixed(pr) : val) + units[i];
1525 subst = subst.toString();
1526 for (var i = subst.length; i < pMinLength; i++)
1527 if (pJustify == '-')
1528 subst = subst + ' ';
1530 subst = pad + subst;
1533 out += leftpart + subst;
1534 str = str.substr(m.length);
1540 String.prototype.nobr = function()
1542 return this.replace(/[\s\n]+/g, ' ');
1545 String.serialize = function()
1548 for (var i = 1; i < arguments.length; i++)
1549 a.push(arguments[i]);
1550 return ''.serialize.apply(arguments[0], a);
1553 String.format = function()
1556 for (var i = 1; i < arguments.length; i++)
1557 a.push(arguments[i]);
1558 return ''.format.apply(arguments[0], a);
1561 String.nobr = function()
1564 for (var i = 1; i < arguments.length; i++)
1565 a.push(arguments[i]);
1566 return ''.nobr.apply(arguments[0], a);