function toggleFilter(id) {
	var x;
	x = document.getElementById(id);
	if(x.style.display == "none") {
		x.style.display = "block";
	}
	else {
		x.style.display = "none";
	}
}

function hideRows(id, cols) {
	cols = cols.split(',');
	table = document.getElementById(id);

	rows = table.getElementsByTagName("tr");
	numRows = rows.length;

	filters = rows[0].getElementsByTagName("td");
	numCols = filters.length;

	checkList = new Array(filters.length);

	for(c in cols) {
		checkList[cols[c]] = new RegExp(filters[cols[c]].getElementsByTagName("input")[0].value, "i");
	}
	
	for(r=2; r < numRows; r++) {
		for(c in cols) {
			if(!checkList[cols[c]].test(getLinkText(rows[r].getElementsByTagName("td")[cols[c]]))) {
				rows[r].style.display = "none";
				break;
			}
			//if(c == numCols - 1) {
			//	rows[r].style.display = "table-row";
			//}
		}
	}
}

function getLinkText(td) {
	links = td.getElementsByTagName("a");
	if(links.length > 0) {
		return links[0].innerHTML;
	}
	else {
		return td.innerHTML
	}
}

function hideRowsEnter(event, id, cols) {
	if(event.keyCode == 13) {
		hideRows(id, cols);
		return false;
	}
	return true;
}

function resetRows(id) {
	table = document.getElementById(id);

	rows = table.getElementsByTagName("tr");
	numRows = rows.length;

	for(r=2; r < numRows; r++) {
		rows[r].style.display = "table-row";
	}
}

function listNumber(a, b) {

}

function compareNumber(a, b) {
	return a > (a - b);
}

function listDate(a, b) {
	
}

function compareDate(a, b) {
	
}


// the maximum number of columns in your list, this should be an arg in sortList?
var maxCol = 5;
var col = 0; // column to be sorted, should be an array with multicolumns?, shouldnt be hard to make work in the compare method 
var toggle = true; // ascending/descending


function listText(a, b) {
	var val = compareText(a.getElementsByTagName("td")[col].innerHTML, b.getElementsByTagName("td")[col].innerHTML);
	//if val == 0, do comparisons with other rows
	return val;
}

function compareText(a, b) {
	if((toggle && a < b) || (!toggle && b < a)) return 1;
	else if(a == b) return 0;
	else return -1;
}



//the sort function 'c' = col, to remove variable ambiguity
function sortList(id, c, compare) {
	//gay DOM non-array
	var rows2 = document.getElementById(id).getElementsByTagName("tr");
	col = c;
	
	//convert it to a normal array w/o first 2 rows
	var rows = [];
	for(var i = 0; i < rows2.length - 2; i++) {
		rows[i] = rows2[i+2];
	}
	
	sort(rows, compare, swapListValues, 0,rows.length);
	
	toggle = !toggle;
}

function swapListValues(a, b) {
	//swaps all columns,  should be an easier way to do this, puts the sort efficiency to hell,
	//more efficient to preprocess this, so its one row object swap vs maxCol # of td swaps.
	//right now this is iterated maxCols * n * log(n), preprocessing its maxCols * n + n * log(n) = n(maxCols + log(n))
	
	//also, since this needs to be preprocessed, the array conversion I do right now in sortList can serve
	//a double purpose thus not needing offset in sort. (for now)
	for(var i = 0; i < maxCol; i++) {
		temp = a.getElementsByTagName("td")[i].innerHTML;
		a.getElementsByTagName("td")[i].innerHTML = b.getElementsByTagName("td")[i].innerHTML;
		b.getElementsByTagName("td")[i].innerHTML = temp;
	}
}


//start partially works, size works fine as long as start = 0
function sort(heap,compare,swap,start,size) {

	for (var v = parseInt(size/2)-1+start; v>=start; v--)
    	downheap(heap,v,size+start,compare,swap);

    for(var count = size - 1; count >= 0; count--) {
		swap(heap[0+start], heap[count+start]);
        downheap(heap,0+start,count+start,compare,swap);
    }
}

function downheap(heap,v,bottom,compare,swap) {
	var w = 2*v + 1;    // first descendant of v
	while(w < bottom) {
		
		if (w + 1 < bottom) {    // is there a second descendant?
        	if (compare(heap[w+1],heap[w]) == 1) {
        		w++;
        	}
		}

		// w is the descendant of v with maximum label
        if (compare(heap[v],heap[w]) >= 0)
        	return;  // v has heap property
            	
        // otherwise
        swap(heap[v], heap[w]);  // exchange labels of v and w
        v=w;        // continue
     	w=2*v+1;

	}
}