function selectBookerForHotel(hotel_id)
{
	showPopuper();

	$.ajax({
		type: "POST",
		url: "/booking.php",
		data: {hotel_id: hotel_id},
		success: function(response){parseResponse(response);},
		error: function(response){alert("Произошла ошибка!");},
		dataType: "text"
	});
}

function selectBookerForRoom(room_id)
{
	showPopuper();

	$.ajax({
		type: "POST",
		url: "/booking.php",
		data: {room_id: room_id},
		success: function(response){parseResponse(response);},
		error: function(response){alert("Произошла ошибка!");},
		dataType: "text"
	});
}

function checkhPrices(id,hotel_id, hname, d11,d12,d13,d21,d22,d23,person,child,rec)
{
//    alert(hname);
   if(rec==0){
      document.getElementById("hourglass").innerHTML = '<img src="/images/indicator.gif" width="24" height="24" border="0" alt="" /> Пожалуйста, дождитесь окончания загрузки. Цены проверяются в режиме реального времени, это занимает примерно минуту';
      document.getElementById("chk_button").disabled=true;
   }   
   rec++;
    var d1=d13+'/'+d12+'/'+d11;
    var d2=d23+'/'+d22+'/'+d21;
//    alert(d1);
//	showPopuper();
	$.ajax({
		type: "POST",
		url: "/chp.php",
		data: {id: id, hotel_id: hotel_id, hname: hname,d1: d1, d2:d2,person:person,child:child},
		success: function(response){document.getElementById("hourglass").innerHTML = response;
        if((response.indexOf('<!--2 nodes-->')==(-1))&&(rec<3)){ document.getElementById("hourglass").innerHTML =document.getElementById("hourglass").innerHTML+'<img src="/images/indicator.gif" width="24" height="24" border="0" alt="" />';setTimeout(checkhPrices(id,hotel_id, hname, d11,d12,d13,d21,d22,d23,person,child,rec),700);}else{document.getElementById("chk_button").disabled=false;}},///parseResponse(response);},
		error: function(response){alert("Произошла ошибка!");document.getElementById("chk_button").disabled=false;document.getElementById("hourglass").innerHTML;},
		dataType: "text"
	});
    
}


function bookHotel(hotel_id, booker)
{
	var selects = document.getElementById("bookers_table").getElementsByTagName("SELECT");

	var error = true;
    var reg=/date.*/;
	for(var i = 0; i < selects.length; i++)
	{
		var select = selects[i];
		if((0 < (rooms_count = select[select.selectedIndex].value)) && (!select.name.match(reg)) )
		{
			var room_id = select.name;
			error = false;
			break;
		}
	}

	if(error)
	{
		alert("Выберите номер для бронировки");
	}
	else
	{
	for(var i = 0; i < selects.length; i++)
	{
		var tsel = selects[i];
		if(tsel.name == 'date11' ){
			var sel11=tsel;
		}
		if(tsel.name == 'date12' ){
			var sel12=tsel;
		}
		if(tsel.name == 'date13' ){
			var sel13=tsel;
		}
		if(tsel.name == 'date21' ){
			var sel21=tsel;
		}
		if(tsel.name == 'date22' ){
			var sel22=tsel;
		}
		if(tsel.name == 'date23' ){
			var sel23=tsel;
		}
	}
        
		$.ajax({
			type: "POST",
			url: "/booking.php",
			data: {hotel_id: hotel_id, room_id: room_id, rooms_count: rooms_count, booker: booker, date11: sel11[sel11.selectedIndex].value
            , date12: sel12[sel12.selectedIndex].value, date13: sel13[sel13.selectedIndex].value, date21: sel21[sel21.selectedIndex].value
            , date22: sel22[sel22.selectedIndex].value, date23: sel23[sel23.selectedIndex].value},
			success: function(response){parseResponse(response);},
			error: function(response){alert("Произошла ошибка!");},
			dataType: "text"
		});
	}
}
//date11[date11.selectedIndex].value
function bookRoom(room_id, booker)
{
	var select = document.getElementById("rooms_count");

	$.ajax({
		type: "POST",
		url: "/booking.php",
		data: {room_id: room_id, rooms_count: select[select.selectedIndex].value, booker: booker},
		success: function(response){parseResponse(response);},
		error: function(response){alert("Произошла ошибка!");},
		dataType: "text"
	});
}

