$(document).ready(function() {
	$("form.email-validator").emailValidator();
	$("#focusbox").focus();
	$("#refcodelink").click(function(event) {
		$("#refcodelink").slideUp();
		$("#refcodebox").slideDown();
		event.stopPropagation();
		event.preventDefault();
	});
});
 
// plugins
(function($) {
	// plugins
	$.fn.extend({
	
	emailValidator: function() {
		// implicit iteration for maintaining good ref to THIS
		return this.each(function(){
			var $this = $(this); // this is the form
			// get submit
			$this.submit(function() {
				// get email field
				var $emailField = $(".email-validator-field", $this);
				var emailFieldVal = $emailField.val();
				if (emailFieldVal == '') {
					// empty
					$this.addClass("error");
					$emailField.focus();
					return false;
				} else if (!Validator.validEmail(emailFieldVal)) {
					// not ok
					$this.addClass("error");
					$emailField.focus();
					return false;
				} else {
					// ok
					return true;
				}			
			});
		});
	}
	// emailValidator
	
	});
	
})(jQuery);


// Validator help functions
var Validator = function() {
	return {
		validEmail : function(strEmail) {
			//var emailReg = "^[\\w-_\.+]*[\\w-_\.]\@([\\w]+\\.)+[\\w]+[\\w]$";
			var emailReg = "^[\\w-_\.+]*[\\w-_\.]\@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$";
			var regex = new RegExp(emailReg);
			return regex.test(strEmail);
		}
	};
}();
