
var path = '/templates/default_site/redstars.group/reservation/';

function change_pic(obj,src){
	$('.ramka:visible').removeClass('ramka').addClass('ramka_none');
	$(obj).addClass('ramka');
	$('.mainpic img:visible').attr('src',src);
}


$(document).ready(function(){
   
    //month_s = {'1':'Январь','2':'Февраль','3':'Март','4':'Апрель','5':'Май','6':'Июнь','7':'July','8':'August','9':'September','10':'Октябрь','11':'Ноябрь','12':'Декабрь'};
    //month_p = {'1':'января','2':'февраля','3':'марта','4':'апреля','5':'мая','6':'июня','7':'июля','8':'августа','9':'сентября','10':'октября','11':'ноября','12':'декабря'};
	
    var difference = 2;
    //1step
    if($('#from_date').length>0)
    {
    	var from_str = $('#from_date').val(),to_str = $('#to_date').val();
    	mass1 = from_str.split('.');
    	//p1 = mass1[0]+ ' ' + month_p[Number( mass1[1])] +' ' + mass1[2];
    	//console.log(p1);
///$('.from_title').html(p1);
    	//mass2 = to_str.split('.');
///p2 = mass2[0]+ ' ' +month_p[Number(mass2[1])] + ' ' +mass2[2];
    	//console.log(p2);
    	
    	
    	var first_date= new Date(mass1[2],Number(mass1[1])-1,mass1[0]);
    	//var first_date = $.datepicker.parseDate("dd.mm.yy", from_str);
    }
    
    var click = 1, from = new Date, to = new Date;
   
    $('#back_month').live("click", function(){
    	DP_jQuery.datepicker._adjustDate('#d_picker_1', -1, 'M'); 	
    });
    $('#next_month').live("click", function(){
    	DP_jQuery.datepicker._adjustDate('#d_picker_1', 1, 'M');
    });
    
    var first_show = 1;
	var one_day=1000*60*60*24;
    $("#d_picker_1").datepicker({
		firstDay: 1,
		numberOfMonths: 2,
		minDate : new Date,
		dateFormat: "dd.mm.yy",
		showButtonPanel: false,
		changeMonth: false,
		defaultDate:first_date,
		onSelect: function( selectedDate ) {
			var format = $(this).data("datepicker").settings.dateFormat || $.datepicker._defaults.dateFormat, date = $.datepicker.parseDate(format, selectedDate);
			if (click++%2){	
				from = date;
				to = date;
				from_str = selectedDate;
				to_str = selectedDate;
				$("#all_night").val(1);
				$('#from_date').val(from_str);
				$('#to_date').val(to_str);
			} else {
				if (date < from){
					from = date;
					from_str = selectedDate;
				} else {
					to = date;
					to_str = selectedDate;
				}
				$('#from_date').val(from_str);
				$('#to_date').val(to_str);
				h = Math.ceil((to.getTime() - from.getTime())/(one_day));
				if(h==0)h=1;
				$("#all_night").val(h);
			}
			$(this).datepicker("refresh");
			return false;
		},
		beforeShowDay : function(date){
			if(first_show = 1) {
				first_show = 0;
				format = $(this).data("datepicker").settings.dateFormat || $.datepicker._defaults.dateFormat,
				from = $.datepicker.parseDate(format, from_str);
				to = $.datepicker.parseDate(format, to_str);
			}
			if (date >= from && date <= to){
				return [true, "ui-state-highlight"];
			}
			return [true, ""];
			
		},
		onChangeMonthYear : function(year, month, inst) { 
			if(month>12)month = month%12;
			next_month = month+1;
			if(next_month>12)next_month = 1;
			$('#current_str').html(month_s[month] + '-' + month_s[next_month] + ' '+year);
		}
		//$("#d_picker_1").datepicker("refresh");
	});
   
    $('.get_days').live("click", function(){
    	//Расшифровка по дням
    	$.post(
    			 path + 'bron_ajax.php',
    			  {
    				 step:'get_days'
    			  },
    			  onAjaxSuccess
    			);
       	function onAjaxSuccess(data)
       	{
       		$('.show_days').html(data);
       	}
    	
    });
	
	
    //2step
    $(".room_type").find('td > a').live("click", function(){
    	$(this).parents('.room_type').parent().hide();
    	show_id = $(this).attr("type_id");
    	$('#room_type_'+show_id).show();
    	$('#room_type').val(show_id);
    	//$('.type_title').html(pr_t);
    	
    	//$('#count_people').val($("select.count_people:visible").val());
    	
    	//count_p = $('#count_people').val();
    	//if(count_p>2)$('.add_bad_p1').show();
		//else $('.add_bad_p1').hide();
    	//if(count_p==4)$('.add_bad_p2').show();
    	//else $('.add_bad_p2').hide();
    	$('#count_people').val('1');
    	$("select.count_people").val('1');
    	$('.count_people_title').html('1');
    	$('#add_bad1').val('');
    	$('#add_bad2').val('');
    	
    	$('.add_bad1').attr('checked','');
    	$('.add_bad1').attr('checked','');
    	$('.add_bad_p1').hide();
    	$('.add_bad_p2').hide();
    	$('.add_bad_p1_title').hide();
    	$('.mini_text').hide();
    	
    	//console.log($("#count_people").val());
    	//console.log($("#add_bad1").val());
    	//console.log($("#add_bad2").val());
    	
    	get_price();
    });
    
    $(".add_bad1").live("change", function(){
    	
    	checked = $(this).attr('checked');
    	//console.log(checked);
    	//console.log($(this).val());
    	if(checked)
    	{ 
    		$('#add_bad1').val($(this).val());
    		$('.add_bad1:visible').not(this).attr('checked','');
    		//$(".add_bad[val="+add_b+"]").attr('checked','checked');
    		///$('#add_bad').val(add_b);
    	}
    	else
    	{
    		$('#add_bad1').val('');
    		//$(".add_bad[val="+add_b+"]").attr('checked','checked');
    		///$('#add_bad').val(add_b);
    	}
    	//console.log($("#add_bad1").val());
    	get_price();
    });
     $(".add_bad2").live("change", function(){
    	
    	checked = $(this).attr('checked');
    	//console.log(checked);
    	//console.log($(this).val());
    	if(checked)
    	{ 
    		$('#add_bad2').val($(this).val());
    		$('.add_bad2:visible').not(this).attr('checked','');
    		//$(".add_bad[val="+add_b+"]").attr('checked','checked');
    		///$('#add_bad').val(add_b);
    	}
    	else
    	{
    		$('#add_bad2').val('');
    		//$(".add_bad[val="+add_b+"]").attr('checked','checked');
    		///$('#add_bad').val(add_b);
    	}
    	get_price();
    });
    
    $(".clear_bad").live("click", function(){
    	
    	$(".add_bad").removeAttr('checked');
    	$('#add_bad').val(0);
    	get_price();
    });
    
    $("select.count_people").live("change", function(){
    	count_p = $(this).val();
    	
    	if(count_p>2)
    	{
    		$('.add_bad_p1').show();
    		$('.mini_text').show();
    	}
    	else
    	{
    		$('.add_bad_p1').hide();

    		$('.add_bad1').each(function(){
          $(this).removeAttr('checked');
    		});
    		$('.mini_text').hide();
    		$('#add_bad1').val('');
    	}
    	if(count_p==4)
    	{
    		$('.add_bad_p2').show();
    		$('.add_bad_p1_title').show();
    		//console.log($('.add_bad_p1_title'));
    	}
    	else
    	{
    	 	$('.add_bad_p2').hide();
    	 	$('.add_bad2').each(function(){
          $(this).removeAttr('checked');
    		});
    	 	$('.add_bad_p1_title').hide(); 
    	 	//console.log($('.add_bad_p1_title'));
      	}
    	$('#add_bad2').val('');
    	$(".count_people").val(count_p);
    	$('#count_people').val(count_p);
    	$('.count_people_title').html(count_p);
    	get_price();
    });
    $(".price_type").live("click", function(){
    	$(".price_type").removeClass('active');
    	pr_t = $(this).attr('val');
    	$(".price_type[val="+pr_t+"]").addClass('active');
    	$('#price_type').val(pr_t);
    	get_price();
    	
    });
  
    $("div.additional_wishes input").live("change", function(){
    	
    	name = $(this).attr('name');
    	if(!$(this).is(':checked'))
    	{
    		$('div.additional_wishes').find('input[name = ' + name + ']').removeAttr("checked");
    	}
    	else $('div.additional_wishes').find('input[name = ' + name + ']').attr("checked","checked");
 
    	get_price();
    });
    
     $('#container').click(function(e){ 
    //777
		if(e.target.tagName!=='A' && e.target.tagName!=='IMG')
		{
    		$("#dp_from").remove();
    		$("#dp_to").remove();
		$("#selectedPicker").remove();
    	}
	 });
	
	
	
    $(".change_date_from").live("click", function(){
    	if($('#dp_from').length==0)$(this).after('<div style="position:absolute" id="dp_from" ></div>');
    	$('#dp_to').remove();
    	s_from = $('#from_date').val();
    	if(s_from.length==0)d_from = new Date();
    	else 
    	{
    		m = s_from.split('.');
    		d_from = new Date(m[2],Number(m[1])-1,m[0]);
    	   
    	}
    	s_to = $('#to_date').val();
    	if(s_to.length==0)d_to = new Date();
    	else 
    	{
    		m = s_to.split('.');
    		d_to = new Date(m[2],Number(m[1])-1,m[0]);
    	    
    	}
    	$("#dp_from").datepicker({
    		minDate : new Date,
    		//maxDate :  next_month,
			firstDay: 1,
    		dateFormat: "dd.mm.yy",
    		showButtonPanel: false,
    		changeMonth: false,
    		defaultDate:d_from,
    		onSelect: function( selectedDate ) {
    			var format = $(this).data("datepicker").settings.dateFormat || $.datepicker._defaults.dateFormat,
    				date = $.datepicker.parseDate(format, selectedDate);
    			if(date>d_to)
    			{
    					alert("Дата прибытия не может быть позже даты выезда");
    					//обновляем хидден поле
        				$('#from_date').val(selectedDate);
        				$('.from_title').html(date.getDate() + ' ' + month_p[date.getMonth()+1] + ' ' + date.getFullYear());
        				//устанавливаем дату въезда
        				d_to = date;
        				d_to.setDate(d_to.getDate() + difference);
        				//console.log(difference + 'from'+ date +'to ' + d_to);
        				$('#to_date').val(d_to.getDate() + '.' + (d_to.getMonth()+1) + '.' + d_to.getFullYear());
        				$('.to_title').html(d_to.getDate() + ' ' + month_p[d_to.getMonth()+1] + ' ' + d_to.getFullYear());	
        				//считаем кол-во ночей
        				//h = Math.ceil((d_to.getTime() - date.getTime())/(one_day));
        				$('#all_night').val(difference);
        				$('.all_night_title').html(difference);
    			}
    			else {
    				//обновляем хидден поле
    				$('#from_date').val(selectedDate);
    				$('.from_title').html(date.getDate() + ' ' + month_p[date.getMonth()+1] + ' ' + date.getFullYear());
    				//считаем кол-во ночей
    				h = Math.ceil((d_to.getTime() - date.getTime())/(one_day));
    				if(h==0) h = 1;
    				$('#all_night').val(h);
    				$('.all_night_title').html(h);
    			}
    			
    			$(this).remove();
    			get_price();
    			get_bad_cost();
    			set_type_container();
    	}
    	});
    	
    	
    	$("#dp_from").datepicker("show");
    });
    
    $(".change_date_to").live("click", function(){
    	//if($('#dp_to').length==0)$(this).after('<input type="hidden" id="dp_to" ></input>');
    	if($('#dp_to').length==0)$(this).after('<div style="position:absolute" id="dp_to" ></div>');
    	$('#dp_from').remove();
    	s_from = $('#from_date').val();
    	if(s_from.length==0)d_from = new Date();
    	else 
    	{
    		m = s_from.split('.');
    		d_from = new Date(m[2],Number(m[1])-1,m[0]);
    	   
    	}
    	s_to = $('#to_date').val();
    	if(s_to.length==0)d_to = new Date();
    	else 
    	{
    		m = s_to.split('.');
    		d_to = new Date(m[2],Number(m[1])-1,m[0]);
    	    
    	}
    	$("#dp_to").datepicker({
			firstDay: 1,
    		minDate : new Date,
    		//maxDate :  next_month,
    		dateFormat: "dd.mm.yy",
    		showButtonPanel: false,
    		changeMonth: false,
    		defaultDate:d_to,
    		onSelect: function( selectedDate ) {
    			var format = $(this).data("datepicker").settings.dateFormat || $.datepicker._defaults.dateFormat,
    				date = $.datepicker.parseDate(format, selectedDate);
    			if(date<d_from)
    			{
    					alert("Дата выезда не может быть раньше даты заезда");
    					//обновляем хидден поле
        				//$('#from_date').val(selectedDate);
        				//$('.from_title').html(date.getDate() + ' ' + month_p[date.getMonth()+1] + ' ' + date.getFullYear());
        				//устанавливаем дату въезда
        				d_to = d_from;
        				d_to.setDate(d_to.getDate() + difference);
        				//console.log(difference + 'from'+ date +'to ' + d_to);
        				$('#to_date').val(d_to.getDate() + '.' + (d_to.getMonth()+1) + '.' + d_to.getFullYear());
        				$('.to_title').html(d_to.getDate() + ' ' + month_p[d_to.getMonth()+1] + ' ' + d_to.getFullYear());	
        				//считаем кол-во ночей
        				//h = Math.ceil((d_to.getTime() - date.getTime())/(one_day));
        				$('#all_night').val(difference);
        				$('.all_night_title').html(difference);
    			}
    			else {
    				//обновляем хидден поле
    				$('#to_date').val(selectedDate);
    				$('.to_title').html(date.getDate() + ' ' + month_p[date.getMonth()+1] + ' ' + date.getFullYear());
    				//считаем кол-во ночей
    				h = Math.ceil((date.getTime() - d_from.getTime())/(one_day));
    				if(h==0) h = 1;
    				$('#all_night').val(h);
    				$('.all_night_title').html(h);
    			}
    			
    			$(this).remove();	
    			set_type_container();
    			setTimeout("get_price()", 2000);
    	}
    	});
    	
    	$("#dp_to").datepicker("show");
    });
    
    //step3
    $('#change_people_step3').live("click", function(){
    	$(this).hide();
    	$(this).next('select').show();
    });
    $('.count_people_unvisible').live("change", function(){
    	$(this).hide();
    	count_p = $(this).val();
    	
    	$('#count_people').val(count_p);
    	//console.log($('#count_people'));
    	$('.count_people_title').html(count_p);
    	get_price();
    	$('#change_people_step3').show();
    });
    
    //step4
    
    $('.leftmenu a').live("click", function(){
    	p = $('.leftmenu p ').not($('.leftmenu p ').has('a'));
    	old_id = p.attr('val');
    	$('div.block_text[val=' + old_id + ']').hide();
    	html = p.html();
    	p.html('<a href="javascript:void(0)">' + html + '</a>');
    	
    	new_id = $(this).parent().attr('val');
    	
    	$('div.block_text[val=' + new_id + ']').show();
    	
    	h = $(this).html();
    	$(this).parent().html(h);
    });
    
    $('#selectedMonth,#selectedDay,#selectedYear').live("change",function() { 
        $('#selectedInput').val($('#selectedDay').val() + '.'  + $('#selectedMonth').val() + '.' + $('#selectedYear').val());
    });
    $('#place_from_id,#car_type_id').live("change",function() { 
       get_transfer_price();
    });
    $('#count_visa').live("change",function() { 
       get_visa_price();
    });
    $('#early_hour,#early_min').live("change",function() { 
       get_early_price();
    });
    $('#late_hour,#late_min').live("change",function() { 
        get_late_price();
     });
    
    $('.ext_service input').live("change",function() { 
        get_wine_price();
     });
    //step5
    
    


});

