// JavaScript Document
//sort enums
var SortType = { Text:0, Number:1 }
var SortOrder = { Asc:0, Desc:1 }

function GridTable(selfName) {
	this.uid = Math.round(Math.random() * 100000);
	this.border = 0;
	this.spacing = 0;
	this.padding = 0;
	this.width;
	this.style;
	this.alternateColors = false;
	this.oddStyle = "";
	this.evenStyle = "";
	this.selectStyle = ""; //*
	this.instanceName = selfName;
	this.sortFunction = "";
	this.hideFunction = "";
	this.hideHeader = false;
	this.selectedRow;	
	
	var soAsc = "&nbsp;";
	var soDsc = "&nbsp;";
	
	this.Columns = new Array();
	this.Rows = new Array();
	//Retuns an index of a row with matching data value in a column specified
	this.getRowIndex = function(cIdx, value) {
		var idxPos = -1;
		var rLen = this.Rows.length - 1;
		var cRow;
		var i = 0;
		if(rLen > 0) {
			if(cIdx != null && (cIdx >= 0 && cIdx <= this.Columns.length)) {
				do {
					cRow = this.Rows[i];
					if(cRow.columns[cIdx] == value) {
						idxPos = i;
						break;
					}
				} while(i++ < rLen);
			}
		}
		return idxPos;
	}
	//Selects a row
	this.selectRow = function(idx) {
		if(idx != null && (idx >= 0 && idx <= this.Rows.length)) {
			if(this.Rows.length) {
			//Reset all other
				var rLen = this.Rows.length-1;
				do {
					this.Rows[rLen].selected = false;
				}while(rLen--);
			
				this.selectedRow = this.Rows[idx];
				if(this.selectedRow) {
					this.selectedRow.selected = true;
				}
			}
		}
	}	
	//Clears all rows data
	this.clearRows = function() {
		this.Rows = new Array();
	}
	//Clears all columns
	this.clearColumns = function() {
		this.Columns = new Array();
	}
	//Appends Grid Column
	this.appendColumn = function(gcol) {
		var insCol;
		insCol = this.Columns.push(gcol);
		return insCol;
	}
	//Sorts Rows by a column index
	this.sortColumn = function(idx, dir) {
		var tA, tB;
		function sortRows(a, b) {				
			if(isNumeric(a.columns[idx]) && isNumeric(b.columns[idx])) {
				tA = parseFloat(a.columns[idx]);
				tB = parseFloat(b.columns[idx]);
			} else {
				tA = a.columns[idx];
				tB = b.columns[idx];
			}
			var res = (tA < tB)?-1:1;
			if(dir && dir == SortOrder.Desc) {
				res = 0 - res;
			}
			return res;
		}
		//try {
			this.Rows.sort(sortRows);
			this.Columns[idx].sortOrder = dir;
		//} catch(e) {}
	}
	//Sets column to hidden
	this.hideColumn = function(idx) {
		if(this.Columns[idx]) {
			this.Columns[idx].visible = false;
		}
	}
	//Sets column to visible
	this.showColumn  = function(idx) {
		if(this.Columns[idx]) {
			this.Columns[idx].visible = true;
		}
	}
	//Row Object
	var Row = function(arr, sclass, onck, onov, onot, isSel) {
		this.columns=arr; 
		this.style=sclass; 
		this.onClick=onck;
		this.onMouseOver = onov;
		this.onMouseOut = onot;
		this.selected = (!isSel)?false:isSel; ///*
	}
	//Inserts Row (accepts array of values)
	this.insertRow = function(dataArr, style, onck, onov, onot, isSel) {
		var insRow;
		if(dataArr.constructor == Array) {
			insRow = this.Rows.push(new Row(dataArr, style, onck, onov, onot, isSel));
		}
		return insRow;
	}
	//Returns Table grid in HTML format
	var table = document.createElement("table");
	var tr = document.createElement("tr");
	var td = document.createElement("td");
	var img = document.createElement("img");
	var link = document.createElement("a");		
	this.renderGrid = function() {
		//Creating Grid Table
		var tbl = table.cloneNode(false);
		tbl.setAttribute("id", "tbl_" + this.uid); //Set unique table identifier
		if(this.border != null)tbl.setAttribute("border", this.border);
		if(this.spacing != null)tbl.setAttribute("cellspacing", this.spacing);
		if(this.padding != null)tbl.setAttribute("cellpadding", this.padding);
		if(this.width != null)tbl.setAttribute("width", this.width);
		if(this.style != null)tbl.className = this.style;
		//Create Header Row
		if(!this.hideHeader) {
			if(this.Columns.length > 0) {
				var hdrRow = tbl.appendChild(tr.cloneNode(false));
					hdrRow.setAttribute("type", "header");
				var oCol, tCol;
				for(var c = 0; c < this.Columns.length; c++) {
					oCol = this.Columns[c];
					if(oCol.visible) {
						tCol = hdrRow.appendChild(td.cloneNode(false));
						buildColumn(oCol, tCol);
						if(oCol.captionStyle != null)tCol.className = oCol.captionStyle;
						buildHeader(oCol, tCol, c, this.sortFunction, this.hideFunction);
					}
				}
			}
		}
		//Render data rows
		if(this.Rows.length > 0) {
			var rowCount = this.Rows.length-1;
			var r = 0, c = 0;
			var oRow, tRow;
			var oCol, tCol;				
			var colLen = this.Columns.length-1;
			var altStyle = this.evenStyle;
			do {
				oRow = this.Rows[r];
				tRow = tbl.appendChild(tr.cloneNode(false));
				tRow.setAttribute("idx", r);
				if(this.alternateColors) {
					altStyle = (altStyle == this.evenStyle)?this.oddStyle:this.evenStyle;
					tRow.className = altStyle;
				}
				do {
					oCol = this.Columns[c];
					if(oCol.visible) {
						tCol = tRow.appendChild(td.cloneNode(false));
						buildColumn(oCol, tCol);
						if(oRow.style != null) {tCol.className =  oRow.style;}
						if(oRow.selected == true) {
							tCol.className = this.selectStyle;
							tRow.setAttribute("selected", "true");
						}
						if(oRow.onClick != null) { tCol.setAttribute("onClick", oRow.onClick);}
						if(oRow.onMouseOver != null) { tCol.setAttribute("onMouseOver", oRow.onMouseOver);}
						if(oRow.onMouseOut != null) { tCol.setAttribute("onMouseOut", oRow.onMouseOut);}						
						tCol.innerHTML = (oRow.columns[c] != null)?oRow.columns[c]:"&nbsp";
					}
				} while(c++ < colLen);
				c = 0; //Reset position for next row
			} while(r++ < rowCount);
		}
		//Return table grid html
		return getOuterHTML(tbl);
	}	
	//set all attributes for a column
	function buildColumn(col, cell) {
		if(col.attributes.length > 0) {
			var aLen = col.attributes.length - 1;
			var cAtt;
			do {
				cAtt = col.attributes[aLen];
				cell.setAttribute(cAtt.name, cAtt.value);
			} while(aLen--);
		}
	}
		
	function buildHeader(col, cell, idx, sf, hf) {
		var hTbl = table.cloneNode(false);
		hTbl.setAttribute("cellspacing", 0);
		hTbl.setAttribute("cellpadding", 1);
		hTbl.setAttribute("border",0);
		hTbl.setAttribute("width","100%");
		var hr = hTbl.appendChild(tr.cloneNode(false));			
		var titleCell = hr.appendChild(td.cloneNode(false));
		titleCell.setAttribute("nowrap", "yes");
		//titleCell.setAttribute("class", col.captionStyle);
		
		if(col.sortable) {
			//Insert sortability features (i.e. put an up/down arrow)
			var sortCell = hr.appendChild(td.cloneNode(false));
			sortCell.setAttribute("width", 10);
			sortCell.innerHTML = "&nbsp;";
			var sortLink = titleCell.appendChild(link.cloneNode(false));
			sortLink.innerHTML = col.caption;
			sortLink.href = "javascript: " + sf + "(" + idx + "," + col.sortOrder + ")";
			sortCell.innerHTML = (col.sortOrder == SortOrder.Desc)?soDsc:soAsc;
		} else {
			titleCell.innerHTML = col.caption;			
		}
		if(col.hideable) {
			//Insert hide features (i.e. put an x to hide...)
			var hideCell = hr.appendChild(td.cloneNode(false));
			hideCell.setAttribute("width", 10);
			var hideLink = hideCell.appendChild(link.cloneNode(false));
			hideLink.innerHTML = "x";
			hideLink.href = "javascript: " + hf + "(" + idx + ")";
		}
		cell.appendChild(hTbl);
	}
}
	
