// ===============================================
// grid type 0 - alternates row class (default)
// grid type 1 - row class only alternates when 
//				 the sorted column's text changes
//================================================
function extendGrid(pobj, pType, pDefaultSortIndex){
	pobj.sortByColumn = grid_sortByColumn;
	pobj.rerow		  = grid_rerow;
	pobj.setRowType	  = setRowType;
	
	if (pType){
		pobj.setRowType(pType);
	} else {
		pobj.setRowType(0);
	}
	
	//default
	if (isNaN(pDefaultSortIndex)) {
		pobj.sortByColumn(0);
	} else {
		pobj.sortByColumn(pDefaultSortIndex);
	}
}
function grid_sortByColumn(pint, type){
	var tbody, thead, tr;
	var sortArray = new Array();
	var tagName;
	var a;
	var tdC; //td counter
	
	if (this.sortColumn == pint){
		if (this.sortOrder == ""){
			this.sortOrder = "desc";
		} else {
			this.sortOrder = "";
		}
	} else {
		this.sortOrder = "";
	}
	this.sortColumn = pint;
	
	//find the thead
	for (var i = 0; i < this.childNodes.length; i++){
		if (this.childNodes[i].tagName == "THEAD"){
			thead = this.childNodes[i];
		}
	}
	if (!thead){
		alert("failed.");
		return false;
	}
	for (var i = 0; i < thead.childNodes.length; i++){
		if (thead.childNodes[i].tagName == "TR") {
			tr = thead.childNodes[i];
			//loop the cells
			tdC = -1;
			for (var j = 0; j < tr.childNodes.length; j++){
				if (tr.childNodes[j].tagName == "TD"){
					tdC++;
					td = tr.childNodes[j];
					for (var k = 0; k < td.childNodes.length; k++){
						if (td.childNodes[k].tagName == "IMG"){
							td.removeChild(td.childNodes[k]);
						}
					}
					if (tdC == pint){
						var img = document.createElement("img");
						img.border = 0;
						(this.sortOrder == "desc") ? img.src = "/images/interface/sortarrow_desc.gif" : img.src = "/images/interface/sortarrow_asc.gif" ;
						td.appendChild(img);
					}
				}
			}
		}
	}
	
	//find the tbody
	for (var i = 0; i < this.childNodes.length; i++){
		if (this.childNodes[i].tagName == "TBODY"){
			tbody = this.childNodes[i];
		}
	}
	if (!tbody){
		alert("ERROR: No table body element found.");
		return false;
	}
	
	//loop the rows to find the trs
	for (var i = 0; i < tbody.childNodes.length; i++){
		if (tbody.childNodes[i].tagName == "TR") {
			tr = tbody.childNodes[i];
			// find the nth TD
			tdC = -1;
			td = null;
			for (var j = 0; j < tr.childNodes.length; j++){
				if (tr.childNodes[j].tagName == "TD"){
					tdC++;
					if (tdC == pint) { td = tr.childNodes[j] };
				}
			}
			// push the value into the sort array
			if (td) {
				if (td.textContent){
					a = new Array (td.textContent.toLowerCase(), tr); //FF
				} else {
					a = new Array (td.innerText.toLowerCase(), tr); //FF
				}
			} else {
				alert("ABORTED: No data cell found for sorting.");
				return false;
			}
			sortArray.push (a);
		}
	}
	
	//re-type the array
	if (type){
		for (var i = 0; i < sortArray.length; i++){
			switch (type){
			case "date":
				var dt = new Date(sortArray[i][0]);
				//add 10 to day and month so that they are all double digits and remain relative for proper ordering.
				sortArray[i][0] = dt.getFullYear() + "-" + (dt.getMonth() + 10) + "-" + (dt.getDate() + 10); 
				break;
			case "integer":
				sortArray[i][0] = parseInt(sortArray[i][0]);
				break;
			}
		}
	}
	
	//sort the array
	if (this.sortOrder == "desc"){
		sortArray.sort(grid_sort);
	} else {
		sortArray.sort();
	}
	
	//replace the tbody in the table
	this.removeChild(tbody);

	//create the new tbody
	tbody = document.createElement("tbody"); 
	
	//put the body back in the table
	this.appendChild(tbody);
	
	//append the rows
	for (var i = 0; i < sortArray.length; i++){
		tbody.appendChild(sortArray[i][1]);
	}
	
	//re-color the rows
	this.rerow();
}

// ----------------------------------
// this is used for DESC sort
function grid_sort(a,b){
	(a > b) ? i = -1 : (b > a) ? i = 1 : i = 0 ;
	return i;
}

function grid_rerow(){
	var tbody;
	var tr;
	var trC, tdC;
	var val = "";
	var cE = "rowEven";
	var cO = "rowOdd";
	var eo; //current even or odd class
	
	//find the tbody
	for (var i = 0; i < this.childNodes.length; i++){
		if (this.childNodes[i].tagName == "TBODY"){
			tbody = this.childNodes[i];
		}
	}
	if (!tbody){
		alert("failed.");
		return false;
	}
	
	trC = -1;
	for (var i = 0; i < tbody.childNodes.length; i++){
		if (tbody.childNodes[i].tagName == "TR") {
			trC++;
			tr = tbody.childNodes[i];
			
			switch (this.rowType){
			case 1 :
				td = null;
				tdC = -1;
				for (var j = 0; j < tr.childNodes.length; j++){
					if (tr.childNodes[j].tagName == "TD"){
						tdC++;
						if (tdC == this.sortColumn) { td = tr.childNodes[j] };
					}
					if (td){
						if (td.textContent){
							if (td.textContent.toLowerCase() != val){
								(eo == cO) ? eo = cE : eo = cO ;
								val = td.textContent.toLowerCase();
							}
						} else {
							if (td.innerHTML.toLowerCase() != val){
								(eo == cO) ? eo = cE : eo = cO ;
								val = td.innerHTML.toLowerCase();
							}
						}
						tr.className = eo;
					}
				}
				break;
			default :
				if (trC % 2){
					tr.className = cE;
				} else {
					tr.className = cO;
				}
				break;
			}
		}
	}
}

function gridOver(obj){
	var n = obj.className;
	obj.className = "over " + n;
}
function gridOut(obj){
	var n = obj.className;
	obj.className = n.replace("over", "");
}
function setRowType(pint){
	this.rowType = pint;
}
