/************************************************************************* 
*************************************************************************** 
*  Program Name: $Id:$
*  Program Author:  Michael T. Schock
*  Creation Date: 01-20-2010
*  CVS Revision: $Revision:$
*  Copyright (c) 2010
*
*  Elemental Framework
*
*************************************************************************** 
*************************************************************************** 
*  Program Summary:
*  Javascript functions for the table class
*
*
*************************************************************************** 
**************************************************************************/

//  Config User Functions
/*  These functions are used for the table class.  These calls are
	for when the page first loads.
*/

$(document).ready(
function () 
{
	//  Variables
	var headerStart;
	var headerCurrent;
	var headerWidth;
	var headerLive;
	
	//  Verify there is a table on the page
	if(!$('.table-class-main').length)
		return(0);
		
	
	
});

jQuery.fn.altRowColors = function() {

	//  Set the classes for the odd and even
	$('tbody tr:odd', this).removeClass('table-class-row-alt').addClass('table-class-row');
	$('tbody tr:even', this).removeClass('table-class-row').addClass('table-class-row-alt');

	//  Return for linkage
	return this;
};

function Table_Sort_Column(controlNumber, colIndex)
{
	//  Variables
	var headerId;
	var sortId;
	var tableId;
	var sortDirection;
	var sortFunction;
	var rows;
	var colData;
	
	//  Get the header id
	headerId = 'Table_Header_' + controlNumber + '_' + colIndex;
	sortId = 'Table_Header_Sort_' + controlNumber + '_' + colIndex;
	tableId = 'Table_Main_' + controlNumber;
	
	//  Clear the other sort directions
	$('#' + headerId).siblings().find('span').removeClass('ui-icon-triangle-1-s ui-icon-triangle-1-n').addClass('ui-icon-triangle-2-n-s').attr('title', '');
	
	//  Determine the direction of sort from class in the sort box
	if($('#' + sortId).hasClass('ui-icon-triangle-1-s'))  //  Decending
	{
		//  Set the new class
		$('#' + sortId).removeClass('ui-icon-triangle-1-s').addClass('ui-icon-triangle-1-n').attr('title', 'Ascending');
		
		//  Set the sort direction
		sortDirection = 'Ascending';
	}
	else
	{
		//  Set the new class
		$('#' + sortId).removeClass('ui-icon-triangle-1-n').removeClass('ui-icon-triangle-2-n-s').addClass('ui-icon-triangle-1-s').attr('title', 'Decending');

		
		//  Set the sort direction
		sortDirection = 'Descending';	
	}
	
	//  Get the rows
	rows = $('#' + tableId).find('tbody > tr').get();
	
	//  Get the row length
	rowLength = $('#' + rows[0].id).children('td').length;
	
	//  Clear the hidden data
	Table_Clear_Data(controlNumber, rowLength);
	
	//  Set the key for the row
	$.each(rows, function(index, row) {
		row.sortKey = $(row).children('td').eq(colIndex).text();
		});

	//  Determine the type of function to use for the sort
	sortFunction = Table_Get_Sort_Type(rows[0].sortKey);
	
	//  Perform the sort
	rows.sort(sortFunction);
	
	if (sortDirection == 'Descending')
		rows.reverse();
	
	//  Reset the table
	$.each(rows, function(index, row) {
		$('#' + tableId).children('tbody').append(row);
		row.sortKey = null;
		});
		
	//  Change the coloring for the odd even
	if($('#' + tableId + ' .table-class-row-alt').length)
		$('#' + tableId).altRowColors();
		
	//  Clear the hidden data
	Table_Set_Data(controlNumber, rowLength);
	
	//  Success
	return(0);
}

function Table_Get_Sort_Type(data)
{
	//  Date type (dd-mm-yyyy)
	if (data.match(/^\d\d[\/-]\d\d[\/-]\d\d\d\d$/))
	return(Table_Sort_Date);

	//  Date type (dd-mm-yy)
	if (data.match(/^\d\d[\/-]\d\d[\/-]\d\d$/))
	return(Table_Sort_Date);

	//  Date type (dd-MMM-yy)
	if (data.match(/^\d\d[\/-]\w\w\w[\/-]\d\d$/))
	return(Table_Sort_Date);

	//  Date type (dd-MMM-yyyy)
	if (data.match(/^\d\d[\/-]\w\w\w[\/-]\d\d\d\d$/))
	return(Table_Sort_Date);

	//  Currency type
	if (data.match(/^[?$]/))
	return(Table_Sort_Currency);

	//  Numeric type
	if (data.match(/^[\d\.]+$/))
	return(Table_Sort_Numeric);
	
	//  Set the default column function
	return(Table_Sort_Caseinsensitive);
}

