Charter.SearchForm = function( options )
{
	this.parent = new Charter();
	this.popup = new Popup();
	this.message = new Message();
	this.init( options );
	//Показывать ошибки или нет
	this.showError = true;
};


Charter.SearchForm.prototype.init = function( options )
{
	for ( var i in options )
		this[ i ] = options[ i ];
	
	//Покажем список стран
	this.parent.show( this.blocks.countries );
	//Покажем список городов
	this.parent.show( this.blocks.cities );
	//Отключим disabled у городов
	this.disabled();
};


/**
 * Загружает города по выбранной стране
 */
Charter.SearchForm.prototype.loadCities = function()
{
	var postData = 'country_id=' + $('#' + this.inputs.country_to).val();
	
	this.popup.show( this.loader );
	
	var obSelf = this;
	$.ajax({
		type: 'post',
		url: '/ajax2/?Action=charterLoadCities&rand=' + Math.random(),
		data: postData,
		dataType: 'json',
		success: function( data, textStatus )
		{
			obSelf.popup.hide( obSelf.loader );
			//Вывод ошибок
			if ( data.error != undefined && obSelf.showError )
				obSelf.message.add( 'error', data.error ).show();
			//Выводим список городов
			obSelf.showCities( data.cities );
			//Загружает и показывает календарь с выделенными датами, где есть предложения
			obSelf.loadCalendarOffers();
			//Показываем шаги
			//obSelf.loadSteps();
			//Выводим текст с ссылкой на расписание
			//obSelf.loadLinkToSchedule();
		},
		error: function( data, textError )
		{
			obSelf.message.add( 'error', obSelf.langs.error.critical ).show();
		}
	});
}
/**
 * Показывает города по выбранной стране
 */
Charter.SearchForm.prototype.showCities = function( cities )
{
	var disabled = cities.length ? '' : ' disabled="disabled"';
	
	var html = '<select type="text" id="city_to" name="city_to" style="width: 200px;" onchange="charter.loadCalendarOffers();"' + disabled + '>';
	html += '<option value="">' + this.langs.common.choose_city + '</option>';
	
	for ( i in cities )
	{
		html += '<option value="' + cities[ i ]['id'] + '">' + cities[ i ]['name'] + '</option>';
	}
	
	html += '</select>';
	
	if ( disabled )
		html += '<input type="hidden" name="city_to" />';
	
	$('#' + this.blocks.cities).html( html );
}


/**
 * Загружает и показывает календарь с выделенными датами, где есть предложения
 */
Charter.SearchForm.prototype.loadCalendarOffers = function( options )
{
	//Формируем POST параметры
	var postData = 'city_to=' + $('#' + this.inputs.city_to).val();
	postData += '&city_from=' + this.values.city_from_id;
	
	if ( options != undefined )
	{
		//Если указан день
		if ( options.calendar_day != undefined )
		{
			postData += '&calendar_day=' + options.calendar_day;
		}
		//Если указан месяц
		if ( options.calendar_month != undefined )
		{
			postData += '&calendar_month=' + options.calendar_month;
			this.calendar_month = options.calendar_month;
		}
		//Если указан год
		if ( options.calendar_year != undefined )
		{
			postData += '&calendar_year=' + options.calendar_year;
			this.calendar_year = options.calendar_year;
		}
	}
	else
	{
		//Если уже есть месяц
		if ( this.calendar_month != undefined ) postData += '&calendar_month=' + this.calendar_month;
		//Если уже есть год
		if ( this.calendar_year != undefined ) postData += '&calendar_year=' + this.calendar_year;
	}
	
	postData += '&uri=' + this.values.uri;
	
	this.popup.show( this.loader );
	
	var obSelf = this;
	$.ajax({
		type: 'post',
		url: '/ajax2/?Action=charterloadCalendarOffers&rand=' + Math.random(),
		data: postData,
		dataType: 'json',
		success: function( data, textStatus )
		{
			obSelf.popup.hide( obSelf.loader );
			//Вывод ошибок
			if ( data.error != undefined && obSelf.showError )
				obSelf.message.add( 'error', data.error ).show();
			//Если выбрана дата, то сохраняем
			if ( data.date_from != undefined )
				data.html += '<input type="hidden" id="date_from" name="date_from" value="' + data.date_from + '" />';
			//Выводим календарь
			$('#' + obSelf.blocks.calendar).html( data.html );
			//Показываем шаги
			obSelf.loadSteps();
			//Выводим текст с ссылкой на расписание
			obSelf.loadLinkToSchedule();
		},
		error: function( data, textError )
		{
			obSelf.message.add( 'error', obSelf.langs.error.critical ).show();
		}
	});
}


