
function setDomain(value){
	$("#domain").attr("value",$.trim(value));
}

function setUsername(value){
	$("#username").attr("value",$.trim(value));
}

/**
 * 检查用户名
 * @return
 */
function chkUsername() {
	var username = $.trim($("#inp_uname").val());
	if(username=="") {
		return 0;
	}
	else if( /^\d.*$/.test( username ) ){
		//用户名不能以数字开头
		return -1;
	}
	else if(fLen( username )<6 || fLen( username )>18 ){
		//合法长度为6-18个字符
		return -2;
	}
	else if(CommonAjax.CkeckUserName( username ).value){
		//判断是否用户名已存在
		return -3;
	}
	return 1;
}
/**
 * 检查邮箱
 * @return
 */
function chkEmail(){
    var email = $.trim($("#email").val())
    if(email==""){
        return 0;
    }
    else if(!/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/.test( email ))
    {
        return -1;
    }
    return 1;
}

$(document).ready(function(){
	/** ----------- 用户名输入框事件 ----------- */
	$("#inp_uname").bind("focus", function(){
		var ret=chkUsername();
		$("#inp_uname").attr("class","inp ipt-focus");
		if(ret==0){
			//用户名输入框为空,显示规则
			$("#inp_uname").attr("class","inp ipt-normal");
			$("#div_uname_rule").show();
			if($("#password_ico_err").is(":visible")){
				$("#div_password_err").hide();
			}
		}
		return false;
	});
	
	
	$("#inp_uname").bind("blur", function(){
		
		var ret=chkUsername();
		if(ret>0) {
			if($("#inp_uname").val()==$("#tmp").val()) return false;
			$("#inp_uname").attr("class","inp ipt-normal");
			$("#div_uname_rule").hide();
			$("#uname_ico_err").hide();
			//$("#uname_ico_ok").hide();
			$("#div_uname_err").hide();
		}
		else if(ret==0){
			//用户名输入框为空,显示规则
			$("#tmp").attr("value","");
			$("#inp_uname").attr("class","inp ipt-normal");
			$("#div_uname_rule").hide();
			$("#div_uname_err").hide();
			$("#uname_ico_err").hide();
			//$("#uname_ico_ok").hide();
			$("#tr_chk_username_result").hide();
			//$("#div_uname_err_info").html("");
		}
		else {
			$("#tmp").attr("value","");
			$("#tr_chk_username_result").hide();
			//更改用户名标签样式
			$("#inp_uname").attr("class","inp ipt-error");
			//显示错误提示图标
			$("#uname_ico_err").show();
			//隐藏正常提示内容div
			$("#div_uname_rule").hide();
			//打开用户名检查错误div
			$("#div_uname_err").show();
			if(ret == -1){
			//显示具体的错误内容
			$("#div_uname_err_info").html("用户名不能以数字开头");
		}
		else if(ret == -2){
			$("#div_uname_err_info").html("合法长度为6-18个字符");
		}
		else if(ret == -3){
			$("#div_uname_err_info").html("用户名已被占用");
		}
		}
		
		if($("#password_ico_err").is(":visible")){
			$("#div_password_err").show();
		}
		return false;
	});
	
	$("#inp_uname").bind("keydown", function(event){
		//event = fGetEvent();
		if (event.keyCode == 13) { 
			if(event.preventDefault) {    
		        // Firefox    
				event.preventDefault();    
				event.stopPropagation();    
		     } else {    
		        // IE    
		    	 event.cancelBubble=true;    
		    	 event.returnValue = false;    
		     }  
			$("#inp_uname").blur();
		}
		return true;
	});
	/** --------- end ------------ */
	
	/** ----------- 密码输入框事件 ----------- */
	$("#password").bind("focus", function(){
		//check account radio
		if($("input[name='rdAccount'][checked]").val()==undefined){
			$("#div_chose_uname").attr("class","chose-list notice");
		}
		
		ret = chkPassword();
		$("#password").attr("class","inp ipt-focus");
		if(ret==0){
			if($("#password_ico_err").is(":visible")){
				$("#div_password_err").hide();
				$("#password_ico_err").hide();
			}
			$("#div_password_rule").show();
			//恢复重复输入密码状态
			$("#div_passwordconfirm_err").hide();
			$("#passwordconfirm").attr("class","inp ipt-normal");
			$("#passwordconfirm").attr("value","");
			$("#passwordconfirm_ico_ok").hide();
			$("#passwordconfirm_ico_err").hide();
		}
		else if(ret>0) {
			chkPasswordStrong($("#password").val());
		}
		return false;
	});
	$("#password").bind("blur", function(){
		ret = chkPassword();
		if(ret>0){
			$("#password").attr("class","inp ipt-normal");
			$("#password_ico_ok").show();
			$("#password_ico_err").hide();
			$("#div_password_rule").hide();
			$("#div_password_err").hide();
			$("#div_password_err_info").html("");
		}
		else {
			if(ret==0){
			$("#password").attr("class","inp ipt-normal");
			$("#password_ico_ok").hide();
			$("#password_ico_err").hide();
			$("#div_password_rule").hide();
			$("#div_password_err").hide();
			$("#div_password_err_info").html("");
		}
		else if(ret==-1){
			$("#password").attr("class","inp ipt-error");
			$("#password_ico_ok").hide();
			$("#password_ico_err").show();
			$("#div_password_rule").hide();
			$("#div_password_err").show();
			$("#div_password_err_info").html("请输入6～16位字符的密码");
			
		}
		}
		return false;
	});
	$("#password").bind("keyup", function(){
		$("#passwordconfirm").attr("value","");
		//检查密码强度
		chkPasswordStrong($("#password").val());
		return false;
	});
	
	$("#passwordconfirm").bind("blur",function(){
		$("#passwordconfirm").attr("class","inp ipt-normal");
		return chkPasswordconfirm();
	}
	);
	/** --------- end ------------ */
	
	/** ----------- 邮箱验证事件 ----------- */
	$("#email").bind("focus", function(){
	    var ret=chkEmail();
	    $("#inp_email").attr("class","inp ipt-focus");
	    if(ret==0){
	    //邮箱输入框为空，显示规则
	    $("#inp_email").attr("class","inp ipt-normal");
	    $("#div_email_rule").show();
	    }
	    return false;
	}
	)
	
	$("#email").bind("blur", function(){
		
		var ret=chkEmail();
		if(ret>0) {
			if($("#email").val()==$("#tmp").val()) return false;
			$("#email").attr("class","inp ipt-normal");
			$("#div_email_rule").hide();
			$("#email_ico_err").hide();
			$("#div_email_err").hide();
		}
		else if(ret==0){
			//用户名输入框为空,显示规则
			$("#tmp").attr("value","");
			$("#email").attr("class","inp ipt-normal");
			$("#div_email_rule").hide();
			$("#div_email_err").hide();
			$("#email_ico_err").hide();
			$("#tr_chk_email_result").hide();
		}
		else {
			$("#tmp").attr("value","");
			$("#tr_chk_email_result").hide();
			//更改用户名标签样式
			$("#email").attr("class","inp ipt-error");
			//显示错误提示图标
			$("#email_ico_err").show();
			//隐藏正常提示内容div
			$("#div_email_rule").hide();
			//打开用户名检查错误div
			$("#div_email_err").show();
			if(ret == -1){
			//显示具体的错误内容
			$("#div_email_err_info").html("输入的电子邮箱格式不正确");
		}
		}
		return false;
	});
	/** --------- end ------------ */
	
	/** --------- 验证码 ------------ */
	$("#authcode").bind("blur",function(){
		ret=chkAuthcode();
		if(ret==0){
			//$("#authcode_ico_ok").hide();
			$("#authcode_ico_err").show();
			$("#div_authcode_err").show();
			$("#div_authcode_err_info").html("验证码不能为空");
		}
		else {
			//$("#authcode_ico_ok").show();
			$("#authcode_ico_err").hide();
			$("#div_authcode_err").hide();
		}
	});
	
	/** --------- end ------------ */
	
	/** --------- 绑定radio事件 ------------ */
	$("input[name='rdAccount']").bind("click", function(){
		if($(this).attr("disable")=='true'){
			return true;
		}
		var val=$.trim($(this).val());
		if(val=='') return true;

		array_tmp=val.split("@");
		setUsername(array_tmp[0]);
		setDomain("@"+array_tmp[1]);
		$('#st_domain').html("@"+array_tmp[1]);
		$('#st_userAtDomain').html(array_tmp[0]+"<span class='Cblue'>@"+array_tmp[1]+"</span>");
		$('#tr_chk_username_result').hide();
		$('#tr_input_username').hide();
		$('#tr_show_username').show();
		$('#div_chose_uname').attr('class','chose-list');
		//$('#uname_ico_ok').hide();
		return true;
		}); 
	/** --------- end ------------ */
	}); 

