date = new Date();
from = date.getTime();
date = new Date(from + from_diff_day * 24 * 3600 * 1000);
var date_from = date.getDate()+'/'+(date.getMonth()+1)+'/'+date.getFullYear();
date = new Date(from + to_diff_day * 24 * 3600 * 1000);
var date_to = date.getDate()+'/'+(date.getMonth()+1)+'/'+date.getFullYear();

if(typeof partner == 'undefined') var partner = false;
if(typeof city_code_from === 'undefined') var city_code_from = false;
if(typeof city_code_to === 'undefined') var city_code_to = false;

var charset = 'windows-1251';
jQuery(function(){
	$('#checkbox_oneway').click(function()
		{
			if(this.checked)
			{
				$('#oneway').val(0);
				$('#tr_oneway1').show();
				$('#tr_oneway2').show();
			}
			else
			{
				$('#oneway').val(1);
				$('#tr_oneway1').hide();
				$('#tr_oneway2').hide();
			}
		});



		var cities = {
				cache: {},
				url: 'http://www.davs.ru/test/sities_suggestion_json.php?search=',
				dataType: "jsonp",

				template: function( ul, item, term )
				{
								term = this.term;
								ir = item.name.replace( new RegExp( '^(' + term + ')', 'i' ), '<span>$1<'+'/span>'  );
								ul.addClass( 'suggestion' );
								 return $( '<li><'+'/li>' )
								.data( 'item.autocomplete', item )
								.append( '<a>' + ir + ' (<spam style="font-size:10px;">' + item.country + '</spam>)<'+'/a>' )
								.appendTo( ul );
				},
				prepare: function( resp ) {
					return $.map(resp, function(e)
					{
					 e['value'] = e.name;
					 return e;
					 });
				},
				select: function (item){

				}};

			$("#FromFull").autocomplete( {format: cities,
				select:function(event, ui) {
					$('#From').val(ui.item.code);
					return true;
				}
			});
			$("#ToFull").autocomplete( {format: cities,
				select:function(event, ui) {
					$('#To').val(ui.item.code);
					return true;
				}
			});



	var dates = $( "#from_date, #to_date" ).datepicker({
		changeMonth: true,
		numberOfMonths: 3,
		onSelect: function( selectedDate ) {
			var option = this.id == "from_date" ? "minDate" : "maxDate",
				instance = $( this ).data( "datepicker" ),
				date = $.datepicker.parseDate(
					instance.settings.dateFormat ||
					$.datepicker._defaults.dateFormat,
					selectedDate, instance.settings );
			dates.not( this ).datepicker( "option", option, date );
			a = selectedDate.split('.');
			date = $.datepicker.parseDate('dd.mm.yy', selectedDate);
			day = date.getDate();
			if(this.id == "from_date")
				$('#DepartureDate').val(day+'/'+(date.getMonth()+1)+'/'+date.getFullYear());
			else
				$('#ArrivalDate').val(day+'/'+(date.getMonth()+1)+'/'+date.getFullYear());
		},
		closeText: 'Закрыть',
		prevText: '&#x3c;Пред',
		nextText: 'След&#x3e;',
		currentText: 'Сегодня',
		monthNames: ['Январь','Февраль','Март','Апрель','Май','Июнь',
		'Июль','Август','Сентябрь','Октябрь','Ноябрь','Декабрь'],
		monthNamesShort: ['Янв','Фев','Мар','Апр','Май','Июн',
		'Июл','Авг','Сен','Окт','Ноя','Дек'],
		dayNames: ['воскресенье','понедельник','вторник','среда','четверг','пятница','суббота'],
		dayNamesShort: ['вск','пнд','втр','срд','чтв','птн','сбт'],
		dayNamesMin: ['Вс','Пн','Вт','Ср','Чт','Пт','Сб'],
		weekHeader: 'Нед',
		dateFormat: 'dd.mm.yy',
		firstDay: 1,
		isRTL: false,
		showMonthAfterYear: false,
		yearSuffix: ''
	});

	if(date_from)
	{
		date = $.datepicker.parseDate('d/m/yy', date_from);
		$( "#to_date" ).datepicker( "option", 'minDate', date );
		$( "#from_date" ).datepicker('setDate', date);
	}
	if(date_to)
	{
		date = $.datepicker.parseDate('d/m/yy', date_to);
		$( "#to_date" ).datepicker('setDate', date);
	}
	//$( "#from_date, #to_date" ).each()
});

