//层ID号处理函数
function $(id) {
  if(document.getElementById) { return document.getElementById(id); }
  else if(document.all) { return document.all[id]; }
  else if(document.layers) { return document.layers[id]; }
  else { return false; }
}
function $$(id)
{
	return $(id).style;
}

//回车
function OnDown(e,obj)
{
    if (e.keyCode == 13)
        $(obj).focus();
}//func

function hiddenOther()
{
	$$("usernameD1").display = 'none';
	$$("userpwdD1").display = 'none';
	$$("repwdD1").display = 'none';
	$$("emailD1").display = 'none';
	$$("quesD1").display = 'none';
	$$("answerD1").display = 'none';
	$$("userpwdD2").display = 'none';
	$$("repwdD2").display = 'none';
	$$("emailD2").display = 'none';
	$$("quesD2").display = 'none';
	$$("answerD2").display = 'none';
}

//处理3种状态的div,DivOrderID是要显示的div的序号,DivName是div的名称（不含尾号）
function showInfoDiv(DivOrderID,DivName)
{
	$$(DivName + '1').display = 'none';
	$$(DivName + '2').display = 'none';
	$$(DivName + '3').display = 'none';
	if (DivOrderID != '0')	//均不显示
		$$(DivName + DivOrderID).display = 'block';
}

//用户名
//用户名获取焦点	*******************************************************
var LastUserName = "";
function UserNameFocue()
{
	hiddenOther()
	if($("userName").value == "")
		showInfoDiv(1,"usernameD");
	else
		UserNameBlur($("userName").value);	//检查用户名
}


//用户名失去焦点;检查用户名
function UserNameBlur(str)
{
	if (str == "")
    {
        $("usernameD2").innerHTML = '用户名不能为空';
		showInfoDiv(2,"usernameD");
        return false;
    }
	if (LastUserName == str)	//如果用户名没有更改过
		return false;
	LastUserName = str;	
	
    var reg = /^(\w|[\u4E00-\u9FA5])*$/;
    if(!str.match(reg))	 //有特殊符号
    {
		var txt = "";
		var word = ""
		for(i=0; i<str.length;i++)
		{
			word = str.substr(i,1);
			if(!(word.match(reg)))	//此字符是特殊符号
			{
				if (word == " ")
					word = "空格";
				if(txt.replace(word,"") == txt)	//如果现在txt中没有这个字符
				{
					if (txt == "")
						txt = word;
					else
						txt = txt + "  " + word;
				}//if
			}//if
		}//for
        $("usernameD2").innerHTML ="请不要在用户名中使用<b>" + txt + "</b>，谢谢";
		showInfoDiv(2,"usernameD");
        return false;
    }
    if(str.substr(0,1) == "_")
    {
        $("usernameD2").innerHTML ="用户名不允许以下划线开头";
		showInfoDiv(2,"usernameD");
        return false;
    }
    if(str.substr((str.length-1),1) == "_")
    {
        $("usernameD2").innerHTML ="用户名不允许以下划线结尾";
		showInfoDiv(2,"usernameD");
        return false;
    }
    if(str.length<2)
    {
		$("usernameD2").innerHTML ="用户名长度不得少于2个字";
		showInfoDiv(2,"usernameD");
       return false;
    }
    else if(str.length>20)
    {
		$("usernameD2").innerHTML ="用户名长度不得多于20字";
		showInfoDiv(2,"usernameD");
       return false;
    }
	else
	{
		//交给ajax验证，并处理
		//return getHtmlValue("Checkreg.aspx?username=" + encodeURIComponent(str), "name");
		return true;

	}//if
}//fun


//密码获取焦点 	*******************************************************
function mimaFocue(str)
{
	hiddenOther();
	if (str == "")
		showInfoDiv(1,"userpwdD");
	else
		mimaBlur(str);	//检查密码

}

//检查密码
function mimaBlur(str)
{
	var txt = "";
    if(str == "")
    {
        $("userpwdD2").innerHTML = "密码不能为空，请填写密码并牢记";
        showInfoDiv(2,"userpwdD");
        return false;
    }
    if (str.length < 6)
    {
        $("userpwdD2").innerHTML =  "您的密码过短，请不要少于6个字符";
        showInfoDiv(2,"userpwdD");
        return false;
    }
    if (str.length > 16)
    {
        $("userpwdD2").innerHTML = "您的密码过长，请不要超过16个字符";
        showInfoDiv(2,"userpwdD");
        return false;
    }
    $("userpwdD2").innerHTML = "";
    showInfoDiv(3,"userpwdD");
    return  true;
}
 //检查密码强度	password
