//Add the Debug script.
document.writeln('<scr' + 'ipt type="text/javascript" src="/Include/JS/Debug.js"></scr' + 'ipt>');


/**************************************************************************************************
Table Selector

Assists in making rows and cells in a table highlight as you move your mouse over them, and then allows you
to select a row/cell in the table.

See TableSelector.htm for an example.
*/
function TableSelector(tableID) {
	//this.debug = new SPS.Debug();
	//this.debug.Enable();
	
	this.table = document.getElementById(tableID);
	
	if (this.table == null) {
		//this.debug.Add("Could not find table with ID: " + tableID + "\n");
	}
	
	//Default to using colors instead of classes for events:  'color' or 'class'.
	this.styleType = 'color';
	
	//Setup our default color styles.
	this.onColor = '#D7D7D7';
	this.offColor = '';
	this.selectColor = '#FEFFB4';
	
	//Setup our CSS classes.
	this.onClass = '';
	this.offClass = '';
	this.selectClass = '';
	
	//Setup some additional options.
	this.clickEvent = null;				//the event that gets called when the user clicks a row
	this.selectedObj = null;			//the currently selected row or cell
	this.defaultRow = null;				//the row to select by default when the page first loads
	this.selectType = 'row';			//what to select: row or cell
	
	this.ignoreRows = new Array();		//contains the row numbers in the table to ignore
	this.ignoreCells = new Array();		//contains x,y coordinates for cells in the table to ignore
}

/*
Set the colors used for the over, out, and click events.
*/
TableSelector.prototype.SetColor = function(onColor, offColor, selectColor) {
	this.styleType = 'color';
	this.onColor = onColor;
	this.offColor = offColor;
	this.selectColor = selectColor;
}


/*
Set the classes used for the over, out, and click events.  If used, the color styles are ignored.
*/
TableSelector.prototype.SetClasses = function(onClass, offClass, selectClass) {
	this.styleType = 'class';
	this.onClass = onClass;
	this.offClass = offClass;
	this.selectClass = selectClass;
}

/*
Enables the TableSelector functionality on the table.
*/
TableSelector.prototype.Enable = function() {
	var rowLength = this.table.rows.length;
	
	for (var i=0; i < rowLength; i++) {
		var row = this.table.rows[i];
		
		//See if we should ignore this row or if we've already enabled TableSelector for it.
		if (this.ignoreRows['r' + i] != null || row.tableSelectorOn != null) {
			continue;
		}
		
		var cellLength = row.cells.length;
		
		for (var y=0; y < cellLength; y++) {
			var cell = row.cells[y];
			
			//See if we should ignore this cell.
			if (this.ignoreCells[i + ',' + y] != null) {
				continue;
			}
			
			var obj = this;
			
			//Save the initial css class name in case we need it later.
			if (this.styleType == 'class' && cell.className) {
				cell.initialClassName = cell.className;
			}
			
			cell.onmouseover = function() {
				obj.OnOver(this, this.parentNode);
			}
			
			cell.onmouseout = function() {
				obj.OnOut(this, this.parentNode);
			}
			
			if (!this.ignoreClick) {
				cell.onmousedown = function() {
					obj.OnMouseDown(this, this.parentNode);
				}
				
				cell.onmouseup = function(e) {
					obj.OnMouseUp(this, this.parentNode, e);
				}
			}
		}
		
		row.tableSelectorOn = true;
	}
	
	//If we have a valid default row, set it.
	if (this.selectType == 'row' && this.defaultRow != null && this.defaultRow >= 0 && this.defaultRow < this.table.rows.length) {
		this.SelectObj(this.table.rows[this.defaultRow]);
	}
}


/*
Sets a user-defined function to be called whenever an OnMouseUp event occurs.

Parameters:
	event - a reference to the function to call when an OnMouseUp event occurs on a row
*/
TableSelector.prototype.ClickEvent = function(event) {
	this.clickEvent = event;
}


/*
Tells the TableSelector to ignore certain rows.

Parameters:
	one or more numbers indicating which row numbers to ignore: 
		IgnoreRows(1,2,50)  (ignore rows 1, 2, and 50)
*/
TableSelector.prototype.IgnoreRows = function() {
	var args = this.IgnoreRows.arguments;
	
	for (var i=0; i < args.length; i++) {
		this.ignoreRows['r' + args[i]] = 1;
	}
}


/*
Tells the TableSelector to ignore certain cells.

Parameters:
	must come in pairs to specify the row/cell of the cell to ignore (zero-based): 
		IgnoreCells(0,4, 0,6)  (ignore the 3rd and 5th cells of the 1st row)
*/
TableSelector.prototype.IgnoreCells = function() {
	var args = this.IgnoreCells.arguments;
	
	if ((args.length % 2) != 0) {
		alert('TableSelector-IgnoreCells: parameters must come in x,y pairs');
		return;
	}
	
	for (var i=0; i < args.length; i+=2) {
		this.ignoreCells[args[i] + ',' + args[i+1]] = 1;
	}
}


/*
Tells the TableSelector to ignore mouse clicks.
*/
TableSelector.prototype.IgnoreClick = function() {
	this.ignoreClick = true;
}


