/*
@depends prototype_1_5_0.js
@depends ofDomFunctions.js
*/

function setAlternatingColors(){
	var altColorTables = document.getElementsByClassName("ofAlternatingRowColor");
	for (i=0; i<altColorTables.length; i++){
		tableRows = altColorTables[i].getElementsByTagName("TR");
		for (j=0; j<tableRows.length; j++){
			Element.removeClassName(tableRows[j],"ofAlternating");
			if (j % 2 == 0){
				Element.addClassName(tableRows[j],"ofAlternating");
			}
		}
	}
}

function setDataType(cValue){ // THIS FUNCTION CONVERTS DATES AND NUMBERS FOR PROPER ARRAY SORTING WHEN IN THE SORT FUNCTION
var isDate = new Date(cValue);
if (isDate == "NaN" || isDate == "Invalid Date"){

    if (isNaN(cValue)){ // THE VALUE IS A STRING, MAKE ALL CHARACTERS IN STRING UPPER CASE TO ASSURE PROPER A-Z SORT
        cValue = cValue.toUpperCase();
		return cValue;
		
      }else{
        // VALUE IS A NUMBER, TO PREVENT STRING SORTING OF A NUMBER ADD AN ADDITIONAL DIGIT THAT IS THE + TO THE LENGTH OF THE NUMBER WHEN IT IS A STRING
        var myNum;
        myNum = String.fromCharCode(48 + cValue.length) + cValue;
		return myNum;
      }
	  
    }else{

    // VALUE TO SORT IS A DATE, REMOVE ALL OF THE PUNCTUATION AND AND RETURN THE STRING NUMBER
    // BUG - STRING AND NOT NUMERICAL SORT .....
    // ( 1 - 10 - 11 - 2 - 3 - 4 - 41 - 5  etc.)
    var myDate = new String();
    myDate = isDate.getFullYear() + " " ;
    myDate = myDate + isDate.getMonth() + " ";
    myDate = myDate + isDate.getDate(); + " ";
    myDate = myDate + isDate.getHours(); + " ";
    myDate = myDate + isDate.getMinutes(); + " ";
    myDate = myDate + isDate.getSeconds();
    //myDate = String.fromCharCode(48 + myDate.length) + myDate;
	return myDate ;
  }
}





function sortTable(theEle, theTable){

var theSortCell = getParent(theEle,"TH");
var theSortRow = getParent(theEle,"TR");
var iCol = theSortCell.cellIndex;
var tbody = theTable.getElementsByTagName("TBODY")[0];
var totalRows = tbody.rows.length;
var totalCols = tbody.rows[0].cells.length;
var colTags = theTable.getElementsByTagName("COL");
var bSort = 0;
var colArray = new Array();
var oldIndex = new Array();
var bArray = new Array();
var newRow;
var newCell;
var i;
var c;
var j;
	

for (i=0; i < totalCols; i++){

	if (i == iCol){
	
		Element.addClassName(colTags[i],"ofHighlight");
		
		var oSortIcon = document.getElementsByClassName("ofSortIcon",theSortRow.cells[i]);
		
		if (oSortIcon.length > 0){
			
			if (oSortIcon[0].src.indexOf("down_icon.gif") > -1){
			
				oSortIcon[0].src = "/static/common/images/up_icon.gif";
				oSortIcon[0].alt = "Sort Descending";
			
			}else{
			
				oSortIcon[0].src = "/static/common/images/down_icon.gif";
				oSortIcon[0].alt = "Sort Ascending";
			
			}
		
		}else{
		
			var img = document.createElement('img');
			img.className = "ofSortIcon";
			img.src = "/static/common/images/down_icon.gif";
			img.alt = "Sort Ascending";
			theEle.appendChild(img);
		
		}
		
		
		
	}else{

		Element.removeClassName(colTags[i],"ofHighlight");
		
		var theIcons = document.getElementsByClassName("ofSortIcon",theSortRow.cells[i]);
		
		if (theIcons.length > 0){
		theIcons[0].removeNode(false) // This doesn't work in firefox
		}
	}
}


    // ** POPULATE THE ARRAY colArray WITH CONTENTS OF THE COLUMN SELECTED
    for (r=0; r < totalRows; r++){
        Element.removeClassName(tbody.rows[r],"ofLastChild");
		colArray[r] = setDataType(tbody.rows[r].cells[iCol].innerHTML);
    }

    // ** COPY ARRAY FOR COMPARISON AFTER SORT
    for (i=0; i < colArray.length; i++){
        bArray[i] = colArray[i];
    }

	colArray.sort();


for (i=0; i < colArray.length; i++){ // LOOP THROUGH THE NEW SORTED ARRAY
	for(j=0; j < bArray.length; j++){ // LOOP THROUGH THE OLD ARRAY
		if (colArray[i] == bArray[j]){  // WHEN THE ITEM IN THE OLD AND NEW MATCH, PLACE THE
		// CURRENT ROW NUMBER IN THE PROPER POSITION IN THE NEW ORDER ARRAY SO ROWS CAN BE MOVED...
		// MAKE SURE CURRENT ROW NUMBER IS NOT ALREADY IN THE NEW ORDER ARRAY
			for (c=0; c<i; c++){
			if (oldIndex[c] == (j)){bSort = 1}
			}
			
			if (bSort == 0){oldIndex[i] = (j)}
			
			bSort = 0;
		}
	}
}



var oSortIcon = document.getElementsByClassName("ofSortIcon",theSortRow.cells[i]);

if (oSortIcon.length > 0){

	// ORDER THE NEW ROWS
	if (oSortIcon[0].src.indexOf("down_icon.gif") > -1){

	for (i=0; i<oldIndex.length; i++){ // Sort Ascending
		tbody.appendChild(tbody.rows[oldIndex[i]].cloneNode(true))
	}
  
	}else{
	
	for (i=oldIndex.length-1; i>-1; i--){ // Sort Descending
		tbody.appendChild(tbody.rows[oldIndex[i]].cloneNode(true))
	}
	
	}

} else {

	for (i=oldIndex.length-1; i>-1; i--){ // Sort Descending
		tbody.appendChild(tbody.rows[oldIndex[i]].cloneNode(true))
	}

}



//DELETE THE OLD ROWS FROM THE TOP OF THE TABLE ....
for (i=0; i<totalRows; i++){
	tbody.deleteRow(0);
}

// Designate the last-child row...
setAlternatingColors();
Element.addClassName(tbody.rows[totalRows - 1],"ofLastChild");
}

