$(document).ready(function(){
//  var namePassed=false;
	$('#txtName').blur(function(){
		var obj = $(this);
		if($.trim(obj.val()) != ''){
			$.ajax({
				url:'/' + shopFolder +'MemberRegister.aspx?NoCopyright=1&ajax=CheckName',
				type:'post',
				data:'name=' + escape($.trim(obj.val())),
				async:false,
				success:function(msg){
					if(msg == '1'){
						$('#nameExist').show();
						obj.focus();
						namePassed = false;
					}else{
						$('#nameExist').hide();
						namePassed = true;
					}
				},error:function(){alert('error');}
			});
		};
	}).keyup(function(){
		$('#nameExist').hide();
	});
	
//	var emailPassed = false;
	$('#txtEmail').blur(function(){
		var obj = $(this);
		if($.trim(obj.val()) != ''){
			$.ajax({
				url:'/' + shopFolder +'MemberRegister.aspx?NoCopyright=1&ajax=CheckEmail',
				type:'post',
				data:'email=' + escape($.trim(obj.val())),
				async:false,
				success:function(msg){
					if(msg == '1'){
						$('#emailExist').show();
						obj.focus();
						emailPassed = false;
					}else{
						$('#emailExist').hide();
						emailPassed = true;
					}
				},error:function(){alert('error');}
			});
		};
	}).keyup(function(){
		$('#emailExist').hide();
	});
	
	var agentPassed = true;
	$('#txtAgentMemberName').blur(function(){
		var obj = $(this);
		if($.trim(obj.val()) != ''){
			$.ajax({
				url:'/' + shopFolder +'MemberRegister.aspx?NoCopyright=1&ajax=CheckAgentMemberName',
				type:'post',
				data:'agentMembername=' + escape($.trim(obj.val())),
				async:false,
				success:function(msg){
					if(msg == '0'){
						$('#agentNotExist').show();
						agentPassed = false;
					}else{
						$('#agentNotExist').hide();
						agentPassed = true;
					}
				},error:function(){alert('error');}
			});
		};
	}).keyup(function(){
		$('#agentNotExist').hide();
	});
	
	$('#btnRegister').click(function(){
        if(!Empty($('#txtName'))){alert('用户名不能为空');return false;};
		if(!Empty($('#txtPassword'))){alert('密码不能为空');return false;};
		if(!Empty($('#txtEmail'))){alert('您的电子邮箱地址不能为空');return false;};		
        if($.trim($('#txtAgentMemberName').val())==''){agentPassed=true;};	
	    if(!namePassed){
			alert('此用户名已被注册,请重新输入.');return false;
		};
		if(!emailPassed){
			alert('此邮箱已被使用,请重新输入.');return false;
		};
        if(!agentPassed){
			alert('推广员不存在,请重新输入.');return false;
		};
//		if(!verifyUsername($('#txtName'))){ alert('用户名长度为6-16位字符，可由中英文、数字及"_"、"-"组成');return false};
//      if(!verifyPassword($('#txtPassword'))){ alert('密码长度为6-16位字符，可由英文、数字及"_"、"-"组成');return false};

		if(!verifyEmail($('#txtEmail'))){alert('您输入的邮箱地址格式错误');return false};
		if(!Same($('#txtPassword'),$('#txtPassword2'))){alert('两次密码输入不一致');return false;};
	    if($('#cbRegCard').attr('checked')){
    		if(!Empty($('#txtCardNo'))){alert('用户卡号不能为空');return false;};
	    	if(!Empty($('#txtCardPwd'))){alert('用户密码不能为空');return false;};
	    }
		if(!$('#cbyhxy').attr('checked')){
			alert('只有同意并接受相关条款才可以注册');
			return false;
		};
	});
	
	showCard();
	$('#cbRegCard').click(function(){
		showCard();
	});
});

function showCard(){
	if($('#cbRegCard').attr('checked')){
		$('#card').show();
	}else{
		$('#card').hide();
	}
}

function Empty(obj){
	if($.trim(obj.val())==''){return false;};
	return true;
};

function Same(obj1,obj2){
	if(obj1.val() != obj2.val()){return false};
	return true;
};
function verifyEmail(obj){
    var reg =/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;

    return reg.test(obj.val());
}

function verifyPassword(obj){
    var reg = /^[a-zA-Z0-9_-]{6,16}$/;
    return reg.test(obj.val());
}
function verifyUsername(obj){
    var reg = /^[a-zA-Z0-9_\u4e00-\u9fa5-]{6,16}$/;
    return reg.test(obj.val());
}

