var Init = false;

function InitCountries() {
	Init = true;
	var f = document.getElementById('Subscribe');
	var sel = f.auth_country;
	for (var i=0; i<CountriesData.length; i++) {
		sel[sel.options.length] = op = new Option(CountriesData[i][1], CountriesData[i][0]);
		if (CountriesData[i][0] == CountryVal) {
			sel.selectedIndex =  sel.options.length-1;
		}
	}
	sel[sel.options.length] = new Option("Другая...", -1);
	sel.style.width='100%';
	if (CountryVal == -1) sel.selectedIndex =  sel.options.length-1;
	ChangeCountry();
	Init = false;
}

function ChangeCountry() {
	var f = document.getElementById('Subscribe');
	var sel_country = f.auth_country;
	var sel_region = f.auth_region;
	var sel_town = f.auth_town;
	var region_tr = document.getElementById('region_tr');
	var town_tr = document.getElementById('town_tr');
	var town_s_tr = document.getElementById('town_s_tr');
	var custom_place_title = document.getElementById('custom_place_title');

	if (sel_country.value == 0) {
		sel_region.selectedIndex = 0;
		region_tr.style.display = 'none';

		sel_town.selectedIndex = 0;
		town_tr.style.display = 'none';

		f.auth_town_s.value = '';
		town_s_tr.style.display = 'none';
	}

	if (sel_country.value == -1) {
		sel_region.selectedIndex = 0;
		region_tr.style.display = 'none';

		sel_town.selectedIndex = 0;
		town_tr.style.display = 'none';

		if (!Init) f.auth_town_s.value = '';
		town_s_tr.style.display = '';
		custom_place_title.innerHTML = 'Страна, регион, населенный пункт:';
	}
	if (sel_country.value > 0) {
		sel_region.selectedIndex = 0;
		region_tr.style.display = '';
		while (sel_region.options.length>1) sel_region[1] = null;
		for (var i=0; i<RegionData.length; i++) if (RegionData[i][1] == sel_country.value) {
			sel_region[sel_region.options.length] = new Option(RegionData[i][2], RegionData[i][0]);
			if (Init && RegionData[i][0] == RegionVal) {
				sel_region.selectedIndex =  sel_region.options.length-1;
			}
		}

		sel_region[sel_region.options.length] = new Option("Другой...", -1);
		if (Init && RegionVal == -1) sel_region.selectedIndex =  sel_region.options.length-1;

		if (Init) ChangeRegion(); else {
			sel_town.selectedIndex = 0;
			town_tr.style.display = 'none';

			f.auth_town_s.value = '';
			town_s_tr.style.display = 'none';
		}

	}
}

function ChangeRegion() {
	var f = document.getElementById('Subscribe');
	var sel_country = f.auth_country;
	var sel_region = f.auth_region;
	var sel_town = f.auth_town;
	var region_tr = document.getElementById('region_tr');
	var town_tr = document.getElementById('town_tr');
	var town_s_tr = document.getElementById('town_s_tr');
	var custom_place_title = document.getElementById('custom_place_title');

	if (sel_region.value == 0) {
		sel_town.selectedIndex = 0;
		town_tr.style.display = 'none';

		f.auth_town_s.value = '';
		town_s_tr.style.display = 'none';
	}

	if (sel_region.value == -1) {
		sel_town.selectedIndex = 0;
		town_tr.style.display = 'none';

		if (!Init) f.auth_town_s.value = '';
    //town_s_tr.style.display = '';
		custom_place_title.innerHTML = 'Регион, населенный пункт:';
	}

  /*
  if (sel_region.value > 0) {
    sel_town.selectedIndex = 0;
    town_tr.style.display = '';

		while (sel_town.options.length>1) sel_town[1] = null;
		for (var i=0; i<TownData.length; i++) if (TownData[i][1] == sel_region.value) {
			sel_town[sel_town.options.length] = new Option(TownData[i][2], TownData[i][0]);
			if (Init && TownData[i][0] == TownVal) {
				sel_town.selectedIndex =  sel_town.options.length-1;
			}
		}
		sel_town[sel_town.options.length] = new Option("Другой...", -1);
		if (Init && TownVal == -1) sel_town.selectedIndex =  sel_town.options.length-1;

		if (Init) ChangeTown(); else {
			f.auth_town_s.value = '';
			town_s_tr.style.display = 'none';
		}
	}
  */
}

function ChangeTown() {
	var f = document.getElementById('Subscribe');
	var sel_country = f.auth_country;
	var sel_region = f.auth_region;
	var sel_town = f.auth_town;
	var region_tr = document.getElementById('region_tr');
	var town_tr = document.getElementById('town_tr');
	var town_s_tr = document.getElementById('town_s_tr');
	var custom_place_title = document.getElementById('custom_place_title');

	if (sel_town.value == 0) {
		f.auth_town_s.value = '';
		town_s_tr.style.display = 'none';
	}

	if (sel_town.value == -1) {
		if (!Init) f.auth_town_s.value = '';
		town_s_tr.style.display = '';
		custom_place_title.innerHTML = 'Другой населенный пункт:';
	}

	if (sel_town.value > 0) {
		f.auth_town_s.value = '';
		town_s_tr.style.display = 'none';
	}
}
