
this.initGrid = function(){
	
	// CONFIG 
	
	// if set to true then mouseover a table cell will highlight entire column (except sibling headings)
	var highlightcols = false;
	
	// if set to true then mouseover a table cell will highlight entire row	(except sibling headings)
	var highlightrows = true;	
	
	// if set to true then click on a table sell will select row or column based on config
	var selectable = true;
	
	// this function is called when 
	// add your own code if you want to add action 
	// function receives object that has been clicked 
	this.clickAction = function(obj){
		//alert(obj.innerHTML);
	};
	// END CONFIG (do not edit below this line)
	
	var tableover = false;
	this.start = function() {
		var tables = document.getElementsByTagName("table");
		var j=0;
		for (var i=0;i<tables.length;i++) {
			if (tables[i].className == "grid") {
				tables[i].onmouseover = function(){ tableover = true };
				tables[i].onmouseout = function(){ tableover = false };			
				rows(tables[i]);
				j++;
			} // if
		};
	};
	
	this.rows = function(table){
		var css = "";
		if (table.className == "grid") {
			var tr = table.getElementsByTagName("tr");
			for (var i=0;i<tr.length;i++) {
				
				if (tr[i].parentNode.parentNode.className == "grid") {
					//css = (css == "odd") ? "even" : "odd";
					//if (tr[i].className) {
						tr[i].className = tr[i].className; // append alternate class
					//} else {
						//tr[i].className = css; // append alternate class
					//} // if
					var arr = new Array();
					for (var j=0; j<tr[i].childNodes.length; j++){				
						if(tr[i].childNodes[j].nodeType == 1) arr.push(tr[i].childNodes[j]);
					}; // for
					
					for (var j=0; j<arr.length; j++) {				
						arr[j].row = i;
						arr[j].col = j;
						if(arr[j].innerHTML == "&nbsp;" || arr[j].innerHTML == "") arr[j].className += " empty";
						
						var checkboxes = arr[j].getElementsByTagName('input');
						var hasCheckbox = 0;
						for (var k=0;k<checkboxes.length;k++) {
							arr[j].hasCheckbox = 1;
						} // for
						
						arr[j].css = arr[j].className;
		
						arr[j].onmouseover = function() {
							over(table,this,this.row,this.col);
						};
						arr[j].onmouseout = function() {
							out(table,this,this.row,this.col);
						};
						arr[j].onmousedown = function() {
							down(table,this,this.row,this.col);
						};
						arr[j].onmouseup = function() {
							up(table,this,this.row,this.col);
						};				
						arr[j].onclick = function() {
							click(table,this,this.row,this.col);
						};								
					}; // for
				}; // if
			};
		};
	};
	
	// appyling mouseover state for objects (th or td)
	this.over = function(table,obj,row,col){
		if (!highlightcols && !highlightrows) obj.className = obj.css + " over";  
		if (check1(obj,col)) {
			//if (highlightcols) highlightcol(table,obj,col);
			if (highlightrows) highlightrow(table,obj,row);		
		};
	};
	
	// appyling mouseout state for objects (th or td)	
	this.out = function(table, obj, row, col) {
		if (!highlightcols && !highlightrows) obj.className = obj.css; 
		//unhighlightcol(table, col);
		unhighlightrow(table, row);
	};
	
	// appyling mousedown state for objects (th or td)	
	this.down = function(table, obj, row, col){
		obj.className = obj.css + " down";  
	};
	
	// appyling mouseup state for objects (th or td)	
	this.up = function(table, obj, row, col){
		obj.className = obj.css + " over";  
	};
	
	// onclick event for objects (th or td)	
	this.click = function(table, obj, row, col){
		if (check1) {
			if (selectable) {
				unselect(table, row);	
				//if (highlightcols) highlightcol(table,obj,col,true);
				if (highlightrows) highlightrow(table, obj, row, true);
				document.onclick = unselectAll;
			} // if
		};
		clickAction(obj); 		
	};		
	
	/*
	this.highlightcol = function(table, active, col, sel){
		var css = (typeof(sel) != "undefined") ? "selected" : "over";
		var tr = table.getElementsByTagName("tr");
		for (var i=0;i<tr.length;i++){	
			var arr = new Array();
			for(j=0;j<tr[i].childNodes.length;j++){				
				if(tr[i].childNodes[j].nodeType == 1) arr.push(tr[i].childNodes[j]);
			};							
			var obj = arr[col];
			if (check2(active,obj) && check3(obj)) obj.className = obj.css + " " + css; 	
			//if (check2(active,obj) && check3(obj)) obj.className = css + " " + obj.css;
		};
	};
	
	this.unhighlightcol = function(table, col){
		var tr = table.getElementsByTagName("tr");
		for (var i=0;i<tr.length;i++){
			var arr = new Array();
			for(j=0;j<tr[i].childNodes.length;j++){				
				if(tr[i].childNodes[j].nodeType == 1) arr.push(tr[i].childNodes[j])
			};				
			var obj = arr[col];
			if(check3(obj)) obj.className = obj.css; 
		};
	};	
	*/
	
	this.highlightrow = function(table, active, row, sel){
		var css = (typeof(sel) != "undefined") ? "selected" : "over";
		var tr = table.getElementsByTagName("tr")[row];		
		for (var i=0;i<tr.childNodes.length;i++) {		
			var obj = tr.childNodes[i];
			
			if (check2(active, obj) && check3(obj)) { 
				
				// just do hover class on rows that initially have these two classes assigned
				var str1 = "gridRow";
				var str2 = "altRow";
				if (typeof(obj.css) != 'undefined') {
					var currentClass = obj.css;
					if (currentClass.match(str1) || currentClass.match(str2)) {
						obj.className = css + " " + obj.css;
					} // if
				} // if
				

				if (active != obj) { // make sure cell clicked on does not contain checkbox				
					if (css == "selected" && obj.nodeType == 1) { // toggle checkboxes in last cell
						var checkboxInput = obj.getElementsByTagName("input");
						for (var k=0;k<checkboxInput.length;k++) {
							var str3 = "gridEnabled"; // input must have this class assigned
							var currentInputClass = checkboxInput[0].className;
							if (checkboxInput[0].disabled != true && currentInputClass.match(str3)) { 
								checkboxInput[0].checked = (!checkboxInput[0].checked) ? true : false;
							} // if
							
							//if (checkboxes[0].checked == true) {
								
							//} else {
								
							//} // if
						} // for
					} // for
				} // if
			}; // if
		};
	};
	
	this.unhighlightrow = function(table, row){
		var tr = table.getElementsByTagName("tr")[row];		
		for (var i=0;i<tr.childNodes.length;i++) {
			var obj = tr.childNodes[i];			
			if (check3(obj)) obj.className = obj.css; 	 // revert to original class/styles		
		};
	};
	
	this.unselect = function(table, row) {
		var tr = table.getElementsByTagName("tr");
		for (var i=0;i<tr.length;i++){
			for (var j=0;j<tr[i].childNodes.length;j++){
				var obj = tr[i].childNodes[j];	
				if (obj.className) {
					obj.className = obj.className.replace("selected","");
				} // if
			};
		};
	};
	
	this.unselectAll = function() {
		if (!tableover) {
			tables = document.getElementsByTagName("table");
			for (var i=0; i<tables.length; i++){
				if (tables[i].className == "grid") {
					unselect(tables[i]);
				} // if
			};		
		};
	};	
	
	this.check1 = function(obj, col){
		if (typeof(obj) != "undefined") {
			return (!(col == 0 && obj.className.indexOf("empty") != -1));
		} // if
	};
	
	this.check2 = function(active, obj){
		if (typeof(obj) != "undefined") {
			return (!(active.tagName == "TH" && obj.tagName == "TH")); 
		} // if
	};
	
	this.check3 = function(obj) {
		if (typeof(obj) != "undefined") {
			return (obj.className) ? (obj.className.indexOf("selected") == -1) : true; 
		} // if
	};	
	
	start();
	
};

window.onload = initGrid;