function validateForm() {
	var hasError = false;
	$('form').find("input[type='text'], textarea").filter('.validate').each(function() {
		$('label').removeClass('inputError');
		if (this.type == 'text') {
			if (this.value == '') {
				showError(this);
				hasError = true;
				return false;
			} else if (this.id == 'txtEmail') {
				var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
				if (!emailReg.test(this.value)) {
					showError(this);
					hasError = true;
					return false;
				}
			}
		}		
		if (this.type == 'textarea') {
			if (this.value == '') {
				showError(this);
				hasError = true;
				return false;
			}
		}		
	});
	if (hasError == false) {
		$('form').fadeOut('slow', function() {
			$('#colRight').html('<br /><div class="loading"><img src="images/loading.gif" alt="loading..." /></div>');					   
		});
		var params = {};
		$('form').find("input[type='text'], textarea").filter(":enabled").each(function() {
			params[this.name] = this.value;
		});
		$.post('scripts/sendmail.php', params, function(data) {
			if (data == 'success') {
				$('#colRight').html('<br /><br /><br /><br /><br /><p>Thanks for your enquiry.<p><br /><p>I\'ll be back in touch with you soon (unless you were trying to sell me stuff).</p>');
			} else {
				$('#colRight').html('<br /><br /><br /><br /><br /><p>Sorry but there was an error sending the message.</p><br /><p>Please try again later or email me directly at <a href="mailto:info@greatbigpointyteeth.com">info@greatbigpointyteeth.com</a>.</p>');
			}
		});
	}
}
	
function showError(input) {
	$(input).prev('label').addClass('inputError');
	$(input).fadeOut(100, function() {
		$(this).fadeIn(100, function() {
			$(this).fadeOut(100, function() {
				$(this).fadeIn(100);
			});
		});
	});
}

function clearForm() {
	$('label').removeClass('inputError');
}

$(document).ready(function() {
	// on form submit
	$("input[type='submit']").click(function() {
		validateForm();
		return false;
	});
	// on form clear
	$("input[type='reset']").click(function() {
		clearForm();
	});
});