
var msgArray=new Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0);
var currentDiv;
var isSubmit=false;
var multiChar=new Array("０","１","２","３","４","５","６","７","８","９","Ｑ","Ｗ","Ｅ","Ｒ","Ｔ","Ｙ","Ｕ","Ｉ","Ｏ","Ｐ","Ａ","Ｓ","Ｄ","Ｆ","Ｇ","Ｈ","Ｊ","Ｋ","Ｌ","Ｚ","Ｘ","Ｃ","Ｖ","Ｂ","Ｎ","Ｍ","ｑ","ｗ","ｅ","ｒ","ｔ","ｙ","ｕ","ｉ","ｏ","ｐ","ａ","ｓ","ｄ","ｆ","ｇ","ｈ","ｊ","ｋ","ｌ","ｚ","ｘ","ｃ","ｖ","ｂ","ｎ","ｍ","．","－","＿","＠"," ","　");
var asciiChar=new Array("0","1","2","3","4","5","6","7","8","9","q","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m","q","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m",".","-","_","@","","");

function validateMail(o)
{
	var pattern = /^[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)*@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+){1,4}$/;
    return pattern.test(o);
}

function validateInArray(mailServer, mailArray)
{
	for (var i=0;i<mailArray.length;++i)
		if (mailServer==mailArray[i])
			return true;
	return false;
}

function replaceAll(str, strFrom, strTo)
{
	return str.replace(new RegExp(strFrom, "g"), strTo);
}

function multi2ascii(str)
{
	for (var i=0;i<multiChar.length;++i)
		str=replaceAll(str, multiChar[i], asciiChar[i]);
	return str;
}

function increaseUserNameFA()
{
	memberform.usernamefa.value++;
}

function increasePasswordFA()
{
	memberform.Password.value++;
}

function increaseEmailFA()
{
	memberform.email.value++;
}

function increaseEmailTD()
{
	memberform.emailtd.value++;
}

function increaseRealName()
{
	memberform.realnamecci.value++;
}

function getMsgStatus(o)
{
	var nCount=0;
	for (var i=0;i<o.length;++i)
		nCount+=o[i];
	return nCount>0;
}

/*
function showErrorMsg(isShow)
{
	errorMsg.style.display=isShow?"block":"none";
	errorMsg.innerHTML=isShow>0?"<table width='100%' border='0' cellspacing='0' cellpadding='0'><tr><td align='right' valign='top'>&nbsp;</td><td valign='top'>&nbsp;</td></tr><tr><td width='15%' align='right' valign='top'>"+noticeBig+"</td><td><span id='b2'><font id='f2'><strong>您填写的信息有误！请根据以下红字提示修改：</strong></font></span></td></tr><tr><td align='right' valign='top'>&nbsp;</td><td valign='top'>&nbsp;</td></tr></table>":"";
}
*/
function showErrorSingle(msg, pos, value)
{
	msgArray[pos]=value;
	//showErrorMsg(getMsgStatus(msgArray));
	currentDiv.style.display=value>0?"block":"none";
	currentDiv.innerHTML=value>0?"<span id='b2'><font id='f2'>"+msg+"</font></span>":"";
}

function checkForm(o)

{

	isSubmit=true;

	//msgArray[0]=checkUserName(o.UserName);

	msgArray[1]=checkPassword(o);

	msgArray[2]=checkQuestion(o);

	msgArray[3]=checkMail(o);

	msgArray[4]=checkSex(o.sex);

	//msgArray[5]=checkBirthday(o);

	//msgArray[6]=checkCity(o); //city code rule changed

	//验证验证码 msgArray[7]=checkDoor(o.door);
	msgArray[8]=checkAnswer(o);
	msgArray[9]=checksection(o); //验证区号
	msgArray[10]=CheckCode(o); //验证电话或手机
	msgArray[11]=checkiaddress(o); //验证地址
	msgArray[12]=checkZipCode(o); //验证地址

	if (getMsgStatus(msgArray))

	{

		alert("注册提示：\n\n您填写的信息有误，请根据页面中的红字提示进行修改！");

		if (msgArray[0]==1)

			o.UserName.focus();

	       if (msgArray[1]==1)

			o.Password.focus();

		else if (msgArray[2]==1)

			o.email.focus();

		else if (msgArray[3]==1)

			o.email.focus();

		else if (msgArray[4]==1)

			o.sex[0].focus();

		else if (msgArray[5]==1)

			o.byear.focus();
		else if (msgArray[6]==1)
			o.city.focus();
		else if (msgArray[7]==1)
                        o.door.focus();
		else if (msgArray[8]==1)
			o.Answer.focus();
		else if (msgArray[9]==1)
			o.phone_section.focus();
		else if (msgArray[10]==1)
			o.phone_code.focus();
		else if (msgArray[11]==1)
			o.iaddress.focus();
		else if (msgArray[12]==1)
			o.ZipCode.focus();
		return false;

	}

	o.Submit.disabled = true;
	return true;

}