//  Date sorting function
function Table_Sort_Date(a,b)
{
	//  Variables
	var monthName = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
	var monthIndex = '00';

	// y2k notes: two digit years less than 50 are treated as 20XX, greater than 50 are treated as 19XX
	aa = a.sortKey;
	bb = b.sortKey;

	//!  Get the first date
	switch(aa.length)
	{
		case 8:			//  dd-mm-yy
			//  Determine if the year prefix is 19 or 20
			if(aa.substr(6,2) < 50)
				year = '20';
			else
				year = '19';

			//  Set the data for comparison
			date1 = year + aa.substr(6,2) + aa.substr(3,2) + aa.substr(0,2);
			break;
		case 9:			//  dd-MMM-yy
			//  Determine if the year prefix is 19 or 20
			if(aa.substr(7,2) < 50)
				year = '20';
			else
				year = '19';

			//  Get the month index
			for(loop = 0; loop < monthName.length; loop++)
			{
				if(aa.substr(3,3).toUpperCase() == monthName[loop].toUpperCase())
				{
					if(loop < 10)
						monthIndex = '0' + loop;
					else
						monthIndex = loop;
				}
			}

			date1 = year + aa.substr(7,2) + monthIndex + aa.substr(0,2);

			break;
		case 10:		//  dd-mm-yyyy
			//  Set the data for comparison
			date1 = aa.substr(6,4) + aa.substr(3,2) + aa.substr(0,2);
			break;
		case 11:		//  dd-MMM-yyyy
			//  Get the month index
			for(loop = 0; loop < monthName.length; loop++)
			{
				if(aa.substr(3,3).toUpperCase() == monthName[loop].toUpperCase())
				{
					if(loop < 10)
						monthIndex = '0' + loop;
					else
						monthIndex = loop;
				}
			}

			date1 = aa.substr(7,4) + monthIndex + aa.substr(0,2);
			break;
	}

	//!  Get the second date
	switch(bb.length)
	{
		case 8:			//  dd-mm-yy
			//  Determine if the year prefix is 19 or 20
			if(bb.substr(6,2) < 50)
				year = '20';
			else
				year = '19';

			//  Set the data for comparison
			date2 = year + bb.substr(6,2) + bb.substr(3,2) + bb.substr(0,2);
			break;
		case 9:			//  dd-MMM-yy
			//  Determine if the year prefix is 19 or 20
			if(bb.substr(7,2) < 50)
				year = '20';
			else
				year = '19';

			//  Get the month index
			for(loop = 0; loop < monthName.length; loop++)
			{
				if(bb.substr(3,3).toUpperCase() == monthName[loop].toUpperCase())
				{
					if(loop < 10)
						monthIndex = '0' + loop;
					else
						monthIndex = loop;
				}
			}

			date2 = year + bb.substr(7,2) + monthIndex + bb.substr(0,2);

			break;
		case 10:		//  dd-mm-yyyy
			//  Set the data for comparison
			date2 = bb.substr(6,4) + bb.substr(3,2) + bb.substr(0,2);
			break;
		case 11:		//  dd-MMM-yyyy
			//  Get the month index
			for(loop = 0; loop < monthName.length; loop++)
			{
				if(bb.substr(3,3).toUpperCase() == monthName[loop].toUpperCase())
				{
					if(loop < 10)
						monthIndex = '0' + loop;
					else
						monthIndex = loop;
				}
			}

			date2 = bb.substr(7,4) + monthIndex + bb.substr(0,2);
			break;
	}

	//  Return positive, negative, or zero
	if (date1 > date2)
		return 1;

	if (date1 < date2)
		return -1;

	return 0;
}

