document.domain = "xunlei.com";
	
var g_strType = "simple";//simple ? or secure?	got from params, but its default value is simple
//begin================注册========================================================================
var g_objRegist = new ObjectInfo();
g_objRegist.Init = function()
{
	this._title = "基本信息";
	this._properties = [];
	this._AppendProperty("username","username","用户名","username",false,/^[a-zA-Z0-9_]{6,16}$/);//6-16个字符(字母、数字)，不分大小写
	this._AppendProperty("nickname","nickname","昵称","nickname",false);
	this._AppendProperty("sex","sex","性别","sex",false);
	this._AppendProperty("mailbox","mailbox","电子邮件","normal_mailbox",false,/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/);
	this._AppendProperty("password","password","密码","password",false);
	this._AppendProperty("confirmPassword","confirmPassword","确认密码","confirmPassword",false);
	this._AppendProperty("verifycode","verifycode","验证码","verifycode",false);
	this._AppendProperty("agree","agree","使用协议","agree",false);
	
	//this._save_url = "http://svr.game.xunlei.com/regandlogin.webGameLogin";
	this._save_url = "http://svr.game.xunlei.com/regandlogin.webGameLogin";
	this._btn_save_id = "btn_regist";
	this._msg_box_id = "div_user_msg_basic";//更新的div区域信息
	//this._default_succeeded_msg = "注册成功"; 

	this._is_2nd = false;
	this._username_existed = false;

	//重要
	this._allow_multiple_exception = true;
	
	for(var i=0;i<this._properties.length;i++)
	{
		var input_obj = $(this._properties[i].ctrl_id);
		input_obj.onfocus = function(e){ input_focus(this); }
		input_obj.onblur = function(e){ input_blur(this); }
		input_obj.onchange = function(e){ if(this.id&&this.id=="username"){checkUserName();} onValidChange(); }
	}
	
	try {g_strType = $P('type');} catch(ex){g_strType="simple";}
	if (g_strType=="secure")
	{
		$('pp_doingShow').style.display = "";
		
		//安全模式，则加入一些密码保护的属性			
		//=begin==================密码保护(此部分可能隐藏)=============================================
		this._AppendProperty("passwordQuestion1","pp_passwordQuestion1","密码提示问题1","passwordQuestion1",false);
		this._AppendProperty("passwordAnswer1","pp_passwordAnswer1","问题1的答案","passwordAnswer1",false);
		this._AppendProperty("passwordQuestion2","pp_passwordQuestion2","密码提示问题2","passwordQuestion2",false);
		this._AppendProperty("passwordAnswer2","pp_passwordAnswer2","问题2的答案","passwordAnswer2",false);
		this._AppendProperty("cardNo","pp_cardNo","证件号码","cardNo",false);
		this._AppendProperty("trueName","pp_trueName","真实姓名","trueName",false);
		this._AppendProperty("mailbox","pp_mailbox","安全邮箱","mailbox",false,/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/);
		//=end====================密码保护(此部分可能隐藏)=============================================
	}
	else
		$('pp_doingShow').style.display = "none";		
	
}//end Init
g_objRegist.OnLoad = function(rr)
{

}
g_objRegist.Validate = function()
{
	this._exceptions = [];
	this._Validate();//常规检验
	if(this._username_existed)
		this._exceptions.push({ctrl_id:'username',msg:'用户名已存在。'});
	
	//检查是否非纯数字
		var allnumber = /^\d*$/;
		var strUserName = $('username').value.trim();
		if ( strUserName.bytes()<6 ||  strUserName.bytes()>16 || allnumber.test(strUserName)){
		this._exceptions.push({ctrl_id:'username', msg:'6-16个字母数字组合（不可为纯数字），不分大小写'});
		}
	
	//检查昵称的长度
	if($('nickname').value.trim().bytes()>10)
	{
		$('nickname').value = $('nickname').value.truncate(10);//截10个字符
	}

	//检查密码与确认密码是否一致
	var strPassword = $('password').value.trim();
	var confirmPassword = $('confirmPassword').value.trim();
	//var isErr= false;
	if ( strPassword.bytes() < 6 ||strPassword.bytes()>16)
	{
		this._exceptions.push({ctrl_id:'password', msg:'密码长度小于6个或者大于16个字符'});
		//isErr = true;
	}
	else if (confirmPassword.bytes()<6 ||confirmPassword.bytes()>16)
	{
		this._exceptions.push({ctrl_id:'confirmPassword', msg:'确认密码长度小于6个或者大于16个字符'});
		//isErr = true;
	}
	else if ( strPassword!= confirmPassword )
	{
		$('password_msg').innerHTML = "密码与确认密码不一致";
		$('confirmPassword_msg').innerHTML = "确认密码与密码不一致";
		this._exceptions.push({ctrl_id:'password', msg:'密码与确认密码不一致'});
		this._exceptions.push({ctrl_id:'confirmPassword', msg:'确认密码与密码不一致'});
		//isErr = true;
	}
	else		//if ( !isErr )//密码格式没有问题
	{
		$('password').parentNode.className = "";
		$('confirmPassword').parentNode.className = "";
	}

	if(!this._is_2nd)
	{
		this._is_2nd = true;
	}
}

