var ReplyOnce=0; //在每个问题中是否每个人只能回答一次, 1只回答一,0可以回答多次
var OnlyZJCanReply=0; //是否只有专家可以回答问题，1只有专家可以回复，其他类型用户不能回答，0都可以回答
var GuestShowPerson=1; //游客是否可以查看会员资料，1可以，0只有登录会员才可查看

$(document).ready(function(){
	inputfocus();
})

function inputfocus(){
	//if ($.browser.msie){
	if (jQuery.browser.msie){	
		$("input[@type='text'], input[@type='password'], textarea").focus(function(){$(this).addClass("iefocus")}).blur(function(){$(this).removeClass("iefocus")});
	}
}

function getCookie(aFolder,aName){
	var sSearch=" "+aFolder+"=";
	var acookie=" "+window.document.cookie+";";
	if(acookie.length>0){
		var iOffset=acookie.indexOf(sSearch);
		if(iOffset!=-1){
			iOffset+=sSearch.length;
			var iEnd=acookie.indexOf(";",iOffset);
			if(iEnd==-1){iEnd=acookie.length;}
			acookie="&"+acookie.substring(iOffset,iEnd)+"&";
			sSearch="&"+aName+"=";
			iOffset=acookie.indexOf(sSearch);
			if(iOffset!=-1){
				iOffset+=sSearch.length;
				iEnd=acookie.indexOf("&",iOffset);
				if(iEnd==-1){iEnd=acookie.length;}
				var s=acookie.substring(iOffset,iEnd);
				if(IsNumeric(s))return s;
			}
		}
	}
	return null;
}

var lastUID="ready";
function getUserID(){
	if(lastUID=="ready") lastUID=getCookie("oASK", "UID");
	return lastUID;
}

function IsLogined(){
  if(getUserID()){return true;}else{return false;}
}

function check_loginform(f){
if(f.name.value==""||f.name.value.length<=0){alert("请输入用户名");f.name.focus();return false;}
if(f.pwd.value==""||f.pwd.value.length<=0){alert("请输入密码");f.pwd.focus();return false;}
if(f.vcode){if(f.vcode.value==""||f.vcode.value.length<=0){alert("请输入验证码");f.vcode.focus();return false;}}
}

function ShowLogin_New(msg,js){
	var surl=weburl + "login.asp";
	if(js)surl=surl+"?js="+escape(js);
	if(msg){msg="用户登陆 ("+msg+")";}else{msg="用户登陆"}
	openWindow(surl,400,180,msg);
}

function ShowLogin(msg,js){
	lastUID="ready";
	if(msg){msg="用户登陆 -- "+msg;}else{msg="用户登陆"}
	var surl="?TB_login=true&TB_vcode=0&height=130&width=320";
	if(js)surl=surl+"&js="+escape(js);
	TB_show(msg, surl, false);
}

function openWindow(_sUrl, _sWidth, _sHeight, _sTitle){
	if(!_sUrl)return false;
	if(!_sTitle)_sTitle="oASK系统提示";
	if(_sUrl.indexOf("?")>-1) {_sUrl=_sUrl+"&TB_iframe=true&height="+_sHeight+"&width="+_sWidth}else{_sUrl=_sUrl+"?TB_iframe=true&height="+_sHeight+"&width="+_sWidth}
	try{window.top.TB_show(_sTitle, _sUrl, false);}catch(e){TB_show(_sTitle, _sUrl, false);}
}

function openWindow_Self(_sUrl, _sWidth, _sHeight, _sTitle){
	if(!_sUrl)return false;
	if(!_sTitle)_sTitle="oASK系统提示";//
	if(_sUrl.indexOf("?")>-1) {_sUrl=_sUrl+"&TB_iframe=true&height="+_sHeight+"&width="+_sWidth}else{_sUrl=_sUrl+"?TB_iframe=true&height="+_sHeight+"&width="+_sWidth}
	TB_show(_sTitle, _sUrl, false);
}

function dlg(_sText, _sWidth, _sHeight, _sTitle){
	if(!_sText)return false;
	if(!_sTitle)_sTitle="oASK系统提示";
	var surl="#TB_message?height="+_sHeight+"&width="+_sWidth+"&msgtext="+escape(_sText);
	try{window.top.TB_show(_sTitle, surl, false);}catch(e){TB_show(_sTitle, surl, false);}
}

