function checkDates()
{
	var dataRight = true;
	message = "";

	var startDate = document.BookingForm.startDate.value.split("/");
	var endDate = document.BookingForm.endDate.value.split("/");
	var numStartDate = new Date(startDate[2], startDate[1]-1, startDate[0]);
	var numEndDate = new Date(endDate[2], endDate[1]-1, endDate[0]);
	var currentTime = new Date();


	if(numStartDate-numEndDate == 0){
		message += "\n -  The arrival and departure dates are the same";
		dataRight=false;
	}
	if(numEndDate < numStartDate){
		message += "\n -  The arrival date is after the departure date";
		dataRight=false;
	}
	if (numStartDate < currentTime) {
		message += "\n -  The arrival date is in the past";
		dateRight=false;
	}
	if (!dataRight){
		if (message != ""){
		   message ="\n" +  "The form contains incorrect or incomplete data as follows:\n" + message + "\n" + "\nPlease correct the errors and press 'Check availability' again.";
	   }
	   alert(message);
	}
	return dataRight;	
}

function checkRoomSelection()
{
	input = false;
	radiolength = document.BookingForm.roomselection.length;
	if (radiolength == undefined) {
		if (document.BookingForm.roomselection.checked) {
			input = true;
		}
	} else {
		for (i=0;i < radiolength;i++) {
			if (document.BookingForm.roomselection[i].checked) {
				input=true;
			}
		}
	}
	if (!input) {
		alert("Please select a room before proceeding");
		return false;
	} else {
		return true;
	}
}

function checkOfferSelection()
{
	input = false;
	radiolength = document.BookingForm.offerselection.length;
	if (radiolength == undefined) {
		if (document.BookingForm.offerselection.checked) {
			input = true;
		}
	} else {
		for (i=0;i < radiolength;i++) {
			if (document.BookingForm.offerselection[i].checked) {
				input=true;
			}
		}
	}
	if (!input) {
		alert("Please select an offer before proceeding");
		return false;
	} else {
		return true;
	}
}

function checkBookingForm(){
	re = /([0-9a-zA-Z\.-_]+)@([0-9a-zA-Z\.-_]+)/;
	var name, email, dataRight = true;
	message = "";

	firstName = document.BookingForm.firstName.value;
	lastName = document.BookingForm.lastName.value;
	email = document.BookingForm.email.value;
	address = document.BookingForm.address.value;
	town = document.BookingForm.town.value;
	county = document.BookingForm.county.value;
	postcode = document.BookingForm.postcode.value;
	phone = document.BookingForm.phone.value;
	country = document.BookingForm.country.value;

	if(firstName.length == 0){
		message += "\n -  Enter your first name";
		dataRight=false;
	}
	if(lastName.length == 0){
		message += "\n -  Enter your last name";
		dataRight=false;
	}
	if(email.length == 0){
		message += "\n -  Enter your email address";
		dataRight=false;
	}
	if (email.length!=0 && email.match(re)==null){
	   dataRight=false;
	   message += "\n -  Email is incorrect";
	}
	if(address.length == 0){
		message += "\n -  Enter your home address";
		dataRight=false;
	}
	if(town.length == 0){
		message += "\n -  Enter your home town";
		dataRight=false;
	}
	if(county.length == 0){
		message += "\n -  Enter your home county";
		dataRight=false;
	}
	if(postcode.length == 0){
		message += "\n -  Enter your home postal code";
		dataRight=false;
	}
	if(country.length == 1){
		message += "\n -  Select your country";
		dataRight=false;
	}
	if(phone.length == 0){
		message += "\n -  Enter your contact telephone number";
		dataRight=false;
	}
	if (!dataRight){
		if (message != ""){
		   message ="\n" +  "You failed to correctly fill in the form:\n" + message + "\n" + "\nPlease re-enter and click the 'Pay Deposit' button again!";
	   }
	   alert(message);
	}
	return dataRight;	
}

function toggleOffers()
{
	if (document.BookingForm.offerswitch[0].checked) {
		document.getElementById('packageselectiondiv').style.display='none';
		document.getElementById('roomselectiondiv').style.display='block';
	}

	if (document.BookingForm.offerswitch[1].checked) {
		document.getElementById('packageselectiondiv').style.display='block';
		document.getElementById('roomselectiondiv').style.display='none';
	}
}

function processRoomForm()
{
		if (document.BookingForm.offerswitch == undefined || document.BookingForm.offerswitch[0].checked) {
			result = checkRoomSelection();
		} else {
			result = checkOfferSelection();
		}
		if (result) {
			document.BookingForm.stage.value='3';
			document.BookingForm.submit();
		}
}
