
// declare a global  XMLHTTP Request object
var XmlHttpObj;

// create an instance of XMLHTTPRequest Object, varies with browser type, try for IE first then Mozilla
function CreateXmlHttpObj()
{
	// try creating for IE (note: we don't know the user's browser type here, just attempting IE first.)
	try
	{
		XmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlHttpObj = null;
		}
	}
	// if unable to create using IE specific code then try creating for Mozilla (FireFox) 
	if(!XmlHttpObj && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttpObj = new XMLHttpRequest();
	}
}

// called from onChange or onClick event of the trade dropdown list
function tradeListOnChange() 
{
    var tradeList = document.getElementById("tradeList");
    
    // get selected continent from dropdown list
    var selectedTrade = tradeList.options[tradeList.selectedIndex].value;
    
    // url of page that will send xml data back to client browser
    var requestUrl;
    requestUrl = "/ajax/get_servicesTwo.asp" + "?filter=" + encodeURIComponent(selectedTrade);
	CreateXmlHttpObj();
	
	// verify XmlHttpObj variable was successfully initialized
	if(XmlHttpObj)
	{
		XmlHttpObj.onreadystatechange = StateChangeHandlerTrade;
		XmlHttpObj.open("GET", requestUrl,  true);
		XmlHttpObj.send(null);	
	}
}
function StateChangeHandlerTrade()
{
	// state ==4 indicates receiving response data from server is completed
	if(XmlHttpObj.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttpObj.status == 200)
		{			
			PopulateservicesList(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			alert("problem retrieving data from the server, status code: " + XmlHttpObj.status);
		}
	}
}

// called from onChange or onClick event of the continent dropdown list
function countryListOnChange() 
{
    var countryList = document.getElementById("countryList");
    
    // get selected continent from dropdown list
    var selectedContinent = countryList.options[countryList.selectedIndex].value;
    
    // url of page that will send xml data back to client browser
    var requestUrl;
    requestUrl = "/ajax/get_towns.asp" + "?filter=" + encodeURIComponent(selectedContinent);
	CreateXmlHttpObj();
	
	// verify XmlHttpObj variable was successfully initialized
	if(XmlHttpObj)
	{
		XmlHttpObj.onreadystatechange = StateChangeHandlerCountry;
		XmlHttpObj.open("GET", requestUrl,  true);
		XmlHttpObj.send(null);	
	}
}
function StateChangeHandlerCountry()
{
	// state ==4 indicates receiving response data from server is completed
	if(XmlHttpObj.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttpObj.status == 200)
		{			
			PopulatetownList(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			alert("problem retrieving data from the server, status code: " + XmlHttpObj.status);
		}
	}
}

function townListOnChange() 
{
    var townList = document.getElementById("townList");
    // get selected continent from dropdown list
    var selectedTown = townList.options[townList.selectedIndex].value;
    // url of page that will send xml data back to client browser
    var requestUrl;
    requestUrl = "/ajax/get_events.asp" + "?filter=" + encodeURIComponent(selectedTown);
	CreateXmlHttpObj();
	
	// verify XmlHttpObj variable was successfully initialized
	if(XmlHttpObj)
	{
		XmlHttpObj.onreadystatechange = StateChangeHandlerTown;	
	
		// define the iteraction with the server -- true for as asynchronous.
		XmlHttpObj.open("GET", requestUrl,  true);
		
		// send request to server, null arg  when using "GET"
		XmlHttpObj.send(null);

	}
}
function StateChangeHandlerTown()
{
	// state ==4 indicates receiving response data from server is completed
	if(XmlHttpObj.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttpObj.status == 200)
		{			
			PopulateeventList(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			alert("problem retrieving data from the server, status code: " + XmlHttpObj.status);
		}
	}
}
function townListVenueOnChange() 
{
    var townList = document.getElementById("townList");
    // get selected continent from dropdown list
    var selectedTown = townList.options[townList.selectedIndex].value;
    // url of page that will send xml data back to client browser
    var requestUrl;
    requestUrl = "/ajax/get_venues.asp" + "?filter=" + encodeURIComponent(selectedTown);
	CreateXmlHttpObj();
	
	// verify XmlHttpObj variable was successfully initialized
	if(XmlHttpObj)
	{
		XmlHttpObj.onreadystatechange = StateChangeHandlerTownVenue;	
	
		// define the iteraction with the server -- true for as asynchronous.
		XmlHttpObj.open("GET", requestUrl,  true);
		
		// send request to server, null arg  when using "GET"
		XmlHttpObj.send(null);

	}
}
function StateChangeHandlerTownVenue()
{
	// state ==4 indicates receiving response data from server is completed
	if(XmlHttpObj.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttpObj.status == 200)
		{			
			PopulatevenueList(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			alert("problem retrieving data from the server, status code: " + XmlHttpObj.status);
		}
	}
}

