;
/**
 * @author caojj
 * @description 快速注册
 * 用在官网各个注册模块
 */
__Register_Config=window.__Register_Config||null;	//验证配置 账号、密码重输密码必须要有

var WN_Register = (function() {
	var cache = {};
	
	var accAjax = new $.scriptLoader("http://register1.woniu.com/ws/register.do",{
		"method":"validatePassport",
		"jsoncallback":"WN_Register.accBack"		
	}).extra({
		$onSuccessed : function(para){
//			debugger;
            try{
                WN_OFFCIAL_ANALYSIS.countAccVerifyBack();
            }catch(e){}

			if (para) {
				this.result = para;			
			}
			
			para = para || this.result;
			var acc = getCache("account");
			
			if (para["result"].toUpperCase()=="Y") {
				acc.pass = true;
				acc.check();
			} else {
				var ret = "该账号已被其他用户使用，请重新选择";			
								
				acc.pass = ret;
				acc.check();
			}

		}
	});

	var _defTip = document.createElement("div");
	
	function getCache(nm) {
		return cache[nm];
	}
	
	return {
		get : getCache,
		getAccAjax : function() {
			return accAjax;
		},
		accBack : function(dt) {
			accAjax.$onSuccessed(dt);
		},
//		getNickAjax : function() {
//			return nickAjax;
//		},
		regFun : function(hand, fun) {
			if (window.attachEvent) {
				hand.attachEvent("on"+hand, fun);
			} else {
				hand.addEventListener(hand, fun, false);
			}
		},
		submit : function() {
			//提交按钮禁用
			$.log("pass all");
		},
		Item : function(_key, _ipt, _tip) {
			this.childs = [];
			
			this.key = _key;
			this.ipt = typeof(_ipt) == "string" ? $(_ipt) : _ipt;
			
			this.tip = (_ipt && typeof(_tip) == "string") ? $(_tip) : (_tip || _defTip);
			if (this.tip) {
				this.tip.def = WN_Register.Tips[_key] || "......"; //this.tip.innerHTML; //默认提示
			}			
			
			this.pass = undefined; //undefined 未检测过； true 通过； false(错误信息)； null 检测中...
			cache[_key] = this;
			
			if (this.ipt) {
				this.bindEvent();
			}
			
			if (!_key) {
				this.$onFailed = this.$onCheck = function() {		//保证有未通过的验证时，重新开始验证	//顶层元素要保证每次验证后将pass复位
					this.pass = undefined;
				};
				this.isTop = true;		//顶层
			}
		}	
	}	
})();

