/*
* Registration Form Class
* Uses jquery framework
* Stardock
* mlucas
* Last Updated: 2011-03-17
*/
$(document).ready(function() {

	var _form = $('#frmRegister');
	var _step2 = $('#step2');
	var _step3 = $('#step3');
	
	$('#btnSubmit').attr('disabled', true);

	_step2.hide();
	_step3.hide();
			
	// Start form off in first field
	$('#txtFirst').focus();
	// Is it a back button ?
	if ($('currentStep').val() == 3)
	{
		_step2.show();
		_step3.show();
		$('#txtNickName').attr('readonly', true);
		$('#btnSubmit').attr('disabled', false);
		$('#btnSubmit').val('Create New Account');
	}
	
	// Assign event handlers
	/* Form submit, process depending on step */
	_form.submit(function() {
		var currentStep = $('#currentStep').val();
		if (currentStep == 1) {
			_step2.slideDown();
			$('#currentStep').val(2);
			$('#txtNickName').val($('#txtFirst').val() + $('#txtLast').val());
			CheckNickNameAvailability();
		} else if (currentStep == 2) {
			if ($('#btnSubmit').val() == "Continue") {
				$('#currentStep').val(3);
				
				_step3.slideDown(1000, function() {
					$('#txtEmail1').focus();
					$('#btnSubmit').val('Create New Account');
					});
			} else {
				CheckNickNameAvailability();
			}		
		} else if (currentStep == 3) {
			/* Use existing validation function */
			if ($('#btnSubmit').val() == "Check Availability") {
				CheckNickNameAvailability();
			} else {
				if (validate()) {
					return true;
				} else {
					return false;
				}
			}
		} else { return false; }
		return false;
	});

	/* First name field, check if both name fields are not empty before enabling continue button */
	$('#txtFirst,#txtLast').bind('keyup', function() {
		if ($('#txtFirst').val() != ""
			&& $('#txtLast').val() != "") {
			$('#btnSubmit').attr('disabled', false);
		} else {
			$('#btnSubmit').attr('disabled', true);
		}
	});

	/* NickName field, make sure nickname is available before enabling continue */
	$('#txtNickName').keypress(function(e)
	{
		var key = e.which;

		if (key != 9 && key != 10 && key != 13) // Tab, Carriage Return, Line Feed
		{
			//$('currentStep').set('value', 2);
			$('#btnSubmit').val('Check Availability');
			$('#btnSubmit').attr('disabled', false);
		}
	});
	
	function CheckNickNameAvailability() {
		var NickRequest = $.post(_form.attr('action'),
			{ 'cmd': 'checknick', 'txtNickName': $('#txtNickName').val() },
			function(data, textStatus, jqXHR) {
				if (data == "0") {
					$('#availability').html("Nickname unavailable, please choose another");
					$('#btnSubmit').attr('disabled', true);
					$('#txtNickName').focus();
					$('#txtNickName').select();
					NameValid = false;
					
				} else if (data == "1") {
					$('#availability').html("<span style=\"color: #F00; font-weight: bold\">Nickname available!</span>");
					$('#btnSubmit').attr('disabled', false);
					$('#txtNickName').focus();
					if ($('#currentStep').val() == 3) {
						$('#txtEmail1').focus();
						$('#btnSubmit').val('Create New Account');
					} else {
						$('#btnSubmit').val('Continue');
					}
					NameValid = true;
				}
		}, 'html');
	}
});
