$(function()
{
	$('a[href="#wine_of_the_month_club_signup"]').click(function(event)
	{
		event.preventDefault();
		$('.wine_of_the_month_club_form').fadeIn(500);
		$('.dark_bg').fadeIn(500);
		$('html, body').animate({scrollTop: 0}, 500);
	});
	
	$('a[href="#discount_buying_club_signup"]').click(function(event)
	{
		event.preventDefault();
		$('.discount_buying_club_form').fadeIn(500);
		$('.dark_bg').fadeIn(500);
		$('html, body').animate({scrollTop: 0}, 500);
	});
	
	$('.form_close').click(function(event)
	{
		event.preventDefault();
		$('.wine_of_the_month_club_form').fadeOut(500);
		$('.discount_buying_club_form').fadeOut(500);
		$('.dark_bg').fadeOut(500);
	});
	
	// preload backgrounds
	$('body').append('<div class="image_preloader" style="display:none"></div>');
	
	$('.image_preloader').append('<img src="/core/plugins/contact/images/wine_of_the_month_club_form_bg.png" />');
	$('.image_preloader').append('<img src="/core/plugins/contact/images/discount_buying_club_form_bg.png" />');
	
	// $('.form_required').each(function()
	// {
	// 	$(this).parent().append('<div class="error_bubble">Please provide a valid ' + ucwords($(this).attr('name').replace('_', ' ')) + '</div>');
	// });
	
	$('.form input[name="phone_number"]').blur(function()
	{
		var stripped = $(this).val().replace(/[^0-9]/g, '');
		valid = stripped.length == 10;
		if(valid)
			$(this).val(stripped.replace(/([0-9]{3})([0-9]{3})([0-9]{4})/, '($1) $2-$3'))
	});
	

	$('.form form').submit(function(event)
	{
		var parent = $(this);
		$('.form_required', parent).each(function()
		{
			var valid = true;
			
			if($(this).attr('name') == 'email')
			{
				var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
				valid = reg.test($(this).val());
				var message = "Enter a valid Email Address";
			}
			else if($(this).attr('name') == 'phone_number')
			{
				var stripped = $(this).val().replace(/[^0-9]/g, '');
				valid = stripped.length == 10;
				if(valid)
					$(this).val(stripped.replace(/([0-9]{3})([0-9]{3})([0-9]{4})/, '($1) $2-$3'))
				
				var message = "Enter a 10 digit Phone Number";
			}
			else
			{
				valid = $(this).val() != '';
				var message = ucwords($(this).attr('name').replace('_', ' ')) + " is a required field";				
			}
			
			if(!valid)
			{
				// $(this).next('.error_bubble').fadeIn(200);
				event.preventDefault();
				$('html, body').animate({scrollTop: 0}, 500);
				
				var label = $(this).parent().parent().prev('.form_row').children('.form_label');
				var index = $(this).parent().parent().children('.form_field').children('input').index(this);
				
				label.children('.form_error_text').html("&nbsp;-&nbsp;" + message);
				label.children('.form_error_highlight').eq(index).css('color', 'red');
			}
			else
			{
				// $(this).next('.error_bubble').fadeOut(200);			
				var label = $(this).parent().parent().prev('.form_row').children('.form_label');
				var index = $(this).parent().parent().children('.form_field').children('input').index(this);
				
				label.children('.form_error_text').html('');;
				label.children('.form_error_highlight').eq(index).css('color', '#60352E');
			}
		});
	});
});

function ucwords (str) { return (str + '').replace(/^([a-z])|\s+([a-z])/g, function ($1) { return $1.toUpperCase(); }); }