g_objRegist.Serialize = function()//序列化参数
{
	var str = "gameid=000019"; //这里不需要"&"
	str += this._Serialize();
	str += this._JoinParam("type",g_strType);
	return str;
}
g_objRegist.OnDoingMsg = function(msg)//正在处理中...
{
	showLoading(true);
	//WebUserMsg.Wait(this._msg_box_id,msg);
}
g_objRegist.OnSucceededMsg = function(msg)//save成功
{
	//注册成功,设置一些cookie信息
	showLoading(false);
	alert("注册成功");
	$('verifycodeImg').src = PageConfig.validPic+String(Date.parse(new Date()));

	UserLogin.setCookie(PageConfig.newUserCook,'',0);
	
	var ureged = $('username').value;
	showRegPnl(false);
	//alert(ureged);
	
	UserLogin.setCookie(PageConfig.cookieLastUN,ureged,PageConfig.cookieTimeOut);
	UserLogin.setCookie(UserLogin.cookieName,ureged,PageConfig.cookieTimeOut);
	PageConfig.loginUname = ureged;
	UserLogin.loginURL = PageConfig.loginURL;
	UserLogin.validURL = PageConfig.validURL;

	UserLogin.onBeforeValid = beforeValid;
	UserLogin.onAfterValid  = function(){afterValid();UserLogin.setCookie('_loginStatus_','1');initUserInfo(true);showUserInfoPanel(1);showServerList(true);var lkPnl = $('linksarea');if(lkPnl){lkPnl.style.display = 'none';}};

	UserLogin.validLoginCache('000019');

	PvCount((!_GET['gid']?'xunlei':_GET['gid']),(!_GET['cflag']?'000':_GET['cflag']),ureged);
	pvCount1(ureged);
	try{
		//$(PageConfig.usernameTxt).value = ureged;
	}catch(e){alert(e);}
	
	//window.location.href="http://shushan.xunlei.com/?advid="+$P("advid")+"&username="+$("username").value;//跳到下一个页面
}

g_objRegist.BackToNormal = function()
{
	for(var i=0;i<this._properties.length;i++)
	{
		$(this._properties[i].ctrl_id).parentNode.className='';
		//$(this._properties[i].ctrl_id+"_msg").innerHTML = '';
	}
}
g_objRegist.OnFailedMultiMsg = function(exceptions)
{
	for(var i=0;i<exceptions.length;i++)
	{
		$(exceptions[i].ctrl_id).parentNode.className='fail';
		if($(exceptions[i].ctrl_id+"_msg"))
			$(exceptions[i].ctrl_id+"_msg").innerHTML = exceptions[i].msg;
	}
	//$('verifycodeImg').src = PageConfig.validPic+String(Date.parse(new Date()));
	showLoading(false);
	showRegPnl(true,false);
}

g_objRegist.OnFailedMsg = function(msg)
{	
	showLoading(false);
	//	if (msg=="验证码错误")
	//		$(this._properties[i].ctrl_id).parentNode.className='error';
	alert(msg);
	//getVerifyCode();
	$('verifycodeImg').src = PageConfig.validPic+String(Date.parse(new Date()));
	showRegPnl(true,false);
}

//end================注册=============================================	
	
//begin================检查用户名是否存在===========================================		
checkUserName_callback = function(rr)
{
	if ( rr.result == -1 )
	{
		g_objRegist._exceptions.push({ctrl_id:'username', msg:'用户名已经存在'});
		$('username_msg').innerHTML = '用户名已经存在';
		g_objRegist.OnFailedMultiMsg(g_objRegist._exceptions);
		g_objRegist._username_existed = true;
		/*if (rr.msg=="用户名已经存在")
		{
			g_objRegist._exceptions.push({ctrl_id:'username', msg:'用户名已经存在'});
			$('username_msg').innerHTML = '用户名已经存在';
			g_objRegist.OnFailedMultiMsg(g_objRegist._exceptions);
			g_objRegist._username_existed = true;
		}
		else
			alert(rr.msg);*/
	}
	else if( 0 == rr.result)//用户名还没有存在
	{
		g_objRegist._username_existed = false;
		$('username').parentNode.className='';
	}
}

