var msie = (window.ActiveXObject) ? true : false;
var moz = (document.implementation && document.implementation.createDocument) ? true : false;

function showCountryDivisions(regionID,countryID)
{
	document.getElementById('noFlash_sitelistHolder').style.display="none";
	
	importXML("/xml/map.xml");	
	
	
	function importXML( xmlFile )
	{
	    if (moz)
	    {
	        xmlDoc = document.implementation.createDocument("", "", null);
	        xmlDoc.onload = traverseXML;
	    }
	    else if (msie)
	    {
	        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	        xmlDoc.onreadystatechange = function () {
	            if (xmlDoc.readyState == 4) traverseXML();
	        };
	     }
	    else
	    {
	        alert('Your browser can\'t handle this script');
	        return;
	    }
	    if ( !xmlDoc.load(xmlFile) ) {
	        alert ("Failed to load XML data source!");
	    };
	}
	
	function traverseXML()
	{
		x = xmlDoc.getElementsByTagName('region');		
		
		//test = getAttribute(x[0].childNodes[1], "name");
		regions = getAllSiblings(x[0]); //Gets all regions into an array
		
		foundRegion = findRegionById(regions, regionID);//Finds a regions by its ID not its order				
		children = getChildren(foundRegion); // gets all children of a node		
		foundCountry = findCountryById(children, countryID);
		
		//test = getAttribute(foundCountry, "name"); // gets an attribute of a node
		
		displayDivision(foundRegion, foundCountry);
				
	}	
}

function displayDivision(regionNode, countryNode)
{
	var s = '';

	divisions = getChildren(countryNode);
	s+='<center>';
	for(i=1; i!=divisions.length; i++)
	{
		if(divisions[i].nodeName == "division")
		{
			imgSrc = getAttribute(divisions[i], "noFlash");
			nodeUrl = getAttribute(divisions[i], "url");
			
			s+='<a href="'+nodeUrl+'"><img src="'+imgSrc+'"></a>';
		
		}
	
	}
	s+='</center>';
	document.getElementById('noFlash_divisionsHolder').style.display="block";
	document.getElementById('noFlash_divisionsHolder').innerHTML = s;	
}

function getAllSiblings(node)
{
	var siblings = new Array;
	var countSiblings = 1;
	 		
	for(var m = node; m != null; m = m.nextSibling)	
	{
		 if(m.nodeType == 1)
		 {
		 	siblings[countSiblings] = m;
		 	countSiblings++;
		 }		
	}
	return siblings;

}

function getChildren(node)
{
	var children = new Array;
	var countChildren = 1;

	 		
	for(var m = node.firstChild; m != null; m = m.nextSibling)	
	{
		 if(m.nodeType == 1)
		 {
		 	children[countChildren] = m;
		 	countChildren++;
		 }		
	}
	return children;
}
		


function getAttribute(node, att)
{
	while(node!= null && node.nodeType != 1)
	{
		node = node.nextSibling;
	}		
	if(node == null)
		return "";
	else return node.getAttribute(att);	
}

function findRegionById(regions, id)
{
	for(i=1; i!=regions.length; i++)
	{
		if(regions[i].getAttribute("id") == id)
			return regions[i];
	}
	
}

function findCountryById(countries, id)
{
	for(i=1; i!=countries.length; i++)
	{
		if(countries[i].getAttribute("id") == id)
			return countries[i];
	}
	
}



