
function chkandcontinue() {

	// Convert selected date to sql format.
	var date = document.f1.year.value+document.f1.month.value+document.f1.day.value
	
	// Get user to confirm if date selected is todays date.
	if (date==today) {
		if (!confirm("Please confirm that you wish to travel TODAY.")) {
			alert("Please select your date of travel (step 1) before booking.");
			return false;
		}
	}

	if (chkdate(date)) {
		document.f1.submit();
	} else {
		return false;
	}
}

function chkdate(date) {
	if (date<today) {
		alert('The earliest we can take a booking for is today.\nPlease select a later date.');
		return false;
	} else if (npsid!='') {
		if (date>nxt_edate) {
			alert('We can only accept bookings from today until '+nxt_edate_f+'.\nPlease select an earlier date.')
			return false;
		}
	} else {
		if (date>cur_edate) {
			alert('We can only accept bookings from today until '+cur_edate_f+'.\nPlease select an earlier date.')
			return false;
		}
	}
	return true;
}

function changecur() {
	document.f1.action='bookssticket.php';
	document.f1.submit();
}


function checkandsubmit(paymentmethod) {
	var err='';
	var errcount=0;
	var havename=true;
	var haveaddress=true;
	
	// Set WP payment method parameter.
	document.f1.paymentType.value=paymentmethod;

	// Validate name and set WP name parameter.
	if (document.f1.M_firstname.value=='') {
		err+='Your first name.\n';
		havename=false;
		errcount++;
	}
	if (document.f1.M_lastname.value=='') {
		err+='Your last name.\n';
		havename=false;
		errcount++;
	}
	if (havename) {
		document.f1.name.value=destination = document.f1.M_firstname.value+' '+document.f1.M_lastname.value;
	}

	// Validate email.
	if (document.f1.email.value=='') {
		err+='Your email address.\n';
		errcount++;
	} else {
		document.f1.M_email.value=document.f1.email.value;
	}

	// Validate phone.
	if (document.f1.tel.value=='') {
		err+='Your phone number.\n';
		errcount++;
	} else {
		document.f1.M_dtel.value=document.f1.tel.value;
	}


	// Validate address and set WP address parameter.
	if (document.f1.M_address1.value=='') {
		err+='The first line of your address.\n';
		haveaddress=false;
		errcount++;
	}
	if (document.f1.city.value=='') {
		err+='The city of your address.\n';
		haveaddress=false;
		errcount++;
	} else {
		document.f1.M_city.value=document.f1.city.value;
	}
	
	// Validate county and set WP county parameter.
	if (document.f1.state.value=='') {
		err+='The county/state/province of your address.\n';
		haveaddress=false;
		errcount++;
	} else {
		document.f1.M_county.value=document.f1.state.value;
	}
	if (haveaddress) {
		if (document.f1.M_houseno.value.length >0)
		{
			document.f1.address.value=document.f1.M_houseno.value+' '+document.f1.M_address1.value+'\n';
		}
		else
		{
			document.f1.address.value=document.f1.M_address1.value+'\n';
		}
		if (document.f1.M_address2.value!='') {
			document.f1.address.value+=document.f1.M_address2.value+'\n';
		}
		document.f1.address.value+=document.f1.city.value+'\n'+document.f1.state.value;
	}

	// Validate postcode.
	if (document.f1.postcode.value=='') {
		err+='The postal code/zip code of your address.\n';
		errcount++;
	}

	// Validate country and set WP country parameter.
	if (document.f1.country.value=='') {
		err+='The country of your address.\n';
		errcount++;
	} else {
		document.f1.M_country.value=document.f1.country.options[document.f1.country.selectedIndex].text
	}
	
	//validate T&C
	if (document.f1.tandc.checked==false) {
		err+='Acceptance of the terms and conditions must be ticked.\n';
		tanc=false;
		errcount++;
	}
	
	//check transaction status
	if (document.f1.M_companyname.value=='test transaction') {
		document.f1.testMode.value= '100';
	}
	
	if (errcount==0) {

		// Make user confirm email address.
		if (!confirm('You\'ve provided the following email address for financial comunications:\n\n'+document.f1.email.value+'\n\nPlease click OK if this address is correct.')) {
			return false;
		} else {

			// Build complete address for WP to pass to callback function.
			document.f1.M_address.value=document.f1.address.value+'\n'+document.f1.postcode.value+'\n'+document.f1.country.options[document.f1.country.selectedIndex].text;

			// Submit form.
			document.f1.submit();
		}
	} else {

		// Display errors.
		alert('The following '+errcount+' items are required to continue:\n\n'+err);
		return false;
	}
}

