var user_wants_to_book = new Array();
var room_max_persons = new Array();
var hotel_curr = '';
var room_type_titles = new Array();
var cansubmit = false;

function check_hotel_avail(curr_hotelid,date_from,date_until){
	ajax_call = new Ajax.Request('/codebase/ajax/get_hotel_availability.php?tmp='+Math.random(),{
		method:'post',
		parameters: { 'hotel_id': curr_hotelid, 'dowhat': 'check_avail', 'arrive': ''+date_from, 'depart': ''+date_until},
		onSuccess:function(transport){
			//console.debug(transport);
			var responseJSON = transport.responseJSON;
			
			if(responseJSON.error == undefined){
				//alert(responseJSON.hotel_text);
				
				var hotel_policies_link = new Element('a',{'href':'#','id':'hotel_policies_link','style':'margin:10px 0px 0px 0px; float:left;','onclick':'show_hotel_policies(); return false;'});
				if(lang=='nl'){
					hotel_policies_link.update('Toon algemene voorwaarden van het hotel');
				}else{
					hotel_policies_link.update('Show the hotels\'s policies');
				}
				
				$('avail_form').appendChild(hotel_policies_link);
				
				
				$('hotel_text').update(responseJSON.hotel_text);
				
				var table = new Element('table',{'width':'600', 'cellspacing':'0', 'cellpadding':'5', 'style':'margin:10px 0px 0px 0px; float:left; border-left:1px solid #ff9900; border-right:1px solid #ff9900;'});
				
				var header = new Element('tr');
					var th1 = new Element('th', {'class':'avail_table_header', 'width':'300', 'style':'border-right:1px solid #E08D2A; padding:1px 5px 1px 5px;'});
					var th2 = new Element('th', {'class':'avail_table_header', 'width':'60', 'style':'border-right:1px solid #E08D2A; padding:1px 5px 1x 5px;'});
					var th3 = new Element('th', {'class':'avail_table_header', 'width':'130', 'style':'border-right:1px solid #E08D2A; padding:1px 5px 1px 5px;'});
					var th4 = new Element('th', {'class':'avail_table_header', 'width':'110', 'style':'padding:1px 5px 1px 5px;'});
					
					if(lang=='nl'){
						th1.update('Kamer type');
						th2.update('Max. personen');
						th3.update('Prijs voor '+date_diff+' nachten');
						th4.update('Aantal kamers');
					}else{
						th1.update('Room type');
						th2.update('Max. persons');
						th3.update('Rate for '+date_diff+' nights');
						th4.update('No. of rooms');
					}
					
					header.appendChild(th1);
					header.appendChild(th2);
					header.appendChild(th3);
					header.appendChild(th4);
				
				table.appendChild(header);
										
				var avail_blocks = responseJSON.block;
				if(avail_blocks.length > 0){
					avail_blocks.each(function(room){
						var block_text_original = room.block_text;
						user_wants_to_book[room.block_id] = '0|0';
						var br = new Element('br');
						
						var avail_row = new Element('tr');
						
						var roomtype = new Element('td',{'style':'text-align:left; padding:5px 5px 5px 5px; border-right:1px solid #684C2F;'});
							
							if(room.photos[0] != undefined){
								var roomphoto = new Element('img',{'style':'float:left; padding-right:5px;','src':room.photos[0].url_square60,'width':'50px','height':'50px'});
								roomtype.appendChild(roomphoto);
							}
							
							var roomname = new Element('div',{'class':'hotelroomname'}).update(room.name);
							room_type_titles[room.block_id] = room.name;
						roomtype.appendChild(roomname);
							var price_num_nights = new Element('div',{'class':'price_num_nights'});
							if(lang=='nl'){
								price_num_nights.update('Prijzen zijn per kamer voor '+date_diff+' nachten.');
							}else{
								price_num_nights.update('Prices are per room for '+date_diff+' nights.');
							}
						roomtype.appendChild(price_num_nights);	
						if(block_text_original != null){/*Het kan nogal eens voorvallen dat er geen roominfo is dus dan is er ook geen moreinfo*/
							var more_room_info = new Element('a',{'class':'more_room_info','href':'#', 'id':'more_info_'+room.block_id, 'onclick':'show_more_info("'+room.block_id+'",false); return false;'});
							if(lang=='nl'){
								more_room_info.update('[Meer informatie]');
							}else{
								more_room_info.update('[More info]');
							}
						roomtype.appendChild(more_room_info);
						}
						var roompersons = new Element('td',{'id':'td_2_'+room.block_id,'style':'text-align:left; padding:5px 5px 5px 5px; border-right:1px solid #684C2F;'});
							roompersons.update(room.max_occupancy);
						room_max_persons[room.block_id] = room.max_occupancy;
						
						var roomprice = new Element('td',{'id':'td_3_'+room.block_id,'style':'text-align:left; padding:5px 5px 5px 5px; border-right:1px solid #684C2F;'});
							var minprice = room.min_price.price;
							var rackrate = room.rack_rate.price;
							hotel_curr = room.rack_rate.currency;
							var currency = getCurrSymb(hotel_curr);
							
							if(rackrate <= minprice || rackrate <= 0){/* rackrate kan wel eens 0 zijn, en dat kan niet */
								var roomprice_minprice = new Element('span',{'style':'color:#ff9900; font-weight:bold;'}).update(currency+' '+minprice);									
								roomprice.appendChild(roomprice_minprice);
								if(hotel_curr != 'EUR'){
									var roomprice_minprice_euro = new Element('span',{'style':'color:#ff9900; font-weight:bold;'}).update(' (&plusmn; &euro;'+getEuros(hotel_curr,minprice)+')');									
									roomprice.appendChild(roomprice_minprice_euro);
								}
							}else{
								var roomprice_rack = new Element('span',{'style':'text-decoration:line-through; padding:0px 5px 0px 0px;'}).update(currency+' '+rackrate);
								roomprice.appendChild(roomprice_rack);
								if(hotel_curr != 'EUR'){
									var roomprice_rack_euro = new Element('span',{'style':'text-decoration:line-through; color:#ffffff; font-weight:normal;'}).update(' (&plusmn; &euro;'+getEuros(hotel_curr,rackrate)+')');									
									roomprice.appendChild(roomprice_rack_euro);
									roomprice.appendChild(br);
								}
								var roomprice_minprice  = new Element('span',{'style': 'color:#ff9900; font-weight:bold;'}).update(currency+' '+minprice);
								roomprice.appendChild(roomprice_minprice);
								if(hotel_curr != 'EUR'){
									var roomprice_minprice_euro = new Element('span',{'style':'color:#ff9900; font-weight:bold;'}).update(' (&plusmn; &euro;'+getEuros(hotel_curr,minprice)+')');									
									roomprice.appendChild(roomprice_minprice_euro);
								}
							}
						
						var roomamount = new Element('td',{'id':'td_4_'+room.block_id,'style':'text-align:left; padding:5px 5px 5px 5px; border-right:1px solid #684C2F; vertical-align:top'});
							var roomamount_selectbox = new Element('select',{'name':'select_box_'+room.block_id,'id':'select_box_'+room.block_id,'style':'width:110px;','onChange':'selectboxchanged("'+room.block_id+'")'});
							var roomamount_option = new Element('option',{'value':'0|0'}).update('0');
							roomamount_selectbox.appendChild(roomamount_option);
							var roomamount_count = 1;
							room.incremental_price.each(function(incr_price){
								if(hotel_curr != 'EUR'){
									var roomamount_option = new Element('option',{'value':roomamount_count+'|'+incr_price.price}).update(roomamount_count+'. ('+currency+' '+incr_price.price+')');
								}else{
									var roomamount_option = new Element('option',{'value':roomamount_count+'|'+incr_price.price}).update(roomamount_count+'. ('+currency+' '+incr_price.price+')');
								}
								roomamount_count += 1;
								roomamount_selectbox.appendChild(roomamount_option);
							});
						roomamount.appendChild(roomamount_selectbox);
						
						avail_row.appendChild(roomtype);
						avail_row.appendChild(roompersons);
						avail_row.appendChild(roomprice);
						avail_row.appendChild(roomamount);
						
						
						if(block_text_original != null){/*Het kan nogal eens voorvallen dat deze null is*/
							var room_text_tr = new Element('tr',{'id':'tr_'+room.block_id,'style':'display:none;'});
								var room_text_td = new Element('td',{'colspan':'4','style':'padding:0px 5px 0px 5px;'});
								
								if(room.photos[0] != undefined){
									room.photos.each(function(photo){
										var roomphoto = new Element('img',{'style':'padding-right:5px;','src':photo.url_square60,'width':'60px','height':'60px'});
									room_text_td.appendChild(roomphoto);
									});
								}
																
									var block_text_nocdatastart = block_text_original.replace("<![CDATA[","");
									var block_text_nocdata = block_text_nocdatastart.replace("]]>","");
									var block_text_roominfotitle = (lang=='nl') ? '<strong>Kamer faciliteiten:</strong>'+block_text_nocdata : '<strong>Room facilities:</strong>'+block_text_nocdata;
									var block_text_policiestitle = block_text_roominfotitle.replace('<div class="policies">','<strong>Policies:</strong><div class="policies">');
								
									var room_text_div = new Element('div',{'class':'room_text','id':'rd_'+room.block_id, 'style':'display:none; margin:0px 0px 5px 0px; padding:0px 0px 0px 0px;'}).update(block_text_policiestitle);
								room_text_td.appendChild(room_text_div);
							room_text_tr.appendChild(room_text_td);
						
							var room_slidedown_tr = new Element('tr');
								var room_slidedown_td = new Element('td',{'colspan':'4', 'style':'border-bottom:1px solid #ff9900; border-top:1px solid #684C2F; padding:0px 5px 0px 5px; text-align:center; cursor:pointer;','onclick':'show_more_info("'+room.block_id+'",false); return false;'});
										var room_slide_down_image = new Element('img',{'id':'img_'+room.block_id,'style':'padding:0px 0px 0px 0px; margin:0px 0px 0px 0px;', 'src':'/images/slide_down_orange.png', 'width':'86px', 'height':'11px'});
								room_slidedown_td.appendChild(room_slide_down_image);
								room_slidedown_tr.appendChild(room_slidedown_td);
							table.appendChild(avail_row);
							table.appendChild(room_text_tr);
							table.appendChild(room_slidedown_tr);
						}else{
							table.appendChild(avail_row);
						}					
							
					});
					
					var book_tr = new Element('tr');
						var book_info_td = new Element('td',{'colspan':'3','style':'text-align:left; font-weight:bold; padding:5px 5px 5px 5px; vertical-align:top; border-bottom:1px solid #ff9900;'});
						if(lang=='nl'){
							book_info_td.update('U betaalt na uw verblijf in het hotel in de valuta waar het hotel de voorkeur aan geeft ('+hotel_curr+').<br />De wisselkoers is onderhevig aan schommelingen.');
						}else{
							book_info_td.update('Payments will be made in the hotel\'s preferred currency ('+hotel_curr+') after your stay at the hotel.<br />The exchange rate is subject to fluctuations.');
						}
						book_tr.appendChild(book_info_td);
						var book_td = new Element('td',{'colspan':'1','style':'text-align:right; border-bottom:1px solid #ff9900;'});
							var book_link = new Element('a',{'href':'javascript:submitform();', 'id':'book_now'});
							var book_img = new Element('img',{'id':'nuboeken_btn', 'style':'padding:10px 5px 5px 5px;', 'src':'/images/nuboeken_disabled_button_105x35_'+lang+'.png', 'width':'105px', 'height':'35px'});
							book_link.appendChild(book_img);
							book_td.appendChild(book_link);
						book_tr.appendChild(book_td);
					
					table.appendChild(book_tr);
					
					$('avail_summ_table').innerHTML = '';//to remove loader
					$('avail_summ_table').appendChild(table);
					//nodig want anders vind IE het niet nodig de tabel visueel te tonen :-S
					$('avail_summ_table').innerHTML += '';
					
					
				}else{
					//foutmelding
					var errormsg = transport.responseText;
					$('avail_summ_table').innerHTML = (lang=='nl') ? '<div>Helaas is er geen beschikbaarheid meer voor deze periode</div>' : '<div>Sorry, we can not find any availability for this period.</div>';
				}
			}else{
				//foutmelding
				var errormsg = transport.responseText;
				//$('avail_summ_table').innerHTML = '<div>'+errormsg+'</div>';
				$('avail_summ_table').innerHTML = (lang=='nl') ? '<div>Helaas is er geen beschikbaarheid meer voor deze periode</div>' : '<div>Sorry, we can not find any availability for this period.</div>';
				
			}
		}
	});
}
function show_hotel_policies(){
	if(!$('hotel_text').visible()){
		//policies zijn nog niet zichtbaar
		Effect.BlindDown('hotel_text',{
			beforeStart:function(transport){
				
			},
			afterFinish:function(transport){
				if(lang=='nl'){
					$('hotel_policies_link').update('Verberg algemene voorwaarden van het hotel');
				}else{
					$('hotel_policies_link').update('Hide the hotels\'s policies');
				}
			}
		});
	}else{
		Effect.BlindUp('hotel_text',{
			beforeStart:function(transport){
				
			},
			afterFinish:function(transport){
				if(lang=='nl'){
					$('hotel_policies_link').update('Toon algemene voorwaarden van het hotel');
				}else{
					$('hotel_policies_link').update('Show the hotels\'s policies');
				}
			}
		});
	}
}
function show_more_info(room_block_id, forceopen){
	if(!$('rd_'+room_block_id).visible() || forceopen){
		if(!$('rd_'+room_block_id).visible()){
			//Effect.BlindDown('rd_'+room_block_id,{
				//beforeStart:function(transport){
					$('rd_'+room_block_id).show();
					$('tr_'+room_block_id).show();
					$('td_4_'+room_block_id).setStyle({
						borderBottom:'1px solid #684C2F'
					});
					$('td_3_'+room_block_id).setStyle({
						borderBottom:'1px solid #684C2F'
					});
					$('td_2_'+room_block_id).setStyle({
						borderBottom:'1px solid #684C2F'
					});
					
					if(lang=='nl'){
						$('more_info_'+room_block_id).update('[Minder informatie]');
					}else{
						$('more_info_'+room_block_id).update('[Less info]');
					}
					
				//},
				//afterFinish:function(transport){
					$('img_'+room_block_id).src = "/images/slide_up_orange.png";	
				//}
			//});
		}
	}else{
		//Effect.BlindUp('rd_'+room_block_id,{
			//afterFinish:function(transport){
				$('tr_'+room_block_id).hide();
				$('td_4_'+room_block_id).setStyle({
					borderBottom:'0px solid #684C2F'
				});
				$('td_3_'+room_block_id).setStyle({
					borderBottom:'0px solid #684C2F'
				});
				$('td_2_'+room_block_id).setStyle({
					borderBottom:'0px solid #684C2F'
				});
				if(lang=='nl'){
					$('more_info_'+room_block_id).update('[Meer informatie]');
				}else{
					$('more_info_'+room_block_id).update('[More info]');
				}
				$('img_'+room_block_id).src = "/images/slide_down_orange.png";	
				
				$('rd_'+room_block_id).hide();
			//}
		//});
	}
}
function submitform(){
	var block_id = "";
	var block_qty = "";
	var max_pers_room = "";
	var block_price = "";
	var roomtype_title = "";
	
	for(tmp_block_id in user_wants_to_book) {
		var tmp_block_qty_and_price = user_wants_to_book[tmp_block_id];

		if(typeof(tmp_block_qty_and_price) != 'function'){//nodig anders poept die array ook al zn functions en methods uit omdat een associative array gezien word als een Object
			var tmp_block_qty_and_price_split = tmp_block_qty_and_price.split("|");
			var tmp_block_qty = tmp_block_qty_and_price_split[0];
			var tmp_block_price = tmp_block_qty_and_price_split[1];
		
			if(tmp_block_qty>0){
				block_id += (block_id.length > 0) ? ","+tmp_block_id : tmp_block_id;
				block_qty += (block_qty.length > 0) ? ","+tmp_block_qty : tmp_block_qty;
				for(var k=0; k<tmp_block_qty; k++){
					max_pers_room += (max_pers_room.length > 0) ? ","+room_max_persons[tmp_block_id] : room_max_persons[tmp_block_id];
				}
				block_price += (block_price.length > 0) ? ","+tmp_block_price : tmp_block_price;
				
				roomtype_title += (roomtype_title.length > 0) ? ","+room_type_titles[tmp_block_id] : room_type_titles[tmp_block_id];			
			}
		}
	}
	
	$('block_id').value = block_id;
	$('block_qty').value = block_qty;
	$('max_pers_room').value = max_pers_room;
	$('begin_date').value = fromdate_4_booking;
	$('end_date').value = untildate_4_booking;
	$('hotel_id').value = curr_hotelid;
	$('block_price').value = block_price;
	$('hotel_curr').value = hotel_curr;
	$('roomtype_title').value = roomtype_title;
	
	if(cansubmit==true){
		document.passtobook_form.submit();
	}else{
		if(lang=='nl'){
			alert("Selecteer een of meerdere kamers aub.");	
		}else{
			alert("Please select one or more rooms to book.");	
		}
	}
}
function selectboxchanged(sel_box){
	user_wants_to_book[sel_box] = $('select_box_'+sel_box).value;
	$('nuboeken_btn').src = '/images/nuboeken_disabled_button_105x35_'+lang+'.png';
	cansubmit = false;
	for(tmp_block_id in user_wants_to_book) {
		if(typeof(user_wants_to_book[tmp_block_id]) != 'function'){
			//alert(user_wants_to_book[tmp_block_id]);
			if(user_wants_to_book[tmp_block_id] != '0|0'){
				$('nuboeken_btn').src = '/images/nuboeken_button_105x35_'+lang+'.png';
				cansubmit = true;
				
				show_more_info(tmp_block_id, true);
			}
		}
	}
}
function getEuros(curr,val){
	var conv_to_euro = eval('currencies.'+curr);
	var res = (val/conv_to_euro);
	return res.toFixed(2);
}
function getCurrSymb(curr_str){
	switch(curr_str){
		case 'EUR':
			ret = "&euro;";
		break;
		case 'GBP':
			ret = "&pound;";
		break;
		case 'USD':
			ret= "$";
		break;
		default:
			ret = "";
		break;
	}
	return ret;
}

