var regName = "";
var userExists = false;

function setCurrentItem(row_id) {
	document.getElementById("row_user").style.backgroundColor = "";
	document.getElementById("row_pass").style.backgroundColor = "";
	document.getElementById("row_pass_confirm").style.backgroundColor = "";
	document.getElementById("row_sex").style.backgroundColor = "";
	document.getElementById("row_age").style.backgroundColor = "";
	document.getElementById("row_mail").style.backgroundColor = "";
	document.getElementById("row_area").style.backgroundColor = "";
	document.getElementById("row_yzm").style.backgroundColor = "";
	
	document.getElementById(row_id).style.backgroundColor = "#EDF2F7";
}

function checkUserName(checkExist) {
	var user_name = document.getElementById("User_Account").value.toLowerCase();
	if (regName != user_name) {
		getObject("username_explain").className = "red";
		getObject("recommend_user").style.display = "none";
		
		var pattern=/^[a-zA-Z][a-zA-Z0-9_]{1,14}[a-zA-Z0-9]$/i;
		var patternLastChar = /^[a-zA-Z0-9_]{1,15}_$/i;
		var patternFirstChar = /^[0-9_][a-zA-Z0-9_]{1,14}$/i;
		if (user_name.indexOf("xx", 0) != -1) { // 注册用户名中不能包含xx
			regName = document.form2.User_Account.value;
			getObject("username_explain").innerHTML = '<img src="http://pic.51.com/i/uc/x.gif" width="15" height="15" align="absmiddle" /> &nbsp;<span class="red">为防止被部分防火墙屏蔽，用户名中不能带有 xx';
		} else if (user_name.length < 3)	{ // 用户名长度不能小于3
			regName = document.form2.User_Account.value;
			getObject("username_explain").innerHTML = '<img src="http://pic.51.com/i/uc/x.gif" width="15" height="15" align="absmiddle" /> &nbsp;<span class="red">用户名不能少于3位';
		} else if (user_name.length > 16) {// 用户名长度不能大于16
			regName = document.form2.User_Account.value;
			getObject("username_explain").innerHTML = '<img src="http://pic.51.com/i/uc/x.gif" width="15" height="15" align="absmiddle" /> &nbsp;<span class="red">用户名不能大于16位';
		} else if (patternFirstChar.test(user_name)) { // 开头字符必须为字母
			regName = document.form2.User_Account.value;
			getObject("username_explain").innerHTML = '<img src="http://pic.51.com/i/uc/x.gif" width="15" height="15" align="absmiddle" /> &nbsp;<span class="red">用户名必须以字母开头';
		} else if (patternLastChar.test(user_name)) { // 末字符不能是下划线
			regName = document.form2.User_Account.value;
			getObject("username_explain").innerHTML = '<img src="http://pic.51.com/i/uc/x.gif" width="15" height="15" align="absmiddle" /> &nbsp;<span class="red">下划线(_)不能放在末尾';
		} else if (!pattern.test(user_name)) { // 用户名只能是字母下画线和数字 并且字母开头 3 - 16位
			regName = document.form2.User_Account.value;
			getObject("username_explain").innerHTML = '<img src="http://pic.51.com/i/uc/x.gif" width="15" height="15" align="absmiddle" /> &nbsp;<span class="red">只能由3-16位字母、数字或下划线(_)构成';
		}  else {
			if (checkExist) { // 是否检查用户名是否存在
				getObject("username_explain").innerHTML = "正在检查...";
				getObject("username_explain").className = "bd2";
				regName = document.form2.User_Account.value;
				validateUserName();
			} else {
				getObject("username_explain").innerHTML = "只能由3-16位字母(a-z)、数字(0-9)或下划线(_)构成，并且必须以字母开头";
				getObject("username_explain").className = "h12";
			}
		}
	} else {
		getObject("username_explain").className = "h12";
	}
}

