var AllHighlightableRows;
var CurrentPage = "Home";
var CurrentRegion = "AfricaMiddleEast";

var AfricaMiddleEastLoaded = false;
var AsiaAustralasiaLoaded = false;
var EuropeLoaded = false;
var NorthSouthAmericaCaribbeanLoaded = false;

function Start()
{
	//AllHighlightableRows = document.getElementsByTagName("tr");
	//for(var i=0;i<AllHighlightableRows.length;i++) 
	//{
	//	if (AllHighlightableRows[i].className == "pricelistrow")
	//	{	
	//		AllHighlightableRows[i].onmouseover = function() { AllHighlightableRows[i].className = "hoveredRow" };
	//		AllHighlightableRows[i].onmouseout = function() { AllHighlightableRows[i].className = "normalRow" };
	//	}
	//}
	RefreshDisplay()
}

function GetPageItem(nameOfItem)
{
	if(document.getElementById) 
	{
		return document.getElementById(nameOfItem);
	}
	return document.all[nameOfItem];
}

function MouseOverSideBar(HTMLobject)
{
	HTMLobject.className = HTMLobject.className.replace("SideBarFree", "SideBarHover")
}

function MouseOutSideBar(HTMLobject)
{
	HTMLobject.className = HTMLobject.className.replace("SideBarHover", "SideBarFree")
}

function MouseOverTopBar(HTMLobject)
{
	HTMLobject.className = HTMLobject.className.replace("buttonFree", "buttonHover")
}

function MouseOutTopBar(HTMLobject)
{
	HTMLobject.className = HTMLobject.className.replace("buttonHover", "buttonFree")
}

function TableRowHoverOver(HTMLobject)
{
	HTMLobject.className = HTMLobject.className.replace("TableRowFree", "TableRowHover")
}

function TableRowHoverOut(HTMLobject)
{
	HTMLobject.className = HTMLobject.className.replace("TableRowHover", "TableRowFree")
}

function PageButtonPress(NewPage)
{
	CurrentPage = NewPage;
	
	if (NewPage == "Home")
	{
		document.getElementById("SideButtonHome").className = "SideBarSelected"
	}
	else
	{
		document.getElementById("SideButtonHome").className = "SideBarFree"
	}
	if(NewPage == "Flights")
	{
		document.getElementById("SideButtonFlights").className = "SideBarSelected"
	}
	else
	{
		document.getElementById("SideButtonFlights").className = "SideBarFree"
	}
//	if(NewPage == "Info")
//	{
//		document.getElementById("SideButtonInfo").className = "SideBarSelected"
//	}
//	else
//	{
//		document.getElementById("SideButtonInfo").className = "SideBarFree"
//	}
	if(NewPage == "AboutUs")
	{
		document.getElementById("SideButtonAboutUs").className = "SideBarSelected"
	}
	else
	{
		document.getElementById("SideButtonAboutUs").className = "SideBarFree"
	}
	
	RefreshDisplay();
}

function RegionButtonPress(NewRegion)
{
	CurrentRegion = NewRegion;
	if (NewRegion == "AfricaMiddleEast")
	{
		document.getElementById("TopButtonAfricaMiddleEast").className = "TopBarMenu buttonSelected"
	}
	else
	{
		document.getElementById("TopButtonAfricaMiddleEast").className = "TopBarMenu buttonFree"
	}
	if(NewRegion == "AsiaAustralasia")
	{
		document.getElementById("TopButtonAsiaAustralasia").className = "TopBarMenu buttonSelected"
	}
	else
	{
		document.getElementById("TopButtonAsiaAustralasia").className = "TopBarMenu buttonFree"
	}
/*	if(NewRegion == "Europe")
	{
		document.getElementById("TopButtonEurope").className = "TopBarMenu buttonSelected"
	}
	else
	{
		document.getElementById("TopButtonEurope").className = "TopBarMenu buttonFree"
	}
	*/
	if(NewRegion == "NorthSouthAmericaCaribbean")
	{
		document.getElementById("TopButtonNorthSouthAmericaCaribbean").className = "TopBarMenu buttonSelected"
	}
	else
	{
		document.getElementById("TopButtonNorthSouthAmericaCaribbean").className = "TopBarMenu buttonFree"
	}
	RefreshDisplay();
}