function checkUserName(o)
{
	codeLength = 0;
	codeLen = 0;
	currentDiv=username;
	if (o.value=="")
	{
		showErrorSingle("请输入您的用户名", 0, 1);
		return 1;
	}
	if (o.value!="")
	{	
		for(i=0;i<o.value.length;i++){
			
			if(o.value.charCodeAt(i)>255){
				codeLen+=1;
				
			}
		}
		codeLength = o.value.length + codeLen
		if (codeLength>16||codeLength<4)
		{
			increaseUserNameFA();
			showErrorSingle("您输入的用户名必须是长度4-16位的字符，请重设", 0, 1);
			document.getElementById("check_name").src="reg/test.html";
			return 1;
		}
		if (isWhitespace(o.value))
		{
			increaseUserNameFA();
			showErrorSingle("输入的用户名中不能包含空格符，请重新输入", 0, 1);
			document.getElementById("check_name").src="reg/test.html";
			return 1;
		}
		var badChar="><,[]{}?/+=|\\'\":;~!@#*$%^&()`";
		var errorChar=isCharsInBagEx(o.value, badChar);
		if (errorChar!="")
		{
			increaseUserNameFA();
			showErrorSingle("用户名中请勿使用 <, >, ', ?, /, \\ 等特殊字符", 0, 1);
			document.getElementById("check_name").src="reg/test.html";
			return 1;
		}
		errorChar=isNameContainEachnet(o.value.toLowerCase());
		if (errorChar!="")
		{
			increaseUserNameFA();
			showErrorSingle("您输入的用户名不能包含关键字“" + errorChar + "”", 0, 1);
			document.getElementById("check_name").src="reg/test.html";
			return 1;
		}
	}
	showErrorSingle("", 0, 0);
	return 0;
}

function checkPassword(o)
{
	currentDiv=Password;
	var pass=o.Password.value;
	var pass2=o.PwdConfirm.value;
	if (pass==""||pass2=="")
	{
		showErrorSingle("<img src=/hoobe/err_pic.gif width=9 height=10 hspace=3 vspace=2 border=0>请输入您的密码", 1, 1);
		return 1;
	}
	if ((pass!="")&&(pass2!=""))
	{
		if ((pass.length>20)||(pass.length<6))
		{
			increasePasswordFA();
			showErrorSingle("<img src=/hoobe/err_pic.gif width=9 height=10 hspace=3 vspace=2 border=0>您输入的密码必须是长度6-20位的字符", 1, 1);
			return 1;
		}
		if (pass!=pass2)
		{
			increasePasswordFA();
			showErrorSingle("<img src=/hoobe/err_pic.gif width=9 height=10 hspace=3 vspace=2 border=0>您两次输入的密码不符！请重新填写", 1, 1);
			return 1;
		}
		//if (pass==o.username.value)
		//{
		//	increasePasswordFA();
		//	showErrorSingle("请不要使用用户名作密码，容易别人猜出", 1, 1);
		//	return 1;
		//}
	}
	showErrorSingle("", 1, 0);
	return 0;
}
function checkQuestion(o)

{
	currentDiv=Question;
	
	if (o.Question.value == 0)
	{
	
		showErrorSingle("<img src=/hoobe/err_pic.gif width=9 height=10 hspace=3 vspace=2 border=0>请选择密码查询问题", 2, 1);
		return 1;
	
	}
	if (o.Question.value == 9)
	{
		eval("tr1.style.display='block'");
		//memberform.question.selectedIndex=check_question_index("其他");
	}
	else
	{
		eval("tr1.style.display='none'");
	}

	showErrorSingle("", 2, 0);
	return 0;
}
function checkAnswer(o)

