function CountryOnChange( countryId, suffixId ) {
	ClearSelectData("region" + suffixId);
	ClearSelectData("city" + suffixId);
	if (countryId) {
		if (
			24 == countryId || //BY 
			172 == countryId || //RU 
			214 == countryId //UK
		) {
			SetRegionsByCountry(currentLanguage, countryId, "region" + suffixId); 
		}
		else {
			SetCitiesByCountry(currentLanguage, countryId, "city" + suffixId); 
		}
	}
}
function RegionOnChange( regionId, suffixId ) {
	ClearSelectData("city" + suffixId);
	if (regionId) {
		SetCitiesByRegion(currentLanguage, regionId, "city" + suffixId); 
	}
}

function SetSelectData( elementId, data ) {
	for (var i=0; i< data.length; ++i) {
		$("#" + elementId).append(
			"<option value='" +
			data[i]['id'] +
			"'>" +
			data[i]['name'] +
			"</option>"
		);
	}
	$("#" + elementId)[0].selectedIndex = -1;
	$("#" + elementId)[0].disabled = false;
}
function ClearSelectData( elementId ) {
	$("#" + elementId)[0].disabled = true; 
	$("#" + elementId).empty();
}
function LoadJSON( url, selectElementId ) {
	$.getJSON(url,
		function(result){
			SetSelectData(selectElementId, result);
		}
	);
}

function SetRegionsByCountry( lang, countryId, selectElementId ) {
	var url = "/" + lang + "/route/getregionsbycountryajax/country/" + countryId;
	LoadJSON(url, selectElementId);
}
function SetCitiesByRegion( lang, regionId, selectElementId ) {
	var url = "/" + lang + "/route/getcitiesbyregionajax/region/" + regionId;
	LoadJSON(url, selectElementId);
}
function SetCitiesByCountry( lang, countryId, selectElementId ) {
	var url = "/" + lang + "/route/getcitiesbycountryajax/country/" + countryId;
	LoadJSON(url, selectElementId);
}

function ShowNotify(cityName, cityType) {
	$('li.citiesField').append("<div class='cityNotify' id='notify_" + cityType + "_block'>" + 
						   messageCitiesNotExists1 + 
						   cityName + 
						   messageCitiesNotExists2 + 
						   " &nbsp; <a href='#' onclick='return AddCity(\"" + cityType + "\");'>" +
						   controlCitiesNotExistsYes +
						   "</a>" +
						   " &nbsp; <a href='#' onclick='return ClearCityFields(\"" + cityType + "\")'>" +
						   controlCitiesNotExistsNo +
						   "</a>" +
						   "</div>");
}
function SubmitAddRouteForm() {
	if (!$('#city_from_name').val() || !$('#city_to_name').val()) {
		$('#' + formName).submit();
		return false;
	}
	
	$('#notify_from_block').remove();
	$('#notify_to_block').remove();
	ClearServerErrorsMessages();
	unexistingCitiesCount = 0;

	$.getJSON('/' + currentLanguage + '/city/checkcityexistsajax/city/' + encodeURIComponent($('#city_from_name').val()),
		function(result){
			if (!result) {
				ShowNotify($('#city_from_name').val(), 'from');
				++unexistingCitiesCount;
			}

			$.getJSON('/' + currentLanguage + '/city/checkcityexistsajax/city/' + encodeURIComponent($('#city_to_name').val()),
				function(result){
					if (result && !unexistingCitiesCount) {
						$('#' + formName).submit();
					}
					else {
						if (!result && $('#city_from_name').val() != $('#city_to_name').val()) {
							ShowNotify($('#city_to_name').val(), 'to');
							++unexistingCitiesCount;
						}
					}
				}
			);	
			
		}
	);

	return false;
}

function AddCity(cityType) {
	$.getJSON('/' + currentLanguage + '/city/adduncheckedcityajax/city/' + encodeURIComponent($('#city_' + cityType + '_name').val()),
		function(result) {
			if (result) {
				$('#notify_' + cityType + '_block').remove();
				$('#city_' + cityType + '_name').parent().children('.errors').remove();
				--unexistingCitiesCount;
				unexistingCitiesCount = Math.max(unexistingCitiesCount, 0);
				if (!unexistingCitiesCount) 
					SubmitAddRouteForm();
			}
		}
	);
	//--unexistingCitiesCount;
}
function ClearCityFields(cityType) {
	if ($('#city_from_name').val() == $('#city_to_name').val()) {
		ClearCityField('from');
		ClearCityField('to');
	}
	else {
		ClearCityField(cityType);
	}
}
function ClearCityField(cityType) {
	$('#city_' + cityType + '_name').val("");
	$('#notify_' + cityType + '_block').remove();
	--unexistingCitiesCount;
	unexistingCitiesCount = Math.max(unexistingCitiesCount, 0);
	$('#city_' + cityType + '_name').parent().children('.errors').remove();
	return false;
}

function ClearServerErrorsMessages() {
	if ($("input[name=user_status_id]:checked").length)
		$("#user_status_id-1").parent().parent().children("ul.errors").remove();
	if ($("#city_from_name").val())
		$("#city_from_name").parent().children("ul.errors").remove();
	if ($("#city_to_name").val())
		$("#city_to_name").parent().children("ul.errors").remove();
}

$(document).ready(function(){
	$("#countryfrom").change( function() { CountryOnChange(Number(this.value), "from"); } );
	$("#regionfrom").change( function() { RegionOnChange(Number(this.value), "from"); } );
	$("#countryto").change( function() { CountryOnChange(Number(this.value), "to"); } );
	$("#regionto").change( function() { RegionOnChange(Number(this.value), "to"); } );
});