//////////////////////////////////////////////////////////////////////////
//// geobacter common js functions/classes
//// Copyright (C) 2006 University of Massachusetts
//// pb 2006/02/06
//////////////////////////////////////////////////////////////////////////
//
// most of this should be replaced by jQuery
//
function safeArray(itemarray)
{
  if (itemarray == null)
  {
    return new Array();
  }
  else if (typeof(itemarray)=='object' && itemarray instanceof Array)
  {
    return itemarray;
  }
  else
  {
    // 'itemarray' is just a singleton...create real array and return it
    var iarray = new Array();
    iarray.push(itemarray);
    return iarray;
  }
}

function selectListItemsObj(sellist, selItems)
{
  if (sellist.options == null)
  {
    return;
  }
  for (i = 0; i < sellist.options.length; i++)
  {
    for (j = 0; j < selItems.length; j++)
    {
      if (sellist.options[i].value == selItems[j])
      {
        sellist.options[i].selected = true;
        break;
      }
      sellist.options[i].selected = false;
    }
  }
}

function selectListItems(id, selItems)
{
  var sellist = document.getElementById(id);
  selectListItemsObj(sellist, selItems);
}

function selectListItem(id, selItem)
{
  var selItems = safeArray(selItem);
  selectListItems(id, selItems);  
}

function rewriteDropdownItemsListObj(sellist, itemarray, sel)
{
  var iarray = safeArray(itemarray);
  if (sellist == null)
  {
    return;
  }
  var selItems = safeArray(sel);
  if (sellist.options != null)
  {
    for (i = sellist.options.length - 1; i >= 0; i--)
    {
      if (sel == null && sellist.options[i].selected)
      {
        selItems.push(sellist.options[i].value);
      }
      sellist.options[i] = null; 
    }
  }
  for (i = 0; i < iarray.length; i++)
  {
    sellist.options[i] = new Option(iarray[i].text);
    sellist.options[i].value = iarray[i].value;
  }
  selectListItemsObj(sellist, selItems);
}


function rewriteDropdownItems(id, itemarray, sel)
{
  var sellist = document.getElementById(id);
  rewriteDropdownItemsListObj(sellist, itemarray, sel);
}

function newTD(str)
{
  var theTd = document.createElement("td");
  if (str.length > 0)
  {
    theTd.appendChild(document.createTextNode(str));
  }
  return theTd;
}

function newTDLink(str, href)
{
  var theTd = document.createElement("td");
  if (str.length > 0)
  {
    var link = document.createElement("a");
    link.href = href;
    link.appendChild(document.createTextNode(str));
    theTd.appendChild(link);
  }
  return theTd;
}

function doExpandContract(btn, itemarray, listname, expandText, contractText)
{
  if (btn.value==expandText)
  {
    btn.value=contractText;
    rewriteDropdownItems(listname, itemarray);
  }
  else
  {
    btn.value=expandText;
    rewriteDropdownItems(listname, itemarray, 0);
  }
  return false;
}

function fold(str, width)
{
  var s ='';
  for (var i = 0; i < str.length; i+=width)
  {
    s+=str.substring(i, i+width);
    s+="\r\n";
  }
  return s;
}

function checkAll(isChecked, theName)
{
  var cbxs = document.getElementsByName(theName);
  for (var i = 0; i < cbxs.length; i++)
  {
    cbxs[i].checked = isChecked;
  }
}
