// jquery on ready
$(document).ready(function () {
  bindLabel();
  stripeTable();
});

// stripe the table
function stripeTable() {
  $('.editForm > * > tr:odd').removeClass('alt');
  $('.editForm > * > tr:even').addClass('alt');
}

function bindLabel() {
  // bind the export_u dropdown
  $('.export_u').bind('change', function(e) {
    var v = $(this).attr('value');
    if (v == '1') {
      $(this).next().show();
    }
    else {
      $(this).next().hide();
    }
  });
  // bind the label itself
  $('.export_u_label').bind('focus', function(e) {
    if ($(this).attr('value') == 'label...') {
      $(this).attr('value', '');
      $(this).css('color', '#000000');
    }
  });
  $('.export_u_label').bind('blur', function(e) {
    if ($(this).attr('value') == '' || !$(this).attr('value')) {
      $(this).attr('value', 'label...');
      $(this).css('color', '#dddddd');
    }
  });
  $('.export_u_label').bind('keyup', function(e) {
    var val = $(this).attr('value');
    var val2 = check_label(val);
    if (val != val2)
      $(this).attr('value', val2);
  });
  
  $('.export_u_label').focus().blur();
}

function check_label(str) {
  if (str.match(/^[a-z\d\-_,]{1,250}$/i)) {
    return str;
  }
  var ret = '';
  for (var item in str) {
    if (str[item].match(/^[a-z\d\-_,]{1,250}$/i)) {
      ret += str[item];
      if (str[item] == ',')
        ret += ' ';
    }
  }
  return ret;
}