function jscstyle(value)
{
	this.initialized = false;
	
	this.color = null;
	this.backgroundColor = null;
	this.border = null;
	this.borderCollapse = null;
	this.font = null;
	this.fontFamily = null;
	this.fontSize = null;
	this.cursor = null;
	this.padding = null;
	this.paddingLeft = null;
	this.paddingRight = null;
	this.paddingTop = null;
	this.paddingBottom = null;
	
	this.isValidValue = function(value)
	{
		return (value || value == "");
	}
	
	this.assignto = function(el)
	{
		if (el && el.style)
		{
			if (this.isValidValue(this.color))  el.style.color = this.color;
			if (this.isValidValue(this.backgroundColor))  el.style.backgroundColor = this.backgroundColor;
			if (this.isValidValue(this.border))  el.style.border = this.border;
			if (this.isValidValue(this.borderCollapse))  el.style.borderCollapse = this.borderCollapse;
			if (this.isValidValue(this.font))  el.style.font = this.font;
			if (this.isValidValue(this.fontFamily))  el.style.fontFamily = this.fontFamily;
			if (this.isValidValue(this.fontSize))  el.style.fontSize = this.fontSize;
			if (this.isValidValue(this.cursor))  el.style.cursor = this.cursor;
			if (this.isValidValue(this.padding))  el.style.padding = this.padding;
			if (this.isValidValue(this.paddingLeft))  el.style.paddingLeft = this.paddingLeft;
			if (this.isValidValue(this.paddingRight))  el.style.paddingRight = this.paddingRight;
			if (this.isValidValue(this.paddingTop))  el.style.paddingTop = this.paddingTop;
			if (this.isValidValue(this.paddingBottom))  el.style.paddingBottom = this.paddingBottom;
		}
	}
	
	this.init = function(value)
	{
		if (value)
		{
			if (this.isValidValue(value.color)) this.color = value.color;
			if (this.isValidValue(value.backgroundColor)) this.backgroundColor = value.backgroundColor;
			if (this.isValidValue(value.border)) this.border = value.border;
			if (this.isValidValue(value.borderCollapse)) this.borderCollapse = value.borderCollapse;
			if (this.isValidValue(value.font)) this.font = value.font;
			if (this.isValidValue(value.fontFamily)) this.fontFamily = value.fontFamily;
			if (this.isValidValue(value.fontSize)) this.fontSize = value.fontSize;
			if (this.isValidValue(value.cursor)) this.cursor = value.cursor;
			if (this.isValidValue(value.padding)) this.padding = value.padding;
			if (this.isValidValue(value.paddingLeft)) this.paddingLeft = value.paddingLeft;
			if (this.isValidValue(value.paddingRight)) this.paddingRight = value.paddingRight;
			if (this.isValidValue(value.paddingTop)) this.paddingTop = value.paddingTop;
			if (this.isValidValue(value.paddingBottom)) this.paddingBottom = value.paddingBottom;
			
			this.initialized = true;
		}
	}

	this.init(value);
}

