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