/* This is Code that needs to be in the page the table is in
// built to work with IE 5.5 and Netscape 6.0
// Author - David Caldwell
<HTML>
<HEAD>
<script language="JavaScript" src="javascript/multiSelect.js"></SCRIPT>
<SCRIPT LANGUAGE=javascript>
<!--
var Table1;
var Table2;

function init() {

Table1 = new msTable('table1')
Table1.rowBGColor = "#E8D7F7";					// This is the beginning row bgColor.
Table1.rowAltBGColor = "#ffffff";					// This is the alternating row bgColor.
Table1.rowSelectColor = "#C3B0D5";				// This is the selected color for a row.
Table1.formName = 'RowForm';						// Only needed for Netscape.
Table1.callRowClick = true;				// Set to true to call a function onClick.
Table1.callRowDblClick = true;
Table1.rowActive = true;							// Set to false to prevent table row click detection.
Table1.reverseColor = false;						// Set to true to begin alternating with rowAltBGColor.
Table1.addRow(new Table1.tableRow(0,1,'Project 1','First Project'));
Table1.addRow(new Table1.tableRow(1,2,'Project 2','Second Project'));
Table1.addRow(new Table1.tableRow(2,3,'Project 3','Third Project'));
Table1.addRow(new Table1.tableRow(3,4,'Project 4','Fourth Project'));
Table1.addRow(new Table1.tableRow(4,5,'Project 5','Fifth Project'));
Table1.activate();

Table2 = new msTable('table2')
Table2.rowBGColor = "#E8D7F7";					// This is the beginning row bgColor.
Table2.rowAltBGColor = "#ffffff";					// This is the alternating row bgColor.
Table2.rowSelectColor = "#C3B0D5";				// This is the selected color for a row.
Table2.formName = 'RowForm';						// Only needed for Netscape.
Table2.callRowClick = true;				// Set to true to call a function onClick.
Table2.callRowDblClick = true;
Table2.rowActive = true;							// Set to false to prevent table row click detection.
Table2.reverseColor = false;						// Set to true to begin alternating with rowAltBGColor.
Table2.addRow(new Table2.tableRow(0,1,'Project 1','First Project'));
Table2.addRow(new Table2.tableRow(1,2,'Project 2','Second Project'));
Table2.addRow(new Table2.tableRow(2,3,'Project 3','Third Project'));
Table2.addRow(new Table2.tableRow(3,4,'Project 4','Fourth Project'));
Table2.addRow(new Table2.tableRow(4,5,'Project 5','Fifth Project'));
Table2.activate();

}

function table1rowClick(e) {
	//alert('Table1 '+obj.rowIndex)
}

function table2rowClick(obj) {
	//alert('Table2 '+obj.rowIndex)
}

function table1rowDblClick(obj) {
	//alert('Table1 '+obj.rowIndex)
}

function table2rowDblClick(obj) {
	//alert('Table2 '+obj.rowIndex)
}

//-->
</SCRIPT>

	
<BODY onload="init();">
<TABLE ID="table1" border="1">
<tr><td>Test Row 1 Cell 1</td><td>Test Row 1 Cell 2</td></tr>
<tr><td>Test Row 2 Cell 1</td><td>Test Row 2 Cell 2</td></tr>
<tr><td>Test Row 3 Cell 1</td><td>Test Row 3 Cell 2</td></tr>
<tr><td>Test Row 4 Cell 1</td><td>Test Row 4 Cell 2</td></tr>
<tr><td>Test Row 5 Cell 1</td><td>Test Row 5 Cell 2</td></tr>
</TABLE>
<TABLE ID="table2" border="1">
<tr><td>Test Row 1 Cell 1</td><td>Test Row 1 Cell 2</td></tr>
<tr><td>Test Row 2 Cell 1</td><td>Test Row 2 Cell 2</td></tr>
<tr><td>Test Row 3 Cell 1</td><td>Test Row 3 Cell 2</td></tr>
<tr><td>Test Row 4 Cell 1</td><td>Test Row 4 Cell 2</td></tr>
<tr><td>Test Row 5 Cell 1</td><td>Test Row 5 Cell 2</td></tr>
</TABLE>
</BODY>
</HTML>

*/

//Set variables used in the Multi Select code and available globally

