
var g_reg_submit = false;
var g_reg_submited = false;
var ga_reg_elements = new Array(
	"email",
	"password",
	"password2",
	"fireol",
	"real_name",
	"gender",
	"birthday",
	"city",
	"status",
	"company",
	"school",
	"baseinfoprivacy",
	"code",
	""
);
var g_reg_checkrealname = false;
var g_reg_isrealname = -1;		//-1/0/1/2 -- 未验证/合法/非法/违禁
var g_reg_emailreged = -1;		//-1/0/1/2 -- 未验证/可用/注册未验证/注册已验证
var g_reg_codevalid = -1;		//-1/0/1 -- 未验证/非法/合法
var g_link_capture = false;
var g_reg_codetext = "";

function changeCode()
{
	document.regform.rcode.value=Math.random();
	var v_randimg = document.getElementById("randimg");
	v_randimg.innerHTML = "<img height=50 src=\"/misu/interface/regcreatepng.jsp?randnum="
		+ document.regform.rcode.value
		+ "\" width=140 /><br /><a href=\"javascript:changeCode();\" title=\"看不清 换一张\">看不清 换一张</a>";
	g_reg_codevalid = -1;
	document.regform.code.value = "";
	document.regform.code.className = "it2";
	document.regform.code.focus();
}

function errorOnblur(v_element, v_obj)
{
	if (g_link_capture)
	{
		return;
	}
	var v_ret = checkregformex(null, false);
	for(var i=0; ga_reg_elements[i].length; i++)
	{
		if (v_ret == v_element)
		{
			return true;
		}
		if (v_ret == ga_reg_elements[i])
		{
			return true;
		}
		if (v_element == ga_reg_elements[i])
		{
			break;
		}
	}
	if (v_element == "birthday")
	{
		showtip_ok(v_element, isNaN($("year").value) || isNaN($("month").value) || isNaN($("day").value));
	}
	else if (v_element == "baseinfoprivacy")
	{
		showtip_ok(v_element, false);
	}
	else if (v_element == "gender" || v_element == "status")
	{
		showtip_ok(v_element, !v_obj.checked);
	}
	else
	{
		showtip_ok(v_element, eval("document.regform." + v_element).value.length == 0);
	}
	return false;
}

function tipWord(v_tipcode)
{
	return $(v_tipcode).innerHTML;
}

function tipWarn()
{
	return '&nbsp;<img src="../i/th_ju.gif" width="18" height="24" vspace="7" />';
}

function tipOk()
{
	return '&nbsp;<img src="../i/fzcg_dh.gif" width="17" height="13" vspace="13" />';
}

function showtip_warn(v_tipname, v_hide)
{
	if (v_tipname == "code")
	{
		return;
	}
	$(v_tipname + "_tip").className = "zc_cwts";
	$(v_tipname + "_tip").innerHTML = tipWarn();
	$(v_tipname + "_tip").style.display = v_hide ? "none" : "block";
}

function showtip_ok(v_tipname, v_hide)
{
	if (eval("document.regform." + v_tipname) && (eval("document.regform." + v_tipname).type == "text" || eval("document.regform." + v_tipname).type == "password"))
	{
		eval("document.regform." + v_tipname).className = "it1";
	}
	$(v_tipname + "_tip2").style.display = "none";
	if (v_tipname == "code")
	{
		return;
	}
	$(v_tipname + "_tip").className = "zc_cwts";
	$(v_tipname + "_tip").innerHTML = tipOk();
	$(v_tipname + "_tip").style.display = v_hide ? "none" : "block";
}

function showtip(v_tipname, v_error , v_tipcode)
{
	if (!v_error)
	{
		for(i=0; ga_reg_elements[i].length; i++)
		{
			if (!$(ga_reg_elements[i] + "_tip2"))
			{
				continue;
			}
			if ($(ga_reg_elements[i] + "_tip2").style.display == "block"
				&& v_tipname != ga_reg_elements[i])
			{
				return;
			}
		}
	}

	for(i=0; ga_reg_elements[i].length; i++)
	{
		if (!$(ga_reg_elements[i] + "_tip2"))
		{
			continue;
		}
		if (v_tipname == ga_reg_elements[i])
		{
			$(ga_reg_elements[i] + "_tip2").innerHTML = tipWord(v_tipcode);
			
			if (v_error)
			{
				if (eval("document.regform." + ga_reg_elements[i]) && (eval("document.regform." + ga_reg_elements[i]).type == "text" || eval("document.regform." + ga_reg_elements[i]).type == "password"))
				{
					eval("document.regform." + ga_reg_elements[i]).className = "it3";
				}
			}
			$(ga_reg_elements[i] + "_tip2").style.display = "block";
			showtip_warn(v_tipname, !v_error);
		}
		else
		{
			$(ga_reg_elements[i] + "_tip2").style.display = "none";
		}
	}
}