function closeWindow(){
	try{
		dialogBoxClose.click();
		try{
			window.top.dialogBoxClose.click();
		}catch(e){}
	}catch(e){}
}

function openWindow2(_sUrl, _sWidth, _sHeight, _sTitle){
	if(!getUserID()){
		ShowLogin("登录后您才能执行此操作!");
	}else{
		openWindow(_sUrl, _sWidth, _sHeight, _sTitle);
	}
	return false;
}

function openScript(url, width, height){
	window.open(url,"openScript",'width=' + width + ',height=' + height + ',resizable=1,scrollbars=yes,menubar=yes,status=yes' );
}

function openScript2(url, width, height){
	window.open(url,"openScript",'width=' + width + ',height=' + height + ',resizable=1,scrollbars=no,menubar=no,status=no' );
}

function ReadSMS(id){
	openWindow2(weburl+"sms.asp?act=read&smsid="+id, 610, 450, "站内短信");
}

function EditSMS(id){
	openWindow2(weburl+"sms.asp?act=edit&smsid="+id, 610, 450, "站内短信");
}

function NewSMS(to){
	var u=weburl+"sms.asp?act=new";
	if(to)u=u+"&touser="+to;
	openWindow2(u, 610, 450, "站内短信");
}

function ShowPerson(id){//by UserID
	if(!GuestShowPerson&&!getUserID()){
		ShowLogin("登录后您才能查看别人的资料!");
	}else{
		openScript(weburl+"showperson.asp?id="+id+"", 600, 400, "用户信息中心");
	}
}

function ShowPerson2(name){//by UserName
	if(!name)return false;
	if(!GuestShowPerson&&!getUserID()){
		ShowLogin("登录后您才能查看别人的资料!");
	}else{
		openScript(weburl+"showperson.asp?name="+name+"", 600, 400, "用户信息中心");
	}
}

function ShowIP(IP){
	if(!getUserID()){
		ShowLogin("登录后您才能查看别人的资料!");
	}else{
		openWindow(weburl+"ip.asp?ip="+IP, 400,200, "用户最后登陆的IP地址");
	}
}

function ShowDealBtn(divid,replyerid){
	var UID = getUserID();
	if (jie==0&&UID==senderid && replyerid!=senderid) {
		show_hidden(divid,1);
	}else{
		show_hidden(divid,0);
	}
	return;
}

function ClickDeal(obj,qid,rid){
	if(!getUserID()){
		ShowLogin("登录后您才能选择最佳答案!");
	}else{
		if(confirm('确实要把这个回复选择为最佳答案吗?  ')){
			obj.href = weburl+"modify.asp?act=deal&qid="+qid+"&rid="+rid;
			return true;
		}else{return false;}
	}
}

function ShowAnswerBtn(){
	var UID = getUserID();
	if (UID == senderid || jie == 1) {
		show_hidden("AnswerBtn",0);
	}else{
		show_hidden("AnswerBtn",1);
	}
	return;
}

function ShowModifyReBtn(divid,replyerid){
	var UID = getUserID();
	if (UID == replyerid && jie != 1) {
		show_hidden(divid,1);
	}else{
		show_hidden(divid,0);
	}
	return;
}

function ShowLoginDiv(divid){
	var UID = getUserID();
	if (UID) {
		show_hidden(divid,0);
	}else{
		show_hidden(divid,1);
	}
	return;
}

//idname: 页面元素的ID
//v: 0隐藏，1显示
function show_hidden_new(idname, v)
{
	var oObj = document.getElementById(idname);
	if (!oObj){ return false;}
	if(v==0) {
		oObj.style.display="none";
		return true;
	}else{
		oObj.style.display="";
		return true;
	}
	return false;
}

function show_hidden(idname,v){
	var oObj=$("#"+idname);
	if(!oObj)return false;
	if(v)oObj.css({display:""});else oObj.css({display:"none"});
	return true;
}

//更新记录后关闭子窗口并刷新父窗口的JS 
function RefreshParent() {
	if (window.top.dialogBoxClose) window.top.dialogBoxClose.click();
	window.top.location = window.top.location;
}

