﻿// ===============================================
// 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;
	pobj.removeRow 		= grid_removeRow;
	pobj.insertRow 		= grid_insertRow;
	pobj.moveRowUp 		= grid_rowUp;
	pobj.moveRowDown 	= grid_rowDown;
	
	if (pType){
		pobj.setRowType(pType);
	} else {
		pobj.setRowType(0);
	}
	
	//default
	if (isNaN(pDefaultSortIndex)) {
		pobj.rerow();
	} else {
		pobj.sortByColumn(pDefaultSortIndex);
	}
}

function grid_rowUp(idx)
{
	var t = this.removeRow(idx);
	this.insertRow(idx - 1, t);
}

function grid_rowDown(idx)
{
	var t = this.removeRow(idx);
	this.insertRow(idx + 1, t);
}

function grid_removeRow(idx)
{
	
	var tb = this.getElementsByTagName("tbody")[0];
	var trs = tb.getElementsByTagName("tr");
	
	this.rerow();
	
	return tb.removeChild(tb.getElementsByTagName("tr")[idx]);
	
	
}

function grid_insertRow(idx, t)
{
	var a = new Array();
	
	var tb = this.getElementsByTagName("tbody")[0];
	var trs = tb.getElementsByTagName("tr");
	
	for (var i = trs.length - 1; i >= idx; i--)
	{
		a.push(trs[idx]);
		this.removeRow(idx);
	}
		
	tb.appendChild(t);
	
	for (var i = 0; i < a.length; i++)
		tb.appendChild(a[i]);
	
	this.rerow();
	
}

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];
					
					td.className = td.className.replace("sorton ", "");
					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;
						img.style.marginLeft = "16px";
						(this.sortOrder == "desc") ? img.src = "/_images/grid/sortarrow_desc.gif" : img.src = "/_images/grid/sortarrow_asc.gif" ;
						td.appendChild(img);
						td.className = "sorton " + td.className;
					}
				}
			}
		}
	}
	
	//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;
	}
	
	// fewer than 2 rows...exit cleanly
	var z = 0;
	for (var i = 0; i < tbody.childNodes.length; i++){
		if (tbody.childNodes[i].tagName == "TR") {
			z++;
		}
	}
	if (z <= 1) {
		//re-color the rows
		this.rerow();
		return true;
	}
		
	//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 {
					if (td.innerText)
					{
						a = new Array (td.innerText.toLowerCase(), tr); //FF
					}
					else
					{
						a = new Array ("", 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;
}

