function getSelected(opt) {
      var selected = new Array();
      var index = 0;
      for (var intLoop=0; intLoop < opt.length; intLoop++) {
         if (opt[intLoop].selected) {
            index = selected.length;
            selected[index] = new Object;
            selected[index].value = opt[intLoop].value;
            selected[index].index = intLoop;
         }
      }
      return selected;
   }

function processStateChange()
{
	
	if (request.readyState == 4)
	{
		contentDiv = document.getElementById(dest);
		if (request.status == 200)
		{
			response = request.responseText;
			window.alert('dfffdf');
			contentDiv.innerHTML = response;
		}
	}
}
function loadHTML(method, URL, destination)
{
	
	dest = destination;
	if(window.XMLHttpRequest)
	{
		request = new XMLHttpRequest();
		request.onreadystatechange = processStateChange;
		request.open(method, URL, true);
		request.send(null);
	}
	else if (window.ActiveXObject) 
	{
		request = new ActiveXObject("Microsoft.XMLHTTP");
		if (request) 
		{
			request.onreadystatechange = processStateChange;
			request.open(method, URL, true);
			request.send();
	    }
	}
}

function loadTowns(multiple) {
	var send = '';
	
	if (multiple) {
		selected = getSelected(document.getElementById("county"));
		
		for (var intLoop=0; intLoop < selected.length; intLoop++) {
         if (selected[intLoop].value) {
			 send += selected[intLoop].value+':';
         }
		}
		loadHTML('POST', 'towns.php?county='+ send +'&multiple=true', 'towns');

      

	} else {
		selected = document.getElementById("county").value;
		loadHTML('POST', 'towns.php?county=' + selected, 'towns');
	}
}

function setDefaultTown(id) {

	document.getElementById("town").value = id;

}

function setTowns() {
    var send = '';
	selected = getSelected(document.getElementById("town"));
		
	for (var intLoop=0; intLoop < selected.length; intLoop++) {
	 if (selected[intLoop].value) {
		 send += selected[intLoop].value+':';
	 }
	}
	document.getElementById("towns_id").value = send;
}

