booking = {
	selectedAccTpid:'',
	checkBookingButtons: new Array(),
	createBookingButtons: new Array(),
	highlightedAccRowColor:'#E3F1FB',
	simpleAccRowColor:'#FFF',
	enableCheckBookingResponse: false,
	clientId: '',
	highlightAccRow: function(tpid)
	{
		var accRow = document.getElementById('accRow_' + tpid);
		if( (accRow.style.backgroundColor != 'rgb(248, 199, 178)') && (accRow.style.backgroundColor != '#f8c7b2') ){
			accRow.style.backgroundColor=this.highlightedAccRowColor;
		}
	},
	selectAccRow: function(tpid)
	{
		var accRow = document.getElementById('accRow_' + tpid);
		if( (accRow.style.backgroundColor != 'rgb(248, 199, 178)') && (accRow.style.backgroundColor != '#f8c7b2') ){
			accRow.style.backgroundColor=this.highlightedAccRowColor;
		}
		accRow.style.fontWeight='bold';
		document.getElementById('radio_people_id_' + tpid).checked=true;
	},
	resetAccRow: function(tpid)
	{
		window.status = this.selectedAccTpid + ' ' + tpid;
		if(this.selectedAccTpid != tpid)
		{
			var accRow = document.getElementById('accRow_' + tpid);
			if( (accRow.style.backgroundColor != 'rgb(248, 199, 178)') && (accRow.style.backgroundColor != '#f8c7b2') ){
				accRow.style.backgroundColor=this.simpleAccRowColor;
			}
			accRow.style.fontWeight='normal';

		}
	},
	selectAcc: function(tpid)
	{
		if(this.selectedAccTpid == tpid)
			return;
		for(i=0;i<this.checkBookingButtons.length;i++)
		{
			if(this.checkBookingButtons[i].id == 'checkBooking_' + tpid + '_button')
			{
				if(typeof(this.checkBookingButtons[i]) != 'undefined')
					this.checkBookingButtons[i].style.display = 'inline';
				//this.createBookingButtons[i].style.display = 'inline';
				//this.createBookingButtons[i].enabled.style.display = 'inline';
				if(typeof(this.createBookingButtons[i]) != 'undefined')
					this.createBookingButtons[i].disabled.style.display = 'inline';
			}
			else
			{
				if(typeof(this.checkBookingButtons[i]) != 'undefined')
					this.checkBookingButtons[i].style.display = 'none';
				if(typeof(this.createBookingButtons[i]) != 'undefined')
					this.createBookingButtons[i].disabled.style.display = 'none';
				if(typeof(this.createBookingButtons[i]) != 'undefined')
					this.createBookingButtons[i].enabled.style.display = 'none';
			}
		}
		//alert(this.selectedAccTpid);
		//if(this.selectedAccTpid != '' && this.selectedAccTpid != tpid)
		var prevSelectedAccTpid = this.selectedAccTpid
		this.selectedAccTpid = tpid;
		this.resetAccRow(prevSelectedAccTpid);
		this.selectAccRow(tpid);
	},
	checkBooking: function(tpid) {
		//alert(document.body.scrollTop);
		//alert(document.documentElement.height);
		//document.getElementById("checkBooking_" + tpid + "_result").innerHTML = '<img src="/touronline/img/ebooking-loader.gif" width="16" height="16" border="0">';
		this.enableCheckBookingResponse = true;
		this.checkCheckBookingResponse(0,tpid);
		document.getElementById('common_loader').style.top= Math.ceil(document.body.clientHeight/2 + document.body.scrollTop - 30) + 'px';
		document.getElementById('common_loader').style.left= Math.ceil(document.body.clientWidth/2 + document.body.scrollLeft - 30) + 'px';
		document.getElementById('common_loader').style.visibility = 'visible';
		JsHttpRequest.query(
			'/touronline/jrbooking.php',
			{
				method: 'CheckBooking',
				tpid: 	tpid,
				client_id: this.clientId
			},
			// Function is called when an answer arrives.
			function(result, errors) {
				if(!booking.enableCheckBookingResponse) return;
				var status = 'сервис недоступен';
				var price = '';

				if(
					result
					&& typeof(result["checkBookingResponse"]) != 'undefined'
					&& typeof(result["checkBookingResponse"]["status"]) != 'undefined'
				)
				{
					if(result["checkBookingResponse"]["status"] == 'waitlist')
					{
						price = 'актуальная&nbsp;цена';
						status = 'по&nbsp;запросу';
					}


					if(result["checkBookingResponse"]["status"] == 'ok')
					{
						price = 'актуальная&nbsp;цена';
						status = 'в&nbsp;продаже';
					}

					if(
						result["checkBookingResponse"]["status"] == 'waitlist'
						|| result["checkBookingResponse"]["status"] == 'ok'
					)
					{
						for(i=0;i<booking.checkBookingButtons.length;i++)
						{
							if(booking.checkBookingButtons[i].id == 'checkBooking_' + tpid + '_button')
							{
								if(this.selectedAccTpid == tpid)
								{
									if(typeof(booking.createBookingButtons[i]) != 'undefined')
										booking.createBookingButtons[i].disabled.style.display = 'none';
									if(typeof(booking.createBookingButtons[i]) != 'undefined')
										booking.createBookingButtons[i].enabled.style.display = 'inline';
								}
							}

						}
					}

					if(result["checkBookingResponse"]["status"] == 'rejected')
						status = 'нет&nbsp;в&nbsp;продаже';
					//status += '<a set="yes" linkindex="23" href="/touronline/help/index.php?topic=checkbooking" onclick="exatShowWindow(\'/touronline/help/index.php?topic=checkbooking\', 600, 200); return false;"><img style="vertical-align: baseline;" src="/touronline/img/icons/help-mini.gif" alt="справка" border="0" height="10" hspace="2" vspace="0" width="10"></a>'

				}

				if(
					result
					&& typeof(result["checkBookingResponse"]) != 'undefined'
					&& typeof(result["checkBookingResponse"]["price"]) != 'undefined'
					&& typeof(result["checkBookingResponse"]["currency"]) != 'undefined'
				)
				{
					price = '' + result["checkBookingResponse"]["price"] + ' ' + result["checkBookingResponse"]["currency"] + '<span class="check_booking_price_descr">, включая доп. платежи - топливный сбор и др.</span>';
				}

				document.getElementById("checkBooking_" + tpid + "_result").innerHTML = status;
				document.getElementById("checkBooking_" + tpid + "_price").innerHTML = price;
				document.getElementById("checkBooking_errors").innerHTML = errors;
				//document.getElementById("checkBooking_" + tpid + "_info").innerHTML = result["booking"];
				document.getElementById('common_loader').style.visibility = 'hidden';
			},
			true  // do not disable caching
		);
	},
	checkCheckBookingResponse: function(time,tpid) {
		nextTime = time + 1000;
		//alert(nextTime);
		if(time > 30000)
		{
			var status = 'сервис недоступен';
			var price = '';
			this.enableCheckBookingResponse = false;
			document.getElementById("checkBooking_" + tpid + "_result").innerHTML = status;
			document.getElementById("checkBooking_" + tpid + "_price").innerHTML = price;
			document.getElementById('common_loader').style.visibility = 'hidden';
		}
		else {
			setTimeout('booking.checkCheckBookingResponse(' + nextTime + ',\'' + tpid + '\');',time);
		}
	}
}
bookingDebug = {
	onclick: function(refInner,debugInner)
	{
		if(typeof(debugInner) != 'undefined')
		{
			if(
				debugInner.style.display == 'block'
			)
			{
				debugInner.style.display = 'none';
				refInner.innerHTML = '<img src="/touronline/img/pl.gif" width="9" height="9" border="0">';
			}
			else
			{
				debugInner.style.display = 'block';
				refInner.innerHTML = '<img src="/touronline/img/mn.gif" width="9" height="9" border="0">';
			}
		}
	},
	sendReport: function(value)
	{
		// Create new JsHttpRequest object.
		document.getElementById('checkBooking_debug_send_result').innerHTML = '<img src="/touronline/img/ebooking-loader.gif" width="16" height="16" border="0">';
		var req = new JsHttpRequest();
		req.onreadystatechange = function()
		{
			if (req.readyState == 4)
			{
				document.getElementById('checkBooking_debug_send_result').innerHTML = req.responseJS.sendDebugResponse;
				//document.getElementById('checkBooking_debug_send_result').innerHTML += req.responseText;
			}
		}
		// Prepare request object (automatically choose GET or POST).
		req.open(null, '/touronline/jrbooking.php', true);
		// Send data to backend.
		req.send({q:value});
	}
}

