// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
//

var bots = [];
var cats = [];
function addThing(new_thing, thing_list, thing_check_prefix, thing_div) {
  toks = new_thing.split('_')
  var new_thing_id = toks[0];
  var new_thing_name = toks[1];
  if (thing_list.indexOf(new_thing_name) < 0)
    thing_list.push(new_thing_name);
   
  updateThing(thing_list, thing_div); 
  $(thing_check_prefix + '_' + new_thing_id).checked = true;
}
function delThing(new_thing, thing_list, thing_check_prefix, thing_div) {
  toks = new_thing.split('_')
  var new_thing_id = toks[0];
  var new_thing_name = toks[1];
  if (thing_list.indexOf(new_thing_name) < 0)
    return;
  thing_list.splice(thing_list.indexOf(new_thing_name), 1)
  
  updateThing(thing_list, thing_div);
  $(thing_check_prefix + '_' + new_thing_id).checked = false;
}
function updateThing(thing_list, thing_div) {
  if (thing_list.length > 0)
    $(thing_div).innerHTML = thing_list.join(", ");
  else
    $(thing_div).innerHTML = "Nothing selected.";
}
function addBot() {
  addThing($('sel_bots').value, bots, 'bot', 'bots');
}
function addCat() {
  addThing($('sel_cats').value, cats, 'category', 'cats');
}
function delBot() {
  delThing($('sel_bots').value, bots, 'bot', 'bots');
}
function delCat() {
  delThing($('sel_cats').value, cats, 'category', 'cats');
}
