	function isset(what)
	{
		return Boolean(typeof what != 'undefined' && what);
	}

	function createXMLHttpRequest( ) 
	{
		try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
		try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
		try { return new XMLHttpRequest( ); } catch(e) {}
		alert("XMLHttpRequest not supported");
		return null;
	}
	
	function createXMLObject(text)
	{
		if (window.ActiveXObject)
		{
			var doc=new ActiveXObject("Microsoft.XMLDOM");
			doc.async="false";
			doc.loadXML(text);
		}
		else
		{
			var parser=new DOMParser();
			var doc=parser.parseFromString(text,"text/xml");
		}		
		
		return doc.documentElement;
	}

	
	
	function init()
	{
		updateSelect();
	}
	
	function update()
	{
		getSelectedValues();
		updateSelect();
	}
	
	
	function getSelectedValues ()
	{
		 selectedCountryId		=	document.forms["form"].country_id[document.forms["form"].country_id.selectedIndex].value;
		
		 if( isset(document.forms["form"].region_id) )
			 selectedRegionId		=	document.forms["form"].region_id[document.forms["form"].region_id.selectedIndex].value;
		
	     if( isset(document.forms["form"].department_id) )
		 	 selectedDepartmentId	=	document.forms["form"].department_id[document.forms["form"].department_id.selectedIndex].value;
		 
  	  	if( isset(document.forms["form"].town_id) )	 
			 selectedTownId			=	document.forms["form"].town_id[document.forms["form"].town_id.selectedIndex].value;
		
		if( isset(document.forms["form"].district_id) )	 	 
		 	selectedDistrictId		=	document.forms["form"].district_id[document.forms["form"].district_id.selectedIndex].value;
	}
	
					
	function updateSelect()
	{
		var params= 'country_id=' + selectedCountryId + '&region_id=' + selectedRegionId + '&department_id=' + selectedDepartmentId + '&town_id=' + selectedTownId + '&district_id=' + selectedDistrictId;
		var xhr = createXMLHttpRequest( );
		xhr.open("POST", "ajaxGet.php", true);
		xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		xhr.setRequestHeader("Content-length", params.length);
		xhr.setRequestHeader("Connection", "close");
	
		xhr.onreadystatechange = function( ) 
		{
			if (xhr.readyState==4) 
			{
				if (xhr.status==200) 
				{
					updateSelectParseXML( xhr.responseText );			
				}
			}
		}
		
		xhr.send(params);
	}
	
	function updateSelectParseXML( text )
	{
		xmlObj = createXMLObject( text );	
		
		
		
		document.forms["form"].country_id.options.length = 0;
		document.forms["form"].country_id.options[0] 	= new Option( 'Oricare', 0);
		var countries = xmlObj.getElementsByTagName("country");
		
		for (var i = 0 ; i < countries.length ; i++) 
		{
			var country = countries[i];
			var name 	= country.getElementsByTagName("name")[0].firstChild.nodeValue;
			var id  	= country.getElementsByTagName("id")[0].firstChild.nodeValue;
			document.forms["form"].country_id.options[i + 1] = new Option( name, id);
			
			if( id == selectedCountryId ) document.forms["form"].country_id.selectedIndex = i + 1;
			
		}
		
		
		if( isset(document.forms["form"].region_id) )
		{
			document.forms["form"].region_id.options.length = 0;
			document.forms["form"].region_id.options[0] 	= new Option( 'Oricare', 0);
			
			
			var regions = xmlObj.getElementsByTagName("region");
			
			for (var i = 0 ; i < regions.length ; i++) 
			{
				var region = regions[i];
				var name 	= region.getElementsByTagName("name")[0].firstChild.nodeValue;
				var id  	= region.getElementsByTagName("id")[0].firstChild.nodeValue;
				document.forms["form"].region_id.options[i + 1] = new Option( name, id);
				
				if( id == selectedRegionId ) document.forms["form"].region_id.selectedIndex = i + 1;
				
			}
		}
		
				
		
		if( isset(document.forms["form"].department_id) )
		{
			document.forms["form"].department_id.options.length = 0;
			document.forms["form"].department_id.options[0] 	= new Option( 'Oricare', 0);
			
			
			var departments = xmlObj.getElementsByTagName("department");
			
			for (var i = 0 ; i < departments.length ; i++) 
			{
				var department = departments[i];
				var name 	= department.getElementsByTagName("name")[0].firstChild.nodeValue;
				var id  	= department.getElementsByTagName("id")[0].firstChild.nodeValue;
				document.forms["form"].department_id.options[i + 1] = new Option( name, id);
				
				if( id == selectedDepartmentId ) document.forms["form"].department_id.selectedIndex = i + 1;
				
			}
		
		}
		
		
		
		
		
		if( isset(document.forms["form"].town_id) )
		{
			document.forms["form"].town_id.options.length = 0;
			document.forms["form"].town_id.options[0] 	= new Option( 'Oricare', 0);
			
			
			var towns = xmlObj.getElementsByTagName("town");
			
			for (var i = 0 ; i < towns.length ; i++) 
			{
				var town = towns[i];
				var name 	= town.getElementsByTagName("name")[0].firstChild.nodeValue;
				var id  	= town.getElementsByTagName("id")[0].firstChild.nodeValue;
				document.forms["form"].town_id.options[i + 1] = new Option( name, id);
				
				if( id == selectedTownId ) document.forms["form"].town_id.selectedIndex = i + 1;
			}
		}
		




		if( isset(document.forms["form"].district_id) )
		{
			document.forms["form"].district_id.options.length = 0;
			document.forms["form"].district_id.options[0] 	= new Option( 'Oricare', 0);
			
			
			var districts = xmlObj.getElementsByTagName("district");
			
			for (var i = 0 ; i < districts.length ; i++) 
			{
				var district = districts[i];
				var name 	= district.getElementsByTagName("name")[0].firstChild.nodeValue;
				var id  	= district.getElementsByTagName("id")[0].firstChild.nodeValue;
				document.forms["form"].district_id.options[i + 1] = new Option( name, id);
				
				if( id == selectedDistrictId ) document.forms["form"].district_id.selectedIndex = i + 1;
			}
		}	
		
	}