//========================================
// Sort table
// Original source code was taken from http://javascript.internet.com/forms/sort-data-table.html
// Changed by: Gideon Ben dor
// sortTable(tableToSort, col, rowOffset, colType, startRow, rowsPerItem)
//		tableToSort = Name of table
//		col = Column number to sort
//		rowOffset = Row number in item row
//		colType = 0=String, 1=Numeric, 2=Date
//		startRow = The first row to begin sort (not include titles)
//		rowsPerItem = Number of rows per item
//      colInt = Column number for internal sort
//		colIntType = 0=String, 1=Numeric, 2=Date
//		bottomRows = Number of rows to leave in the bottom of the table
//========================================

var _sortDown = false;		// true: sort in descending order

// THIS FUNCTION CONVERTS DATES AND NUMBERS FOR PROPER ARRAY
// SORTING WHEN IN THE SORT FUNCTION
function setDataType(cValue, colType)
{
	var rc = cValue;
	//alert(rc)
	if (colType == 0)
	{
		rc = MakeUpper(rc);
		return rc;
	}
	else if (colType == 1) {
		var s = "";
		var isNumeric = false;
		for (var i = 0; i < cValue.length; i++) {
			var c = cValue.substr(i,1);
			if (c >= '0' && c <= '9' || c == '+' || c == '-') {
				s += c;
				isNumeric = true;
			}
		}
		if (isNumeric)
			rc = parseInt(s);	
		else
			rc = 0;
		return rc;
	}
	else if (colType == 2) {
		if (cValue.length == 8) // DD-MM-YY
			rc = cValue.substr(6,2) + cValue.substr(3,2) + cValue.substr(0,2);
		else if (cValue.length == 9) {	// DD-MMM-YY
			var monthStr = cValue.substr(3,3);
			var mm = 1;
			for (var i = 1; i <= 12; i++) {
				if (monthStr == monthName[i].substr(0,3)) {
					mm = i;
					break;
				}
			
			}
			rc = cValue.substr(7,2) + mm + cValue.substr(0,2);
		}
		else if (cValue.length == 10) {	// DD/MM/YYYY
			rc = cValue.substr(6,4) + cValue.substr(3,2) + cValue.substr(0,2);
		}
		return rc;
	}
	return rc;
}

function sortCompate(x, y)
{
	if (!_sortDown) {
		if (x > y)
			return 1
		else if (x < y)
			return -1;
	}
	else {
		if (x > y)
			return -1
		else if (x < y)
			return 1;
	}
	return 0;
}

//Sort table that contain one inner table for each row
function sortDivWithInnerTables(divToSort,innerTableRow,innerTableCol,innerTableColType)
{
	try{
	// Check whether it's viewed by IE 5.0 or greater
	if (!checkBrowser()) 
		return;
	// Set the cursor to busy mode
	document.body.style.cursor = "wait";
	var tables = new Array();
	tables = divToSort.getElementsByTagName("table");
	var colArray = new Array();
	var rowArray = new Array();
	var colArrayFirst = new Array();
	var rowArrayFirst = new Array();
    var bArray = new Array();
    var bArrayFirst = new Array();
    var i, j, c, rowIndex,numCols, dest;
    var innerTbl;
    var totalRows = tables.length;
    var bSort = 0;
    var innerStr = "";
	var numOfAlwaysOnTop  =0
	// POPULATE THE ARRAY colArray WITH CONTENTS OF THE COLUMN SELECTED
	for (i = 0; i < totalRows ;i++)
    {
		if( (tables[i].parentElement == divToSort) /*& (eval(tables[i].rows[i].cells(4).innerText)!=0)*/)
		{
				
				innerTbl = tables[i];
				if(innerTbl.rows[innerTableRow].cells(4).innerText==0){
					rowArrayFirst[i] = tables[i].outerHTML;		
					colArrayFirst[i] = setDataType(innerTbl.rows[innerTableRow].cells(innerTableCol).innerText, innerTableColType);
					numOfAlwaysOnTop += 1
				}
		}
	}
    for (i = 0; i < totalRows ;i++)
    {
		//Check if the table belong to the div
		if( (tables[i].parentElement == divToSort) /*& (eval(tables[i].rows[i].cells(4).innerText)!=0)*/)
		{
				innerTbl = tables[i];
				if(innerTbl.rows[innerTableRow].cells(4).innerText!=0){
					rowArray[i] = tables[i].outerHTML;		
					colArray[i] = setDataType(innerTbl.rows[innerTableRow].cells(innerTableCol).innerText, innerTableColType);
				}
		}
	}	
	// COPY ARRAY FOR COMPARISON AFTER SORT
    for (i = 0; i < colArray.length; i++)
		bArray[i] = colArray[i];
	for (i = 0; i < colArray.length; i++)
		bArrayFirst[i] = colArrayFirst[i];
	// SORT THE COLUMN ITEMS
	colArrayFirst.sort(sortCompate);
    colArray.sort(sortCompate);
    
    for (i = 0; i < colArrayFirst.length; i++)
    {
		for(j = 0; j < bArrayFirst.length; j++)
		{
			//Find row and add it to the end of the table
			if (bArrayFirst[j] && colArrayFirst[i] == bArrayFirst[j])
			{  
				innerStr = innerStr + rowArrayFirst[j];
				
				//divToSort.innerHTML = divToSort.innerHTML + rowArray[j]
				bArrayFirst[j] = null;
				break;
			}
		} 
	} 
	if(colArrayFirst.length){
		innerStr = innerStr + "<div class='HotelSeparate' style='width:720px;height:17px;'></div>"
	}
	
	//innerStr = innerStr + "<br><table width='720' cellpadding=0 cellspacing=0 class='HotelSeparate'><tr><td height=7></td></tr></table><br>"      
    // LOOP THROUGH THE NEW SORTED ARRAY
    for (i = 0; i < colArray.length; i++)
    {
		// LOOP THROUGH THE OLD ARRAY
        for(j = 0; j < bArray.length; j++)
        {
			//Find row and add it to the end of the table
			if (bArray[j] && colArray[i] == bArray[j])
            {  
				innerStr = innerStr + rowArray[j];
				//divToSort.innerHTML = divToSort.innerHTML + rowArray[j]
				bArray[j] = null;
				break;
            }
        }
    }
    divToSort.innerHTML =innerStr
    }
	catch (e){ }
	document.body.style.cursor = "";
	var sepTable = document.getElementById("sepTable");
	if(sepTable){
		sepTable.style.display="none";
	}
}

