/*
	Created by Erik Herrington 3/16/2007
	Modified by Rehan Qureshy 4/11/2008

	To use:
	1) Create an empty table called tblPerformance.  This is where the chart will be rendered.
	2) Create one row inside the table.  This row can have column width information for the rest of the table.
	3) For each column call AddColumn
	4) For each row call AddRow
*/

var aColName = new Array();
var aIsColVisible = new Array();	// non-visible columns are used for filtering purposes
var aFormatFunction = new Array();
var aStyleName = new Array();

var aTableRows = new Array();		// 2D Array of all the rows in the table - aTableRows[Row][Col]
					// aTableRows[Row]["isVisible"] filters individual rows
var nSortCol = -1;
var sSortDir;
var iRenderedRows = 0;
var selFlagPerf = false;

// These params should be overridden for customization
var sUpArrow = "sort_up_arrow.gif";
var sDownArrow = "sort_down_arrow.gif";
var titleCounter = 0;
var titleFlag = false;

function AddColumn(sColName, isVisible, sFormatFunction, className) {
	// Creates a new range type
	aColName.push(sColName);
	aIsColVisible.push(isVisible);
	aFormatFunction.push(sFormatFunction);
	aStyleName.push(className);
}

function AddRow() {
	// Adds each parameter to an object and sets up
	var aTableRow = new Array();
	if (aColName.length != AddRow.arguments.length) {
		alert("Invalid number of params sent to child row.");
	}

	for (i=0; i < AddRow.arguments.length; i++) {
		//aTableRow[aAttrName[i]] = AddRow.arguments[i];
		aTableRow.push(AddRow.arguments[i]);
	}
	aTableRow["isVisible"] = false;

	aTableRows.push(aTableRow);
}

function AddSubheader(fnName, stCategory, stClass) {
	var aTableRow = new Array();
	aTableRow["isFunction"] = "function";
	aTableRow["isVisible"] = false;
	aTableRow[0]=stCategory;
	aTableRow[1]=stClass;
	aTableRow.push(fnName);
	aTableRows.push(aTableRow);
}

function RenderTable(strNoSelectionMsg) {
	titleCounter = 0;
	titleFlag = false;
	
	var table = document.getElementById("tblPerformance");

	// Clear the table before re-rendering
	//for (row=0; row < iRenderedRows; row++) {
	//	table.deleteRow(1);
	//}

	newRow = table.insertRow(iRenderedRows);
	//alert(iRenderedRows);
	//iRenderedRows = 1;
	//newRow.className = "bgMain";
	newRow.setAttribute('align','center');
	newRow.setAttribute('valign','top');
	newRow.setAttribute('id','Perfrow1');
	for (col=0; col < aColName.length; col++) {
		if (aIsColVisible[col]) {
			newCol = newRow.insertCell(-1);
			if (col == 1) {
				newCol.setAttribute('align','left');
			}
			newCol.className = aStyleName[col];
			newCol.innerHTML = aColName[col];
		}
	}
	var tdColor = "Interior";
	var tdSubC = "Interior";
	var tdSwitchC = "Interior";

	for (row=0; row < aTableRows.length; row++) {
		if (aTableRows[row]["isFunction"] == "function") {
			if (aTableRows[row]["isVisible"]) {
				eval(aTableRows[row][2]);
			}
		} else if (aTableRows[row]["isVisible"]) {
			iRenderedRows++;
			newRow = table.insertRow(iRenderedRows);
			newRow.setAttribute("align","center");
			newRow.setAttribute("valign","top");
			newRow.vAlign="top";
			//alert("i="+iRenderedRows);
			if (tdColor == "Interior") {
				tdColor = "White";
				tdSubC = "Column";
				tdSwitchC = "White";
			} else {
				tdColor = "Interior";
				tdSubC = "Interior";
				tdSwitchC = "Interior";
			}
			
			for (col=0; col < aColName.length; col++) {
				if (aIsColVisible[col]) {
					newCol = newRow.insertCell(-1);
					//alert(col);
					if (col == 2) {
						newCol.className="bg" + tdColor;
						newRow.setAttribute("width","4");
					} else if (col == 3) {
						newCol.className="perfTable" + tdColor + "Col";
						newCol.setAttribute("align","left");
					} else if (col == 8) {
						newCol.className="perfTable" + tdSubC + "Col";
						if (tdSubC == "Column") {
							if (tdSwitchC == "Column") {
								tdSwitchC = "White";
							} else {
								tdSwitchC = "Column";
							}
						}
					} else if (col == 13) {
						newCol.className="bg" + tdColor;				
					} else {
						if (tdSubC == "Column") {
							if (tdSwitchC == "Column") {
								tdSwitchC = "White";
							} else {
								tdSwitchC = "Column";
							}
						} 
						newCol.className=" perfTable" + tdSwitchC;
					}
									
					//alert('b');
					displayVal = aTableRows[row][col];
					//alert('c');
					newCol.innerHTML = displayVal;
					//alert('d');
				}
			}
		}
	}
	if (titleFlag) {
		table.deleteRow(-1);
		iRenderedRows--;
		table.deleteRow(-1);
		iRenderedRows--;		
	}
	if (!selFlagPerf) {
		noInfo(strNoSelectionMsg);
		document.getElementById("Perfrow1").style.display = "none";
	}
}

