$(document).ready(function() {
	$('#frmSignup').submit(function() {
		$('#signup_loading').show();
		
		$.ajax({
			type : 'POST',
			url : '/user/signup/',
			data : $(this).serialize(),
			dataType : 'json',
			success : function(response) {
				if (response == 'ok') {
					$('#frmSignup').hide();
					$('#signed_up').show();
					$('#email').text($('#user_email').val());
				} else {
					$('#signup_loading').hide();
					
					$('#frmSignup').find('.error').removeClass('error');
					$.each(response, function(i, val) {
						$('#field_' + val[0] + ' div.errorMsg').html(val[1]);
						$('#field_' + val[0]).addClass('error');
					});
				}
			},
			error : function(request, status, err) {
				$('#signup_loading').hide();
				ajaxError(request, status, err);
			}
		});
		return false;
	});
	
	$('#user_username').keypress(function(e) {
		var key = e.which;
		var character = String.fromCharCode(key).toLowerCase();
		
		// control keys
		if (key == null || key == 0 || key == 8 || key == 9 || key == 13 || key==27) {
			return true;
		}
		
		if (("abcdefghijklmnopqrstuvwxyz0123456789-").indexOf(character) > -1) {
			return true;
		} else {
			return false;
		}
	});
});