/**
 * Отключение/Включение disabled
 */
Charter.SearchForm.prototype.disabled = function()
{
	//Если выбрана страна
	if ( $('#' + this.inputs.country_to).val() )
		$('#' + this.inputs.city_to).removeAttr( 'disabled' );
	//Иначе
	else
		$('#' + this.inputs.city_to).attr( 'disabled', 'disabled' );
};


Charter.SearchForm.prototype.loadSteps = function()
{
	var postData = 'country_to=' + $('#' + this.inputs.country_to).val();
	postData += '&city_to=' + $('#' + this.inputs.city_to).val();
	
	this.popup.show( this.loader );
	
	var obSelf = this;
	$.ajax({
		type: 'post',
		url: '/ajax2/?Action=charterLoadSteps&rand=' + Math.random(),
		data: postData,
		dataType: 'json',
		success: function( data, textStatus )
		{
			obSelf.popup.hide( obSelf.loader );
			//Выводим шаги
			$('#steps').html( data.html );
		},
		error: function( data, textError )
		{
			obSelf.message.add( 'error', obSelf.langs.error.critical ).show();
		}
	});
}


Charter.SearchForm.prototype.loadSpecialOffers = function( options )
{
	var postData = 'sort=' + options.sort;
	postData += '&order=' + options.order;
	
	this.popup.show( this.loader );
	
	var obSelf = this;
	$.ajax({
		type: 'post',
		url: '/ajax2/?Action=charterLoadSpecialOffers&rand=' + Math.random(),
		data: postData,
		dataType: 'json',
		success: function( data, textStatus )
		{
			obSelf.popup.hide( obSelf.loader );
			//Вывод ошибок
			if ( data.error != undefined && obSelf.showError )
				obSelf.message.add( 'error', data.error ).show();
			//Выводим шаги
			$('#special_offers').html( data.html );
			
			obSelf.showError = true;
		},
		error: function( data, textError )
		{
			obSelf.message.add( 'error', obSelf.langs.error.critical ).show();
		}
	});
}


Charter.SearchForm.prototype.loadOffersBack = function( options )
{
	//Формируем POST параметры
	var postData = 'city_to=' + this.values.city_to_id;
	postData += '&date_from=' + this.values.date_from;
	postData += '&sort=' + options.sort;
	postData += '&order=' + options.order;

	this.popup.show( this.loader );
	
	var obSelf = this;
	$.ajax({
		type: 'post',
		url: '/ajax2/?Action=charterLoadOffersBack&rand=' + Math.random(),
		data: postData,
		dataType: 'json',
		success: function( data, textStatus )
		{
			obSelf.popup.hide( obSelf.loader );
			//Вывод ошибок
			if ( data.error != undefined && obSelf.showError )
				obSelf.message.add( 'error', data.error ).show();
			//Выводим предложения
			$('#' + obSelf.blocks.offers_back).html( data.html );
			
			obSelf.showError = true;
		},
		error: function( data, textError )
		{
			obSelf.message.add( 'error', obSelf.langs.error.critical ).show();
		}
	});
}


Charter.SearchForm.prototype.loadLinkToSchedule = function()
{
	var postData = 'city_to=' + $('#' + this.inputs.city_to).val();
	postData += '&date_from=' + this.values.date_from;
	
	this.popup.show( this.loader );
	
	var obSelf = this;
	$.ajax({
		type: 'post',
		url: '/ajax2/?Action=charterLoadLinkToSchedule&rand=' + Math.random(),
		data: postData,
		dataType: 'json',
		success: function( data, textStatus )
		{
			obSelf.popup.hide( obSelf.loader );
			//Выводим текст с ссылкой на расписание
			$('#link_to_schedule').html( data.html );
		},
		error: function( data, textError )
		{
			obSelf.message.add( 'error', obSelf.langs.error.critical ).show();
		}
	});
}