// JavaScript Document

// This is a global variable whose value changes to reflect the column number that has been clicked.
var sort_col = 0;



//This function is used to numerically sort an array.
function sort_numeric(item1, item2){
	
	return item1[sort_col] - item2[sort_col];
  	/*if(item1[sort_col] < item2[sort_col]){
	  	return -1;
		//return item1-item2;
  	};
  	if(item1[sort_col] == item2[sort_col]){
	  	return 0;
		//return item1-item2;
  	};
	if(item1[sort_col] > item2[sort_col]){
	  	return 1;
		//return item1-item2;
  	};*/
	
  		}
		
//This function is used to alphabetically sort the array.		
function sort_alpha(item1,item2) {
	
	if (item1[sort_col].indexOf('<a') > -1) {
		var first_val = item1[sort_col].substring(item1[sort_col].indexOf('>')+1,item1[sort_col].indexOf('</a>'));
	//alert(first_val);
	}
	else {
		var first_val = item1[sort_col];
	}
	
	
	if (item2[sort_col].indexOf('<a') > -1) {
		var second_val = item2[sort_col].substring(item2[sort_col].indexOf('>')+1,item2[sort_col].indexOf('</a>'));

	}
	else {
		var second_val = item2[sort_col];
	}
	

	if(first_val < second_val){
	  	return -1;

  	};
  	if(first_val == second_val){
	  	return 0;

  	};
	if(first_val > second_val){
	  	return 1;

  	};


	/*if(item1[sort_col] < item2[sort_col]){
	  	return -1;

  	};
  	if(item1[sort_col] == item2[sort_col]){
	  	return 0;

  	};
	if(item1[sort_col] > item2[sort_col]){
	  	return 1;

  	};*/
}
	
//This does the sorting.

var last_table;
var last_column;
var direction = 0;

function reorder(table,col) {
	//The two variables that are passed on teh click are the table id and the column number within that table. The sort_col variable is global and records the column number that was clicked. The regex is used to test for numeric or alphabetical sorting.
	sort_col = col;
	var check_numeric = /^[0-9]*$/i;

//This variable grabs the array that corresponds to the current table. It uses the table variable that was passed on the onclick.
	var the_array = eval('table_' + table + '_array');
	
//This variable grabs the table object of the table to be sorted.
	var the_table = eval('table_' + table); 
		
//This tests for numeric or alpha sorting by looking at the first value in the array and running it through the numeric regex. If it's numeric, it goes to the sort_numeric function. If it's not numeric, it goes to the sort_alpha function.
	if (check_numeric.test(the_array[0][col])==true) {
		the_array.sort(sort_numeric);
		
		if (last_table == table && last_column == col && direction ==0) {
			the_array.reverse();
			direction = 1;
			}
		else {direction=0;};	
		
		}
	
	else {
		
		//the_array.sort(sort_alpha);
		
		if (last_table == table && last_column == col && direction == 0) {
			the_array.reverse();
			direction = 1;
		}
		else {the_array.sort(sort_alpha); direction = 0;}
		
				  
			
	}
	
//This variable grabs the tbody section of the table in question. The second one grabs all of the rows within that body.	


	var the_head = the_table.getElementsByTagName('thead');
	var column_heads = the_head[0].getElementsByTagName('th');
	div_up.style.display = 'none';
	div_down.style.display = 'none';
	
	if (direction ==0) {
		div_up.style.display = '';
		column_heads[col].appendChild(div_up);
		}
	else {
		div_down.style.display = '';
		column_heads[col].appendChild(div_down);
	}

	var the_body = the_table.getElementsByTagName('tbody');
	var the_rows = the_body[0].getElementsByTagName('tr');
//A loop is started based on the number of rows in the body. It grabs the cells in each row, then starts another loop that goes through those cells replacing contents with the shuffled contents of the array.
	for (l=0; l<the_rows.length; l++) {
		var the_cells = the_rows[l].getElementsByTagName('td');
		for (c=0; c<the_cells.length; c++) {
			the_cells[c].innerHTML = the_array[l][c];
		}//closes the cell thing
		
	}//closes the row thing
	
	last_table = table;
	last_column = col;
	//direction = 1;
	
}


/*Highlights the column if the column head text is hovered. It gets passed the table positon and the column that's being hovered on. It grabs the table, then the body, then the rows, then loops through the rows and grabs all the cells, changing the background of the cell in the selected column positon. */
function highlight(table,col) {
	var the_table = eval('table_' + table); 
	var the_body = the_table.getElementsByTagName('tbody');
	var the_rows = the_body[0].getElementsByTagName('tr');
	for (l=0; l<the_rows.length; l++) {
		var the_cells = the_rows[l].getElementsByTagName('td');
		//for (c=0; c<the_cells.length; c++) {
			//the_cells[c].style.backgroundColor = '';
		//}
			the_cells[col].style.backgroundColor = '#BCE774';
	}//closes the row thing
	
}

/*Works just like the one above, but the color is set to blank, letting any original formatting in the row take over.*/
function off_highlight(table,col) {

	var the_table = eval('table_' + table); 
	var the_body = the_table.getElementsByTagName('tbody');
	var the_rows = the_body[0].getElementsByTagName('tr');
	for (l=0; l<the_rows.length; l++) {
		var the_cells = the_rows[l].getElementsByTagName('td');
			the_cells[col].style.backgroundColor = '';
	}//closes the row thing
	
}