function RefreshDisplay()
{
	document.getElementById("AfricaMiddleEastPriceList").style.display = "none";
	document.getElementById("AsiaAustralasiaPriceList").style.display = "none";
	document.getElementById("EuropePriceList").style.display = "none";
	document.getElementById("NorthSouthAmericaCaribbeanPriceList").style.display = "none";
	
//	document.getElementById("AfricaMiddleEastInfo").style.display = "none";
//	document.getElementById("AsiaAustralasiaInfo").style.display = "none";
//	document.getElementById("EuropeInfo").style.display = "none";
//	document.getElementById("NorthSouthAmericaCaribbeanInfo").style.display = "none";
	
	document.getElementById("FFAboutUs").style.display = "none";
	document.getElementById("FFHome").style.display = "none";
	document.getElementById("FFCountryInfoContainer").style.display = "none";
	document.getElementById("FFCountryPriceList").style.display = "none";
	
	
	if (CurrentPage == "Home")
	{
		document.getElementById("FFHome").style.display = "";
	}
	else if (CurrentPage == "AboutUs")
	{
		document.getElementById("FFAboutUs").style.display = "";
	}
	else
	{
		document.getElementById("FFCountryInfoContainer").style.display = "";
		document.getElementById("FFCountryPriceList").style.display = "";
		if(CurrentRegion == "AfricaMiddleEast")
		{
			if (!AfricaMiddleEastLoaded)
			{
				PopulateTable(GetPageItem('tblAfricaMiddleEastPriceList'), 'CSVFiles\\AfricaMiddleEastPricelist.csv');
				AfricaMiddleEastLoaded = true;
			}
			document.getElementById("AfricaMiddleEastPriceList").style.display = "";
		}
		else if (CurrentRegion == "AsiaAustralasia")
		{
			if (!AsiaAustralasiaLoaded)
			{
				PopulateTable(GetPageItem('tblAsiaAustralasiaPriceList'), 'CSVFiles\\AsiaAustralasiaPricelist.csv');
				AsiaAustralasiaLoaded = true;
			}
			document.getElementById("AsiaAustralasiaPriceList").style.display = "";
		}
		else if (CurrentRegion == "Europe")
		{			
			if (!EuropeLoaded)
			{
				PopulateTable(GetPageItem('tblEuropePriceList'), 'CSVFiles\\EuropePricelist.csv');
				EuropeLoaded = true;
			}
			document.getElementById("EuropePriceList").style.display = "";
		}
		else
		{
			if (!NorthSouthAmericaCaribbeanLoaded)
			{
				PopulateTable(GetPageItem('tblNorthSouthAmericaCaribbeanPriceList'), 'CSVFiles\\NorthSouthAmericaCaribbeanPriceList.csv');
				NorthSouthAmericaCaribbeanLoaded = true;
			}
			document.getElementById("NorthSouthAmericaCaribbeanPriceList").style.display = "";
		}
	}
}

function getFile(filename)
{ 
	oxmlhttp = null;
	try
	{ 
		oxmlhttp = new XMLHttpRequest();
		oxmlhttp.overrideMimeType("text/xml");
	}
	catch(e)
	{ 
		try
		{ 
			oxmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{ 
			return null;
		}
	}
	if(!oxmlhttp) 
	return null;
	
	try
	{ 
		oxmlhttp.open("GET",filename,false);
		oxmlhttp.send(null);
	}
	catch(e)
	{ 
		return null;
	}
	return oxmlhttp.responseText;
}

function PopulateTable(Table, DataFile)
{
	//Table.innerHTML = "";
	ClearTable(Table)
	var BrokenString = new Array();
	BrokenString = getFile(DataFile).split('\n');
	for (i=0; i < BrokenString.length; i++)
	{
		//var partString = BrokenString[i];
		//Table.innerHTML += "<tr class='TableRowFree' onmouseover='TableRowHoverOver(this);' onmouseout='TableRowHoverOut(this);'><td class='TableCellTitle'>";
		//Table.innerHTML +=  partString.substring(0,partString.indexOf(","));
		//partString = partString.substring(partString.indexOf(",")+1);
		//Table.innerHTML += "</td><td class='TableCellData'>";
		//Table.innerHTML += partString.substring(0,partString.indexOf(","));
		//partString = partString.substring(partString.indexOf(",")+1);
		//Table.innerHTML += "</td><td class='TableCellData'>";
		//Table.innerHTML += partString;
		//Table.innerHTML += "</td></tr>";
		
		var Breakdown = new Array();
		Breakdown = BrokenString[i].split(",");
		AddRowToTable(Table, Breakdown[0], Breakdown[1], Breakdown[2]);
	}
}

//generic function for clearing a tables contents
function ClearTable(Table)//except title row
{
	while (Table.rows.length > 1)
	{
		Table.deleteRow(1);
	}
}

//generic function to add a row to a table
function AddRowToTable(Table, RowDataName, RowDataOneWay, RowDataReturn)
{
	var xrow;
	var xcell;
	// Add row
	xrow = Table.insertRow(Table.rows.length);
	xrow.className = "TableRowFree";
	xrow.id = Math.random();
	xrow.onmouseover=function(){TableRowHoverOver(this);} ;
	xrow.onmouseout=function(){TableRowHoverOut(this); };
	//  xrow.setAttribute('onmouseover','TableRowHoverOver(this.id)');
	//  xrow.setAttribute('onmouseout','TableRowHoverOut(this.id)');
	  ///START
	  
	// Add cells
	xcell = xrow.insertCell(0);
	xcell.className = "TableCellTitle";
	xcell.innerHTML = RowDataName	;
	xcell = xrow.insertCell(1);
	xcell.className = "TableCellData";
	xcell.innerHTML = "£" + RowDataOneWay	;
	xcell = xrow.insertCell(2);
	xcell.className = "TableCellData";
	xcell.innerHTML = "£" + RowDataReturn	;
}
