//$('avail_summ_table').hide();

function checkAvailableDaysForCalendar(accocode,number_of_nights){
	$('errormsg').hide();
	var sensitivedates = '';
	ajax_call = new Ajax.Request('/codebase/ajax/get_belvilla_availability.php?tmp='+Math.random(),{
		method:'get',
		asynchronous:false,
		parameters: {'accocode': accocode, 'number_of_nights': number_of_nights, 'dowhat': 'checkForCalendar'},
		onSuccess:function(transport){
			json_transport = transport.responseJSON;
			sensitivedates = json_transport.sensitivedates;
			
			if(sensitivedates==''){
				no_avail_for_period(number_of_nights);
			}else{
				mCal_arrive.setSensitive(sensitivedates);
				enableDisableButton();
			}
		},
		onFailure:function(transport){
			var errormsg = transport.responseText;
			$('errormsg').update(errormsg);
			$('errormsg').show();
			$('avail_summ_table').hide();
		}
	});	
	return sensitivedates;
}

function checkBelvillaAvailability(accocode,number_of_nights,arrive_date){
	$('errormsg').hide();
	var sensitivedates = checkAvailableDaysForCalendar(accocode,number_of_nights);
	
	if(sensitivedates!=''){
		var splitted_arrive_date = arrive_date.split('-');
		var arrive_date_to_check = splitted_arrive_date[2]+'-'+splitted_arrive_date[1]+'-'+splitted_arrive_date[0];
		var sensitivedates_split = sensitivedates.split(',');
		
		if(sensitivedates_split.indexOf(arrive_date_to_check)>-1){
			//$('avail_summ_table').show();
		//alert('arrive_date qs: '+arrive_date);
		var arrive_date_split = arrive_date.split('-');
		
		ajax_call = new Ajax.Request('/codebase/ajax/get_belvilla_availability.php?tmp='+Math.random(),{
			method:'get',
			parameters: {'accocode': accocode, 'number_of_nights': number_of_nights, 'arrive_date':arrive_date_split[2]+'-'+arrive_date_split[1]+'-'+arrive_date_split[0], 'dowhat': 'checkAvailability'},
			onSuccess:function(transport){
				if(transport.getHeader('Content-Type') == 'application/json; charset=UTF-8'){
					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;'}).update('Accomodation type');
						var th2 = new Element('th', {'class':'avail_table_header', 'width':'110', 'style':'border-right:1px solid #E08D2A; padding:1px 5px 1x 5px;'}).update('Max');
						var th3 = new Element('th', {'class':'avail_table_header', 'width':'190', 'style':'border-right:1px solid #E08D2A; padding:1px 5px 1px 5px;'}).update('Rate for '+number_of_nights+' nights');
						
						header.appendChild(th1);
						header.appendChild(th2);
						header.appendChild(th3);
					
					table.appendChild(header);
					
					var br = new Element('br');
					var json_transport = transport.responseJSON;
					//console.debug(json_transport);
					
					json_transport.availability.each(function(block){
						var avail_row = new Element('tr');
						var accotype = new Element('td',{'style':'text-align:left; padding:5px 5px 5px 5px; border-right:1px solid #684C2F;'});
							var accotypename = new Element('div',{'class':'hotelroomname'}).update(block.accotype);
							var acconame = new Element('div',{'class':'hotelroomname'}).update(block.acconame);
							accotype.appendChild(accotypename);
							accotype.appendChild(acconame);
						avail_row.appendChild(accotype);
						
						var roompersons = new Element('td',{'style':'text-align:left; padding:5px 5px 5px 5px; border-right:1px solid #684C2F;'});
							roompersons.update(block.maxnumberofpersons);
						avail_row.appendChild(roompersons);
						
						var roomprice = new Element('td',{'style':'text-align:left; padding:5px 5px 5px 5px; border-right:1px solid #684C2F;'});
							var verk_price = parseInt(block.bbpr_verkprijs);
							var verk_price_orig = parseInt(block.bbpr_verkprijs_orig);
							var currency = '&euro;';
							
							if(verk_price_orig == verk_price){
								var roomprice_verk_price = new Element('span',{'style':'color:#ff9900; font-weight:bold; font-size:14px;'}).update(currency+' '+verk_price.toString());									
								roomprice.appendChild(roomprice_verk_price);
							}else{
								var roomprice_verk_price_orig = new Element('span',{'style':'text-decoration:line-through; padding:0px 5px 0px 0px; font-size:14px;'}).update(currency+' '+verk_price_orig.toString());
								roomprice.appendChild(roomprice_verk_price_orig);
								
								var roomprice_verk_price  = new Element('span',{'style': 'color:#ff9900; font-weight:bold; font-size:14px;'}).update(currency+' '+verk_price.toString());
								roomprice.appendChild(roomprice_verk_price);
							}
						avail_row.appendChild(roomprice);					
					table.appendChild(avail_row);
					});
					
					var book_tr = new Element('tr');
						var book_info_td = new Element('td',{'colspan':'2','style':'text-align:left; font-weight:bold; padding:5px 5px 5px 5px; vertical-align:top; border-bottom:1px solid #ff9900; border-top:1px solid #ff9900;'});
							book_info_td.update('');
						book_tr.appendChild(book_info_td);
						var book_td = new Element('td',{'colspan':'1','style':'text-align:right; border-bottom:1px solid #ff9900; border-top:1px solid #ff9900;'});
							var book_link = new Element('a',{'href':'javascript:booknow();', 'id':'book_now'});
							var book_img = new Element('img',{'id':'nuboeken_btn', 'style':'padding:10px 5px 5px 5px;', 'src':'/images/nuboeken_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;
					alert(errormsg);
					//$('avail_summ_table').innerHTML = '<div>'+errormsg+'</div>';
				}
			}
		});
	
		}else{
			no_avail_for_period(number_of_nights,arrive_date);
		}
	}else{
		no_avail_for_period(number_of_nights,arrive_date);
	}	
}
function booknow(){
	document.passtobook_form.submit();
}
function no_avail_for_period(number_of_nights,arrive_date){
	mCal_arrive.setSensitive(['1900-01-01']);
	var msg = '';
	if(lang=='nl'){
		msg += 'Geen beschikbaarheid gevonden voor '+number_of_nights+' nachten';
		msg += (arrive_date!=null) ? ' met aankomst op '+arrive_date+'.' : '.';
	}else{
		msg += 'No availability found for '+number_of_nights+' nights';
		msg += (arrive_date!=null) ? ' arriving '+arrive_date+'.' : '.';
	}
	
	$('errormsg').update(msg);
	$('errormsg').show();
	$('avail_summ_table').hide();
}
function enableDisableButton(){
	if($('callInputArrive').value != '' && $('selverb').value){
		$('check_avail').enable();
	}else{
		$('check_avail').disable();
	}
}
