﻿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 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 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);
		});
	}
	//即时校验注册函数
	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;
	});
	ajaxValidateInit(email,/^[^@\s]+@[^@\.\s]+(\.[^@\.\s]+)+$/i);
	submit1.onclick=function(e){	
		chkList.push(function(){
			if(!accept.checked){
				$alert(accept,"Please check to indicate you agree to the terms and conditions.");
				return false;
			}
		})
		chkList.push(function(){submit1.form.submit();});
		return chkList.each();
	};

});