function mimaKeyup(str)
{
	if (str.length < 6) //无强度
	{
		$$('pwdstoleft').width = 0 + 'px';
		$$("pwdstoright").width = 150 + 'px';
	}
	else if (str.length <= 16)
	{
		var wd = 0;
		var len = str.length - 5;	//比6个字符多处几个字符
		wd = 10 * len;

		var reg = /^(\w|[\u4E00-\u9FA5])*$/;
		if(!str.match(reg))	//中
		{
			wd = wd + 50;
		}
		$$('pwdstoleft').width = wd;
		try{
		    $$("pwdstoright").width = (150 - wd);
		    }catch(e)
		    {}
	}
	return;
	
}//function

//确定密码得到焦点
function repwdFocue(str)
{
	//hiddenOther()
	if ($$("userpwdD3").display != 'block')	//如果密码还没有正确
	{
		$("repwdD2").innerHTML = "请先正确输入密码";
		showInfoDiv(2,"repwdD");
		return false;
	}
	if($("rePassword").value == "")
		showInfoDiv(1,"repwdD");
	else
		repwdBlur(str);
}

//确定密码失去焦点
function repwdBlur(str)
{
	if ($$("userpwdD3").display != 'block')
	{
		$("repwdD2").innerHTML = "请先输入密码";
		showInfoDiv(2,"repwdD");
		return false;
	}
    if(str =="")
	{
        $("repwdD2").innerHTML ="确认密码不能为空，请填写密码并牢记";
		showInfoDiv(2,"repwdD");
        return false;
	}
	else
    {
        if (str == $("password").value)
        {
            showInfoDiv(3,"repwdD");
            return true;
        }
        else
        {
			$("repwdD2").innerHTML ="确认密码不正确";
			showInfoDiv(2,"repwdD");
		    return false;
        }
    }
}

 //验证邮箱				*******************************************************
var LastUseremail = "";
function emailFocue()
{
	if($$("emailD3").display != "block")
		showInfoDiv(1,"emailD");
}
//信箱格式检测
function isEmail(str) {
    var regexp = /^(\w)+@(\w)+\.(\w)+/;
    return(regexp.test(str));
}
//邮箱失去焦点
function emailBlur(str)
{
	if (LastUseremail == str)
	{
		if($$("emailD3").display != "block")
		{
			showInfoDiv(2,"emailD");
			return false;
		}
		else
			return true;
	}
	LastUseremail = str;
    if(str != "")
    {
        if (isEmail(str))
        {
			//交给ajax检测
			//return getHtmlValue("Checkreg.aspx?usermail=" + str, "email");
            showInfoDiv(3,"emailD");
            return true;
        }
        else
        {
             $("emailD2").innerHTML ="邮箱输入不正确，请修改";
             showInfoDiv(2,"emailD");
             return false;
        }
    }
    else
    {
        $("emailD2").innerHTML ="请填写您的邮箱";
		showInfoDiv(2,"emailD");
		return false;
    }
}

//密码问题			   	*******************************************************
//得到焦点
function quesFocue(str)
{
	//hiddenOther();
	if($$("quesD3").display!='none')
	{
		showInfoDiv(1,"quesD");
	}
}	
//失去焦点
function quesBlur(str)
{
    if(str != "")
    {
		hiddenOther();
		showInfoDiv(3,"quesD");
        return  true;
    }
    else
    {
		showInfoDiv(2,"quesD");
		$("quesD2").innerHTML = "请您选择一个查询问题";
        return false;
    }
}
//密码答案				*******************************************************
//失去焦点
function answerBlur(str)
{
    if(str == "")
    {
		$("answerD2").innerHTML = "请您填写找回密码的答案";
        showInfoDiv(2,"answerD");
		return false;
    }
    else if (str.length > 20)
    {
		$("answerD2").innerHTML = "您填写的的答案太长了，请不要超过20个字";
        showInfoDiv(2,"answerD");
		return false;
    }
    else
    {
        showInfoDiv(3,"answerD");
        return true;
    }
}