function GridColumn(capt, width) {
	this.captionStyle;
	this.visible = true;
	this.sortable = false;
	this.hideable = false;
	this.caption = capt;
	this.sortOrder;		
	
	this.attributes = new Array();
	var attr = function(n, v) {this.name = n;this.value = v;}
	//Finds attribute
	this.indexOf = function(n) {
		var attLen = this.attributes.length - 1;
		var idx = 0;
		var cAtt;
		if(attLen >= 0) {
			do {
				cAtt = this.attributes[attLen];
				if(cAtt.name == n) return attLen;
			} while(attLen--);
		}
		return -1;
	}
	//Sets Attribute (overwrites existing)
	this.setAttribute = function(n, v) {
		var idxOf = this.indexOf(n);
		if(idxOf != -1) {
			this.attributes[idxOf].value = v;
		} else {
			this.attributes.push(new attr(n,v));
		}
	}
	//Retrieves Attribute
	this.getAttribute = function(n) {
		var idxOf = this.indexOf(n);
		if(idxOf != -1) {
			return this.attributes[idxOf].value
		} else {
			return null;
		}
	}
	//Removes Attribute
	this.removeAttribute = function(n) {
		var idxOf = this.indexOf(n);
		if(idxOf != -1) {
			this.attributes.splice(idxOf,1);
		}
	}	
	//Creates a copy of itself
	this.copyOf = function(newName, newWidth) {
		var nC = new this.constructor;
		nC.attributes = this.attributes;
		nC.captionStyle = this.captionStyle;
		nC.visible = this.visible = true;
		nC.sortable = this.sortable = false;
		nC.hideable = this.hideable = false;		
		nC.sortOrder = this.sortOrder;		
		nC.caption = newName;
		if(newWidth) {
			nC.setAttribute("width", width);
		}
		return nC;
	}
	if(width) {
		this.setAttribute("width", width);
	}
}