var currentRowId = -1;
var currentRowIndex = -1;
var oldRowId = -1;
var oldRowIndex = -2;
var tableItems = new Array();
var beginRow = 0;
var resetBeginRow = false;
var lastRowUnselected = new Array();
var selectedItems = new Array();
var tableIndex = 0;
var allSelected = false;
var shiftDown = false;
var ctrlDown = false;
var form;
var ndx = -1;
var addItem = true;
var j = 0;

is = new BrowserCheck()

function msTable(name){
	this.name = name
	if (is.ie) {
		this.table = eval(this.name+'.children(0)')
	} else if (is.ns6) {
		this.table = document.getElementById(this.name)
	}
	this.hasHeader = false
	this.buildTable = false
	this.rowBGColor = '#EBF8E9'
	this.rowAltBGColor = '#ffffff'
	this.rowSelectColor = '#BFD3DE'
	this.formName = ''
	this.callRowClick = false
	this.callRowDblClick = false
	this.rowActive = true
	this.multiSelect = true
	this.reverseColor = false
	this.items = new Array()	
	this.activate = activateTableItems
	this.addRow = addTableRow
	this.tableRow = tableRowArray
	this.shift = doShiftSelect
	this.ctrl = doCtrlSelect
	this.selectRow = doSelect
	this.sort = sortTableRows
	this.selectAll = selectAllRows
	this.unselectAll = unselectAllRows
	this.colorRows = ResetOriginalRowColor
	this.originalColor = GetOriginalRowColor
	if (is.ns6) document.captureEvents(Event.KEYDOWN | Event.KEYUP)
	if (is.ns6) document.onkeydown = setKeyCode
	if (is.ns6) document.onkeyup = clearKeyCode
}

function setKeyCode(e) {
	shiftDown = (e.keyCode == 16)? true:false;
	ctrlDown = (e.keyCode == 17)? true:false;
}

function clearKeyCode(e) {
	shiftDown = false
	ctrlDown = false
}

function addTableRow(item) {
	this.items[this.items.length] = item;
}

function activateTableItems() {
	for (i=0; i<this.items.length; i++) tableItems[i] = this.items[i];
	//(this.formName != '') ? form = document.forms[this.formName]:form = document.forms[0];
	if (is.ie) {
		if (this.buildTable) {
			for (var j=0; j<tableItems.length; j++) {
				this.table.insertRow(j)
				for (var o=0; o<10; o++) {
					if (eval('tableItems[j].p'+(o+3)) != null)
						this.table.children(j).insertCell(o)
				}
				for (var i=0; i<this.table.children(j).cells.length; i++) 
					this.table.children(j).cells(i).innerText = eval('tableItems[j].p'+(i+3));
			}
		}
		for (var i=0; i<this.table.children.length; i++) {
			if (this.hasHeader && i==0)	continue 
			this.table.children(i).onclick = OnRowClick
			this.table.children(i).ondblclick = OnRowDblClick
			this.table.children(i).onmouseover = SetRowIndex
			this.table.children(i).onmouseout = UnSetRowIndex
			this.table.children(i).parent = this
		}
	} 
	if (is.ns6) {
		if (this.buildTable) {
			for (var j=0; j<this.table.rows.length; j++) {
				for (var i=0; i<this.table.rows[j].cells.length; i++) 
					this.table.rows[j].cells[i].innerHTML = eval('tableItems[j].p'+(i+3));
			}
		}
		for (var j=0; j<this.table.rows.length; j++) {
			if (this.hasHeader && j==0)	continue 
			this.table.rows[j].onclick = OnRowClick
			//this.table.rows[j].ondblclick = OnRowDblClick
			this.table.rows[j].onmouseover = SetRowIndex
			this.table.rows[j].onmouseout = UnSetRowIndex
			this.table.rows[j].parent = this
		}
	}
	this.colorRows();
}

function tableRowArray(p1,p2,p3,p4,p5,p6,p7,p8,p9) {
	this.p1 = p1; // Always the .rowId which must be unique.
	this.p2 = (arguments.length > 1)? p2:null; // .id
	this.p3 = (arguments.length > 2)? p3:null;
	this.p4 = (arguments.length > 3)? p4:null;
	this.p5 = (arguments.length > 4)? p5:null;
	this.p6 = (arguments.length > 5)? p6:null;
	this.p7 = (arguments.length > 6)? p7:null;
	this.p8 = (arguments.length > 7)? p8:null;
	this.p9 = (arguments.length > 8)? p9:null;
}