//获取焦点
function answerFocue()
{
	if ($("passAnswer").innerHTML == '')
		showInfoDiv(1,"answerD");
	else
		answerBlur(str);
}

//更换验证码	 	*******************************************************
function changeCode()
{
	var _code = GetRandomCode();
	$("page_code").innerHTML = _code;
	$("re_code").value = _code;
//	$$("page_code").backgroundPositionX = (0 -GetRandomNum(30,182)) + 'px';
//	$$("page_code").backgroundPositionY = (0 -GetRandomNum(1,92)) + 'px';
	$("code").select();
}

//其它信息	 	*******************************************************
function qqFocue(str)   //QQ
{
    showInfoDiv(1,"qqD");
}
function qqBlur(str)
{
    if (str == "")
    {
        showInfoDiv(0,"qqD");
        return true;
    }
    else if (str.length < 5)
    {
        showInfoDiv(2,"qqD");
        return false;
    }
    else if(/^([0-9]|[\-])+$/g.test(str))  //是数字
    {
        showInfoDiv(3,"qqD");
        return true;
    }
    else if(isEmail(str))   //是邮箱
    {
        showInfoDiv(3,"qqD");
        return true;    
    }
    else
    {
        showInfoDiv(2,"qqD");
        return false;
    }
}

function msnFocue(str)  //msn
{
    showInfoDiv(1,"msnD");
}
function msnBlur(str)
{
    if (str == "")
    {
        showInfoDiv(0,"msnD");
        return true;
    }
    else if(isEmail(str))   //是邮箱
    {
        showInfoDiv(3,"msnD");
        return true;    
    }
    else
    {
        showInfoDiv(2,"msnD");
        return false;
    }
}

function phoneFocue(str)    //电话
{
    showInfoDiv(1,"phoneD");
}
function phoneBlur(str)
{
    if(str == "")
    {
        showInfoDiv(0,"phoneD");
        return true;
    }
    else if(str.length<7 || str.length>18)
    {
        showInfoDiv(2,"phoneD");
        return false;    
    }
    else
    {
        var reg=/^([0-9]|[\-])+$/g;
        if (reg.exec(str))
        {
            showInfoDiv(3,"phoneD");
            return true;    
        }
        else
        {
            showInfoDiv(2,"phoneD");
            return false;    
        }
    }
}//func
//提交		*******************************************************
function formsubmit()
{		
	provincecityOnChange(); //  add by zhangjinhong 将省市的文本写到文本框中
	//重新检查一遍
	UserNameBlur($("userName").value);
	mimaBlur($("password").value);
	repwdBlur($("rePassword").value);	    
	emailBlur($("email").value);
    qqBlur($("qq").value);
	msnBlur($("msn").value);
	phoneBlur($("phone").value);
	quesBlur($("passQuestion").value);
	answerBlur($("passAnswer").value);

	/*if (!$('allowService').checked)
	{
		alert("请接受“服务协议”");
		$("allowService").focus();
		return false;
	}
	else */
	if ($("code").value == "")
	{
		alert("请填写验证码");
		$("code").focus();
		return false;
	}
	else if ($("code").value != $("re_code").value)
	{
		alert("您的验证码不正确");
		changeCode();
		$("code").select();
		return false;
	}
	//----------------
//	else if($$("usernameD3").display != "block")  
//	{
//		var txt = $("usernameD2").innerHTML;
//		txt = txt.replace('<B>',"");
//		txt = txt.replace('</B>',"");
//		alert(txt);
//		$("userName").select();
//		return false;
//	}
//	else if($$("userpwdD3").display != "block")		
//	{
//		alert($("userpwdD2").innerHTML);
//		$("password").select();
//		return false;
//	}
//	else if($$("repwdD3").display != "block")	 
//	{
//		alert($("repwdD2").innerHTML);
//		$("rePassword").select();
//		return false;
//	}
//	else if (($("sexboy").checked == false) & ($("sexgirl").checked == false))
//	{
//		alert("请选择性别");
//		$("sexboy").focus();
//		return false;
//	}
//	//
////	else if($$("emailD3").display != "block")
////	{
////		alert($("emailD2").innerHTML);
////		$("email").select();
////		return false;
////	}
//	else if($$("quesD3").display != "block")
//	{
//		alert("请选择密码提示的问题");
//		$("passQuestion").focus();
//		return false;
//	}
//	else if($$("answerD3").display != "block")
//	{
//		alert($("answerD2").innerHTML);
//		$("passAnswer").select();
//		return false;
//	}
//	else if (($("qq").value=="") & ($("msn").value=="") & ($("phone").value==""))
//	{		
//		alert("QQ、MSN和联系电话至少要填写一项");
//		$("qq").focus();
//		return false;
//	}
//	//
////	else if(($("qq").value!="") & ($$("qqD3").display!="block"))
////	{
////	    alert("您填写的QQ帐号不正确");
////	    $("qq").select();
////	    return false;
////	}
////	else if(($("msn").value!="") & ($$("msnD3").display!="block"))
////	{
////	    alert("您填写的msn帐号不正确");
////	    $("msn").select();
////	    return false;
////	}
////	else if(($("phone").value!="") & ($$("phoneD3").display!="block"))
////	{
////	    alert("您填写的电话号码不正确");
////	    $("phone").select();
////	    return false;
////	}	
//	else if ($("province").value=="")
//	{
//		alert("请选择您所在的省份");
//		$("province").focus();
//	    return false;
//	}
//	else if ($("city").value=="")
//	{
//		alert("请选择您所在的城市");
//		$("city").focus();
//	    return false;
//	}
	else
	{
		//return true;
		$("myform").submit();
		//$$("submitform").display = 'none';
		//$("regbutton").innerHTML = "请稍后...";
    }
	return false;
}//func