function add_service_1(btn) {
	$.post(
			 path + 'bron_ajax.php',
			  {
				 step:'add_service_1',
				 place_from_id:$('#from').val(),
				 car_type_id:$('#car').val(),
				 service_date_arrive:$('#selectedInput').val(),
				 hour_arrive:$('#hour').val(),
				 min_arrive:$('#min').val(),
				 rais_number:$('#rais_number').val(),
				 tel_number:$('#code').val()+'-'+$('#code2').val()+'-'+$('#tel_number').val(),
				 client_name:$('#name').val(),
				 transfer_return:($('#transfer_return').is(':checked')?1:0),
				 pay_delay:($('#pay_delay').is(':checked')?1:0),
				 transfer_comments:$('#transfer_comments').val(),
				 cost:$('.rub_title1').html()
			  },
			  onAjaxSuccess
			);
	function onAjaxSuccess(data) {
		var data = eval("(" + data + ")");
		if (data.error_number) {
			alert(data.error_msg);
		} else {	
			$('#chosen_services tr[val="1"]').remove();
			$('#chosen_services').append('<tr val="1"><td style="width:50%">' + TEXT1 + '</td><td style="width:16%">'+ $('.rub_title1').html() +' '+TEXTrub+'</td><td style="width:14%"><a href="javascript:void(0)" onclick="delete_service(this,1)">'+TEXTdelete+'</a></td><td style="width:47%"><a href="javascript:void(0)" onclick="change_service(1)">' + TEXTchange + '</a></td></tr>');	
			//$(btn).html('Сохранить услугу');
			$(btn).hide()
		}
	}
}
function add_service_2(btn) {
	$.post(
			path + 'bron_ajax.php',
			  {
				 step:'add_service_2',
				 count_visa:$('#count_visa').val(),
				 cost:$('.rub_title2').html()
			  },
			  onAjaxSuccess
			);
	function onAjaxSuccess(data)
	{
		var data = eval("(" + data + ")");
		
		if(data.error_number){
			alert(data.error_msg);
		}else{	
			$('#chosen_services tr[val="2"]').remove();
			$('#chosen_services').append('<tr val="2"><td style="width:50%">' + TEXT2 + '</td><td style="width:16%">'+ $('.rub_title2').html() +' '+TEXTrub+'</td><td style="width:14%"><a href="javascript:void(0)" onclick="delete_service(this,2)">'+TEXTdelete+'</a></td><td style="width:47%"><a href="javascript:void(0)" onclick="change_service(2)">' + TEXTchange + '</a></td></tr>');	
			//$(btn).html('Сохранить услугу');	
			$(btn).hide()
		}
	}
}