function BrowserCheck() {
	var b = navigator.appName
	if (b=='Netscape') this.b = 'ns'
	else if (b=='Microsoft Internet Explorer') this.b = 'ie'
	else this.b = b
	this.version = navigator.appVersion
	this.v = parseInt(this.version)
	this.ns = (this.b=='ns' && this.v>=4)
	this.ns4 = (this.b=='ns' && this.v==4)
	this.ns5 = (this.b=='ns' && this.v==5)
	this.ns6 = (this.b=='ns' && this.v>=5)
	this.ie = (this.b=='ie' && this.v>=4)
	this.ie4 = (this.version.indexOf('MSIE 4')>0)
	this.ie5 = (this.version.indexOf('MSIE 5')>0)
	this.min = (this.ns||this.ie)
}

function SetRowIndex() {
	if (currentRowId != -1) {
		oldRowId = currentRowId;
		oldRowIndex = currentRowIndex;
	}
	currentRowId = tableItems[this.rowIndex].p2
	currentRowIndex = this.rowIndex
}

function UnSetRowIndex() {
	oldRowId = -1;
	oldRowIndex = -1;
}

function OnRowClick(e) {
	if (!tableItems[this.rowIndex]) {
		this.parent.unselectAll()
		return
	}
	ndx = 0
	addItem = true
	if (is.ie) {
		if (window.event.shiftKey && this.parent.multiSelect)
			this.parent.shift(this.rowIndex)
		else if (window.event.ctrlKey && this.parent.multiSelect)
			this.parent.ctrl(this.rowIndex)
		else
			this.parent.selectRow(this.rowIndex)
	} else if (is.ns6) {
		if (shiftDown && this.parent.multiSelect)
			this.parent.shift(this.rowIndex)
		else if (ctrlDown && this.parent.multiSelect)
			this.parent.ctrl(this.rowIndex)
		else
			this.parent.selectRow(this.rowIndex)
	}
	if (this.parent.callRowClick) eval(this.parent.name+'rowClick(this)')
}

function OnRowDblClick() {
	if (this.parent.callRowDblClick) eval(this.parent.name+'rowDblClick(this)')
}

function doShiftSelect(RowId) {
	if (resetBeginRow) {
		if (lastRowUnselected.length > 0) {
			beginRow = lastRowUnselected[0].p1
			selectedItems[selectedItems.length] = lastRowUnselected[0];
			lastRowUnselected = new Array();
		} else if (selectedItems.length > 0) {
			beginRow = selectedItems[selectedItems.length-1].p1;
		}
	} 
	if (beginRow > RowId) {	
		var iStart = RowId;	
		var iEnd = beginRow;
	} else {
		var iStart = beginRow;	
		var iEnd = RowId;
	}
	this.colorRows()
	selectedItems = new Array()
	j=0;
	for (i=iStart; i<=iEnd; i++) {
		selectedItems[j] = new this.tableRow(tableItems[i].p1,tableItems[i].p2,tableItems[i].p3,tableItems[i].p4,tableItems[i].p5,tableItems[i].p6,tableItems[i].p7,tableItems[i].p8,tableItems[i].p9);
		j++
		if (is.ie) this.table.children(i).style.backgroundColor = this.rowSelectColor;
		if (is.ns6) this.table.rows[i].bgColor = this.rowSelectColor;
	}
	resetBeginRow = false
	return true
}

function doCtrlSelect(RowId) {
	if (is.ie) this.table.children(RowId).style.backgroundColor = this.rowSelectColor;
	if (is.ns6) this.table.rows[RowId].bgColor = this.rowSelectColor
	resetBeginRow = true;		
	for (var k in selectedItems) {
		if (RowId == selectedItems[k].p1) {
			addItem = false;
			ndx = k;
		}
	}
	if (addItem) {
		lastRowUnselected = new Array();
		selectedItems[j] = new this.tableRow(tableItems[RowId].p1,tableItems[RowId].p2,tableItems[RowId].p3,tableItems[RowId].p4,tableItems[RowId].p5,tableItems[RowId].p6,tableItems[RowId].p7,tableItems[RowId].p8,tableItems[RowId].p9);
		j++
	} else {
		lastRowUnselected[0] = new this.tableRow(selectedItems[ndx].p1,selectedItems[ndx].p2,selectedItems[ndx].p3,selectedItems[ndx].p4,selectedItems[ndx].p5,selectedItems[ndx].p6,selectedItems[ndx].p7,selectedItems[ndx].p8,selectedItems[ndx].p9);
		selectedItems[ndx].p1 = null;
		var tempArray = new Array();
		var h = 0;
		if (selectedItems.length != 0) {
			if (is.ie) this.table.children(RowId).style.backgroundColor = this.originalColor(RowId)
			if (is.ns6) this.table.rows[RowId].bgColor = this.originalColor(RowId)
			for (var l in selectedItems) {
				if (selectedItems[l].p1 != null) {
					tempArray[h] = new this.tableRow(selectedItems[l].p1,selectedItems[l].p2,selectedItems[l].p3,selectedItems[l].p4,selectedItems[l].p5,selectedItems[l].p6,selectedItems[l].p7,selectedItems[l].p8,selectedItems[l].p9);
					h++
				}
			}
		}
		selectedItems = new Array();
		for (g=0; g<tempArray.length; g++) {
			selectedItems[g] = tempArray[g];
		}
		j=g;
		g=0;
	}
	return true
}