//ajax
function getHtmlValue(url,type)
{
	//window.open(url);
	var xmlHttp="";
	var _result = null;
	url = url + "&rnd=" + GetRandomNum();
	if (window.ActiveXObject) 
	{ 
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	} 
	else if (window.XMLHttpRequest) 
	{ 
		xmlHttp = new XMLHttpRequest();
	} 

	xmlHttp.onreadystatechange = function()
	{
		if(xmlHttp.readyState == 4)
		{
			var ret = xmlHttp.responseText;
			if(type=="name")
			{
				if (ret == "1")
				{
					$("usernameD2").innerHTML ="";
					showInfoDiv(3,"usernameD");
					_result = true;
				}
				else
				{
					var txt = "";
					if (ret == "-1")
						txt =	"用户名不能为空";
					else if (ret == "-2")
						txt =	"用户名长度不得少于2个字";
					else if (ret == "-3")
						txt =	"您的用户名过长，请不要多于20字";
					else if (ret == "-11")
						txt =	"您填写的用户名已被注册";
					$("usernameD2").innerHTML = txt;
					showInfoDiv(2,"usernameD");
					_result = false;
				}//if
			}
			else if (type=="email")
			{
				if (ret == "1")
				{
					//$("emailD2").innerHTML ="";
					showInfoDiv(3,"emailD");
					_result = true;
				}
				else
				{
					var txt = "";
					if (ret == "-1")
						txt =	"请填写您的邮箱";
					else if (ret == "-2")
						txt =	"您的邮箱格式不正确";
					else if (ret == "-3")
						txt =	"您的邮箱格式不正确";
					else if (ret == "-11")
						txt =	"您填写的邮箱已被注册";
					$("emailD2").innerHTML = txt;
					showInfoDiv(2,"emailD");
					_result = false;
				}//if
			}
			else if (type== "code")
			{
				if (ret == "1"){
					_result = true;
				}
				else
					_result = false;
			}
		}
		else
			_result = false;
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	return _result;
}

//随机数
function GetRandomNum(){
        var Range = 9999;
        var Rand = Math.random();
        return(Math.round(Rand * Range));
}
function GetRandomCode(){
	var Range = 9999 - 1000;
    var Rand = Math.random();
    return(1000 + Math.round(Rand * Range));
}

//将省市选择的值写入隐藏控件
function provincecityOnChange()//add by zhangjinhong
{
    var ddpProvince=document.getElementById("province");
    var ddpCity=document.getElementById("city");
    document.getElementById("hidprovince").value=ddpProvince.options[ddpProvince.selectedIndex].text;
    document.getElementById("hidcity").value=ddpCity.options[ddpCity.selectedIndex].text;
}