function checkfrom(){
	var u = document.getElementById('username');
	if(u.value==''){
		alert("用户名不能为空！");
		document.getElementById('username').focus();
		return false;
	}
	if(u.length<4){
		alert("用户名长度不够,应为4-16位！");
		document.getElementById('username').focus();
		return false;
	}
	
	var u_v = document.getElementById('user_val');
	if(ischi(u_v.value)){
		alert("用户名中不能包含中文！");
		return false;
	}
	if(u_v.value=='1'){
		if(u.value.length<4){
			alert("用户名长度错误,应为4-16位！");
			return false;
		}else{
			alert("用户名已占用！");
			set_username_css("用户名已占用！");
			return false;	
		}	
	}
	
	if(/.*[\u4e00-\u9fa5]+.*$/.test(u.value))    
	{    
		alert("用户名不能含有汉字！");    
		return false;    
	}
	if(chekemail($('#user_email').val())==false){
		alert('邮箱格式不正确!');
		return false;	
	}
	var e_v = document.getElementById('email_val');
	if(e_v.value=='1'){
		alert("邮箱已占用！");
		set_email_css("邮箱已占用！");
		return false;
	}
	
	if($('#username').val()==''){
		alert('用户名不能为空！');
		return false;
	}
	
	var user1 = $('#username').val();
	if(user1.length >16){
	alert('用户名的长度不能大于16个字符');
	return false;
	}
	
	var pass1=$('#pass1').val();
	if($('#pass1').val()==''){
		alert('密码不能为空!');
		$('#password').focus()
		return false;	
	}
	
	if( $('#pass1').val().length<6 || $('#pass1').val().length>20 ){
		alert('密码长度不正确!');
		$('#password').focus()
		return false;	
	}
	
	if($('#pass1').val() != $('#pass2').val()){
		alert('密码与重复密码不一致!');
		return false;	
	}
	
	if($('#code').val()==''){
		alert('验证码不能为空!');
		$('#code').focus()
		return false;	
	}
	
	if($('#user_email').val() == ''){
		alert('邮箱不能为空!');
		return false;	
	}

}

function check_username_css(){
	$('#usererror').hide();
	$("#username").css({ color: "black", background: "white" });
	$("#div_username").css({ color: "black"});
	$("#div_username").html('由英文或数字，长度3~18个字符。(不允许包含中文字符)');
	$("#user_val").val('0');
}
function set_username_css(msg){
	$('#userok').hide();
	$('#usererror').show();
	$("#div_username").html(msg);
	$("#div_username").css({ color: "red"});
	$("#username").css({ color: "blue", background: "red" });
	$("#user_val").val('1');
}
function check_email_css(){
	$('#emailerror').hide();
	$("#user_email").css({ color: "black", background: "white" });
	$("#div_email").css({ color: "black"});
	$("#div_email").html('邮箱是您找回丢失密码的唯一途径');
	$("#email_val").val('0');
}
function set_email_css(msg){
	$('#emailok').hide();
	$('#emailerror').show();
	$("#div_email").html(msg);
	$("#div_email").css({ color: "red"});
	$("#user_email").css({ color: "blue", background: "red" });
	$("#email_val").val('1');
}


function iscn(username){
	if(/.*[\u4e00-\u9fa5]+.*$/.test(username))    
	{    
		return true;    
	}
}
function ischi(name){
	if(name.match(/[^\u4e00-\u9fa5]/g)){
		return false;
	}else{
		return true;
	}
}
function chekemail(temail){ 
	var pattern=/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/; 
	if(pattern.test(temail)){ 
		return true; 
	}else{ 
		return false; 
	} 
}

//检测用户名是否重复
function chk_user(){
	var user = $('#username').val();
		if(user=='' || user.length <=3 || user.length >16 || ischi(user) || iscn(user)){	
			$('#userok').hide();
			$('#usererror').show();
			$("#username").css({ color: "blue", background: "red" });
			$("#user_val").val('1');
			return false;
		}else{
			$('#usererror').hide();
			$("#username").css({ color: "black", background: "white" });
			$("#user_val").val('0');
		}
		$("#text").hide();$("#ok").hide();
		$.ajax({
			type:'GET',
			url:'reg.php',
			data:'do=chk_user&username='+user,
			beforeSend: function (){
				$('#usererror').hide();
				$('#userok').hide();
				$('#usermove').show();
			},
			complete: function (){
				$('#usermove').hide();
			},
			success: function(data){
				
				switch(data)
				{
					case '1':
						$('#usererror').show();
						$("#username").css({ color: "blue", background: "red" });
						$("#user_val").val('1');
						set_username_css("用户名已占用！");
						break;
					case '0':
						$('#userok').show();
						$("#username").css({ color: "black", background: "white" });
						$("#user_val").val('0');
						break;
				}
   		    }
	});
}