function userNameFocus() {
	getObject("username_explain").innerHTML = "&nbsp;只能由3-16位字母(a-z)、数字(0-9)或下划线(_)构成，并且必须以字母开头";
	getObject("username_explain").className = "h12";
}

function sexFocus() {
	getObject("sex_explain").innerHTML = "&nbsp;<span class='red' style='font-weight:bold'>一旦选择，不可更改</span>";
	getObject("sex_explain").className = "h12";
}

function ageFocus() {
	getObject("age_explain").innerHTML = "&nbsp;请一定选择真实年份，<span class='red' style='font-weight:bold'>选择后将不可更改</span>";
	getObject("age_explain").className = "h12";
}

function userPwdFocus(id) {
	getObject(id).innerHTML =  '&nbsp;密码要至少8位以上，且不能包含空格，英文字母区分大小写 <a href="#" onclick="try {window.showModalDialog(\'reg/suggestion.html\', \'\', \'dialogWidth=530px;dialogHeight=380px\');} catch (e) {window.open (\'reg/suggestion.html\', \'密码建议\', \'height=320, width=520, top=150, left=150, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=n o, status=no\');} return false;"><span style="12px;text-decoration:underline;color:#0000ff;">如何设置密码>></span></a>';
	getObject(id).className = "h12";
}

function emailFocus() {
	getObject("mail_explain").innerHTML = "<br />如果您有邮箱，请认真填写，它将用于密码保护。<a href='http://mail.cn.yahoo.com/?id=40000' target='_blank' style='text-decoration:underline;color:#0000ff'>我们推荐@yahoo.cn无限容量邮箱</a>";
	getObject("mail_explain").className = "h12";
}

function changeAuthCode() {
	var num = 	new Date().getTime();
	var rand = Math.round(Math.random() * 10000);
	num = num + rand;
	document.getElementById("yzm").src = "authCode.php?tag=" + num;
	// 设置唯一标识
	document.getElementById("identifier").value = num;
	
	return false;	
}

function setRecommendUserName(name) {
	userExists = false;
	getObject("User_Account").value = name;
	regName = name;
	getObject("username_explain").innerHTML = "√"
	getObject("username_explain").className = "lv";
}