WN_Register.Item.prototype = {
	extra : function(o) {
		for(var i in o) {
			this[i] = o[i];
		}
		return this;
	},
	add : function(_item) {
		if (_item instanceof(WN_Register.Item)) {
			this.childs.push(_item);
		}
		return this;
	},
	get : function(nm) {
		nm = nm||"ipt";
		return this.ipt;
	},
	syncVerify : function() {
		var childs = this.childs;
		for(var i in childs) {
			var ret = childs[i].check();
			if (ret!==true) {				
//				this.$onFailed(ret);	//ret中带有错误提示ret.des
				return undefined;
			}
		}
		
//		this.$onSuccessed();
		
		return ret||true;
//		return {index:i, ret : ret};
	},
	check : function() {
//		var ret = this.pass||this.$verify();	//ret == undefined 用于异步验证 并且优先用于 pass 标示
//debugger
		var ret ;
		if(this.pass===undefined) {
			ret = this.$verify();
		} else {
			ret = this.pass;
		}
		
		this.pass = ret;	//typeof ret=="string" ? false : ret;		//true|false(错误信息)|undefined|null
		this.pass === true ? this.$onSuccessed() : (this.pass===false||typeof this.pass=="string" ? this.$onFailed(ret) : this.$onCheck() );
		return ret;
	},
	/**
	 * 待复写的验证函数
	 */
	$verify : function(_callBack) {
//		if (!this.isTop) {
			throw new Error("未实现");
//		}
	},
	$onSuccessed : function() {
//		this.tip.innerHTML = this.ipt.value.length?"&nbsp;":"";	//"√";	//非必填项不要出√
//		FT.show(this.tip.def, this.ipt);
		this.tip.className = "reg_chkMsg reg_chkMsg_Suc";
		if(this.isTop) {
			this.pass = undefined;
		}
		$.log(this.ipt.id,this.pass);
		if(this.$cbSucc) {
			this.$cbSucc();
		}
	},
	$onFailed : function(_r) {		
//		this.tip.innerHTML = _r||"x";
		var ipt = this.ipt;
//		if (ipt.value.length) {
			if (WN_Register._tmr) {
				clearTimeout(WN_Register._tmr);
				WN_Register._tmr = null;
			};
			WN_Register._tmr = setTimeout(function(){
				FT.show(_r, ipt);
				ipt.select();
			}, 200);
//		}
		this.tip.className = "reg_chkMsg reg_chkMsg_Fai";
		this.doBlink();
//		var me = this;
		
//		this.ipt.select();
		$.log(this.ipt.id,this.pass);
		if(this.$cbFail) {
			this.$cbFail();
		}
	},
	$onCheck : function() {
		this.pass = null;	//on checking
//		this.tip.innerHTML = "检测中...";	//"√";
		this.tip.className = "commonRegTcTs";
		$.log(this.ipt.id,this.pass);
		if(this.$cbCheck) {
			this.$cbCheck();
		}
	},
	$tip : function(str) {
		if (this.tip) {
			this.tip.style.display = "";
		}
//		var _str = this.tip.def || str;
//		this.tip.innerHTML = _str;
//		
//		this.tip.className = "commonRegTcTs";
//		this.tip.blurTip = this.pass;	
//		if (this.pass===undefined) {		//如果尚未验证过
		
//		if (typeof this.pass != "string") { //防止验证失败的提示被覆盖
			var _str = "";
			if (typeof this.pass=="string") {
				_str = this.pass;
			} else {
				_str = (this.tip&&this.tip.def) || str;
			}
			var ipt = this.ipt;
			setTimeout(function(){
				FT.show(_str, ipt);
			}, 200);
			
//		}
//			this.tip.innerHTML = _str;			
//		} else {
//			
//		}	
	},
	/**
	 * 绑定提示事件
	 */
	bindEvent : function() {
		var me = this;
		if(!this.ipt) return;
//		this.ipt.verify = this.$verify;
		this.ipt.onfocus = function(){
			me.$tip();
		}; /*this.___focusTip;*/
		var tag = this.ipt.type.toLowerCase();
		if(tag=="checkbox"||tag=="radio") {
			this.ipt.onclick = function(){
				me.pass = undefined;
				me.check();//??
			};
		} else {
			this.ipt.onchange = function(){
				me.pass = undefined;
				me.check();
			};
		}  /*this.___changeVerify;*/
		
		if(this.blurFun) {
			this.ipt.onblur = this.blurFun.bind(this);
		}
		
		if (this.tip) {
			this.tip.style.display = "none";
		}
		
		return this;
	},
	/**
	 * 输入容器闪烁
	 */
	doBlink : function() {
//		WN_Register.gameOptTips(this.ipt);
	}
};



