var curr_sel_subcat = 0;
var curr_sel_subcat_name = '';


/**
* Shows a sub category for a specific top-level category
*
* @param int val The ID of the top-level category to show the sub-cats of
* @param string list_id The id of the node to load the list items into
**/
function show_sub_category (val, list_id) {
  var sub_select = document.getElementById (list_id);
  
  if (sub_select) {
    var option;
    
    // clear existing options
    var children = sub_select.childNodes;
    while (children.length > 0) {
      sub_select.removeChild (children.item (0));
    }
    
    // If there is nothing selected, do something quite clever
    if (val == '' || val == 0) {
      show_all_subs (list_id);
      curr_sel_subcat = 0;
      curr_sel_subcat_name = '';
      return;
    }
    
    sub_select.name = 'subcat';
    
    // Find if there are items
    var length = 0;
    for (var i in file_sub_cats[val]) {
      length++;
      break;
    }
    
    // If there are items, show them
    if (length > 0) {
      option = document.createElement ('option');
      option.appendChild (document.createTextNode ('-- Type --'));
      option.value = '';
      option.style.fontStyle = 'italic';
      sub_select.appendChild (option);
      
      option = document.createElement ('option');
      option.appendChild (document.createTextNode ('Show all'));
      option.value = '';
      sub_select.appendChild (option);
      
      // add sub categories
      for (var subcat_id in file_sub_cats[val]) {
        option = document.createElement ('option');
        option.value = subcat_id;
        if (subcat_id == curr_sel_subcat) {
          option.selected = true;
        }
        option.appendChild (document.createTextNode (file_sub_cats[val][subcat_id]));
        sub_select.appendChild (option);
      }
      
      
    // Otherwise just be blank
    } else {
      option = document.createElement ('option');
      option.appendChild (document.createTextNode ('N/A'));
      option.value = '';
      option.style.fontStyle = 'italic';
      sub_select.appendChild (option);
    }
  }
  
  curr_sel_subcat = 0;
  curr_sel_subcat_name = '';
}


/**
* This gets unique values for all sub categories
* It then offers these values in the select list
**/
function show_all_subs (list_id) {
  var sub_select = document.getElementById (list_id);
  
  sub_select.name = 'subcat_name';
  
  option = document.createElement ('option');
  option.appendChild (document.createTextNode ('-- Type --'));
  option.value = '';
  option.style.fontStyle = 'italic';
  sub_select.appendChild (option);
  
  option = document.createElement ('option');
  option.appendChild (document.createTextNode ('Show all'));
  option.value = '';
  sub_select.appendChild (option);
  
  for (var i = 0; i < distinct_sub_cats.length; i++) {
    var subcat = distinct_sub_cats[i];
    
    option = document.createElement ('option');
    option.value = subcat;
    if (subcat == curr_sel_subcat_name) {
      option.selected = true;
    }
    option.appendChild (document.createTextNode (subcat));
    sub_select.appendChild (option);
  }
}

