/*======================================================================*\
|| #################################################################### ||
|| # 51ps.cn v1.0 - main programer: Micah Riven                       # ||
|| # ---------------------------------------------------------------- # ||
|| #  Copyright ?000?004 ChinaE3.com Dev. Team. All Rights Reserved.  # ||
|| #                                                                  # ||
|| # ---------------------- WEBSITE URLADDRESS ---------------------- # ||
|| # http://www.chinae3.com | http://www.ad184.com | http://ad184.net # ||
|| #################################################################### ||
\*======================================================================*/

var tmpHtml1 = "";
var tmpHtml2 = "";
var mloop=1;

// 检测 Email规范性
function checkemail(email)
{
	var pattern = /^([^@])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/; 
    flag = pattern.test(email); 

	if(flag) { 
		return true; 
	} else { 
		return false; 
	}
}

function validateUsername(str){
    if(str=="")
	{
	   return false;	
	}
	patn = /^[a-zA-Z][0-9a-zA-Z_]{3,19}$/; 
	if(patn.test(str)){
		 return true;
	}
	return false; 
}

function checkByteLength(str,minlen,maxlen) {
	if (str == null) return false;
	var l = str.length;
	var blen = 0;
	for(i=0; i<l; i++) {
		if ((str.charCodeAt(i) & 0xff00) != 0) {
			blen ++;
		}
		blen ++;
	}
	if (blen > maxlen || blen < minlen) {
		return false;
	}
	return true;
}

// 检测填写内容是否完整
function validate()
{
	var div_error = document.getElementById("errlayer");
	
	if(!validateUsername(document.getElementById("username").value)) {
		div_error.style.display="block";
		div_error.innerHTML=tmpHtml1 + "用户名为4-20个字符, 允许的格式有: taoxi, tao_xi, taoxi1982。一旦注册成功会员名不能修改" + tmpHtml2;
		document.getElementById("username").focus();
		return false;
	}
	
	if(document.getElementById("password").value.length == 0)
	{
		div_error.style.display="block";
		div_error.innerHTML="请输入密码!";
		document.getElementById("password").focus();
		return false;		
	}
	
	if(document.getElementById("password").value=='' || !checkByteLength(document.getElementById("password").value,6,16)){
		div_error.style.display="block";
		div_error.innerHTML=tmpHtml1 + "密码由6-16个英文字母、数字或符号组成" + tmpHtml2;
		document.getElementById("password").focus();
		return false;
	}
	
	if(document.getElementById("password").value!=document.getElementById("passwordcmp").value){
		div_error.style.display="block";
		div_error.innerHTML=tmpHtml1 + "两次输入的密码不一致，请重新输入" + tmpHtml2;
		document.getElementById("passwordcmp").focus();
		return false;
	}
	
	if(document.getElementById("email").value == '') {
		div_error.style.display="block";
		div_error.innerHTML=tmpHtml1 + "请务必输入真实可用的Email，密码将由系统发送到此邮箱内" + tmpHtml2;
		document.getElementById("email").focus();
		return false;
	}
	if(!checkemail(document.getElementById("email").value)) {
		div_error.style.display="block";
		div_error.innerHTML=tmpHtml1 + "Email地址不符合规范!" + tmpHtml2;
		document.getElementById("email").focus();
		return false;
	}
	if(document.getByElementById("authcode").value.length != 4) {
		div_error.style.display="block";
		div_error.innerHTML=tmpHtml1 + "请完整填写验证码!" + tmpHtml2;
		document.getByElementById("authcode").focus();
		return false;
	}
	return true;	
	/*
	if(regform.email.value != regform.cmpemail.value) {
		document.all.errlayer.innerHTML=tmpHtml1 + "Email地址与确认Email不匹配，请确认" + tmpHtml2;
		document.regform.email.focus();
		mloop=setTimeout("motion_obj('errtable',1,10,0)", 3000);
		return false;
	}
	if(regform.question.value.length==0 || regform.question.value.length>20) {
		div_error.innerHTML=tmpHtml1 + "取回密码提示问题不能为空且不能超过20个字符" + tmpHtml2;
		document.regform.question.focus();
		mloop=setTimeout("motion_obj('errtable',1,10,0)", 3000);
		return false;
	}
	if(regform.answer.value.length==0 || regform.answer.value.length>20) {
		document.all.errlayer.innerHTML=tmpHtml1 + "取回密码问题答案不能为空且不能超过20个字符" + tmpHtml2;
		document.regform.answer.focus();
		mloop=setTimeout("motion_obj('errtable',1,10,0)", 3000);
		return false;
	}
	*/
//	document.regform.submit();
}
function check_first_pwd()
{	
	var div_error=document.getElementById("passwordmemo");
	if(document.getElementById("password").value=='' || !checkByteLength(document.getElementById("password").value,6,16))
	{
		div_error.style.display="block";
		div_error.innerHTML=tmpHtml1 + "<font color=red>密码由6-16个英文字母、数字或符号组成</font>" + tmpHtml2;
		return false;
	}
	div_error.innerHTML="√";
}
function check_pwd()
{
	var div_error=document.getElementById("passwordmemo2");
	if(document.getElementById("passwordcmp").value == "")
	{
		div_error.style.display="block";
		div_error.innerHTML= "<font color=red>密码不能为空</font>" ;

		return false;
	}
   if(document.getElementById("password").value!=document.getElementById("passwordcmp").value){
		div_error.style.display="block";
		div_error.innerHTML= "<font color=red>两次输入的密码不一致，请重新输入</font>" ;
	
		return false;
	}
	div_error.innerHTML="√";
}