function add_service_3(btn)
{
	$.post(
	path + 'bron_ajax.php',
			  {
				 step:'add_service_3',
				 early_hour:$('#early_hour').val(),
				 early_min:$('#early_min').val(),
				 cost:$('.rub_title3').html()
			  },
			  onAjaxSuccess
			);
	function onAjaxSuccess(data)
	{
		var data = eval("(" + data + ")");
		
		if(data.error_number){
			alert(data.error_msg);
		}else{	
			$('#chosen_services tr[val="3"]').remove();
			$('#chosen_services').append('<tr val="3"><td style="width:50%">' + TEXT3 + '</td><td style="width:16%">'+ $('.rub_title3').html() +' '+TEXTrub+'</td><td style="width:14%"><a href="javascript:void(0)" onclick="delete_service(this,3)">'+TEXTdelete+'</a></td><td style="width:47%"><a href="javascript:void(0)" onclick="change_service(3)">' + TEXTchange + '</a></td></tr>');	
			//$(btn).html('Сохранить услугу');
			$(btn).hide()
		}
	}
}

function add_service_4(btn)
{
	$.post(
			 path + 'bron_ajax.php',
			  {
				 step:'add_service_4',
				 late_hour:$('#late_hour').val(),
				 late_min:$('#late_min').val(),
				 cost:$('.rub_title4').html()
			  },
			  onAjaxSuccess
			);
	function onAjaxSuccess(data)
	{
		var data = eval("(" + data + ")");
		
		if(data.error_number){
			alert(data.error_msg);
		}else{	
			$('#chosen_services tr[val="4"]').remove();
			$('#chosen_services').append('<tr val="4"><td style="width:50%">' + TEXT4 + '</td><td style="width:16%">'+ $('.rub_title4').html() +' '+TEXTrub+'</td><td style="width:14%"><a href="javascript:void(0)" onclick="delete_service(this,4)">'+TEXTdelete+'</a></td><td style="width:47%"><a href="javascript:void(0)" onclick="change_service(4)">' + TEXTchange + '</a></td></tr>');	
			//$(btn).html('Сохранить услугу');
			$(btn).hide()
		}
	}
}

