var numErrors = 0;

$(document).ready(function() {
    $('#first_name, #last_name, #company, #title').blur(function(){
        if ($.trim($(this).val()) == '') { return writeError($(this), 'Please enter your ' + getLabel($(this)) + '.'); } else { return writeSuccess($(this));	}
    });
	$('#job_function, #business').bind("change blur", function() {
		if ($.trim($(this).val()) == '_default') { return writeError($(this), 'Please select your ' + getLabel($(this)) + '.'); } else { return writeSuccess($(this)); }
	});
	$('#email').blur(function() {
		$.ajax({
			type: "POST", url: "/register/index.php?mode=ajax", dataType: "json",	data: {action: "validate_email", email: $(this).val()},
			success: function(data) { if (data.isValid != false) { return writeSuccess($('#email'), true);	} else { return writeError($('#email'), data.msg); } }
		});
	});
	$('#email_conf').blur(function() {
		if ($.trim($(this).val()) == '') {
			return writeError($(this), 'Please re-enter your e-mail address.');
		}
		else {
			if ($(this).val() != $('#email').val()) {
				return writeError($(this), 'The e-mail addresses you have entered do not match.');
			}
			else {
				return writeSuccess($(this));
			}
		}
	});
	$('#password_curr').blur(function() {
		if ($.trim($(this).val()) == '') { return writeError($(this), 'Please enter your current password.'); } else { return writeSuccess($(this)); }
	});
	$('#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 getLabel(obj) {
	var label = $(obj).parent().parent().find('label').text().toLowerCase();
	return label.substring(0, label.length-1);
}

function writeSuccess(obj, showCheck){
	$('#' + $(obj).attr('id') + '-error').remove();
	if (showCheck == true) {
		obj.removeClass('error').parent().parent().find('td.validation').html('<img src="/images/global/field-ok.png" alt="Field is valid" />');
	} else {
		obj.removeClass('error').parent().parent().find('td.validation').html('');
	}
	return true;
}

function writeError(obj, errorMsg)
{
	$('#' + obj.attr('id') + '-error').remove();
	var row = obj.addClass('error').parent().parent();
	$(row).find('td.validation').html('<img src="/images/global/field-error.png" alt="Field has an error" />');
	$(row).after($('<tr/>').attr({id: obj.attr('id') + '-error', className: 'error-msg'}).append($('<td/>')).append($('<td/>').attr('colspan', '2').html(errorMsg)));
	numErrors++;
	return false;
}

function showWhyRegister()
{
	$('#preview-message').slideDown();
}

function validate()
{
	numErrors = 0;
	$("#register input[rel='validate'], #register select[rel='validate']").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 ' + numErrors + ' errors. Please correct them and re-submit.');
		}
		return false;
	} else {
		return true;
	}
}

var RecaptchaOptions = {
   theme: 'custom',
   lang: 'en'
};