$verybignumer = 1000000;
$step = 1;
booked_days = new Array();
best_days = new Array();

$days_count = 1;

function scrolltoh1(){
	var destination = $('#content h1').offset().top - 15;
	var w = $.browser.webkit ? $('body') : $('html');
	w.animate({ scrollTop: destination}, 400 );
}

function set_tr_functions(){
	$('tr.choose_room_or_special').each(function(){
		$(this).find('span.money').show();
		$(this).find('select').show();
		$(this).removeClass('booked_tr');
	});
}

function refresh_all_info(){

$('table.all_info_table').animate({
    opacity: 0
  }, 1000, function() {
		$('table.all_info_table').html('');
		$date_in = $('input[name="date_in"]').val();
		$date_out = $('input[name="date_out"]').val();
		$people = $('select[name="people"]').val();
		$children = $('select[name="children"]').val();

		if ($step == 1){
			$('table.all_info_table').append('<tr><td><p>К Вашим услугам одноместные и двухместные номера с одной большой или двумя раздельными кроватями. При необходимости возможно предоставление дополнительного места..</p></td></tr>');
		}

		if ($step == 5){
			$('table.all_info_table').append('<tr><td><p><b>Условия отмены</b>: данное бронирование является негарантированным. Негарантированное бронирование автоматически отменяется в&nbsp;16:00.</p></td></tr>');
			$('table.all_info_table').append('<tr><td><p>Предоставление копии (двусторонней) кредитной карты является необходимым условием для гарантирования Вашего бронирования. Во&nbsp;избежание удержания штрафа в&nbsp;размере стоимости одной ночи проживания, просим Вас сообщить об&nbsp;отмене бронирования до&nbsp;16:00 дня заселения в&nbsp;отель. В&nbsp;случае незаезда, без предварительного уведомления, отель оставляет за&nbsp;собой право удержать штраф, равным полной стоимости одной ночи проживания в&nbsp;номере подтвержденной категории.</p></td></tr>');
			$('table.all_info_table').append('<tr><td><p><b>Информация о&nbsp;заселении</b>: время заезда в&nbsp;номер с&nbsp;14:00, время выезда до&nbsp;12:00. Ранний заезд и&nbsp;поздний выезд осуществляются по&nbsp;предварительной договоренности в&nbsp;зависимости от&nbsp;загрузки отеля. Заезд в&nbsp;номер до&nbsp;6:00 оплачивается дополнительно в&nbsp;размере полной стоимости одной ночи проживания в&nbsp;отеле. Заезд в&nbsp;номер с&nbsp;6:00 до&nbsp;14:00 оплачивается дополнительно в&nbsp;размере половины стоимости одной ночи проживания в&nbsp;отеле. Выезд из&nbsp;номера после 12:00 до&nbsp;18:00 оплачивается дополнительно в&nbsp;размере половины стоимости одной ночи проживания в&nbsp;отеле. Выезд из&nbsp;номера после 18:00 оплачивается дополнительно в&nbsp;размере полной стоимости одной ночи проживания в&nbsp;отеле.</p></td></tr>');
			$('table.all_info_table').append('<tr><td><p>Просьба указать предполагаемое время заезда в&nbsp;разделе &laquo;Особые пожелания&raquo; во&nbsp;избежание отмены бронирования.</p></td></tr>');
		}

		if ($step == 2 || $step == 3 || $step == 4){
			if ($date_in != '') $('table.all_info_table').append('<tr><td class="text-right">Дата заезда</td><td><span><nobr>'+$date_in+'</nobr></span></td></tr>');
			if ($date_out != '') $('table.all_info_table').append('<tr><td class="text-right">Дата выезда</td><td><span><nobr>'+$date_out+'</nobr></span></td></tr>');
			$('table.all_info_table').append('<tr><td class="text-right"><nobr>Количество гостей</nobr></td><td> </td></tr>');
			if ($people != '') $('table.all_info_table').append('<tr><td class="text-right">Взрослые</td><td><span>'+$people+'</span></td></tr>');
			if ($children != '') $('table.all_info_table').append('<tr><td class="text-right">Дети</td><td><span>'+$children+'</span></td></tr>');
		}

		if ($step == 2){
			$('table.all_info_table').append('<tr><td colspan="2"><p>&nbsp;</p></td></tr>');
			$('table.all_info_table').append('<tr><td colspan="2"><p>&nbsp;</p></td></tr>');
			$('table.all_info_table').append('<tr><td colspan="2"><table><tr><td><p>*</p></td><td><p> для&nbsp;выбора номера или&nbsp;спецпредложения, кликните по&nbsp;соответствующей строчке</p></td></tr></table></td></tr>');
		}

		$all_price = 0;
		if ($step == 3 || $step == 4){
			$room_or_spec = $('tr.active');
			if( parseInt($room_or_spec.find('span.id').html()) ) $isroom = 1; else $isroom = 0;
			if ($isroom){
				$name = $room_or_spec.find('span.name').html();
				$price_all = $room_or_spec.find('span.price_all').html();
				$count = $room_or_spec.find('select').val();
				if (!$count) $count = 1;
				$html = '<tr><td class="text-right">Номер</td>';
				$html += '<td>'+$name+' ('+$count+')<br/>'+$price_all*$count+' рублей</td></tr>';
				$all_price = parseInt($price_all*$count);
			}
			else{
				$name = $room_or_spec.find('span.name').html();
				$price = $room_or_spec.find('span.price').html();
				$html = '<tr><td class="text-right">Спецпредложение</td>';
				$html += '<td>'+$name+'<br/>'+$price+' рублей</td></tr>';
				$all_price = parseInt($price);
			}
			$('table.all_info_table').append($html);
		}

		if ($step == 3){
			$('table.all_info_table').append('<tr><td colspan="2"><p>&nbsp;</p></td></tr>');
			$('table.all_info_table').append('<tr><td colspan="2"><p>&nbsp;</p></td></tr>');
			$('table.all_info_table').append('<tr><td colspan="2"><table><tr><td><p>*</p></td><td><p> для&nbsp;выбора дополнительной услуги, кликните по&nbsp;соответствующей строчке</p></td></tr></table></td></tr>');
		}

		if ($step == 4){
			$services_count = $('tr.active_service').length;
			if($services_count){
				$html_service = '<tr><td class="text-right">Дополнительные услуги</td><td></td></tr>';
				$('tr.active_service').each(function(){
					$id_service = $(this).find('span.id').html();
					$name_service = $(this).find('span.name').html();
					$count_service = $(this).find('select[name="servicecount"]').val();
					$price_service = $(this).find('span.price').html();
					if($count_service) {
						$html_service += '<tr><td class="text-right">'+$name_service+' ('+$count_service+')</td><td>'+$price_service*$count_service+' рублей</td></tr>';
						$all_price += parseInt($price_service*$count_service);
					}
					else{
						if ($id_service == 10) $price_service = parseInt($price_service*$days_count);
						$all_price += parseInt($price_service);
						$html_service += '<tr><td class="text-right">'+$name_service+'</td><td>'+$price_service+' рублей</td></tr>';
					}

				});
				$('table.all_info_table').append($html_service);
			}
			$('table.all_info_table').append('<tr class="underlined"><td class="text-right">Общая сумма</td><td>'+$all_price+' рублей</td></tr>');
		}

	});
	$('table.all_info_table').animate({opacity: 1}, 1000);

}

