$(document).ready(function() {
	$('#team_select_gold').click(function() {
		_gaq.push(['_trackEvent', 'Registration', 'Choose Gold']);	
		if (!$('#GoldTeamId').attr('checked')) {
			$('#GoldTeamId').attr('checked',true);
			current_id = 3;
			$(".team_cb").each(function() {
				if ($(this).val() != current_id) {
					$(this).attr('checked',false);
				}
			});
			$('#UserTeamId').val(current_id);
		} else {
			$('#GoldTeamId').attr('checked',false);
			$('#UserTeamId').val(0);
		}
	});
	$('#team_select_red').click(function() {
		_gaq.push(['_trackEvent', 'Registration', 'Choose Red']);	
		if (!$('#RedTeamId').attr('checked')) {
			$('#RedTeamId').attr('checked',true);
			current_id = 1;
			$(".team_cb").each(function() {
				if ($(this).val() != current_id) {
					$(this).attr('checked',false);
				}
			});
			$('#UserTeamId').val(current_id);
		} else {
			$('#RedTeamId').attr('checked',false);
			$('#UserTeamId').val(0);
		}
	});
	
	$('#team_select_blue').click(function() {
		_gaq.push(['_trackEvent', 'Registration', 'Choose Blue']);	
		if (!$('#BlueTeamId').attr('checked')) {
			$('#BlueTeamId').attr('checked',true);
			current_id = 2;
			$(".team_cb").each(function() {
				if ($(this).val() != current_id) {
					$(this).attr('checked',false);
				}
			});
			$('#UserTeamId').val(current_id);
		} else {
			$('#BlueTeamId').attr('checked',false);
			$('#UserTeamId').val(0);
		}
	});
	$('#create_your_account').click(function() {
		all_false = true;
		$(".team_cb").each(function() {
			if ($(this).attr('checked')) {
				all_false = false;
			}
		});
		if (all_false) {
			$('#UserTeamId').val(0);
		}
		if (($('#registration_slider').css('marginLeft') == '0px') && ($('#UserTeamId').val() == 0)) {
			$('#registration_slider .error-message').show();
		} else {
			$('#registration_slider .error-message').hide();
			$('#registration_slider').animate( { marginLeft:"-792px"}, 1000 );
			_gaq.push(['_trackEvent', 'Registration', 'Create Your Account']);
			_gaq.push(['_trackPageview','/users/add/create']);		
		}
		return false;
	});

	$('#get_connected').click(function() {
		_gaq.push(['_trackEvent', 'Registration', 'Get Connected']);
		_gaq.push(['_trackPageview','/users/add/connected']);		
		if (checkFormValues()) {
			$('#registration_slider').animate( { marginLeft:"-1584px"}, 1000 );
		}
		return false;
	});
	
	$('#get_connected_first').click(function() {
		$('#registration_slider').animate( { marginLeft:"-1584px"}, 1000 );
		return false;
	});
	
	$('input:checkbox:not([safari])').checkbox();
	
	$('.team_cb').click(function() {
		current_id = $(this).val();
		if (current_id == 1) {
			_gaq.push(['_trackEvent', 'Registration', 'Choose Red']);	
		} else if (current_id == 2) {
			_gaq.push(['_trackEvent', 'Registration', 'Choose Blue']);	
		} else if (current_id == 3) {
			_gaq.push(['_trackEvent', 'Registration', 'Choose Gold']);	
		}
		$(".team_cb").each(function() {
			if ($(this).val() != current_id) {
				$(this).attr('checked',false);
			}
		});
		$('#UserTeamId').val(current_id);
	});
	var pheight = 0;
	$('#social_networks .round_380 .inner p').each(function() {
		if ($(this).height() > pheight) {
			pheight = $(this).height();
		}
	});
	$('#social_networks .round_380 span.inner p').height(pheight + 'px');
	$('#thickbox_scroll').click(function() {
		_gaq.push(['_trackEvent', 'Registration', 'View Terms']);	
		$('#pane1').jScrollPane({scrollbarWidth:31,dragMaxHeight:57,dragMinHeight:57});
	});
	$('#UserEmail').blur(function() {
		if ($(this).val().length > 0) {
			$.get("/users/check_existing", { name: 'email', value: $('#UserEmail').val() },checkEmail);	
		}
	});
	$('#UserUsername').keyup(function() {
		$('#username_display').html($(this).val());
	});
	$('#UserUsername').blur(function() {
		$('#username_display').html($(this).val());
	});
	$('#UserUsername').blur(function() {
		$('#final_error').hide();
		if ($(this).val().indexOf('&') != -1) {
			$('#final_error').show();
			$('#final_error').html('The username contains invalid characters.');		
		} else if ($(this).val().indexOf('?') != -1) {
			$('#final_error').show();
			$('#final_error').html('The username contains invalid characters.');		
		} else if ($(this).val().indexOf('"') != -1) {
			$('#final_error').show();
			$('#final_error').html('The username contains invalid characters.');		
		} else if ($(this).val().indexOf(':') != -1) {
			$('#final_error').show();
			$('#final_error').html('The username contains invalid characters.');		
		} else if ($(this).val().indexOf("'") != -1) {
			$('#final_error').show();
			$('#final_error').html('The username contains invalid characters.');		
		} else if ($(this).val().indexOf("/") != -1) {
			$('#final_error').show();
			$('#final_error').html('The username contains invalid characters.');		
		} else if ($(this).val().length > 0) {
			$.get("/users/check_existing", { name: 'username', value: $('#UserUsername').val() },checkUsername);	
		}
	});
	$('#select_team .step_num a').click(function() {
		_gaq.push(['_trackEvent', 'Registration', 'Forward to Page 2']);
		$('#registration_slider').animate( { marginLeft:"-792px"}, 1000 );
		return false;
	});
	$('#user_info .step_num a').click(function() {
		_gaq.push(['_trackEvent', 'Registration', 'Back to Page 1']);
		$('#registration_slider').animate( { marginLeft:"0px"}, 1000 );
		return false;
	});
});

