////////////////////////////////////////////////////////////////////////////////////////////////////

var maillistFormId = 'ml_form';
var storeLocatorFormId = 'storeLocator';

////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////

// MAILING LIST FORM
var ml = {
	errors : '',
	run : function() {
		this.form = $(maillistFormId);
		this.validate();
		return false;
	},
	
	validate : function() {
		this.email();
		this.birth();
		if(this.errors != '') {
			alert(this.errors);
			this.errors = "";
		} else {
			this.save();
		}
	},
	
	save : function() {
		var formData = $('ml_form').serialize();
		e.jax({
			url : '/optin.php',
			vars : formData,
			onSuccess : function(c) {
				$('update').innerHTML = c;
			}
		});
	},
	
	error : function(msg) {
		this.errors = this.errors + msg + "\r\n";
	},
	
	email : function() {
		var email = $F('email'), vemail = $F('confirm_email');
		var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
		if(filter.test(email)) {
			if(email != vemail) {
				this.error('Your confirmation email address doesn\'t match the email field');
			}
		} else {
			this.error('Please provide a valid email');
		}
	},

	birth : function() {
		var now = new Date();
		var birth = new Date($F('birth_year'), $F('birth_month')-1, 1);
		var years = (((now.getTime() - birth.getTime()) / 3600000) / 24) / 365;
		return (years >= 13) ? true : this.error('Not old enough to Opt-in');
	}
};

var catchEmailOptin = function() {
	ml.run();
	return false;
};

////////////////////////////////////////////////////////////////////////////////////////////////////

// STORE LOCATOR FORM


////////////////////////////////////////////////////////////////////////////////////////////////////