function jsctable()
{
	this.tag = "jsctable";
	this.accessKey = "T";
	this.firstRowIsHeader = true;
		
	this.tables = new Array();
	this.activeTable = null;
	
	this.higlightedrowstyle = new jscstyle();
	this.selectedrowstyle = new jscstyle();
	this.selectedhiglightedrowstyle = new jscstyle();
	this.rowstyle = new jscstyle();
	this.cellstyle = new jscstyle();
	this.altrowstyle = new jscstyle();
	this.headerrowstyle = new jscstyle();
	this.tablestyle = new jscstyle();

	this.sourceBodyOnKeyDown = null;
	this.sourceOnKeyDown = new Array();
	this.sourceOnFocus = new Array();
	this.selectedRow = new Array();
	this.higlightedRow = new Array();

	this.cellpadding = 3;
	this.cellspacing = 0;
	
	this.assignStyle = function(style, el)
	{
		if (style && style.assignto && el && el.style)
		{
			style.assignto(el);
			return true;
		}
		return false;
	}
	
	this.initTable =function(table)
	{
		if (table && table.tagName && table.tagName == "TABLE" && table.attributes && table.attributes[this.tag])
		{
			if (table.onkeydown)
				this.sourceOnKeyDown[table.uniqueID] = table.onkeydown;
			table.onkeydown = this.onkeydown;
	
			if (table.onfocus)
				this.sourceOnFocus[table.uniqueID] = table.onfocus;
			table.onfocus = this.onfocus;
			
			//table.accessKey = this.accessKey;
			
			this.assignStyle(this.tablestyle, table);
			
			for (var i=0;i<table.tBodies.length;i++)			
				for (var j=0;j<table.tBodies[i].rows.length;j++)
				{
					table.tBodies[i].rows[j].onmouseenter = this._highlightrow;
					table.tBodies[i].rows[j].onmouseleave = this._unhighlightrow;
					table.tBodies[i].rows[j].onclick = this._toggleselection;
					if(this.isValidTableRow(table.tBodies[i].rows[j]))
						this.assignStyle(this.rowstyle, table.tBodies[i].rows[j]);
					if (this.cellstyle.initialized)
					{
						//alert(0);
						for (var m=0;m<table.tBodies[i].rows[j].cells.length;m++)
							this.assignStyle(this.cellstyle, table.tBodies[i].rows[j].cells[m])
					}
				
				}
			this.addJscTableFooter(table);	
			this.tables[this.tables.length] = table;
		}
		
		
	}
	
	this.setJscTableFooterStyleSheet = function()
	{
		for (var i=0;i<document.styleSheets.length;i++)
			if (document.styleSheets[i].title == "__jsctablestylesheet")
				return;
		var ss = document.createStyleSheet();
		ss.media = "print";
		ss.title = "__jsctablestylesheet";
		ss.addRule(".jsctable-footer", "display:none",0);
	}
	
	this.addJscTableFooter = function(table)
	{
		var tFoot = null;
		if (!table.tFoot)	
			tFoot = table.createTFoot();
		else
			tFoot = table.tFoot;
		
		var cellsCount = table.tBodies[0].rows[0].cells.length;
		var row = tFoot.insertRow();
		row.className = "jsctable-footer";
		var cell = row.insertCell();
		cell.colSpan =  cellsCount;
		cell.align = "right";
		cell.vAlign = "middle";
		cell.style.height = "28";
		
		var fhtml = "";
		fhtml += "<font style='font-size:10;font-family:verdana'>Action: &nbsp;";
		fhtml += "<select style='font-size:10;font-family:verdana'>";
		fhtml += "<option value='' selected> [ Choose Action ] </option>";
		fhtml += "<option value='jscTable.selectAll(jscTable.getParentTable(this))'>Select All</option>";
		fhtml += "<option value='jscTable.unselectAll(jscTable.getParentTable(this))'>Unselect All</option>";
		fhtml += "<option value='jscTable.toggleSelectionAll(jscTable.getParentTable(this))'>Toggle Selection</option>";
		fhtml += "<option value='jscTable.showSelected(jscTable.getParentTable(this))'>Show Only Selected</option>";
		fhtml += "<option value='jscTable.showUnselected(jscTable.getParentTable(this))'>Show Only Unselected</option>";		
		fhtml += "<option value='jscTable.showAll(jscTable.getParentTable(this))'>Show All</option>";
		fhtml += "</select>&nbsp;";
		fhtml += "<input type='button' value='Do' style='font-size:10;font-family:verdana' onclick='if(this.previousSibling.previousSibling.value){eval(this.previousSibling.previousSibling.value)}'></font>";
		
		cell.innerHTML = fhtml;
		
		this.setJscTableFooterStyleSheet();

	}
	
	this.onfocus = function()
	{
		var table = this;
		if (jscTable.isValidTable(table))
			if (!jscTable.higlightedRow[table.uniqueID])
				jscTable.goNextRow(table);
		return true;
	}
	
	this.onkeydown = function()
	{
		var kcDOWN = 40;
		var kcUP = 38;
		var kcPAGEDOWN = 34;
		var kcPAGEUP = 33;

		var kcLEFT = 37;
		var kcRIGHT = 39;
		var kcRETURN = 13;
		var kcESC = 27;
		var kcDELETE = 46;
		var kcBACKSPACE = 8;
		var kcSPACE = 32;
		var table = this;
		if (jscTable.sourceOnKeyDown[table.uniqueID])
			jscTable.sourceOnKeyDown[table.uniqueID]();
		
		if (!event.altKey)
		{
			//alert(this.name);
			if (event.keyCode==kcUP)
			{
				jscTable.goPreviousRow(table);
				event.returnValue = null;
			}
			else if (event.keyCode==kcDOWN)
			{
				jscTable.goNextRow(table);
				event.returnValue = null;
			}
			else if (event.keyCode==kcSPACE && event.ctrlKey)
			{
				jscTable.toggleSelection(table, jscTable.higlightedRow[table.uniqueID]);
				event.returnValue = null;
			}
		}
		
	}
	
	this.onbodykeydown = function()
	{
		var kcDOWN = 40;
		var kcUP = 38;
		
		if (event.keyCode == kcDOWN && event.altKey)
		{
			if (jscTable.sourceBodyOnKeyDown)
				jscTable.sourceBodyOnKeyDown();
		
			jscTable.selectNextTable();
			event.returnValue = false;
			event.cancelBubble = true;
		}
		else
		if (event.keyCode == kcUP && event.altKey)
		{
			if (jscTable.sourceBodyOnKeyDown)
				jscTable.sourceBodyOnKeyDown();
		
			jscTable.selectPreviousTable();
			event.returnValue = false;
			event.cancelBubble = true;
		}
		
	}
	
	this.selectNextTable = function()
	{
		
		var found = false;
		var activeTable = null;
		
		for (var i=0;i<this.tables.length;i++)
		{
			if (found)
			{
				activeTable = this.tables[i];
				break;
			}
			
			if (this.tables[i] == this.activeTable)
				found = true;
		}
		
		if (!activeTable && this.tables.length>0)
			activeTable = this.tables[0];
		
		if (activeTable)
		{
			this.activeTable = activeTable;
			this.activeTable.focus();
			
		}
		
		
	}

	this.selectPreviousTable = function()
	{
		
		var found = false;
		var activeTable = null;
		
		for (var i=this.tables.length-1;i>=0;i--)
		{
			if (found)
			{
				activeTable = this.tables[i];
				break;
			}
			
			if (this.tables[i] == this.activeTable)
				found = true;
		}
		
		if (!activeTable && this.tables.length>0)
			activeTable = this.tables[this.tables.length-1];
		
		if (activeTable)
		{
			this.activeTable = activeTable;
			this.activeTable.focus();
			
		}
		
		
	}
	
	this.isValidTable = function(table)
	{
		return (table && table.tagName && String(table.tagName).toUpperCase() == "TABLE");
	}

	this.isValidTableRow = function(row)
	{
		if (row && row.tagName && String(row.tagName).toUpperCase() == "TR" && row.cells && row.cells.length && row.cells.length>0 && row.cells[0].tagName)
		{
			if (this.firstRowIsHeader && row.rowIndex == 0)
				return false;
			else
				return (String(row.cells[0].tagName).toUpperCase() == "TD");
		}
		return true;
	}
	
	this.goPreviousRow = function(table)
	{
		if (this.isValidTable(table))
		{
			var row = this.higlightedRow[table.uniqueID];
			if (this.firstRowIsHeader)
				row = (!row) ? ((table.tBodies[0] && table.tBodies[0].rows[1]) ? table.tBodies[0].rows[1] : null) : row.previousSibling;
			else
				row = (!row) ? ((table.tBodies[0] && table.tBodies[0].rows[0]) ? table.tBodies[0].rows[0] : null) : row.previousSibling;
				
			return this.highlightRow(table, row);
		}
		return false;
	}

	this.goNextRow = function(table)
	{
		if (this.isValidTable(table))
		{
			var row = this.higlightedRow[table.uniqueID];
			if (this.firstRowIsHeader)
				row = (!row) ? ((table.tBodies[0] && table.tBodies[0].rows[1]) ? table.tBodies[0].rows[1] : null) : row.nextSibling;
			else
				row = (!row) ? ((table.tBodies[0] && table.tBodies[0].rows[0]) ? table.tBodies[0].rows[0] : null) : row.nextSibling;

			return this.highlightRow(table, row);
		}
		return false;
	}
	
	this.getElementTopOffset = function (el)
	{
		var result = 0;
		while (el)
		{
			result += el.offsetTop;
			el = el.offsetParent;
		}
	
		return result;
	}

	this.scrollIntoView = function(el)
	{
		if (el)
		{
			var elementTopOffset = this.getElementTopOffset(el);
			if (elementTopOffset > document.body.scrollTop + document.body.clientHeight - el.clientHeight)
				document.body.scrollTop = (elementTopOffset + el.clientHeight) - (document.body.clientHeight - document.body.topMargin) ;
			else if (elementTopOffset < document.body.scrollTop)
				document.body.scrollTop = (elementTopOffset - document.body.topMargin) ;
		}
	}
	
	
	this.highlightRow = function(table, row)
	{
		//debugger;
		if (table && row && this.isValidTableRow(row))
		{
			this.scrollIntoView(row);
			
			if (this.higlightedRow[table.uniqueID] == row)
				return true;
			if (!this.isRowSelected(table, this.higlightedRow[table.uniqueID]))
				this.assignStyle(this.rowstyle, this.higlightedRow[table.uniqueID])
			else
				this.assignStyle(this.selectedrowstyle, this.higlightedRow[table.uniqueID]);
				
			this.higlightedRow[table.uniqueID] = row;
			if (!this.isRowSelected(table, row))
				this.assignStyle(this.higlightedrowstyle, row);
			else
				this.assignStyle(this.selectedhiglightedrowstyle, row);
			return true;
		}
		return false;
	}

	this.unhighlightRow = function(table, row)
	{
		if (table && row && this.isValidTableRow(row))
		{
			if (this.higlightedRow[table.uniqueID] == row)
			{
				if (!this.isRowSelected(table, row))
					this.assignStyle(this.rowstyle, this.higlightedRow[table.uniqueID]);		
				else
					this.assignStyle(this.selectedrowstyle, this.higlightedRow[table.uniqueID]);		
					
				this.higlightedRow[table.uniqueID] = null;
				return true;
			}
		}
		return false;
	}
	
	this.getRowTable = function(row)
	{
		var result = null;
		
		if (row && row.tagName && String(row.tagName).toUpperCase() == "TR")
		{
			result = row.parentElement;
		
			while(result != null && String(result.tagName).toUpperCase() != "TABLE")
				result = result.parentElement;
		}
		return result;
	}
	
	this.getParentTable = function(el)
	{
		var result = null;
		
		if (el)
		{
			result = el.parentElement;
		
			while(result != null && String(result.tagName).toUpperCase() != "TABLE")
				result = result.parentElement;
		}
		return result;
	}

	this._highlightrow = function()
	{
		var row = this;
		var table = jscTable.getRowTable(row);
		
		if (table && row)
			return jscTable.highlightRow(table, row);
			
		return false;
	}

	this._unhighlightrow = function()
	{
		var row = this;
		var table = jscTable.getRowTable(row);
		
		if (table && row)
			return jscTable.unhighlightRow(table, row, true);
			
		return false;
	
	}

	this._toggleselection = function()
	{
		var row = this;
		var table = jscTable.getRowTable(row);
		if (table && row)
			return jscTable.toggleSelection(table, row);
			
		return false;
		
	}

	this.toggleSelectionAll = function(table)
	{
		if (this.isValidTable(table))
		{
			for (var i=0;i<table.tBodies.length;i++)
				for (var j=0;j<table.tBodies[i].rows.length;j++)
					this.toggleSelection(table, table.tBodies[i].rows[j]);
			return true;
		}
		return false;
		
	}
	
	this.toggleSelection = function(table, row)
	{
		if (table && row)
		{
			if (this.isRowSelected(table, row))
				return this.unselectRow(table,row);
			else
				return this.selectRow(table,row);
		}
		else
			return false;
	}

	this.showAll = function(table)
	{
		if (this.isValidTable(table))
		{
			for (var i=0;i<table.tBodies.length;i++)
				for (var j=0;j<table.tBodies[i].rows.length;j++)
					if (this.isValidTableRow(table.tBodies[i].rows[j]))
					{
						table.tBodies[i].rows[j].style.visibility = "visible";
						table.tBodies[i].rows[j].style.display = "";
					}
			return true;
		}
		return false;
	
	}

	this.showUnselected = function(table)
	{
		if (this.isValidTable(table))
		{
			for (var i=0;i<table.tBodies.length;i++)
				for (var j=0;j<table.tBodies[i].rows.length;j++)
					if (this.isValidTableRow(table.tBodies[i].rows[j]))
					{
						table.tBodies[i].rows[j].style.visibility = (this.isRowSelected(table, table.tBodies[i].rows[j])) ? "hidden" : "visible";
						table.tBodies[i].rows[j].style.display = (this.isRowSelected(table, table.tBodies[i].rows[j])) ? "none" : "";
					}
			return true;
		}
		return false;
	
	}

	this.showSelected = function(table)
	{
		if (this.isValidTable(table))
		{
			for (var i=0;i<table.tBodies.length;i++)
				for (var j=0;j<table.tBodies[i].rows.length;j++)
					if (this.isValidTableRow(table.tBodies[i].rows[j]))
					{
						table.tBodies[i].rows[j].style.visibility = (this.isRowSelected(table, table.tBodies[i].rows[j])) ? "visible" : "hidden";				
						table.tBodies[i].rows[j].style.display = (this.isRowSelected(table, table.tBodies[i].rows[j])) ? "" : "none";										
					}
			return true;
		}
		return false;
		
	}

	this.selectAll = function(table)
	{
		if (this.isValidTable(table))
		{
			for (var i=0;i<table.tBodies.length;i++)
				for (var j=0;j<table.tBodies[i].rows.length;j++)
					this.selectRow(table, table.tBodies[i].rows[j]);
			return true;
		}
		return false;
		
	}

	this.selectRow = function(table, row)
	{
		if (table && row && this.isValidTableRow(row))
		{
			if (!this.selectedRow[table.uniqueID])
				this.selectedRow[table.uniqueID] = new Array();	
			this.selectedRow[table.uniqueID][row.uniqueID]= row;
			this.assignStyle(this.selectedrowstyle, row);		
			
			return true;
		}
		return false;
	}

	this.unselectAll = function(table)
	{
		if (this.isValidTable(table))
		{
			for (var i=0;i<table.tBodies.length;i++)
				for (var j=0;j<table.tBodies[i].rows.length;j++)
					this.unselectRow(table, table.tBodies[i].rows[j]);
			return true;
		}
		return false;
		
	}

	this.unselectRow = function(table, row)
	{
		if (table && row && this.isValidTableRow(row) && this.selectedRow[table.uniqueID] && this.selectedRow[table.uniqueID][row.uniqueID])
		{
			this.assignStyle(this.rowstyle, row);		
			this.selectedRow[table.uniqueID][row.uniqueID] = null;
			this.unhighlightRow(table, row);
			this.highlightRow(table, row);
			return true;
		}
		return false;
	}
	
	this.isRowSelected = function(table, row)
	{
		if (table && row && this.selectedRow[table.uniqueID])
			return (this.selectedRow[table.uniqueID][row.uniqueID] == row);
		
		return false;
	}
	
	
	this.init = function(value)
	{
		this.accessKey = "T";
		
		if (!value)
			value = new Array();
		
/*		
{higlightedrowstyle:{backgroundColor:"#FFFFCC",color:"black",cusor:"hand"},selectedrowstyle:{backgroundColor:"#FFFF00",color:"black",cusor:"hand"},selectedhiglightedrowstyle:{backgroundColor:"darkred",color:"#FFFFCC",cusor:"hand"},rowstyle:{backgroundColor:"#CCCCCC",color:"black",cursor:"hand"},cellstyle:{border:"1 solid white", padding:"2"},tablestyle:{border:"2 grove black",fontFamily:"Verdana",fontSize:"10",borderCollapse:"collapse"}}
		higlightedrowstyle:{backgroundColor:"#FFFFCC",color:"black",cusor:"hand"},selectedrowstyle:{backgroundColor:"#FFFF00",color:"black",cusor:"hand"},selectedhiglightedrowstyle:{backgroundColor:"darkred",color:"#FFFFCC",cusor:"hand"},rowstyle:{backgroundColor:"#CCCCCC",color:"black",cursor:"hand"},cellstyle:{border:"1 solid white", padding:"2"},tablestyle:{border:"2 solid black",fontFamily:"Verdana",fontSize:"10",borderCollapse:"collapse"}}
*/		
		this.higlightedrowstyle = new jscstyle(value.higlightedrowstyle);
		this.selectedrowstyle = new jscstyle(value.selectedrowstyle);
		this.selectedhiglightedrowstyle = new jscstyle(value.selectedhiglightedrowstyle);		
		this.rowstyle = new jscstyle(value.rowstyle);
		this.cellstyle = new jscstyle(value.cellstyle);
		this.altrowstyle = new jscstyle();
		this.headerrowstyle = new jscstyle();
		this.tablestyle = new jscstyle(value.tablestyle);

		this.sourceOnKeyDown = new Array();
		this.sourceOnFocus = new Array();
		this.selectedRow = new Array();
		this.higlightedRow = new Array();	
		
		var elements = document.body.getElementsByTagName("TABLE");
		
		for (var i=0;i<elements.length;i++)
			this.initTable(elements[i]);

		this.sourceBodyOnKeyDown = document.body.onkeydown;
		document.body.onkeydown = this.onbodykeydown;
		
	}
	
	//this.init();
}

var jscTable = new jsctable();
