function getCookie(name){
	var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
	if(arr != null){
		return unescape(arr[2]);
	}
	return null;
}
function __unclear(){
	$('#vfcodeimg').attr('src', 'uc!vfcode.uc?'+Math.random());
}

function _username_blur(){
	$('#username_0').hide();
	$('#username_1').hide();
	if(/^[A-Z|a-z|0-9]{5,16}$/.test($('#username').val())){
		$.ajax({
			url:'uc!check_username.uc', type:'POST', dataType: 'json',
			data:{username:$('#username').val(), r: Math.random()}, 
			success: function(res){
				if(res.success){
					$('#username_1').show();
				}else{
					$('#username_0').show();
					alert(res.message);	//显示错误信息
				}
			}
		});
	}else{
		$('#username_0').show();
	}
}
function _username_(){
	$('#username_0').hide();
	$('#username_1').hide();
	if(/^[A-Z|a-z|0-9]{5,16}$/.test($('#username').val())){
		$('#username_1').show();
	}else{
		$('#username_0').show();
	}
}
function _password_(){
	$('#password_0').hide();
	$('#password_1').hide();
	if(/^.{5,16}$/.test($('#password').val())){
		$('#password_1').show();
	}else{
		$('#password_0').show();
	}
}

function _repassword_(){
	$('#repassword_0').hide();
	$('#repassword_1').hide();
	if(/^.{5,16}/.test($('#repassword').val()) && $('#password').val()==$('#repassword').val()){
		$('#repassword_1').show();
	}else{
		$('#repassword_0').show();
	}
}
function _email_(){
	$('#email_0').hide();
	$('#email_1').hide();
	if(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test($('#email').val())){
		$('#email_1').show();
	}else{
		$('#email_0').show();
	}
}
function _idcard_(){
	$('#idcard_0').hide();
	$('#idcard_1').hide();
	if(checkIDCard($('#idcard').val()) == 'ok'){
		$('#idcard_1').show();
	}else{
		$('#idcard_0').show();
	}
}

function __register(){
	//alert(getCookie('vfcode'));
	if($('#username').val()==''){
		alert("用户名不能为空!");	return;
	}
	if($('#password').val()==''){
		alert("密码不能为空!");	return;
	}
	if($('#password').val() != $('#repassword').val()){
		alert("两次输入的密码不一致!");	return;
	}
	if($('#email').val()==''){
		alert("邮箱不能为空!");	return;
	}
	if(! /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test($('#email').val())){
		alert("邮箱格式错误!");	return;
	}
	if($('#vfcode').val()==''){
		alert("验证码不能为空!");	return;
	}
	if($('#vfcode').val() != getCookie('vfcode')){
		alert("验证码不正确!");	return;
	}
	if(!$('#applay_order_1').attr('checked')){
		alert("请仔细阅读并同意《使用条款》后再提交表单！");	return;
	}
	if(!$('#applay_order_1').attr('checked')){
		alert("请仔细阅读并同意《网络游戏防沉迷协议》后再提交表单！");	return;
	}
	
	lock();
	$.ajax({
		url: 'uc!register.uc', type: 'POST', dataType: 'json',
		data: {
			username: $('#username').val(),
			password: $('#password').val(),
			repassword: $('#repassword').val(),
			email: $('#email').val(),
			idcard: $('#idcard').val(),
			vfcode: $('#vfcode').val(),
			r: Math.random()
		},
		success: function(res){
			unlock();
			alert(res.message);
			if(res.success){
				//top.location	= './register_profile.html';
				top.location	= './register.jsp?fwd=register_profile.html';
			}
		}
	});
}



$(document).ready(function(){
	loadReference();	//加载地址栏中的ref参数
	loadHeader();
	loadHelper();
	loadFooter();
	/* 基本信息处理结束 */
	
	
	/* 处理本页信息开始 */
	$('#username').bind("blur", _username_blur);
	$('#username').bind("keyup", _username_);
	$('#username').bind("keydown", _username_);
	
	$('#password').bind("blur", _password_);
	$('#password').bind("keyup", _password_);
	$('#password').bind("keydown", _password_);
	
	$('#repassword').bind("blur", _repassword_);
	$('#repassword').bind("keyup", _repassword_);
	$('#repassword').bind("keydown", _repassword_);
	
	$('#email').bind("blur", _email_);
	$('#email').bind("keyup", _email_);
	$('#email').bind("keydown", _email_);
	
	$('#idcard').bind("blur", _idcard_);
	$('#idcard').bind("keyup", _idcard_);
	$('#idcard').bind("keydown", _idcard_);
	
	$('#_unclear').bind('click', __unclear);
	
	$('#_register').bind('click', __register);
	$('#_goto_login').bind('click', function(){ location='./sso-login.html'; });
});