function add_service_5(btn)
{
	var ext_service = "";
	$('div.ext_service input:checkbox').each(function(index) {
		//ext_service =  $(this).attr('name') + ':' + ($(this).is(':checked')?1:0) + ',' + ext_service;
		if ($(this).is(':checked')==1) ext_service =  ext_service + $(this).attr('name')+';';
	});
	//ext_service = ext_service.substr(0,ext_service.length-1);
	$.post(
			 path + 'bron_ajax.php',
			  {
				 step:'add_service_5',
				 ext_service: ext_service,
				 cost:$('.rub_title5').html()
			  },
			  onAjaxSuccess
			);
	function onAjaxSuccess(data)
	{
		var data = eval("(" + data + ")");
		
		if(data.error_number){
			alert(data.error_msg);
		}else{	
			$('#chosen_services tr[val="5"]').remove();
			$('#chosen_services').append('<tr val="5"><td style="width:50%">' + TEXT5 + '</td><td style="width:16%">'+ $('.rub_title5').html() +' '+TEXTrub+'</td><td style="width:14%"><a href="javascript:void(0)" onclick="delete_service(this,5)">'+TEXTdelete+'</a></td><td style="width:47%"><a href="javascript:void(0)" onclick="change_service(5)">' + TEXTchange + '</a></td></tr>');	
			//$(btn).html('Сохранить услугу');
			$(btn).hide()
		}
	}
}


