/****************************************************************************
 * Author: Joe Reynoldson
 * Date: 12/1/05
 * Description: The following code enables dynamically adding or removing
 * organisms or groups of organisms as targets for a BLAST query.
 * *************************************************************************/

/******************************************************************************
 * Global Variables
 *****************************************************************************/

// initialize the CSS class for displaying rows in alternating colors
var toggle = "evenRow";

/** an array to keep track of the list of organisms selected for BLAST
 this array is necessary because Safari does not support tablerow object ids */
var currentSelection = new Array();

// the array of possible database names for building checkBoxes and labels
var blast_db_names = new Array( "EST", "cDNA","HTC","TSA","EST_Contig", "GSS", "STS", "HTG", 
			        "other_DNA", "Protein");

// "all plants" associative array to add all plants to the blast db table
var plants = new Array();
plants["name"] = "All";
for (i in blast_db_names ){
	plants[blast_db_names[i]] = 1;
}

// "all grasses" associative array to add all grasses to the blast db table
var grasses = new Array();
grasses["name"] = "All Grass";
for (i in blast_db_names ){
	grasses[blast_db_names[i]] = 1;
}

// "all monocots" associative array to add all monocots to the blast db table
var monocots = new Array();
monocots["name"] = "All Monocots";
for (i in blast_db_names ){
	monocots[blast_db_names[i]] = 1;
}

// "all dicots" associative array to add all dicots to the blast db table
var dicots = new Array();
dicots["name"] = "All Dicots";
for (i in blast_db_names ){
	dicots[blast_db_names[i]] = 1;
}

// "all coniferales" associative array to add all coniferales to the blast db table
var coniferales = new Array();
coniferales["name"] = "All Conifers";
for (i in blast_db_names ){
        coniferales[blast_db_names[i]] = 1;
}
/******************************************************************************
 * Row Update Methods
 *****************************************************************************/

// adds a row to a table if a row with that ID hasn't already been added
function addRow( tableID,org, whichSelect)
{
	/* hack, by mjsabby */
	if (whichSelect == null)
		whichSelect = "";
	if (whichSelect == "PUT")
		whichSelect = "EST_Contig";
	if (whichSelect == "PLN")
		whichSelect = "Other_DNA";
	//if (whichSelect == "EST" || whichSelect == "HTC" || whichSelect == "TSA" || whichSelect == "cDNA")
	//	whichSelect = "Transcript";

	// get the table object, then get the object representing its body
	var tBodyObj = document.getElementById(tableID).tBodies[0];

	//check to see if the item is already in the list
	for(i in currentSelection){
		if(org["name"] == currentSelection[i]) return;
	}

	var delButton = "<button onclick=\"return delRow('" + tableID + "',this.parentNode.parentNode.sectionRowIndex)\">remove</button>";

	// add a row to the body
	var rowObj = tBodyObj.insertRow(tBodyObj.rows.length);
	rowObj.id = org["name"];
	rowObj.className = toggle;

	// update toggle for row color alternation
	if(toggle == "evenRow") toggle = "oddRow";
	else toggle = "evenRow";

	//add the item to the list
	currentSelection.splice(currentSelection.length,0,org["name"]);

	// populate the first column (organism name)
	var cellObj = rowObj.insertCell(rowObj.cells.length);
	cellObj.innerHTML = org["name"] + " " + delButton;
	cellObj.style.width = "40%";

        //add the "Check All" checkbox
        var check_box = document.createElement("input");
        check_box.type = "button";
        check_box.value = "Select All";
        check_box.onclick = selectAll;
        var e = cellObj.appendChild(check_box);

	// add check_boxes for each blast DB for this organism
	// disable check_boxes for which this organism does not have support
	for (i in blast_db_names){
		var db_name = blast_db_names[i];
		var check_box = document.createElement("input");
		check_box.type = "checkbox";
		check_box.name = "db";
		check_box.value = org["name"] + " " + db_name;
		if (blast_db_names[i].toLowerCase() == whichSelect.toLowerCase()) {
			check_box.checked = "checked";
		}
		if(!org[db_name])
		{
			check_box.disabled = true;
			check_box.style.visibility = "hidden";
		}
		cellObj = rowObj.insertCell(rowObj.cells.length);
		var e = cellObj.appendChild(check_box);
		cellObj.style.width = "40px";
		cellObj.style.textAlign = "center";
	}

	// add the "Check All" checkbox
	//var cellObj = rowObj.insertCell(rowObj.cells.length);
	//var check_box = document.createElement("input");
	//check_box.type = "checkbox";
	//check_box.name = org["name"];
	//check_box.onclick = selectAll;
	//var e = cellObj.appendChild(check_box);
	//cellObj.style.width = "35px";
	//cellObj.style.textAlign = "center";
}

function selectAll()
{
	var checkbox;
	var row = this.parentNode.parentNode;
	var cells = row.getElementsByTagName("td");
	for (var i = 1; i < cells.length-1; i++)
	{
		checkbox = cells[i].getElementsByTagName("input");
		if (checkbox.length != 1) continue;
		checkbox[0].checked = true;
	}
}

// removes a single row from the body of a table
function delRow( tableID, rowID )
{
	// delete the row from the table body
	document.getElementById(tableID).tBodies[0].deleteRow(rowID);

	// delete the organism from the array of selected organisms
	currentSelection.splice(rowID,1);

	// update the classnames of the subsequent table rows
	updateRowColors(tableID,rowID);

	return false;
}

// removes all rows from the body of a table
function delAllRows(tableID)
{
	tBodyObj = document.getElementById(tableID).tBodies[0];
	while(tBodyObj.rows.length > 0){
		tBodyObj.deleteRow(0);
	}
}

// updates the classnames of all rows subsequent to a row deletion
function updateRowColors( tableID, rowID)
{
	// get the body of this table
	var tBodyObj = document.getElementById(tableID).tBodies[0];

	// update all row classnames beginning from this rowID
	for(i=rowID;i<tBodyObj.rows.length;i++){
		var rowObj = tBodyObj.rows[i];
		if(rowObj.className == "evenRow") rowObj.className = "oddRow";
		else rowObj.className = "evenRow";
	}

	// update row classname toggle
	if(toggle == "evenRow") toggle = "oddRow";
	else toggle = "evenRow";
}