function codeOnfocus(obj)
{
	if (obj.className == "it1")
	{
		obj.className = "it2";
	}
	if(obj.value.length == 0 && !g_reg_submit)
	{
		showtip("code", false , "html_code1");
	}
}

function codeOnblur(obj)
{
	if (obj.className == "it2")
	{
		obj.className = "it1";
	}
	if (obj.value.length > 0)
	{
		codeAjaxSubmit(obj.value);
	}
	errorOnblur("code", obj);
}

function codeOnchange(obj)
{
	g_reg_codevalid = -1;
	codeOnblur(obj);
}

function codeAjaxSubmit(text)
{
	var url = "/misu/interface/checkcode.jsp";
	var text = encodeURIComponent(text);
	
	if (text == g_reg_codetext)
	{
		return;
	}
	g_reg_codetext = text;

	var pars = "code=" + text + "&rcode=" + document.regform.rcode.value;
	var myAjax = new Ajax.Request(url, {method: "post", parameters: pars, onComplete: function (req) { codeAjaxShow(req); } });
}


function codeAjaxShow(req)
{
	g_reg_codevalid = req.responseText;
	if(g_reg_codevalid == 1)
	{
		showtip_ok("code" , false);
	}
	else if(g_reg_codevalid == 0)
	{
		showtip("code", true , "html_code2");
	}
	
	if (g_reg_submit)
	{
		var v_ret = checkregformex(document.regform, true);
		if (v_ret.length == 0)
		{
			if (g_reg_codevalid != 1)
			{
				g_reg_submit = false;
			}
			if (g_reg_codevalid == 1 && g_reg_emailreged == 0 && !g_reg_submited && (g_reg_isrealname == 0 || !g_reg_checkrealname))
			{
				g_reg_submited = true;
				document.regform.submit();
			}
		}
	}
}

function baseinfoprivacyOnchange(obj)
{
	if (obj.value == 0)
	{
		obj.value = 1;
		alert("    为了防止陌生人恶意获取你的资料，对你进行骚扰或其它举动，\n请先不要将隐私权限设为“任何人可见”。\n\n    等你对米鼠网平台熟悉以后，可以随时修改隐私权限的设置。");
	}
}

function baseinfoprivacyOnfocus(obj)
{
	if(!g_reg_submit)
	{
	}
}

function baseinfoprivacyOnblur(obj)
{
	errorOnblur("baseinfoprivacy", obj);
}

function schoolOnfocus(obj)
{
	if (obj.className == "it1")
	{
		obj.className = "it2";
	}
	if(obj.value.length == 0 && !g_reg_submit)
	{
	}
}

function schoolOnblur(obj)
{
	if (obj.className == "it2")
	{
		obj.className = "it1";
	}
	errorOnblur("school", obj);
}

function schoolOnchange(obj)
{
	errorOnblur("school", obj);
}

function companyOnfocus(obj)
{
	if (obj.className == "it1")
	{
		obj.className = "it2";
	}
	if(obj.value.length == 0 && !g_reg_submit)
	{
	}
}

function companyOnblur(obj)
{
	if (obj.className == "it2")
	{
		obj.className = "it1";
	}
	errorOnblur("company", obj);
}

function statusOnfocus(obj)
{
	if (!obj.checked && !g_reg_submit)
	{
	}
}

function statusOnblur(obj)
{
	errorOnblur("status", obj);
}

function cityOnfocus(obj)
{
	if (obj.className == "it1")
	{
		obj.className = "it2";
	}
	if(obj.value.length == 0 && !g_reg_submit)
	{
	}
}

function cityOnblur(obj)
{
	if (obj.className == "it2")
	{
		obj.className = "it1";
	}
	errorOnblur("city", obj);
}

