$(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'] );
	$(".datePickerExIn").datepicker({
		showOn: 'both', buttonImage: '/images/a_calendar.gif', buttonImageOnly: true
	});
});

function doMainOrder(){
	$('#trTitle').val('');
	$('#trPrice').val('');
	var trTitle = new Array();
	var trPrice = new Array();
	var i = 0;
	$("input:checked").each(function(){
		trTitle[i] = $(this).attr('title');
		trPrice[i] = $(this).val();
		i++;
	});
	trTitle = trTitle.join('|');
	trPrice = trPrice.join('|');
	if( trTitle != '' && trPrice != '' ){
		$('#trTitle').val(trTitle);
		$('#trPrice').val(trPrice);
		$('#mainOrder').submit();
	}
}

function doOrder() {
$('#orderBody').html('');
$('#errorDivOrder').html('').hide();
var dateVal = '';
$('.datePickerExIn').each(function(){
	if ($(this).val() == '' || $(this).val() == 'undefined') dateVal = 1;
});
if (dateVal != '') {
	$("#orderError").dialog({
	    bgiframe: true,
	    autoOpen: false,
	    modal: true,
	    resizable: false,
		buttons: { 'Закрыть': function() { $("#orderError").dialog('close'); } }
		});
	$("#orderError").show();
	$("#orderError").dialog('open');
} else {
	if ($('#allPrice').text() != '') {
		loader();
		$('#orderBody').html($('#orderPreBody').html());
		$('#orderBody .datePickerExIn').each(function(){
			var textVal = $(this).val();
			$(this).parent().text(textVal);
			$(this).remove();
		});
		$('#orderBody .numPeople').each(function() {
			var textVal = $(this).val();
			$(this).parent().text(textVal);
			$(this).remove();
		});
		$('#orderBody img').remove();
		var orderBody = $('#orderBodyDiv').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);
				$("#resultOrder").html(result);
				$("#resultOrder").append('<div class="spacer15"></div><p class="cText"><a href="#" onclick="window.location.href=window.location.href; return false;">Вернуться</a></p>');
			}
			else {
				$('#errorDivOrder').html(result).show();
			}
		});
	}
}
}

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 calculatePrice() {
	$('#allPrice').html('');
	$('.numPeople').each(function(){
		var id = $(this).attr('rel');
		var val = $(this).val();
		if(val != '') $('#rowSumm'+id).html( '<span class="summ">'+$('#price'+id).text()*val+'</span>$' );
		else $('#rowSumm'+id).html('');
	});
	var summ = 0;
	$('.summ').each(function(){
		summ += $(this).text()*1;
	});
	if (summ != 0) $('#allPrice').html(summ+'$');
}