$.extra({
	init : function() {
		this.initForm();
		
		this.loadVerifyImg();
	},
	getParas : function() {
//		debugger
//		var basic = $.$("#ipt{account password spreadName verify}").vals();
//		var extra = $.$("#ipt{email nickName realName identity}").vals();
//		var sex = $("ipt.sex_1").checked ? "1" : "0";
//		var all = $.extra(basic, extra, true);
//		all.sex = sex;
//		return basic;
	},
	initForm : function() {	//main
		this.ctrls = {};
		
			var account = 	new WN_Register.Item("account", "ipt.account", "tip.account").extra({
				$verify : function( ) {
					var val = this.ipt.value;
					var flag = Reg_Verify.account(val);
					if (flag == true) {
                        try{
                            WN_OFFCIAL_ANALYSIS.countAccVerify();
                        }catch(e){}

						WN_Register.getAccAjax().ask({
							account : val,
							userName : val
						});
					}else{
						return flag;
					}						
				}
			});
			this.ctrls.account = account;	
		
		/********/
			var password = new WN_Register.Item("password", "ipt.password", "tip.password").extra({
				$verify : function( ) {
					var rv = $("ipt.rpwd").value;
					var v = this.ipt.value;
					if(rv.length && v!=rv) {	//如果确认密码填写后 修改了密码，验证之前的确认密码
						WN_Register.get("rpwd").pass=undefined;
						WN_Register.get("rpwd").check();
					}
					return Reg_Verify.password(v,$("ipt.account").value,rv);
				},
				blurFun : function() {
					if (this.pass == true) {
						this.pass = undefined;
						this.check();
					}
				}
			}).bindEvent();
			this.ctrls.password = password;
		
		
		
			var rpwd = new WN_Register.Item("rpwd", "ipt.rpwd", "tip.rpwd").extra({
				$verify : function( ) {
					return Reg_Verify.rpwd(this.ipt.value, $("ipt.password").value);
				},
				$cbFail : function() {
					this.pass = undefined;
				},
				blurFun : function() {
					if (this.pass == true) {
						this.pass = undefined;
						this.check();
					}
				}
			}).bindEvent();
			this.ctrls.rpwd = rpwd;
			/*
			var verify = new WN_Register.Item("verify", "ipt.verify", "tip.verify").extra({
				$verify : function() {					
					return Reg_Verify.verify(this.ipt.value);					
				},
				$cbSucc : function() {	//验证码不提示√
					this.tip.className = "reg_chkMsg";
				}
			});
			this.ctrls.verify = verify;
			
			var email = new WN_Register.Item("email", "ipt.email", "tip.email").extra({
				$verify : function() {					
					return Reg_Verify.email(this.ipt.value);					
				},
				$cbSucc : function() {
					this.tip.className = "reg_chkMsg";
				}
			});
			this.ctrls.email = email;
		
			var agree = new WN_Register.Item("agree", "ipt.agree", "tip.agree").extra({
				$verify : function() {					
					return this.ipt.checked ? true : "请选择接受协议";
				}
			});
			this.ctrls.agree = agree;
			*/
		
		var form = new WN_Register.Item().extra({
			$verify : function() {
				return this.syncVerify();
			},
			$onSuccessed : function() {
				$.log("pass all");
				return true;
//				var postVars = WN_Register.getParas();
//				var extraVars = WN_Register.getExtraParas();
//				$.extra(extraVars, postVars);
//				WN_Register.getRegAjax().asyncAsk(postVars);
				this.pass = undefined;	//复位标示*imp
			}
		});
		this.ctrls.form = form;

		if(!__Register_Config) {
			form.add(	//账号
				account
			).add(	//密码
				password
			).add(	//确认密码
				rpwd
			);//.add( // email
				//email
			//);	
		} else {			
			for(var i in __Register_Config) {
				var cfg = this.ctrls[__Register_Config[i]];
				form.add(cfg);
			}
		}
		
	},
	
	loadVerifyImg : function() {		
		var verifyImg = $("reg.verifyImg");
		if(!verifyImg) {return}
		if(!WN_Register.verifySrc) {
			WN_Register.verifySrc = verifyImg.src;
		}
		
		if(!verifyImg.onclick){
			var verifyLink = $("reg.verifyLink");
			verifyImg.onclick = function() {
				var _baseSrc = WN_Register.verifySrc || 'http://act.woniu.com/reg/php/checkpic.php';
				var src = _baseSrc + "?t=" + new Date().getTime();
				setTimeout(function(){
					verifyImg.src = src;
				},50);
//				this.style.backgroundImage = 'url(0)'.replace('0',src);				
			};
			verifyLink.onclick = function() {
				verifyImg.onclick();
			}
		};
		
		verifyImg .onclick();
	},
	check : function() {
		var _ret = this.ctrls.form.check();
        try{
            WN_OFFCIAL_ANALYSIS.countRegister();
        }catch(e){}

        return _ret || false;
	}

},WN_Register);


WN_Register.Tips = {
	"account" : "4-16位英文字母、数字及下划线",	
	"password" : "6-30位英文字母、数字，区分大小写",
	"rpwd" : "请输入确认密码"
	//"email" : "取回密码的重要途径"
};


