$(
  	function( )
	{
		$("#uname input").blur(function( ){checkUserName("#uname");clearBright("#uname")})
		$("#uname input").focus(function( ){descBright("#uname")})
		$("#pwd input").blur(function( ){checkPassword("#pwd");clearBright("#pwd")})
		$("#pwd input").focus(function( ){descBright("#pwd")})
		$("#pwd2 input").blur(function( ){checkTowContent("#pwd","#pwd2");clearBright("#pwd2")})
		$("#pwd2 input").focus(function( ){descBright("#pwd2")})
		$("#em input").blur(function( ){checkEmail("#em");clearBright("#em")})
		$("#em input").focus(function( ){descBright("#em")})
		$("#em2 input").blur(function( ){checkTowContent("#em","#em2");clearBright("#em2")})
		$("#em2 input").focus(function( ){descBright("#em2")})
		$("#uname input").change(function( ){$("#uname .error2").html("")})
		$("#pwd input").change(function( ){$("#pwd .error2").html("")})
		$("#pwd2 input").change(function( ){$("#pwd2 .error2").html("")})
		$("#em input").change(function( ){$("#em .error2").html("")})
		$("#em2 input").change(function( ){$("#em2 .error2").html("")})
		$("#submit").click( function() {valid(); return false})
	}
)

submitOn = true; /*判断是否能发送注册信息的全局变量

/**********************************/
/*****   是否为合法的用户名   *****/
/*****用于客户开户时用户名的校验 *****/
/**********************************/

function checkUserName( userName )
	{	
		var input = $(userName + " input").val( );
		var error = userName + " .error2";
		var len = input.length;
		String.prototype.length2 = function() {
		var cArr = this.match(/[^\x00-\xff]/ig);
		return this.length + (cArr == null ? 0 : cArr.length);
		}
		$(error).css({ color: "#CC0000" });
		submitOn = false;
				
		if ( len == 0 )
			{
				$(error).html("用户名不能为空")
			}
		else if ( len < 2 )
			{
				$(error).html("你输入的用户名过短!")
			}
		else if ( len > 20 )
			{
				$(error).html("你输入的用户名过长")
			}
		else if (input.indexOf(" ") >= 0 )
			{
				$(error).html("用户名不能含有空格")
			}	
		else if(input.length-input.length2()!=0) 
			{
				$(error).html("不能输入中文")
			}
		else if(input.replace(/[a-zA-Z_0-9]/g,'')!='') 
			{
				$(error).html("不能包含“"+input.replace(/[a-zA-Z_0-9]/g,'')+"”")
			}
		
		else
			{
				$.get("/cbsite/globals/users/checkusername/",
						{u:$("#uname input").val()},
						function(data)
						{
							if (data == 0)
								{
									$(error).html("你输入的用户名可以使用")
									$(error).css({ color: "#336600" });
									submitOn = true;
								}
							else
								{
									$(error).html("你输入的用户名已被使用")
								}
						}
					)
			}
	}

/**********************************/
/*****   是否为合法的用户密码  *****/
/*****用于客户开户时密码的校验  *****/
/**********************************/
function checkPassword( pwd )
	{	
		var input = $(pwd + " input").val( );
		var error = pwd + " .error2";
		var len = input.length;
		$(error).css({ color: "#CC0000" });
		submitOn = false;
		
		if ( len == 0 )
			{
				$(error).html("密码不能为空")
			}
		else if ( len < 6 )
			{
				$(error).html("请输入六位以上密码")
			}
		else if ( len > 20 )
			{
				$(error).html("请输入二十位以下密码")
			}
		else if (input.indexOf(" ") >= 0 )
			{
				$(error).html("密码不能含有空格")
			}
		else
			{
			submitOn = true;
		  }
	}

/*****************************************/
/*****   判断两次输入的内容是否一致  *****/
/****************************************/
function checkTowContent(id1,id2)
	{	
		if ( $(id2 + " input").val( ) != $(id1 + " input").val( ) )
			{
				$(id2 + " .error2").css({ color: "#CC0000" });
				$(id2 + " .error2").html("两次输入不一致，请重新输入");
				submitOn = false;
			}
	}

/**********************************/
/*****   是否为有效的Email    *****/
/**********************************/
function checkEmail( em )
{
	var input = $(em + " input").val( );
	var error = em + " .error2";
	var len = input.length;
    $(error).css({ color: "#CC0000" });
	fals = 0;
	submitOn = false;

    //没有@或者@在首尾
	if((input.indexOf('@') == -1)||(input.indexOf('@') == 0)||(input.indexOf('@') == len-1))
        fals = 1; 
	if(input.indexOf('@') != input.lastIndexOf('@'))   
        fals = 1;
	if(input.indexOf('.') == -1)
        fals = 1;
    if (input.lastIndexOf('.') == (len-1) )//'.'在最后一位
        fals = 1;
    if (input.indexOf(" ") >= 0 )
		fals = 1;  
    for(var j=0;j<len-1;j++)
    {//判断是否有".."或者"@."
    if( (input.charAt(j) == '.' && input.charAt(j+1) == '.') ||( input.charAt(j) == '@' && input.charAt(j+1) == '.' ) )
        fals = 1;
    }

    validstr = "1234567890abcdefghijklmnopqrstuvwxyz_-.~@$#";
    lowerstr = input.toLowerCase();
    for(i=0;i<len;i++)
    {
       if(validstr.indexOf(lowerstr.charAt(i)) == -1){
           fals = 1;
       }
       //检测是否为"qq.com"的邮箱
       if ( input.charAt(i) == '@' ) {
          var domain="",k=i+1;
          while( k < len ){
              domain+=lowerstr.charAt(k++);
          }  
          if( domain=="qq.com" ){
              fals=2;
          }
       }
    }          
    if ( fals == 1 )
    {
        $(error).html("请输入有效的邮箱地址")
    }
    else if ( fals == 2 ){
        $(error).html("请不要使用qq.com后缀的邮箱")
    }
    else
    {
        submitOn = true;
    }
}


/**********************************/
/*****   突出显示提示文字  *****/
/*****用于客户输入信息时的提醒  *****/
/**********************************/
function descBright( descID )
	{	
		$(descID + " .error2").html("");
		$(descID + " .desc").css({ color: "#990000", background: "#FFF2E9" ,border: "1px #FF6600 solid"});
	}
	
/**********************************/
/*****   取消突出显示提示文字  *****/
/**********************************/
function clearBright( descID )
	{	
		$(descID + " .desc").css({ color: "", background: "" ,border: ""});
	}



function valid( )
{
	checkUserName("#uname");
	checkPassword("#pwd");
	checkTowContent("#pwd","#pwd2");
	checkEmail("#em");
	checkTowContent("#em","#em2");
	
	if (submitOn == true)
	{
		form1.submit( );
	}
    else
		alert("请把所有资料填写完整");
}

