$(document).ready(function() {
	
	$('#register_btn').click(register);
	
	initRecaptcha();
	
	$('#register-tabs').tabs({
		collapsible: true,
		selected: -1
	});
	
    $('#first_name, #last_name, #company, #job_title').blur(function(){
        if ($.trim($(this).val()) == '') { return writeError($(this), 'Please enter your ' + getLabel($(this)) + '.'); } else { return writeSuccess($(this));	}
    });
	$('#business_type, #job_level, #job_function').bind("change blur", function() {
		if ($.trim($(this).val()) == '-1') { return writeError($(this), 'Please select your ' + getLabel($(this)) + '.'); } else { return writeSuccess($(this)); }
	});
	$('#secret_question').bind("change blur", function() {
		if ($.trim($(this).val()) == '-1') { 
			writeSuccess($('#secret_answer')); $('#secret_answer').val('').closest('tr').hide();
		} else { $('#secret_answer').closest('tr').show(); $('#secret_answer').focus(); return writeSuccess($(this)); }
	});
	$('#secret_answer').blur(function(){
        if ($.trim($(this).val()) == '') {
			if ($.trim($('#secret_question').val()) != '-1') {
				return writeError($(this), 'Please enter your ' + getLabel($(this)) + '.');
			}
		} else { return writeSuccess($(this));	}
    });
	$('#email').blur(function() {
		$.post(window.location.pathname, {
			action: 'validateEmail',
			email: $(this).val()
		}, function(data, textStatus) {
			if (data.isValid) {
				return writeSuccess($('#email'), true);
			} else {
				return writeError($('#email'), data.message);
			}
		}, 'json');
	});
	$('#password').blur(function() {
		if ($.trim($(this).val()).length < 6) { return writeError($(this), 'Please choose a password that is at least 6 characters long.'); } else { return writeSuccess($(this)); }
	});
	$('#password_conf').blur(function() {
		if ($.trim($(this).val()) == '') {
			return writeError($(this), 'Please re-enter your password.');
		}
		else {
			if ($(this).val() != $('#password').val()) {
				return writeError($(this), 'The passwords you have entered do not match.');
			}
			else {
				return writeSuccess($(this));
			}
		}
	});
	$('#recaptcha_response_field').blur(function(){
        if ($.trim($(this).val()) == '') { return writeError($(this), 'Please enter the words shown in the box above.'); } else { return writeSuccess($(this)); }
    });
});

function register()
{
	numErrors = 0;
	$("#register input.validate, #register select.validate, #recaptcha_response_field").each(function () {
		var vRs = $(this).blur();
	});

	if (numErrors > 0) {
		if (numErrors == 1) {
			alert('Your form has 1 error. Please correct it and re-submit.');
		} else {
			alert('Your form has errors. Please correct them and re-submit.');
		}
	} else {
		showLoadingModal('Processing &hellip;');
		$('#register_btn').attr('disabled', 'disabled').text('Processing ...');
		$.post(window.location.pathname, {
			action: 'register',
			data: $('#register').serialize()
		}, function(data, textStatus) {
			initRecaptcha();
			if (data.success) {
				showLoadingModal('Setting up your account &hellip;');
				writeSuccess($('#recaptcha_response_field'));
				window.location = data.forward;
			}  else {
				if (data.message == 'CAPTCHA') {
					hideLoadingModal();
					$('#register_btn').removeAttr('disabled').text('Register');
					writeError($('#recaptcha_response_field'), 'The words that you entered were not correct. Please try again by entering the words in the box.');
					$('#recaptcha_response_field').focus();
				}
			}
		}, 'json');
	}
}