checkUserName = function()
{
	var allnumber = /^\d*$/;
	var strUserName = $('username').value.trim();
	if ( strUserName.bytes()<6 ||  strUserName.bytes()>16 || allnumber.test(strUserName))
	{
		g_objRegist._exceptions.push({ctrl_id:'username', msg:'6-16个字母数字组合（不可为纯数字），不分大小写'});
		g_objRegist.OnFailedMultiMsg(g_objRegist._exceptions);
		return;
	}
	var loadUrl = "http://svr.game.xunlei.com/checkuser.webGameLogin?gameid=000019&username=" + encodeURIComponent(strUserName);
	var act_loader = new BufferedLoader(loadUrl, checkUserName_callback);
	act_loader.load();
}
//end================检查用户名是否存在===========================================	

onValidChange = function()
{
	if(g_objRegist._is_2nd)
	{
		g_objRegist.BackToNormal();
		g_objRegist.Validate();
		g_objRegist.OnFailedMultiMsg(g_objRegist._exceptions);
	}
}


isSameQuestion = function()
{
	$('pp_passwordQuestion2').parentNode.className = "";
	//g_objPasswordProtect.BackToNormal();
	var q1 = $('pp_passwordQuestion1').options[$('pp_passwordQuestion1').selectedIndex].value;
	var q2 = $('pp_passwordQuestion2').options[$('pp_passwordQuestion2').selectedIndex].value;
	//alert(q1);
	//alert(q2);
	if ( q1==q2 )
	{
		//alert("问题一样");
		$('pp_passwordQuestion2').parentNode.className = "fail";
		g_objRegist._exceptions.push({ctrl_id:'pp_passwordQuestion2',msg:''});
	}
}

showAnsFormatInfo = function(objName)
{
	var selectLabel;
	var qObj;
	var infoObj;
	if ( objName == "pp_passwordAnswer1" )//question one
	{
		qObj = $('pp_passwordQuestion1');
		infoObj = $('pp_passwordAnswer1_info');
	}
	else
	{
		qObj = $('pp_passwordQuestion2');
		infoObj = $('pp_passwordAnswer2_info');

	}
	
	selectLabel = qObj.options[qObj.selectedIndex].parentNode.label;
	
	if ( selectLabel == "生日" )//选择了生日
	{ 
		infoObj.innerHTML = "生日格式：年月日(YYYYMMDD)，例如20050705 ";
	}
	else//选择了其他
	{
		infoObj.innerHTML = "请输入：1-19位中文或者1-38位英文。";
	}
}

	
init = function()
{
	g_objRegist.Load();
	//setVerifyCode($('verifycodeImg'));
	var strSrc;
	try { strSrc = $P('src');} catch(ex){strSrc = "";}
	
	var strUrl;
	try { strUrl = $P('url');} catch(ex){strUrl = "";}
	strUrl = decodeURIComponent(strUrl);
	Browser.setCookie("i_url", strUrl);
	
	var strText;
	try { strText = $P('text');} catch(ex){strText = "";}
	strText = decodeURIComponent(strText);
	Browser.setCookie("i_text", strText);
					
	var strCode;
	try { strCode = $P('code');} catch(ex){strCode = "";}
	
}	
function setDefaultValue(){	
	if($("username").value.length<=10){
		$("nickname").value=$("username").value;
	}else{
		$("nickname").value=$("username").value.substr(0,10); 
	}
	$("sex").checked=true;
	//setDefaultValue($("username").value,$("password").value);
	
}
function setDefaultStyle(){
	$("pnameP").style.display="none";
	$("sexP").style.display="none";	
}

function showRegPnl(f,init){
	var sFlag = ((f===undefined || f)?true:false);
	var pnl = $('regitPnl');
	if(pnl){		
		showPanel(PageConfig.maskDiv,sFlag);		
		pnl.style.display = sFlag?'':'none';
		if(sFlag){
			if(init || init==undefined){
				$('username').value = '';
				$('password').value = '';
				$('confirmPassword').value = '';
				$('mailbox').value = '';
				$('verifycode').value = '';
				$('verifycodeImg').src = PageConfig.validPic+String(Date.parse(new Date()));
				g_objRegist.BackToNormal();
			}
			//setVerifyCode($('verifycodeImg'));
			pnl.style.top  = '100px';
			pnl.style.left = (document.body.offsetWidth - pnl.offsetWidth)/2 + 'px';
		}else{
		}
	}
}
