window.onerror = function(){
	return false;
}
//验证值是否有效
function fValue(obj){
	if(obj.value == ""){
		obj.focus();
		return false;
	}else{
		return true;
	}
}
//验证email地址
function fEmail(obj){
	var pattern = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
	if(obj.value != "" && !(pattern.exec(obj.value))){
		obj.focus();
		return false;
	}else{
		return true;
	}
}
//验证是否是非负整数
function fNum(obj){
	var pattern = /^\d+$/;
	if(!pattern.exec(obj.value)){
		obj.focus();
		return false;
	}else{
		return true;
	}
}
//验证url地址
function fUrl(obj){
	var pattern = /((http|ftp):\/\/)(((([\d]+\.)+){3}[\d]+(\/[\w.\/]+)?)|([a-z]\w*((\.\w+)+){2,})([\/][\w.~]*)*)/;
	if(obj.value != "" && !(pattern.exec(obj.value))){
		obj.focus();	
		return false;
	}else{
		return true;
	}
}

//删除确定
function fConfirm(url){
	if(confirm("确定删除，不可恢复。")){
		self.location=url;
	}
}
function fHandleForm(oForm){
	try{
		var obj = $("explain");
		if(oForm.isSubmit){
			obj.innerHTML = "处理中，请不要重复提交";
			return false;
		}else{
			oForm.button.disabled = true;
			obj.style.display = "";
			obj.innerHTML = "正在提交，请稍后……";
			oForm.isSubmit = true;
			return true;
		}
	}catch(exp){
		return true;	
	}
	
}
//header头部搜索处理
function fSearchFocus(oInput){
	oInput.value = "";
	oInput.style.color="";
}
function fSearchBlur(oInput){
	if(!oInput.value){
		oInput.value = oInput.defaultValue;
		oInput.style.color="#ccc";
	}	
}
function fCustomAlert(str){
	try{
		var oP = $("explain");
		oP.innerHTML = str;
		oP.className = " lineresult";
	}catch(exp){
		alert(str);	
	}	
}

//注册处理
function fReg(oForm){
	if (!fValue(oForm.username)){
		fCustomAlert("请输入用户名");
		return false;
	}
	if (!fValue(oForm.password)){
		fCustomAlert("请输入密码");
		return false;
	}
	if (!fValue(oForm.password2) || oForm.password.value != oForm.password2.value){
		fCustomAlert("两次密码不一致");
		return false;
	}
	if (oForm.question && !fValue(oForm.question)){
		fCustomAlert("请输入密码问题");
		return false;
	}
	if (oForm.answer && !fValue(oForm.answer)){
		fCustomAlert("请输入问题答案");
		return false;
	}
	if(!fValue(oForm.email)){
		fCustomAlert("请输入邮箱");
		return false;
	}
	if(!fEmail(oForm.email)){
		fCustomAlert("请正确输入邮箱地址");
		return false;
	}
	if(!fNum(oForm.checkCode)){
		fCustomAlert("请正确填写验证码");
		return false;
	}
	return fHandleForm(oForm);
}
//链接处理
function fLink(oForm){
	if (!fValue(oForm.title)){
		fCustomAlert("请输入网站名");
		return false;
	}
	if(oForm.url.value=="http://"){
		oForm.url.value = "";	
	}
	if (!fValue(oForm.url)){
		fCustomAlert("请输入网站地址");
		return false;
	}
	if(!fIsUrl(oForm.url.value)){
		fCustomAlert("请输入正确的网站地址");
		return false;
	}
	if(oForm.logo.value=="http://"){
		oForm.logo.value = "";	
	}
	if(oForm.logo.value.length>0&&!fIsUrl(oForm.url.value)){
		fCustomAlert("请输入正确的标识地址");
		return false;
	}
	if(!fNum(oForm.checkCode)){
		fCustomAlert("请正确填写验证码");
		return false;
	}
	if(!oForm.agree.checked){
		fCustomAlert("请勾选同意交换协议");
		return false;
	}
	return fHandleForm(oForm);
}
//登录处理
function fLogin(oForm){
	if (!fValue(oForm.username)){
		fCustomAlert("请输入用户名");
		return false;
	}
	if (!fValue(oForm.password)){
		fCustomAlert("请输入密码");
		return false;
	}
	if(!fNum(oForm.checkCode)){
		fCustomAlert("请正确填写注册码");
		return false;
	}
	return fHandleForm(oForm);
}

