﻿$(document).ready(function() 
{
	$("#username").blur(function()
	{
		if($("#username").val() == "")
		{
			$("#c_username").html("<font color=red>用户名不能为空</font>");
			
			return false;
		}
		if($("#username").val().length < 3 || $("#username").val().length > 20)
		{
			$("#c_username").html("<font color=red>用户名的长度为3到20个字符</font>");
			
			return false;
		}
		username();
	});
	
	$("#password").blur(function()
	{
		if($("#password").val() == "")
		{
			$("#c_password").html("<font color=red>密码不能为空</font>");
		}
		else if($("#password").val().length < 6 || $("#password").val().length > 12)
		{
			$("#c_password").html("<font color=red>密码的长度范围在6到12字符之内</font>");
		}
		else if($("#password").val().length >= 6 && $("#password").val().length <= 12)
		{
			$("#c_password").html("<font color=blue>可以注册</font>");
		}
		
	});
	$("#repassword").blur(function()
	{
		if($("#repassword").val() == "")
		{
			$("#c_repassword").html("<font color=red>密码不能为空</font>");
		}
		else if($("#password").val() != $("#repassword").val())
		{
			$("#c_repassword").html("<font color=red>两次输入的密码不同</font>");
		}
		else
		{
			$("#c_repassword").html("<font color=blue>可以注册</font>");
		}
	
	});

	$("#email").blur(function()
	{
		if($("#email").val() == "")
		{
			$("#c_email").html("<font color=red>邮箱不能为空</font>");
			
			return false;
		}
		if(!$("#email").val().match("^([a-zA-Z0-9]+[_|\-|\.]?)*@([a-zA-Z0-9]+[_|\-|\.]?)*\.[a-zA-Z]{2,3}$"))
		{
			$("#c_email").html("<font color=red>邮箱填写方式错误</font>");
			
			return false;
		}
		email();
	});

	$("#checkcode").blur(function()
	{
		if($("#checkcode").val() == "")
		{
			$("#c_checkcode").html("<font color=red>验证码不能为空</font>");
			
			return false;
		}
		checkcode();

	})
})

function username()
{
	var username = $("#username").val();
	$.ajax
	({ 
	   type:"post",
	   url :"member.php?act=ajax&type=username",
	   dataType:"json",
	   data:"username="+username, 
	   success: function(json)
	   {
		   if(json == 0)
		   {
				$("#c_username").html("<font color=red>用户名已被注册</font>");
				$("#username").val("");
		   }
		   else if(json == 1)
		   {
				$("#c_username").html("<font color=blue>用户名可以注册</font>");
		   }
	   }   
   }); 
}

function email()
{
	var email = $("#email").val();
	$.ajax
	({ 
	   type:"post",
	   url :"member.php?act=ajax&type=email",
	   dataType:"json",
	   data:"email="+email, 
	   success: function(json)
	   {
		   if(json == 0)
		   {
				$("#c_email").html("<font color=red>邮箱已被注册</font>");
				$("#email").val("");
		   }
		   else if(json == 1)
		   {
				$("#c_email").html("<font color=blue>邮箱可以注册</font>");
		   }
	   }   
   }); 
}

function checkcode()
{
	var checkcode = $("#checkcode").val();
	$.ajax
	({
		type:"post",
		url :"member.php?act=ajax&type=checkcode",
		dataType:"json",
		data:"checkcode="+checkcode,
		success: function(json)
		{
			if(json == 1)
			{
				$("#c_checkcode").html("<font color=blue>输入正确</font>");
			}else{
				$("#c_checkcode").html("<font color=red>输入错误</font>");
				return false;
			}
		}
   });
}

function check_reg()
{
	var username = document.getElementById("username");
	var password = document.getElementById("password");
	var repassword = document.getElementById("repassword");
	var email = document.getElementById("email");
	var checkcode = document.getElementById("checkcode");

	if(username.value == "")
	{	
		$("#c_username").html("<font color=red>用户名不能为空</font>");
		username.focus();
		return false;
	}
	if(username.value.length < 3 || username.value.length > 20)
	{
		document.getElementById("c_username").innerHTML="<font color=red>用户名的长度为3到20个字符</font>";
		document.getElementById("username").focus();
		return false;
	}
	if(password.value == "")
	{
		password.focus();
		return false;
	}
	if(repassword.value == "")
	{
		repassword.focus();
		return false;
	}
	if(password.value != repassword.value)
	{
		repassword.focus();
		return false;
	}
	if(email.value == "")
	{
		email.focus();
		return false;
	}
	if(!email.value.match("^([a-zA-Z0-9]+[_|\-|\.]?)*@([a-zA-Z0-9]+[_|\-|\.]?)*\.[a-zA-Z]{2,3}$"))
	{
		document.getElementById("c_email").innerHTML="<font color=red>邮箱填写方式错误</font>";
		document.getElementById("email").focus();
		return false;
	}
	if(checkcode.value == "")
	{
		document.getElementById("c_checkcode").innerHTML="<font color=red>验证码填写错误</font>";
		checkcode.focus();
		return false;
	}
	checkcode();
}
