﻿window.$r("domready",function(){
	var conIndex = 0;
	var chkList=[];
	var radioList = [
		'signIn',
		'regist',
		'guest'
	];
	var tabState = $("tabHidden").value;

	var userName = $("engUserName");
	var password = $("engPassword");
	var password2 = $("engPassword2");
	var email = $("reEmail");
	var checkCode = $("reCaptcha");
	var accept = $("reAccept");
	var submit1 = $("reSubmit1");

	var classList={
		"true":"pubmencenterinfo_tips",
		"false":"pubmencenterinfo_tips_wrong",
		"load":"pubmencenterinfo_tips"
	};
	function ajaxValidateInit(obj,re,isAllowNull){
		var chkObj = getSibling(obj);
		var chkTip = getSibling(chkObj);
		var notice;
		if (!chkObj)
			return;
		obj.passValue={};
		obj.msgExist = {
			"engUserName":"This username is already taken. Please try another.",
			"reEmail":"This email address is already taken. Please try another."
		}[obj.id];
		obj.msg={
			"engUserName":"Letters, numbers, underscore only. First character must be a letter.",
			"reEmail":"Enter a valid email address.."
		}[obj.id];
		obj.showClass=function(flag,isSubmit,isAjax,msg){
			if (isSubmit){
				if (flag){
					if (isAjax)
						submit1.click();
				}else
					$alert(obj,notice||obj.msg);
			}
			chkObj.className=flag=="null"?"":classList[flag];
			chkObj.innerHTML=flag===true?"":(flag=="load"?"Checking availability":msg||obj.msg);
			chkObj.style.display=flag==true?"none":"";
			chkTip.style.display=flag==true?"":"none";
			notice = msg||obj.msg;
			return flag;
		};
		obj.check=function(isSubmit){
			
			var value=obj.value;
			if (!value)
			{
				return obj.showClass(isAllowNull?"null":false,isSubmit);
			}
			if (re&&!re.test(value))
			{
				return obj.showClass(false,isSubmit);
			}
			if (value in obj.passValue){
				return obj.showClass(obj.passValue[value],isSubmit,null,notice==obj.msg?obj.msg:obj.msgExist)===true?true:false;
			}
			else{
				obj.passValue[value]="load";
				$ajax(obj.getAttribute("ajaxUrl"),obj.name+"="+escape(value),function(txt){
					obj.showClass(obj.passValue[value]=txt.indexOf("check succeed")>-1,isSubmit,true,obj.msgExist);
					
				});
				obj.showClass("load",isSubmit);
				return false;
			}
		};
		obj.$r("blur",function(){
			obj.check();
		});
		chkList.push(function(){
			return obj.check(true);
		});
	}

	//091109 增加即时校验验证码
	function ajaxChkKey(obj,re){
		var chkImg = $(obj.name+"_img");
		var chkText = $(obj.name+"_text");
		var timeout;
		var flag = false;
		obj.msg = {
			"refresh" : $s2t("Unclear? Click again!"),
			"false" : $s2t("Enter verification code."),
			"load" : $s2t("checking..."),
			"true" : "&nbsp;"
		};
		obj.classList1 = {
			"true":"pubmencenterinfo_correct",
			"false":"pubmencenterinfo_tips_wrong",
			"load":"pubmencenterinfo_tips",
			"refresh" : "pubmencenterinfo_tips"
		}

		obj.$r('focus' , function(){
			obj.chclass("refresh");
			obj.$r('blur' , obj.check);
		});
		
		obj.chclass = function(type,isSubmit){
			obj.flag = type=="true"?true:false;
			chkImg.style.display = (type=="true"||type=="load")?"none":"";
			chkText.className =obj.classList1[type];
			chkText.innerHTML = obj.msg[type];
			if(type=="refresh") obj.value = "";
			if(isSubmit===true){
				if(type=="true")
					submit1.click();
				if(type=="false")
					$alert(obj,obj.msg["false"]);
			}
		};

		obj.check = function(isSubmit){
			if(re.test(obj.value)){
				if (isSubmit===true&&obj.flag)
					return true;
				obj.chclass("load");
				$ajax(obj.getAttribute("ajaxUrl"), 'checkCode=' + obj.value, function(txt){
					if(txt && txt.indexOf("check succeed") >= 0)
						obj.chclass("true" , isSubmit);
					else
						obj.chclass("false" , isSubmit);
				});
			}
			else
				obj.chclass("false" , isSubmit);
			obj.$ur('blur' , obj.check);
			return false;
		};

		chkImg.$r('mousedown' , function(e){
			this.src='Safecode.asp?'+Math.random();
			obj.chclass("refresh");
			obj.$ur('blur' , obj.check);
		});

		chkList.push(function(){
			return obj.check(true);
		});
	}

	//即时校验注册函数
	function validateInit(obj,re,func){
		var clock;
		var chkObj= getSibling(obj);
		var chkTip = getSibling(chkObj);
		if (!chkObj)
			return;
		obj.lastValue="";
		obj.msg = chkObj.innerHTML;
		obj.check=function(chkFlag){
			var value=obj.value;
			if (chkFlag!==true&&value==obj.lastValue)
				return;
			obj.lastValue=value;
			var flag=re?re.test(value):true;
			if (func)
				flag=flag&&func(value);
			chkObj.className=classList[flag];
			chkObj.style.display=flag?"none":"";
			chkTip.style.display=flag?"":"none";
			return flag;
		};
		obj.$r("focus",function(){
			clock=setInterval(obj.check,100);
		});
		obj.$r("blur",function(value){
			clearInterval(clock);
			obj.check();
		});
		chkList.push(function(){
			var flag=obj.check(true);
			if (!flag)
				$alert(obj,obj.msg);
			return flag;
		});
	}
	
	function getSibling(el){
		do
		{
			el = el.nextSibling;
		}
		while (el.nodeType != 1);
		return el;
	}





	if(tabState){
		changeTab(tabState);
		for(var i = 0; i<radioList.length;i++){
			$(radioList[i]).checked = false;
		}
		conIndex = tabState;
		$(radioList[parseInt(tabState)]).checked = true;
	}
	(function(){
		for(var n = 0; n<radioList.length;n++){
			(function(t){
				$(radioList[t]).onclick = function(){
					if(t == conIndex)	return;
					for(var i = 0; i<radioList.length;i++){
						$(radioList[i]).checked = false;
					}
					$(radioList[parseInt(t)]).checked = true;
					changeTab(t);
					conIndex = t;
					$("tabHidden").value = t;
				};
			})(n);
			
		}
	})();
	function changeTab(no){
		var el = $(radioList[no]);
		for(var j = 0; j<radioList.length; j++){
			var ele = $(radioList[j]+"Text");
			var div = $(radioList[j]);
			do
			{
				div = div.parentNode;
			}
			while (div.tagName != "DIV");
			div.className="pubmencenterinfo_title02";
			ele.style.display="none";
		}
		$(radioList[no]+"Text").style.display="";
		do
		{
			el = el.parentNode;
		}
		while (el.tagName != "DIV");
		el.className = "pubmencenterinfo_title01";
	}

	ajaxValidateInit(userName,/^[a-z][a-z0-9_]*$/i);

	validateInit(password,/^.{4,}$/,function(){
		return password2.check(true)||true;
	});
	validateInit(password2,/^.{4,}$/,function(value){
		return password.value==value;
	});
	if (checkCode)
		ajaxChkKey(checkCode,/^[a-zA-Z0-9]{6}$/);
	ajaxValidateInit(email,/^[^@\s]+@[^@\.\s]+(\.[^@\.\s]+)+$/i);

	chkList.push(function(){
		if(!accept.checked){
			$alert(accept,"Please check to indicate you agree to the terms and conditions.");
			return false;
		}
	});

	submit1.form.onsubmit=function(){
		return false;
	};

	submit1.onclick=function(e){
		if (chkList.each()){
			submit1.form.submit();
		}
	};

	// Show Ads
	if(_enRegAds && _enRegAds[0]){
		var adContainer = $("reg_ads");
		var imgs = _enRegAds;
		for(i=0,l=imgs.length;i<l;i++){
			img = imgs[i].split("|");
			adContainer.innerHTML+="<div class=\"priad_pic\"><a href=\""+img[0]+"\"><img alt=\""+img[2]+"\" src=\""+img[1]+"\"/></a>";
		}
	}
	// Show Ads [END]
	
});