function SortCol(nColNum) {
	// sorts the given column
	if (nSortCol == nColNum) {
		if (sSortDir == sUpArrow) {
			sSortDir = sDownArrow;
		} else {
			sSortDir = sUpArrow;
		}
	} else {
		sSortDir = sUpArrow;
		nSortCol = nColNum;
	}
	aTableRows.sort(subSort);

	RenderTable();
}
function subSort(x, y) {
	// function that does the comparing between rows in a column
	str1 = x[nSortCol];
	str2 = y[nSortCol];

	if (str1 == "") {
		if (str2 == "") {
			sort = 0;
		} else {
			sort = 1;
		}
	} else if (str2 == "") {
		sort = -1;
	} else if (sSortDir == sDownArrow) {
		sort = compare(str1, str2) * -1;
	} else {
		sort = compare(str1, str2);
	}
	//alert(str1 + ">" + str2 + "=" + sort);

	return sort;
}

function compare(x, y) {
	// Compares 2 strings x and y
	// This comparison is case insensitive and treats actual numbers as numbers
	var nullvalx = false;
	var nullvaly = false;
	if (x == "" || x == null) nullvalx = true;
	if (y == "" || y == null) nullvaly = true;
	if (nullvalx || nullvaly) {
		if (nullvalx && nullvaly) return 0;
		// Always sort nulls to the bottom of the list
		if (nullvalx) {
			if (sSortDir == sDownArrow) return -1;
			return 1;
		} else {
			if (sSortDir == sDownArrow) return 1;
			return -1;
		}
	}

	if (isDecimalNumber(x)) {
		x = (x * 1.0);
	} else {
		x = x.toUpperCase();
	}
	if (isDecimalNumber(y)) {
		y = (y * 1.0);
	} else {
		y = y.toUpperCase();
	}

	if (x > y) return 1;
	if (x < y) return -1;
	return 0;
}

function isDecimalNumber(sString) {
	// accepts numerals and a single decimal only
	sCompare = "0123456789.";

	for (x = 0; x < sString.length; x++) {
		c = sString.charAt(x);
		if (sCompare.indexOf(c) == -1) return false;
	}
	if (sString.split(".").length > 2) return false;
	return true;
}

function AddHeaderPerf(text1, text2) {
	var headerRow=document.getElementById('tblPerformance').insertRow(iRenderedRows);
	headerRow.align="center";
	var col1=headerRow.insertCell(0);
	col1.className="perfTableSub";
	col1.setAttribute("width","4");
	var col2=headerRow.insertCell(1);
	col2.className="perfTableSubCol";
	col2.setAttribute("width","20%");
	var col3=headerRow.insertCell(2);
	col3.className="perfTableSubCol";
	col3.colSpan=5;
	var col4=headerRow.insertCell(3);
	col4.className="perfTableSub";
	col4.colSpan=5;
	col1.innerHTML="";
	col2.innerHTML="";
	col3.innerHTML=text1;
	col4.innerHTML=text2;
	iRenderedRows++;
	//alert('iRenderedRows='+iRenderedRows);
}

function AddSubHeaderingPerf(text1, text2) {
	iRenderedRows++;
	var headerRow=document.getElementById('tblPerformance').insertRow(iRenderedRows);
	headerRow.className="bgName";
	var col1=headerRow.insertCell(0);
	col1.className="bgName";
	col1.setAttribute("width","4");
	var col2=headerRow.insertCell(1);
	col2.className="perfTableNameCol";
	var col3=headerRow.insertCell(2);
	col3.className="perfTableNameCol";
	col3.colSpan=5;
	var col4=headerRow.insertCell(3);
	col4.colSpan=5;
	col1.innerHTML="";
	col2.innerHTML=text1;
	col3.innerHTML=text2;
	col4.innerHTML="";
	//iRenderedRows++;
}