function fBaike(oForm){	
	if(oForm.title && !fValue(oForm.title)){
		fCustomAlert("请输入词条名")
		return false;
	}
	/*
	if(!fValue(oForm.tags)){
		fCustomAlert("请输入标签")
		return false;
	}
	*/
	return fHandleForm(oForm);
}

function fXiaode(oForm){
	if(oForm.title && !fValue(oForm.title)){
		fCustomAlert("请输问题标题");
		return false;
	}
	return fHandleForm(oForm);
}

function fQuan(oForm){
	if(oForm.title && !fValue(oForm.title)){
		fCustomAlert("请输圈圈标题");
		return false;
	}
	return fHandleForm(oForm);
}

function fSms(oForm){
	
	if(!fValue(oForm.touser)){
		fCustomAlert("请输收件人");
		return false;
	}
	if(oForm.title && !fValue(oForm.title)){
		fCustomAlert("请输短信标题");
		return false;
	}
	if(!fValue(oForm.content)){
		fCustomAlert("请输短信内容");
		return false;
	}
	return true;
}

function fInfo(oForm){
	if(!fValue(oForm.email)){
		fCustomAlert("请输入邮箱");
		return false;
	}
	if(!fEmail(oForm.email)){
		fCustomAlert("请正确输入邮箱地址");
		return false;
	}
	return fHandleForm(oForm);
}
function fPassword(oForm){
	if(!fValue(oForm.passwordOld)){
		fCustomAlert("请输入旧密码");
		return false;
	}
	if(!fValue(oForm.passwordNew1)){
		fCustomAlert("请输入新密码");
		return false;
	}
	if(!fValue(oForm.passwordNew2)){
		fCustomAlert("请输入新密码");
		return false;
	}
	if(oForm.passwordNew1.value!=oForm.passwordNew2.value){
		fCustomAlert("新密码不一致，请重新输入");
		return false;
	}
	return fHandleForm(oForm);
}

function fBaikeAdmin(oForm){
	var oDes = oForm.description;
	if(oDes.value.length > 255){
		oDes.value = oDes.value.substring(0,255);	
	}
	return true;
}

function fChangeTag(sType){
	if(!window.tagName){
		window.tagName = "baike";	
	}
	if(!sType || window.tagName == sType){
		return;	
	}
	var aType = ["baike","xiaode","quan"];
	var oTagDiv = $("tagDiv");
	var oPointerDiv = $("pointerDiv");
	for(var i=0;i<aType.length;i++){
		var oLi = $(aType[i]+"Li");
		var oDiv = $(aType[i]+"Div");
		if(sType == aType[i]){
			oLi.className = "on";
			oDiv.style.display = "";
			oLi.appendChild(oPointerDiv);
			oTagDiv.className = oTagDiv.className.replace("m"+window.tagName.upFirstChar(),"m"+aType[i].upFirstChar());
			window.tagName = aType[i];
		}else{
			oLi.className = "";
			oDiv.style.display = "none";
		}
	}	
}
function fChangeIndexTag(sType){
	if(!window.tagName){
		window.tagName = "baike";
	}
	if(!sType || window.tagName == sType){
		return;	
	}
	var oDiv = $("indexFormDiv");
	var oForm = $("searchForm");
	oDiv.className = sType+"Form";
	oForm.action = "/"+sType+"/list.asp";
	try{
		var oHrefDiv = $("hotKeywordDiv");
		var aHrefList = oHrefDiv.getElementsByTagName("A");
		for(var i=0,m=aHrefList.length;i<m;i++){
			aHrefList[i].href = aHrefList[i].href.replace(window.tagName,sType);	
		}
	}catch(exp){}
	window.tagName = sType;
	if(oForm.tags.value.length > 0){
		window.searchForm = oForm;
		window.setTimeout("window.searchForm.searchButton.click()",50);
	}	
}


