if(typeof ghf == "undefined")
{
	ghf = {};
}

ghf.uright = {
	/**
	*	检查是否登录和是否为群成员
	**/
	check : function()
	{
		if($("__ghf_login_pop"))
		{
			GHF.Utils.showDiv("__ghf_login_pop");
		}else if($("__ghf_join_group_pop"))
		{
			GHF.Utils.showDiv("__ghf_join_group_pop");
		}
	},
	/**
	*	没有权限
	**/
	noRight : function()
	{
		if(this.isRight())
		{
			GHF.Utils.showDiv("__ghf_uright_top_pop");
		}
	},
	reg:
	{
		sex : -1,
		/**
		*	准备注册
		**/
		preg : function()
		{
			if($("__ghf_login_pop"))
			{
				if($("__ghf_login_pop").visible())
				{
					$("__ghf_login_pop").hide();
				}
			}
			if($("__ghf_reg_pop_div"))
			{
				GHF.Utils.showDiv("__ghf_reg_pop_div");
			}
		},
		/**
		*	检查邮箱是否合法
		**/
		checkMail : function(func)
		{
			if($("__ghf_reg_mail"))
			{
				$("__ghf_reg_mail").value = $("__ghf_reg_mail").value.replace(/\s*/i,"");
				var strReg = /(youban.com|.com.com|.cn.cn|.con|\s)/i ;	
				if(/(.+)@(.+)\.(.+)/i.test($("__ghf_reg_mail").value) && !strReg.test($("__ghf_reg_mail").value) && !/[\u0391-\uFFE5]/g.test($("__ghf_reg_mail").value))
				{
					new Ajax.Request("/group_utils.php?cmd=isIN",{
						method : "post",
						postBody : "mail=" + $("__ghf_reg_mail").value,
						onSuccess:function(r)
						{
							if(r.responseText == "N")
							{
								if($("__ghf_reg_mail_err"))
								{
									$("__ghf_reg_mail_err").update('');						
								}
								if(func)
								{
									func();
								}
								return true;
							}else
							{
								if($("__ghf_reg_mail_err"))
								{
									$("__ghf_reg_mail_err").update('<img src="images/other/cbox_unchecked.gif" width="18" height="24" class="mr5" /><span class="red">该邮箱已存在</span>');						
								}
								return false;
							}
						}
					});
				}else
				{
					if($("__ghf_reg_mail_err"))
					{
						$("__ghf_reg_mail_err").update('<img src="images/other/cbox_unchecked.gif" width="18" height="24" class="mr5" /><span class="red">邮箱地址不合法</span>');						
					}
					return false;
				}
			}
			if($("__ghf_reg_mail_err"))
			{
				$("__ghf_reg_mail_err").update('<img src="images/other/cbox_unchecked.gif" width="18" height="24" class="mr5" /><span class="red">请输入合法邮箱</span>');
			}
			return false;
		},
		/**
		*	检查密码
		**/
		checkPass : function()
		{
			if($("__ghf_reg_pass1"))
			{
				$("__ghf_reg_pass1").value = $("__ghf_reg_pass1").value.replace(/\s*?/i,"");
				if($("__ghf_reg_pass1").value.length < 4)
				{
					if($("__ghf_reg_pass_err"))
					{
						$("__ghf_reg_pass_err").update('<img src="images/other/cbox_unchecked.gif" width="18" height="24" class="mr5" /><span class="red">密码长度最少4位</span>');
					}
					return false;
				}else
				{
					if(/(\\|\/)/i.test($("__ghf_reg_pass1").value))
					{
						if($("__ghf_reg_pass_err"))
						{
							$("__ghf_reg_pass_err").update('<img src="images/other/cbox_unchecked.gif" width="18" height="24" class="mr5" /><span class="red">密码中不允许出现“\,/”符号</span>');
						}
					}else
					{
						if($("__ghf_reg_pass_err"))
						{
							$("__ghf_reg_pass_err").update('');
						}
						return true;
					}
				}
			}
			$("__ghf_reg_pass_err").update('<img src="images/other/cbox_unchecked.gif" width="18" height="24" class="mr5" /><span class="red">请填写密码</span>');
			return false;
		},
		checkPass2 : function()
		{
			if($("__ghf_reg_pass2"))
			{
				if($("__ghf_reg_pass1"))
				{
					if($("__ghf_reg_pass2").value == $("__ghf_reg_pass1").value)
					{
						if($("__ghf_reg_pass2_err"))
						{
							$("__ghf_reg_pass2_err").update('');
						}
						return true;
					}
				}
			}
			if($("__ghf_reg_pass2_err"))
			{
				$("__ghf_reg_pass2_err").update('<img src="images/other/cbox_unchecked.gif" width="18" height="24" class="mr5" /><span class="red">两次输入的密码不一致</span>');
			}
			
			return false;
		},
		checkName : function()
		{
			if($("__ghf_reg_name"))
			{
				$("__ghf_reg_name").value = $("__ghf_reg_name").value.replace(/\s*?/i,"");
				if($("__ghf_reg_name").value.length < 2)
				{
					if($("__ghf_reg_name_err"))
					{
						$("__ghf_reg_name_err").update('<img src="images/other/cbox_unchecked.gif" width="18" height="24" class="mr5" /><span class="red">请你输入真实的名字</span>');
					}
					return false;
				}else
				{
					for(var i = 0 ; i < $("__ghf_reg_name").value.length ;i++)
					{
						if($("__ghf_reg_name").value.charCodeAt(i) < 19968 || $("__ghf_reg_name").value.charCodeAt(i) > 40869)
						{
							if($("__ghf_reg_name_err"))
							{
								$("__ghf_reg_name_err").update('<img src="images/other/cbox_unchecked.gif" width="18" height="24" class="mr5" /><span class="red">名字只可以是2-4个汉字</span>');
							}
							return false
						}
					}
					$("__ghf_reg_name_err").update("");
					return true;
				}
				
			}
			if($("__ghf_reg_name_err"))
			{
				$("__ghf_reg_name_err").update('<img src="images/other/cbox_unchecked.gif" width="18" height="24" class="mr5" /><span class="red">请输入名字</span>');
			}
			return false;
		},
		/**
		*	检查性别
		**/
		checkSex : function()
		{			
			if($("__ghf_reg_sex_m"))
			{
				if($("__ghf_reg_sex_m").checked)
				{
					ghf.uright.reg.sex = 1;
				}
			}
			if($("__ghf_reg_sex_f"))
			{
				if($("__ghf_reg_sex_f").checked)
				{
					ghf.uright.reg.sex = 0;
				}
			}
			
			if(ghf.uright.reg.sex == -1)
			{				
				if($("__ghf_reg_sex_err"))
				{
					$("__ghf_reg_sex_err").update('<img src="images/other/cbox_unchecked.gif" width="18" height="24" class="mr5" /><span class="red">性别不能为空</span>');
				}
				return false;
			}else
			{
				if($("__ghf_reg_sex_err"))
				{
					$("__ghf_reg_sex_err").update("");
				}
				return true;
			}
		},
		checkAll : function()
		{
			if($("__ghf_reg_tiaokuan"))
			{
				if($("__ghf_reg_tiaokuan").checked)
				{
					if(ghf.uright.reg.checkPass() && ghf.uright.reg.checkPass2() && ghf.uright.reg.checkName() && ghf.uright.reg.checkSex())
					{
						new Ajax.Request("/group_utils.php?cmd=reg",{
							metod : "post",
							postBody : "mail=" + $("__ghf_reg_mail").value + "&name=" + $("__ghf_reg_name").value + "&pass=" + $("__ghf_reg_pass1").value + "&sex=" + ghf.uright.reg.sex + "&home_right=" + $("__ghf_reg_right").options[$("__ghf_reg_right").selectedIndex].value + "&gid=" + $("__ghf_gid_join").value ,
							onSuccess : function(r)
							{
								switch(r.responseText)
								{
									case "-1":
										alert("注册失败");
										break;
									
									default :
										$("__ghf_reg_pop_div").hide();
										if($("__ghf_reg_ok_pop"))
										{
											GHF.Utils.showDiv("__ghf_reg_ok_pop");
											$("__ghf_ok_jihuo").observe("click",function(ev){
												var mail = $("__ghf_reg_mail").value;
												var url = "";
												if(/.+(@gmail.com)/i.test(mail))
												{
													url = "gmail.google.com";
												}else if(/.+(@(hotmail|live).com)/i.test(mail))
												{
													url = "www.hotmail.com";
												}else if(/.+@vip.163.com/i.test(mail))
												{
													url = "vip.163.com";
												}else
												{
													url = mail.replace(/.+@(.+)/i,"mail.$1");
												}
												window.open("http://" + url);
											});
										}
										break;
								}								
							}
						});
					}
				}else
				{
					alert('你尚未同意有伴使用条款');
				}
			}			
		}		
	},
	login : {
		init : function()
		{			
			$("__ghf_login_pop").hide();
		}
	},
	/**
	*	是否有曾在弹出的div在
	**/
	isRight : function()
	{
		if($("__ghf_login_pop"))
		{
			if($("__ghf_login_pop").visible())
			{
				return false;
			}
		}
		if($("__ghf_join_group_pop"))
		{
			if($("__ghf_join_group_pop").visible())
			{
				return false;
			}
		}
		if($("__ghf_join_wait_pop"))
		{
			if($("__ghf_join_wait_pop").visible())
			{
				return false;
			}
		}
		if($("__ghf_leave_ok_pop"))
		{
			if($("__ghf_leave_ok_pop").visible())
			{
				return false;
			}
		}
		if($("__ghf_join_ok_pop"))
		{
			if($("__ghf_join_ok_pop").visible())
			{
				return false;
			}
		}		
		return true;
	}
	
};