function check_username()
{	
	txtUserName = document.getElementById("username");
	var msg="";
	var div = document.getElementById('check_user_name');
	var username = document.getElementById('username');	

	if(!validateUsername(txtUserName.value)) {
		msg="4至32个字符(包括字母、数字、下划线)，且必须以英文字母开始。如example2007"; 
		div.style.color = "#FF0000";
		//username.focus();
		//username.select();				
	}
	else
	{
		/*var result = AJAX.SubmitUseGet("register_check.php","action=un&chkcont="+txtUserName.value,"");
	 
		if(result == "true")
		{
			msg="恭喜，这个用户名可以使用"; 
			div.style.color = "#009900";		
		}
		else
		{
			msg="很遗憾，这个用户名已经被注册了，再选个更好的吧";		
			div.style.color = "#FF0000";
			//username.focus();
			//username.select();		
		}*/
		div.style.display = "block";
	 $("#check_user_name").html("验证中，请稍候 ");  
        $.ajax
		  (
			{
				type    : "GET",
				url     : "register_check.php",
				data    : "action=un&chkcont="+txtUserName.value,
				success : function(result)
							{
				
								if(result == "true")
									{
										msg="恭喜，这个用户名可以使用"; 
										div.style.display = "block";
										div.innerHTML = msg;
										div.style.color = "#009900";
									}
									else
									{
										msg="很遗憾，这个用户名已经被注册了，再选个更好的吧"; 
										div.style.display = "block";
										div.innerHTML = msg;
										div.style.color = "#FF0000";
								
									}
								
							}
				
			}
		
		)




	
	}
	
	div.style.display = "block";
	div.innerHTML = msg;	
}

function check_email()
{
	var div = document.getElementById('check_user_email');
	var email = document.getElementById('email');	
	var msg="";
	if(!checkemail(email.value))
	{
		msg="邮件地址的格式不正确"; 
		div.style.color = "#FF0000";

	}
	else
	{
		//var result = AJAX.SubmitUseGet("register_check.php","action=em&chkcont="+email.value,"");
		div.style.display = "block";
  $("#check_user_email").html("验证中，请稍候 ");  
		$.ajax
		(
			{
				type    : "GET",
				url     : "register_check.php",
				data    : "action=em&chkcont="+email.value,
				success : function(result)
							{
								if(result == "true")
									{
										msg="恭喜，您可以使用"+document.getElementById("email").value+"邮箱"; 
									    div.style.display = "block";
										div.innerHTML = msg;
										div.style.color = "#009900";
									}
									else
									{
										msg=document.getElementById("email").value+" 已经被使用"; 
										div.style.display = "block";
										div.innerHTML = msg;
										div.style.color = "#FF0000";
		
									}
								
							}
				
			}
		
		)
	

		
	/*	if(result == "true")
		{
			msg="恭喜，您可以使用"+document.getElementById("email").value+"邮箱"; 
			div.style.color = "#009900";
		}
		else
		{
			msg=document.getElementById("email").value+" 已经被使用"; 
			div.style.color = "#FF0000";
			email.focus();
			email.select();
		}*/	
	 
	}
	
	div.style.display = "block";
	div.innerHTML = msg;	
}