function fInitKindSelect(aList,fid,kid){

	if(aList){
		window.kindList = aList;
	}else{
		aList = window.kindList;
	}
	if(aList.length > 0){
		if(fid == 0){
			fid = aList[0].id;
		}
		var oFid = $("fid");
		var oKid = $("kid");
		var nFindex = 0;
		var nKindex = 0;
		oFid.length = 0;
		oKid.length = 0;
		for(var i=0,m=aList.length;i<m;i++){
			oFid.options[oFid.options.length] = new Option(aList[i].name,aList[i].id);
			if(fid == aList[i].id){
				nFindex = i;
				var aSonList = aList[i].list;
				for(var j=0,n=aSonList.length;j<n;j++){
					oKid.options[oKid.options.length] = new Option(aSonList[j].name,aSonList[j].id);
					if(kid == aSonList[j].id){
						nKindex = j;
					}
				}
			}
		}
		oFid.selectedIndex = nFindex;
		oKid.selectedIndex = nKindex;
	}
}
function fVote(sType,nId,sVote,nIndex){
	var sVoteId = "";
	var sHrefId = "";
	switch(sType){
		case "wiki":
		case "answer":
		case "quan":
			sVoteId = sType+"VoteDiv"
			sHrefId = sType+"HrefDiv"
			break;
		case "wikiVersion":
		case "answerReply":
		case "quanReply":
			sVoteId = sType+"VoteDiv_"+nIndex;
			sHrefId = sType+"HrefDiv_"+nIndex;
			break;
	};
	try{
		var oVoteDiv = $(sVoteId);
		var oHrefDiv = $(sHrefId);
		oVoteDiv.innerHTML = oVoteDiv.innerHTML - 0 + (sVote == "up" ? 1 : -1);
		oHrefDiv.innerHTML = sVote == "up" ? "已顶" : "已踩";
	}catch(exp){
	}
	var sId = sVoteId+"_Frame";
	var sUrl = "/custom.asp?action=vote&type="+sType+"&id="+nId+"&vote="+sVote;
	fGet(sId,sUrl);
}
//公共请求隐藏页面方法
function fGet(sId,sUrl){
	var oIframe = $(sId);
	if(!oIframe){
		oIframe = document.createElement("IFRAME");
		oIframe.style.display = "none";
		document.body.appendChild(oIframe);
	}
	sUrl = sUrl + (sUrl.indexOf("?")>0?"&":"?") + (new Date()).getTime();
	oIframe.src = sUrl;
}
//设置个人中心的iframe高度
function fResetFrame(sName){
	var oDiv = document.getElementById(sName+"Div");
	var oFrame = top.document.getElementById(sName+"Frame");
	oFrame.height = oDiv.offsetHeight;
}
//阅读sms
function fToggleSms(id){
	var oInfoTr = $("smsInfoTr_"+id);
	var oContentTr = $("smsContentTr_"+id);
	if(oContentTr.style.display == "none"){
		var oTd = oInfoTr.firstChild;
		if(oTd.className.indexOf("new")>-1){
			oTd.className = oTd.className.replace("new","");
			fGet("smsFrame","message.asp?action=read&id="+id);
		}
		oContentTr.style.display = "";
	}else{
		oContentTr.style.display = "none";
	}
	fResetFrame("message");
}
//设置激活标签
function fResetHref(sName){
	var sSearch = location.search;
	var oHref;
	if(sName == "message"){
		switch(sSearch){
			case "?floder=Sender":
				oHref = $("SenderHref");
				break;
			case "?floder=Write" :
				oHref = $("WriteHref");
				break;
			default :
				oHref = $("InboxHref")
		}
	}else if(sName == "article"){
		switch(sSearch){
			case "?kind=wiki&attention=true":
				oHref = $("wikiattentionHref");
				break;
			case "?kind=answer" :
				oHref = $("answerHref");
				break;
			case "?kind=answer&attention=true":
				oHref = $("answerattentionHref");
				break;
			case "?kind=quan" :
				oHref = $("quanHref");
				break;
			case "?kind=quan&attention=true":
				oHref = $("quanattentionHref");
				break;
			default :
				oHref = $("wikiHref");
		}	
	}	
	if(oHref){
		oHref.style.color = "#C00";	
	}
}
function fUpload(sType,sInfo){
	if(sType == "wrong"){
		fCustomAlert(sInfo);
	}else{
		window.parent.$("userface").value = sInfo;
		window.parent.$("userfaceShow").src = sInfo;
	}
	location.href="upload.asp";
}

