//***********************************************************
// Trail Locator Project
// The Trans Canada Trail
// http://www.tctrail.ca
// 
// Coding by Stephane Simard
// ssoutbox-tctrail@yahoo.com
//************************************************************


// Variable global
var xmlCompact;			// Get the xml data
var xmlExtend;			// Get the xml data


//-------------------------------------------------------------------------------------------
// Loading provinces from xml

function loadProvincesFromXml()
{
	// Select to load
	var Selectprovince1 = document.getElementById('provinceList');
	var Selectprovince2 = document.getElementById('trailProvinceList');

	// message wait
	Selectprovince1.options[0] = new Option(lang(1), "");
	
	// Read the data from Provinces.xml
	var request = GXmlHttp.create();
	request.open("GET", "xml/Provinces.xml", true);
	request.onreadystatechange = function()
	{
		if (request.readyState == 4)
		{
			var xmlDoc = request.responseXML;
			var provinces = xmlDoc.getElementsByTagName("province");
			
			// read each line
			for (var p=0; p<provinces.length; p++)
			{
				var name = provinces[p].getElementsByTagName("name");
				var coord = provinces[p].getElementsByTagName("coord");
				var bounds = provinces[p].getElementsByTagName("bounds");
				// Load Select
				Selectprovince1.options[p+1] = new Option(name[0].getAttribute(language), p);
				Selectprovince2.options[p] = new Option(name[0].getAttribute(language), p);
				// Load in memory
				addNewProvince(provinces[p].getAttribute("abr"), name[0].getAttribute(language), coord[0].getAttribute("lat"), coord[0].getAttribute("lng"), coord[0].getAttribute("zoom"), bounds[0].getAttribute("sw"), bounds[0].getAttribute("ne"));				
			}
			Selectprovince1.options[0] = new Option(lang(3), "");	// Message "Select your province"
			Selectprovince1.selectedIndex = 0;	// first row of select
		}
	}
	request.send(null);
}


//-------------------------------------------------------------------------------------------
// Load trails informations from xml file

function loadTrailsFromXml()
{
	// Read the data from Trails.xml
	var request = GXmlHttp.create();
	request.open("GET", "xml/Trails.xml", false);
	request.send(null);
	if (request.readyState == 4)
	{
		var xmlDoc = request.responseXML;
		var trail = xmlDoc.getElementsByTagName("trail");
					
		// read each line & Load in memory
		for (var t = 0; t < trail.length; t++) 
		{
			var bounds = trail[t].getElementsByTagName("bounds");		// get the bounds						
			addNewTrail(trail[t].getAttribute("name"), trail[t].getAttribute("nb"), trail[t].getAttribute("abr"), bounds[0].getAttribute("sw"), bounds[0].getAttribute("ne"));
		}
	}
}


//-------------------------------------------------------------------------------------------
// Load compact coordinates from xml file

function loadCompactFromXml()
{
	var request = GXmlHttp.create();
	request.open("GET", "xml/Compact.xml", false);
	request.send(null);
	if (request.readyState == 4)
	{
		var xmlDoc = request.responseXML;
		xmlCompact = xmlDoc.getElementsByTagName("trail");
	}
}


//-------------------------------------------------------------------------------------------
// Load extend coordinates from xml file

function loadExtendFromXml()
{
	var request = GXmlHttp.create();
	request.open("GET", "xml/Extend.xml", false);
	request.send(null);
	if (request.readyState == 4)
	{
		var xmlDoc = request.responseXML;
		xmlExtend = xmlDoc.getElementsByTagName("trail");
	}
}


//-------------------------------------------------------------------------------------------
// Loading Pavilions from xml

function loadPavilionsFromXml()
{
	var SelectPav = document.getElementById('pavList');

	// If 0 data then continue and load pavilions
	if(SelectPav.options.length > 0)
		return;
	
	// message wait
	SelectPav.options[0] = new Option(lang(1), "");
	
	// Read the data from Pavilions.xml
	var request = GXmlHttp.create();
	request.open("GET", "xml/Pavilions.xml", true);
	request.onreadystatechange = function()
	{
		if (request.readyState == 4)
		{
			var xmlDoc = request.responseXML;
			var pav = xmlDoc.getElementsByTagName("pav");
			
			// read each line and Load Select
			for (var p=0; p<pav.length; p++)	
				SelectPav.options[p] = new Option(pav[p].getAttribute("name"), pav[p].getAttribute("name") + "," + pav[p].getAttribute("lat") + "," + pav[p].getAttribute("lng") + "," + pav[p].getAttribute("img"));
		}
	}
	request.send(null);
}