$(window).load(function(){bodyOnLoad();return false; });

function chkPassword(){
	password= $("#password").val();
	if(password == "") return 0;
	var len;
	var i;
	var isPassword = true;
	len = 0;
	for (i=0;i<password.length;i++){
		if (password.charCodeAt(i)>255) isPassword = false;
	}
	if(!isPassword || password.length > 16 || password.length < 6)
		return -1;
	
	return 1;
}

/**
 * 显示密码强弱
 * @return
 */
function chkPasswordStrong(me) {
	//恢复重复输入密码状态
	$("#div_passwordconfirm_err").hide();
	$("#passwordconfirm").attr("class","inp ipt-normal");
	$("#passwordconfirm_ico_ok").hide();
	$("#passwordconfirm_ico_err").hide();
	//-----
	$("#password_ico_ok").hide();
	$("#password_ico_err").hide();
	
	$("#div_password_err").hide();
	$("#div_password_err_info").html("");
	
	$("#password").attr("class","inp ipt-normal");
	//打开密码提示内容div
	$("#div_password_rule").show();
	var csint = checkStrong(me);
	$("#div_passowrd_Strong").attr("class","bar state"+csint);
}


function CharMode(iN){ 
	if (iN>=48 && iN <=57) //数字 
	return 1; 
	if (iN>=65 && iN <=90) //大写字母 
	return 2; 
	if (iN>=97 && iN <=122) //小写 
	return 4; 
	else 
	return 8; //特殊字符 
} 