function IsNumeric(s)
{	
	if(s=="")return false;
	var r,re;
	re = /\d*/i; //\d表示数字,*表示匹配多个数字
	//re = /^\d*$/;
	r = s.match(re);
	return (r==s)?true:false;
}

function regInput(obj, reg, inputStr)
{
	var docSel= document.selection.createRange()
	if(docSel.parentElement().tagName != "INPUT") return false;
	oSel = docSel.duplicate();
	oSel.text = "";
	var srcRange= obj.createTextRange();
	oSel.setEndPoint("StartToStart", srcRange);
	var str = oSel.text + inputStr + srcRange.text.substr(oSel.text.length);
	return reg.test(str);
}

function ShowErr(msg, act, aurl){
	var re = /\n/g;
	document.write("<center>"+msg.replace(re,"<br>")+"</center>");
	if(act=="close"){
		try{window.parent.dialogBoxClose.click();}catch(e){}
		if(msg.length>0)alert(msg);
	}else if(act=="refresh"){
		try{window.parent.dialogBoxClose.click();}catch(e){}
		top.location.reload();
	}else if(act=="back"){
		if(msg.length>0)alert(msg);
		history.back();
	}else if(act=="goto" && aurl.length>0){
		if(msg.length>0)alert(msg);
		window.top.location=aurl;
	}else{
		if(msg.length>0)alert(msg);
	}
}

function check_replyform(aform){
	if(!getUserID()){
		ShowLogin("请登录后再回答","top.document.hd.ok.click();");
		return false;
	}
	
	if(!aform.co.value)
		{alert("回答内容不能为空！"); return false; }
	else if(aform.co.value.length>20000)
		{alert("回答内容的长度不能超过20000字"); return false; }
	else{
		show_hidden("da_loading", 1);
		return true;
	}
}

function ShowIMG(imgsrc,maxw,maxh){
try{
	var img=new Image;
	img.src=imgsrc;
	var w=img.width;
	var h=img.height;
	img=null;
	var r=parseInt(w)/parseInt(h);
	if(!maxw)maxw=500;
	if(!maxh)maxh=800;
	if(w>maxw){w=maxw;h=maxw/r;}
	if(h>maxh){h=maxh;w=maxh*r;}
	if(w<1)w=100;if(h<1)h=100;
	document.write("<a href='"+imgsrc+"' target='_blank'><img width="+w+" height="+h+" src='"+imgsrc+"' /></a>");
}catch(e){document.write("<a href='"+imgsrc+"' target='_blank'><img src='"+imgsrc+"' /></a>");}
}

function resizeme(aimg,maxw,maxh){
try{
	var img=new Image;
	img.src=aimg.src;
	var w=img.width;
	var h=img.height;
	img=null;
	var r=parseInt(w)/parseInt(h);
	if(!maxw)maxw=500;
	if(!maxh)maxh=800;
	if(w>maxw){w=maxw;h=maxw/r;}
	if(h>maxh){h=maxh;w=maxh*r;}
	if(w<1)w=100;if(h<1)h=100;
	$(aimg).css("width",w);
	$(aimg).css("height",h);
}catch(e){}
}

function showit(id){$("#"+id).css({display:""});}
function hideit(id){$("#"+id).css({display:"none"});}
String.prototype.trim=function(){return this.replace(/(^\s*)|(\s*$)/g,"");}

function hideifblank(id){var s=$("#"+id).text();if(!s || s.trim()=="")hideit(id);}

