
function class_css(a,o,c1,c2)
{
  switch (a){
    case 'check':
      return new RegExp('\\b'+c1+'\\b').test(o.className)
    break;
    case 'swap':
      o.className=!class_css('check',o,c1)?o.className.replace(c2,c1):o.className.replace(c1,c2);
    break;
    case 'add':
      if(!class_css('check',o,c1)){o.className+=o.className?' '+c1:c1;}
    break;
    case 'remove':
      var rep=o.className.match(' '+c1)?' '+c1:c1;
      o.className=o.className.replace(rep,'');
    break;
  }
}



function toggle_class(itemname, itemtype)
{

    var tmp = document.getElementsByTagName(itemtype);
    var my_len = tmp.length;
    for (var i=0;i<my_len;i++)
    {
        if (class_css('check', tmp[i], itemname)) 
	{
		tmp[i].style.display = (tmp[i].style.display == 'none') ? '' : 'none';
	}
    }
    
}

function toggle_column(col_no, do_show) 
{
	var stl;
	if (do_show) 
	{
		stl = '';
	} else
	{
		stl = 'none';
	}

	var tbl  = document.getElementById('id_of_table');
	var rows = tbl.getElementsByTagName('tr');
	var my_len = rows.length;
	for (var row=0; row<my_len;row++) 
	{
		var cels = rows[row].getElementsByTagName('td')
		cels[col_no].style.display=stl;
	}
}

function show( id )
{
	document.getElementById(id).style.display = 'block';
}

function hide( id )
{
	document.getElementById(id).style.display = 'none';
} 

function toggle_visibility(id) 
{
	var e = document.getElementById(id);
	if(e.style.display == 'block')
	{
		e.style.display = 'none';
	}else if(e.style.display == 'none')
	{
		e.style.display = 'block';
	}else
	{
		e.style.display = 'block';
	}
}

function toggle_class_name(this_object, class_name)
{			
	// First check if this class_type exists in the class
	if(this_object)
	{
		if(new RegExp('\\b'+class_name+'\\b').test(this_object.className) )
		{
			// Remove it
			var rep = this_object.className.match(' '+class_name)?' '+class_name:class_name;
			this_object.className = this_object.className.replace(rep, '');
		} else
		{
			// Add it
			this_object.className += this_object.className?' ' +class_name:class_name;
		}
	}


}

function toggle_class_group(group_id, class_type)
{
	var group_obj = document.getElementById(group_id);
	var my_group_len = group_obj.rows.length;
	for(var x=0;x < my_group_len; x++)
	{
		var this_row = group_obj.rows[x];
		var my_cell_len = this_row.cells.length;
		for(var y=0; y < my_cell_len; y++)
		{
			var this_cell = this_row.cells[y];
			toggle_class_name(this_cell, class_type);
			// TODO: If there is a table inside this cell, call this on all the table groups also
		}
	}
}


var NS4 = (navigator.appName == "Netscape" && parseInt(navigator.appVersion) < 5);

function addOption(theSel, theText, theValue)
{
  var newOpt = new Option(theText, theValue);
  var selLength = theSel.length;
  theSel.options[selLength] = newOpt;
}

function deleteOption(theSel, theIndex)
{ 
  var selLength = theSel.length;
  if(selLength>0)
  {
    theSel.options[theIndex] = null;
  }
}
function deleteSelectedOption(theSel)
{

  var selLength = theSel.length;
  var selectedText = new Array();
  var selectedValues = new Array();
  var selectedCount = 0;
  
  var i;
  
  // Find the selected Options in reverse order
  // and delete them from the 'from' Select.
  for(i=selLength-1; i>=0; i--)
  {
    if(theSel.options[i].selected)
    {
      deleteOption(theSel, i);
      selectedCount++;
    }
  }
  
}
function copyOptions(theSelFrom, theSelTo)
{
  
  var selLength = theSelFrom.length;
  var selectedText = new Array();
  var selectedValues = new Array();
  var selectedCount = 0;
  
  var i;
  
  // Find the selected Options in reverse order
  // and delete them from the 'from' Select.
  for(i=selLength-1; i>=0; i--)
  {
    if(theSelFrom.options[i].selected)
    {
      selectedText[selectedCount] = theSelFrom.options[i].text;
      selectedValues[selectedCount] = theSelFrom.options[i].value;
      selectedCount++;
    }
  }
  
  // Add the selected text/values in reverse order.
  // This will add the Options to the 'to' Select
  // in the same order as they were in the 'from' Select.
  for(i=selectedCount-1; i>=0; i--)
  {
    addOption(theSelTo, selectedText[i], selectedValues[i]);
  }
  
  if(NS4) history.go(0);
}