var emailTaken = false;
var usernameTaken = false;

function checkEmail(data) {
	if (data == 'exists') {
		$('#final_error').show();
		$('#final_error').html('That email address already exists.');
		emailTaken = true;
	} else if (!echeck($('#UserEmail').val())) {
		emailTaken = true;
	} else {	
		$('#final_error').hide();
		emailTaken = false;
	}
}

function echeck(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	var errorString = 'Invalid email address';
	if (str.indexOf(at)==-1) {
		$('#final_error').show();
		$('#final_error').html(errorString);
		return false;
	}
	
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr) {
		$('#final_error').show();
		$('#final_error').html(errorString);
		return false;
	}
	
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr) {
		$('#final_error').show();
		$('#final_error').html(errorString);
		return false;
	}
	
	if (str.indexOf(at,(lat+1))!=-1) {
		$('#final_error').show();
		$('#final_error').html(errorString);
		return false;
	}
	
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) {
		$('#final_error').show();
		$('#final_error').html(errorString);
		return false;
	}
	
	if (str.indexOf(dot,(lat+2))==-1) {
		$('#final_error').show();
		$('#final_error').html(errorString);
		return false;
	}
	
	if (str.indexOf(" ")!=-1) {
		$('#final_error').show();
		$('#final_error').html(errorString);
		return false;
	}
	return true;				
}
function checkUsername(data) {
	if (data == 'exists') {
		$('#final_error').show();
		$('#final_error').html('That username is taken.');
		usernameTaken = true;
	} else {
		$('#final_error').hide();
		usernameTaken = false;
	}
}
function checkUsernameFinal(data) {
	if (data == 'exists') {
		$('#final_error').show();
		$('#final_error').html('That username is taken.');
	} else {
		$('#final_error').hide();
		$.get("/users/check_existing", { name: 'email', value: $('#UserEmail').val(), timestamp: new Date().getTime() },checkEmailFinal);	
	}
}
function checkEmailFinal(data) {
	if (data == 'exists') {
		$('#final_error').show();
		$('#final_error').html('That email address already exists.');
	} else {	
		$('#final_error').hide();
		document.UserAddForm.submit();
	}
}

function checkFormAndSubmit() {
	if (checkFormValues()) {
		if ($('#UserAccept').attr('checked')) {
			$('#final_error').hide();
			$.get("/users/check_existing", { name: 'username', value: $('#UserUsername').val(), timestamp: new Date().getTime() },checkUsernameFinal);	
		} else {
			$('#final_error').show();
			$('#final_error').html('You must accept the Terms and Conditions before you can register.');
		}
	}
}

function checkFormValues() {
	if (($('#UserEmail').val() == '') ||
		($('#UserUsername').val() == '') ||
		($('#UserPassword').val() == '') ||
		($('#UserConfirmpassword').val() == '')) {
		$('#reg_form .error-message').html('All fields are required.');		
		$('#reg_form .error-message').show();		
		return false;
	} else if (($('#UserTeamId').val() < 1) || ($('#UserTeamId').val() > 3)) {
		$('#select_team .error-message').show();
		$('#registration_slider').animate( { marginLeft:"0px"}, 1000 );
	} else if (($('#UserPassword').length > 0) && ($('#UserPassword').val().length < 6)) {
		$('#reg_form .error-message').show();		
		$('#reg_form .error-message').html('Password must be greater than 6 characters in length.');		
		return false;
	} else if (!echeck($('#UserEmail').val())) {
		return false;
	} else if (($('#UserUsername').val().length < 1) || ($('#UserUsername').val().length > 13)) {
		$('#reg_form .error-message').show();		
		$('#reg_form .error-message').html('Username must be between 1 and 13 characters');		
		return false;
	} else if (($('#UserUsername').val().indexOf('&') != -1) || ($('#UserUsername').val().indexOf('?') != -1) || ($('#UserUsername').val().indexOf(':') != -1) || ($('#UserUsername').val().indexOf('"') != -1) || ($('#UserUsername').val().indexOf("'") != -1) || ($('#UserUsername').val().indexOf("/") != -1)) {
		$('#final_error').show();
		$('#final_error').html('The username contains invalid characters.');		
		return false;
	} else if ($('#UserPassword').val() != $('#UserConfirmpassword').val()) {
		$('#reg_form .error-message').show();
		$('#reg_form .error-message').html('Passwords do not match');		
		return false;
	} else {
		if (usernameTaken || emailTaken) {			
			$('#reg_form .error-message').show();
			if (emailTaken) {
				$('#reg_form .error-message').html('Email already taken.');		
			} else {
				$('#reg_form .error-message').html('Username taken.');		
			}
			return false;
		} else {
			$('#reg_form .error-message').hide();
			return true;
		}
	}
}

