	
	function trimStr(text){
 	   return (text || "").replace( /^\s+|\s+$/g, "" );
	}
	
	function showTip(id){
		document.getElementById(id).style.display = "block";	
	}
	
	function hideTip(id){
		document.getElementById(id).style.display = "none";
	}
	
	function login(){
		document.getElementById("selhost").submit();	
	}
	
	function doreg(){
		
		if(!checkUsername()||isUsernameExist()||!checkPassword1()||!checkPassword2()||!checkEmail1()||isEmailExist()||!checkCode()||!checkPact()){
			return;	
		}
		
		document.getElementById("regForm").submit();
	}
	
	function checkCode(){
		
		var obj = document.getElementById("code");
		var code = trimStr(obj.value);
		if(code==""){
			showTip("codetip");	
			return false;
		}else{
			hideTip("codetip");	
			return true;
		}	
	}
	
	function checkUsername(){
		var obj = document.getElementById("username");
		var username = trimStr(obj.value);
		if(username==""){
			hideTip("usernametip1");
		}
		hideTip("usernametip2");	
		if( !username.match(/^[a-zA-Z][a-zA-Z0-9]{3,15}$/i) || username.length<4 || username.length>16){
	    showTip("usernametip1");
	    return false;
	  }else{
	  	hideTip("usernametip1");
	  }	  
	  	
	  return true;
	}
	
	function isUsernameExist(){
		
		if(!checkUsername()){
			return false;
		}
	
	  var flag = true;
		var obj = document.getElementById("username");
		var username = trimStr(obj.value);
	
		$.ajax({
			async:false,
			url:"/public/checkreg.jsp",
			data:"type=2&username="+username,
			timeout:1000,
			success:function(result){
	  		if(result == 1){
	  			showTip("usernametip2");
	  			flag = true;	
	  		}else{
	  			hideTip("usernametip2");
	  			flag = false;	
	  		}
	  	},
	  	error:function(){
	  			alert("网络繁忙，请稍后再试！");
	  			flag = true;
	  		}
			});
			
			return flag;
	}
	
	function checkPassword1(){
		
		var obj  = document.getElementById("password1");
		var password1 = trimStr(obj.value);
		if(password1.length<4 || password1.length>16){		
			document.getElementById("passwordtip1").style.display = "block";
			return false;
     }else{
			document.getElementById("passwordtip1").style.display = "none";
			return true;
			}
		}
	
	function checkPassword2(){
		
		if( $("#password1").val() != $("#password2").val()){
			
			document.getElementById("passwordtip2").style.display = "block";
			return false;
		}else{
			document.getElementById("passwordtip2").style.display = "none";	
			return true;
		}
	}
	
	function checkEmail1(){
		
		var obj = document.getElementById("email1");
		var email1 = trimStr(obj.value);
		if(email1==""){
			hideTip("emailtip1");
			hideTip("emailtip2");
			showTip("emailtip3");
			return false;	
		}
		hideTip("emailtip3");
		if(!email1.match(/^[A-Za-z0-9_]+@[A-Za-z0-9]+(.[A-Za-z0-9]+)+$/)){
			showTip("emailtip1");	
      return false;
     }else{
     	hideTip("emailtip1");
     	return true;	
     }     
	}
	
	function isEmailExist(){
		
		var flag = true;
		var obj = document.getElementById("email1");
		var email1 = trimStr(obj.value);
	
		$.ajax({
			async:false,
			url:"/public/checkreg.jsp",
			data:"type=1&email="+email1,
			timeout:1000,
			success:function(result){
		 	
	  		if(result == 1){
	  			showTip("emailtip2");
	  			flag = true;	
	  		}else{
	  				hideTip("emailtip2");
	  				flag = false;	
	  			}
	  	},
	  	error:function(){
	  			alert("网络繁忙，请稍后再试！");
	  			flag = true;
	  		}
			});
			
			return flag;
	}

	function checkPact(){
			
				if($("#pact").attr("checked")){
					return true;
				}else{
					alert('请选择是否同意服务条款！');
					return false;	
				}
		}
	
  
  