function delete_service(obj,service_id) {
	$.post(
			 path + 'bron_ajax.php',
			  {
				 step:'delete_service',
				 service:service_id
			  },
			  onAjaxSuccess
			);
	function onAjaxSuccess(data) {
		$(obj).parent().parent().remove();
		//$('a[service='+service_id+']').html('Добавить к моему бронированию');
		$('a[service='+service_id+']').show();
		get_price();
	}
}

function change_service(service_id) {
	$(".leftmenu p[val=" + service_id + '] a').trigger("click");
}

function change_service_from_step5(service_id) {

	$.post(
			 path + 'bron_ajax.php',
			  {
				 step:'back_to_4',
				 service_id:service_id
			  },
			  onAjaxSuccess
			);
	function onAjaxSuccess(data)
	{
		$.post(
				  path + 'bron_block4.php',
				  {},
				 RefreshContent
		);
	}
	
}
function send()
{
	
	$.post(
			 path + 'bron_ajax.php',
			  {
				 step:'send',
				 arrival_hour:$('select[name="hour_arrival"]').val(),
				 arrival_min:$('select[name="min_arrival"]').val(),
				 count_people:$('#count_people').val(),
				 from_date:$('#from_date').val(),
				 to_date:$('#to_date').val(),
				 all_night:$('#all_night').val(),
				 garanted_early_arrive_check:($('input[name="garanted_early_arrive_check"]').is(':checked')?1:0),
				 additional_room:($('input[name=additional_room]').is(':checked')?1:0),
				 last_comment:$('[name=last_comment]').val()
			  },
			  onAjaxSuccess22
			);
	function onAjaxSuccess22(data)
	{
		$.post(
				  path + 'bron_block6.php',
				  {},
				 RefreshContent
		);
	}
	
}
function open_selected_picker(data)
{
	if($('#selectedPicker').length>0)$('#selectedPicker').remove();
	else $(data).after('<div style="position:absolute" id="selectedPicker" ></div>');
	$('#selectedPicker').datepicker({
		minDate : new Date,
		dateFormat: "dd.mm.yy",
		showButtonPanel: false,
		changeMonth: false,
		defaultDate:$.datepicker.parseDate("dd.mm.yy", $('#selectedInput').val()),
		altField: '#selectedInput', 
		onSelect: function( selectedDate ) {
			var format = $(this).data("datepicker").settings.dateFormat || $.datepicker._defaults.dateFormat,
			date = $.datepicker.parseDate(format, selectedDate);
			
			_month = date.getMonth() + 1;
			if(_month[0]!=0)if(_month<10) _month = '0' + _month;

			_day = date.getDate();
			if(_day[0]!=0)if(_day<10) _day = '0' + _day;

			$('#selectedMonth').val(_month ); 
			$('#selectedDay').val(_day); 
			$('#selectedYear').val(date.getFullYear()); 
			
			$(this).remove();	
			//get_price();
		}
	});
}
function get_bad_cost()
{
		$.post(
				path + 'bron_ajax.php',
				  {
					 step:'get_bad_cost',
					 room_type_id:$('#room_type').val(),
					 count_people:$('#count_people').val(),
					 from_date:$('#from_date').val(),
					 to_date:$('#to_date').val(),
					 all_night:$('#all_night').val(),
					 price_type_id:$('#price_type').val()
				  },
				  onAjaxSuccess
				);
		function onAjaxSuccess(data)
		{
		//console.log(data);
			var data = eval("(" + data + ")");
			
			if(data.error_number){
				alert(data.error_msg);
			}else{	
					if(data.bad1_val1)$('.bad1_val1').html(data.bad1_val1);
					if(data.bad1_val2)$('.bad1_val2').html(data.bad1_val2 );
					if(data.bad1_val3)$('.bad1_val3').html(data.bad1_val3 );
					if(data.bad2_val1)$('.bad2_val1').html(data.bad2_val1 );
					if(data.bad2_val2)$('.bad2_val2').html(data.bad2_val2 );
					if(data.bad2_val3)$('.bad2_val3').html(data.bad2_val3 );
					
			}
		}
}//get_bad_cost
function get_price(all)
{
		$.post(
				path + 'bron_ajax.php',
				  {
					 step:'get_price',
					 room_type_id:$('#room_type').val(),
					 count_people:$('#count_people').val(),
					 from_date:$('#from_date').val(),
					 to_date:$('#to_date').val(),
					 all_night:$('#all_night').val(),
					 add_bad1_id:$('#add_bad1').val(),
					 add_bad2_id:$('#add_bad2').val(),
					 price_type_id:$('#price_type').val(),
					 fin:all
				  },
				  onAjaxSuccess
				);
		function onAjaxSuccess(data)
		{
         try{
                //console.log(data);
                //скрываем расшифровку по дням
                $('.show_days').html('');
                var data = eval("(" + data + ")");
                
                if(data.error_number){
                  alert(data.error_msg);
                }else{	
                    $('.rub_title').html(data.rub );
                    $('.usd_title').html( data.usd);
                    $('.eur_title').html(data.eur);
                    
                }
			  } catch(e) {
          alert(e.name)
        }
			
		}
}//get_price
function get_transfer_price()
{
		$.post(
				path + 'bron_ajax.php',
				  {
					 step:'get_transfer_price',
					 place_from_id:$('#place_from_id').val(),
					 car_type_id:$('#car_type_id').val()
				  },
				  onAjaxSuccess
				);
		function onAjaxSuccess(data)
		{
			var data = eval("(" + data + ")");
			update_price(data,1);
		}
}//get_transfer_price

