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