// JavaScript Document
var AutoWhereGet = {
	cityId: null,
	makeId: null,
	cityField: 'wg1',
	makeField: 'wg2',
	resultId: 'search_result',
	isExecute: false,
	
	initFields: function(type) {
		this.cityId = $('#' + this.cityField).val();
		this.makeId = $('#' + this.makeField).val();
		
		if(type == 'city') {
			if(this.cityId && this.cityId != 0) {
				this.initMakes();
			} else {
				$('.make').hide();	
			}
			this.makeId = null;
		} else {
			this.initCities();
			$('.make').hide();
			this.cityId = null;
			this.makeId = null;
		}
		
//		this.initCities();
		$('.search_result').hide();
	},
	
	initCities: function() {
		if(!this.isExecute) {
			this.isExecute = true;
			query = '';
			if (this.makeId) { query = '&make_id='+this.makeId; }
			$.ajax({
				async: false,
			   	type: 'POST',
			   	url: '/ajax/auto_where_get/',
			   	data: 'action=get_cities'+query,
				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(AutoWhereGet.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++;
									}
								);
							}
						}
					} else {
						alert('Произошла ошибка. Пожалуйста, обратитесь к администратору.');
					}
					
					AutoWhereGet.isExecute = false;
				},
				error: function() {
					alert('Произошла ошибка. Пожалуйста, обратитесь к администратору.');
				},
				complete: function() {
					AutoWhereGet.isExecute = false;
				}
			});
			
		} else {
			alert('Выполняется, подождите...');
		}
	},
	
	initMakes: function() {
		if(!this.isExecute) {
			this.isExecute = true;
			
			query = '';
			if (this.cityId) { query = '&city_id='+this.cityId; }
			$.ajax({
				async: false,
			   	type: 'POST',
			   	url: '/ajax/auto_where_get/',
			   	data: 'action=get_makes'+query,
				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(AutoWhereGet.makeField);
								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++;
									}
								);
								$('.make').show();
							}
						}
						
					} else {
						alert('Произошла ошибка. Пожалуйста, обратитесь к администратору.');
					}
					
					AutoWhereGet.isExecute = false;
				},
				error: function() {
					alert('Произошла ошибка. Пожалуйста, обратитесь к администратору.');
				},
				complete: function() {
					AutoWhereGet.isExecute = false;
				}
			});
			
		} else {
			alert('Выполняется, подождите...');
		}
	},
	
	getResult: function(_page) {
		this.cityId = $('#' + this.cityField).val();
		this.makeId = $('#' + this.makeField).val();
		if(this.makeId && this.makeId != '0' && this.cityId && this.cityId != '0') {
			$('#' + this.resultId).load('/ajax/auto_where_get/result/', {
			   	make_id: this.makeId,
				city_id: this.cityId,
				page: _page
			}, function() {$('.search_result').show();});
		}
	}
};