function get_visa_price()
{
		$.post(
				path + 'bron_ajax.php',
				  {
					 step:'get_visa_price',
					 count_visa:$('#count_visa').val()
				  },
				  onAjaxSuccess
				);
		function onAjaxSuccess(data)
		{
			var data = eval("(" + data + ")");
			update_price(data,2);
		}
}//get_visa_price


function get_early_price()
{
		$.post(
				 path + 'bron_ajax.php',
				  {
					 step:'get_early_price',
					 early_hour:$('#early_hour').val(),
					 early_min:$('#early_min').val()
				  },
				  onAjaxSuccess
				);
		function onAjaxSuccess(data)
		{
			var data = eval("(" + data + ")");
			update_price(data,3);
		}
}//get_early_price
function get_late_price()
{
		$.post(
				path + 'bron_ajax.php',
				  {
					 step:'get_late_price',
					 early_hour:$('#late_hour').val(),
					 early_min:$('#late_min').val()
				  },
				  onAjaxSuccess
				);
		function onAjaxSuccess(data)
		{
			var data = eval("(" + data + ")");
			update_price(data,4);
		}
}//get_late_price
function get_wine_price()
{
	var ext_service = "";
	$('div.ext_service input:checkbox').each(function(index) {
		ext_service =  ext_service + $(this).attr('name') + ':' + ($(this).is(':checked')?1:0) + ',' ;
	});
	ext_service = ext_service.substr(0,ext_service.length-1);
		$.post(
				 path + 'bron_ajax.php',
				  {
					 step:'get_wine_price',
					 ext_service:ext_service
				  },
				  onAjaxSuccess
				);
		function onAjaxSuccess(data)
		{
			var data = eval("(" + data + ")");
			update_price(data,5);
		}
}//get_late_price
function update_price(data,serv_id)
{
	if(data.error_number)alert(data.error_msg);
	else{
		$('.rub_title'+serv_id).html(data.rub);
		$('.usd_title'+serv_id).html(data.usd);
		$('.eur_title'+serv_id).html(data.eur);
	}
}
function next_step_1() {
		$("#formStep1").validationEngine('attach');
		if (!$("#formStep1").validationEngine('validate')) return;

		$.post(
				 path + 'bron_ajax.php',
				  {
				    step:1,
				    from_date:$('#from_date').val(),
				    to_date:$('#to_date').val(),
				    all_night:$('#all_night').val(),
					  partner_id:$('#partner_id').val()
				  },
				  onAjaxSuccess
				);
		function onAjaxSuccess(data)
		{
          try{
                var data = eval("(" + data + ")");
                if(data.error_number){
                  alert(data.error_msg);
                }else{	
                    $.post(
                         path +  'bron_block2.php',
                          {},
                         RefreshContent
                    );

                    setTimeout("set_type_container()", 200);
                    setTimeout("get_price()", 200);
                    setTimeout("get_price()", 1000);
                    
                }
                
			   } catch(e) {
          alert(e.name)
        }
		}
}//next_step_1

