function validate(form) {
	
	var allow_submit 	= true;
	
	$(form).find('input,select,textarea').each(function() {
		
		field_error = false;
		all_classes = $(this).attr('class').split(' ');
		for(i = 0, len = all_classes.length; i < len; i++) {
			
			// What type of validation?
			switch(all_classes[i]) {
				
				case 'text': {
					
					if(undefined == $(this).val() || $(this).val() == '' || $(this).val().replace(/ /g, '') == '' || $(this).val() == $(this).attr('title')) {
						$(this).addClass('error');
						field_error = true;
						$(this).val($(this).attr('title'));
					}
					
					break;
				}
				case 'email': {
					
					if(!$(this).val().match(/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ig)) {
						$(this).addClass('error');
						field_error = true;
					}
					
					break;
				}
				case 'postcode': {

					if(!$(this).val().match(/^[a-z]{1,2}\d{1,2}[a-z]?\s*\d[a-z]{2}$/i)) {
						$(this).addClass('error');
						field_error = true;
					}

					break;
				}
			}
		}
		
		if(field_error) {
			allow_submit = false;
		}
		else {
			$(this).removeClass('error');
		}
		
	});
	
	if(!allow_submit) {
		$(form).find('.validation span').text('Please correct the highlighted fields below').parent(':hidden').show('slide', { easing: 'easeOutQuint', direction: 'up' }, 1000);
	}
	
	return allow_submit;
}


$(function() {	
	// Init Live Search
	$('input[name="terms"]').kakartLiveSearch({
		overallContainer: '#utility-box',
		source: '/livesearch/',
		resultsContainer: '#ajax_results',
		itemTemplate: function(index, item) {
			return '<li class="clearfix ' + (index % 2 == 0 ? 'even' : 'odd') + '" id="variant' + item.variant_id + '">\
				<a title="' + item.product_name + ': ' + item.value_name + '" href="/products/view/' + item.product_fname + '.' + item.value_fname + '">\
					<img src="/media/thumbs/' + item.product_image + '" alt="Image" class="hidden loading" />\
					<span class="title">' + item.product_name + '</span>\
					<span class="category">' + item.product_root_category.substr(0, item.product_root_category.length - 1) + '</span>\
					<span class="colourway">' + item.value_name + '</span>\
				</a>\
			</li>';
		}
	});
});

if (!window.console) {
	window.console = {
		log: function() {
			
		}
	}
}