function checkPassword() {
	var password1 = getObject("User_Password").value;
	var userName = getObject("User_Account").value;
	var password2 = getObject("User_RePassword").value;
	
	var numericPattern = /^[0-9]{1,9}$/i;
	var repeatPattern = /^[\w]*(\w)\1{3,}[\w]*$/i;
	if (password1 == "") {
		getObject("pass_explain").className = "h12";
		getObject("pass_explain").innerHTML = '&nbsp;密码要至少8位以上，且不能包含空格，英文字母区分大小写 <a href="#" onclick="try {window.showModalDialog(\'reg/suggestion.html\', \'\', \'dialogWidth=530px;dialogHeight=380px\');} catch (e) {window.open (\'reg/suggestion.html\', \'密码建议\', \'height=320, width=520, top=150, left=150, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=n o, status=no\');} return false;"><span style="12px;text-decoration:underline;color:#0000ff;">如何设置密码>></span></a>';
	} else if (password1.indexOf(" ", 0) != -1) {
		getObject("pass_explain").style.color = "red";
		getObject("pass_explain").innerHTML = '<img src="http://pic.51.com/i/uc/x.gif" width="15" height="15" align="absmiddle" /> &nbsp;不能包含空格 <a href="#" onclick="try {window.showModalDialog(\'reg/suggestion.html\', \'\', \'dialogWidth=530px;dialogHeight=380px\');} catch (e) {window.open (\'reg/suggestion.html\', \'密码建议\', \'height=320, width=520, top=150, left=150, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=n o, status=no\');} return false;"><span style="12px;text-decoration:underline;color:#0000ff;">如何设置密码>></span></a>';
	} else if (password1.length < 8) {
		getObject("pass_explain").style.color = "red";
		getObject("pass_explain").innerHTML = '<img src="http://pic.51.com/i/uc/x.gif" width="15" height="15" align="absmiddle" /> &nbsp;至少要8位以上 <a href="#" onclick="try {window.showModalDialog(\'reg/suggestion.html\', \'\', \'dialogWidth=530px;dialogHeight=380px\');} catch (e) {window.open (\'reg/suggestion.html\', \'密码建议\', \'height=320, width=520, top=150, left=150, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=n o, status=no\');} return false;"><span style="12px;text-decoration:underline;color:#0000ff;">如何设置密码>></span></a>';
	} else if (numericPattern.test(password1)) {
		getObject("pass_explain").style.color = "black";
		getObject("pass_explain").innerHTML = '&nbsp;不能使用9位以下的纯数字，这样容易被人猜到 <a href="#" onclick="try {window.showModalDialog(\'reg/suggestion.html\', \'\', \'dialogWidth=530px;dialogHeight=380px\');} catch (e) {window.open (\'reg/suggestion.html\', \'密码建议\', \'height=320, width=520, top=150, left=150, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=n o, status=no\');} return false;"><span style="12px;text-decoration:underline;color:#0000ff;">如何设置密码>></span></a>';
	} else if ((password1.indexOf(userName, 0) != -1) && (password1.length - userName.length < 3)) {
		getObject("pass_explain").style.color = "black";
		getObject("pass_explain").innerHTML = '&nbsp;不能和用户名太相似，这样容易被人猜到 <a href="#" onclick="try {window.showModalDialog(\'reg/suggestion.html\', \'\', \'dialogWidth=530px;dialogHeight=380px\');} catch (e) {window.open (\'reg/suggestion.html\', \'密码建议\', \'height=320, width=520, top=150, left=150, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=n o, status=no\');} return false;"><span style="12px;text-decoration:underline;color:#0000ff;">如何设置密码>></span></a>';
	} else if (repeatPattern.test(password1)) {
		getObject("pass_explain").style.color = "black";
		getObject("pass_explain").innerHTML = '&nbsp;太多连续或重复的字符(如：1234、abcd、3333、ssss等) <a href="#" onclick="try {window.showModalDialog(\'reg/suggestion.html\', \'\', \'dialogWidth=530px;dialogHeight=380px\');} catch (e) {window.open (\'reg/suggestion.html\', \'密码建议\', \'height=320, width=520, top=150, left=150, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=n o, status=no\');} return false;"><span style="12px;text-decoration:underline;color:#0000ff;">如何设置密码>></span></a>';
	} else {
		getObject("pass_explain").style.color = "green";
		getObject("pass_explain").innerHTML = "√";
	}
}

function checkPasswordConfirm()
{
	var password1 = getObject("User_Password").value;
	var password2 = getObject("User_RePassword").value;
	
	if (password2 != "") {
		if (password1 != password2) {
			getObject("pass_confirm_explain").innerHTML = '<img src="http://pic.51.com/i/uc/x.gif" width="15" height="15" align="absmiddle" /> &nbsp; 与上次输入的密码不一致';
			getObject("pass_confirm_explain").style.color = "red";
		} else if (password2.length > 0) {
			getObject("pass_confirm_explain").style.color = "green";
			getObject("pass_confirm_explain").innerHTML = "√";
			getObject("pass_explain").style.color = "green";
			getObject("pass_explain").innerHTML = "√";
		}
	}
}

function getObject(id) {
	if (document.getElementById(id)) {
		return document.getElementById(id);
	} else if(document.all) {
		return document.all[id];
	} else if(document.layers) {
		return document.layers[id];
	}
}

