var xmlhttp = false;
try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e2) {
            xmlhttp = false;
        }
    }
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        xmlhttp = new XMLHttpRequest();
   }

function checkuser(obj)
{
   var username=trim(obj.value);
   if(username.length<5){
	   document.getElementById("chkname").innerHTML="<img src='/images/w.gif'/> <font color=red>用户名必须大于5个字节</font>";
	   }else
	{
   xmlhttp.open("GET","http://www.51tr.com/reg.php?username="+username);
        xmlhttp.send(null);
        xmlhttp.onreadystatechange=function(){
            if (4==xmlhttp.readyState){
                if (200==xmlhttp.status){
                 document.getElementById("chkname").innerHTML=xmlhttp.responseText;
				}
            }
    }
	}
}
function checkpass(obj)
{
	if(trim(obj.value).length<6)
	{
		document.getElementById("chkpass").innerHTML="&nbsp;<img src='/images/w.gif'/> <font color=red>密码必须6个字节以上</font>";
		}
	else{
	document.getElementById("chkpass").innerHTML="&nbsp;<img src='/images/r.gif'/> 密码填写正确";
	}
	}
function checkpass2(obj)
{
	if(obj.value!=document.theForm.password.value)
	{
		document.getElementById("chkpass2").innerHTML="&nbsp;<img src='/images/w.gif'/> <font color=red>两次密码输入不一至</font>";
		}
	else if(trim(obj.value).length<6)
	{
	document.getElementById("chkpass2").innerHTML="&nbsp;<img src='/images/w.gif'/> <font color=red>密码必须6个字节以上</font>";	
		}
	else{
	document.getElementById("chkpass2").innerHTML="&nbsp;<img src='/images/r.gif'/> 密码填写正确";
	}
	}
function checkemail(obj)
{
	var i=obj.value.indexOf("@");
	var j=obj.value.indexOf(".");
	if((i<0)||(j<0)||obj.value.length<3)
	  {
	  document.getElementById("mailstr").innerHTML="&nbsp;<img src='/images/w.gif'/> <font color=red>邮箱输入有误</font>";
      }else {
	  document.getElementById("mailstr").innerHTML="&nbsp;<img src='/images/r.gif'/> 邮箱输入正确";
	  }
	}
function checkcontactor(obj)
{
	var actor=trim(obj.value);
	if(actor.length<2)
	{
	document.getElementById("chactor").innerHTML="&nbsp;<img src='/images/w.gif'/> <font color=red>真实姓名输入有误</font>";	
	}
	else
	{
	document.getElementById("chactor").innerHTML="&nbsp;<img src='/images/r.gif'/> 真实姓名输入正确";	
		}
	
}
function ckcompany(obj)
{
	if(trim(obj.value).length<3)
	{
	document.getElementById("cpstr").innerHTML="<img src='/images/w.gif'/> <font color=red>公司名称输入有误</font>";		
		}
	else
	{
	document.getElementById("cpstr").innerHTML="<img src='/images/r.gif'/> 公司名称输入正确";	
		}
	}
function cktel(obj){
    if(trim(obj.value).length<3)
	{
	document.getElementById("telstr").innerHTML="<img src='/images/w.gif'/> <font color=red>联系电话输入有误</font>";
		}
	else
	{
	document.getElementById("telstr").innerHTML="<img src='/images/r.gif'/> 联系电话输入正确";	
		}
}
function checkform(){
	if(document.theForm.Province.value==""||document.theForm.City.value==""){
	alert("请正确选择所在省市");
	document.theForm.Province.focus();
	return false;
	}
	if(document.theForm.username.value.length<5||document.theForm.username.value.length>12){
	alert("用户名必须在5至12位之间");
	document.theForm.username.select();		
	return false;
	}
	if(document.theForm.password.value.length<6)
	{alert("密码必须大于6位");document.theForm.password.select();return false;}
	if(document.theForm.password2.value!=document.theForm.password.value)
	{alert("两次密码输入不一至");document.theForm.password2.select();return false;}
	if(trim(document.theForm.contactor.value).length<2)
	{alert("真实姓名输入错误");document.theForm.contactor.select();return false;}
	if(trim(document.theForm.comany_namec.value).length<3)
	{alert("公司名称输入错误");document.theForm.comany_namec.select();return false;}
	if(trim(document.theForm.telephone.value).length<3)
	{alert("联系电话输入错误");document.theForm.telephone.select();return false;}
	var i=document.theForm.email.value.indexOf("@");
	var j=document.theForm.email.value.indexOf(".");
	if((i<0)||(j<0))
	{alert(i + j + "邮箱输入错误");document.theForm.email.select();return false;}
	var validcode=document.getElementById("validcode").value;
	var username=document.theForm.username.value;
	var email=document.getElementById("email").value;
	xmlhttp.open("GET","http://www.51tr.com/checkcode.php?validcode="+validcode+"&username="+username+"&email="+email);
    xmlhttp.send(null);
        xmlhttp.onreadystatechange=function(){
            if (4==xmlhttp.readyState){
                if (200==xmlhttp.status){
				 if(xmlhttp.responseText=="validcode")
				 {
				  alert("验证码输入错误");
				  return false;
				 }
				 else if(xmlhttp.responseText=="username")
				 {
					 alert("用户名已存在"); 
					 return false;
				 }
				 else if(xmlhttp.responseText=="email")
				 {
					 alert("邮箱已存在"); 
					return false;
				}
				else
				 {
				document.theForm.submit(); 
				}
				}
            }
    }
	
}

function trim(s){
return s.replace(/[ ]/g,"");
}

