$(document).ready(function() {
	//Sign up
	$('#signup_submit').bind("click", function(event) {
		var f_name = $("#f_name").val();
		var l_name = $("#l_name").val();
		var signup_email = $("#signup_email").val();
		var signup_password = $("#signup_password").val();
		var sex = $("#sex").val();
		var birthdate_month = $("#birthdate_month").val();
		var birthdate_day = $("#birthdate_day").val();
		var birthdate_year = $("#birthdate_year").val();
		var code = $("#code").val();
		var tos = $("#tos").attr('checked');
	
		//Remove all errors
		$("#f_name_error").html('&nbsp;');
		$("#l_name_error").html('&nbsp;');
		$("#signup_email_error").html('&nbsp;');
		$("#signup_password_error").html('&nbsp;');
		$("#sex_error").html('&nbsp;');
		$("#birthdate_error").html('&nbsp;');
		$("#code_error").html('&nbsp;');
		$("#tos_error").html('&nbsp;');
	
		var section = '';
	
		if(f_name.length < 2 ) {
			$("#f_name_error").html('first name is too short').addClass('error_message');
			$("#signup_status").html('');
			return false;
		}
		if(l_name.length < 2 ) {
			$("#l_name_error").html('last name too short').addClass('error_message');
			$("#signup_status").html('');
			return false;
		}
		if(f_name == 'First Name') {
			$("#f_name_error").html('enter your first name').addClass('error_message');
			$("#signup_status").html('');
			return false;
		}
		if(l_name == 'Last Name') {
			$("#l_name_error").html('enter your last name').addClass('error_message');
			$("#signup_status").html('');
			return false;
		}
	
		var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	
		if(signup_email.length <= 0 || signup_email == 'myname@example.com') {
			$("#signup_email_error").html('enter an email address').addClass('error_message');
			$("#signup_email").focus();
			$("#signup_status").html('');
			return false;
		} 
	
		if(!re.test(signup_email)) {
			$("#signup_email_error").html('invalid email address').addClass('error_message');
			$("#signup_email").focus();
			$("#signup_status").html('');
			return false;
		}
		if(signup_password.length < 5 || signup_password.length > 20) {
			$("#signup_password_error").html('enter 5 - 20 characters').addClass('error_message');
			$("#signup_password").focus();
			$("#signup_status").html('');
			return false;
		}
		if(sex == -1) {
			$("#sex_error").html('enter your sex').addClass('error_message');
			$("#sex").focus();
			$("#signup_status").html('');
			return false;
		}
		if(birthdate_month == 0) {
			$("#birthdate_error").html('enter the month of your birthdate').addClass('error_message');
			$("#birthdate_month").focus();
			$("#signup_status").html('');
			return false;
		}
		if(birthdate_day == 0) {
			$("#birthdate_error").html('enter the day of your birthdate').addClass('error_message');
			$("#birthdate_day").focus();
			$("#signup_status").html('');
			return false;
		}
		if(birthdate_year == 0) {
			$("#birthdate_error").html('enter the year of your birthdate').addClass('error_message');
			$("#birthdate_year").focus();
			$("#signup_status").html('');
			return false;
		}
		if(code.length != 5) {
			$("#code_error").html('enter 5 characters').addClass('error_message');
			$("#code").focus();
			$("#signup_status").html('');
			return false;
		}
		if(!tos) {
			$("#tos_error").html('agree by checking the box').addClass('error_message');
			$("#tos").focus();
			$("#signup_status").html('');
			return false;
		}
		_lag.hover_box('#posting');	
		$.post("/includes/signup.php", {
			'f_name' : f_name,
			'l_name' : l_name,
			'email': signup_email,
			'password': signup_password,
			'sex' : sex,
			'birthdate_month' : birthdate_month,
			'birthdate_day' : birthdate_day,
			'birthdate_year' : birthdate_year,
			'code' : code,
			'tos' : tos
			}, function(data) {
				if(data == 'success') {
					$('#hover_box').css({'display' : 'none'});
					$('#modal-overlay').css({'display' : 'none'});
					_lag.popped = false;
					_lag.hover_box('#signup_msg');
					setTimeout(function(){window.location = '/registered.php';}, 1000);
				} else {
					$(_lag.target).html(_lag.temp);
					$('#hover_box').css({'display' : 'none'});
					$('#modal-overlay').css({'display' : 'none'});
					_lag.popped = false;
					$("#signup_status").html(data);
				}
		});
	
		return true;
	});
});