// 0 в "нет", 1 в "да"
function danet($var){
	if ($var==1) return 'да';
	return 'нет';
}

function validate(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false) {
      //alert('Invalid Email Address');
      return false;
   }
   return true;
}

function show_message(text,isok){
	if (isok) text += '<br/><br/><div style="text-decoration: underline; cursor: pointer;" class="close_message">OK</a>';
	$.blockUI({ message: text, timeout: 200000, css: {
		border: 'none',
		padding: '15px',
		backgroundColor: '#000',
		'-webkit-border-radius': '10px',
		'-moz-border-radius': '10px',
		opacity: .7,
		color: '#fff',
		fontSize: '20px'
	} });
	$('.blockOverlay').attr('title','Click to unblock').click($.unblockUI);
	if (isok) {
		$('div.close_message').live("click",$.unblockUI);
	}
}

function bestAndBooked(date, inMonth) {
	if (inMonth) {
		for (i = 0; i < best_days.length; i++) {
			if (date.getMonth() + 1 == best_days[i][0] &&
					date.getDate() == best_days[i][1]) {
				return {dateClass: 'best_day'};
			}
		}
	}
	return {};
}

$(document).ready(function(){
	refresh_all_info();

	$('.booked_div').click(function(){
		//saveUserInfo();
	});

	$('select.servicecount').live('click', function(){
		//$(this).parent().parent().click();
		$(this).parent().parent().addClass('active_service');
		return false;
	});

	$('select[name="roomcount"]').live('click', function(){
		$(this).parent().parent().addClass('active');
	});

	$.ajax({
		type: "POST",
		beforeSend: function(){
			$('#ajax_loading').css('background','url(/images/icons/ajax-loader.gif) top left no-repeat');
		},
		url: "/ob-admin/frontend/best.php",
		success: function(msg){
			i = 0;
			$(msg).find('span.item_best').each(function(){
				$month = parseInt($(this).find('span.month').html());
				$day = parseInt($(this).find('span.day').html());
				best_days.splice(i, 0, [$month,$day]);
				i++;
			});
			i = 0;
			$(msg).find('span.item_booked').each(function(){
				$month = parseInt($(this).find('span.month').html());
				$day = parseInt($(this).find('span.day').html());
				booked_days.splice(i, 0, [$month,$day]);
				i++;
			});

			$('#date_in_datepicker').datepick({
				changeMonth: false,
				minDate: 0,
				maxDate: +124,
				defaultDate: '0',
				onDate: bestAndBooked,
				selectDefaultDate: true,
				//showTrigger: '#calImg',
				onSelect: function(){
					$date = $(this).datepick('getDate')[0];
					$('input[name="date_in"]').val($.datepick.formatDate('dd-mm-yyyy', $date));
					$('input[name="date_in_php"]').val($.datepick.formatDate('yyyy-mm-dd', $date));
				}
			});
			$('#date_out_datepicker').datepick({
				changeMonth: false,
				minDate: 0,
				maxDate: +124,
				defaultDate: '+1',
				onDate: bestAndBooked,
				selectDefaultDate: true,
				//showTrigger: '#calImg',
				onSelect: function(){
					$date = $(this).datepick('getDate')[0];
					$('input[name="date_out"]').val($.datepick.formatDate('dd-mm-yyyy', $date));
					$('input[name="date_out_php"]').val($.datepick.formatDate('yyyy-mm-dd', $date));
				}
			});
		},
		complete: function(){
			$('#ajax_loading').css('background','none');
		}
	});

	$date = $.datepick.add($.datepick.today(), 1, 'd');
	$('input[name="date_in"]').val($.datepick.formatDate('dd-mm-yyyy', $.datepick.today()));
	$('input[name="date_out"]').val($.datepick.formatDate('dd-mm-yyyy', $date));
	$('input[name="date_in_php"]').val($.datepick.formatDate('yyyy-mm-dd', $.datepick.today()));
	$('input[name="date_out_php"]').val($.datepick.formatDate('yyyy-mm-dd', $date));

	$.ajax({
		type: "POST",
		beforeSend: function(){
			$('#ajax_loading').css('background','url(/images/icons/ajax-loader.gif) top left no-repeat');
		},
		complete: function(){
			$('#ajax_loading').css('background','none');
		},
		url: "/ob-admin/frontend/rooms_and_specials.php",
		success: function(msg){
			$('#step2').html(msg);
			$('a.foto').each(function(){ $(this).lightBox(); });
			$('tr.choose_room_or_special').find('td:first').tooltip({
				delay: 0,
				track: true,
				fade: 250,
				showURL: false,
				extraClass: "w500",
				bodyHandler: function() {
					return $(this).find('span.description').html();
				}
			});
		}
	});

	$.ajax({
		type: "POST",
		beforeSend: function(){
			$('#ajax_loading').css('background','url(/images/icons/ajax-loader.gif) top left no-repeat');
		},
		complete: function(){
			$('#ajax_loading').css('background','none');
		},
		url: "/ob-admin/frontend/services.php",
		success: function(msg){
			$('#step3').html(msg);
			$('a.foto').each(function(){ $(this).lightBox(); });
			$('tr.choose_service').find('td:first').tooltip({
				delay: 0,
				track: true,
				fade: 250,
				showURL: false,
				extraClass: "w500",
				bodyHandler: function() {
					return $(this).find('span.description').html();
				}
			});
		}
	});

	$('#step2').hide();
	$('#step3').hide();
	$('#step4').hide();
	$('#step5').hide();

	$('a.next_step').click(function(){
		if($step==1){
			$correct = true;
			$what = '';
			if($correct){
				$date_in = $('input[name="date_in_php"]').val();
				$date_out = $('input[name="date_out_php"]').val();
				$request = 'date_in='+$date_in+'&date_out='+$date_out;
				$.ajax({
					type: "POST",
					beforeSend: function(){
						$('#ajax_loading').css('background','url(/images/icons/ajax-loader.gif) top left no-repeat');
					},
					complete: function(){
						$('#ajax_loading').css('background','none');
					},
					data: $request,
					url: "/ob-admin/frontend/prices.php",
					success: function(msg){
						//$hasbooked = $(msg).find('#hasbooked').text();
						$days_count = $(msg).find('#days_count').text(); //alert(msg); alert($days_count);
						$hasbooked = $(msg).find('#hasbooked').text();
						$hasbooked_standart = $(msg).find('#hasbooked_standart').text();
						$hasbooked_comfort = $(msg).find('#hasbooked_comfort').text();
						$hasbooked_lux = $(msg).find('#hasbooked_lux').text();
						$hasbooked_president = $(msg).find('#hasbooked_president').text(); //alert($hasbooked_president);
						$in_more_out = $(msg).find('#in_more_out').text();
						if($in_more_out){
							//if($hasbooked) show_message('Выбраны дни, когда все номера выкуплены',true);
							if($in_more_out) show_message('Дата заезда позднее даты выезда',true);
						}
						else{
							$standart = $(msg).find('span.price_standart').html();
							$standart_all = $(msg).find('span.price_standart_all').html();
							$comfort = $(msg).find('span.price_comfort').html();
							$comfort_all = $(msg).find('span.price_comfort_all').html();
							$lux = $(msg).find('span.price_lux').html();
							$lux_all = $(msg).find('span.price_lux_all').html();
							$president = $(msg).find('span.price_president').html();
							$president_all = $(msg).find('span.price_president_all').html();

							set_tr_functions();
							$('tr.choose_room_or_special').each(function(){
								if($(this).find('span.id').html()=='1') {
									$(this).find('span.price').html($standart);
									$(this).find('span.price_all').html($standart_all);
									if($hasbooked_standart) {
										$(this).find('span.price').html('Извините, в указанные Вами даты данный тип номеров не доступен');
										$(this).find('span.money').hide();
										$(this).find('select').hide();
										$(this).addClass('booked_tr');
									}
								}
								if($(this).find('span.id').html()=='2') {
									$(this).find('span.price').html($comfort);
									$(this).find('span.price_all').html($comfort_all);
									if($hasbooked_comfort) {
										$(this).find('span.price').html('Извините, в указанные Вами даты данный тип номеров не доступен');
										$(this).find('span.money').hide();
										$(this).find('select').hide();
										$(this).addClass('booked_tr');
									}
								}
								if($(this).find('span.id').html()=='3') {
									$(this).find('span.price').html($lux);
									$(this).find('span.price_all').html($lux_all);
									if($hasbooked_lux) {
										$(this).find('span.price').html('Извините, в указанные Вами даты данный тип номеров не доступен');
										$(this).find('span.money').hide();
										$(this).find('select').hide();
										$(this).addClass('booked_tr');
									}
								}
								if($(this).find('span.id').html()=='4') {
									$(this).find('span.price').html($president);
									$(this).find('span.price_all').html($president_all);
									if($hasbooked_president) {
										$(this).find('span.price').html('Извините, в указанные Вами даты данный тип номеров не доступен');
										$(this).find('span.money').hide();
										$(this).find('select').hide();
										$(this).addClass('booked_tr');
									}
								}
							});
							$('#step1').hide();
							$('#step2').show();
							$('a.prev_step').show();
							$('#step_count').text('2');

							$('select[name="roomcount"]').eq(0).val(1);
							$('select[name="roomcount"]').eq(1).val(1);
							$('select[name="roomcount"]').eq(2).val(1);

							$people_count = parseInt($('select[name="people"]').val()) + parseInt($('select[name="children"]').val());
							if ($people_count > 3) {
								$('select[name="roomcount"]').eq(0).val(2);
								$('select[name="roomcount"]').eq(1).val(2);
								$('select[name="roomcount"]').eq(2).val(2);
								$('select[name="roomcount"]').find('option').each(function(){
									if($(this).val() == '1') $(this).hide();
								});
								$('select[name="roomcount"]:last').find('option').each(function(){
									if($(this).val() == '1') $(this).show();
								});
							}

							$step=2;
							refresh_all_info();
							scrolltoh1();
						}
					}
				});
			}
			else{

			}
			return false;
		}
		if($step==2){
			$correct = true;
			$what = '';
			if ( $('tr.active').length < 1 ) { $correct = false; $what = 'Вы не выбрали номер либо спецпредложение'; }
			if($correct){
				if( !parseInt($('tr.active').find('span.id').html()) ) {
					$('tr.choose_service').each(function(){
						$id = $(this).find('span.id').html();
						if ($id != 5 && $id != 3 && $id != 4) {$(this).removeClass('active_service').hide(); }
					});
				}
				else{
					$room_id = parseInt($('tr.active').find('span.id').html());
					if($room_id != 1){
						$('tr.choose_service').each(function(){
							$id = $(this).find('span.id').html();
							if ($id != 8 && $id != 1 && $id != 2 && $id != 3 && $id != 4) {
								//$(this).hide();
								$(this).removeClass('active_service').hide();
							}
						});
					}
				}

				$room_id = parseInt($('tr.active').find('span.id').html());
				if($room_id == 1){
					$('tr.choose_service').eq(0).removeClass('active_service').hide();
					$('tr.choose_service').eq(1).removeClass('active_service').hide();
				}
				if (($room_id == 1) || ($room_id == 2)){
					$('tr.choose_service').each(function(){
						$id = $(this).find('span.id').html();
						if ($id == 10) $(this).show();
					});
				}

				$breakfest_price = $('tr.choose_service').find('span.price').eq(0);
				$breakfest_price.html(parseInt($breakfest_price.html())*$days_count);
				$breakfest_price = $('tr.choose_service').find('span.price').eq(1);
				$breakfest_price.html(parseInt($breakfest_price.html())*$days_count);

				$('#step2').hide();
				$('#step3').show();
				$('#step_count').text('3');
				$step=3;
				refresh_all_info();
				scrolltoh1();
			}
			else{
				show_message($what,true);
			}
			return false;
		}
		if($step==3){
			$correct = true;
			$what = '';
			if($correct){
				$('#step3').hide();
				$('#step4').show();
				$('#step_count').text('4');
				$step=4;
				refresh_all_info();
				scrolltoh1();
			}
			else{
				//show_message($what,true);
			}
			return false;
		}
		if($step==4){
			$correct = true;
			$what = '';
			$email = $('#contact_form').find('input[name="email"]').val();
			if( !validate($email) ) { $correct = false; $what = 'Введите корректный email адрес'; };
			if( $('#contact_form').find('input[name="phone"]').val() == '') { $correct = false; $what = 'Поле &laquo;Контактный телефон&raquo; должно быть заполнено'; };
			if( $('#contact_form').find('input[name="email"]').val() == '') { $correct = false; $what = 'Поле &laquo;E-mail&raquo; должно быть заполнено'; };
			if( $('#contact_form').find('input[name="fio"]').val() == '') { $correct = false; $what = 'Поле &laquo;ФИО&raquo; должно быть заполнено'; };

			if($correct){
				$prename = $('#contact_form').find('select[name="prename"]').val();
				$fio = $('#contact_form').find('input[name="fio"]').val();
				$index = $('#contact_form').find('input[name="index"]').val();
				$country = $('#contact_form').find('input[name="country"]').val();
				$city = $('#contact_form').find('input[name="city"]').val();
				$adress = $('#contact_form').find('input[name="adress"]').val();
				$email = $('#contact_form').find('input[name="email"]').val();
				$phone = $('#contact_form').find('input[name="phone"]').val();
				$pay_method = $('#contact_form').find('input[name="pay_method"]:checked').val();
				$info = $('#contact_form').find('textarea[name="info"]').val();
				$('table.confirm').html('');
				if ($prename != '') $('table.confirm').append('<tr class="fio"><td class="text-right">Обращение</td><td><span class="fio">'+$prename+'</span></td></tr>');
				if ($fio != '') $('table.confirm').append('<tr class="fio"><td class="text-right">ФИО</td><td><span class="fio">'+$fio+'</span></td></tr>');
				if ($index != '') $('table.confirm').append('<tr class="index"><td class="text-right">Индекс</td><td><span class="index">'+$index+'</span></td></tr>');
				if ($country != '') $('table.confirm').append('<tr class="country"><td class="text-right">Страна</td><td><span class="country">'+$country+'</span></td></tr>');
				if ($city != '') $('table.confirm').append('<tr class="city"><td class="text-right">Город</td><td><span class="city">'+$city+'</span></td></tr>');
				if ($adress != '') $('table.confirm').append('<tr class="adress"><td class="text-right">Улица, дом</td><td><span class="adress">'+$adress+'</span></td></tr>');
				if ($email != '') $('table.confirm').append('<tr class="email"><td class="text-right">E-mail</td><td><span class="email">'+$email+'</span></td></tr>');
				if ($phone != '') $('table.confirm').append('<tr class="phone"><td class="text-right">Контактный телефон</td><td><span class="phone">'+$phone+'</span></td></tr>');
				if ($pay_method != '') $('table.confirm').append('<tr class="pay_method"><td class="text-right">Метод оплаты</td><td><span class="pay_method">'+$pay_method+'</span></td></tr>');
				if ($info != '') $('table.confirm').append('<tr class="info"><td class="text-right">Особые пожелания</td><td><span class="info">'+$info+'</span></td></tr>');

				$date_in = $('input[name="date_in_php"]').val();
				$date_out = $('input[name="date_out_php"]').val();
				$people = $('select[name="people"]').val();
				$children = $('select[name="children"]').val();

				if ($date_in != '') $('table.confirm').append('<tr><td class="text-right">Дата заезда</td><td><span><nobr>'+$date_in+'</nobr></span></td></tr>');
				if ($date_out != '') $('table.confirm').append('<tr><td class="text-right">Дата выезда</td><td><span><nobr>'+$date_out+'</nobr></span></td></tr>');
				$('table.confirm').append('<tr><td class="text-right">Количество гостей</td><td> </td></tr>');
				if ($people != '') $('table.confirm').append('<tr><td class="text-right">Взрослые</td><td><span>'+$people+'</span></td></tr>');
				if ($children != '') $('table.confirm').append('<tr><td class="text-right">Дети</td><td><span>'+$children+'</span></td></tr>');

				$all_price = 0;

				$room_or_spec = $('tr.active');
				if( parseInt($room_or_spec.find('span.id').html()) ) $isroom = 1; else $isroom = 0;
				if ($isroom){
					$name = $room_or_spec.find('span.name').html();
					$price_all = $room_or_spec.find('span.price_all').html();
					$count = $room_or_spec.find('select[name="roomcount"]').val();
					if (!$count) $count = 1;
					$html = '<tr><td class="text-right">Номер</td>';
					$html += '<td>'+$name+' ('+$count+')<br/>'+$price_all*$count+' рублей</td></tr>';
					$all_price = parseInt($price_all*$count);
				}
				else{
					$name = $room_or_spec.find('span.name').html();
					$price = $room_or_spec.find('span.price').html();
					$html = '<tr><td class="text-right">Спецпредложение</td>';
					$html += '<td>'+$name+'<br/>'+$price+' рублей</td></tr>';
					$all_price = parseInt($price);
				}
				$('table.confirm').append($html);

				$services_count = $('tr.active_service').length;
				if($services_count){
					$html_service = '<tr><td class="text-right">Дополнителные услуги</td><td></td></tr>';
					$('tr.active_service').each(function(){
						$name_service = $(this).find('span.name').html();
						$count_service = $(this).find('select[name="servicecount"]').val();
						$price_service = $(this).find('span.price').html();
						if($count_service) {
							$html_service += '<tr><td class="text-right">'+$name_service+' ('+$count_service+')</td><td>'+$price_service*$count_service+' рублей</td></tr>';
							$all_price += parseInt($price_service*$count_service);
						}
						else{
							$html_service += '<tr><td class="text-right">'+$name_service+'</td><td>'+$price_service+' рублей</td></tr>';
							$all_price += parseInt($price_service);
						}

					});
					$('table.confirm').append($html_service);
				}
				$('table.confirm').append('<tr><td class="text-right">Общая сумма</td><td>'+$all_price+' рублей</td></tr>');

				if($prename == 'Г-н (господин)') $words = 'ый господин';
				if($prename == 'Г-жа (госпожа)') $words = 'ая госпожа';
				if($prename == 'Г-н и Г-жа') $words = 'ые господин и госпожа';
				if($prename == 'Доктор') $words = 'ый доктор';

				$('#step5').find('span.words').html($words);
				$('#step5').find('span.words_fio').html($fio);

				$('#step4').hide();
				$('#step5').show();
				$('#step_count').text('5');
				$step=5;
				refresh_all_info();
				scrolltoh1();
				$('a.next_step').hide();
				$('#send_message').show();
			}
			else{
				show_message($what,true);
			}
			return false;
		}
		return false;
	});

	$('a.prev_step').click(function(){
		if ($step==2) {
			$('tr.choose_room_or_special').removeClass('active');
			$('select[name="roomcount"]').find('option').each(function(){
				$(this).show();
			});
			$('#step2').hide(); $('#step1').show(); $step=1; $(this).hide(); refresh_all_info(); scrolltoh1();
		};
		if ($step==3) { $('#step3').hide(); $('#step2').show(); $step=2;  refresh_all_info(); scrolltoh1();
			if( !parseInt($('tr.active').find('span.id').html()) ) {
				$('tr.choose_service').show();
				$('tr.choose_service:first').prev().show();
			}
			$('tr.choose_service').show();

			$breakfest_price = $('tr.choose_service').find('span.price').eq(0);
			$breakfest_price.html(parseInt($breakfest_price.html())/$days_count);
			$breakfest_price = $('tr.choose_service').find('span.price').eq(1);
			$breakfest_price.html(parseInt($breakfest_price.html())/$days_count);

		};
		if ($step==4) { $('#step4').hide(); $('#step3').show(); $step=3; refresh_all_info(); scrolltoh1(); };
		if ($step==5) { $('#step5').hide(); $('#step4').show(); $step=4; $('a.next_step').show(); $('#send_message').hide(); refresh_all_info(); scrolltoh1(); };
		$('#step_count').text(parseInt($('#step_count').text())-1);
		return false;
	});

	$('tr.choose_room_or_special').live('click', function(){
		if($(this).hasClass('booked_tr')){
		}
		else{
			$('tr.choose_room_or_special').removeClass('active');
			$(this).addClass('active');
		}
	});

	$('tr.choose_service').live('click', function(){
		if ( $(this).hasClass('active_service') ) $(this).removeClass('active_service'); else $(this).addClass('active_service');
	});


	$('#send_message').click(function(){
		$date_in = $('input[name="date_in"]').val(); // date_in
		$date_out = $('input[name="date_out"]').val();  // date_out
		$date_in_php = $('input[name="date_in_php"]').val(); // date_in_php
		$date_out_php = $('input[name="date_out_php"]').val(); // date_out_php
		$people = $('select[name="people"]').val(); // people
		$children = $('select[name="children"]').val(); // children
		// 2 шаг
		$room_or_spec_id = $('tr.active').find('span.id').html(); // room_or_spec_id
		if(!$room_or_spec_id) $room_or_spec_id = 0;
		$roomcount = $('tr.active').find('select[name="roomcount"]').val(); // roomcount
		if(!$roomcount) $roomcount = 1;
		$room_or_spec_price = 0; // room_or_spec_price
		if($('tr.active').find('span.price').html())
			{$room_or_spec_price = $('tr.active').find('span.price').html(); }
		if($('tr.active').find('span.price_all').html())
			{$room_or_spec_price = $('tr.active').find('span.price_all').html();}
		$room_or_spec_name = '';	 // room_or_spec_name
		if($('tr.active').find('span.name').html())
			{$room_or_spec_name = $('tr.active').find('span.name').html(); }
		$services = new Array(); // services
		i = 0;
		$('tr.active_service').each(function(){
			$id = $(this).find('span.id').html();
			$name = $(this).find('span.name').html();
			$price = $(this).find('span.price').html();
			$count = 0;
			if ( $(this).find('select[name="servicecount"]').val() ) $count = $(this).find('select[name="servicecount"]').val();
			$services.splice(i, 0, [$id,$name,$price,$count]);
			i++;
		});
		$prename = $('#contact_form').find('select[name="prename"]').val();
		$fio = $('#contact_form').find('input[name="fio"]').val();
		$index = $('#contact_form').find('input[name="index"]').val();
		$country = $('#contact_form').find('input[name="country"]').val();
		$city = $('#contact_form').find('input[name="city"]').val();
		$adress = $('#contact_form').find('input[name="adress"]').val();
		$email = $('#contact_form').find('input[name="email"]').val();
		$phone = $('#contact_form').find('input[name="phone"]').val();
		$pay_method = $('#contact_form').find('select[name="pay_method"]').val();
		$info = $('#contact_form').find('textarea[name="info"]').val();

                

		$.ajax({
			type: "POST",
			url: "/ob-admin/frontend/send.php",
			data:
			{	date_in: $date_in,
				date_out: $date_out,
				date_in_php: $date_in_php,
				date_out_php: $date_out_php,
				people: $people,
				children: $children,
				room_or_spec_id: $room_or_spec_id,
				room_or_spec_name: $room_or_spec_name,
				room_or_spec_price: $room_or_spec_price,
				roomcount: $roomcount,
				services: $services,
				prename: $prename,
				fio: $fio,
				index: $index,
				country: $country,
				city: $city,
				adress: $adress,
				email: $email,
				phone: $phone,
				pay_method: $pay_method,
				info: $info
			},
			beforeSend: function(){
				$('#ajax_loading').css('background','url(/images/icons/ajax-loader.gif) top left no-repeat');
			},
			complete: function(){
				$('#ajax_loading').css('background','none');
			},
			success: function(data){
				show_message('Заявка отправлена', true);
				$('#send_message').hide();
				$('.prev_step').hide();
				$('table.all_info_table').fadeOut(500);
				$('#step5').html('<p>Ваша заявка будет обработана в течение 24 часов. Подтверждение бронирования будет выслано на указанный Вами адрес электронной почты.</p>');
				$('#step5').append(data);
			}
		});

	});
});