function checkMail() {
	var email = getObject("User_Email").value;
	var provider = "";
	if (getObject("Email_Provider")) {
		provider = getObject("Email_Provider").value;
	}
	if (email == "" || provider == "") {
		return;
	}

	email = email + "@" + provider;

	if (email != "" && !isMail(email)) {
		getObject("mail_explain").innerHTML = '<img src="http://pic.51.com/i/uc/x.gif" width="15" height="15" align="absmiddle" /> &nbsp;您的电子邮箱写错了，请修改';
		getObject("mail_explain").className = "red";
	} else {
		getObject("mail_explain").innerHTML = "如果您有邮箱，请认真填写，它将用于密码保护。";
		getObject("mail_explain").className = "h12";
	}
}

function isMail(mail)  { 
    return(new RegExp(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/).test(mail)); 
}

function changeProvince() {
	gpm.initCity1(document.getElementById('User_Town'), gpm.getSelValue(document.getElementById('User_Shen')));
	gpm.initCity2(document.getElementById('User_City'), '', '');
}

function changeTown() {
	gpm.initCity2(document.getElementById('User_City'), gpm.getSelValue(document.getElementById('User_Shen')), gpm.getSelValue(document.getElementById('User_Town')));
}

function validateUserName() {
	var oXmlHttp = XHConn();
	var request_url = "/reg/userCheck.php?user=" + regName + "&id=" + Math.random() * 10000;

	try {
		oXmlHttp.open("GET", request_url, true);
		oXmlHttp.onreadystatechange = function() {
			if (oXmlHttp.readyState == 4 && oXmlHttp.status == 200) {
				var tmp = oXmlHttp.responseText.split("|");
				if (tmp[0] == "false") {
					getObject("username_explain").innerHTML = "√"
					getObject("username_explain").className = "lv";
					userExists = false;
				} else if(tmp[0] == "true") {
					userExists = true;
					getObject("username_explain").innerHTML = '<img src="http://pic.51.com/i/uc/x.gif" width="15" height="15" align="absmiddle" /> &nbsp;<span class="red"> <em>'+regName+'</em> 此用户名已经存在，请换其它用户名再试！';
					getObject("username_explain").className = "red";
					if (tmp.length == 2) {
						var available_user = tmp[1].split(",");
						getObject("user_text_1").innerHTML = available_user[0];
						getObject("user_text_2").innerHTML = available_user[1];
						getObject("user_text_3").innerHTML = available_user[2];
						getObject("recommend_user").style.display = ""; 
					}
				}
			}
		}
		oXmlHttp.send(null);
	} catch (e) {
		// do nothing
	}
}

function XHConn() {
    var xmlhttp;
    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) { 
  	    try {
  		    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  	    } catch (e) {
  		    try {
  			    xmlhttp = new XMLHttpRequest();
  		    } catch (e) {
  			    xmlhttp = false;
  		    }
  	    }
    }
  
    return xmlhttp;
}

function trim(obj){
	str = obj.split(/^\s+|\s+$/)[0];
	return str;
}

