//检验字符传是否邮箱地址
function isEmail(str){
	var reg = /^([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/gi;
	return reg.test(str);
}

//是否合法的红茶id
function isNumberId(str){
	var reg = /^[1-9]\d{4,9}$/;
	return reg.test(str);
}

//检测手机号是否合法，验证13系列和150-159(154除外)、180、185、186、187、188、189几种号码，长度11位 
function isCellphone(num){   
	return (/^13\d{9}$/g.test(num)) || (/^15[0-35-9]\d{8}$/g.test(num)) || (/^18[05-9]\d{8}$/g.test(num));   
}

//加入收藏夹
function addFavorite(tid){
	$.post('/ujax.php?action=favorite.topic.insert', {'topicId':tid}, function(data){
			data = eval("(" + data + ")"); //生成对象
			alert(data.msg);
		});
	//window.external.addFavorite(window.location.href, document.title);	
}

//更改验证码
function showRandImg(){
	document.getElementById("vcimg").src = "/vcode.php?rand" + Math.random();
}

//检测用户登录
function checkLogin(self){
	if(!isEmail(self.umail.value) && !isNumberId(self.umail.value)){
		alert("请输入正确的邮箱地址或红茶ID");
		self.umail.focus();
		return false;
	}
	if(self.pwd.value.length<6){
		alert("密码长度应该大于5位");
		self.pwd.focus();
		return false;
	}
}

//只看主题中的某用户
function onlyLookTopic(uid){
	if(uid == 0){
		alert('只能过滤注册用户发的回帖');
		return false;
	}
	window.location.href = window.location.href.split('?').shift() + '?uid=' + uid;
}

//检测用户登录
function checkBind(self){
	if(!isEmail(self.umail.value)){
		alert("请输入正确的邮箱地址");
		self.umail.focus();
		return false;
	}
	if(self.pwd.value.length<6){
		alert("密码长度应该大于5位");
		self.pwd.focus();
		return false;
	}
	if(self.pwd.value != self.pwd2.value){
		alert("两次密码应一致");
		self.pwd.focus();
		return false;
	}
}

//检测用户发主题
function checkTopicPublish(self){
	len  = self.subject.value.length
	if(len < 4){
		alert("主题长度不能小于4个字符");
		self.subject.focus();
		return false;
	}

	len  = self.message.value.length
	if(len < 6 || len>10000){
		alert("帖子长度需在6-10000个字符之间");
		self.message.focus();
		return false;
	}
	
	len  = self.vcode.value.length
	if(user.vcode && len != 4){
		alert("请输入4位验证码");
		self.vcode.focus();
		return false;
	}
	return true;
}

//检测用户发回帖
function checkPostPublish(self){
	len  = self.message.value.length
	if(len < 6 || len>10000){
		alert("帖子长度需在6-10000个字符之间");
		self.message.focus();
		return false;
	}
	
	len  = self.vcode.value.length
	if(user.vcode && len != 4){
		alert("请输入4位验证码");
		self.vcode.focus();
		return false;
	}
	return true;
}

//显示验证码
function showVcodeTr(){
	if(!user.vcode){return true;} //已登录用户，不调用验证码
	tr = window.document.getElementById("vctr");
	if(tr.className == "vctr"){
		tr.className = "";
		showRandImg();
	}
	return false;
}

//快速提交表单
function quickSubmit(e){
	var ret = false;
	e = e ? e : window.event;
	if(e.ctrlKey && e.keyCode == 13){
		document.getElementById("subbtn").click();
		ret = true;
	}
	return ret;
}

function hcbusHotkey(e){
	var uid = 1;
	e = e ? e : window.event;
	num = e.keyCode;
	if((typeof(HCBUSHOTKEY) == 'string') && e.altKey && ((num >= 48 && num <= 57) || num==45 || (num>=96 && num<=105))){
		num == 45 && (num = 48);
		num >=96 && (num -=48);
		num -=48;
		var keyArr = HCBUSHOTKEY.split('^_^');
		if(keyArr[num]){
			if(parseInt(keyArr[num]) == keyArr[num]){
				window.history.go(keyArr[num]);
			}
			else{
				window.location.href = keyArr[num];
			}
		}
	}
}
//绑定函数
$(function(){
	$(document).bind("keydown", function(event){
		num = event.keyCode;
		if((typeof(HCBUSHOTKEY) == 'string') && event.altKey && ((num >= 48 && num <= 57) || num==45 || (num>=96 && num<=105))){
			num == 45 && (num = 48);
			num >=96 && (num -=48);
			num -=48;
			var keyArr = HCBUSHOTKEY.split('^_^');
			if(num == 0){window.location.reload = '/'; return true;}
			if(num == 1){window.history.go(1); return true;}
			if(num == 2){window.history.go(-1); return true;}
			if(keyArr[num]){
				window.location.href = keyArr[num];
			}
			else{
				//系统设置的埋点，格式<input type="hidden" id="HOTKEY-n" value="url">,如果用户未定义则使用系统默认的
				if(document.getElementById("HOTKEY-"+num)){
					window.location.href = document.getElementById("HOTKEY-"+num).value;
				}
			}
		}
	}); 
});