Reg_Verify = {
	trim : function(s) {
		return (s||"").replace(/^\s+|\s+$/,"");
	},
	account : function(v, tp) {
		v = this.trim(v);
		tp = this.trim(tp);
		var ret;
		
		//通用验证方法
		if(!v){
			return "请输入账号"
		}
		
		if(v.length<6){
			return "账号不能少于6个字符"
		}
		
		//common账号验证方法(之前还区分邮箱账号和手机账号)
		
		if (/^\d+$/.test(v)) {
			return "账号不能全为数字"
		}
		
		if (v.length > 25) {
			return "账号不能多于25个字符"
		}
		
		if (/^woniu/i.test(v)) {
			return "账号不能以'woniu'开头"
		}
		
		if (/^snail/i.test(v)) {
			return "账号不能以'snail'开头"
		}
		
		if (/^gm/i.test(v)) {
			return "账号不能以'gm'开头"
		}
		
		if (/^d1xn/i.test(v)) {
			return "账号不能以'd1xn'开头"
		}
		
		if (/^dixn/i.test(v)) {
			return "账号不能以'dixn'开头"
		}
		
		if (/^dlxn/i.test(v)) {
			return "账号不能以'dlxn'开头"
		}
		
		if (/^dyxn/i.test(v)) {
			return "账号不能以'dyxn'开头"
		}
		
		if (! (/^\w{6,25}$/.test(v) || /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(v)) ) {
			return "账号只能是英文、数字、下划线的组合（不能为全角字符）或有效电子邮箱"
		}
		
		return true;
			
	},
	
	password:function(v,acc,rv){
//		v = this.trim(v);
		acc = this.trim(acc);
		if(!v){
			return "请输入密码"
		}
		
		if(v==acc){
			return "账号和密码不能相同"
		}
		
		if(v.length<6){
			return "密码至少为6位"
		}
		
		if(v.length>20){
			return "密码不能多于20个字符"
		}
		
		if(!/^[0-9a-zA-Z]{6,20}$/.test(v)){
			return "密码只能使用英文（区分大小写）和数字"
		}
		
		if(/^(\w)\1*$/.test(v)){
			return "密码不能是完全相同的字母或数字"
		}
		
		for(var i=1,c=[v.charCodeAt(0)],cd=[v.charCodeAt(0)],cu=[v.charCodeAt(0)];i<v.length;i+=1){
			cd[i] = cd[i-1]-1;
			cu[i] = cu[i-1]+1;
			c[i] = v.charCodeAt(i);
		}
		c = c.join(":");
		if(c==cd.join(":") || c==cu.join(":")){
			return "你不能使用该密码，非常容易被猜测！"
		}
		
//		if(rv.length && v!=rv) {
//			return '';
//		}

		return true;
		
	},
	rpwd : function(a, b) {
		if(!a) {
			return "请输入确认密码";
		}
		
		if (a!=b) {
			return '两次密码输入不一致';
		}
		
		return true;
	}
	/*
	verify : function(v) {
		if(!v){
			return "请输入验证码";						
		}
		
		if(!/^[0-9a-zA-Z]{4}$/.test(v)){
			return "验证码格式不正确";						
		}
		
		return true;
	},
	email:function(v){
		v = this.trim(v);
		if(!v) {
			return true
		}
		
		if(!v){
			return "请输入您的电子邮箱";
		}
		
		if(!/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(v) || v.length<6 || v.length>25){
			return "邮箱格式不正确，请确认后重新输入";			
		}
		
		return true;		
	},
	mobile : function(v) {
		v = this.trim(v);
		if(!v){	//可不填			
			return true;
		} else if(!/^(13[0-9]|15[0|3|6|7|8|9]|18[8|9])\d{8}$/.test(v) || v.length<6 || v.length>25){
			return "您的手机号格式不正确，请重新填写";			
		}
		
		return true;
	}
	*/
};


$.extra({
	FloatTip : function(offset) {
		this.tipDom = $.CNs(["div",{className:"reg_chkMsgLay", style:{display:"none"}},[
			["p", {className:"reg_descIcon"}],
			["div",{ref:"Ctt",innerHTML:"...", className:"reg_chkDesc"}]
		]]);
		document.body.appendChild(this.tipDom);
		
		this.diff = offset || {x:0,y:0};
		
		try {
			document.body.attachEvent("onclick", function(){
				FT.hide();
			})
		} catch(e) {
			document.body.addEventListener("click", function(){
				FT.hide();
			},false)
		}
	}
}, WN_Register);

WN_Register.FloatTip.prototype = {
	show : function(str, dom) {
		//get pos
		var pos = $(dom, "getCoord");
		this.tipDom.refs.Ctt.innerHTML = str;
		this.tipDom.style.left = pos.left + this.diff.x + "px";
		this.tipDom.style.top = pos.top + this.diff.y + "px";
		this.tipDom.style.display = "";
	},
	hide : function() {
		this.tipDom.style.display = "none";
	}
};

!function(){
	FT = new WN_Register.FloatTip({x:-68,y:26});
	var sid = $.getQuery("sid") || $.getQuery("Sid");
	if (sid) {
		$("Reg.sid").value = sid || 0;
	}
	/*
	var uc = $.getQuery("uc") || $.getQuery("Uc");
	if (uc) {
		$("Reg.uc").value = uc || 0;
	}*/
	var subsid = $.getQuery("subsid") || $.getQuery("subSid");
	if (subsid) {
		$("Reg.subsid").value = subsid || 0;
	}
	var fromUrl = $.getQuery("fromurl") || $.getQuery("fromUrl");
	if (fromUrl) {
		$("Reg.fromUrl").value = fromUrl+'.'+sid;
	}else{
		$("Reg.fromUrl").value = $("Reg.pagename").value+'.'+sid;
	}
	WN_Register.init();
	
	var counter = new Image();
//	var games = document.domain.split(".");	//取域名
	var game = $("Reg.game").value; //games.length>3 ? games[1] : game;
	var pagename = $("Reg.pagename").value;
	var reffer = encodeURIComponent(document.referrer||"");
	counter.src = "/framework/php/cps.fangwen.php?sid="+sid+"&game="+game+"&pagename="+pagename+"&fromurl=" + fromUrl + "&reffer="+reffer;
	
	try {
		if(WN_Register.get("account").ipt.getAttribute("noFocus")) {
			setTimeout(function(){
				WN_Register.get("account").ipt.focus();
			},500);	
		}
		
	}catch(e){}
}();

//http://act.woniu.com/reg/php/fangwen.php?game=tz&pagename=reg2010.2.3&fromurl=client.1.1

