// JavaScript Document
// AJAX Function to get counties based on region selected

function GetLocalAuthorities()
{
	var xmlhttp;
		if (window.XMLHttpRequest)
		  {
		  // code for IE7+, Firefox, Chrome, Opera, Safari
		  xmlhttp=new XMLHttpRequest();
		  }
		else if (window.ActiveXObject)
		  {
		  // code for IE6, IE5
		  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
		else
		  {
		  alert("Your browser does not support XMLHTTP!");
		  }
	
	xmlhttp.onreadystatechange=function()
		{
			if(xmlhttp.readyState==4)
			  {
				  //alert(xmlhttp.responseText);
				  var ac = new Array();
				  ac = xmlhttp.responseText.split(",");
				
				  var x=document.getElementById("localauthselect");
				  
				  // remove all current counties from Select Counties drop down
				  x.options.length=0		
				 
				  x.options[0]=new Option("By Local Authority", "0", true, false)
				  
				  for(i=1;i<=(ac.length);i++)
					{
						var p = ac[i-1].indexOf("-");
						var laid = ac[i-1].slice(0,p);
						var laname = ac[i-1].slice(p+1,(ac[i-1].length));
						
						// add returned counties to Select Counties drop down
						x.options[i]=new Option(laname, laid, true, false) 
					}
			  }
		}
	
	var x=document.getElementById("regionselect").value;
	var y=document.getElementById("countyselect").value;
	if( y == 0)
	{
	 var url = "scripts/GetLocalAuth.php?r="+x;
	}
	else
	{
	var url = "scripts/GetLocalAuth.php?r="+x+"&c="+y;
	}
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
 }