function sortTable(tableToSort, col, rowOffset, colType, startRow, rowsPerItem , colInt , colIntType , bottomRows)
{
	// Check whether it's viewed by IE 5.0 or greater
	if (!checkBrowser()) 
		return;
	//alert(tableToSort.rows.length);
    var totalRows = tableToSort.rows.length - bottomRows;
    var bSort = 0;
    var colArray = new Array();
    var oldIndex = new Array();
    var bArray = new Array();
    var newRow, newCell;
    var i, j, c, rowIndex,rowCount, numCols, dest;
	var data1,data2,intData1,intData2,contFlag,temp;
    
	document.body.style.cursor = "wait";
	_sortDown = tableToSort.sortDown;
    // POPULATE THE ARRAY colArray WITH CONTENTS OF THE COLUMN SELECTED
    rowCount = 0;
    for (i = startRow; i < tableToSort.rows.length - bottomRows; i += rowsPerItem)
    {
		colArray[rowCount] = setDataType(tableToSort.rows[i+rowOffset].cells(col).innerText, colType);
		rowCount++;
	}
    // COPY ARRAY FOR COMPARISON AFTER SORT
    for (i = 0; i < colArray.length; i++)
		bArray[i] = colArray[i];
      
    // SORT THE COLUMN ITEMS
    colArray.sort(sortCompate);
    
    // LOOP THROUGH THE NEW SORTED ARRAY
    for (i = 0; i < colArray.length; i++)
    {
		// LOOP THROUGH THE OLD ARRAY
        for(j = 0; j < bArray.length; j++)
        {
			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
				var idx = j*rowsPerItem+startRow;
                for (c = 0; c < i; c++)
                {
					if ( oldIndex[c] == idx)
	                    bSort = 1;
                }
                if (bSort == 0)
					oldIndex[i] = idx;
                bSort = 0;
            }
        }
    }
	// ** MAKE THE INTERNAL SORT 
	if( col != colInt && colInt != -1)
	{
		for (i = colArray.length ; i > 0 ; i--) 
		{
			contFlag = false;
			for (j = 0; j < (i - 1) ; j++) 
			{
			
				//READ MAIN AND SECONDARY VALUES FROM TWO SEQUENTIAL ROWS
				data1 = setDataType(tableToSort.rows[oldIndex[j]].cells(col).innerText, colType);
				data2 = setDataType(tableToSort.rows[oldIndex[j+1]].cells(col).innerText, colType);
				intData1 = setDataType(tableToSort.rows[oldIndex[j]].cells(colInt).innerText, colIntType);
				intData2 = setDataType(tableToSort.rows[oldIndex[j+1]].cells(colInt).innerText, colIntType);
			
				if( data1 == data2 && intData1 > intData2 )
				{	
					contFlag = true; // Turn on the 'continue the sorting' flag
					//SWAP THE ROWS
					temp = oldIndex[j];
					oldIndex[j] = oldIndex[j+1];
					oldIndex[j+1] = temp;
				}
			
			}
			if( contFlag == false )
				break;
		}
	}
	// ** SORTING COMPLETE, SWITCH BETWEEN ROWS POSITIONS
	for (i = 0; i < oldIndex.length; i++)
	{
		dest = i*rowsPerItem+startRow;
		if (oldIndex[i] > dest) {
			for (j = 0; j < rowsPerItem; j++) {
				rowIndex = oldIndex[i]+j;
				tableToSort.moveRow(rowIndex,dest+j);
			}
			for (j = 0; j < oldIndex.length; j++) {
				if (oldIndex[j] < oldIndex[i])
					oldIndex[j] += rowsPerItem;
			}
		}
	}
	
	document.body.style.cursor = "";
    return false;
}

function checkBrowser()
{
	if (navigator.appName == "Microsoft Internet Explorer"
		&& parseInt(navigator.appVersion) >= 4)
	{
		return true;
	}
	// For some reason, appVersion returns 5 for Netscape 6.2
	else if (navigator.appName == "Netscape"
		&& navigator.appVersion.indexOf("5.") >= 0)
	{
		return true;
	}
	else
		return false;
}