function birthdayOnchange(obj)
{
	var year = $("year").value;
	if(year == "year")
		return;
	else
		year = parseInt(year , 10);
	
	var month = $("month").value;
	if(month == "month")
		return;
	else
		month = parseInt(month , 10);
	
	var day = $("day").value;
	var wholeday = getDays(year , month);

	if(1)
	{
		var options = $("day").options;
		for(var i=1 ; i<=wholeday ; i++)	
		{
			var j = i.toString();
			j = j.length==1 ? "0" + j : j;
			options.length = i+1;
			options[i].value = j;
			options[i].text = j;
			if(day <= wholeday && i==day)
			{
				options[i].selected = true;
			}
		}
	}
}

function birthdayOnfocus(obj)
{
	if((isNaN($("year").value) || isNaN($("month").value) || isNaN($("day").value)) && !g_reg_submit)
	{
	}
}

function birthdayOnblur(obj)
{
	//errorOnblur("birthday", obj);
	g_reg_birthday = "";
	if (!isNaN($("year").value))
	{
		g_reg_birthday += $("year").value;
		if (!isNaN($("month").value))
		{
			g_reg_birthday += $("month").value;
			if (!isNaN($("day").value))
			{
				g_reg_birthday += $("day").value;
			}
		}
	}
}

function genderOnfocus(obj)
{
	if (!obj.checked && !g_reg_submit)
	{
	}
}

function genderOnblur(obj)
{
	errorOnblur("gender", obj);
}

function realNameOnfocus(obj)
{
	if (obj.className == "it1")
	{
		obj.className = "it2";
	}
	if(obj.value.length == 0 && !g_reg_submit)
	{
		showtip("real_name", false , "html_real_name1");
	}
}

function realNameOnblur(obj)
{
	if (obj.className == "it2")
	{
		obj.className = "it1";
	}
	if (g_reg_checkrealname)
	{
		realNameAjaxSubmit(obj.value);
	}
	errorOnblur("real_name", obj);
	g_reg_realname = obj.value;
}

function realNameOnchange(obj)
{
	g_reg_isrealname = -1;
}

function realNameAjaxSubmit(text)
{
	var url = "/misu/interface/checkname.jsp";
	var text = encodeURIComponent(text);

	var pars = "mode=detail&real_name=" + text;
	var myAjax = new Ajax.Request(url, {method: "post", parameters: pars, onComplete: function (req) { realNameAjaxShow(req); } });
}

function realNameAjaxShow(req)
{
	g_reg_isrealname = parseInt(req.responseText , 10);
	if(g_reg_isrealname == 0)
	{
		showtip_ok("real_name" , false);
	}
	else if(g_reg_isrealname == 3)
	{
		showtip("real_name", true , "html_real_name3");
	}
	else if(g_reg_isrealname == 2)
	{
		showtip("real_name", true , "html_real_name2");
	}
	else 
	{
		showtip("real_name", true , "html_real_name1");
	}
	
	if (g_reg_submit)
	{
		var v_ret = checkregformex(document.regform, true);
		if (v_ret.length == 0)
		{
			if (g_reg_isrealname != 0)
			{
				g_reg_submit = false;
			}
			if (g_reg_codevalid == 1 && g_reg_emailreged == 0 && !g_reg_submited && (g_reg_isrealname == 0 || !g_reg_checkrealname))
			{
				g_reg_submited = true;
				document.regform.submit();
			}
		}
	}
}

function password2Onfocus(obj)
{
	if (obj.className == "it1")
	{
		obj.className = "it2";
	}
	if(obj.value.length == 0 && !g_reg_submit)
	{
	}
}

function password2Onblur(obj)
{
	if (obj.className == "it2")
	{
		obj.className = "it1";
	}
	errorOnblur("password2", obj);
}

function passwordOnkeydown(obj, evnt)
{
	if(evnt.keyCode == 9 && evnt.shiftKey)
	{
		document.regform.email.focus();
		return false;
	}
	return true;
}

function passwordOnfocus(obj)
{
	if (obj.className == "it1")
	{
		obj.className = "it2";
	}
	if(obj.value.length == 0 && !g_reg_submit)
	{
		showtip("password", false , "html_password1");
	}
}

function passwordOnblur(obj)
{
	if (obj.className == "it2")
	{
		obj.className = "it1";
	}
	errorOnblur("password", obj);
}

function passwordOnkeyup(obj)
{
	if (obj.value.length >= 6)
	{
		var weight = passwordCheck(obj.value);
		showtip("password", false , "html_password"+(6-weight).toString());
	}
	else if (obj.value.length >= 1)
	{
		showtip("password", false , "html_password1");
	}
}

function emailOnkeydown(obj, evnt)
{
	if(evnt.keyCode == 9 && !evnt.shiftKey)
	{
		document.regform.password.focus();
		return false;
	}
	return true;
}

