function ClearForm()
{
	$('#firstName').val('');
	$('#country_list').val(0);
	$('#gender').val(0);
	$('#email').val('');
	$('#password').val('');
	$('#confirm_password').val('');
	$('#codeinput').val('');
	var obj = $('#agree');
	obj[0].checked = false;
}
function RefreshCaptcha()
{
	$('#imgcpatcha').html('');
	$('#imgcpatcha').load('/register #imgcpatcha');
}
$().ready(function(){
	$('#firstName').focus();
	var obj = $('#agree');
	$('#RegisterForm').submit(function(){
		//check firstName
		$('.errormsg').html('');
		var firstName = $('#firstName').val();
		if($().isEmpty(firstName))
		{
			$("#error_firstname").html('Please enter your name.');
			$('#firstName').focus();
			return false;
		}

		
		//check gender
		var gender = $('#gender').val();
		if($().isEmpty(gender))
		{
			$("#error_gender").html('Please select your gender.');
			$('#gender').focus();
			return false;
		}

		//check country
		var country = $('#country_list').val();
		if($().isEmpty(country))
		{
			$("#error_countrylist").html('Please select your country of residence.');
			$('#country_list').focus();
			return false;
		}

		//check email
		var email = $('#email').val();
		if(!$().isEmail(email))
		{
			$("#error_email").html('Please enter a valid email address.');
			$('#email').focus();
			return false;
		}

		//check password
		var password = $('#password').val();
		if($().isEmpty(password))
		{
			$("#error_password").html('Please enter your password.');
			$('#password').focus();
			return false;
		}
		if(password.length<5)
		{
			$("#error_password").html('Your password must be at least 5 characters long.');
			$('#password').focus();
			return false;
		}

		//check confirm_password
		var confirm_password = $('#confirm_password').val();
		if($().isEmpty(confirm_password))
		{
			$("#error_confirmpassword").html('Please re-enter your password.');
			$('#confirm_password').focus();
			return false;
		}
		if(password != confirm_password)
		{
			$("#error_confirmpassword").html('Please re-enter your password.');
			$('#confirm_password').focus();
			return false;
		}

		//check codeinput
		var codeinput = $('#codeinput').val();
		if($().isEmpty(codeinput))
		{
			$("#error_codeinput").html('Please enter the letters and numbers as shown in the image.');
			$('#codeinput').focus();
			return false;
		}

		//check agree
		var obj = $('#agree');
		var agree = obj[0].checked;
		if(!agree)
		{
			$("#error_agree").html('Please check the box.');
			$('#agree').focus();
			return false;
		}
		var refer = $('#refer').val();
		var collect_data = {'firstName':firstName,'gender':gender,'country':country,'email':email,'password':password,'codeinput':codeinput,'refer':refer};
		
		$('#RegisterForm #error_hint').html('processing...');
		$.post('/register/register', collect_data, function(data)
		{
			$('#RegisterForm #error_hint').html('');
			var msg = data;
			
			switch(data)
			{
				case "SUCCESS":
					ClearForm();
					msg = 'Register successful.';
					$('#RegisterForm #error_hint').html(msg);
					setTimeout(location.href=refer,200);
				break;
				
				case "DUPLICATE_EMAIL":
					msg = 'Sorry, this email address is already registered. Please try another.';
					$('#RegisterForm #error_hint').html(msg);
				break;
				
				case "ERROR_CODE":
					RefreshCaptcha();
					msg = 'Please enter the letters and numbers as shown in the image.';
					$("#error_codeinput").html(msg);
				break;
			}
		});
		return false;
	})
})
