// JavaScript Document
var WhereGet = {
	cityId: null,
	regionId: null,
	regionField: 'wg1',
	cityField: 'wg2',
	resultId: 'search_result',
	isExecute: false,
	
	initFields: function(type) {
		this.regionId = $('#' + this.regionField).val();
		this.cityId = $('#' + this.cityField).val();
		
		if(type == 'region') {
			if(this.regionId && this.regionId != 0) {
				this.initCities();
			} else {
				$('.city').hide();	
			}
			this.cityId = null;
		} else {
			this.initRegions();
			$('.city').hide();
			this.regionId = null;
			this.cityId = null;
		}
		
		$('.search_result').hide();
	},
	
	initRegions: function() {
		if(!this.isExecute) {
			this.isExecute = true;
			
			$.ajax({
				async: false,
			   	type: 'POST',
			   	url: '/ajax/where_get/',
			   	data: 'action=get_regions',
				success: function(r){
					var xml = r;
					
					if(xml) {
						var success = xml.getElementsByTagName('success');
						
						if(success.length) {
							var items = success[0].getElementsByTagName('item');
							if(items.length) {
								var obj_select = document.getElementById(WhereGet.regionField);
								obj_select.options.length = 0;
								obj_select.options[0] = new Option('Выберите регион', 0);
								var i = 1;
								$(xml).find('item').each(
									function() {
										obj_select.options[i] = new Option($(this).text(), $(this).attr('id'));
										i++;
									}
								);
							}
						}
					} else {
						alert('Произошла ошибка. Пожалуйста, обратитесь к администратору.');
					}
					
					WhereGet.isExecute = false;
				},
				error: function() {
					alert('Произошла ошибка. Пожалуйста, обратитесь к администратору.');
				},
				complete: function() {
					WhereGet.isExecute = false;
				}
			});
			
		} else {
			alert('Выполняется, подождите...');
		}
	},
	
	initCities: function() {
		if(!this.isExecute) {
			this.isExecute = true;
			
			$.ajax({
				async: false,
			   	type: 'POST',
			   	url: '/ajax/where_get/',
			   	data: 'action=get_cities&region_id=' + this.regionId,
				success: function(r){
					var xml = r;
					
					if(xml) {
						var success = xml.getElementsByTagName('success');
						
						if(success.length) {
							var items = success[0].getElementsByTagName('item');
							if(items.length) {
								
								var obj_select = document.getElementById(WhereGet.cityField);
								obj_select.options.length = 0;
								obj_select.options[0] = new Option('Выберите город', 0);
								var i = 1;
								$(xml).find('item').each(
									function() {
										obj_select.options[i] = new Option($(this).text(), $(this).attr('id'));
										i++;
									}
								);
								$('.city').show();
							}
						}
						
					} else {
						alert('Произошла ошибка. Пожалуйста, обратитесь к администратору.');
					}
					
					WhereGet.isExecute = false;
				},
				error: function() {
					alert('Произошла ошибка. Пожалуйста, обратитесь к администратору.');
				},
				complete: function() {
					WhereGet.isExecute = false;
				}
			});
			
		} else {
			alert('Выполняется, подождите...');
		}
	},
	
	getResult: function(_page) {
		this.regionId = $('#' + this.regionField).val();
		this.cityId = $('#' + this.cityField).val();
		if(this.cityId && this.cityId != '0' && this.regionId && this.regionId != '0') {
			$('#' + this.resultId).load('/ajax/where_get/result/', {
			   	city_id: this.cityId,
				region_id: this.regionId,
				page: _page
			}, function() {$('.search_result').show();});
		}
	}
};