function fSearchBar(oForm){
	if(/在最成都.+中搜索/.test(oForm.tags.value)){
		oForm.tags.value = "";
	}
	oForm.tags.value = oForm.tags.value.encodeHTML();
	if(oForm.tags.value==""){
		oForm.action = oForm.action.replace(/(\/.+\/)list.asp/gi,"$1");
	}
	oForm.searchButton.disabled = true;
	return true;
}
function fBaiduBar(oForm){
	oBaiduForm = $("baiduForm");
	oBaiduForm.q.value = oForm.tags.value;
	oBaiduForm.submit();
}
function fSetImage(oImg,nWidth,nHeight){
	if(nWidth == 0){
		var nW = oImg.width;
	}else{
		var nW = nWidth;
	}
	if(nHeight == 0){
		var nH = oImg.height;
	}else{
		var nH = nHeight;
	}

	var p = (oImg.clientHeight-0) / (oImg.clientWidth-0);//默认高宽比

	var h = nW * p;//按宽缩小后的高
	var w = nH / p;//按高缩小后的宽
	
	if(w > nW){//如果按照高来缩放，宽超出要求
		oImg.width = nW;//宽就是默认的
		oImg.height = Math.round(h);//按照宽的要求来缩放
	}
	
	if(h > nH){//如果按照宽来缩放，高超出要求
		oImg.width = Math.round(w);//按照高的要求来缩放
		oImg.height = nH;//高就是默认的
	}
}
function fSetReadImg(){

	if(window.location.href.indexOf("read.asp")>-1){
		var aList = document.getElementsByTagName("IMG");
		for(var i=0,m=aList.length;i<m;i++){
			fSetImage(aList[i],620,0);
		}
	}	
}
function fInitSelect(){
	var oSelect = $("pickuid");
	var aOptions = oSelect.options;
	if(aOptions.length>1){
		var nRnd = fGetRnd(aOptions.length-1);
		oSelect.selectedIndex=nRnd+1;
	}
}
function fChangeSelect(){
	var oSelect = $("pickuid");
	var oInput = $("pickname");
	var aOptions = oSelect.options;
	if(aOptions.length>1){
		for(var i=0;i<aOptions.length;i++){
			if(aOptions[i].text==oInput.value){
				oSelect.selectedIndex = i;
				oInput.value = "";
				break;
			}
		}
	}
	
}
function fGetRnd(num){
	if(num < 1){
		return 0;
	}else{
		return Math.round((num-1)*Math.random());
	}
};

EV.observe(window,"load",fSetReadImg,false);
if(location.host.toLowerCase()=="xiaode.chengtu.com"){
	location.href = location.href.toLowerCase().replace("xiaode.","");	
}

function fIsUrl(sUrl){
  var strRegex = "^((https|http)?://)" 
  		+ "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" //ftp的user@ 
        + "(([0-9]{1,3}.){3}[0-9]{1,3}" // IP形式的URL- 199.194.52.184 
        + "|" // 允许IP和DOMAIN（域名）
        + "([0-9a-z_!~*'()-]+.)*" // 域名- www. 
        + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]." // 二级域名 
        + "[a-z]{2,6})" // first level domain- .com or .museum 
        + "(:[0-9]{1,4})?" // 端口- :80 
        + "((/?)|" // a slash isn't required if there is no file name 
        + "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$"; 
        var re=new RegExp(strRegex); 
  //re.test()
        if (re.test(sUrl)){
            return (true); 
        }else{ 
            return (false); 
        }
    }