{
	currentDiv=Answers;
	
	if (o.Answer.value == "")
	{
	
		showErrorSingle("<img src=/hoobe/err_pic.gif width=9 height=10 hspace=3 vspace=2 border=0>请填写密码提示答案", 2, 1);
		return 1;
	
	}
	var badChar="><,[]{}?/+=|\\'\":;~!@#*$%^&()`";
	var errorChar=isCharsInBagEx(o.Answer.value, badChar);
	if (errorChar!="")
	{
		
		showErrorSingle("<img src=/hoobe/err_pic.gif width=9 height=10 hspace=3 vspace=2 border=0>密码提示答案请勿使用 "+errorChar+"特殊字符", 0, 1);
		return 1;
	}
	PcodeLength = 0;
	PcodeLen = 0;
	for(i=0;i<o.Answer.value.length;i++){
			
			if(o.Answer.value.charCodeAt(i)>255){
				PcodeLen+=1;
			}
		}
		PcodeLength = o.Answer.value.length + PcodeLen
       if (PcodeLength < 8)
        {
                showErrorSingle("<img src=/hoobe/err_pic.gif width=9 height=10 hspace=3 vspace=2 border=0>您输入的密码提示答案必须8个字母、数字或4个汉字以上", 2, 1); 
		return 1;
	}
	else if(PcodeLength > 80)
	{
		showErrorSingle("<img src=/hoobe/err_pic.gif width=9 height=10 hspace=3 vspace=2 border=0>您输入的密码提示答案必须80个字母、数字或40个汉字以下", 2, 1); 
		return 1;
	}
	
	showErrorSingle("", 2, 0);
	return 0;
}
function checkMail(o)
{
	currentDiv=email;
	var mail=multi2ascii(o.email.value.toLowerCase());
	
	if (mail=="")
	{
		showErrorSingle("<img src=/hoobe/err_pic.gif width=9 height=10 hspace=3 vspace=2 border=0>请输入您的E-mail地址", 3, 1);
		return 1;
	}
	
	if (mail!="")
	{
		if (isWhitespace(mail))
		{
			increaseEmailFA();
			showErrorSingle("<img src=/hoobe/err_pic.gif width=9 height=10 hspace=3 vspace=2 border=0>输入的E-mail地址中不能包含空格符，请重新输入", 3, 1);
			return 1;
		}
		
		if (!validateMail(mail))
		{
			increaseEmailFA();
			showErrorSingle("<img src=/hoobe/err_pic.gif width=9 height=10 hspace=3 vspace=2 border=0>您填入的E-mail格式不正确，请检查有无拼写错误", 3, 1);
			return 1;
		}
		
	}
	showErrorSingle("", 3, 0);
	return 0;
}
function checkBirthday(o)

{

	currentDiv=birthday;

	if ((o.byear.value==""||

		parseInt(o.byear.value)>2004||

		parseInt(o.byear.value)<1900||

		o.bmonth.value==0||

		o.bday.value==0||

		(Math.abs(Math.abs(o.bmonth.value*2-15)-5)==2&&(o.bday.value==31))||

		(o.bmonth.value==2&&((o.bday.value>28&&parseInt(o.byear.value)%4!=0)||(o.bday.value>29&&parseInt(o.byear.value)%4==0)))))

	{

		showErrorSingle("<img src=/hoobe/err_pic.gif width=9 height=10 hspace=3 vspace=2 border=0>请正确选择您的出生日期", 5, 1);

		return 1;

	}

	showErrorSingle("", 5, 0);

	return 0;

}

function checkCity(o)
{
	currentDiv=city;
	//if (!isCharsInBagEx(o.city.value, "0"))
	if (o.city.value == 0)
	{
		showErrorSingle("<img src=/hoobe/err_pic.gif width=9 height=10 hspace=3 vspace=2 border=0>请您选择正确的居住城市", 6, 1);
		return 1;
	}
	showErrorSingle("", 6, 0);
	return 0;
}

function checkTrueName(o)
{
	currentDiv=truename;
	if (o.value=="")
	{
		showErrorSingle("<img src=/hoobe/err_pic.gif width=9 height=10 hspace=3 vspace=2 border=0>请输入您的姓名", 7, 1);
		return 1;
	}
	if (o.value!="")
	{
		var badChar="><,[]{}?/+=|\\'\":;~!#$%()`1234567890";
		var errorChar=isCharsInBagEx(o.value, badChar);
		if (errorChar!="")
		{
			increaseRealName();
			showErrorSingle("<img src=/hoobe/err_pic.gif width=9 height=10 hspace=3 vspace=2 border=0>您输入的姓名"+o.value+"是无效的！请重新输入", 7, 1);
			return 1;
		}
		if (o.value.length>=20)
		{
			increaseRealName();
			showErrorSingle("<img src=/hoobe/err_pic.gif width=9 height=10 hspace=3 vspace=2 border=0>您输入的姓名太长，请重新输入", 7, 1);
			return 1;
		}
	}
	showErrorSingle("", 7, 0);
	return 0;
}

function checkSex(o)
{
	currentDiv=sex;
	if (!(o.sex[0].checked||o.sex[1].checked))
	{
		showErrorSingle("<img src=/hoobe/err_pic.gif width=9 height=10 hspace=3 vspace=2 border=0>请选择您的性别", 4, 1);
		return 1;
	}
	showErrorSingle("", 4, 0);
	return 0;
}

//验证区号
function checksection(o)