function emailOnfocus(obj)
{
	if (obj.className == "it1")
	{
		obj.className = "it2";
	}
	if (0 == obj.value.length && !g_reg_submit)
	{
		showtip("email", false , "html_email1");
	}
}

function emailOnblur(obj)
{
	if (obj.className == "it2")
	{
		obj.className = "it1";
	}
	if(emailCheck(obj.value))
	{
		emailAjaxSubmit(obj.value);
	}
	errorOnblur("email", obj);
}

function linkOnmouseover()
{
	g_link_capture = true;
}

function linkOnmouseout()
{
	g_link_capture = false;
}

function emailOnchange(obj)
{
	g_reg_emailreged = -1;
}

function emailCheck(email)
{
	if ((email.length > 128) || (email.length < 6))
	{
		return false;
	}

	var format = /^[A-Za-z0-9+]+[A-Za-z0-9\.\_\-+]*@([A-Za-z0-9\-]+\.)+[A-Za-z0-9]+$/;
	if (!email.trim().match(format))
	{
		return false; 
	}
	
	return true;
}

function emailAjaxSubmit(text)
{
	var url = "/misu/interface/checkemail.jsp";
	var text = encodeURIComponent(text);

	var pars = "email=" + text;
	var myAjax = new Ajax.Request(url, {method: "post", parameters: pars, onComplete: function (req) { emailAjaxShow(req); } });
}


function emailAjaxShow(req)
{
	g_reg_emailreged = req.responseText;
	if(g_reg_emailreged == 2)	
	{
		showtip("email", true , "html_email3");

	}
	else if(g_reg_emailreged == 1)
	{
		showtip("email", true , "html_email4");
	}
	else if(g_reg_emailreged == 0)
	{
		showtip_ok("email" , false);
	}
	else if (g_reg_emailreged == 3)
	{
		showtip("email", true , "html_email6");
	}
	
	if (g_reg_submit)
	{
		var v_ret = checkregformex(document.regform, true);
		if (v_ret.length == 0)
		{
			if (g_reg_emailreged != 0)
			{
				g_reg_submit = false;
			}
			if (g_reg_codevalid == 1 && g_reg_emailreged == 0 && !g_reg_submited && (g_reg_isrealname == 0 || !g_reg_checkrealname))
			{
				g_reg_submited = true;
				document.regform.submit();
			}
		}
	}
}

function tagokregform()
{
	if (eval("document.regform." + "password").value.length >= 6)
	{
		showtip_ok("password", false);
	}
	else if (eval("document.regform." + "password").value.length > 0)
	{
		showtip_warn("password", false);
	}
	else
	{
		showtip_ok("password", true);
	}

	if (eval("document.regform." + "password").value.length >= 6
		&& eval("document.regform." + "password").value == eval("document.regform." + "password2").value)
	{
		showtip_ok("password2", false);
	}
	else if (eval("document.regform." + "password2").value.length > 0)
	{
		showtip_warn("password2", false);
	}
	else
	{
		showtip_ok("password2", true);
	}
	
	if (document.regform.gender[0].checked || document.regform.gender[1].checked)
	{
		showtip_ok("gender", false);
	}
	
	if (!isNaN($('year').value) && !isNaN($('month').value) && !isNaN($('day').value))
	{
		showtip_ok("birthday", false);
	}
	else if (!isNaN($('year').value) || !isNaN($('month').value) || !isNaN($('day').value))
	{
		showtip_warn("birthday", false);
	}
	else
	{
		showtip_ok("birthday", true);
	}

	if ($("city").value.length)
	{
		showtip_ok("city", false);
	}
	else
	{
		showtip_ok("city", true);
	}

	if (document.regform.status[0].checked || document.regform.status[1].checked || document.regform.status[2].checked)
	{
		showtip_ok("status", false);
	}

	if ($("company").value.length)
	{
		showtip_ok("company", false);
	}
	else
	{
		showtip_ok("company", true);
	}

	var schoolvalue = "";
	if ($("school").options)
	{
		schoolvalue = $("school").options[$("school").selectedIndex].value;
	}
	else
	{
		schoolvalue = $("school").value;
	}
	if (schoolvalue.length)
	{
		showtip_ok("school", false);
	}
	else
	{
		showtip_ok("school", true);
	}
}

