﻿function fill_city()
{
	var selectCountry = document.getElementById("ad_region");
	if(!selectCountry)return;
	
	var optCity = document.getElementById("ad_subregion");
	if(!optCity)return;
	
	var optProp = document.getElementById("ad_prop");
	if(!optProp)return;
	
	if(selectCountry.value==-1)
	{
		
		for(y = optCity.options.length;y>0;y--)//mazání selectu
		{
			if(y!=0){optCity.options[y]=null;}
		}
		if(optCity.options.length>0) optCity.options.selectedIndex=0;
		
		for(y = optProp.options.length;y>0;y--)//mazání selectu
		{
			if(y!=0){optProp.options[y]=null;}
		}
		if(optProp.options.length>0) optProp.options.selectedIndex=0;
		return;
			
	}
	var cityAr = cityArray[selectCountry.value].toString().split(';');
	var i,y;
	
	
	for(y = optCity.options.length;y>0;y--)//mazání selectu
	{
		if(y!=0){optCity.options[y]=null;}
	}
	
	for(i = 0;i<cityAr.length;i++)//nové naplnění selectu
	{
		var val = cityAr[i].split(',');
		addOption(optCity,val[1],val[0]);
	}
	if(optCity.options.length==2)
	{
		optCity.options.selectedIndex=1;
		fill_property();
	}
}
function fill_property()
{
	var selectCountry = document.getElementById("ad_subregion");
	if(!selectCountry)return;
	var optProp = document.getElementById("ad_prop");
	if(!optProp)return;
	if(selectCountry.value==-1)
	{
		for(y = optProp.options.length;y>0;y--)//mazání selectu
		{
			if(y!=0){optProp.options[y]=null;}
		}
		if(optProp.options.length>0) optProp.options.selectedIndex=0;
		return;
	}
	var propAr = residenceArray[selectCountry.value].toString().split(';');
	var i,y;
	
	
	for(y = optProp.options.length;y>0;y--)//mazání selectu
	{
		if(y!=0){optProp.options[y]=null;}
	}
	
	for(i = 0;i<propAr.length;i++)//nové naplnění selectu
	{
		var val = propAr[i].split(',');
		addOption(optProp,val[1],val[0]);
	}
	if(optProp.options.length==2)
	{
		optProp.options.selectedIndex=1;
	}
	
}

function addOption(selectObject,optionText,optionValue) {
    var optionObject = new Option(optionText,optionValue)
    var optionRank = selectObject.options.length
    selectObject.options[optionRank]=optionObject
}

function deleteOption(selectObject,optionRank) {
    if (selectObject.options.length!=0) { selectObject.options[optionRank]=null }
}
function goToProperty()
{
	var country = document.getElementById("ad_region");
	var city = document.getElementById("ad_subregion");
	var prop = document.getElementById("ad_prop");
	if(country && city && prop)
	{
		if(city.value==-1 && prop.value==-1){alert("You must select city!");return;}
		if(prop.value==-1)
		{
			var i = 0;
			var hr = "";
			var y = 0;
			for(y=0;y<cityArray.length;y++)
			{
				var cv = cityArray[y].toString().split(';');
				for(i = 0;i<cv.length;i++)
				{
					var pv = cv[i].split(',');
					if(pv[0]==city.value)hr=pv[2];
				}	
			}
			document.location=hr;
		}
		else
		{
			var i = 0;
			var hr = "";
			var y = 0;
			for(y=0;y<residenceArray.length;y++)
			{
				var cv = residenceArray[y].toString().split(';');
				for(i = 0;i<cv.length;i++)
				{
					var pv = cv[i].split(',');
					if(pv[0]==prop.value)hr=pv[2];
				}	
			}
			document.location=hr;
		}
	}
}
