+
+function cbi_filebrowser(id, url, defpath) {
+ var field = document.getElementById(id);
+ var browser = window.open(
+ url + ( field.value || defpath || '' ) + '?field=' + id,
+ "luci_filebrowser", "width=300,height=400,left=100,top=200,scrollbars=yes"
+ );
+
+ browser.focus();
+}
+
+//Hijacks the CBI form to send via XHR (requires Prototype)
+function cbi_hijack_forms(layer, win, fail, load) {
+ var forms = layer.getElementsByTagName('form');
+ for (var i=0; i<forms.length; i++) {
+ $(forms[i]).observe('submit', function(event) {
+ // Prevent the form from also submitting the regular way
+ event.stop();
+
+ // Submit via XHR
+ event.element().request({
+ onSuccess: win,
+ onFailure: fail
+ });
+
+ if (load) {
+ load();
+ }
+ });
+ }
+}
+
+
+function cbi_t_add(section, tab) {
+ var t = document.getElementById('tab.' + section + '.' + tab);
+ var c = document.getElementById('container.' + section + '.' + tab);
+
+ if( t && c ) {
+ cbi_t[section] = (cbi_t[section] || [ ]);
+ cbi_t[section][tab] = { 'tab': t, 'container': c, 'cid': c.id };
+ }
+}
+
+function cbi_t_switch(section, tab) {
+ if( cbi_t[section] && cbi_t[section][tab] ) {
+ var o = cbi_t[section][tab];
+ var h = document.getElementById('tab.' + section);
+ for( var tid in cbi_t[section] ) {
+ var o2 = cbi_t[section][tid];
+ if( o.tab.id != o2.tab.id ) {
+ o2.tab.className = o2.tab.className.replace(/(^| )cbi-tab( |$)/, " cbi-tab-disabled ");
+ o2.container.style.display = 'none';
+ }
+ else {
+ if(h) h.value = tab;
+ o2.tab.className = o2.tab.className.replace(/(^| )cbi-tab-disabled( |$)/, " cbi-tab ");
+ o2.container.style.display = 'block';
+ }
+ }
+ }
+ return false
+}
+
+function cbi_t_update() {
+ for( var sid in cbi_t )
+ for( var tid in cbi_t[sid] )
+ if( cbi_c[cbi_t[sid][tid].cid] == 0 ) {
+ cbi_t[sid][tid].tab.style.display = 'none';
+ }
+ else if( cbi_t[sid][tid].tab && cbi_t[sid][tid].tab.style.display == 'none' ) {
+ cbi_t[sid][tid].tab.style.display = '';
+
+ var t = cbi_t[sid][tid].tab;
+ window.setTimeout(function() { t.className = t.className.replace(/ cbi-tab-highlighted/g, '') }, 750);
+ cbi_t[sid][tid].tab.className += ' cbi-tab-highlighted';
+ }
+}
+
+
+function cbi_validate_form(form, errmsg)
+{
+ if( form.cbi_validators )
+ {
+ for( var i = 0; i < form.cbi_validators.length; i++ )
+ {
+ var validator = form.cbi_validators[i];
+ if( !validator() && errmsg )
+ {
+ alert(errmsg);
+ return false;
+ }
+ }
+ }
+
+ return true;
+}
+
+function cbi_validate_reset(form)
+{
+ window.setTimeout(
+ function() { cbi_validate_form(form, null) }, 100
+ );
+
+ return true;
+}
+
+function cbi_validate_field(cbid, optional, type)
+{
+ var field = document.getElementById(cbid);
+ var vldcb = cbi_validators[type];
+
+ if( field && vldcb )
+ {
+ var validator = function(reset)
+ {
+ // is not detached
+ if( field.form )
+ {
+ field.className = field.className.replace(/ cbi-input-invalid/g, '');
+
+ // validate value
+ var value = (field.options) ? field.options[field.options.selectedIndex].value : field.value;
+ if( !(((value.length == 0) && optional) || vldcb(value)) )
+ {
+ // invalid
+ field.className += ' cbi-input-invalid';
+ return false;
+ }
+ }
+
+ return true;
+ };
+
+ if( ! field.form.cbi_validators )
+ field.form.cbi_validators = [ ];
+
+ field.form.cbi_validators.push(validator);
+ field.onblur = field.onkeyup = field.validate = validator;
+
+ validator();
+ }
+}
+