function moveOptions(theSelFrom, theSelTo)
{
  
  var selLength = theSelFrom.length;
  var selectedText = new Array();
  var selectedValues = new Array();
  var selectedCount = 0;
  
  var i;
  
  // Find the selected Options in reverse order
  // and delete them from the 'from' Select.
  for(i=selLength-1; i>=0; i--)
  {
    if(theSelFrom.options[i].selected)
    {
      selectedText[selectedCount] = theSelFrom.options[i].text;
      selectedValues[selectedCount] = theSelFrom.options[i].value;
      deleteOption(theSelFrom, i);
      selectedCount++;
    }
  }
  
  // Add the selected text/values in reverse order.
  // This will add the Options to the 'to' Select
  // in the same order as they were in the 'from' Select.
  for(i=selectedCount-1; i>=0; i--)
  {
    addOption(theSelTo, selectedText[i], selectedValues[i]);
  }
  
  if(NS4) history.go(0);
}

function gatherOptions(delim)
{
  var selObj = document.getElementById('sel1');
  var hideObj = document.getElementById('hide1');
  for (var i=0; i<selObj.options.length; i++) {
    hideObj.value = hideObj.value ==
      '' ? selObj.options[i].value : hideObj.value + delim + selObj.options[i].value;
  }
}
function gatherOptions2(allOptions)
{
  var SelLists = allOptions.split(";");
  var thisList;
  for (var a=0, thisList; thisList=SelLists[a]; a++)
  {
    var selObj = document.getElementById(thisList);
    for (var i=0; i<selObj.options.length; i++) {
      selObj.options[i].selected = true;
    }
  }
}

// log (message)
// Logs a message. Every second, all logged messages are displayed
// in an alert box. This saves you from having to hit Return a ton
// of times as your script executes.
//
// inspect (object)
// Logs the interesting properties an object possesses. Skips functions
// and anything in CAPS_AND_UNDERSCORES.
//
// inspectValues (object)
// Like inspect(), but displays values for the properties. The output
// for this can get very large -- for example, if you are inspecting
// a DOM element.


function log (message)
{
	if (! _log_timeout)
		_log_timeout = window.setTimeout(dump_log, 1000);
	
	_log_messages.push(message);


	function dump_log()
	{
		var message = '';
		
		for (var i = 0; i < _log_messages.length; i++)
			message += _log_messages[i] + '\n';
				
		alert(message);
		
		_log_timeout = null;
		delete _log_messages;
		_log_messages = new Array();
	}
}

function inspect (obj)
{
	var message = 'Object possesses these properties:\n';
	
	if (obj)
	{
		for (var i in obj)
		{
			if ((obj[i] instanceof Function) || (obj[i] == null) ||
					(i.toUpperCase() == i))
				continue;
			
			message += i + ', ';
		}
		
		message = message.substr(0, message.length - 2);
	}
	else
		message = 'Object is null';
	
	log(message);
}

function inspectValues (obj)
{
	var message = '';
	
	if (obj)
		for (var i in obj)
		{
			if ((obj[i] instanceof Function) || (obj[i] == null) ||
					(i.toUpperCase() == i))
				continue;
			
			message += i + ': ' + obj[i] + '\n';
		}
	else
		message = 'Object is null';
	
	log(message);
}

var _log_timeout;
var _log_messages = new Array();

function deleteRows(rowObjArray)
{
	if (1) {
		for (var i=0; i<rowObjArray.length; i++) {
			var rIndex = rowObjArray[i].sectionRowIndex;
			rowObjArray[i].parentNode.deleteRow(rIndex);
		}
	}
}

function deleteCurrentRow(obj)
{
	if (1) {
		var delRow = obj.parentNode.parentNode;
		var tbl = delRow.parentNode.parentNode;
		var rIndex = delRow.sectionRowIndex;
		var rowArray = new Array(delRow);
		deleteRows(rowArray);
	}
}

