function Check_Availability(id)
	{
	query_array = new Array();

	url='/_scripts/bookingcom_check_availability.php';
	query_array['hotel_ids'] = id;
	query_array['arrival_date'] = setupdate(document.getElementById('checkin').value);
	query_array['departure_date'] = setupdate(document.getElementById('checkout').value);
	query_array['max_persons'] = 0; // Max persons is silly, but we probably should implement it at some point.

	if (query_array['arrival_date'] != undefined && query_array['departure_date'] != undefined)
		apilite_get(url,query_array,"Check_Availability_Returned",'',"Check_Availability_Failed",'');
	else
		Check_Availability_Failed("Please provide a check in and check out date");
	}

function setupdate(day_month_year)
	{
	var parts = day_month_year.split("-");
	var year_month_day = parts[2] + "-" + parts[1] + "-" + parts[0];
	return year_month_day;
	}

function Check_Availability_Returned(response)
	{
	results = parse_response(response)
	var arrival_date = setupdate(document.getElementById('checkin').value);
	var departure_date = setupdate(document.getElementById('checkout').value);

	var html = "<div class='success'>" + results['available_rooms'] + " rooms available: "
			+ "Min Price: &pound;" + num_to_cash(results['min_total_price']) + ", "
			+ "Max Price: &pound;" + num_to_cash(results['max_total_price']) + ".<br />\n"
			+ "<a href=\"javascript:popUp('https://secure.booking.com/book.html?aid="+bookingcomaid+";hotel_id="+results['hotel_id']+";stage=0;checkin="+arrival_date+";checkout="+departure_date+"#topanchor')\">Book Room</a></div>";
	document.getElementById('lookup_results').innerHTML = html;
	}

function Check_Availability_Failed(response)
	{
//ert(response);
	document.getElementById('lookup_results').innerHTML = "<div class='alert'>" + (response?response:'Sorry, an error has occurred, please check your dates and try again.') + '</div>';	
	}



function parse_response (response)
	{
	results = new Array();
	var details = response.split(",");
	for (var detail in details)
		{
		var temp = details[detail].split("=");
		results[temp[0]] = temp[1];
		}  
	return results;
	}

function num_to_cash(amount)
	{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
	}

function popUp(URL)
	{
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=500,height=600,left = 690,top = 425');");
	}