function dg_AddOption(element, value, text)
{
	
	 /*
	newOption = createElement("OPTION");
	newOption.text = text;
	newOption.value = value;
	*/
	document.anketa.elements[element].options[document.anketa.elements[element].options.length] = new Option(text,value,false,false);

	//anketa.elements[element].options.add(newOption, anketa.elements[element].length);
	 
}
function dg_ForceUpdate(type, lang)
{	
	//document.anketa.elements[this.region_el].disabled = true;
	
	this.UplType = type;
	
	var country = document.anketa.elements[this.country_el].options[document.anketa.elements[this.country_el].selectedIndex].value;

	while((document.anketa.elements[this.town_el].options.length-1)>0){
		
		document.anketa.elements[this.town_el].options[document.anketa.elements[this.town_el].options.length-1] = null;

	}
	document.anketa.elements[this.town_el].disabled = true;
	//document.getElementById('data_frame').document.write("");
	if(document.all){data_frame.document.write = "";}

	
/*
	for (i = document.anketa.elements[this.town_el].options.length - 1; i >= 1 ; i--)
	{
		try{
			document.anketa.elements[this.region_el].options[document.anketa.elements[this.region_el].options.length-1] = null;
		}catch(){
			e
		}
			//document.anketa.elements[this.town_el].options.remove(i);
	}*/
			
		
	if (type == 1)
	{
				
		if (country == 0)
		{	
		
			document.anketa.elements[this.region_el].disabled = true;
			document.anketa.elements[this.town_el].disabled = true;
			return;
		}
		
		/*
		for (i = document.anketa.elements[this.region_el].options.length - 1; i >= 1 ; i--)
		{
			//document.anketa.elements[this.region_el].options.remove(i);
			//document.anketa.elements[this.region_el].options.remove(i);
			document.anketa.elements[this.region_el].options[document.anketa.elements[this.region_el].options.length-1] = null;
		}
		*/
		while((document.anketa.elements[this.region_el].options.length-1)>0){
		
			document.anketa.elements[this.region_el].options[document.anketa.elements[this.region_el].options.length-1] = null;

		}
		document.anketa.elements[this.region_el].disabled = true;
		//alert(document.getElementById('data_frame'));
		//alert(1);
		//document.write("geo_data.php?lang=" + this.lang + "&country=" + country);
		document.getElementById('data_frame').src = "geo_data.php?lang=" + this.lang + "&country=" + country;
	
		//data_frame.location.href = "geo_data.php?lang=" + this.lang + "&country=" + country;
		
		//document.write(data_frame.location.href);
		this.CheckDataLoaded();
	}
	else
	{
		var region = document.anketa.elements[this.region_el].options[document.anketa.elements[this.region_el].selectedIndex].value;
		if (region == 0){
			document.anketa.elements[this.town_el].disabled = true;
		}else
		{	
			
			
			document.getElementById('data_frame').src = "geo_data.php?lang=" + this.lang + "&country=" + country + "&region=" + region;
			
			//data_frame.location.href = "geo_data.php?lang=" + this.lang + "&country=" + country + "&region=" + region;
			//document.write(data_frame.location.href);
			this.CheckDataLoaded();
		}
	}
}
function dg_CheckDataLoaded()
{
	
	
	self.clearTimeout(this.to);
	if (this.cur_el == null)
	{
		
		if (this.UplType == 1)
			this.cur_el = document.anketa.elements[this.region_el];
		else
			this.cur_el = document.anketa.elements[this.town_el];
			
		this.tmp = this.cur_el.options[0].text;
		
		this.cur_el.options[0].text = "Пожалуйста, подождите.";
		if(document.all){
			//data_frame.document.body.innerHTML = "";
		}
		this.to = window.setTimeout(this.obj_name + ".CheckDataLoaded()", 500);
		return;
	}
	//alert(3)
	this.cur_el.options[0].text += '.';
	//alert(document.getElementById('data_frame').document.getElementById('data_div'));
	if(document.all){
		//alert(4)
		try{
			if (data_frame.data_div)
			{
				//alert(5);
				//clearInterval(this.to);
				var str = data_frame.document.body.innerText.split(",");
				//alert(str);
				if (this.UplType == 1)
				{
					for (i = 0; i < str.length; i+=2){
						this.AddOption(this.region_el, str[i], str[i+1]);
						//alert("orig: "+str[i+1]);
					}
					
					anketa.elements[this.region_el].selectedIndex = 0;
					anketa.elements[this.region_el].disabled = false;
				}
				else
				{
					for (i = 0; i < str.length; i+=2){
						this.AddOption(this.town_el, str[i], str[i+1])
					}
					if(str.length==2){
						anketa.elements[this.town_el].selectedIndex = 1;
					}
					else{
						anketa.elements[this.town_el].selectedIndex = 0;
					}
					anketa.elements[this.town_el].disabled = false;
				}
				
				//alert(str.length);
				data_frame.document.body.innerText = "";
				this.cur_el.options[0].text = this.tmp;
				this.cur_el = null;
				try{
					data_frame.document.close();
					data_frame.document.write = "";
				}
				catch(e){
				}
				if(this.UplType == 1 && str.length==2){
						anketa.elements[this.region_el].selectedIndex = 1;
						this.ForceUpdate(2);
				}
			}
			else{
				//alert("pause");
				this.to = window.setTimeout(this.obj_name + ".CheckDataLoaded()", 1000);
			}
		}catch(e){
			//alert("fehler");
			//self.clearTimeout(this.to);
			this.to = window.setTimeout(this.obj_name + ".CheckDataLoaded()", 1000);
		}
	}
}

function CDynamicGeo(obj_name, lang, country_el, region_el, town_el)
{
	this.to = -1;
	this.cur_el = null;
	this.obj_name = obj_name;
	this.UplType = 0;
	this.lang = lang;
	this.country_el = country_el;
	this.region_el = region_el;
	this.town_el = town_el;

	this.AddOption = dg_AddOption;
	this.ForceUpdate = dg_ForceUpdate;
	this.CheckDataLoaded = dg_CheckDataLoaded;
}