if(typeof(orient) != 'undefined' && orient == 'horizontal')
{
	document.write('<div class="small_avia horizontal">');
	document.write('<form name="form1" id="form1" action="'+form_url+'" method="'+form_method+'" onsubmit="return false">');
	document.write('<input name="From" id="From" type="hidden" value="'+city_code_from+'">');
	document.write('<input name="To" id="To" type="hidden" value="'+city_code_to+'">');
	document.write('<input type="hidden" name="DepartureDate" id="DepartureDate" value="'+date_from+'">');
	document.write('<input type="hidden" name="ArrivalDate" id="ArrivalDate" value="'+date_to+'">');
	document.write('<input type="hidden" name="oneway" id="oneway" value="0" />');
	document.write('<input type="hidden" name="oneway2" id="oneway2" value="1" />');
	document.write('<input type="hidden" name="date_type" value="exactly" />');
	document.write('<input type="hidden" name="ADT" value="1" />');
	document.write('<input type="hidden" name="CHD" value="0" />');
	document.write('<input type="hidden" name="INF" value="0" />');
	if(auto_search)
	{
		document.write('<input type="hidden" name="small" value="1" />');
	}
	if(partner)
	{
		document.write('<input type="hidden" name="partner" value="'+partner+'" />');
	}
	if(form_method == 'POST')
		document.write('<input type="hidden" name="aviaticket_form_submit" value="1" />');

	document.write('	<table width="100%">');
	document.write('	<tr>');
	document.write('		<td colspan=3 class=small_aviatitle>Найти и купить авиабилеты </td>');
	document.write('	</tr>');
	document.write('	<tr valign="top">');
	document.write('		<td class=small_aviacontent>');
	document.write('			<div class=small_aviaform>');
	document.write('				<table>');
	document.write('				<tr style="height:23px">');
	document.write('					<td>Город&nbsp;отправления:&nbsp;</td>');
	document.write('					<td></td>');
	document.write('					<td>');
	document.write('						<input class="small_aviacity" name="FromFull" id="FromFull" type="text" maxlength="50" value="'+city_from+'" size="18" />');
	document.write('					</td>');
	document.write('				</tr>');
	document.write('				<tr style="height:24px">');
	document.write('					<td>Город&nbsp;прибытия:</td>');
	document.write('					<td></td>');
	document.write('					<td> ');
	document.write('						<input class="small_aviacity" name="ToFull" id="ToFull" type="text" maxlength="50" value="'+city_to+'" size="18" />');
	document.write('					</td>');
	document.write('				</tr>');
	document.write('				</table>');
	document.write('			</div>');
	document.write('		</td>');
	document.write('		<td>');
	document.write('			<div class=small_aviaform>');
	document.write('				<table>');
	document.write('				<tr class="small_padbot">');
	document.write('					<td colspan=2>Туда</td>');
	document.write('					<td>');
	document.write('						<input class="small_aviacity" type="text" id="from_date"/>');
	document.write('					</td>');
	document.write('					<td>');
	document.write('						<a href="" onclick="$(\'#from_date\').datepicker(\'show\'); return false;" name="depdate" id="depdate"><img src=http://www.davs.ru/smallform/themes/calendarem.gif border=0></a>');
	document.write('					</td>');
	document.write('				</tr>');
	document.write('				<tr class="small_padbot">');
	document.write('					<td>Обратно</td>');
	document.write('					<td>');
	document.write('						<input type="checkbox" id="checkbox_oneway" checked/>');
	document.write('					</td>');
	document.write('					<td id="tr_oneway1">');
	document.write('						<input class="small_aviacity" type="text" id="to_date"/>');
	document.write('					</td>');
	document.write('					<td id="tr_oneway2">');
	document.write('						<a href=""  onclick="$(\'#to_date\').datepicker(\'show\');return false;"  name="arivdate" id="arivdate"><img align=right src=http://www.davs.ru/smallform/themes/calendarem.gif border=0></a>');
	document.write('					</td>');
	document.write('				</tr>');
	document.write('				</table>');
	document.write('			</div>');
	document.write('		</td>');
	document.write('		<td valign="top" >');
	document.write('			<div class=small_find_horizontal><input name="small" class="small_findavia" type="button" onclick="$(\'#form1\').attr(\'onsubmit\', \'\').submit();" value=""></div>');
	document.write('		</td>');
	document.write('	</tr>');
	document.write('	</table>');
	document.write('</div>');
}
else
{
	document.write('<div class="small_avia">');
	document.write('<form name="form1" id="form1" action="'+form_url+'" method="'+form_method+'" onsubmit="return false">');
	document.write('<input type="hidden" name="DepartureDate" id="DepartureDate" value="'+date_from+'">');
	document.write('<input type="hidden" name="ArrivalDate" id="ArrivalDate" value="'+date_to+'">');
	document.write('<input type="hidden" name="oneway" id="oneway" value="0" />');
	document.write('<input type="hidden" name="date_type" value="1-3" />');
	document.write('<input type="hidden" name="ADT" value="1" />');
	if(form_method == 'POST')
		document.write('<input type="hidden" name="aviaticket_form_submit" value="1" />');
	document.write('<table class=small_mainavia>');
	document.write('<TR> <td class=small_aviatitle>Найти и купить авиабилеты </td>');
	document.write('</tr>');
	document.write('<TR> <td class=small_aviacontent>');
	document.write('<div class=small_aviaform>');
	document.write('<table>');
	document.write('<tr>');
	document.write('<td>Город&nbsp;отправления:&nbsp;</td>');
	document.write('<td></td>');
	document.write('<td><input class="small_aviacity" name="From" id="From" type="text" maxlength="50" value="'+city_from+'" size="18" /> </td>');
	document.write('</tr>');
	document.write('<tr>');
	document.write('<td>Город&nbsp;прибытия:</td>');
	document.write('<td></td>');
	document.write('<td> ');
	document.write('<input class="small_aviacity" name="To" id="To" type="text" maxlength="50" value="'+city_to+'" size="18" />');
	document.write('	</td>');
	document.write('</tr>');
	document.write('</table>');
	document.write('<table>');
	document.write('<tr class="small_padbot"><td>Туда</td>');
	document.write('<td><input class="small_aviacity" type="text" id="from_date"/></td><td><a href="" onclick="$(\'#from_date\').datepicker(\'show\');return false;" name="depdate" id="depdate"><img src=http://www.aviazd.com/images/calendarem.gif border=0></a></td></tr>');
	document.write('<tr class="small_padbot"><td>Обратно <input type="checkbox" id="checkbox_oneway" checked/></td>');
	document.write('<td id="tr_oneway1"><input class="small_aviacity" type="text" id="to_date"/></td>');
	document.write('<td id="tr_oneway2"> <a href=""  onclick="$(\'#to_date\').datepicker(\'show\');return false;"  name="arivdate" id="arivdate"><img align=right src=http://www.aviazd.com/images/calendarem.gif border=0></a></td></tr>');
	document.write('</table>');
	document.write('</div>');
	document.write('<div class=small_find><input name="small" class="small_findavia" type="button" onclick="$(\'#form1\').attr(\'onsubmit\', \'\').submit()" value=""></div>');
	document.write('</td></tr>');
	document.write('</table>');
	document.write('</div>');
}

