function showCondition() {
$("#condition").dialog({
    bgiframe: true,
    autoOpen: false,
    height: 510,
    width: 700,
    modal: true,
    resizable: false,
	buttons: { 'Закрыть': function() { $("#condition").dialog('close'); } }
	});
$("#condition").show();
$("#condition").dialog('open');
}
$(function() {
	var theDate = new Date();
	var curYear = (theDate.getYear() < 2000) ? theDate.getYear()+1900 : theDate.getYear();
	$.datepicker.setDefaults( {dateFormat: 'dd.mm.yy', changeMonth: true, changeYear: true, duration: 'fast', yearRange: curYear+':'+(curYear*1+10),
		monthNamesShort: ['Январь','Февраль','Март','Апрель','Май','Июнь','Июль','Август','Сентябрь','Октябрь','Ноябрь','Декабрь'],
		dayNamesMin: ['Вс', 'Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб'] }, $.datepicker.regional['ru'] );
	$("#fromDate").datepicker({
		showOn: 'both', buttonImage: '/images/a_calendar.gif', buttonImageOnly: true,
		onSelect: function(dateText, inst) {
			var selDate = dateText.split('.');
			$.post('/ajax/reloadhotelprice.php', {hotelId:$('#hotelId').val(), hotelRatio:$('#hotelRatio').val(), currDate:selDate[2]+'-'+selDate[1]+'-'+selDate[0]},
			function(result){ $('#priceTblBody').html(result); });
			addNewDatePicker(selDate);
		}
	});
});
function addNewDatePicker(selDate) {
	$('#toDate').remove();
	$('#orderToDate').html('');
	$('#orderToDate').append('по&nbsp;&nbsp;<input id="toDate" type="text" class="datePickerInputPrice" readonly /><span id="nummDaysSpan"></span>');
	$('#toDate').datepicker({
		showOn: 'both', buttonImage: '/images/a_calendar.gif', buttonImageOnly: true,
		minDate: new Date(selDate[2], selDate[1] - 1, selDate[0]*1 + 1),
		onSelect: function(dateText, inst) {
			var fromDateConverted = Date.parse(selDate[1]+'/'+selDate[0]+'/'+selDate[2]);
			var selToDate = dateText.split('.');
			var toDateConverted = Date.parse(selToDate[1]+'/'+selToDate[0]+'/'+selToDate[2]);
			var numNights = Math.round((toDateConverted-fromDateConverted)/86400000);
			$('#nummDaysSpan').html('');
			$('#nummDaysSpan').append('&nbsp;&nbsp;&nbsp;&nbsp;кол-во ночей:&nbsp;<span id="nummDays" style="font-weight:bold">'+Math.ceil(numNights)+'</span>');
			calculatePrice();
		}
	});

}
function calculatePrice() {
	$('#allPrice').html('');
	$('.roomNum').each(function(){
		var room = $(this).attr('rel');
		var val = $(this).val();
		var numDays = ($('#nummDays').text() != '') ? $('#nummDays').text() : 1;
		if(val != '') $('#price'+room).html( '<span class="summ">'+$('#'+room).text()*val*numDays+'</span>$' );
		else $('#price'+room).html('');
	});
	var summ = 0;
	$('.summ').each(function(){
		summ += $(this).text()*1;
	});
	if (summ != 0) $('#allPrice').html(summ+'$');
}
function doOrder() {
if ( $('#allPrice').text() != '' && $('#nummDays').text() != '' ) {
	$('#doOrderRooms').html('');

	$('#doOrderFromDate').text( $('#fromDate').val() );
	$('#doOrderToDate').text( $('#toDate').val() );
	$('#doOrderNumNight').text( $('#nummDays').text() );

	var doOrderPriceList = '<p>';

	$('.roomNum').each(function(){
		if( $(this).val() != '' ) {
			var roomId = $(this).attr('rel');
			doOrderPriceList += 'Номер: <strong>'+$('#roomName'+roomId).text()+'</strong>&nbsp;&nbsp;цена:&nbsp;<strong>'
							 +$('#'+roomId).text()+'$</strong>&nbsp;&nbsp;количество:&nbsp;<strong>'+$(this).val()
							 +'</strong>.&nbsp;&nbsp;&nbsp;Сумма:&nbsp;<strong class="blue">'+$('#price'+roomId).text()+'</strong><br />';
		}
	});
	doOrderPriceList += '</p><p>Общая сумма заказа: <strong class="blue" style="font-size:11pt">'+$('#allPrice').text()+'</strong><p>';
	$('#doOrderRooms').append(doOrderPriceList);

	if( $.cookie('clientName') != '' && $.cookie('clientName') != null ) $('#clientName').val($.cookie('clientName'));
	if( $.cookie('clientCountry') != '' && $.cookie('clientCountry') != null ) $('#clientCountry').val($.cookie('clientCountry'));
	if( $.cookie('clientPhone') != '' && $.cookie('clientPhone') != null ) $('#clientPhone').val($.cookie('clientPhone'));
	if( $.cookie('clientEmail') != '' && $.cookie('clientEmail') != null ) $('#clientEmail').val($.cookie('clientEmail'));

	$("#order").dialog({
	    bgiframe: true,
	    autoOpen: false,
	    width:390,
	    modal: true,
	    resizable: false,
	    close: function() { $('#errorDivOrder').html('').hide(); },
		buttons: {
		'Отмена': function() {
				$('#errorDivOrder').html('').hide();
				$("#order").dialog('close');
			},
		'Заказать': function() {
				loader();
				$('#errorDivOrder').html('').hide();
				var orderBody = $('#orderBody').html();
				var clientName = $('#clientName').val();
				var clientCountry = $('#clientCountry').val();
				var clientPhone = $('#clientPhone').val();
				var clientEmail = $('#clientEmail').val();
				var clientNote = $('#clientNote').val();

				$.cookie('clientName', clientName, {expires: 90});
				$.cookie('clientCountry', clientCountry, {expires: 90});
				$.cookie('clientPhone', clientPhone, {expires: 90});
				$.cookie('clientEmail', clientEmail, {expires: 90});

				$.post('/ajax/hotelorder.php', {orderBody:orderBody, clientName:clientName, clientCountry:clientCountry, clientPhone:clientPhone, clientEmail:clientEmail, clientNote:clientNote},
				function(result){
					loader();
					if(result.substr(0,2) == 'ok') {
	        			var result = result.substr(2);
	        			$("#order").dialog('close');
	        			$("#orderOk").html(result);
	        			$("#orderOk").dialog({
						    bgiframe: true,
						    autoOpen: false,
						    modal: true,
						    resizable: false,
							buttons: { 'Закрыть': function() { $(this).dialog('close'); } }
							});
						$("#orderOk").show();
						$("#orderOk").dialog('open');
					} else {
						$('#errorDivOrder').html(result).show();
					}
				});
			}
		}
		});
	$("#order").show();
	$("#order").dialog('open');
} else {
	$("#orderError").dialog({
	    bgiframe: true,
	    autoOpen: false,
	    modal: true,
	    resizable: false,
		buttons: { 'Закрыть': function() { $("#orderError").dialog('close'); } }
		});
	$("#orderError").show();
	$("#orderError").dialog('open');
}
}