function showpwminfo (theSelFrom)
{

  var selLength = theSelFrom.length;
  var selectedText = new Array();
  var selectedValues = new Array();
  var selectedCount = 0;

  var i;

  // Find the selected Options in reverse order
  // and delete them from the 'from' Select.
  for(i=selLength-1; i>=0; i--)
  {
    if(theSelFrom.options[i].selected)
    {
	window.open('?page=show_alignment&amp;matrix_id=' + theSelFrom.options[i].value,'PWM-' + theSelFrom.options[i].text,'width=700,height=500,scrollbars=yes,toolbar=no,location=no,status=no,menubar=no,directories=no');
      	selectedCount++;
    }
  }

}

function filter_genome_list_alignment(pwm_select, genome_select)
{
	var pwmLen = pwm_select.length;
	var genome_len = genome_select.length;

	// Search through each pwm and get the taxon list it will work for
	// For genomes it does not work with, hide the entry
	for (var i = 0; i < pwmLen;i++)
	{
	}
	
}

function filterList(theSelList, filterValue)
{
	var filterValueLen = filterValue.length;
	var MatchRegEx;

	var selId = theSelList.id;
	var oldlist_id = "oldlist_##" + selId;

	if( document[oldlist_id] == null)
	{
		var oldList = theSelList.cloneNode(true);
		document[oldlist_id] = oldList;
	} else
	{
		// First empty the list
		var selLength = theSelList.length;
		var newLength = document[oldlist_id].length;

		for(var i=selLength-1;i >=0;i--)
		{
			theSelList.options[i] = null;
		}

		for(var i = 0;i < newLength;i++)
		{
			var newOpt = document[oldlist_id].options[i].cloneNode(true);
			theSelList.appendChild(newOpt);
			
		}
	}

	if(filterValueLen < 3 )
	{
		return 0;
//		var MatchRegEx = new RegExp(".*");
	} else
	{
		var MatchRegEx = new RegExp(".*" + filterValue + ".*", "i");
	}
	

	// Create the regular expression

	var selLength = theSelList.length;
	

	// Find the selected options and hide them (or don't delete them because IE doesn't do the hide stuff)
	for(var i=selLength-1;i >=0;i--)
	{
		if(theSelList.options[i].text.match(MatchRegEx) )
		{
		//	theSelList.options[i].style.display = "block";
		//	theSelList.options[i].visibility = "visible";
		} else
		{
			// theSelList.options[i].style.display = "none";
			theSelList.options[i] = null;
		
		}
	}
	
}


function sortCartOptionsFunction (option1, option2)
{
	var a_array = new Array();
	var b_array = new Array();
	a_array = option1.text.split('|');
	b_array = option2.text.split('|');

	if(a_array[1] < b_array[1])
	{
		return -1;
	} else if(a_array[1] > b_array[1])
	{
		return 1;
	} else
	{
		//  they are equal by organism
		if(a_array[0] < b_array[0])
		{
			return -1;
		} else if(a_array[0] > b_array[0])
		{
			return 1;
		}
		return 0;
	}

	return 0;
}

function compareText (option1, option2) {
  return option1.text < option2.text ? -1 :
    option1.text > option2.text ? 1 : 0;
}
function compareValue (option1, option2) {
  return option1.value < option2.value ? -1 :
    option1.value > option2.value ? 1 : 0;
}
function compareTextAsFloat (option1, option2) {
  var value1 = parseFloat(option1.text);
  var value2 = parseFloat(option2.text);
  return value1 < value2 ? -1 :
    value1 > value2 ? 1 : 0;
}
function compareValueAsFloat (option1, option2) {
  var value1 = parseFloat(option1.value);
  var value2 = parseFloat(option2.value);
  return value1 < value2 ? -1 :
    value1 > value2 ? 1 : 0;
}

function sortSelect (select, compareFunction) {
  if (!compareFunction)
  {
    compareFunction = compareText;
  }
  var options = new Array (select.options.length);
  for (var i = 0; i < options.length; i++)
    options[i] = 
      new Option (
        select.options[i].text,
        select.options[i].value,
        select.options[i].defaultSelected,
        select.options[i].selected
      );
  options.sort(compareFunction);
  select.options.length = 0;
  for (var i = 0; i < options.length; i++)
    select.options[i] = options[i];
}