function check() {
	var account = trim(getObject("User_Account").value.toLowerCase());
	var password1 = getObject("User_Password").value;
	var password2 = getObject("User_RePassword").value;
	var sex = getObject("User_Male").checked || getObject("User_Female").checked;
	var ageIndex = getObject("User_Age").selectedIndex;
	var provinceIndex = getObject("User_Shen").selectedIndex;
	var townIndex = getObject("User_Town").selectedIndex;
	var cityIndex = getObject("User_City").selectedIndex;
	var code = trim(getObject("number").value);
	var mail = "";
	if ((getObject("User_Email").value != "") && getObject("Email_Provider") && (getObject("Email_Provider").value != "")) {
		mail = trim(getObject("User_Email").value) + "@" + trim(getObject("Email_Provider").value);
	}
	var pattern=/^[a-zA-Z][a-zA-Z0-9_]{1,14}[a-zA-Z0-9]$/i;
	
	if (account.indexOf("xx", 0) != -1) { // 不能包含xx
		alert("为防止被部分防火墙屏蔽，用户名中不能带有 xx!");
		getObject("User_Account").focus();
		return false;
	} else if (!pattern.test(account)) {
		alert("只能由3-16位字母(a-z)、数字(0-9)或下划线(_)构成，并且必须以字母开头!");
		getObject("User_Account").focus();
		return false;
	} else if (account.length < 3) { // 判断长度
  	    alert("用户名不能少于3位!");
	    getObject("User_Account").focus();
	    return false;
    } else if (account.length > 16) {
  	    alert("用户名不能大于16位!");
	    getObject("User_Account").focus();
	    return false;
    } else if (password1 == "") {
  		alert("密码不能为空");
		getObject("User_Password").focus();
		return false;
    } else if(password1.length < 8) {
  		alert("密码不能少于8位!");
		getObject("User_Password").focus();
		return false;
    } else if (password1 != password2){
  		alert("密码两次输入不同!");
		getObject("User_RePassword").focus();
		return false;
    } else if (password1.indexOf(" ") != -1) {
  		alert("密码不能包含空格!");
		getObject("User_Password").focus();
		return false;
    } else if (!sex) {
	  	alert("您是帅哥还是美女呢？请选择性别!");
		return false;
    } else if (ageIndex == 0) {
	  	alert("对不起，您还没有选择出生年份!");
		getObject("User_Age").focus();
		return false;
    } else if (provinceIndex == 0) {
  		alert("请选择您所在的省份!");
		getObject("User_Shen").focus();
		return false;
    } else if (townIndex == 0) {
  		alert("请选择您所在的地区!");
		getObject("User_Town").focus();
		return false;
    } else if (cityIndex == 0) {
  		alert("请选择您所在的县!");
		getObject("User_City").focus();
		return false;
    } else if ((mail != "") && !isMail(mail)) {
  		alert("您的电子邮箱格式有误，请修改!");
  		getObject("User_Email").focus();
		return false;
    } else if (code == ""){
  		alert("请输入验证码!");
		getObject("number").focus();
		return false;
    } else if (userExists == true) {
    	alert("此用户名已经存在，请换其它用户名再试!");
    	getObject("User_Account").focus();
    	return false;
    }
  
  getObject("subButton").value = " 正在注册... ";
  getObject("subButton").disabled = "disabled";
  return true;
}

function encodeParse(obj) {
    if ((obj == null) || (obj.value == "")) {
    	return false;
    }
    var str = obj.value;
    var result = "";
    for(var i = 0; i < str.length; i++) {
        var c = str.charCodeAt(i);
        if(c == 12288) {
            result += String.fromCharCode(32);
            continue;
        }
        if (c > 65280 && c < 65375) {
            result += String.fromCharCode(c - 65248);
            continue;
        }
        result += String.fromCharCode(c);
    }
    obj.value = result;
    
    return true;
}

function CharMode(iN) {  
	if (iN>=48 && iN <=57) {
		return 1;
	}
	
	if (iN>=65 && iN <=90) {
		return 2; 
	}
	
	if (iN>=97 && iN <=122) {
		return 4; 
	} else  {
		return 8;
	}
}  

function bitTotal(num) {  
	var modes=0;  
	for (i = 0; i < 4; i++) {  
		if (num & 1) {
			modes++; 
		}
		num>>>=1;  
	}
	return modes;  
}  

function checkStrong(sPW){  
	if (sPW.length<=4) {
		return 0;
	}
	var Modes = 0;
	for (i = 0; i < sPW.length; i++) {
		Modes |= CharMode(sPW.charCodeAt(i));
	} 
	
	return bitTotal(Modes);  
}

function changeEmailProvider(tips) {
    var emailProvider = document.getElementById("Email_Provider");
    if (emailProvider) {
    	if (emailProvider.value == "-1") {
    		var container = document.getElementById("Email_Provider_Container");
    		container.innerHTML = '&nbsp;@&nbsp;<input id="Email_Provider" name="Email_Provider" tabindex="8" size="6" onchange="changeEmailProvider(); checkMail();" />';
    	}
    }
}
