//Ed Wingfield 02/04/2007

var a;	//global to this page as problems with Firefox passing parameter from one method to another hence a and b here
var b;

// functions prototyped below to make them work with Firefox
function showTowns(str)
	{
	var q = str.substr(0,2);	//first two characters of code
	a = str.substr(0,2);
	var temp = new Array();
	if (str !=='')				//check to see if there is a sub code after first ',' in code - if there is then need to filter using this too
	{
		temp = str.split(',');
		var s = temp[1];
		b = temp[1];
		
	}
	// code for Internet Explorer to load towns.xml 
	if (window.ActiveXObject)
	  {
			xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.async=false;
			xmlDoc.setProperty('SelectionLanguage', 'XPath');
			xmlDoc.load("towns.xml");
			generateCombo();
	  }
	// code for Mozilla, Firefox, Opera, etc. to load towns.xml
	else if (document.implementation &&
	document.implementation.createDocument)
	  {
			xmlDoc=document.implementation.createDocument("","",null);
			xmlDoc.load("towns.xml");
		    xmlDoc.onload=generateCombo; 
	  }
	else
	  {
		alert('Your browser cannot handle this script');
	  }
	}

	function generateCombo()
		{
			//a is first part of code , b is second part of code or sub-code
			var output = '';
			var q = a;

			if(b=='')
			{
				var xPath="location/p[starts-with(c," + "'" + a + "')]";
			}
			else
			{
				var xPath="location/p[starts-with(c," + "'" + a + "," + b + "')]";
			}
		
			var nodes=xmlDoc.selectNodes(xPath);  


			output = ('<div class="gullTableTown"><div class="gullTownTitle' + gullTab + '">TOWN<\/div>');

			if(nodes.length==0 || q=='')	//empty combo as no matching nodes
			{
				output = output + ('<select class="gullTowns" id="gullSelTowns" name="gullSelTowns"><option>Any Town</option></select></div></div></div>');
				document.getElementById("txtTowns").innerHTML=output;
				return;
			}
			else
			{
				output = output + ('<select class="gullTowns" id="gullSelTowns" name="gullSelTowns"> <option>Any Town</option>>');
			}

			if(document.implementation &&				//FIREFOX
			document.implementation.createDocument)	
			{
				for(var x=1; x <= nodes.length-1; x++)
				{
					for(var y=1; y<=1; y++)
					{
						if(nodes[x].childNodes[y].nodeName=='c')
						{
							str=  '<option value=' + nodes[x].childNodes[y].textContent + '>';
						}
						
						if(nodes[x].childNodes[y+2].nodeName=='t')
						{
							str = str + nodes[x].childNodes[y+2].textContent + '</option>';
							output = output + str;
						}
					}		
				}
			}
			else										//INTERNET EXPLORER
			{
				for(var x=1; x <= nodes.length-1; x++)
				{
					for(var y=0; y<=1; y++)
					{
						
						if(nodes.item(x).childNodes(y).nodeName=='c')
						{
							str=  '<option value=' + nodes.item(x).childNodes(y).text + '>';
									
						}
						else if(nodes.item(x).childNodes(y).nodeName=='t')
						{
							str = str + nodes.item(x).childNodes(y).text + '</option>';
							output = output + str;
						}
					}		
				}
			}

			output = output + ('</div></div>');
			document.getElementById("txtTowns").innerHTML=output;
		}