function disableRooms(counter, room_id)
{
	var select = document.getElementById("roomscount_" + counter + "_" + room_id);
	var selects = document.getElementById("bookers_table").getElementsByTagName("SELECT");

	for(var i = 0; i < selects.length; i++)
	{
		if(0 == selects[i].id.indexOf("roomscount_" + counter) && select.id != selects[i].id)
		{
			selects[i].disabled = 0 < select[select.selectedIndex].value ? true : false;
		}
	}
}

function parseResponse(response)
{
	document.getElementById("popuper_content").innerHTML = response;

	if(document.getElementById("popuper_content").childNodes[0] && document.getElementById("popuper_content").childNodes[0].childNodes[0])
	{
		document.getElementById("popuper").style.width = document.getElementById("popuper_content").childNodes[0].childNodes[0].offsetWidth + 20 + "px";
		document.getElementById("popuper").style.height = document.getElementById("popuper_content").childNodes[0].childNodes[0].offsetHeight + 30 + "px";
	}

	jQuery('#popuper').css({top: calculatePopuperTop()});
	jQuery('#popuper').css({left: calculatePopuperLeft()});
}

function showPopuper()
{
	document.getElementById("popuper").style.width = "200px";
	document.getElementById("popuper").style.height = "200px";
	document.getElementById("popuper_content").innerHTML = '<img src="/images/indicator.gif" width="24" height="24" border="0" alt="" style="margin-top: 64px" />';

	setOpacity(document.getElementById("black_back"), 0.7);

	jQuery('#black_back').fadeIn('fast');	
	jQuery('#popuper').fadeIn('fast');
	
	jQuery('#popuper').css({top: calculatePopuperTop()});
	jQuery('#popuper').css({left: calculatePopuperLeft()});
	
	/*jQuery(window).scroll(function () {
		jQuery('.popuper').css({top: calculatePopuperTop()});
	});*/
}

function hidePopuper()
{
	jQuery("#popuper").fadeOut("fast");
	jQuery("#black_back").fadeOut("fast");
}

function calculatePopuperTop()
{
	var top_margin = Math.round((getClientHeight() - jQuery('#popuper').height()) / 2);
	top_margin = top_margin < 15 ? 15 : top_margin;
	top_margin += getBodyScrollTop();

	return(top_margin);
}

function calculatePopuperLeft()
{
	var left_margin = Math.round((getClientWidth() - jQuery('#popuper').width()) / 2);
	left_margin = left_margin < 15 ? 15 : left_margin;
	left_margin += getBodyScrollLeft();

	return(left_margin);
}

function setOpacity(e, op)
{
	if(op==null) op=100;
	if(isNaN(op)) op = parseFloat(op);
	if(op<=1) op=op*100;
	e.style.filter = "alpha(opacity="+op+")";
	op = op/100;
	e.style.MozOpacity = op;
	e.style.KhtmlOpacity = op;
	e.style.opacity = op;
}

function getBodyScrollTop()
{
	return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
}

function getBodyScrollLeft()
{
	return self.pageXOffset || (document.documentElement && document.documentElement.scrollLeft) || (document.body && document.body.scrollLeft);
}

function getClientHeight()
{
	return 'CSS1Compat' == document.compatMode ? document.documentElement.clientHeight : document.body.clientHeight;
}

function getClientWidth()
{
	return 'CSS1Compat' == document.compatMode ? document.documentElement.clientWidth : document.body.clientWidth;
}
