function text_lb_add(listboxNode, itemId, orgClass, useClass, itemText, itemClick)
{
  var elemDIV = document.createElement('DIV');
  elemDIV.className = useClass;
  elemDIV.setAttribute('orgClass', orgClass);
  elemDIV.innerHTML = '<A id="' +itemId + '" class="' + (orgClass == useClass ? 'text_listtext_nrm' : 'text_listtext_sel') + '" href="' + itemClick + '">' + itemText + '</A>';
  listboxNode.appendChild(elemDIV);
}

function text_lb_clear(listboxNode)
{
  listboxNode.setAttribute('itemSelected', '');
  while (listboxNode.hasChildNodes())
  {
    listboxNode.removeChild(listboxNode.firstChild);
  }
}

function text_lb_sel(rowNode, newClass)
{
  var divNode = rowNode.parentNode;
  var listNode = divNode.parentNode;
  var id_selected = listNode.getAttribute('itemSelected');
  if (id_selected != null && id_selected != '')
  {
    var selectedNode = getHtmlElement(id_selected);
    selectedNode.className = "text_listtext_nrm";
    selectedNode = selectedNode.parentNode;
    selectedNode.className = selectedNode.getAttribute('orgClass');
  }
  rowNode.className = "text_listtext_sel";
  divNode.className = newClass;
  listNode.setAttribute('itemSelected', rowNode.id);
}

//

function text_fill_lb(listboxNode, categoryId, itemClick, selectId, selectClass)
{
  var count = 0;
	for (i = 0; i < category_arr.length; i = i + 4)
  {
		if (category_arr[i] == categoryId)
    {
      if (category_arr[i+1] > 0 || true)
      {
        var rowClass = (count++ % 2 ? 'text_listrow_1' : 'text_listrow_2');
        if (category_arr[i+1] == selectId)
        {
          text_lb_add(listboxNode, 'text_cat_' + category_arr[i+1], rowClass, selectClass, category_arr[i+2], "javascript:" + itemClick + "(" + category_arr[i+1] + ");");
          listboxNode.setAttribute('itemSelected', 'text_cat_' + category_arr[i+1]);
        }
        else
        {
          text_lb_add(listboxNode, 'text_cat_' + category_arr[i+1], rowClass, rowClass, category_arr[i+2], "javascript:" + itemClick + "(" + category_arr[i+1] + ");");
        }
      }
    }
    else if (category_arr[i] > categoryId)
    {
      break;
    }
  }
}

//

function text_show_desc(categoryId)
{
}

//

function text_build_lb1(selectId, selectClass)
{
  var listboxNode = getHtmlElement('text_lb1');
  text_lb_clear(listboxNode);
  text_build_lb2(0, 0, '');

  text_fill_lb(listboxNode, 0, 'text_onlb1', selectId, selectClass);
}


function text_build_lb2(categoryId, selectId, selectClass)
{
  var listboxNode = getHtmlElement('text_lb2');
  text_lb_clear(listboxNode);

  if (categoryId)
  {
    text_fill_lb(listboxNode, categoryId, 'text_onlb2', selectId, selectClass);
  }
}


function text_rebuild_lb(categoryId)
{
  var tree = new Array();

  // Get tree info
  first = true;
	for (i = category_arr.length-4; i >= 0; i = i - 4)
  {
		// Assure only the choosen category and its main category is pushed on the array.
		if (category_arr[i+1] == categoryId && (category_arr[i] == 0 || first))
    {
      tree.unshift(categoryId);
			categoryId = category_arr[i];
			first = false;
    }
  }
	
	if (tree.length == 0)
  {
		text_build_lb1();
    return;
  }

  // Rebuild listboxes
  var parentId = 0;
  var selectId = tree.shift();
  text_build_lb1(selectId, (selectId == -1 ? 'text_listsel_2' : 'text_listsel_1'));

	if (tree.length)
  {
    parentId = selectId;
    selectId = tree.shift();
    text_build_lb2(parentId, selectId, 'text_listsel_2');
  }

  text_show_desc(selectId);
}

//

function text_onlb1(category_id)
{
  var rowNode = getHtmlElement('text_cat_' + category_id);
  if (category_id == -1)
  {
    var listdivNode = getHtmlElement('text_question_list');
    listdivNode.innerHTML = '<br><span class="text_highlight"><b>Vänta, läser in artiklar...</b></span><br>';

    text_lb_sel(rowNode, 'text_listsel_2');
    text_show_desc(category_id);
    text_build_lb2(0);
    window.location = 'text_nav.php?category_id=' + category_id;
  }
  else if (category_id)
  {
    var listdivNode = getHtmlElement('text_question_list');
    listdivNode.innerHTML = '<br>';

    text_lb_sel(rowNode, 'text_listsel_1');
    text_show_desc(category_id);
    text_build_lb2(category_id);
  }
}


function text_onlb2(category_id)
{
  var rowNode = getHtmlElement('text_cat_' + category_id);
  if (category_id)
  {
    var listdivNode = getHtmlElement('text_question_list');
    listdivNode.innerHTML = '<br><span class="text_highlight"><b>Vänta, läser in artiklar...</b></span><br>';

    text_lb_sel(rowNode, 'text_listsel_2');
    text_show_desc(category_id);
    window.location = 'text_nav.php?category_id=' + category_id;
  }
}

function text_onlb3(text_id)
{
  var rowNode = getHtmlElement('text_text_' + text_id);
  if (text_id)
  {
    text_lb_sel(rowNode, 'text_listsel_3');
//    SetCookie('last_qa_id', qa_id);
    window.location = 'text.php?text_id=' + text_id;
  }
}
