function addCategory() {
  var categoryId = $('#typeCategoryId').val();
  var category = ddMatchStr;
  var categorySelections = $('#categorySelections').val();
  var newHTML;

  if (ddMatchStr !== "") {
    newHTML = 
      '<span id="typeCategorySelection_' + ddMatchId + '"> ' + 
        ddMatchStr + " " + 
        '<a class="del" ' +
          "onclick=\"removeId('types','t','catStr',"+(ddMatchId) + ");\">"+
          '[-]' +
        '</a>' + 
      '</span>';
    $('#categoryContainer').append(newHTML);
    categorySelections += getValueFromId(ddMatchId) + ';';
    $('#categorySelections').val(categorySelections);
    $('#typeCategorySearch').val('');
  }
}

function setBorrowedNutrition() {
  document.getElementById('borrowedNutInput').value = ddMatchStr;
  document.getElementById('borrowedNutId').value = ddMatchId;
}



function updateNutritionView() {
  // retrieve the radio buttons for inspection
  var calcChildTypes = document.getElementById('calcChildTypes');
  var childItems = document.getElementById("childItems");
  var borrowedNutri = document.getElementById('borrowedNutri');
  var enteredNut = document.getElementById('enteredNut');
  
  // the various elements that will either be shown or hidden
  var childrenTypeNutDiv = document.getElementById('childrenTypeNutDiv');
  var borrowedNutDiv = document.getElementById("borrowedNutDiv");
  
  // check all of the radio buttons to see which one is selected
  if ((calcChildTypes !== null) && (calcChildTypes.checked)) {
    // hide the borrowed nutrition chooser
    borrowedNutDiv.style.display = "none";

    // if it is being hidden, show the child category multilist
    if (childrenTypeNutDiv !== null) { childrenTypeNutDiv.style.display = "block"; }
  }
  else if ((borrowedNutri !== null) && (borrowedNutri.checked)) {
  	// hide the child category multilist
    if (childrenTypeNutDiv !== null) { childrenTypeNutDiv.style.display = "none"; } 
    
    // show the borrowed nutrition chooser
    borrowedNutDiv.style.display = "block";
  } 
  else if (((enteredNut !== null) && (enteredNut.checked)) ||
           ((childItems !== null) && (childItems.checked))) {
  	// hide the child category multilist
    if (childrenTypeNutDiv !== null) { childrenTypeNutDiv.style.display = "none"; } 
    
    // hide the borrowed nutrition chooser
    borrowedNutDiv.style.display = "none";
  }
}

function convertToAlias(id, aliases) {
  var msg = "This action will make this food type into an alias." +
            " If there are multiple parent types listed, the first " + 
            "one will be the aliased type.\n\n" +
            " Since aliases cannot contain food items, "+ 
            "any items in this type will be moved to the aliased type. " +
            " Are you SURE that you want to do this?"; 
  var answer = confirm(msg);
  if (answer) { 
    var url = "/type/convertToAlias.php?id="+id+"&aliases="+aliases;
    window.location = url;
  }
}



function addFoodType(name, groupJustStarted) {
  $.ajax({
    url : "../type/add?q="+encodeURIComponent(name),
    success: function(data) {
	  var typeId = data;	
  	  if (typeId != -1) {
  		  addIngredText(name, 0, groupJustStarted);  
	      document.getElementById('ingredStr').value += ";i" + typeId;
	      document.getElementById('ingredInput').value = "";
	  }
    }
  });
}

