// JavaScript Document
var Where = {
	cityId: null,
	typeId: null,
	tubeId: null,
	typeField: 'sd2',
	cityField: 'sd3',
	tubeField: 'sd4',
	resultId: 'search_result',
	isExecute: false,
	
	initFields: function(type) {
		this.typeId = $('#' + this.typeField).val();
		this.cityId = $('#' + this.cityField).val();
		
		if(type == 'type') {
			if(this.typeId && (this.typeId == '00003')) {
				$('#' + this.resultId).html('Оплату кредита вы можете произвести в ближайшем отделении «Почта России» вашего города.');
				$('.city, .tube').hide();
				$('.search_result').show();
				$('#search_result').show();
			} else if(this.typeId && this.typeId != 0) {
				this.initCities();
				$('#button_search').hide();
				$('.search_result').hide();
			} else {
				$('.city, .tube, #button_search').hide();	
				$('.search_result').hide();
			}
			$('.tube').hide();
		} else if(type == 'city') {
			if(this.cityId && this.cityId != 0) {
				this.initTube();
				$('#button_search').show();
			}
			$('.search_result').hide();
		} else {
			this.initTypes();
			$('.city, .tube, #button_search').hide();
			$('.search_result').hide();
		}
	},
	
	initTypes: function() {
		if(!this.isExecute) {
			this.isExecute = true;
			
			$.ajax({
				async: false,
			   	type: 'POST',
			   	url: '/ajax/where/',
			   	data: 'action=get_types',
				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(Where.typeField);
								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++;
									}
								);
								//obj_select.options[i] = new Option('Не имеет значения...', 'all');
							}
						}
					} else {
						alert('Произошла ошибка. Пожалуйста, обратитесь к администратору.');
					}
					
					Where.isExecute = false;
				},
				error: function() {
					alert('Произошла ошибка. Пожалуйста, обратитесь к администратору.');
				},
				complete: function() {
					Where.isExecute = false;
				}
			});
			
		} else {
			alert('Выполняется, подождите...');
		}
	},
	
	initCities: function() {
		if(!this.isExecute) {
			this.isExecute = true;
			
			$.ajax({
				async: false,
			   	type: 'POST',
			   	url: '/ajax/where/',
			   	data: 'action=get_cities&type_id=' + this.typeId,
				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(Where.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('Произошла ошибка. Пожалуйста, обратитесь к администратору.');
					}
					
					Where.isExecute = false;
				},
				error: function() {
					alert('Произошла ошибка. Пожалуйста, обратитесь к администратору.');
				},
				complete: function() {
					Where.isExecute = false;
				}
			});
			
		} else {
			alert('Выполняется, подождите...');
		}
	},
	
	initTube: function() {
		if(!this.isExecute && this.cityId && this.cityId != 0) {
			this.isExecute = true;
			
			$.ajax({
				async: false,
			   	type: 'POST',
			   	url: '/ajax/where/',
			   	data: 'action=get_tube&city_id=' + this.cityId + '&type_id=' + this.typeId,
				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(Where.tubeField);
								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++;
									}
								);
								$('.tube').show();
							}
						} else {
							$('.tube').hide();
						}
						
					} else {
						alert('Произошла ошибка. Пожалуйста, обратитесь к администратору.');
					}
					
					Where.isExecute = false;
				},
				error: function() {
					alert('Произошла ошибка. Пожалуйста, обратитесь к администратору.');
				},
				complete: function() {
					Where.isExecute = false;
				}
			});
			
		} else {
			$('.tube').hide();
		}
	},
	
	getResult: function(_page) {
		this.typeId = $('#' + this.typeField).val();
		this.cityId = $('#' + this.cityField).val();
		this.tubeId = $('#' + this.tubeField).val();
		if(this.cityId && this.typeId) {
			$('#' + this.resultId).load('/ajax/where/result/', {
			   	city_id: this.cityId,
				type_id: this.typeId,
				tube_id: this.tubeId,
				page: _page
			}, function() {$('.search_result').show();});
		}
	}
};