function ShowEmot(p)
{
	var Emots='01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48|';
	Emots=Emots.split("|");
	var ps=12;
	var pc;
	var ec=Emots.length-1;
	if(ec%ps==0)
	{
		pc=(ec)/ps
	}else{
		pc=Math.floor((ec)/ps)+1
	}
	p=parseInt(p);
	if (p<=pc){
	var istr;
	var s='&nbsp;';
	var ep=weburl+'img/emot/em';
	if (p!=1 && pc>1)
	{s+='<img style="cursor: pointer;" onClick="ShowEmot('+(p-1)+');" src="'+weburl+'img/'+'Previous.gif" width="14" height="14" title="上一页">&nbsp;';}
	for(i=(p-1)*ps;i<(p-1)*ps+ps;i++)
	{
		if (i==ec){break}
		if (i<9)
			{istr='em0'+(i+1)}
			else
			{istr='em'+(i+1)}
		s+='<img title="'+istr+'" style="cursor: pointer;" onClick=putEmot("'+istr+'"); src="'+ep+Emots[i]+'.gif">&nbsp;';
	}
	if (p!=pc)
	{s+='<img style="cursor: pointer;" onClick="ShowEmot('+(p+1)+');" src="'+weburl+'img/'+'Next.gif" width="14" height="14" title="下一页">&nbsp;';}
	s+='分页：<b>'+p+'</b>/<b>'+pc+'</b>，共<b>'+(ec)+'</b>个';
	s+="<select id=emotpage onchange=\"ShowEmot(this.value);\">";
	for (i=1; i<=pc;i++ )
	{
		s+="<option value=\""+i+"\">"+i;
	}
	s+="<\/select>";
	var o=document.getElementById("emot");
	o.innerHTML=s;
	document.getElementById('emotpage').options[p-1].selected=true;
	}
}

function putEmot(thenNo)
{
	var ToAdd = '['+thenNo+']';
	hd.co.value=hd.co.value+ToAdd;
}

function reset_vcd(){try{var obj=document.getElementById("oask_vcdimg");obj.src=obj.src;}catch(e){}}

function SearchBar_oask(keyword){document.write("<form method='get' action='"+weburl+"search.asp' target='_top'><table cellSpacing=1 cellPadding=1 width=90%><tr><td nowrap='nowrap' valign='middle' align='left' height='32'><img src='"+weburl+"img/search.gif' border='0' align='middle' /><input type='text' name='q' id='q' size='10' maxlength='100' value='"+keyword+"' /> <input onclick=\"this.form.action='"+weburl+"search.asp';\" type='Submit' class='btn' value='站内搜索' /> <input onclick=\"this.form.action='"+weburl+"ask.asp';\" type='Submit' class='btn' value='我要提问' /></td></tr></table></form>");}

function SearchBar(keyword){
document.write("<form method='get' action='http://www.google.cn/custom' target='_top'><table><tr><td nowrap='nowrap' valign='middle' align='left' height='32'><img src='"+weburl+"img/search.gif' border='0' align='middle'></img><input type='text' name='q' size='25' maxlength='255' value='"+keyword+"' id='sbi'></input> ");
document.write("<input onClick=\"this.form.action='http://www.google.cn/custom';\" type='submit' class='btn' name='sa' value='Google搜索' id='sbb'></input> ");
//document.write("<input onClick=\"this.form.Keyword.value=this.form.q.value; this.form.action='/search.asp';\" type='Submit' class='btn' value='文章搜索'></input> ");
document.write("<input onClick=\"this.form.action='"+weburl+"search.asp';\" type='Submit' class='btn' value='问答搜索'></input> ");
document.write("<input onClick=\"this.form.action='"+weburl+"ask.asp';\" type='Submit' class='btn' value='我要提问'></input> ");
document.write("<input type='hidden' name='Keyword' /><Input type='hidden' name='ModuleName' value='Article'><input type='hidden' name='client' value='pub-6402503674359372'></input><input type='hidden' name='forid' value='1'></input><input type='hidden' name='channel' value='6976134779'></input><input type='hidden' name='ie' value='GB2312'></input><input type='hidden' name='oe' value='GB2312'></input><input type='hidden' name='cof' value='GALT:#008000;GL:1;DIV:#336699;VLC:663399;AH:center;BGC:FFFFFF;LBGC:336699;ALC:0000FF;LC:0000FF;T:000000;GFNT:0000FF;GIMP:0000FF;FORID:1'></input><input type='hidden' name='hl' value='zh-CN'></input></td></tr></table></form>");
}