function next_step_2()
{
	//var wishes = Array();
	var wishes = Object();
	$('div.additional_wishes input:visible').each(function(index) {
	    
	    name_field = $(this).attr('name');
      if($(this).is(':checked'))
	    wishes[name_field] = 1;
	    else wishes[name_field] = 0;
	    //wishes =  $(this).attr('name') + ':' + ($(this).is(':checked')?1:0) + ',' + wishes;
	});
	//wishes = wishes.substr(0,wishes.length-1);

		$.post(
				 path + 'bron_ajax.php',
				  {
				    step:2,
				    room_type_id:$('#room_type').val(),
				    wishes:wishes,
				    count_people:$('#count_people').val(),
				    from_date:$('#from_date').val(),
				    to_date:$('#to_date').val(),
				    all_night:$('#all_night').val(),
				    price_type_id:$('#price_type').val(),
				    add_bad1_id:$('#add_bad1').val(),
				    add_bad2_id:$('#add_bad2').val(),
				    
				  },
				  onAjaxSuccess
				);
		function onAjaxSuccess(data)
		{
		   try{
		   
            var data = eval("(" + data + ")");
            if(data.error_number){
              alert(data.error_msg);
            }else{
                $.post(
							  path + 'bron_block3.php',
							  {},
							  RefreshContent
              );
             setTimeout("get_price('all')", 3000);
            }
       
        } catch(e) {
          alert(e.name)
        }
		}	
}//#next_step_2

