$(document).ready(function() {

	//validate and submit
	$('#contact').validate({
		rules: {
			enquiry_name: {
				required: true,
				minlength: 3
			},
			enquiry_email: {
				required: true,
				email: true,
				minlength: 5
			},
			enquiry_comment: {
				required: true,
				minlength: 3
			},
			security_code: {
				required: true,
				minlength: 6
			}
		},
		messages: {
			enquiry_name: {
				required: 'Please fill in your full name.',
				minlength: $.format('At least {0} characters required!')
			},
			enquiry_email: {
				required: 'Please fill in your email address.',
				minlength: $.format('At least {0} characters required!')
			},
			enquiry_comment: {
				required: 'Please fill in your comment.'
			},
			security_code: {
				required: 'Please fill in your security code.',
				minlength: $.format('At least {0} characters required!')
			}
		}
	});


});

// reset the captcha number
function resetSecurityNumber(fieldId,imageId) {
	var date = new Date();
	if (fieldId != null && fieldId != '') $('#' + fieldId).val('');
	if (imageId != null && imageId != '') $('#' + imageId).attr('src','/includes/captcha/CaptchaSecurityImages.php?width=120&height=40&noCache=' + date.getSeconds());
}

function doSend() {
	if ($('#contact').valid()) {
		
		$('#contact_status').html('processing...');
		
		$('#contact').ajaxSubmit({
			url: '/includes/contact_post.php',
			dataType: 'json',
			success: function(json) {
				if (json.success) {
					$('#contact_status').html('enquiry sent succesfully...');
					location.href = '/contact/thank_you.php?ref_no='+json.ref_no;
				} else {
					window.alert('Error:\n' + json.error);
				}
			}
		});
	} else {
		window.status = 'One or more of your fields have a validation error, please check what is filled in.';
	}
}