function AskTools(){
	var s="<div id='AskTools'>"; 
	var sReply="<a class='buttona' href='#ireply'>我来回答</a>";
	var uid=getUserID();
	if(uid==senderid){
		s=s+"<a class='buttona' style='width:110px;' href='#' onClick=\"return openWindow2('"+weburl+"attach/up.asp?qid="+qid+"&rid=0','550','350','问题附件')\" title='上传附件文档、屏幕截图或演示动画，将会使问题或解答更清晰易懂'>上传/更换附件</a>&nbsp;";
		s=s+"<a class='buttona' href='#' onClick=\"return openWindow2('"+weburl+"modify.asp?act=bu&qid="+qid+"',500,220,'问题补充')\" title='可以对您的提问补充细节，以得到更准确的答案'>问题补充</a>&nbsp;";
		s=s+"<a class='buttona' href='#' onClick=\"return openWindow2('"+weburl+"modify.asp?act=tg&qid="+qid+"',400,160,'提高悬赏')\" title='提高悬赏分，以提高问题的关注度'>提高悬赏</a>&nbsp;";
		s=s+"<a class='buttona' href='#' onClick=\"return openWindow2('"+weburl+"modify.asp?act=wu&qid="+qid+"',440,140,'无满意答案')\" title='没有满意的回答，还可直接结束提问，关闭问题'>无满意答案</a>";
	}else if(!CanShowReply()){
		sReply="";
	}
	document.write(s+sReply+"</div>");
}

function AdminTools(cid){
try{	
	if(!cid)return;
	if(IsLogined()==false)return;
	if(!MasterC)return;
	if(MasterC.indexOf(","+cid+",")>-1){
		document.write("<div id='AdminTools'><a class='buttona' href='#' onClick=\"this.href='"+weburl+"answerit.asp?qid="+qid+"'\">管理此问题</a></div>");
	}
}catch(e){}
}

function AnswerTools(rid, ruid){
	if(getUserID()==ruid){
		var s;
		s="<div id='AnswerTools'>";
		s=s+"<a class='buttona' href='#' onClick=\"return openWindow2('"+weburl+"modify.asp?act=mr&qid="+qid+"&rid="+rid+"', '500', '350', '修改答复')\">修改答复</a>&nbsp;";
		s=s+"<a class='buttona' style='width:110px;' href='#' onClick=\"return openWindow2('"+weburl+"attach/up.asp?qid="+qid+"&rid="+rid+"','550','350','回答附件')\" title='上传附件文档、屏幕截图或演示动画，将会使您的解答更清晰易懂'>上传/更换附件</a>";
		s=s+"</div>";
		document.write(s);
	}
}

function DealTools(rid, ruid){
	var s;
	if(getUserID()==senderid&&senderid!=ruid){
		document.write("<a href='#' class='buttona' id='deal' onClick='return ClickDeal(this,"+qid+","+rid+")'>采纳</a>");
	}
}

function CanShowReply(){
	var uid=getUserID();
	//如果只能专家回复，未登录时，或者普通用户，就不显示回复框
	if( OnlyZJCanReply==1){
		if(!uid || UserRights.indexOf(",5,")==-1) return false;
	}
	//如果每个问题每个人只能回答一次，如果回答过，就不显示回复框
	if(ReplyOnce==1 && uid && rus.indexOf(","+uid+",")>-1) return false;
	return true;
}

function ShowIReplyDiv(){
	if(CanShowReply())show_hidden("ireply", 1);else show_hidden("ireply", 0);
}

function Ping(atype,rid){
	if(!getUserID()){
		ShowLogin("登录后您才能做出评价!");
	}else{
		var s;
		if(atype==1){s='做出好评';}else{s='做出坏评';}
		openWindow2(weburl+"modify.asp?act=pj&type="+atype+"&qid="+qid+"&rid="+rid, '400', '150', s);
	}
}

function AddPinglun(divid,ping,isSender){
	try{
		var n;
		if(ping){
			if(isSender==1){
				$("#pinglun").html("提问人的评论："+ping+"<hr class='hr_p'>"+$("#pinglun").html());
				return;
			}else{
				$("#pinglun").html($("#pinglun").html()+ping+"<hr class='hr_p'>");
			}
		}
		n=$("#"+divid).text().valueOf();
		n++;
		$("#"+divid).text(n.toString());
	}catch(e){}
}

function Show_Nav(id,aCount,num){
	for(var i=1;i<=aCount;i++){
		//try{
		if(num==i){
			document.getElementById("Nav_"+id+"_"+i).className="nav"+id+"_sel";
			document.getElementById("NavContent_"+id+"_"+i).style.display="";
		}else{
			document.getElementById("Nav_"+id+"_"+i).className="nav"+id+"_unsel";
			document.getElementById("NavContent_"+id+"_"+i).style.display="none";
		}
		//catch(e){}
	}
}