function chkPasswordconfirm(){
	var password= $("#password").val();
	var passwordconfirm = $("#passwordconfirm").val();
	
	if(password != passwordconfirm){
		$("#div_passwordconfirm_err").show();
		$("#passwordconfirm").attr("class","inp ipt-error");
		$("#passwordconfirm_ico_ok").hide();
		$("#passwordconfirm_ico_err").show();
		return false;
	}
	else if(passwordconfirm==''){
		$("#div_passwordconfirm_err").hide();
		$("#passwordconfirm").attr("class","inp ipt-normal");
		$("#passwordconfirm_ico_ok").hide();
		$("#passwordconfirm_ico_err").hide();
	}
	else {
		$("#div_passwordconfirm_err").hide();
		$("#passwordconfirm").attr("class","inp ipt-normal");
		
		$("#passwordconfirm_ico_err").hide();
		if($("#password_ico_err").is(":visible")){
			$("#passwordconfirm_ico_ok").hide();
		}
		else $("#passwordconfirm_ico_ok").show();
	}
	return true;
}

//bitTotal函数 
//计算出当前密码当中一共有多少种模式 
function bitTotal(num){
	modes=0; 
	for (i=0;i<4;i++){ 
		if (num & 1) modes++; 
		num>>>=1; 
	} 
	return modes; 
} 

//checkStrong函数 
//返回密码的强度级别 
function checkStrong(sPW){
	Modes=0; 
	for (i=0;i<sPW.length;i++){ 
		//测试每一个字符的类别并统计一共有多少种模式. 
		Modes|=CharMode(sPW.charCodeAt(i)); 
	} 
	return bitTotal(Modes);
}
/**
 * 获取事件
 * @param e
 * @return
 */
function fGetEvent (e) {
	var ev = e || window.event;
	
	if (!ev) {
		var aCaller = [];
		var c = fGetEvent.caller;
		while (c) {
			ev = c.arguments[0];
			if (ev && Event == ev.constructor) {
				break;
			}
			
			var b = false;
			for(var i=0;i<aCaller.length;i++){
				if(c == aCaller[i]){
					b = true;
					break;
				}
			}
			if(b){
				break;
			}else{
				aCaller.push(c);
			}
			c = c.caller;
		}
	}

	return ev;
}


//计算字符数，一个中文2个字符
function fLen(Obj){
  var nCNLenth = 0;
  var nLenth = Obj.length;
  for (var i=0; i<nLenth; i++){
    if(Obj.charCodeAt(i)>255){
      nCNLenth += 2; 
    }else{
      nCNLenth++;
    }
  }
  return nCNLenth;
}

function checkNumber( num ,max){
    if( /^[0-9]+$/.test( num ) && num <=max && num > 0)
		return true;
	else return false;
}

function chkAuthcode(){
	authcode=$.trim($("#authcode").val());
	if(authcode=="") return 0;
	return 1;
}

