var PARC = 1;
var ARRIVALDATE = 2;
var DURATION = 3;
var MOBILEHOME = 4;
var SAVINGS = 5;
var PRICE =6 ;
var RATING = 7

function isOdd(num)
{
	if(num%2==1)
		return true;
	else
		return false;
}

function isEven(num)
{
	if(num%2==0)
		return true;
	else
		return false;
}

function getOption(selectedOption)
{
	var selectedValue = selectedOption.value;
	var option = 0;
	if (selectedValue=='PRCA')
	{
		option = PRICE;
	}
	else if (selectedValue=='PKNA')
	{
		option = PARC;
	}
	else if (selectedValue=='GLRD')
	{
		option = RATING;
	}	
	var tableToSort = document.getElementById('earlyBirdOffers');
	sortTable(option,tableToSort);
}

function setDataType(cValue)
{
    // THIS FUNCTION CONVERTS DATES AND NUMBERS FOR PROPER ARRAY
    // SORTING WHEN IN THE SORT FUNCTION
    var isDate = new Date(cValue);

	//FOR IE COMPARE WITH 'NaN' AND FOR FIREFOX COMPARE WITH 'Invalid Date'.
    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 ;
    }
}

// ** SORTS THE REQUIRED TABLE
function sortTable(option,tableToSort)
{
/*	alert(option);*/
	var colCount = tableToSort.rows[0].cells.length;
	var colArray = new Array();
	var rowCountBeforeSort = tableToSort.rows.length;
    var bSort = 0;
    var oldIndex = new Array();
    var indexArray = new Array();
    var bArray = new Array();
    var newRow;
    var newCell;
    var i;
    var c;
    var j;
	var k;
/*	alert(iCurCell);*/
 // ** POPULATE THE ARRAY colArray WITH CONTENTS OF THE COLUMN SELECTED
	for (i=1; i < tableToSort.rows.length; i++)
      {
		var currentRowCells = tableToSort.rows[i].cells;
		if (option == 1)
		{
			// THE PARC NAME STRUCTURE IS <td><p><a>..PARC NAME..</a></p></td>
			// FIRST GET THE PARAGRAPH ELEMENT.
			var anchor = (currentRowCells[option-1].childNodes)[0];
		
			// THEN GET THE PARC NAME PRESENT INSIDE THE ANCHOR TAG
			
			//var anchor = (para.childNodes)[0];	
			
			colArray[i - 1] = anchor.innerHTML;
			
		}
		else if (option == 6)
		{
			// THE PRICE STRUCTURE IS <td><p>..PRICE..</p></td>
			// FIRST GET THE PARAGRAPH ELEMENT.
			var priceWithSymbol = currentRowCells[option-1].innerHTML
			//var priceWithSymbol = para.innerHTML;

			var priceWithoutSymbol = priceWithSymbol.substring(1);
		//	alert(priceWithoutSymbol);
			colArray[i - 1] = setDataType(priceWithoutSymbol);

		}
		else if (option == 7)
		{
			//alert('not implemented');
			//return false;
			var para = (currentRowCells[option-1].childNodes)[0];

			// THEN GET THE PARC NAME PRESENT INSIDE THE ANCHOR TAG
			
			colArray[i - 1] = para.innerHTML;
		}
	}

// ** COPY ARRAY FOR COMPARISON AFTER SORT
	for (i=0; i < colArray.length; i++)
    {
		bArray[i] = colArray[i];
    }

// ** SORT THE COLUMN ITEMS
    colArray.sort();

// ** indexArray CONTAINS THE NEW ROWINDICES
	for (j=0; j < bArray.length; j++)
    { 
        for(i=0; i < colArray.length; i++)
        {
            if (bArray[j]==colArray[i])
            {  
				indexArray[j] = i;
			}
        }
    }

	var rowIndex= tableToSort.rows.length;

// ** CREATE NEW ROWS AT THE BOTTOM OF THE TABLE WITH THE CONTENT BEING SORTED
	for(i=0;i<bArray.length;i++)
	{
		for(j=0;j<indexArray.length;j++)
		{
			if(indexArray[j]==i)
			{
				newRow = tableToSort.insertRow(rowIndex);
				rowIndex++;
				for(c=0;c<colCount;c++)
				{
					newCell = newRow.insertCell(c);
					newCell.innerHTML = tableToSort.rows[j+1].cells[c].innerHTML;
					newCell.className = tableToSort.rows[j+1].cells[c].className;
				}
			}
		}
	}

// ** DELETE THE UNSORTED TOP ROWS
	for(i=1;i<rowCountBeforeSort;i++)
	{
		tableToSort.deleteRow(1);
	}

//CALL THE PAGINATION AGAIN
	init(); 
    showPage(1);
}

