/* travelosoft.public.js 
   Copyright 2000 - 2010 TravelCook.com 
*/

travelosoft['stash']= { 
	currency: undefined
}

travelosoft['public'] = {
	init: function(options) {
		travelosoft['public'].dates.init();

		if ( options.city ) {
			travelosoft['stash'].city = options.city || 0 ; 
		}
	},
	bonus: {
		requests: [],
		update: function(price) {
			if ( travelosoft['public'].bonus.requests.length > 0 ) {
				return;
			}

			travelosoft['public'].bonus.requests.push(1);

			params = "g_city="+travelosoft['stash'].city + "&price="+price;
			if ( travelosoft['stash'].currency != undefined ) {
				params += "&currency="+travelosoft['stash'].currency;
			}

			$.ajax({
				url: "ajax/bonus.js", 
				dataType: 'json', 
				data: params, 
				success: function(result) {
					if ( result['credit'] ) {
						if ( result['credit'] > 0 ) {
							$("#bonus").show();
							$("#credit").html(result['credit']);
						} else {
							$("#bonus").hide();
						}
					}
					travelosoft['public'].bonus.requests.pop();
				}, 
				error: function(req, textStatus, errorThrown) {
					window.alert("Error retriving bonus.");
				}
			});
		}
	},
	filter: {
		_eparams: {}, 
		init: function(p) {
			_eparams = p;
			$("form#filters").find("input:checkbox").each( function() {
				$(this).click(travelosoft['public'].filter.update_count);
			}) 
		},

		check_sup: function(e) {
			var state = $(e).attr('checked');

			if ( state ) {
				var $t = $(e).parent().parent();
				while ( $t.attr("class") == "sub" ) {
					$t = $t.prev("tr");
				}

				$t.find("td > input:checkbox").attr('checked', true);
			}
		},
		uncheck_all_sub: function(e, type) {
		},
		check_sub: function(e) {
			var state = $(e).attr('checked');
			var $t = $(e).parent().parent();

			while ( $t.next("tr").attr("class") == "sub" ) {
				$t = $t.next("tr.sub");

				if ( state ) {
					var $i = $t.find("td > input:checkbox");
					$i.attr('checked', true);
				} else {
					$t.find("td > input:checkbox").attr('checked',false);
				}
			}
		},

		update_count: function() {
			$("#filter_update_icon").show();
			var $form = $("form#filters");

			var params = "";

			if ( _eparams ){
				for(p in _eparams) {
					if ( _eparams[p] ) {
						params += "&" + p + "=" + _eparams[p];
					}
				}
			}

			$form.find("input:checkbox:checked").each( function() {
				if ( $(this).attr("checked") ) {
					params += "&" + $(this).attr('name') + "=" + $(this).val();
				}
			});

			params = params.substr(1, params.length);

			$.ajax({
				url: "ajax/search.js", 
				dataType: 'json', 
				data: params, 
				success: function(result) {
					if ( result['count'] == 1 ) {
						$("form#filters").find("input:submit").val("Show 1 result")
					} else {
						$("form#filters").find("input:submit").val("Show "+ result['count'] + " results")
					}
					$("#filter_update_icon").hide();
				}
			});
		}
	},
	maps: {
		options: { 
			center:  0
		},
		init: function(options) {
			if ( options ) {
				travelosoft['public'].maps.options.canvas = options.canvas || "map_canvas";
				travelosoft['public'].maps.options.id = options.id || "map";

				if (options.center) {
					var geocoder = new google.maps.Geocoder();

					geocoder.geocode( { 'address': options.center }, function(results, status) {
						if (status == google.maps.GeocoderStatus.OK) {
							travelosoft['public'].maps.options.center = results[0].geometry.location;
							$("#"+travelosoft['public'].maps.options.id).dialog( {
									title: options.title, 
									autoOpen: false, 
									width: 800, height: 600 
								} 
							);
						}
					});
				}
			}
		},
		infowindows: [],
		labels: [
			new InfoBox({
				content: "Old Town (Stare Mesto)",
				position: new google.maps.LatLng(50.088979, 14.414921), 
				boxStyle: {
					   border: "2px solid white",
					   textAlign: "center",
					   color: "white",
					   fontSize: "8pt",
					   fontWeight: "bold",
					   width: "200px",
					   height: "17px",
					   background: "black", 
					   opacity: 0.5, 
					   filter: "alpha(opacity=50)"
				}
			}),
			new InfoBox({
				content: "New Town (Nove mesto)",
				position: new google.maps.LatLng(50.084243, 14.424405),
				boxStyle: {
					   border: "2px solid white",
					   textAlign: "center",
					   color: "white",
					   fontSize: "8pt",
					   fontWeight: "bold",
					   width: "200px",
					   height: "17px",
					   background: "black", 
					   opacity: 0.5, 
					   filter: "alpha(opacity=50)"
				}
			}),
			new InfoBox({
				content: "Lesser Town (Mala strana)",
				position: new google.maps.LatLng(50.091292, 14.392905),
				boxStyle: {
					   border: "2px solid white",
					   textAlign: "center",
					   color: "white",
					   fontSize: "8pt",
					   fontWeight: "bold",
					   width: "220px",
					   height: "17px",
					   background: "black", 
					   opacity: 0.5, 
					   filter: "alpha(opacity=50)"
				}
			}),
			new InfoBox({
				content: "Smichov",
				position: new google.maps.LatLng(50.079892, 14.397712),
				boxStyle: {
					   border: "2px solid white",
					   textAlign: "center",
					   color: "white",
					   fontSize: "8pt",
					   fontWeight: "bold",
					   width: "150px",
					   height: "17px",
					   background: "black", 
					   opacity: 0.5, 
					   filter: "alpha(opacity=50)"
				}
			})
		],
		polygons: [
			new google.maps.Polygon({
				paths: [
					new google.maps.LatLng(50.081324,14.409943),
					new google.maps.LatLng(50.081475,14.415264),
					new google.maps.LatLng(50.083086,14.42183),
					new google.maps.LatLng(50.087231,14.428418),
					new google.maps.LatLng(50.089282,14.428225),
					new google.maps.LatLng(50.094499,14.426916),
					new google.maps.LatLng(50.093219,14.416959),
					new google.maps.LatLng(50.089502,14.412539),
					new google.maps.LatLng(50.081324,14.409943)
				],
				strokeWeight: "red", 
				strokeOpacity: 1,
				fillColor: "red", 
				fillOpacity: 0.2, 
				clickable: false
			}),
			new google.maps.Polygon({
				paths: [
					new google.maps.LatLng(50.081324,14.409943),
					new google.maps.LatLng(50.067429,14.413075),
					new google.maps.LatLng(50.067828,14.417903),
					new google.maps.LatLng(50.068062,14.419663),
					new google.maps.LatLng(50.067869,14.421058),
					new google.maps.LatLng(50.065845,14.423075),
					new google.maps.LatLng(50.068351,14.430285),
					new google.maps.LatLng(50.069412,14.430928),
					new google.maps.LatLng(50.075499,14.430199),
					new google.maps.LatLng(50.07762,14.430478),
					new google.maps.LatLng(50.079176,14.432774),
					new google.maps.LatLng(50.080525,14.433632),
					new google.maps.LatLng(50.082384,14.434168),
					new google.maps.LatLng(50.086528,14.437473),
					new google.maps.LatLng(50.092022,14.437666),
					new google.maps.LatLng(50.096014,14.437001),
					new google.maps.LatLng(50.094499,14.426916),
					new google.maps.LatLng(50.089282,14.428225),
					new google.maps.LatLng(50.087231,14.428418),
					new google.maps.LatLng(50.083086,14.42183),
					new google.maps.LatLng(50.081475,14.415264),
					new google.maps.LatLng(50.081324,14.409943)
				], 
				strokeWeight: "green", 
				strokeOpacity: 1,
				fillColor: "green", 
				fillOpacity: 0.2,
				clickable: false
			}),
			new google.maps.Polygon({
				paths: [
					new google.maps.LatLng(50.081324,14.409943),
					new google.maps.LatLng(50.081021,14.397283),
					new google.maps.LatLng(50.081489,14.395738),
					new google.maps.LatLng(50.080883,14.394407),
					new google.maps.LatLng(50.082012,14.393806),
					new google.maps.LatLng(50.082315,14.392605),
					new google.maps.LatLng(50.081792,14.391017),
					new google.maps.LatLng(50.082921,14.390888),
					new google.maps.LatLng(50.083362,14.389515),
					new google.maps.LatLng(50.083031,14.38797),
					new google.maps.LatLng(50.083802,14.387841),
					new google.maps.LatLng(50.085289,14.384537),
					new google.maps.LatLng(50.085592,14.381275),
					new google.maps.LatLng(50.088566,14.382048),
					new google.maps.LatLng(50.092696,14.387455),
					new google.maps.LatLng(50.094486,14.391661),
					new google.maps.LatLng(50.095091,14.396768),
					new google.maps.LatLng(50.093192,14.398398),
					new google.maps.LatLng(50.095091,14.409127),
					new google.maps.LatLng(50.093219,14.416959),
					new google.maps.LatLng(50.089502,14.412539),
					new google.maps.LatLng(50.081324,14.409943)
				], 
				strokeWeight: "blue", 
				strokeOpacity: 1,
				fillColor: "blue", 
				fillOpacity: 0.2,
				clickable: false
			}),
			new google.maps.Polygon({
				paths: [
					new google.maps.LatLng(50.081324,14.409943),
					new google.maps.LatLng(50.081021,14.397283),
					new google.maps.LatLng(50.081489,14.395738),
					new google.maps.LatLng(50.080883,14.394407),
					new google.maps.LatLng(50.082012,14.393806),
					new google.maps.LatLng(50.082315,14.392605),
					new google.maps.LatLng(50.081792,14.391017),
					new google.maps.LatLng(50.082921,14.390888),
					new google.maps.LatLng(50.083362,14.389515),
					new google.maps.LatLng(50.083031,14.38797),
					new google.maps.LatLng(50.083802,14.387841),
					new google.maps.LatLng(50.085289,14.384537),
					new google.maps.LatLng(50.085592,14.381275),
					new google.maps.LatLng(50.082756,14.383249),
					new google.maps.LatLng(50.072951,14.397755),
					new google.maps.LatLng(50.065349,14.400501),
					new google.maps.LatLng(50.067429,14.413075),
					new google.maps.LatLng(50.081324,14.409943)
				],  
				strokeWeight: "black", 
				strokeOpacity: 1,
				fillColor: "black", 
				fillOpacity: 0.2,
				clickable: false
			})
		],
		show_hotel: function(id) {
			travelosoft['public'].maps.open();
			travelosoft['public'].maps.close_infowindows();
			map_hotels[id]['iw']['infowindow'].open(travelosoft['public'].maps.map, map_hotels[id]['iw']['marker']);
		},
		close_infowindows: function() {
			for(var w in travelosoft['public'].maps.infowindows) {
				travelosoft['public'].maps.infowindows[w].close();
			}
		},
		create: function(selector) {
			var center = travelosoft['public'].maps.options.center;

			if ( !center ) {
				window.alert("Map initialization failed");
				return 0;
			}

			center = new google.maps.LatLng(center.lat(), center.lng());

			var map = travelosoft['public'].maps.map = new google.maps.Map(document.getElementById(selector), {
				zoom: 14,
				navigationControl: true, 
				streetViewControl: true,
				center: center,
				mapTypeId: google.maps.MapTypeId.ROADMAP
			});

			function add_polygons() {
				for( var polygon in travelosoft['public'].maps.polygons ) {
			 		travelosoft['public'].maps.polygons[polygon].setMap(map);
				}
			}

			function add_labels() {
				for( var label in travelosoft['public'].maps.labels ) {
			 		travelosoft['public'].maps.labels[label].open(map);
				}
			}

			function attachMessage(marker, hotel) {
				var infowindow = new google.maps.InfoWindow({ 
						content: hotel['description']
				});
				hotel['iw'] = { infowindow: infowindow, marker: marker };

				travelosoft['public'].maps.infowindows.push(infowindow);

				google.maps.event.addListener(marker, 'click', function() {
					travelosoft['public'].maps.close_infowindows();
					infowindow.open(map,marker);
				});
			}

			for(var i in map_hotels) {
				var marker = new google.maps.Marker({
					position: new google.maps.LatLng( map_hotels[i]['lat'], map_hotels[i]['lng']), 
					map: map, 
					title: map_hotels[i]['title'],
					icon: new google.maps.MarkerImage(map_hotels[i]['icon'], new google.maps.Size(22, 22)) 
				});

				attachMessage(marker, map_hotels[i]);
			}

			add_polygons();
		},
		open: function() {
			$('#'+travelosoft['public'].maps.options.id).dialog('open');
			if ( !travelosoft['public'].maps._created ) {
				travelosoft['public'].maps.create(travelosoft['public'].maps.options.canvas);
				travelosoft['public'].maps._created = 1;
			}
		} 
	},
	dates: {
		onchange: function() {
				var s_day = $("select[name=s_day]").val();
				if ( s_day == "" ) return;

				var s_my = $("select[name=s_monthYear]").val();
				if ( s_my == "" ) return;

				var s_my_tmp = s_my.split('-');
				var date1 = new Date();

				date1.setFullYear(s_my_tmp[1], s_my_tmp[0]-1, s_day);

				if ( $("select[name=e_day]").val() == "" || $("select[name=e_monthYear]").val() == "" ) {
					var date2 = date1;
					date2.setDate(date1.getDate() + 1);
					$("select[name=e_day]").val($.datepicker.formatDate("dd", date2));
					$("select[name=e_monthYear]").val($.datepicker.formatDate("mm-yy", date2));
					$("#check_out").val($.datepicker.formatDate("dd/mm-yy", date2));
				}
		},
		init: function() {
			$("select[name=s_day]").change( travelosoft['public'].dates.onchange );
			$("select[name=s_monthYear]").change( travelosoft['public'].dates.onchange );
		},
		calendar: {
			init: function() { 
				function on_select(dtext, inst) {
					var tmp = dtext.split('/');
					if ( inst.id == 'check_in' ) {
						$("select[name=s_day]").val(tmp[0]);
						$("select[name=s_monthYear]").val(tmp[1]);
						travelosoft['public'].dates.onchange();
					} 

					if ( inst.id == 'check_out' ) {
						$("select[name=e_day]").val(tmp[0]);
						$("select[name=e_monthYear]").val(tmp[1]);
					} 
				}

				var options = {
					dateFormat: "dd/mm-yy",
					onSelect: on_select,
					minDate: 0
				};
				$("#check_in").datepicker(options);
				$("#check_out").datepicker(options);

				$("select[name=s_day]").change(function() {
					var day = $(this).val();
					var my = $("select[name=s_monthYear]").val();

					if ( day && my )
						$("#check_in").val(day+"/"+my);
				});
				$("select[name=s_monthYear]").change(function() {
					var day = $("select[name=s_day]").val();
					var my = $(this).val();

					if ( day && my )
						$("#check_in").val(day+"/"+my);
				});
				$("select[name=e_day]").change(function() {
					var day = $(this).val();
					var my = $("select[name=e_monthYear]").val();

					if ( day && my ) {
						$("#check_out").val(day+"/"+my);
					}
				});
				$("select[name=e_monthYear]").change(function() {
					var day = $("select[name=e_day]").val();
					var my = $(this).val();

					if ( day && my ) {
						$("#check_out").val(day+"/"+my);
					}
				});

			}, 
			show: function(id) {
				$("#"+id).datepicker('show');
			} 
		},
		check: function() {
			var now = new Date();
			now = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0,0,0,0);

			function check_date(date, y, m, d) {
				if ( date.getFullYear() != y ) return false;
				if ( date.getMonth() != m-1 ) return false;
				if ( date.getDate() != d ) return false;
				return true;
			}

			var s_day = parseInt($("select[name=s_day]").val(), 10);
			var s_my = $("select[name=s_monthYear]").val();

			if ( s_day == "" || s_my == "" ) {
				window.alert("Please select your CHECK-IN date.");
				return false;
			}
			var s_my_tmp = s_my.split('-');
			s_my_tmp[1] = parseInt(s_my_tmp[1], 10);
			s_my_tmp[0] = parseInt(s_my_tmp[0], 10);

			var date1 = new Date(s_my_tmp[1], s_my_tmp[0]-1, s_day);

			if ( !check_date(date1, s_my_tmp[1], s_my_tmp[0], s_day) ) {
				window.alert("Invalid CHECK-IN date");
				return false;
			}

			var e_day = parseInt($("select[name=e_day]").val(), 10);
			var e_my = $("select[name=e_monthYear]").val();

			if ( e_day == "" || e_my == "" ) {
				window.alert("Please select your CHECK-OUT date.");
				return false;
			}

			var e_my_tmp = e_my.split('-');

			e_my_tmp[1] = parseInt(e_my_tmp[1], 10);
			e_my_tmp[0] = parseInt(e_my_tmp[0], 10);

			var date2 = new Date(e_my_tmp[1], e_my_tmp[0]-1, e_day);

			if ( !check_date(date2, e_my_tmp[1], e_my_tmp[0], e_day) ) {
				window.alert("Invalid CHECK-OUT date");
				return false;
			}


			if ( date1 > date2 ) {
				window.alert("CHECK-IN date has to be before CHECK-OUT date.");
				return false;
			}


			if ( date1 < now ) {
				window.alert("Your dates are in past.");
				return false;
			}
		}
	},
	rooms: {
		onselect_rooms: function() { },
		onselect_persons: function() { },
		onupdate_price: function() { },
		init: function(options) {
			if ( options ) {
				if ( options.onselect_rooms ) {
					travelosoft['public'].rooms.onselect_rooms = options.onselect_rooms;
				}

				if ( options.onselect_persons ) {
					travelosoft['public'].rooms.onselect_persons = options.onselect_persons;
				}

				if ( options.onupdate_price ) {
					travelosoft['public'].rooms.onupdate_price = options.onupdate_price;
				}
			}

			$("select._persons_count").each( function() { $(this).attr('disabled', 'disabled') } );
		},
		preselect: function(room, persons) {
			$("tr#room_"+room+"_1").find("select._rooms_count").val("1");
			var $sel = $("tr#room_"+room+"_1").find("select._persons_count");
			$sel.val(persons);

			travelosoft['public'].rooms.select(room, 1);
			travelosoft['public'].rooms.persons_select(room, $sel);
			travelosoft['public'].rooms.show_close_description(room);
		},
		persons_select: function(id, select) {
			var standard_persons = parseInt($(select).prev("td._persons_count > input:hidden").val());
			var count = parseInt($(select).val());

			var $el = $(select).parent().parent();

			if ( count > standard_persons ) {
				var extra_beds = count - standard_persons;

				while ( $el.next().attr('class') == '_room_discount' ) {
					$el = $el.next();
					
					var $rate_extra_bed_el = $el.find("td._room_select > input._room_rate_extra_bed");
					var $rate_total_el = $el.find("td._room_select > input._room_rate_total");
					var $rate_el = $el.find("td._room_select > input._room_rate");

					var rate = parseInt($rate_el.val());
					var rate_extra_bed = parseInt($rate_extra_bed_el.val());
					var total_room_price = rate + rate_extra_bed * extra_beds;

					$el.find("td._room_price_total").find("span").html(total_room_price);
					$el.find("td._room_price_per_night").find("span").html(Math.floor(total_room_price/nights));
					$rate_total_el.val(total_room_price);
				}
			} else {
				while ( $el.next().attr('class') == '_room_discount' ) {
					$el = $el.next();
					
					var $rate_total_el = $el.find("td._room_select > input._room_rate_total");
					var $rate_el = $el.find("td._room_select > input._room_rate");

					var total_room_price = parseInt($rate_el.val());
					$rate_total_el.val(total_room_price);
					$el.find("td._room_price_total").find("span").html(total_room_price);
					$el.find("td._room_price_per_night").find("span").html(Math.floor(total_room_price/nights));
				}

			}

			travelosoft['public'].rooms.onselect_persons();
			travelosoft['public'].rooms.update_total();
			travelosoft['public'].rooms.price_list.update_total_price();
		},

		select: function(id, count) {
			function clone_room($e, i) {
				// clone room
				var $n = $e.clone();

				$n.attr('class', '_room_additional');

				// add number to type and change id
				var id = $n.attr('id').replace(/[0-9]+$/, i);
				$n.attr('id', id);

				var name = $n.attr("name");
				$n.find("td:nth-child(1) > h4").html(name + " #"+i);

				// remove number of rooms
				$n.find("td:nth-child(2)").html('');

				$n.find("td:nth-child(1) > a").css('cursor', 'pointer').click(function() {
					var $d = $(this).parent().parent();

					while ( $d.next().attr('class') != '_room_description' ) 
						$d = $d.next();


					$d = $d.find("div");

					if ( $d.is(":hidden") )
						$d.show();
					else
						$d.hide();
				});

				// return new room
				return $n;
			}
			
			c = 2;
			while ($("tr#room_"+id+"_"+c).attr('id') != undefined ) {
				var $r = $("tr#room_"+id+"_"+c);
				while ( $r.next().attr('class') == '_room_discount' ) {
					$r.next().remove();
				}

				if ( $r.next().attr('class') == '_room_description' )
					$r.next().remove();

				$r.remove();
				c++;
			}

			if ( count > 0 ) {
				// the room
				var $room = $("tr#room_"+id+"_1");
				var discounts = [];

				// enable select people
				$room.find("td:nth-child(3) > select").removeAttr('disabled');

				var $el = $room;

				// find all discounts
				while ( $el.next().attr('class') == '_room_discount' ) {
					$el.next().show()
					discounts.push($el.next());
					$el = $el.next();
				} 

				var discounts_count = discounts.length;

				if ( discounts_count == 1 ) {
					discounts[0].find("td:nth-child(4) > input:radio").attr('checked', true);
				}

				$room.find("td:nth-child(1)").attr('rowspan', discounts.length + 1);
				$room.find("td:nth-child(2)").attr('rowspan', discounts.length + 1);
				$room.find("td:nth-child(3)").attr('rowspan', discounts.length + 1);
				$room.find("td._room_price").each(function() {
					$(this).hide();
				});

				// after discounts follows description
				var $room_desc = $el.next();

				for (var i = count; i > 1; i--) {
					// add room
					var $new_room = clone_room($room, i);
					$room_desc.after($new_room);
					
					// add discounts
					var $new_el = $new_room;
					var regx = new RegExp('room\\['+id+'\\]__id\\[1\\]'); 
					var $persons = $new_el.find("td:nth-child(3) > select");
					$persons.attr('name', $persons.attr('name').replace(regx, 'room['+id+']__id['+i+']'));

					for (var d = 0; d != discounts_count; d++ ) {
						var $newdisc = discounts[d].clone();
						// copy room_rate to room_rate_total to reset it, because browser won't reset element, which doesn't exist in time of form resetting ...
						$newdisc.find("td._room_select > input._room_rate_total").val($newdisc.find("td._room_select > input._room_rate").val());

						// change id in radio button
						var $radio = $newdisc.find("td:nth-child(4) > input:radio");
						$radio.attr('name', $radio.attr('name').replace(regx, 'room['+id+']__id['+i+']')).attr('checked', (discounts_count == 1 ? true : false ));

						// add after current
						$new_el.after($newdisc);
						// set as current
						$new_el = $newdisc;
					}

					// and finaly add description
				}

				travelosoft['public'].rooms.onselect_rooms();
			} else {
				var $room = $("tr#room_"+id+"_1");
				var $el = $room;

				while ( $el.next().attr('class') == '_room_discount' ) {
					$el.next().hide();
					$el.next().find("td:nth-child(4) > input:radio").attr('checked', false);
					$el = $el.next();
				}

				$room.find("td:nth-child(1)").attr('rowspan', 1);
				$room.find("td:nth-child(2)").attr('rowspan', 1);
				$room.find("td:nth-child(3)").attr('rowspan', 1).find("select").attr('disabled','disabled').val(0);
				$room.find("td._room_price").each(function() {
					$(this).show();
				});

				travelosoft['public'].rooms.update_total();
				travelosoft['public'].rooms.price_list.update_total_price();
			}
		},
		update_total: function() {
				var total_persons = 0; 
				var total_rooms = 0;

				$("select._rooms_count").each ( function() {
					var r_count = parseInt($(this).val()-0);
					total_rooms += r_count;
				});

				$("select._persons_count").each ( function() {
					if ( $(this).is(":enabled")) {
						var p_count = parseInt($(this).val()-0);
						total_persons += p_count;
					}
				});

				$("#total_persons").html(total_persons);
				$("#total_rooms").html(total_rooms);
		},
		price_list: {
			update_total_price: function() {
				var total_price = 0;

			
				$("tr._room_discount").each( function() {
					var $radio = $(this).find("td:nth-child(4) > input:radio");
					if ( $radio.is(":checked") ) {
						total_price += parseInt($radio.prev("._room_rate_total").val());
					}
				});

				$("#total_price").html(total_price);
				travelosoft['public'].rooms.onupdate_price(total_price);
			}
		}, 
		show_close_description: function(room_id) {
			$("div#room_description_"+room_id).toggle(400);
		}, 
		check: function() {
			var persons_fail = 0;
			var rooms_selected = 0;
			$("tr._room").each( function() {
				var rooms =  parseInt($(this).find("select._rooms_count").val()-0);
				var persons = parseInt($(this).find("select._persons_count").val()-0);

				if ( rooms > 0 && persons == 0 )
					persons_fail = 1;

				rooms_selected += rooms;
			} );

			if ( rooms_selected == 0 ) {
				window.alert("Select at least one room");
				return false;
			}

			if ( persons_fail ) {
				window.alert("Select persons for each room");
				return false;
			}

			persons_fail = 0;
			$("tr._room_additional").each( function() { 
				var persons = parseInt($(this).find("select._persons_count").val()-0);
				if ( persons == 0 ) {
					persons_fail = 1;
					return false;
				}
			} );

			if ( persons_fail ) {
				window.alert("Select persons for each room");
				return false;
			}

			var discounts = $("tr._room_discount:visible");

			if (discounts.length > 0 ) {
				var selected = 0;
				$(discounts).each( function() {
					var $radio = $(this).find("td._room_select > input:radio");
					if ( $radio.is(":checked") ) {
						selected++;
					}
				});

				if ( selected != rooms_selected ) {
					window.alert("Select conditions for each room");
					return false;
				}
			}

			return true;
		}
	}
}

