function clearQuotes(theForm) {
	for (i=0;i<theForm.elements.length;i++) {
		if (theForm.elements[i].value) {
			theForm.elements[i].value = theForm.elements[i].value.replace(/'/gi,"`");
		}
	}
}
function checkEmail(theEmail) {
	if (theEmail.indexOf('.')==-1) return false;
	if (theEmail.indexOf('@')==-1) return false;
	if (theEmail.length<6) return false;
	return true;
}
function checkDate(theMonth,theDay) {
	theMonth++; // change months to calendar number to keep James sane.
	switch(theMonth) {
		case "2": 
			if (theDay>29) return false; // assume every year is a leap year to keep James sane.
		break;
		case "4":
		case "6":
		case "9":
		case "11":
			if (theDay>30) return false;
		break;
		return true; 
	}
}

var monthArray = ['January','February','March','April','May','June','July','August','September','October','November','December'];
var dayArray = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'];

function checkSpecify(theMenu) {
	theSel = theMenu.options[theMenu.selectedIndex].value;
	if (theSel.indexOf(':')>-1) {
		document.bookingForm.specify.focus();
	} else {
		document.bookingForm.specify.value = "";
	}
}

function checkBookingForm() {
	theForm=document.bookingForm;
	clearQuotes(theForm);
	
	if (theForm.fname.value.length<1) 			{alert("Please enter your first name.");theForm.fname.focus();return false;}
	if (theForm.lname.value.length<1) 			{alert("Please enter your last name.");theForm.lname.focus();return false;}
	if (theForm.email.value.length<1) 			{alert("Please enter your email address.");theForm.email.focus();return false;}
	if (checkEmail(theForm.email.value)==false) {alert("The email address you have entered does not seem to be valid.");theForm.email.focus();return false;}

	var checksum=0;
	if (theForm.phone1.value.length>0) checksum++;
	if (theForm.phone2.value.length>0) checksum++;
	if (checksum==0) {alert("Please enter a contact phone number.");theForm.phone1.focus();return false;}

	if (theForm.street.value.length<1) 		{alert("Please enter your street and number.");theForm.street.focus();return false;}
	if (theForm.suburb.value.length<1) 		{alert("Please enter your town or suburb.");theForm.suburb.focus();return false;}
	if (theForm.state.value.length<1)		{alert("Please enter your state or region.");theForm.state.focus();return false;}
	if (theForm.postcode.value.length<1) 	{alert("Please enter your post or zip code.");theForm.postcode.focus();return false;}

	if (theForm.country.options[theForm.country.selectedIndex].value=="-") 	{alert("Please indicate your country.");theForm.country.focus();return false;}

	var arriveDay	= theForm.aDate.options[theForm.aDate.selectedIndex].value;
	var arriveMonth	= theForm.aMonth.options[theForm.aMonth.selectedIndex].value;
	var arriveYear	= theForm.aYear.options[theForm.aYear.selectedIndex].value;
	var departDay	= theForm.dDate.options[theForm.dDate.selectedIndex].value;
	var departMonth	= theForm.dMonth.options[theForm.dMonth.selectedIndex].value;
	var departYear	= theForm.dYear.options[theForm.dYear.selectedIndex].value;

	if (checkDate(arriveMonth,arriveDay)==false) {alert("Sorry, the arrival date you have indicated does not exist");theForm.aDate.focus();return false;}
	if (checkDate(departMonth,departDay)==false) {alert("Sorry, the departure date you have indicated does not exist");theForm.aDate.focus();return false;}

	var dayLength  = 86400000; // milliseconds in a day
	var todayDate  = new Date().getTime();
	var arriveDateDate = new Date(arriveYear,arriveMonth,arriveDay);
	var departDateDate = new Date(departYear,departMonth,departDay);
	var arriveDate = arriveDateDate.getTime();
	var departDate = departDateDate.getTime();

	if (todayDate > arriveDate+dayLength) {alert("Sorry, the arrival date you have indicated has already passed!");theForm.aDate.focus();return false;}

//	if ((arriveDate-todayDate)<(dayLength*4)) {alert("Sorry, bookings must be made at least four days in advance");theForm.aDate.focus();return false;}	

	var lengthOfStay = departDate-arriveDate;
	if (lengthOfStay<(dayLength*2)) {alert("The minimum length of stay is two nights.");theForm.aDate.focus();return false;}

	theForm.data00.value = dayArray[arriveDateDate.getUTCDay()]+" "+arriveDay+" "+monthArray[arriveMonth]+" "+arriveYear;
	theForm.data01.value = dayArray[departDateDate.getUTCDay()]+" "+departDay+" "+monthArray[departMonth]+" "+departYear;

	if (theForm.data02.value.length<1) {alert("Please enter the number of adults you are booking for.");theForm.data02.focus();return false;}
	if (theForm.data03.value.length<1) {alert("Please enter the number of children you are booking for.");theForm.data03.focus();return false;}

	// prefered town
	checksum=0;
	if (theForm.data04.selectedIndex!=0) checksum++;
	if (theForm.data05.selectedIndex!=0) checksum++;
	if (checksum==0) {alert("Please indicate a town or property of interest.");theForm.data04.focus();return false;}

	theForm.data08.value = theForm.howFind.options[theForm.howFind.selectedIndex].value;
	if (theForm.data08.value.indexOf(':')>-1) {
		if (theForm.specify.value.length<1) {alert("Please specify how you found our site.");theForm.specify.focus();return false;}
		theForm.data08.value += " "+theForm.specify.value;
	}
	alert("Please Note: Reservations are not confirmed\nuntil your deposit is paid.");

	return true;
}

function checkListingForm() {
	theForm=document.listingForm;
	clearQuotes(theForm);
	
	if (theForm.fname.value.length<1) 			{alert("Please enter your first name.");theForm.fname.focus();return false;}
	if (theForm.lname.value.length<1) 			{alert("Please enter your last name.");theForm.lname.focus();return false;}
	if (theForm.email.value.length<1) 			{alert("Please enter your email address.");theForm.email.focus();return false;}
	if (checkEmail(theForm.email.value)==false) {alert("The email address you have entered does not seem to be valid.");theForm.email.focus();return false;}

	var checksum=0;
	if (theForm.phone1.value.length>0) checksum++;
	if (theForm.phone2.value.length>0) checksum++;
	if (theForm.fax.value.length>0) checksum++;
	if (checksum==0) {alert("Please enter a contact phone or fax number.");theForm.phone1.focus();return false;}

	if (theForm.street.value.length<1) 		{alert("Please enter your street and number.");theForm.street.focus();return false;}
	if (theForm.suburb.value.length<1) 		{alert("Please enter your town or suburb.");theForm.suburb.focus();return false;}
	if (theForm.state.value.length<1)		{alert("Please enter your state or region.");theForm.state.focus();return false;}
	if (theForm.postcode.value.length<1) 	{alert("Please enter your post or zip code.");theForm.postcode.focus();return false;}

	if (theForm.country.options[theForm.country.selectedIndex].value=="-") 	{alert("Please indicate your country.");theForm.country.focus();return false;}

	if (theForm.data00.value.length<1) 			{alert("Please enter the street address of your property.");theForm.data00.focus();return false;}
	if (theForm.data01.value.length<1) 			{alert("Please enter the town your property is in.");theForm.data01.focus();return false;}

	return true;
}