function back_step_3() {
	$('#persdata').validationEngine('hideAll');
	$.post(
			 path + 'bron_ajax.php',
			  {
			    step:'back_to_2'
			  }
	);
	$.post(
			  path + 'bron_block2.php',
			  {},
			  RefreshContent
			 
	);
	setTimeout("set_type_container()", 200);
	setTimeout("get_price()", 3000);	
}//back_step_3()

function next_step_3()	{
		$("#persdata").validationEngine('attach');
		if (!$("#persdata").validationEngine('validate')) return;
		
		$.post(
				 path + 'bron_ajax.php',
				  {
				    step:3,
				    pay_method:$('input[name=pay_method]:checked').attr('value'),
				    count_people:$('#count_people').val(),
				    from_date:$('#from_date').val(),
				    to_date:$('#to_date').val(),
				    all_night:$('#all_night').val(),
				    treatment_id:$('select[name="treatment"]').val(),
				    user_name:$('input[name="user_name"]').val(),
				    user_surname:$('input[name="user_surname"]').val(),
				    national_id:$('select[name="national"]').val(),
				    user_phone:$('input[name="phone_code"]').val()+'-'+$('input[name="phone_code2"]').val()+'-'+$('input[name="phone_number"]').val(),
				    user_fax:$('input[name="fax_code"]').val()+'-'+$('input[name="fax_code2"]').val()+'-'+$('input[name="fax_number"]').val(),
				    user_mail:$('input[name="mail1"]').val()+'@'+$('input[name="mail2"]').val(),
				    user_company:$('input[name="company"]').val(),
				    agree_with_rule:($('input[name="agree_with_rule"]').is(':checked')?1:0)
				  },
				  onAjaxSuccess
				);
		function onAjaxSuccess(data)
		{
		     try{
          var data = eval("(" + data + ")");
		
          if(data.error_number){
            alert(data.error_msg);
          }else{
              $.post(
                   path +  'bron_block4.php',
                    {},
                    RefreshContent
              );
              $('#selectedPicker').datepicker();
          }
        } catch(e) {
          alert(e.name)
        }    
		setTimeout("get_wine_price()", 3000);		
		setTimeout("get_early_price()", 3000);		
		setTimeout("get_late_price()", 3000);		
		setTimeout("get_visa_price()", 3000);		
		setTimeout("get_price()", 3000);		
		}
	
}//#next_step_3
function next_step_4()
{
		$.post(
				 path + 'bron_ajax.php',
				  {
				    step:4,
				    special_request:$('[name="special_request"]').val()
				    
				  },
				  onAjaxSuccess
				);
		function onAjaxSuccess(data)
		{
          try{
            var data = eval("(" + data + ")");
            if(data.error_number){
              alert(data.error_msg);
            }else{
                $.post(
                     path +  'bron_block5.php',
                      {},
                      RefreshContent
                );
                 setTimeout("get_price('all')", 3000);	
            }
            
        } catch(e) {
          alert(e.name)
        }
		}	
}//#next_step_4

function back_step_4()
{
	$.post(
			 path + 'bron_ajax.php',
			  {
			    step:'back_to_3'
			  }
	);
	$.post(
			  path + 'bron_block3.php',
			  {},
			  RefreshContent
	);		
}//back_step_4()

function back_step_5()
{
	$.post(
			 path + 'bron_ajax.php',
			  {
			    step:'back_to_4',
          arrival_hour:$('select[name="hour_arrival"]').val(),
          arrival_min:$('select[name="min_arrival"]').val(),
				  last_comment:$('[name=last_comment]').val()
			  },onAjaxSuccess
	);
	$.post(
			  path + 'bron_block4.php',
			  {},
			  RefreshContent
	);		
	function onAjaxSuccess(data)
		{
		setTimeout("get_wine_price()", 3000);		
		setTimeout("get_early_price()", 3000);		
		setTimeout("get_late_price()", 3000);		
		setTimeout("get_visa_price()", 3000);		
		setTimeout("get_price()", 3000);	
		}

}//back_step_4()

function RefreshContent(content)
{
	$('#bron_block').html(content);
}
 function set_type_container()
    {
      //Блок с вариантами комнат
    	$.post(
    			 path + 'bron_ajax.php',
    			  {
    				 step:'set_type_container',
					 from_date:$('#from_date').val(),
					 to_date:$('#to_date').val()
    			  },
    			 function onAjaxSuccess(data)
          {
              $('.type_container').html(data);
          }
    	); 
 }
