function $(id){return document.getElementById(id);}
function CheckForm(){
	this.form = null;
	this.msg = null;
	this.shopurl = null;
	this.init();
}
CheckForm.prototype.init = function(){
	this.form = document.form;
	//this.shopurl = document.getElementById("shopurl");
	this.msg = [
		document.getElementById("username-msg"),
		document.getElementById("p-msg"),
		document.getElementById("p1-msg"),
		document.getElementById("realname-msg"),
		document.getElementById("email-msg"),
		document.getElementById("checkcode-msg")
	];
	//this.shopurl.value = "请输入您的网店地址";
	//this.shopurl.style.color = "#999";
	this.form.username.onblur = Delegate.create(this,this.verifyUname);
	this.form.password.onblur = Delegate.create(this,this.verifyPassword);
	this.form.password1.onblur = Delegate.create(this,this.verifyPassword1);
	this.form.realname.onblur = Delegate.create(this,this.verifyRealname);
	this.form.email.onblur = Delegate.create(this,this.verifyEmail);
	this.form.checkcode.onblur = Delegate.create(this,this.verifyCheckcode);
	//this.form.status[0].onclick=this.form.status[1].onclick = Delegate.create(this,function(){this.shopurl.style.display="none"});
	//this.form.status[2].onclick = Delegate.create(this,function(){this.shopurl.style.display="block";});
	//this.shopurl.onfocus = function(){if(!this.changed){this.style.color="#333";this.value="";}};
	//this.shopurl.onblur = function(){if(this.value!=""){this.changed=true;}else{this.changed=false;this.style.color="#999";this.value="请输入您的网店地址";}};
	document.getElementById("btnsub").onclick = Delegate.create(this,this.submit);
	document.getElementById("btnsub2").onclick = Delegate.create(this,this.submit2);
};
CheckForm.prototype.verifyUname = function(){
	var o = this.form.username;
	if(o.value.gblen() < 4){
		this.msg[0].innerHTML = "英文用户名字数不能少于4个，中文用户名字数不能少于2个";
		o.valid = false;
		o.className = "warn";
		return;
	}
	if(o.value.match(/\+|\&|\(|\)|\@|\#|\$|\%|\^|\[|\]|\'|\{|\}/) != null){
		this.msg[0].innerHTML = "用户名中不可含有+,&,(),@,#,$,%,^,[],{}等符号";
		o.valid = false;
		return;
	}
	var ajax = new AJAXRequest;
	ajax.post(
			 "/index.php/member/reg/check",
			 'username='+o.value,
			 Delegate.create(this,this.verifyUname_return)
	);
};
CheckForm.prototype.verifyUname_return = function(resultObj){
	var r = unescape(resultObj.responseText);
	r = r.split("{$$}");
	var o = this.form.username;
	if(r[0] == "0") {
		o.valid = false;
		o.className = "warn";
	}else{
		o.valid = true;
		o.className = "text";
	}
	this.msg[0].innerHTML = r[1];
};
CheckForm.prototype.verifyCheckcode = function(){
	var o = this.form.checkcode;
	if(o.value.gblen() < 4){
		this.msg[5].innerHTML = "验证码不能少于4个";
		o.valid = false;
		o.className = "warn";
		return;
	}
	var ajax = new AJAXRequest;
	ajax.post(
			 "/index.php/member/reg/Checkcode",
			 'checkcode='+o.value,
			 Delegate.create(this,this.verifyCheckcode_return)
	);
};
CheckForm.prototype.verifyCheckcode_return = function(resultObj){
	var r = unescape(resultObj.responseText);
	r = r.split("{$$}");
	var o = this.form.checkcode;
	if(r[0] == "0") {
		o.valid = false;
		o.className = "warn";
	}else{
		o.valid = true;
		o.className = "text";
	}
	this.msg[5].innerHTML = r[1];
};
CheckForm.prototype.verifyPassword = function(){
	var o = this.form.password;
	if(o.value.length < 6){ 
		this.msg[1].innerHTML = ("密码字数不能少于6个字符");
		o.valid = false;
		o.className = "warn";
		return;
	}
	o.valid = true;
	o.className = "text";
	this.msg[1].innerHTML = ("密码有效");
};
CheckForm.prototype.verifyPassword1 = function(){
	var o = this.form.password1;
	if(this.form.password.valid!=true) return;
	if(o.value != this.form.password.value){ 
		this.msg[2].innerHTML = ("两次密码输入不相同");
		o.valid = false;
		o.className = "warn";
		return;
	}
	o.valid = true;
	o.className = "text";
	this.msg[2].innerHTML = ("密码有效");
};
CheckForm.prototype.verifyRealname = function(){
	var o = this.form.realname;
	if(o.value == ""){ 
		this.msg[3].innerHTML = ("请输入真实姓名");
		o.valid = false;
		o.className = "warn";
		return;
	}
	o.valid = true;
	o.className = "text";
	this.msg[3].innerHTML = ("真实姓名有效");
};
CheckForm.prototype.verifyEmail = function(){
	var o = this.form.email;
	if(o.value.match(/^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3}$/gi)==null){ 
		this.msg[4].innerHTML = ("请输入有效的邮箱地址");
		o.valid = false;
		o.className = "warn";
		return;
	}
	o.valid = true;
	o.className = "text";
	this.msg[4].innerHTML = ("邮箱有效");
};
CheckForm.prototype.submit = function(){
	if(this.form.username.valid == null){
		this.verifyUname();
		return;
	}
	if(this.form.checkcode.valid == null){
		this.verifyCheckcode();
		return;
	}
	if(this.form.password.valid == null){
		this.verifyPassword();
		return;
	}
	if(this.form.password1.valid == null){
		this.verifyPassword1();
		return;
	}
	if(this.form.realname.valid == null){
		this.verifyRealname();
		return;
	}
	if(this.form.email.valid == null){
		this.verifyEmail();
		return;
	}
	this.verifyPassword1();
	with(this.form){
		if(username.valid&&password.valid&&password1.valid&&realname.valid&&email.valid){
			//如果不是网店用户或者网址没有填写，就置空
			/*if(!this.shopurl.changed || !this.form.status[2].checked){
				this.shopurl.value = "";
			}*/
			if(this.form.status[1].checked == true){
				$('reg').style.display = 'none';
				$('regstep2').style.display = 'block';
			}
			else{
				submit();
			}
	
			
		}
	}
};

CheckForm.prototype.submit2 = function(){
	with(this.form){
	submit();
	}
};

//判断字符串数量，区分汉字和英文
String.prototype.gblen = function() {   
    var len = 0;   
    for (var i=0; i<this.length; i++) {   
        if (this.charCodeAt(i)>127) {   
            len += 2;   
        } else {   
            len ++;   
        }   
    }   
    return len;   
}  
