function clearForm(e, name) {

  form = e.form;
  el = e.elements[name];
  el.value = '';

}

function addItem(e, type, id, model, disabled, extras) {

  var input, target;

  var item = new Element(type, { 'class' : 'newly_chosen' } );

  for (var i = 0; i < extras.length; i++) {

    x = extras[i]; 

    input_container = new Element('span', { 'class' : 'float_right' } );

    item.appendChild(input_container);

    input = new Element('input', { 'type' : 'checkbox',
				   'value' : id,
				   'name' : 'data[' + model +  ']['+x+']['+id+']' } );
    input_container.appendChild(input);

  } 

  text_container = new Element('span');
  text_container.appendChild(document.createTextNode(e.innerHTML));
  if (extras) {
    text_container.onclick = function() {removeItem(this.parentNode,type,id,model,disabled,extras);};
  } else {
    item.onclick = function() {removeItem(this,type,id,model,disabled,extras);};
  }    
  item.appendChild(text_container);

  input = new Element('input', { 'type' : 'hidden',
				 'value' : id,
                                 'name' : 'data[' + model +  '][' + model + '][]' } );
  item.appendChild(input);

  target = 'chosen_' + model.toLowerCase();
  $(target).appendChild(item);
  e.onclick = '';
  e.className = disabled;

}
   
function removeItem(e, type, id, model, disabled, extras) {

  target = 'choice_item_' + model.toLowerCase() + '_' + id;
  $(target).className='newly_unchosen';
  $(target).onclick=function() {addItem(this,type,id,model,disabled,extras);};
  $(e).remove();

}