function doSelect(RowId) {
	this.colorRows()
	if (is.ie) this.table.children(RowId).style.backgroundColor = this.rowSelectColor
	if (is.ns6) this.table.rows[RowId].bgColor = this.rowSelectColor
	lastRowUnselected = new Array()
	resetBeginRow = true
	selectedItems = new Array()
	selectedItems[0] = new this.tableRow(tableItems[RowId].p1,tableItems[RowId].p2,tableItems[RowId].p3,tableItems[RowId].p4,tableItems[RowId].p5,tableItems[RowId].p6,tableItems[RowId].p7,tableItems[RowId].p8,tableItems[RowId].p9)
	j=1
	return true
}

function sortTableRows() {
//this function needs work
	this.unselectAll()
	tableItems.reverse()
	if (is.ie) {
		for (var i=0; i<this.table.children.length; i++) {
			var newIndex = (this.table.children.length - 1) - i
			this.moveRow(i,newIndex)		
			if (newIndex < i) break
		}
		for (var j=0; j<this.table.children.length; j++) {
			for (var i=0; i<this.table.children(j).cells.length; i++) 
				this.table.children(j).cells(i).innerText = eval('tableItems[j].p'+(i+3));
		}
	} 
	if (is.ns6) {
		for (var i=0; i<this.table.rows.length; i++) {
			var newIndex = (this.table.rows.length - 1) - i
			this.moveRow(i,newIndex)		
			if (newIndex < i) break
		}
		for (var j=0; j<this.table.rows.length; j++) {
			for (var i=0; i<this.table.rows[j].cells.length; i++) 
				this.table.rows[j].cells[i].innerHTML = eval('tableItems[j].p'+(i+3));
		}
	}
}

function selectAllRows() {
	selectedItems = new Array();
	for (i=0; i<tableItems.length; i++) {
		if (!allSelected) {
			selectedItems[i] = tableItems[i]
			if (is.ie) this.table.children(i).style.backgroundColor = this.rowSelectColor;
			if (is.ns6) this.table.rows[i].bgColor = this.rowSelectColor;
		} else {
			this.colorRows()
		}
	}
	allSelected = !allSelected;
}

function unselectAllRows() {
	this.colorRows()
	selectedItems = new Array();
	return;
}

function ResetOriginalRowColor() {
	if (this.reverseColor) {
		var lrowBGColor = this.rowAltBGColor;
		var lrowAltBGColor = this.rowBGColor;
	} else {
		var lrowBGColor = this.rowBGColor;
		var lrowAltBGColor = this.rowAltBGColor;
	}
	if (is.ie) {
		for(var i=0; i<this.table.children.length; i++) {
	    if (i == 0 && this.hasHeader) continue;	  
			if (i % 2 == 0)	this.table.children(i).style.backgroundColor = lrowBGColor
			else this.table.children(i).style.backgroundColor = lrowAltBGColor
		}
	} else if (is.ns6) {
		for (var i=0; i<this.table.rows.length; i++) {
	    if (i == 0 && this.hasHeader) continue;	  
			if (i % 2 == 0)	this.table.rows[i].bgColor = lrowBGColor
			else this.table.rows[i].bgColor = lrowAltBGColor
		}
	}
}

function GetOriginalRowColor(RowId) {
	var lrowColor
	var lRowId = RowId;
	if (this.reverseColor) lRowId++
	if ((lRowId) % 2 == 0) lrowColor = this.rowBGColor;
	else lrowColor = this.rowAltBGColor;
	return lrowColor;
}
