function sendPurchase() {
	var rpcDateObject = new Date();

	if ($('#checkoutBuyButton').hasClass('disabled')) {
		validateCheckout(true);
		return false;
	}
	if ($('#customerTypeNew').prop('checked') == true || $('#useHistoricTxn_disable').prop('checked') == true) {
		if ($('#cardType').val() == "Please select...") {
			alert("Please select your payment card type.");
			return false;
		} 
		
		if ($('#cardType').val() != 'PAYPAL') {
			
			if ($('#cardNumber').val().length < 10 || isNaN($('#cardNumber').val())) {
				alert("Please check you have entered your full card number, without spaces or hyphens.");
				return false;
			} else if ($('#expiryMonth').val() == 'Month' || $('#expiryYear').val() == 'Year') {
				alert("Please enter your card's expiry date.");
				return false;
			} else if ($('#expiryYear').val() == (rpcDateObject.getFullYear()) && $('#expiryMonth').val() <= (rpcDateObject.getMonth())) {
				alert("You may not enter an expiry date in the past.");
				return false;
			} else if ($('#nameOnCard').val() == '') {
				alert("Please enter the your name as it appears on your card.");
				return false;
			} else if ($('#cv2Number').val().length < ($('#cardType').val()=='AMEX'?4:3) || isNaN($('#cv2Number').val())) {
				alert("Please enter your card's CV2 number (the last 3 digits on the signature strip).");
				return false;
			}
			
		}
	} else if($('#useHistoricTxn_disable').checked == false){
		if($('#historicTxnCv2Number').val() < 3) {
			alert("Please enter the CV2 number for your chosen payment card.");
			return false;
		}
		$('#cardType').val("TOKEN");
	}

	if ($('#accept_terms').prop('checked') != true) {
		$('#accept_terms').parent('td').css({
			"background-color" : '#ffcccc'
		});
		alert("Please check the box to confirm you have read and accepted our terms and conditions.\n\nYour order cannot be placed until this is done.");
		return false;
	} else {
		$('#accept_terms').parent('td').css({
			"background-color" : 'inherit'
		});
	}
	
	$('#checkoutBuyButtonLink').unbind('click');
	
	$('#processingMessage').show();

	var postData = '';
	$('#checkoutForm input[type="text"]:not(.loginField, :disabled, :hidden), #checkoutForm select').each(function(item) { 
		postData += '&' + $(this).attr('name') + '=' + $(this).val(); 
	});    
	$('#checkoutForm input:radio:checked').each(function(item) { 
		postData += '&' + $(this).attr('name') + '=' + $(this).val(); 
	});
	$('#checkoutPaymentForm input:not(:disabled, :checkbox, :radio), #checkoutPaymentForm select:not(:disabled)').each(function(item) { 
		postData += '&' + $(this).attr('name') + '=' + $(this).val(); 
	});
	$('#checkoutPaymentForm input:radio:checked').each(function(item) { 
		postData += '&' + $(this).attr('name') + '=' + $(this).val(); 
	});
	$('#checkoutPaymentForm input:checkbox').each(function(item) { 
		postData += '&' + $(this).attr('name') + '=' + $(this).attr('checked'); 
	});
	$('#checkoutPaymentForm input, select').attr('disabled', true);

	$.ajax("/index.php?module=main&page=processPurchaseAJAX", {
		cache: false,
		data: postData,
		type: 'post',
		timeout: 5000,
		error: function(xhr, status, error) {
			switch (status) {
				case 'error':
					$('#paymentStatusMessage').html(xhr.responseText);
					break;
				case 'timeout':
					alert("A timeout occurred.");
					break;
				default:
					alert("Oops: " + status + "\n" + error + "\n\n" + xhr.responseText);
					break;
			}
			$('#processingMessage').hide();
			$('#checkoutPaymentForm input, select').attr('disabled', false);
		},
		success: function(data, status, xhr) {
			switch (data.action) {
				case 'PayPal':
					// Need to redirect to the provided PayPal URL
					if (data.url) {
						$('#paymentEntryCell').hide();
						$('#paymentStatusMessage').css({
							color: '#333333'
						});
						$('#paymentStatusMessage').html('<br /><br />You will now be redirected to PayPal to make your payment.<br /><br />If this does not happen within a few seconds please <a href="' + data.url + '">click here to proceed</a>.');
						document.location = data.url;
					} else {
						$('#paymentStatusMessage').html('Unable to send your request to PayPal. Please try again or use an alternative payment method.');
						$('#checkoutPaymentForm input, select').attr('disabled', false);
					}
					break;
				case 'ThreeD':
					$('#threeDSecureForm').attr('action', data.acsurl);
					$('#3ds_PaReq').val(data.pareq);
					$('#3ds_MD').val(data.md);
					$('#3dSecureContainer').show();
					$('#threeDSecureForm').submit();
					break;
				case 'confirm':
					var link = '/confirmation.htm?initial=1&key=' + data.order_key;
					$('#paymentEntryCell').hide();
					$('#paymentStatusMessage').css({
						color: '#333333'
					});
					$('#paymentStatusMessage').html('<br /><br />Thank you. Your payment was accepted.<br /><br />You will now be redirected to your order confirmation; if this does not happen within a few seconds please <a href="' + link + '">click here</a>.');
					document.location.href = link;
					break;
				default:
					alert("Oops. Something went wrong - don't know how to handle action '" + data.action + "'");
					break;
			}
			$('#processingMessage').hide();
		}
	});

    return true;
}

function complete3DSecure(orderKey) {
	$('#3dSecureContainer').hide();
	if (orderKey) {
		var link = '/confirmation.htm?initial=1&key=' + orderKey;
		var msg = '<br /><br />Thank you. Your payment was accepted.<br /><br />You will now be redirected to your order confirmation; if this does not happen within a few seconds please <a href="' + link + '">click here</a>.';
		$('#paymentEntryCell').hide();
		$('#paymentStatusMessage').html(msg);
		$('#paymentStatusMessage').css({
				color : '#333333'
		});
		document.location.href = link;
	} else {
		$('#paymentStatusMessage').html('An error occurred with 3D Secure processing. Please try again.')
		$('#checkoutPaymentForm input, select').attr('disabled', false);
	}
}

function blurOnEnter(f, e, evt) {
	evt = (evt) ? evt : event;
	var charCode = (evt.charCode) ? evt.charCode : ((evt.which) ? evt.which : evt.keyCode);
	if (charCode == 13 || charCode == 2) {
		e.blur();
		return false;
	}
	return true;
}

function displayProcessingMessage(display) {
	if (!display) display = 'inline';
	if (document.getElementById('processingMessage')) {
		document.getElementById('processingMessage').style.display=display;
	}
}