// populate the contents of the country dropdown list
function PopulatetownList(countryNode)
{
    var townList = document.getElementById("townList");
	// clear the country list 
	for (var count = townList.options.length-1; count >-1; count--)
	{
		townList.options[count] = null;
	}

	var countryNodes = countryNode.getElementsByTagName('town');
	var idValue;
	var textValue; 
	var optionItem;
	// populate the dropdown list with data from the xml doc
	for (var count = 0; count < countryNodes.length; count++)
	{
   		textValue = GetInnerText(countryNodes[count]);
		idValue = countryNodes[count].getAttribute("id");
		optionItem = new Option( textValue, idValue,  false, false);
		townList.options[townList.length] = optionItem;
	}
}
// populate the contents of the event dropdown list
function PopulateeventList(eventNode)
{
    var eventList = document.getElementById("eventList");
	// clear the event list 
	for (var count = eventList.options.length-1; count >-1; count--)
	{
		eventList.options[count] = null;
	}

	var eventNodes = eventNode.getElementsByTagName('event');
	var idValue;
	var textValue; 
	var optionItem;
	// populate the dropdown list with data from the xml doc
	for (var count = 0; count < eventNodes.length; count++)
	{
   		textValue = GetInnerText(eventNodes[count]);
		idValue = eventNodes[count].getAttribute("id");
		optionItem = new Option( textValue, idValue,  false, false);
		eventList.options[eventList.length] = optionItem;
	}
}
// populate the contents of the venue dropdown list
function PopulatevenueList(venueNode)
{
    var venueList = document.getElementById("venueList");
	// clear the venue list 
	for (var count = venueList.options.length-1; count >-1; count--)
	{
		venueList.options[count] = null;
	}

	var venueNodes = venueNode.getElementsByTagName('venue');
	var idValue;
	var textValue; 
	var optionItem;
	// populate the dropdown list with data from the xml doc
	for (var count = 0; count < venueNodes.length; count++)
	{
   		textValue = GetInnerText(venueNodes[count]);
		idValue = venueNodes[count].getAttribute("id");
		optionItem = new Option( textValue, idValue,  false, false);
		venueList.options[venueList.length] = optionItem;
	}
}
// populate the contents of the services dropdown list
function PopulateservicesList(serviceNode)
{
    var serviceList = document.getElementById("serviceList");
	// clear the venue list 
	for (var count = serviceList.options.length-1; count >-1; count--)
	{
		serviceList.options[count] = null;
	}

	var serviceNodes = serviceNode.getElementsByTagName('service');
	var idValue;
	var textValue; 
	var optionItem;
	// populate the dropdown list with data from the xml doc
	for (var count = 0; count < serviceNodes.length; count++)
	{
   		textValue = GetInnerText(serviceNodes[count]);
		idValue = serviceNodes[count].getAttribute("id");
		optionItem = new Option( textValue, idValue,  false, false);
		serviceList.options[serviceList.length] = optionItem;
	}
}
// returns the node text value 
function GetInnerText (node)
{
	 return (node.textContent || node.innerText || node.text) ;
}
