﻿function getCookie(firstname,secondname)//取cookies函数
{
    var   dc   =   document.cookie; 
    //document.all("ta1").value=dc;
    var   fbegin=dc.indexOf(firstname+"=");
    if(fbegin<0)
      return null;
    //alert(fbegin);
    var   fend=dc.indexOf(";",fbegin);
    //alert(fend);
    if(fend==-1)
      fend=dc.length;
    //alert(fend);
    var   prefix=secondname+"=";
    var   begin=dc.indexOf(prefix,fbegin); 
    //alert(begin);
    var   end=-1;
    var   end1=document.cookie.indexOf( ";",begin); 
    var   end2=document.cookie.indexOf( "&",begin);
    //alert(end1);
    //alert(end2);
      if(end1==-1&&end2==-1)
         end=dc.length;
      else
      {
         if(end1>=0&&end2<0)
         end=end1;
         else if(end1<0&&end2>=0)
         end=end2;
         else if(end1<0&&end2<0)
         end=dc.length;
         else if(end1>end2)
         end=end2;
         else if(end2>end1)
         end=end1;
         else
         end=dc.length;
      }
     // alert(end);
      if(fbegin<=begin&&fend>=end)
      {
        var value=dc.substring(begin +prefix.length,end);
        if(value==null||value=="")
            return null;
        else
         return decodeURI(dc.substring(begin +prefix.length,end));
      }
      else
      return null;
}


function delCookie(firstname,secondname)//删除cookie
{
        var exp = new Date();
    exp.setTime(exp.getTime() - 10000);
    var cval=getCookie(firstname,secondname);
    if(cval!=null)
	document.cookie= firstname+"=;expires="+exp.toGMTString()+";domain=datihu.com";

}

function showdiv(type)  //true 是现实要登录，false是要注销
{
   
   if(type)
   {
    var str=getCookie('user','realname');
    if(str!=null&&str!="")
    {
        document.getElementById('dlogin').style.display='block';
        document.getElementById('dunlogin').style.display='none';
        document.getElementById('spusername').innerHTML="你好,<b>"+str+"</b>!";
    }
     else
        {
            str=getCookie('user','name');
            if(str!=null&&str!="")
            {
             document.getElementById('dlogin').style.display='block';
             document.getElementById('dunlogin').style.display='none';
              document.getElementById('spusername').innerHTML="你好,<b>"+str+"</b>!";
            }
             else
              {
              document.getElementById('dunlogin').style.display='block';
              document.getElementById('dlogin').style.display='none';
              document.getElementById('spusername').innerHTML="用户名";
              }
        }
   }
   else
   {
        delCookie('user','name');
        document.getElementById('dunlogin').style.display='block';
        document.getElementById('dlogin').style.display='none';
        document.getElementById('spusername').innerHTML="用户名";
   }
}

//下面的跳转知道百科

function searchaction()
{
    if(document.getElementById("searchtext").value!="")
    {
       if (document.getElementById("RadioGroup1_0").checked)　//百科的id
       {
            var desurl="http://www.datihu.com/bk/wiki_searcharticle.aspx?searchkey="+ escape(document.getElementById("searchtext").value);
            //window.open (desurl,'','height=100,width=400,top=0,left=0,toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no');
            window.location =desurl;
       }
    
       else 
       {
            var desurl="http://www.datihu.com/zd/ask_search.aspx?searchtype="+encodeURI("已解答")+"&searchkey="+ encodeURI(document.getElementById("searchtext").value);
            //window.open (desurl,'','height=100,width=400,top=0,left=0,toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no');
            window.location =desurl;
       }
    }
    else
    {
       if (document.getElementById("RadioGroup1_0").checked)　　//百科的id
       {
            //window.open ('http://wiki.datihu.com','','height=100,width=400,top=0,left=0,toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no');
             window.location ='http://www.datihu.com/bk/wiki_index.aspx';
       }
       
       else 
       {
            //window.open ('http://zhidao.datihu.com','','height=100,width=400,top=0,left=0,toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no');
            window.location ='http://www.datihu.com/zd/ask_index.aspx';
       }
    }
    return true;

}

function autosearch(key)  //实现按下回车键，自动相应搜索事件
{
    if (key==13) 
    {
        searchaction();
    }
}


//下面是跳转公司百科
function searchcompanywikiaction()
{
    if(document.getElementById("searchcompanywiki").value!="")
    {
     var desurl="http://www.datihu.com/bk/wiki_searchcompany.aspx?searchkey="+ escape(document.getElementById("searchcompanywiki").value);
     window.location =desurl;
    }
    else
    {
     window.location ='http://www.datihu.com/bk/wiki_index.aspx';
    }
    return true;
}

function autosearchcompanywiki(key)  //实现按下回车键，自动相应搜索事件
{
    if (key==13) 
    {
        searchcompanywikiaction();
    }
}

//下面是创建词条按钮事件

function createwikiarticle() 
{
    var str=getCookie('user','name');
    if(str!=null&&str!="")
    {
        window.location="http://www.datihu.com/bk/wiki_newarticle.aspx";
    }
   else 
   {
        if(confirm('创建词条需要您登录，现在是否要登录？'))
        {
            window.location="http://user.datihu.com/webpage/login.aspx?Url=http%3a%2f%2fwww.datihu.com%2fbk%2fwiki_newarticle.aspx";
        }
   }
}

//关于知道提问的事件
function createaskquestion() 
{
    var str=getCookie('user','name');
    if(str!=null&&str!="")
    {
        window.location="http://www.datihu.com/zd/ask_new_question.aspx";
    }
   else 
   {
        if(confirm('提问需要您登录，现在是否要登录？'))
        {
            window.location="http://user.datihu.com/webpage/login.aspx?Url=http%3a%2f%2fwww.datihu.com%2fzd%2fask_new_question.aspx";
        }
   }
}




//弹出修炼版体验页面
function openxl() 
{
   window.showModalDialog('./xltiyan.html',window,'dialogWidth=800px;dialogHeight=600px;help:no;status:no;scrollbars=no;left:yes;overflow-y:hidden;');
}




