var sSelectedClassName = 'selected';

function s(row) {
	HighlightRow(row);
	if (window.clickRow)
		window.clickRow(row);
}

function d(row) {
	if (window.selectRow)
		window.selectRow(row);
}

function ss(row, e) {
	if (!e) e = window.event;
	MultipleHighlight(row, e);
}
// Used in grid paging, uses HTTP requests to ask for *just* the grid, to improve performancefunction grid_move(node, uri) {	var grid = node;
	while (!hasClass(grid, 'grid')) grid = grid.parentNode;	if (!uri) uri = node.href;	var sNoHeader = '&noheader=true';
	if (uri.indexOf(sNoHeader) == -1)
		uri += sNoHeader;	HTTPGet(uri, grid_move_receive, grid);	if (window.on_page)		window.on_page();
	return false;}
// Callback function for the above
function grid_move_receive(html, http, grid) {	if (http.status != 200) return alert('Error loading next page in grid.');	if (grid.parentNode.className == 'dummy') grid = grid.parentNode;
	var dummy = document.createElement('div');
	dummy.className = 'dummy';	grid.parentNode.insertBefore(dummy, grid);
	dummy.innerHTML = html;	grid.parentNode.removeChild(grid);	gridwrap_auto_attach();
}
// OOP grid handler, used to get information about clsGridsfunction JSGrid(id) {
	this.id = id;	this.node = document.getElementById(id);	var tables = this.node.getElementsByTagName('table');	this.scrolling = (tables.length > 1);	this.inputs = this.node.getElementsByTagName('input');
	this.multiple = (this.inputs[0] && this.inputs[0].type == 'checkbox') ? true : false;	this.thead = (this.scrolling) ? tables[1] : this.node.getElementsByTagName('thead')[0];
	this.tbody = (this.scrolling) ? tables[2] : this.node.getElementsByTagName('tbody')[0];	this.headers = this.thead.getElementsByTagName('th');	this.selectedClassName = 'selected';
}// Returns an array of the selected rowsJSGrid.prototype.selectedRows = function () {	var rows = this.tbody.getElementsByTagName('tr');
	if (!rows) return [];
	var selectedRows = [];
	for (var i in rows)
		if (hasClass(rows[i], this.selectedClassName)) {
			selectedRows.push(rows[i]);
			if (!this.multiple) break;		}
	return selectedRows;}
// Returns the first selected rowJSGrid.prototype.selectedRow = function() {
	return this.selectedRows()[0];}
// Returns the value of a single cell
JSGrid.prototype.cellValueFromRow = function(column, row) {
	if (!row) return false;
	var cells = row.getElementsByTagName('td');
	if (cells.length == 0)
		return false;
	if (typeof column == 'number') {
		column = (this.multiple) ? column + 1 : column;
		return cells[column].innerHTML;
	}
	var objData = {};
	for (i = 0; cells[i] && this.headers[i]; i++) {
		var header = this.headers[i];
		if (typeof column == 'string') {
			if (header.innerHTML == column)
				return cells[i].innerHTML;
		} else {
			if (!hasClass(header, 'checkbox') && header.innerHTML != 'Actions') {
				objData[header.innerHTML] = cells[i].innerHTML;
			}
		}
	}

	if (typeof column == 'string')
		return false;

	return objData;
}
// Returns the value of a single cell on the current rowJSGrid.prototype.cellValue = function(column) {	var row = this.selectedRow();
	return this.cellValueFromRow(column, row);}
// Returns a JS object of information about the entire row (or one column)function RowContents(node, column) {	if (!node || node.tagName.toLowerCase() != 'a')
		return false;	var grid = node;	while (!hasClass(grid, 'grid')) grid = grid.parentNode;	grid = new JSGrid(grid.id);
	return grid.cellValueFromRow(column, node.parentNode.parentNode);}

var HighlightCache = false;

function HighlightRow(row) {
	if (HighlightCache.oldrow) {
		HighlightCache.oldrow.className = HighlightCache.oldclass;
	}
	HighlightCache = { oldrow: row, oldclass: row.className };
	row.className = row.className + ' selected';
}

function HighlightedRow() {
	if (HighlightCache.row)
		return HighlightCache.row;
	else
		return false;
}

function MultipleHighlight(row, event) {
	if (!HighlightCache) HighlightCache = [];

	var className = sSelectedClassName;

	var checkbox = row.getElementsByTagName('input')[0];
	if (hasClass(row, className)) {
		removeClass(row, className);
		checkbox.checked = false;
	} else {
		addClass(row, className);
		checkbox.checked = true;
	}
}

function SwitchCheckboxes(node) {
	var thead = node;
	while (thead.tagName.toLowerCase() != 'thead')
		thead = thead.parentNode;

	var tbody = thead.nextSibling;
	var rows = tbody.getElementsByTagName('tr');
	for (var i in rows) {
		var row = rows[i];
		if (rows && row.childNodes) {
			row.getElementsByTagName('input')[0].checked = node.checked;
			if (node.checked)
				addClass(row, sSelectedClassName);
			else
				removeClass(row, sSelectedClassName);
		}
	}
}
function grid_wrap(node) {	var grid = grab(node.id.substr(8));
	var className = 'nowrap';	if (node.checked)
		removeClass(grid, className);	else
		addClass(grid, className);
}
if (window.addLoadEvent)	addLoadEvent(gridwrap_auto_attach);else	window.onload = gridwrap_auto_attach;

function gridwrap_auto_attach() {
	if (navigator.userAgent.indexOf('MSIE') == -1) return;
	var spans = document.getElementsByTagName('span');
	for (i=0;spans[i];i++)
		if (hasClass(spans[i], 'wrap'))
			gridwrap_attach(spans[i]);
}

function gridwrap_attach(node) {
	var id = node.id;
	node.id = '';
	var checkbox = document.createElement('input');
	var text = document.createTextNode(' Wrap');
	var label = document.createElement('label');
	checkbox.type = 'checkbox';
	checkbox.id = id;
	checkbox.onclick = function () { grid_wrap(this); };
	label.setAttribute('for', id);
	label.appendChild(checkbox);
	label.appendChild(text);
	node.appendChild(label);
	checkbox.checked = hasClass(node, 'checked');
}