function AddSubFooterPerf(text1) {
	iRenderedRows++;
	titleCounter++;
	titleFlag = false;

	var headerRow=document.getElementById('tblPerformance').insertRow(iRenderedRows);
	headerRow.className="bgWhite";
	var col1=headerRow.insertCell(0);
	col1.className="bgWhite";
	col1.setAttribute("width","4");
	var col2=headerRow.insertCell(1);
	col2.className="bgWhite";
	col2.colSpan=11;
	col1.innerHTML="";
	col2.innerHTML=text1;
	
	if (titleCounter == "2") {
		iRenderedRows++;

		var table = document.getElementById("tblPerformance");

		newRow = table.insertRow(iRenderedRows);
		newRow.className="perfTableWhiteBot";
		col1=newRow.insertCell(0);
		col1.className="perfTableWhiteBot";
		col1.colSpan=12;		
		col1.innerHTML="&nbsp;";

	
	iRenderedRows++;
						
	
		newRow = table.insertRow(iRenderedRows);
		newRow.setAttribute('align','center');
		newRow.setAttribute('valign','top');
		newRow.setAttribute('id','Perfrow1');
		for (col=0; col < aColName.length; col++) {
			if (aIsColVisible[col]) {
				newCol = newRow.insertCell(-1);
				if (col == 1) {
					newCol.setAttribute('align','left');
				}
				newCol.className = aStyleName[col];
				newCol.innerHTML = aColName[col];
			}
		}
		titleCounter = 0;
		titleFlag = true;
	}
}

//The following is a sample filter statement for hiding rows.  Extend as needed.
function FilterRange(nCol, sRangeMin, sRangeMax) {
	// Marks rows hidden in the given column if the value in the column falls in between the min and max
	for (row=0; row < aTableRows.length; row++) {
		cellVal = aTableRows[row][nCol];
		if (compare(cellVal, sRangeMin) < 0 || compare(cellVal, sRangeMax) > 0) {
			aTableRows[row]["isVisible"] = false;
		}
	}
}

function filterResultPerf (objCat, objClass) {
	var flag = false;
	var tmpId = "";
	var tmpSub1 = "";
	var tmpSub2 = "";
	selFlagPerf = false;
	if (typeof objCat != "undefined" && typeof objClass != "undefined") {	
		for (i=0; i < objCat.length; i++) {
			if (objCat[i].checked) {
				for(j=0; j < aTableRows.length; j++) {
					//check categories
					if (objCat[i].value == aTableRows[j][0]) {
							if (aTableRows[j][1] == "-1") {
								aTableRows[j]["isVisible"] = true;
								flag = false;
								tmpId = j;
								tmpSub1 = "";
								tmpSub2 = "";
								continue;
							} else if (aTableRows[j][1] == "-2") {
								//aTableRows[j]["isVisible"] = true;					
								if (tmpSub1 == "") {
									tmpSub1 = j;
								} else {
									tmpSub2 = j;
								}
								continue;
							} else if (aTableRows[j][1] == "-3") {
								if (!flag) {
									aTableRows[j]["isVisible"] = false;
								} else {
									aTableRows[j]["isVisible"] = true;
								}
								continue;
							} else {
								for (k=0; k < objClass.length; k++) {
									if (objClass[k].checked) {
										if(aTableRows[j][1]=="Class R1" || aTableRows[j][1]=="Class R2"
									    || aTableRows[j][1]=="Class R3"){
										    aTableRows[j][1]="Class R";
									    }
										if (objClass[k].value == aTableRows[j][1]) {
											aTableRows[j]["isVisible"] = true;
											if (!flag) {
												//alert(j);
												aTableRows[tmpId]["isVisible"] = true;
												flag = true;
												selFlagPerf = true;
											}
										}
										if (tmpSub1 != "") {
											aTableRows[tmpSub1]["isVisible"] = true;
										}
										if (tmpSub2 != "") {
											aTableRows[tmpSub2]["isVisible"] = true;
										}										
									}
								}
								if (!flag) {
									aTableRows[tmpId]["isVisible"] = false;
									if (tmpSub1 != "") {
										aTableRows[tmpSub1]["isVisible"] = false;
									}
									if (tmpSub2 != "") {
										aTableRows[tmpSub2]["isVisible"] = false;
									}		
								}
							}
					}
				}
			}
		}
	}
}

function clearTablePerf() {
	var table = document.getElementById("tblPerformance");

	// Clear the table before re-rendering
	for (row=0; row < iRenderedRows+1; row++) {
		table.deleteRow(-1);
	}
	iRenderedRows=0;
	ClearFilters();
}

function ClearFilters() {
	for (row=0; row< aTableRows.length; row++) {
		aTableRows[row]["isVisible"] = false;
	}
}

function noInfo(text1) {
	iRenderedRows++;
	var headerRow=document.getElementById('tblPerformance').insertRow(iRenderedRows);
	headerRow.className="bgWhite";
	var col1=headerRow.insertCell(0);
	col1.className="bgWhite";
	col1.setAttribute("width","4");
	var col2=headerRow.insertCell(1);
	col2.className="bgWhite";
	col2.colSpan=11;
	col1.innerHTML="";
	col2.innerHTML=text1;
}