
// When the DOM is ready...
$(function(){
		   
		   
    function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
	return pattern.test(emailAddress);
	}
	
			
	
	
	
	// Hide stuff with the JavaScript. If JS is disabled, the form will still be useable.
	$("#submit_button").attr("disabled",true);
	$("#friends_name_wrap").hide();
	$("#submit_button").hide();
	
		
	// Reset form elements back to default values
	$("#step_1 input[type=radio]").each(function(){
		this.checked = false;
	});
	$("#privacy").each(function(){
		this.checked = false;
	});

	$("#step_3").hide();
	
	
	$.stepOneComplete_one = "not complete";
	$.stepOneComplete_two = "not complete"; 
	$.stepTwoComplete_one = "not complete";
		
	
	function stepOneTest() {
		if (($.stepOneComplete_one == "complete") && ($.stepOneComplete_two == "complete") && ($.stepTwoComplete_one == "complete")) {
			$("#step_1")
			.animate({
				"paddingBottom": "120px"
			})
			.css({
				"background-image": "url(images/check.gif)",
				"background-position": "bottom center",
				"background-repeat": "no-repeat"
			});
			
			$("#step_3").show();
			
		}
		else
		{
			$("#step_1")
			.css({
				"background-image": "none"
			});
			$("#step_3").hide();
		}
	};
	
	
	$("#name").blur(function(){
		if ($(this).val() != '' ) {
		$.stepOneComplete_one = "complete"; 
		}
		stepOneTest();
	});
	
		$("#email").blur(function(){
		if(isValidEmailAddress($("#email").val()))
		{
			$.stepOneComplete_two = "complete"; 
		}
		else {
			$.stepOneComplete_two = "not complete"; 	
		}
		stepOneTest();
	});
		

	

	$("#step_1 input[name=friends_toggle_group]").click(function(){
		$.stepTwoComplete_one = "complete"; 
		if ($("#friends_toggle_on:checked").val() == 'on') {
			$("#friends_name_wrap").slideDown();
		} else {
			$("#friends_name_wrap").slideUp();
		};
		stepOneTest();
	});
	
	$("#privacy").click(function(){
		if (this.checked && $.stepOneComplete_one == 'complete' && $.stepOneComplete_two == 'complete'
		  	 && $.stepTwoComplete_one == 'complete') {
				$("#submit_button").show();
				$("#submit_button").attr("disabled",false);
			} else {
				$("#submit_button").hide();
				$("#submit_button").attr("disabled",true);
		}
	});
	
});