$(document).ready(function() {
	function checkEmail(email)
	{	
		var pattern = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var emailVal = $("#" + email).val();
		return pattern.test(emailVal);
	}
	$(function()
	{
		$("#subForm input.submit").click(function() {	
			
			// First, disable the form from submitting
			$('form#subForm').submit(function() { return false; });
			
			// Grab form action
			var formAction = $("form#subForm").attr("action");
			
			// Hacking together id for email field
			// Replace the xxxxx below:
			// If your form action were http://mysiteaddress.createsend.com/t/r/s/abcde/, then you'd enter "abcde" below
			var id = "krdhhd";
			var emailId = id + "-" + id;
			
			// Validate email address with regex
			if (!checkEmail(emailId)) 
			{
				alert("Please enter a valid email address");
				return;
			}
			
			// Serialize form values to be submitted with POST
			var str = $("form#subForm").serialize();
			
			// Add form action to end of serialized data
			// CDATA is used to avoid validation errors
			//<![CDATA[
			var serialized = str + "&action=" + formAction;
			// ]]>
			
			// Submit the form via ajax
			$.ajax({
				url: "http://artevino.ca/_globals/proxy/",
				type: "POST",
				data: serialized,
				success: function(data){
					// Server-side validation
					if (data.search(/invalid/i) != -1) {
						alert('The email address you supplied is invalid and needs to be fixed before you can subscribe to this list.');
					}
					else
					{
						$(".sign-up").hide();
						$("#confirmation").fadeIn("slow");
					}
				}
			});
		});
	});

	$(".clear").focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});

	$("form#register").validate({
		rules: {
			first_name: "required",
			last_name: "required",
			username: {
				required: true,
				email: true
			},
			password: {
				required: true,
				minlength: 5
			},
			password_confirm: {
				required: true,
				minlength: 5,
				equalTo: "#password"
			}
		},
		messages: {
			first_name: "Please enter your first name.",
			last_name: "Please enter your last name.",
			username: "Please enter a valid email address.",
			password: {
				required: "Please provide a password",
				minlength: "Your password must be at least 5 characters long."
			},
			password_confirm: {
				required: "Please provide a password.",
				minlength: "Your password must be at least 5 characters long.",
				equalTo: "Please enter the same password as above."
			}
		}
	});

	$("form#login").validate({
		rules: {
			username: {
				required: true,
				email: true
			},
			password: {
				required: true
			}
		},
		messages: {
			username: "Please enter a valid email address.",
			password: {
				required: "Please enter your password."
			}
		}
	});

	$("form#reset-password").validate({
		rules: {
			email: {
				required: true,
				email: true
			}
		},
		messages: {
			email: "Please enter a valid email address."
		}
	});
	
	$("form#checkout").validate({
		rules: {
			first_name: "required",
			last_name: "required",
			address: "required",
			city: "required",
			state: "required",
			zip: "required",
			country_code: "required",
			email_address: {
				required: true,
				email: true
			}
		},
		messages: {
			first_name: "Please enter your first name.",
			last_name: "Please enter your last name.",
			address: "Please enter your address.",
			city: "Please enter your city.",
			state: "Please select your province.",
			zip: "Please enter your Postal Code.",
			country_code: "Please select your Country.",
			email_address: "Please enter a valid email address."
		}
	});
	


});