function doRegFormSubmit(){
	if($("#servItems").attr("checked") != true){
		alert("您还没有阅读服务条款!");
		return false;
	}
	ok = true;
	
	ret = chkUsername();
	if(ret<1) {
		ok = false;
		$("#tr_chk_username_result").hide();
		//更改用户名标签样式
		$("#inp_uname").attr("class","inp ipt-error");
		//显示错误提示图标
		$("#uname_ico_err").show();
		//隐藏正常提示内容div
		$("#div_uname_rule").hide();
		//打开用户名检查错误div
		$("#div_uname_err").show();
		if(ret == 0){
			$("#div_uname_err_info").html("用户名不能为空");
		}
		else if(ret == -1){
			//显示具体的错误内容
			$("#div_uname_err_info").html("用户名不能以数字开头");
		}
		else if(ret == -2){
			$("#div_uname_err_info").html("合法长度为6-18个字符");
		}
		else if(ret == -3){
			$("#div_uname_err_info").html("用户名已被占用");
		}
	}

	ret = chkPassword();
	if(ret<1){
		ok=false;
		$("#password").attr("class","inp ipt-error");
		$("#password_ico_ok").hide();
		$("#password_ico_err").show();
		$("#div_password_rule").hide();
		$("#div_password_err").show();
		$("#div_password_err_info").html("请输入6～16位字符的密码");
	}
	else {
		if(!chkPasswordconfirm()){
			ok=false;
			$("#div_passwordconfirm_err").show();
			$("#passwordconfirm").attr("class","inp ipt-error");
			$("#passwordconfirm_ico_ok").hide();
			$("#passwordconfirm_ico_err").show();
		}
		else if($.trim($("#password").val())==$.trim($("#username").val()) 
				|| $.trim($("#password").val())==($.trim($("#username").val())+$.trim($("#domain").val()))){
			//检查用户名与密码是否相同
			ok=false;
			$("#password").attr("class","inp ipt-error");
			$("#password_ico_ok").hide();
			$("#password_ico_err").show();
			$("#div_password_rule").hide();
			$("#div_password_err").show();
			$("#div_password_err_info").html("输入的密码不能与用户名一样");
		}
	}
	/** 检查邮箱 */
	ret=chkEmail();
	if(ret<1){
	    ok=false;
	    $("#tr_chk_email_result").hide();
		//更改用户名标签样式
		$("#inp_email").attr("class","inp ipt-error");
		//显示错误提示图标
		$("#email_ico_err").show();
		//隐藏正常提示内容div
		$("#div_email_rule").hide();
		//打开用户名检查错误div
		$("#div_email_err").show();
		if(ret == 0){
			$("#div_email_err_info").html("电子邮箱地址不能为空");
		}
		else if(ret == -1){
			//显示具体的错误内容
			$("#div_email_err_info").html("输入的电子邮箱格式不正确");
		}
	}
		
	/** 检查验证码 */
		ret=chkAuthcode();
		if(ret<1){
			ok=false;
			$("#authcode").attr("class","inp ipt-error");
			//$("#authcode_ico_ok").hide();
			$("#authcode_ico_err").show();
			$("#div_authcode_err").show();
			$("#div_authcode_err_info").html("验证码不能为空");
		}
		
		return ok;
}
//获取事件
function fGetEvent (e) {
	var ev = e || window.event;
	
	if (!ev) {
		var aCaller = [];
		var c = fGetEvent.caller;
		while (c) {
			ev = c.arguments[0];
			if (ev && Event == ev.constructor) {
				break;
			}
			
			var b = false;
			for(var i=0;i<aCaller.length;i++){
				if(c == aCaller[i]){
					b = true;
					break;
				}
			}
			if(b){
				break;
			}else{
				aCaller.push(c);
			}
			c = c.caller;
		}
	}

	return ev;
}

function bodyOnLoad(){
	document.getElementById('inp_uname').focus();
	//$(":text").each({"inp_uname"},function(){$(this).attr("value","");});
	$("#inp_uname").attr("value","");
	$("#authcode").attr("value","");
	$("#cusproblem").attr("value","");
	$("#secanswer").attr("value","");
	$("#username").attr("value","");
	$("#birthday").attr("value","");
	$("#tmp").attr("value","");
	$("input[name='rdAccount']").each(function(){$(this).attr("value","");$(this).attr("checked",false);});
}
//打开服务条款
function openServiceItems(){
	url="serviceitems.htm";
	window.open(url,'regconfirm','height=620,width=850,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no');
	return true;
}