function Table_Sort_Currency(a,b)
{
	//  Currency 1
	aa = a.sortKey.replace(/[^0-9.]/g,'');

	//  Currency 2
	bb = b.sortKey.replace(/[^0-9.]/g,'');

	//  Return positive, negative, or zero
	return parseFloat(aa) - parseFloat(bb);
}

function Table_Sort_Numeric(a,b)
{
	//  First number
	aa = parseFloat(a.sortKey);

	//  Verify that the data is a number
	if (isNaN(aa))
		aa = 0;

	//  Second number
	bb = parseFloat(b.sortKey);

	//  Verify that the data is a number
	if (isNaN(bb))
		bb = 0;

	//  Return positive, negative, or zero
	return aa-bb;
}

function Table_Sort_Caseinsensitive(a,b)
{
	//  First data field
	aa = a.sortKey.toLowerCase();

	//  Second data field
	bb = b.sortKey.toLowerCase();

	//  Return positive, negative, or zero
	if (aa==bb) return 0;
	if (aa<bb) return -1;
	return 1;
}


function Table_Select_Row(rowId, multiSelect)
{
	//  Variables
	var rowString;
	var idString;
	var controlNumber;
	var tableid;
	var rowLength;
	
	//  Check for valid rowObj
	if($('#' + rowId).get(0).tagName.toLowerCase() != 'tr')
		return(0);
	
	//  Get the initial data
	idString = rowId.split("_");
	controlNumber = idString[2];
	tableId = 'Table_Main_' + controlNumber;
	rowLength =  $('#' + rowId).children('td').length;
	
	//  Clear the hidden data
	Table_Clear_Data(controlNumber, rowLength);
	
	//  Remove all selections (if necessary)
	if(!multiSelect)
	{
		if(!$('#' + rowId + ' td').hasClass('ui-state-active'))
			$('#' + tableId + ' td').removeClass('ui-state-active');
	}
	
	//  Check for select or deselect
	if($('#' + rowId + ' td').hasClass('ui-state-active'))
		$('#' + rowId + ' td').removeClass('ui-state-active');
	else
		$('#' + rowId + ' td').addClass('ui-state-active');
	
	//  Add the data for all selected rows
	Table_Set_Data(controlNumber, rowLength);
	
	
}

function Table_Clear_Data(controlNumber, rowLength)
{
	//  Variables
	var loop;
	
	//  Loop and clear
	for(loop =  0; loop < rowLength; loop++)
	{
		$('#Table_Hidden_' + controlNumber + '_' + loop).val('');
	}
	
	//  Success
	return(0);
}

function Table_Set_Data(controlNumber, rowLength)
{
	//  Variables
	var colId;
	var colIndex;
	var colSplit;
	var colData;
	var cellData;
	
	//  Find the selected rows
	$('#Table_Main_' + controlNumber + ' tr').find('.ui-state-active').each(function()
		{
			//  Get the ID of the field
			colId = $(this).attr('id');
			
			//  Split the string and get the index
			colSplit = colId.split('_');
			
			//  Get the index
			colIndex = colSplit[4];
			
			//  Get and store the data in the hidden fields
			colData = $('#Table_Hidden_' + controlNumber + '_' + colIndex).val();
			if(colData.length)
				$('#Table_Hidden_' + controlNumber + '_' + colIndex).val(colData + ',' + $(this).text());
			else
				$('#Table_Hidden_' + controlNumber + '_' + colIndex).val($(this).text());
		});
		
	//  Success
	return(0);
}



//  Reset the table data
function Table_Clear_Selection(controlNumber)
{
	//  Find the selected rows
	$('#Table_Main_' + controlNumber + ' tr').each(function()
		{
			//  Check for a td
			if($(this).find('td').first().hasClass('ui-state-active'))
			{
				$(this).click();
			}
		});
		
	//  Success
	return(0);
}




//  Clear Table Rows
/*  This function will remove the rows of a table
	
	Input:
		none
		
	Return:
		0:  Success
		1:  Failure
*/
function Table_Clear_Rows(tableNumber)
{
	//  Check for existance
	if($('#Table_Main_' + tableNumber).length)
	{
		//  Clear the table rows
		$('#Table_Main_' + tableNumber + ' tbody').find('tr').remove();
	}
	
	//  Success
	return(0);
}