/*
Whether to select rows or cells.
	type - "cell" or "row"
*/
TableSelector.prototype.SelectType = function(type) {
	if (type != 'cell' && type != 'row') {
		alert('TableSelector-SelectType: invalid parameter');
		return;
	}
	
	this.selectType = type;
}


/*
Tells the TableSelector to ignore the first row(s).
Parameters:
	number - the number of rows to ignore, starting from the top of the table.
*/
TableSelector.prototype.DefaultRow = function(rowNum) {
	this.defaultRow = rowNum;
}


//Clears the selected row or cell, if there was one selected.
TableSelector.prototype.ClearSelected = function() {
	if (this.selectedObj != null) {
		this.SetStyles(this.selectedObj, "clear");
		this.selectedObj = null;
	}
}


TableSelector.prototype.GetDebugger = function() {
	return this.debug;
}

/*
*** PRIVATE METHODS AND PROPERTIES ****************************************************************
*/

TableSelector.prototype.OnOver = function(cell, row) {
	//this.debug.Add("over - " + row.cells[2].innerHTML + "\n");
	if (this.selectType == 'row') {
		this.SetStyles(row, "over");
	}
	else {
		this.SetStyles(cell, "over");
	}
}

TableSelector.prototype.OnOut = function(cell, row) {
	//this.debug.Add("out\n");
	if (this.selectType == 'row') {
		this.SetStyles(row, "out");
	}
	else {
		this.SetStyles(cell, "out");
	}
}

TableSelector.prototype.OnMouseUp = function(cell, row, event) {
	//this.debug.Add("click\n");
	
	//Prevent our event from ocurring if the user clicked on a link or button.
	var clickObj = (window.event ? window.event.srcElement : event.target)
	
	//this.debug.Add(clickObj.tagName + " - " + clickObj.type + "\n");
	
	//See if we have an object that, if clicked, should not trigger an OnMouseUp event.
	if (this.EventShouldCancel(clickObj)) {
		//this.debug.Add('ignoring click\n');
		return;
	}
	
	if (this.selectType == 'row') {
		this.SelectObj(row);
		
		if (this.clickEvent) {
			this.clickEvent(row);
		}
	}
	else {
		this.SelectObj(cell);
		
		if (this.clickEvent) {
			this.clickEvent(cell);
		}
	}
}


//Used by OnMouseUp to determine if the object clicked on should fire the OnMouseUp event.
TableSelector.prototype.EventShouldCancel = function(clickObj) {
	if ((clickObj.tagName == 'INPUT' && (clickObj.type == 'submit' || clickObj.type == 'button')) || clickObj.tagName == 'BUTTON') {
		return true;
	}
	
	//See if this object is contained inside a link, or is a link.
	var obj = clickObj;
	
	while (obj != null && obj.tagName != "BODY") {
		if (obj.tagName == 'A') {
			return true;
		}
		
		obj = obj.parentNode;
	}
	
	return false;
}


TableSelector.prototype.OnMouseDown = function(cell, row) {
	//If this is the selected row, do a little "row click" effect.  Ignore all others.
	if (this.selectType == 'row') {
		if (this.selectedObj == row) {
			this.SetStyles(row, "clear");
		}
	}
	else {
		if (this.selectedObj == cell) {
			this.SetStyles(cell, "clear");
		}
	}
}

//Hilites the clicked-on row or cell.
TableSelector.prototype.SelectObj = function(obj) {
	//Unselect the previously selected object, if there was one.
	this.ClearSelected();
	
	this.SetStyles(obj, "click");
	this.selectedObj = obj;
}

/*
Sets the style on all the cells in the given row.
Parameters:
	obj - the obj to set styles on, a row or cell object
	type - the event type: out, over, click, or clear
*/
TableSelector.prototype.SetStyles = function(obj, type) {
	var color, styles, className;
	
	//If this is the selected row and we are on our way out, recolor it as the selected row.
	if (obj == this.selectedObj && type == "out") {
		color = this.selectColor;
		className = this.selectClass;
	}
	//Else if this is the selected row and we are mousing over the row, exit now.
	else if (obj == this.selectedObj && type == "over") {
		return;
	}
	else {
		switch(type) {
			case "over":	
				color = this.onColor;		
				className = this.onClass;
				styles = this.onStyle;
				break;
				
			case "out":		
				color = this.offColor;		
				className = this.offClass;		
				styles = this.offStyle;
				break;
				
			case "click":	
				color = this.selectColor;	
				className = this.selectClass;	
				styles = this.selectStyle;
				break;
				
			case "clear":	
				color = this.offColor;		
				className = this.offClass;		
				styles = this.offStyle;
				break;
		}
	}
	
	if (this.selectType == 'row') {
		for(var i=0; i < obj.cells.length; i++) {
			this.SetCellStyles(obj.cells[i], color, className);
		}
	}
	else {
		this.SetCellStyles(obj, color, className);
	}
}


//For use with SetStyles above.  Changes the style on a single cell.
TableSelector.prototype.SetCellStyles = function(cell, color, className) {
	if (this.styleType == 'color') {
		cell.style.backgroundColor = color;
		cell.style.cursor = 'default';
	}
	else {
		//Else use stylesheet classes.
		if (className != null) {
			cell.className = className;
		}
		else if (obj.initialClassName) {
			cell.className = obj.initialClassName;
		}
		else {
			cell.className = '';
		}
	}
}