{
	currentDiv=section;
	var phone_section=o.phone_section.value;
	if (phone_section=="")
	{
		showErrorSingle("<img src=/hoobe/err_pic.gif width=9 height=10 hspace=3 vspace=2 border=0>请输入您的区号", 9, 1);
		return 1;
	}
	showErrorSingle("", 9, 0);
	return 0;	
}
function CheckCode(o)

{
	currentDiv=Phonecode;
	var phone_code=o.phone_code.value;
	if (phone_code=="")
	{
		showErrorSingle("<img src=/hoobe/err_pic.gif width=9 height=10 hspace=3 vspace=2 border=0>请输入您的电话或手机", 10, 1);
		return 1;
	}
	if ((phone_code.length>11)||(phone_code.length<6))
		{
			
			showErrorSingle("<img src=/hoobe/err_pic.gif width=9 height=10 hspace=3 vspace=2 border=0>您输入的电话或手机必须是长度6-11位的数字", 10, 1);
			return 1;
		}
	showErrorSingle("", 10, 0);
	return 0;		
}
function checkiaddress(o)
{
	currentDiv=address;
	var iaddress=o.iaddress.value;
	if (iaddress=="")
	{
		showErrorSingle("<img src=/hoobe/err_pic.gif width=9 height=10 hspace=3 vspace=2 border=0>请输入您的通信地址", 11, 1);
		return 1;
	}
	showErrorSingle("", 11, 0);
	return 0;	
}
function checkZipCode(o)

{

	currentDiv=Zip;
	var ZipCode=o.ZipCode.value;
	if (ZipCode=="")

	{

		showErrorSingle("<img src=/hoobe/err_pic.gif width=9 height=10 hspace=3 vspace=2 border=0>请输入邮编", 12, 1);

		return 1;

	}
	if ((ZipCode.length>6)||(ZipCode.length<6))
		{
			increasePasswordFA();
			showErrorSingle("<img src=/hoobe/err_pic.gif width=9 height=10 hspace=3 vspace=2 border=0>您输入的邮编必须是长度6位的数字", 1, 1);
			return 1;
		}
	showErrorSingle("", 12, 0);

	return 0;

}

function checkDoor(o)

{

	currentDiv=door;
	if (o.value=="")

	{

		showErrorSingle("<img src=/hoobe/err_pic.gif width=9 height=10 hspace=3 vspace=2 border=0>请输入验证码", 7, 1);

		return 1;

	}
	
var badChar="1234567890";
	var errorChar=isCharsInBagEx(o.value, badChar);
	if (errorChar=="")
	{
		showErrorSingle("<img src=/hoobe/err_pic.gif width=9 height=10 hspace=3 vspace=2 border=0>请输入半角数字", 7, 1);

		return 1;
	
	}

	showErrorSingle("", 7, 0);

	return 0;

}

//后加验证用户名
function isNameContainEachnet (s)
{  
  // Search through string's characters one by one.
  // If character is in bag, append to returnString.
  var s1, s2, s3, s4 ,s5,s6;
  s1 =  "红本网" 
  s2 =  "hoobe" 
  s3 =  "管理员"
  s4 =  "admin"
  s5 =  "　"
  s6 =  "法轮工"


  if (s.indexOf(s1) > -1) 
        return s1;
        
  if (s.indexOf(s2) > -1) 
        return s2;

  if (s.indexOf(s3) > -1) 
        return s3;

  if (s.indexOf(s4) > -1) 
        return s4;

  if (s.indexOf(s5) > -1) 
        return '空格';
  if (s.indexOf(s6) > -1) 
        return s6;

  return "";
}

function regInit()
{
	memberform.check_count.value = 0;
	memberform.emailfa.value = 0;
	memberform.emailtd.value = 0;
	memberform.usernamefa.value = 0;
	memberform.passwordfa.value = 0;
	memberform.realnamecci.value = 0;
}

function check_select_index(sel_obj,opn_val)
{
 	var idx_i = 0;
	for ( idx_i = 0; idx_i<sel_obj.length; idx_i++)
	{
		if(sel_obj.options[idx_i].value == opn_val)
		{ return idx_i; }
	}
	return 0;
}

function check_city_index2(provcode,citycode)
{
        document.memberform.city.options[0].value = 0;
        document.memberform.city.options[0].text = "\xc7\xeb\xd1\xa1\xd4\xf1...";

        if (provcode == 0)
        {
                document.memberform.city.options.length = 1;
        }
        else
        {
                var citylist = eval("prov" + provcode + ".split(',')");
                var codelist = eval("code" + provcode + ".split(',')");
                document.memberform.city.options.length = citylist.length + 1;
                for(var i=1; i<citylist.length+1; i++)
                {
                        document.memberform.city.options[i].value = codelist[i-1];
                        document.memberform.city.options[i].text = citylist[i-1];
                }
        }
        document.memberform.city.selectedIndex=0;

        return false;
}