//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)
	{
	if( document.implementation.hasFeature("XPath", "3.0") )
	{
		XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
		{
			if( !xNode ) { xNode = this; } 

			var oNSResolver = this.createNSResolver(this.documentElement)
			var aItems = this.evaluate(cXPathString, xNode, oNSResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
			var aResult = [];
			for( var i = 0; i < aItems.snapshotLength; i++)
			{
				aResult[i] =  aItems.snapshotItem(i);
			}
			
			return aResult;
		}
		XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode)
		{
			if( !xNode ) { xNode = this; } 

			var xItems = this.selectNodes(cXPathString, xNode);
			if( xItems.length > 0 )
			{
				return xItems[0];
			}
			else
			{
				return null;
			}
		}
		Element.prototype.selectNodes = function(cXPathString)
		{
			if(this.ownerDocument.selectNodes)
			{
				return this.ownerDocument.selectNodes(cXPathString, this);
			}
			else{throw "For XML Elements Only";}
		}
		Element.prototype.selectSingleNode = function(cXPathString)
		{	
			if(this.ownerDocument.selectSingleNode)
			{
				return this.ownerDocument.selectSingleNode(cXPathString, this);
			}
			else{throw "For XML Elements Only";}
		}
	}

	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="PROPERTIES/PROPERTY[starts-with(CODE," + "'" + a + "')]";
			}
			else
			{
				var xPath="PROPERTIES/PROPERTY[starts-with(CODE," + "'" + a + "," + b + "')]";
			}
		
			var nodes=xmlDoc.selectNodes(xPath);  

			output = ('<table cellspacing=0 border=0><tr><td width=95>Town:<\/td><td>');

			if(nodes.length==0 || q=='')	//empty combo as no matching nodes
			{
				output = output + ('<select name=selTowns WIDTH=200 STYLE=width:200px><option>please select</option></select></td></tr></table>');
				document.getElementById("txtTowns").innerHTML=output;
				return;
			}
			else
			{
				output = output + ('<select name=selTowns WIDTH=200 STYLE=width:200px> <option>please select</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=='CODE')
						{
							str=  '<option value=' + nodes[x].childNodes[y].textContent + '>' 
						}
						
						if(nodes[x].childNodes[y+2].nodeName=='TOWN')
						{
							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=='CODE')
						{
							str=  '<option value=' + nodes.item(x).childNodes(y).text + '>' 
									
						}
						else if(nodes.item(x).childNodes(y).nodeName=='TOWN')
						{
							str = str + nodes.item(x).childNodes(y).text + '</option>'
							output = output + str;
						}
					}		
				}
			}

			output = output + ('</select></td></tr></table>');
			document.getElementById("txtTowns").innerHTML=output;
		}
