$(document).ready(function() {
	
	timeOutVal = {
			timeout: 1000,
			typingTimeout: ''	
	};
	
	$('a.formsubmit').each(function(){
		$(this).click(function(){
			$("#"+$(this).parents("form").attr("id")).submit();
		});
	});
	createAccountInit();
	
	
	
	/* click on the shopping cart box redirects to the shopping cart.*/
	$('#nav .cart_box .content').click(function(){
		var cartLink = $('.text a', this).attr('href');
		if(cartLink)
			window.location = cartLink;
		return false;
	});
	
});

$.fn.typeTimer = function(inputField, toCall, dataToPass){
	if (timeOutVal.typingTimeout != undefined) 
		clearTimeout(timeOutVal.typingTimeout);
	
	timeOutVal.typingTimeout = setTimeout(function(){
		if($(inputField, timeOutVal.pElement).val() != '')
			eval(toCall);
	}, timeOutVal.timeout);
};

function createAccountInit(){
	
	$.validator.setDefaults({
		highlight: function(input) {
			$(input).parents("tr").addClass("error");
		},
		unhighlight: function(input) {
			$(input).parents("tr").removeClass("error");
		}
	});
	
	$("#create_account").validate({
		rules: {
			gender: "required",
			firstname: {
				required: true,
				minlength: 2
			},
			lastname: {
				required: true,
				minlength: 2
			},
			email_address: {
				required: true,
				email: true,
				minlength: 6
			},
			street_address: {
				required: true,
				minlength: 5
			},
			postcode:{
				required: true,
				minlength: 4
			},
			city:{
				required: true,
				minlength: 3
			},
			country: "required",
			telephone: {
				required: true,
				minlength: 3
			},
			password: {
				required: true,
				minlength: 5
			},
			confirmation: {
				required: true,
				minlength: 5,
				equalTo: '#create_account input[name="password"]'
			},
			email: {
				required: true,
				email: true
			}
		}
	});
}
