var theMonths = ["January","February","March","April","May","June","July","August","September","October","November","December"];

// syncs return date to departure date
function syncReturn() {
	var inputForm = document.inputForm;
	inputForm.rYear.selectedIndex = inputForm.dYear.selectedIndex;
	inputForm.rMonth.selectedIndex = inputForm.dMonth.selectedIndex;
	inputForm.rDay.selectedIndex = inputForm.dDay.selectedIndex;
}

// increments return date by 1
function addRDay() {
	var inputForm = document.inputForm;
	if (inputForm.rDay.selectedIndex<(inputForm.rDay.options.length-1)) {
		inputForm.rDay.selectedIndex = (inputForm.rDay.selectedIndex+1);
	} else {
		if (inputForm.rMonth.selectedIndex<(inputForm.rMonth.options.length-1)) {
			inputForm.rMonth.selectedIndex = (inputForm.rMonth.selectedIndex+1);
			inputForm.rDay.selectedIndex = 0;
		}
	}
}

function makeDate(theYear,theMonth,theDay) {
	theYear = theYear-0; theMonth = theMonth-0; theDay = theDay-0;
	return new Date(theYear,theMonth,theDay);
}

function compDate(firstDate,lastDate) {
	if (firstDate.getTime()>lastDate.getTime()) return false;
	return true;
}

function checkDate(theYear,theMonth,theDay) {

	theYear = theYear-0;
	theMonth = theMonth-0;
	theDay = theDay-0;

	// Check date actually exists (darn Gregorian calendar!) Assumes all years are leap
	// years - I'm not nuts enough to program leap year detection
	switch(theMonth) {
		case 8: case 3:
		case 5: case 10:
			if (theDay>30) {alert("There is no such date as "+theMonths[theMonth]+" "+theDay);return false;}
		break;
		case 1:
			if (theDay>29) {alert("There is no such date as "+theMonths[theMonth]+" "+theDay);return false;}
		break;
	}

	// Check date hasn't passed
	var today = new Date();
	var testDate = makeDate(theYear,theMonth,theDay)
	if (compDate(today,testDate)==false) {alert(theMonths[theMonth]+" "+theDay+" "+theYear+" has already passed. Please pick another date.");return false;}
}


