// JavaScript Document

var win = null;
function mail(mypage,myname,w,h,scroll)
{
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	settings =
	'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable'
	win = window.open(mypage,myname,settings);
}

function createRequestObject()
{
	var request_o; //declare the variable to hold the object.
	var browser = navigator.appName; //find the browser name
	if(browser == "Microsoft Internet Explorer")
	{
		/* Create the object using MSIE's method */
		request_o = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		/* Create the object using other browser's method */
		request_o = new XMLHttpRequest();
	}
	return request_o; //return the object
}

var http	 	= createRequestObject(); 

function RunBookingValidation ()
{
	
	var property 			= document.BookingTeaser.property;
	var property_index 		= property.options.selectedIndex;
	var property			= property.options[property_index].value;
	
	var day					= document.getElementById('arr_date').value;
	
	var nights				= document.BookingTeaser.nights.value;
	
	var adults				= document.BookingTeaser.adults.value;
	
	var children			= document.BookingTeaser.children.value;
	
	var params 				= "property=" + property + "&arr_date=" + day + "&nights=" + nights + "&adults=" + adults + "&children=" + children;
	
	http.open('POST', '/booking/ajax_handle');
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	
	http.onreadystatechange = handleAjaxReturn; 
	http.send(params);
	
	return false;
}

function handleAjaxReturn ()
{
	if(http.readyState == 4)
	{
		var response = http.responseText;
		//alert(response);
		
		if (response == 'VALIDATION_PASSED')
		{
			//alert('submitting form');
			document.BookingTeaser.submit();
		} else {
			document.getElementById('BookingTeaserError').innerHTML = response;
		}
		
	} else {
		document.getElementById('BookingTeaserError').innerHTML = 'Please Wait...';	
	}
}


/******************************************************
			FUNCTIONS FOR THE NETSPIN BOOKING FORM 
******************************************************/

var targetfield = null;
var targetroomid = null;
var targethotelid = null;
var targetxmlroomid = null;

var isused = new Array();
isused[1] = null;
isused[2] = null;
isused[3] = null;

function getRoomFields(HotelID, RoomId, Field, XMLroomID)
{
	var ajax_fieldname;
	if (XMLroomID == 'none')
	{
		ajax_fieldname = 'AJAX_Adults_' + RoomId;
		document.getElementById(ajax_fieldname).innerHTML = '&nbsp;';
		
		ajax_fieldname = 'AJAX_Child_' + RoomId;
		document.getElementById(ajax_fieldname).innerHTML = '&nbsp;';
		
		isused[RoomId] = false;
	} else {
			
		targetfield 	= Field;
		targetroomid	= RoomId;
		targethotelid	= HotelID;
		targetxmlroomid	= XMLroomID;
		
		var A_field	= Field + '_' + RoomId;
		
		//alert('1 ' + A_field);
		
		http	 	= createRequestObject(); 
		
		http.open('get', '/booking/netspin_handle_room_ajax/' + HotelID + '/' + XMLroomID + '/' + A_field);
		http.onreadystatechange = handleRoomAjaxReturn; 
		http.send(null);
	}
}

function handleRoomAjaxReturn ()
{
	ajax_fieldname = 'AJAX_' + targetfield + '_' + targetroomid;
	
	if(http.readyState == 4)
	{
		var response = http.responseText;
				
		document.getElementById(ajax_fieldname).innerHTML = response;
		
		if (targetfield == 'Adults')
		{
			targetfield = 'Child';
			getRoomFields(targethotelid, targetroomid, 'Child', targetxmlroomid);			
			placeholder();
		} else {
			placeholder();
		}
	} else {
		document.getElementById(ajax_fieldname).innerHTML = '<img src="../images/ajax-loader.gif" width="16" height="16" border="0" alt="Loading" />';
	}
}

function placeholder ()
{
	// moo
}

function checkLimits (RoomID, Maxocupancy)
{						
	var adults 			= document.getElementById('Adults_' + RoomID);
	var adults_index	= adults.options.selectedIndex;
	var adults_count	= adults.options[adults_index].value;
	
	var child 			= document.getElementById('Child_' + RoomID);
	var child_index		= child.options.selectedIndex;
	var child_count		= child.options[child_index].value;
	
	adults_count		= parseInt(adults_count);
	child_count			= parseInt(child_count);
	
	var totalcount 		= adults_count+child_count;
	
	var tick 		= '<img src="../images/icons/tick_256.png" width="16" height="16" alt="Valid number of occupants" border="0" align="absmiddle" />';
	var cross 		= '<img src="../images/icons/cross_256.png" width="16" height="16" alt="Invalid number of occupants" border="0" align="absmiddle" />';
	var image		= null;
		
	if (!isNaN(totalcount))
	{
		isused[RoomID] = true;
		
		if (totalcount < 1)
		{
			image = cross;
			isused[RoomID] = 'fail';
			
		} else if (totalcount > Maxocupancy) {
			image = cross;
			isused[RoomID] = 'fail';
			
		} else {
			image = tick;
			isused[RoomID] = 'ok';
		}
		
		var counter 		= 'AJAX_Count_' + RoomID;
		document.getElementById(counter).innerHTML = image + ' <strong>' + totalcount + '/' + Maxocupancy + '</strong>';
	}
}

function doFinalCheck ()
{
	var fail 	= false;
	var allnull = true;
	
	if (isused[1] != null)
	{
		allnull = false;
		if (isused[1] == 'fail')
		{
			fail = true;
		}
	}
	
	if (isused[2] != null)
	{
		allnull = false;
		if (isused[2] == 'fail')
		{
			fail = true;
		}
	}
	
	if (isused[3] != null)
	{
		allnull = false;
		if (isused[3] == 'fail')
		{
			fail = true;
		}
	}
	
	var message = document.getElementById('AJAX_Message');
	
	if (allnull == true)
	{
		message.innerHTML = "Please select at least one room and ensure you have selected <br />both the number of adults and number of children.";
	} else {
		if (fail == true)
		{
			message.innerHTML = "At least one selected room does not meet occupancy. <br />(Look for a red cross)";
		} else {
			document.SelectRooms.submit();
		}
	}
}