libs/web: fix undesired autofocus of DynamicList widget
[project/luci.git] / libs / web / htdocs / luci-static / resources / cbi.js
1 /*
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008-2011 Jo-Philipp Wich <xm@subsignal.org>
6
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
10
11 http://www.apache.org/licenses/LICENSE-2.0
12 */
13
14 var cbi_d = [];
15 var cbi_t = [];
16 var cbi_c = [];
17
18 var cbi_validators = {
19
20 'integer': function(v)
21 {
22 return (v.match(/^-?[0-9]+$/) != null);
23 },
24
25 'uinteger': function(v)
26 {
27 return (cbi_validators.integer(v) && (v >= 0));
28 },
29
30 'float': function(v)
31 {
32 return !isNaN(parseFloat(v));
33 },
34
35 'ufloat': function(v)
36 {
37 return (cbi_validators['float'](v) && (v >= 0));
38 },
39
40 'ipaddr': function(v)
41 {
42 return cbi_validators.ip4addr(v) || cbi_validators.ip6addr(v);
43 },
44
45 'neg_ipaddr': function(v)
46 {
47 return cbi_validators.ip4addr(v.replace(/^\s*!/, "")) || cbi_validators.ip6addr(v.replace(/^\s*!/, ""));
48 },
49
50 'ip4addr': function(v)
51 {
52 if (v.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})(\/(\S+))?$/))
53 {
54 return (RegExp.$1 >= 0) && (RegExp.$1 <= 255) &&
55 (RegExp.$2 >= 0) && (RegExp.$2 <= 255) &&
56 (RegExp.$3 >= 0) && (RegExp.$3 <= 255) &&
57 (RegExp.$4 >= 0) && (RegExp.$4 <= 255) &&
58 ((RegExp.$6.indexOf('.') < 0)
59 ? ((RegExp.$6 >= 0) && (RegExp.$6 <= 32))
60 : (cbi_validators.ip4addr(RegExp.$6)))
61 ;
62 }
63
64 return false;
65 },
66
67 'neg_ip4addr': function(v)
68 {
69 return cbi_validators.ip4addr(v.replace(/^\s*!/, ""));
70 },
71
72 'ip6addr': function(v)
73 {
74 if( v.match(/^([a-fA-F0-9:.]+)(\/(\d+))?$/) )
75 {
76 if( !RegExp.$2 || ((RegExp.$3 >= 0) && (RegExp.$3 <= 128)) )
77 {
78 var addr = RegExp.$1;
79
80 if( addr == '::' )
81 {
82 return true;
83 }
84
85 if( addr.indexOf('.') > 0 )
86 {
87 var off = addr.lastIndexOf(':');
88
89 if( !(off && cbi_validators.ip4addr(addr.substr(off+1))) )
90 return false;
91
92 addr = addr.substr(0, off) + ':0:0';
93 }
94
95 if( addr.indexOf('::') >= 0 )
96 {
97 var colons = 0;
98 var fill = '0';
99
100 for( var i = 1; i < (addr.length-1); i++ )
101 if( addr.charAt(i) == ':' )
102 colons++;
103
104 if( colons > 7 )
105 return false;
106
107 for( var i = 0; i < (7 - colons); i++ )
108 fill += ':0';
109
110 if (addr.match(/^(.*?)::(.*?)$/))
111 addr = (RegExp.$1 ? RegExp.$1 + ':' : '') + fill +
112 (RegExp.$2 ? ':' + RegExp.$2 : '');
113 }
114
115 return (addr.match(/^(?:[a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4}$/) != null);
116 }
117 }
118
119 return false;
120 },
121
122 'port': function(v)
123 {
124 return cbi_validators.integer(v) && (v >= 0) && (v <= 65535);
125 },
126
127 'portrange': function(v)
128 {
129 if( v.match(/^(\d+)-(\d+)$/) )
130 {
131 var p1 = RegExp.$1;
132 var p2 = RegExp.$2;
133
134 return cbi_validators.port(p1) &&
135 cbi_validators.port(p2) &&
136 (parseInt(p1) <= parseInt(p2))
137 ;
138 }
139 else
140 {
141 return cbi_validators.port(v);
142 }
143 },
144
145 'macaddr': function(v)
146 {
147 return (v.match(/^([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}$/) != null);
148 },
149
150 'host': function(v)
151 {
152 return cbi_validators.hostname(v) || cbi_validators.ipaddr(v);
153 },
154
155 'hostname': function(v)
156 {
157 if (v.length <= 253)
158 return (v.match(/^[a-zA-Z]+$/) != null ||
159 (v.match(/^[a-zA-Z0-9][a-zA-Z0-9\-.]*[a-zA-Z0-9]$/) &&
160 v.match(/[^0-9.]/)));
161
162 return false;
163 },
164
165 'network': function(v)
166 {
167 return cbi_validators.uciname(v) || cbi_validators.host(v);
168 },
169
170 'wpakey': function(v)
171 {
172 if( v.length == 64 )
173 return (v.match(/^[a-fA-F0-9]{64}$/) != null);
174 else
175 return (v.length >= 8) && (v.length <= 63);
176 },
177
178 'wepkey': function(v)
179 {
180 if( v.substr(0,2) == 's:' )
181 v = v.substr(2);
182
183 if( (v.length == 10) || (v.length == 26) )
184 return (v.match(/^[a-fA-F0-9]{10,26}$/) != null);
185 else
186 return (v.length == 5) || (v.length == 13);
187 },
188
189 'uciname': function(v)
190 {
191 return (v.match(/^[a-zA-Z0-9_]+$/) != null);
192 },
193
194 'neg_network_ip4addr': function(v)
195 {
196 v = v.replace(/^\s*!/, "");
197 return cbi_validators.uciname(v) || cbi_validators.ip4addr(v);
198 },
199
200 'range': function(v, args)
201 {
202 var min = parseInt(args[0]);
203 var max = parseInt(args[1]);
204 var val = parseInt(v);
205
206 if (!isNaN(min) && !isNaN(max) && !isNaN(val))
207 return ((val >= min) && (val <= max));
208
209 return false;
210 },
211
212 'min': function(v, args)
213 {
214 var min = parseInt(args[0]);
215 var val = parseInt(v);
216
217 if (!isNaN(min) && !isNaN(val))
218 return (val >= min);
219
220 return false;
221 },
222
223 'max': function(v, args)
224 {
225 var max = parseInt(args[0]);
226 var val = parseInt(v);
227
228 if (!isNaN(max) && !isNaN(val))
229 return (val <= max);
230
231 return false;
232 },
233
234 'neg': function(v, args)
235 {
236 if (args[0] && typeof cbi_validators[args[0]] == "function")
237 return cbi_validators[args[0]](v.replace(/^\s*!\s*/, ''));
238
239 return false;
240 },
241
242 'list': function(v, args)
243 {
244 var cb = cbi_validators[args[0] || 'string'];
245 if (typeof cb == "function")
246 {
247 var cbargs = args.slice(1);
248 var values = v.match(/[^\s]+/g);
249
250 for (var i = 0; i < values.length; i++)
251 if (!cb(values[i], cbargs))
252 return false;
253
254 return true;
255 }
256
257 return false;
258 }
259 };
260
261
262 function cbi_d_add(field, dep, next) {
263 var obj = document.getElementById(field);
264 if (obj) {
265 var entry
266 for (var i=0; i<cbi_d.length; i++) {
267 if (cbi_d[i].id == field) {
268 entry = cbi_d[i];
269 break;
270 }
271 }
272 if (!entry) {
273 entry = {
274 "node": obj,
275 "id": field,
276 "parent": obj.parentNode.id,
277 "next": next,
278 "deps": []
279 };
280 cbi_d.unshift(entry);
281 }
282 entry.deps.push(dep)
283 }
284 }
285
286 function cbi_d_checkvalue(target, ref) {
287 var t = document.getElementById(target);
288 var value;
289
290 if (!t) {
291 var tl = document.getElementsByName(target);
292
293 if( tl.length > 0 && tl[0].type == 'radio' )
294 for( var i = 0; i < tl.length; i++ )
295 if( tl[i].checked ) {
296 value = tl[i].value;
297 break;
298 }
299
300 value = value ? value : "";
301 } else if (!t.value) {
302 value = "";
303 } else {
304 value = t.value;
305
306 if (t.type == "checkbox") {
307 value = t.checked ? value : "";
308 }
309 }
310
311 return (value == ref)
312 }
313
314 function cbi_d_check(deps) {
315 var reverse;
316 var def = false;
317 for (var i=0; i<deps.length; i++) {
318 var istat = true;
319 reverse = false;
320 for (var j in deps[i]) {
321 if (j == "!reverse") {
322 reverse = true;
323 } else if (j == "!default") {
324 def = true;
325 istat = false;
326 } else {
327 istat = (istat && cbi_d_checkvalue(j, deps[i][j]))
328 }
329 }
330 if (istat) {
331 return !reverse;
332 }
333 }
334 return def;
335 }
336
337 function cbi_d_update() {
338 var state = false;
339 for (var i=0; i<cbi_d.length; i++) {
340 var entry = cbi_d[i];
341 var next = document.getElementById(entry.next)
342 var node = document.getElementById(entry.id)
343 var parent = document.getElementById(entry.parent)
344
345 if (node && node.parentNode && !cbi_d_check(entry.deps)) {
346 node.parentNode.removeChild(node);
347 state = true;
348 if( entry.parent )
349 cbi_c[entry.parent]--;
350 } else if ((!node || !node.parentNode) && cbi_d_check(entry.deps)) {
351 if (!next) {
352 parent.appendChild(entry.node);
353 } else {
354 next.parentNode.insertBefore(entry.node, next);
355 }
356 state = true;
357 if( entry.parent )
358 cbi_c[entry.parent]++;
359 }
360 }
361
362 if (entry && entry.parent) {
363 if (!cbi_t_update())
364 cbi_tag_last(parent);
365 }
366
367 if (state) {
368 cbi_d_update();
369 }
370 }
371
372 function cbi_bind(obj, type, callback, mode) {
373 if (!obj.addEventListener) {
374 obj.attachEvent('on' + type,
375 function(){
376 var e = window.event;
377
378 if (!e.target && e.srcElement)
379 e.target = e.srcElement;
380
381 return !!callback(e);
382 }
383 );
384 } else {
385 obj.addEventListener(type, callback, !!mode);
386 }
387 return obj;
388 }
389
390 function cbi_combobox(id, values, def, man) {
391 var selid = "cbi.combobox." + id;
392 if (document.getElementById(selid)) {
393 return
394 }
395
396 var obj = document.getElementById(id)
397 var sel = document.createElement("select");
398 sel.id = selid;
399 sel.className = 'cbi-input-select';
400
401 if (obj.nextSibling) {
402 obj.parentNode.insertBefore(sel, obj.nextSibling);
403 } else {
404 obj.parentNode.appendChild(sel);
405 }
406
407 var dt = obj.getAttribute('cbi_datatype');
408 var op = obj.getAttribute('cbi_optional');
409
410 if (dt)
411 cbi_validate_field(sel, op == 'true', dt);
412
413 if (!values[obj.value]) {
414 if (obj.value == "") {
415 var optdef = document.createElement("option");
416 optdef.value = "";
417 optdef.appendChild(document.createTextNode(def));
418 sel.appendChild(optdef);
419 } else {
420 var opt = document.createElement("option");
421 opt.value = obj.value;
422 opt.selected = "selected";
423 opt.appendChild(document.createTextNode(obj.value));
424 sel.appendChild(opt);
425 }
426 }
427
428 for (var i in values) {
429 var opt = document.createElement("option");
430 opt.value = i;
431
432 if (obj.value == i) {
433 opt.selected = "selected";
434 }
435
436 opt.appendChild(document.createTextNode(values[i]));
437 sel.appendChild(opt);
438 }
439
440 var optman = document.createElement("option");
441 optman.value = "";
442 optman.appendChild(document.createTextNode(man));
443 sel.appendChild(optman);
444
445 obj.style.display = "none";
446
447 cbi_bind(sel, "change", function() {
448 if (sel.selectedIndex == sel.options.length - 1) {
449 obj.style.display = "inline";
450 sel.parentNode.removeChild(sel);
451 obj.focus();
452 } else {
453 obj.value = sel.options[sel.selectedIndex].value;
454 }
455
456 try {
457 cbi_d_update();
458 } catch (e) {
459 //Do nothing
460 }
461 })
462 }
463
464 function cbi_combobox_init(id, values, def, man) {
465 var obj = document.getElementById(id);
466 cbi_bind(obj, "blur", function() {
467 cbi_combobox(id, values, def, man)
468 });
469 cbi_combobox(id, values, def, man);
470 }
471
472 function cbi_filebrowser(id, url, defpath) {
473 var field = document.getElementById(id);
474 var browser = window.open(
475 url + ( field.value || defpath || '' ) + '?field=' + id,
476 "luci_filebrowser", "width=300,height=400,left=100,top=200,scrollbars=yes"
477 );
478
479 browser.focus();
480 }
481
482 function cbi_browser_init(id, respath, url, defpath)
483 {
484 function cbi_browser_btnclick(e) {
485 cbi_filebrowser(id, url, defpath);
486 return false;
487 }
488
489 var field = document.getElementById(id);
490
491 var btn = document.createElement('img');
492 btn.className = 'cbi-image-button';
493 btn.src = respath + '/cbi/folder.gif';
494 field.parentNode.insertBefore(btn, field.nextSibling);
495
496 cbi_bind(btn, 'click', cbi_browser_btnclick);
497 }
498
499 function cbi_dynlist_init(name, respath, datatype, optional, choices)
500 {
501 var input0 = document.getElementsByName(name)[0];
502 var prefix = input0.name;
503 var parent = input0.parentNode;
504
505 var values;
506
507 function cbi_dynlist_redraw(focus, add, del)
508 {
509 values = [ ];
510
511 while (parent.firstChild)
512 {
513 var n = parent.firstChild;
514 var i = parseInt(n.index);
515
516 if (i != del)
517 {
518 if (n.nodeName.toLowerCase() == 'input')
519 values.push(n.value || '');
520 else if (n.nodeName.toLowerCase() == 'select')
521 values[values.length-1] = n.options[n.selectedIndex].value;
522 }
523
524 parent.removeChild(n);
525 }
526
527 if (add >= 0)
528 {
529 focus = add+1;
530 values.splice(focus, 0, '');
531 }
532 else if (values.length == 0)
533 {
534 focus = 0;
535 values.push('');
536 }
537
538 for (var i = 0; i < values.length; i++)
539 {
540 var t = document.createElement('input');
541 t.id = prefix + '.' + (i+1);
542 t.name = prefix;
543 t.value = values[i];
544 t.type = 'text';
545 t.index = i;
546 t.className = 'cbi-input-text';
547
548 var b = document.createElement('img');
549 b.src = respath + ((i+1) < values.length ? '/cbi/remove.gif' : '/cbi/add.gif');
550 b.className = 'cbi-image-button';
551
552 parent.appendChild(t);
553 parent.appendChild(b);
554 parent.appendChild(document.createElement('br'));
555
556 if (datatype)
557 {
558 cbi_validate_field(t.id, ((i+1) == values.length) || optional, datatype);
559 }
560
561 if (choices)
562 {
563 cbi_combobox_init(t.id, choices[0], '', choices[1]);
564 t.nextSibling.index = i;
565
566 cbi_bind(t.nextSibling, 'keydown', cbi_dynlist_keydown);
567 cbi_bind(t.nextSibling, 'keypress', cbi_dynlist_keypress);
568
569 if (i == focus || -i == focus)
570 t.nextSibling.focus();
571 }
572 else
573 {
574 cbi_bind(t, 'keydown', cbi_dynlist_keydown);
575 cbi_bind(t, 'keypress', cbi_dynlist_keypress);
576
577 if (i == focus)
578 {
579 t.focus();
580 }
581 else if (-i == focus)
582 {
583 t.focus();
584
585 /* force cursor to end */
586 var v = t.value;
587 t.value = ' '
588 t.value = v;
589 }
590 }
591
592 cbi_bind(b, 'click', cbi_dynlist_btnclick);
593 }
594 }
595
596 function cbi_dynlist_keypress(ev)
597 {
598 ev = ev ? ev : window.event;
599
600 var se = ev.target ? ev.target : ev.srcElement;
601
602 if (se.nodeType == 3)
603 se = se.parentNode;
604
605 switch (ev.keyCode)
606 {
607 /* backspace, delete */
608 case 8:
609 case 46:
610 if (se.value.length == 0)
611 {
612 if (ev.preventDefault)
613 ev.preventDefault();
614
615 return false;
616 }
617
618 return true;
619
620 /* enter, arrow up, arrow down */
621 case 13:
622 case 38:
623 case 40:
624 if (ev.preventDefault)
625 ev.preventDefault();
626
627 return false;
628 }
629
630 return true;
631 }
632
633 function cbi_dynlist_keydown(ev)
634 {
635 ev = ev ? ev : window.event;
636
637 var se = ev.target ? ev.target : ev.srcElement;
638
639 if (se.nodeType == 3)
640 se = se.parentNode;
641
642 var prev = se.previousSibling;
643 while (prev && prev.name != name)
644 prev = prev.previousSibling;
645
646 var next = se.nextSibling;
647 while (next && next.name != name)
648 next = next.nextSibling;
649
650 /* advance one further in combobox case */
651 if (next && next.nextSibling.name == name)
652 next = next.nextSibling;
653
654 switch (ev.keyCode)
655 {
656 /* backspace, delete */
657 case 8:
658 case 46:
659 var del = (se.nodeName.toLowerCase() == 'select')
660 ? true : (se.value.length == 0);
661
662 if (del)
663 {
664 if (ev.preventDefault)
665 ev.preventDefault();
666
667 var focus = se.index;
668 if (ev.keyCode == 8)
669 focus = -focus+1;
670
671 cbi_dynlist_redraw(focus, -1, se.index);
672
673 return false;
674 }
675
676 break;
677
678 /* enter */
679 case 13:
680 cbi_dynlist_redraw(-1, se.index, -1);
681 break;
682
683 /* arrow up */
684 case 38:
685 if (prev)
686 prev.focus();
687
688 break;
689
690 /* arrow down */
691 case 40:
692 if (next)
693 next.focus();
694
695 break;
696 }
697
698 return true;
699 }
700
701 function cbi_dynlist_btnclick(ev)
702 {
703 ev = ev ? ev : window.event;
704
705 var se = ev.target ? ev.target : ev.srcElement;
706
707 if (se.src.indexOf('remove') > -1)
708 {
709 se.previousSibling.value = '';
710
711 cbi_dynlist_keydown({
712 target: se.previousSibling,
713 keyCode: 8
714 });
715 }
716 else
717 {
718 cbi_dynlist_keydown({
719 target: se.previousSibling,
720 keyCode: 13
721 });
722 }
723
724 return false;
725 }
726
727 cbi_dynlist_redraw(-1, -1, -1);
728 }
729
730 //Hijacks the CBI form to send via XHR (requires Prototype)
731 function cbi_hijack_forms(layer, win, fail, load) {
732 var forms = layer.getElementsByTagName('form');
733 for (var i=0; i<forms.length; i++) {
734 $(forms[i]).observe('submit', function(event) {
735 // Prevent the form from also submitting the regular way
736 event.stop();
737
738 // Submit via XHR
739 event.element().request({
740 onSuccess: win,
741 onFailure: fail
742 });
743
744 if (load) {
745 load();
746 }
747 });
748 }
749 }
750
751
752 function cbi_t_add(section, tab) {
753 var t = document.getElementById('tab.' + section + '.' + tab);
754 var c = document.getElementById('container.' + section + '.' + tab);
755
756 if( t && c ) {
757 cbi_t[section] = (cbi_t[section] || [ ]);
758 cbi_t[section][tab] = { 'tab': t, 'container': c, 'cid': c.id };
759 }
760 }
761
762 function cbi_t_switch(section, tab) {
763 if( cbi_t[section] && cbi_t[section][tab] ) {
764 var o = cbi_t[section][tab];
765 var h = document.getElementById('tab.' + section);
766 for( var tid in cbi_t[section] ) {
767 var o2 = cbi_t[section][tid];
768 if( o.tab.id != o2.tab.id ) {
769 o2.tab.className = o2.tab.className.replace(/(^| )cbi-tab( |$)/, " cbi-tab-disabled ");
770 o2.container.style.display = 'none';
771 }
772 else {
773 if(h) h.value = tab;
774 o2.tab.className = o2.tab.className.replace(/(^| )cbi-tab-disabled( |$)/, " cbi-tab ");
775 o2.container.style.display = 'block';
776 }
777 }
778 }
779 return false
780 }
781
782 function cbi_t_update() {
783 var hl_tabs = [ ];
784 var updated = false;
785
786 for( var sid in cbi_t )
787 for( var tid in cbi_t[sid] )
788 {
789 if( cbi_c[cbi_t[sid][tid].cid] == 0 ) {
790 cbi_t[sid][tid].tab.style.display = 'none';
791 }
792 else if( cbi_t[sid][tid].tab && cbi_t[sid][tid].tab.style.display == 'none' ) {
793 cbi_t[sid][tid].tab.style.display = '';
794
795 var t = cbi_t[sid][tid].tab;
796 t.className += ' cbi-tab-highlighted';
797 hl_tabs.push(t);
798 }
799
800 cbi_tag_last(cbi_t[sid][tid].container);
801 updated = true;
802 }
803
804 if( hl_tabs.length > 0 )
805 window.setTimeout(function() {
806 for( var i = 0; i < hl_tabs.length; i++ )
807 hl_tabs[i].className = hl_tabs[i].className.replace(/ cbi-tab-highlighted/g, '');
808 }, 750);
809
810 return updated;
811 }
812
813
814 function cbi_validate_form(form, errmsg)
815 {
816 /* if triggered by a section removal or addition, don't validate */
817 if( form.cbi_state == 'add-section' || form.cbi_state == 'del-section' )
818 return true;
819
820 if( form.cbi_validators )
821 {
822 for( var i = 0; i < form.cbi_validators.length; i++ )
823 {
824 var validator = form.cbi_validators[i];
825 if( !validator() && errmsg )
826 {
827 alert(errmsg);
828 return false;
829 }
830 }
831 }
832
833 return true;
834 }
835
836 function cbi_validate_reset(form)
837 {
838 window.setTimeout(
839 function() { cbi_validate_form(form, null) }, 100
840 );
841
842 return true;
843 }
844
845 function cbi_validate_field(cbid, optional, type)
846 {
847 var field = (typeof cbid == "string") ? document.getElementById(cbid) : cbid;
848 var vargs;
849
850 if( type.match(/^(\w+)\(([^\(\)]+)\)/) )
851 {
852 type = RegExp.$1;
853 vargs = RegExp.$2.split(/\s*,\s*/);
854 }
855
856 var vldcb = cbi_validators[type];
857
858 if( field && vldcb )
859 {
860 var validator = function()
861 {
862 // is not detached
863 if( field.form )
864 {
865 field.className = field.className.replace(/ cbi-input-invalid/g, '');
866
867 // validate value
868 var value = (field.options && field.options.selectedIndex > -1)
869 ? field.options[field.options.selectedIndex].value : field.value;
870
871 if( !(((value.length == 0) && optional) || vldcb(value, vargs)) )
872 {
873 // invalid
874 field.className += ' cbi-input-invalid';
875 return false;
876 }
877 }
878
879 return true;
880 };
881
882 if( ! field.form.cbi_validators )
883 field.form.cbi_validators = [ ];
884
885 field.form.cbi_validators.push(validator);
886
887 cbi_bind(field, "blur", validator);
888 cbi_bind(field, "keyup", validator);
889
890 if (field.nodeName == 'SELECT')
891 {
892 cbi_bind(field, "change", validator);
893 cbi_bind(field, "click", validator);
894 }
895
896 field.setAttribute("cbi_validate", validator);
897 field.setAttribute("cbi_datatype", type);
898 field.setAttribute("cbi_optional", (!!optional).toString());
899
900 validator();
901
902 var fcbox = document.getElementById('cbi.combobox.' + field.id);
903 if (fcbox)
904 cbi_validate_field(fcbox, optional, type);
905 }
906 }
907
908 function cbi_row_swap(elem, up, store)
909 {
910 var tr = elem.parentNode;
911 while (tr && tr.nodeName.toLowerCase() != 'tr')
912 tr = tr.parentNode;
913
914 if (!tr)
915 return false;
916
917 var table = tr.parentNode;
918 while (table && table.nodeName.toLowerCase() != 'table')
919 table = table.parentNode;
920
921 if (!table)
922 return false;
923
924 var s = up ? 3 : 2;
925 var e = up ? table.rows.length : table.rows.length - 1;
926
927 for (var idx = s; idx < e; idx++)
928 {
929 if (table.rows[idx] == tr)
930 {
931 if (up)
932 tr.parentNode.insertBefore(table.rows[idx], table.rows[idx-1]);
933 else
934 tr.parentNode.insertBefore(table.rows[idx+1], table.rows[idx]);
935
936 break;
937 }
938 }
939
940 var ids = [ ];
941 for (idx = 2; idx < table.rows.length; idx++)
942 {
943 table.rows[idx].className = table.rows[idx].className.replace(
944 /cbi-rowstyle-[12]/, 'cbi-rowstyle-' + (1 + (idx % 2))
945 );
946
947 if (table.rows[idx].id && table.rows[idx].id.match(/-([^\-]+)$/) )
948 ids.push(RegExp.$1);
949 }
950
951 var input = document.getElementById(store);
952 if (input)
953 input.value = ids.join(' ');
954
955 return false;
956 }
957
958 function cbi_tag_last(container)
959 {
960 var last;
961
962 for (var i = 0; i < container.childNodes.length; i++)
963 {
964 var c = container.childNodes[i];
965 if (c.nodeType == 1 && c.nodeName.toLowerCase() == 'div')
966 {
967 c.className = c.className.replace(/ cbi-value-last$/, '');
968 last = c;
969 }
970 }
971
972 if (last)
973 {
974 last.className += ' cbi-value-last';
975 }
976 }
977
978 if( ! String.serialize )
979 String.serialize = function(o)
980 {
981 switch(typeof(o))
982 {
983 case 'object':
984 // null
985 if( o == null )
986 {
987 return 'null';
988 }
989
990 // array
991 else if( o.length )
992 {
993 var i, s = '';
994
995 for( var i = 0; i < o.length; i++ )
996 s += (s ? ', ' : '') + String.serialize(o[i]);
997
998 return '[ ' + s + ' ]';
999 }
1000
1001 // object
1002 else
1003 {
1004 var k, s = '';
1005
1006 for( k in o )
1007 s += (s ? ', ' : '') + k + ': ' + String.serialize(o[k]);
1008
1009 return '{ ' + s + ' }';
1010 }
1011
1012 break;
1013
1014 case 'string':
1015 // complex string
1016 if( o.match(/[^a-zA-Z0-9_,.: -]/) )
1017 return 'decodeURIComponent("' + encodeURIComponent(o) + '")';
1018
1019 // simple string
1020 else
1021 return '"' + o + '"';
1022
1023 break;
1024
1025 default:
1026 return o.toString();
1027 }
1028 }
1029
1030
1031 if( ! String.format )
1032 String.format = function()
1033 {
1034 if (!arguments || arguments.length < 1 || !RegExp)
1035 return;
1036
1037 var html_esc = [/&/g, '&#38;', /"/g, '&#34;', /'/g, '&#39;', /</g, '&#60;', />/g, '&#62;'];
1038 var quot_esc = [/"/g, '&#34;', /'/g, '&#39;'];
1039
1040 function esc(s, r) {
1041 for( var i = 0; i < r.length; i += 2 )
1042 s = s.replace(r[i], r[i+1]);
1043 return s;
1044 }
1045
1046 var str = arguments[0];
1047 var out = '';
1048 var re = /^(([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X|q|h|j|t|m))/;
1049 var a = b = [], numSubstitutions = 0, numMatches = 0;
1050
1051 while( a = re.exec(str) )
1052 {
1053 var m = a[1];
1054 var leftpart = a[2], pPad = a[3], pJustify = a[4], pMinLength = a[5];
1055 var pPrecision = a[6], pType = a[7];
1056
1057 numMatches++;
1058
1059 if (pType == '%')
1060 {
1061 subst = '%';
1062 }
1063 else
1064 {
1065 if (numSubstitutions++ < arguments.length)
1066 {
1067 var param = arguments[numSubstitutions];
1068
1069 var pad = '';
1070 if (pPad && pPad.substr(0,1) == "'")
1071 pad = leftpart.substr(1,1);
1072 else if (pPad)
1073 pad = pPad;
1074
1075 var justifyRight = true;
1076 if (pJustify && pJustify === "-")
1077 justifyRight = false;
1078
1079 var minLength = -1;
1080 if (pMinLength)
1081 minLength = parseInt(pMinLength);
1082
1083 var precision = -1;
1084 if (pPrecision && pType == 'f')
1085 precision = parseInt(pPrecision.substring(1));
1086
1087 var subst = param;
1088
1089 switch(pType)
1090 {
1091 case 'b':
1092 subst = (parseInt(param) || 0).toString(2);
1093 break;
1094
1095 case 'c':
1096 subst = String.fromCharCode(parseInt(param) || 0);
1097 break;
1098
1099 case 'd':
1100 subst = (parseInt(param) || 0);
1101 break;
1102
1103 case 'u':
1104 subst = Math.abs(parseInt(param) || 0);
1105 break;
1106
1107 case 'f':
1108 subst = (precision > -1)
1109 ? ((parseFloat(param) || 0.0)).toFixed(precision)
1110 : (parseFloat(param) || 0.0);
1111 break;
1112
1113 case 'o':
1114 subst = (parseInt(param) || 0).toString(8);
1115 break;
1116
1117 case 's':
1118 subst = param;
1119 break;
1120
1121 case 'x':
1122 subst = ('' + (parseInt(param) || 0).toString(16)).toLowerCase();
1123 break;
1124
1125 case 'X':
1126 subst = ('' + (parseInt(param) || 0).toString(16)).toUpperCase();
1127 break;
1128
1129 case 'h':
1130 subst = esc(param, html_esc);
1131 break;
1132
1133 case 'q':
1134 subst = esc(param, quot_esc);
1135 break;
1136
1137 case 'j':
1138 subst = String.serialize(param);
1139 break;
1140
1141 case 't':
1142 var td = 0;
1143 var th = 0;
1144 var tm = 0;
1145 var ts = (param || 0);
1146
1147 if (ts > 60) {
1148 tm = Math.floor(ts / 60);
1149 ts = (ts % 60);
1150 }
1151
1152 if (tm > 60) {
1153 th = Math.floor(tm / 60);
1154 tm = (tm % 60);
1155 }
1156
1157 if (th > 24) {
1158 td = Math.floor(th / 24);
1159 th = (th % 24);
1160 }
1161
1162 subst = (td > 0)
1163 ? String.format('%dd %dh %dm %ds', td, th, tm, ts)
1164 : String.format('%dh %dm %ds', th, tm, ts);
1165
1166 break;
1167
1168 case 'm':
1169 var mf = pMinLength ? parseInt(pMinLength) : 1000;
1170 var pr = pPrecision ? Math.floor(10*parseFloat('0'+pPrecision)) : 2;
1171
1172 var i = 0;
1173 var val = parseFloat(param || 0);
1174 var units = [ '', 'K', 'M', 'G', 'T', 'P', 'E' ];
1175
1176 for (i = 0; (i < units.length) && (val > mf); i++)
1177 val /= mf;
1178
1179 subst = val.toFixed(pr) + ' ' + units[i];
1180 break;
1181 }
1182 }
1183 }
1184
1185 out += leftpart + subst;
1186 str = str.substr(m.length);
1187 }
1188
1189 return out + str;
1190 }