function checkForm() {
	var inputForm = document.inputForm;
	var sendForm = document.submitForm;

	// Get Departure date and check validity
	var theYear  = (inputForm.dYear.options[inputForm.dYear.selectedIndex].value);
	var theMonth = (inputForm.dMonth.selectedIndex);
	var theDay   = (inputForm.dDay.selectedIndex+1);
	if (checkDate(theYear,theMonth,theDay)== false) return false;

	// Get Return Date and check Validity
	var therYear  = (inputForm.rYear.options[inputForm.rYear.selectedIndex].value);
	var therMonth = (inputForm.rMonth.selectedIndex);
	var therDay   = (inputForm.rDay.selectedIndex+1);
	if (checkDate(therYear,therMonth,therDay)== false) return false;

	// Check Return Date is after departure date
	if (compDate(makeDate(theYear,theMonth,theDay),makeDate(therYear,therMonth,therDay))==false) {
		alert("Return date must be after departure date!");
		return false;
	}
	
	// Set iContact Subcat
	sendForm.subcat.value = theYear+"-"+theMonth+" ("+theMonths[theMonth-0]+")";

	// Fill in dates
	sendForm.data02.value = theYear+"-"+(theMonth+1)+"-"+theDay;
	sendForm.data03.value = therYear+"-"+(therMonth+1)+"-"+therDay;

	// Get destination (which should be indicated by the deal, but who am I to argue?)
	if (inputForm.destination.value.length<1) {alert("Please enter the destination.");return false;}

	// Fill in destination
	sendForm.data05.value = inputForm.destination.value;

	// Check the number of adults and children
	if (isNaN(inputForm.adultNum.value/1) || isNaN(inputForm.childNum.value/1)) {alert("Please enter only numbers for the number of adults and children.");return false;}
	if (inputForm.adultNum.value+inputForm.childNum.value < 1) {alert("Please enter the number of adults and children.");return false;}

	// Fill in numbers
	sendForm.data06.value = inputForm.adultNum.value+"/"+inputForm.childNum.value;

	// Fill in Passengers
	var passengerNum = (inputForm.adultNum.value-0)+(inputForm.childNum.value-0);
	if (passengerNum>8) passengerNum=8;

	if (inputForm.newsletter)
	{
		if (inputForm.newsletter[0].checked)
		{
			sendForm.data12.value = "Yes";
		}
		else
		{
			sendForm.data12.value = "No";
		}
	}

	var passengerString="";

	for (s=1;s<passengerNum+1;s++) {
		var passSal   = eval("inputForm.salutation"+s+".options[inputForm.salutation"+s+".selectedIndex].text");
		var passFName = eval("inputForm.fname"+s+".value");
		var passLName = eval("inputForm.lname"+s+".value");
		var passAge   = eval("inputForm.age"+s+".value");

		if (passFName.length<1) {alert("Please enter the first name of Passenger "+s);return false;}
		if (passLName.length<1) {alert("Please enter the last name of Passenger "+s);return false;}
		if (passAge.length<1) {alert("Please enter the age of Passenger "+s);return false;}

		passengerString += (passSal+"^ ");
		passengerString += (passFName+"^ ");
		passengerString += (passLName+" ^Age:^ ");
		passengerString += (passAge+", ");

		var passengerMoB = eval("inputForm.MoB"+s+".options[inputForm.MoB"+s+".selectedIndex].text");
		var passengerDoB = eval("inputForm.DoB"+s+".options[inputForm.DoB"+s+".selectedIndex].text");
		var passengerYoB = eval("inputForm.YoB"+s+".value");
		if (passengerYoB.length!=4) {alert("Please enter the four digit year of birth for Passenger "+s);return false;}

		passengerString += ("DOB: "+passengerMoB+" "+passengerDoB+" "+passengerYoB+", ");
	}
	sendForm.data07.value = passengerString;

	//Check for Name
	sendForm.salutation.value = inputForm.salutation.options[inputForm.salutation.selectedIndex].value;
	if (inputForm.fname.value.length<1) {alert("Please enter your first Name");return false;}
	sendForm.fname.value = inputForm.fname.value;
	if (inputForm.lname.value.length<1) {alert("Please enter your family Name");return false;}
	sendForm.lname.value = inputForm.lname.value;

	// Check Address
	sendForm.street.value = inputForm.street.value;
	sendForm.suburb.value = inputForm.suburb.value;
	sendForm.country.value = inputForm.country.value;
	if (inputForm.postcode.value.length<1) {alert("Please enter your postcode");return false;}
	sendForm.postcode.value = inputForm.postcode.value;

	// Check Email
	var theEmail = inputForm.email.value;
	if (theEmail.length<1) {alert("Please enter your email address");return false;}
	if (theEmail.indexOf('.')==-1 || theEmail.indexOf('@')==-1){alert("The email you have entered is invalid");return false;}
	sendForm.email.value = theEmail;

	// Check Phones
	if (inputForm.phone1.value.length<1) {alert("Please enter your phone number");return false;}
	sendForm.phone1.value = inputForm.phone1.value;
	sendForm.mobile.value = inputForm.mobile.value;

	// Check Notes
	sendForm.notes.value = inputForm.notes.value;

	// Check conditions;
	if (inputForm.accept.checked != true) {alert("You must accept our Terms & Conditions to submit your enquiry");return false;}

	// Clear up any single quotes
	for (i=0;i<sendForm.elements.length;i++) {
		sendForm.elements[i].value = sendForm.elements[i].value.replace(/'/g,"`");
	}

	// final alert thingie
	var popUpMessage = "                         ONLINE BOOKING CONFIRMATION REQUEST\n\nYOU ARE ABOUT TO PROCEED AND MAKE AN ACTUAL BOOKING REQUEST\nFOR THE ABOVE SELECTION.\n\nPLEASE HAVE YOUR CREDIT CARD READY AS A CONSULTANT WILL BE IN TOUCH\nWITH YOU SHORTLY TO CONFIRM YOUR ONLINE BOOKING DETAILS.\n\nIF YOU ARE MAKING A GENERAL ENQUIRY, PLEASE CLICK ‘CANCEL’ AND CONTACT\nOUR INTERNET RESERVATIONS ON 08 9426 9909.\n\nBy clicking ‘OK’ you agree that you have read and understood the booking inclusions\nand Terms & Conditions.\n\nAll Booking Requests are subject to availability once we have received your online booking\nwhich will be confirmed by our Internet Reservations Team.";
	var isOK=window.confirm(popUpMessage);

	if (isOK==true) document.submitForm.submit();
}