//检测邮箱是否重复
function chk_email(){
		var email = $('#user_email').val();
		if(email=='' || chekemail(email)==false){
			$('#emailok').hide();
			$('#emailerror').show();
			$("#user_email").css({ color: "blue", background: "red" });
			$("#email_val").val('1');
			return false;
		}
		else{
			$('#emailerror').hide();
			$("#user_email").css({ color: "black", background: "white" });
			$("#email_val").val('0');
		}
		
		$("#email_possess").hide();$("#email_ok").hide();
		$.ajax({
			type:'GET',
			url:'reg.php',
			data:'do=chk_email&email='+email,
			beforeSend: function (){
			
				$('#emailerror').hide();
				$('#emailok').hide();
				$('#emailmove').show();
			},
			complete: function (){
				$('#emailmove').hide();
			},
			success: function(data){
				//alert(data);
				switch(data)
				{
					case '1':
						$('#emailerror').show();
						$("#user_email").css({ color: "blue", background: "red" });
						$("#email_val").val('1');
						set_email_css("邮箱已占用！");
						break;
					case '0':
						$('#emailok').show();
						$("#user_email").css({ color: "black", background: "white" });
						$("#email_val").val('0');
						break;
				}
   		    }
	});
}

//检测身份证是否重复
function chk_id(){
		var id = $('#card1').val();

		if(id == ''){
			$('#iderror').show();
			$("#card1").css({ color: "blue", background: "red" });
			$("#card1_val").val('1');
			return false;	
		}
		if( id.length !=18 && id.length !=15 ){
			$('#iderror').show();
			$("#card1").css({ color: "blue", background: "red" });
			$("#card1_val").val('1');
			return false;	
		}
		
		$("#iderror").hide();
		$("#idok").hide();
		$.ajax({
			type:'GET',
			url:'reg.php',
			data:'do=chk_id&id='+id,
			beforeSend: function (){
			
				$('#iderror').hide();
				$('#idok').hide();
				$('#idmove').show();
			},
			complete: function (){
				$('#idmove').hide();
			},
			success: function(data){
				switch(data)
				{
					case '1':
						
						$('#iderror').show();
						$("#card1").css({ color: "blue", background: "red" });
						$("#card1_val").val('1');
						break;
					case '0':
						
						$('#idok').show();
						$("#card1").css({ color: "black", background: "white" });
						$("#card1_val").val('0');
						break;
				}
   		    }
	});
}

//检测手机号是否重复
function chk_mobile(){
		var mobile = $('#mobile').val();

		if(mobile == ''){
			$('#mobileerror').show();
			$("#mobile").css({ color: "blue", background: "red" });
			$("#mobile_val").val('1');
			return false;	
		}
		if( mobile.length !=11){
			$('#mobileerror').show();
			$("#mobile").css({ color: "blue", background: "red" });
			$("#mobile_val").val('1');
			return false;	
		}
		
		$("#mobileerror").hide();
		$("#mobileok").hide();
		$.ajax({
			type:'GET',
			url:'reg.php',
			data:'do=chk_mobile&mobile='+mobile,
			beforeSend: function (){
			
				$('#mobileerror').hide();
				$('#mobileok').hide();
				$('#mobilemove').show();
			},
			complete: function (){
				$('#mobilemove').hide();
			},
			success: function(data){
				switch(data)
				{
					case '1':
						
						$('#mobileerror').show();
						$("#mobile").css({ color: "blue", background: "red" });
						$("#mobile_val").val('1');
						break;
					case '0':
						
						$('#mobileok').show();
						$("#mobile").css({ color: "black", background: "white" });
						$("#mobile_val").val('0');
						break;
				}
   		    }
	});
}

function changeImg(){   
    var imgSrc = $("#imgObj");   
    var src = imgSrc.attr("src");
    imgSrc.attr("src",src+"?i="+Math.round());   
} 

function checklogin(){
	if($('#username').val()==''){
	alert('用户名不能为空！');
	return false;
	}
	if($('#password').val()==''){
	alert('密码不能为空！');
	return false;
	}
	$('#password').val(md5($('#password').val()));
	var tmp_utl = document.referrer;
	if(tmp_utl!="" && $('#index').val()==""){
		$('#index').val(tmp_utl);
	}
}