function checkregformex(v_form, v_submit)
{
	if (!v_submit)
	{
		tagokregform();
	}

	if (eval("document.regform." + "email").value.length == 0)
	{
		if (v_submit)
		{
			showtip("email", true , "html_email5");
			return "email";
		}
	}
	else if (!emailCheck(eval("document.regform." + "email").value))
	{
		showtip("email",true , "html_email2");
		return "email";
	}
	else if(g_reg_emailreged == 2)
	{
		showtip("email",true , "html_email3");
		return "email";
	}
	else if(g_reg_emailreged == 1)
	{
		showtip("email",true , "html_email4");
		return "email";
	}

	if (eval("document.regform." + "password").value.length == 0)
	{
		if (v_submit)
		{
			showtip("password", true , "html_password7");
			return "password";
		}
	}
	else if (eval("document.regform." + "password").value.length < 6)
	{
		showtip("password", true , "html_password6");
		return "password";
	}

	if ($("password2").value.length == 0)
	{
		if (v_submit)
		{
			showtip("password2", true , "html_password9");
			return "password2";
		}
	}
	else if ($("password2").value != eval("document.regform." + "password").value)
	{
		showtip("password2", true , "html_password8");
		return "password2";
	}

	if (typeof(checkregformex_ex1) == "function")
	{
		var v_ret = checkregformex_ex1(v_form, v_submit);
		if (v_ret.length)
		{
			return v_ret;
		}
	}

	if ($("real_name").value.length == 0)
	{
		if (v_submit)
		{
			showtip("real_name", true , "html_real_name2");
			return "real_name";
		}
	}
	else if (b_strlen($("real_name").value) > 12)
	{
		showtip("real_name", true , "html_real_name2");
		return "real_name";
	}
	if (v_submit)
	{
		if (!v_form.gender[0].checked && !v_form.gender[1].checked)
		{
			showtip("gender", true , "html_gender1");
			return "gender";
		}
	}

	if (v_submit)
	{
		if (isNaN(v_form.year.value) || isNaN(v_form.month.value) || isNaN(v_form.day.value))
		{
			showtip("birthday", true , "html_birthday1");
			return "birthday";
		}
	}
	else
	{
		if (isNaN($("year").value) || isNaN($("month").value) || isNaN($("day").value))
		{
			if (!(isNaN($("year").value) && isNaN($("month").value) && isNaN($("day").value)))
			{
				showtip("birthday", true , "html_birthday1");
				return "birthday"; 
			}
		}
	}

	if ($("city").value.length == 0)
	{
		if (v_submit)
		{
			showtip("city", true , "html_city1");
			return "city";
		}
	}

	if (v_submit)
	{
		if (!v_form.status[0].checked && !v_form.status[1].checked && !v_form.status[2].checked)
		{
			showtip("status", true , "html_status1");
			return "status";
		}
	}

	if ($("company").value.length == 0 && document.regform.status[0].checked)
	{
		if (v_submit)
		{
			showtip("company", true , "html_company1");
			return "company";
		}
	}

	var schoolvalue = "";
	if ($("school").options)
	{
		schoolvalue = $("school").options[$("school").selectedIndex].value;
	}
	else
	{
		schoolvalue = $("school").value;
	}
	if (schoolvalue.length == 0 && document.regform.status[1].checked)
	{
		if (v_submit)
		{
			showtip("school", true , "html_school1");
			return "school";
		}
	}

	if ($("code").value.length == 0)
	{
		if (v_submit)
		{
			showtip("code", true , "html_code3");
			return "code";
		}
	}
	else if (g_reg_codevalid == 0)
	{
		showtip("code", true , "html_code2");
		return "code";
	}

	if (typeof(checkregformex_ex2) == "function")
	{
		var v_ret = checkregformex_ex2(v_form, v_submit);
		if (v_ret.length)
		{
			return v_ret;
		}
	}

	return "";
}

function checkregform(v_form)
{
	g_reg_submit = true;
	var v_ret = checkregformex(v_form, true);
	if (v_ret.length == 0)
	{
		if (g_reg_codevalid == 1 && g_reg_emailreged == 0 && !g_reg_submited && (g_reg_isrealname == 0 || !g_reg_checkrealname))
		{
			g_reg_submited = true;
			return true;
		}
		else
		{
			return false;
		}
	}
	if (eval("document.regform." + v_ret) && (eval("document.regform." + v_ret).type == "text" || eval("document.regform." + v_ret).type == "password"))
	{
		eval("document.regform." + v_ret).focus();
	}
	g_reg_submit = false;
	return false;
}


