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