// JScript 文件
//定义全局变量
var xmlhttp=GetAjaxObject();
var divobj=document.getElementById("desc");
var url="http://www.cnblogs.com/me-sa/Rss.aspx";
var title=[];
var link=[]; 
var pubDate=[];
var description=[];
var ServiceUrl="PassportService.ashx";

//获取XML
function GetAjaxObject(){
  var httprequest=false;
  if(window.XMLHttpRequest){
     httprequest=new XMLHttpRequest();
     if(httprequest.overrideMimeType){
        httprequest.overrideMimeType('text/xml');
     }
  }
  else if(window.ActiveXObject){
     try{
        httprequest=new ActiveXObject("Msxml2.XMLHTTP");
        
     }
     catch(e){
       try{
          httprequest=new ActiveXObject("Microsoft.XMLHTTP");
       }
       catch(e){}
     }  
  }
  return httprequest;
}
function loginUser()
{
   var UserName=escape($("UserName").value);
   var Password=escape($("Password").value);
   if(UserName==""||Password==""){
       window.alert("用户名或密码不能为空");
       return false;
  }
  
  var ajaxobj=GetAjaxObject();
  ajaxobj.open("GET","Login.aspx?UserName="+UserName+"&UserPassword="+Password+"&EncodeType=utf8&Rnds="+Math.random(),true);
  
  ajaxobj.onreadystatechange=function()
  {
     if(ajaxobj.readystate==4)
     {  
        if(ajaxobj.status==200)
        {    
            var returntext="";   
			if(getCookie("Howave_UcodeUserName")!=null){
                ShowLoginSuccess();
                ShowTopLoginSuccess();
			}else{
				window.alert("登录失败，用户名或密码错！");
			}
            $("UserName").value="";
            $("Password").value="";
          
        }    	 
     }
  }
  ajaxobj.send(null);
 
}
function $(id)
{
  return document.getElementById(id);
} 
function RegUser()
{
   var UserName=escape($("RegUserName").value);
   var Password=escape($("RegPassword").value);
   var RePassword=escape($("ConfirmPassword").value);
   var Email=escape($("RegEmail").value);
   var CheckCode=escape($("RegCheckCode").value);
   if(UserName=="" || Password=="" || RePassword=="" ||Email=="" ||CheckCode=="")
   {
      alert("注册项不能为空");
      return;
    }
    else if(Password!=RePassword)
    {
      alert("两次输入密码不相同");
      return;
    }
    else if(Password.length<6 ||Password.length>16)
    {
       alert("密码位数不正确");
       $("RegPassword").focus();
       return;
    }
    else if(!chkemail(Email))
    {
      alert("邮箱格式不正确");
      $("RegEmail").focus();
      return;
    }
    var ajaxobj2=GetAjaxObject();
   ajaxobj2.open("GET","UserReg.aspx?UserName="+UserName+"&Password="+Password+"&Email="+Email+"&CheckCode="+CheckCode+"&Answer=&Question=&EncodeType=utf8&Rnds="+Math.random(),true);
  
  ajaxobj2.onreadystatechange=function()
  {
     if(ajaxobj2.readystate==4)
     {  
        if(ajaxobj2.status==200)
        {          
           var returntext=ajaxobj2.responseText;
           alert(returntext);
          
        }    	 
     }
  }
  ajaxobj2.send(null);
}
function ClearReg()
{
  $("RegUserName").value=$("RegPassword").value=$("ConfirmPassword").value=$("RegEmail").value=$("RegCheckCode").value="";
}    
function logoutUser()
{
//	  var usercookie=getCookie("Howave_UserName");
//	  if(usercookie==""||usercookie==null)
//	     return;
//	  else
//	  {
//	      
//	                    
//	  }
       var ajaxobjs=GetAjaxObject();
	      ajaxobjs.open("GET","Logout.aspx?Rnd="+Math.random(),true);
	      ajaxobjs.onreadystatechange=function()
	      {
	           if(ajaxobjs.readystate==4)
	           {   
	             
	              if(ajaxobjs.status==200)
	              {  
	                 ShowLogin();
	               }
	           }
	      }
	      ajaxobjs.send(null);
	    
	  
}
function CheckUserLogin()
{
    var LoginUserName=getCookie("Howave_UserName");
    if(LoginUserName!=""&&LoginUserName!=null)
    {    
    
       ShowLoginSuccess();      
               
    }
    else
    {      
       ShowLogin();
    }  
}
function CheckIndexLogin()
{
    var LoginUserName=getCookie("Howave_UserName");
    if(LoginUserName!=""&&LoginUserName!=null)
    {    
       ShowTopLoginSuccess();
    }
   
}

var _regUserNameOld;
function CheckRegName(ctl,displayId,RegNameMin,RegNameMax)
{
  
  var regname=$(ctl).value;
  if(regname==""||regname==null)
  {
     $("reg_UserName").className="reg_tips_empty";
     $(displayId).innerHTML="用户名不能为空";
     return;
  }
  else {
    if(_regUserNameOld==regname)
       return;
    else 
      _regUserNameOld=regname;
  }
//  if(!CheckSQLInj(regname)){
//    $(displayId).innerHTML="用户名有非法字符，请重新输入";
//    return;
//  }
   if(regname.length<2)
   {
      $("reg_UserName").className="reg_tips_empty";
      $(displayId).innerHTML="&nbsp;用户名长度不正确";
      return;
   }
     
   regname=escape(regname);
   $(displayId).innerHTML="<img src=\"../IMS_Img/Index/Loading.gif\"/>正在连接数据库...";
   var ajaxobj=GetAjaxObject();
   var submitURL=ServiceUrl+"?Action=CheckReg&UserName="+regname+"&RegNameMin="+RegNameMin+"&RegNameMax="+RegNameMax+"&Rnd="+Math.random();
   ajaxobj.open('GET',submitURL,true); 
   ajaxobj.onreadystatechange=function()
   {
     if(ajaxobj.status==200)
     { 
        if(ajaxobj.responseText=="true"){
           $("reg_UserName").className="reg_tips_onchange";
           $(displayId).innerHTML="&nbsp;该用户名可以注册";
        }
        else if(ajaxobj.responseText=="lengthwrong")
        {
            $("reg_UserName").className="reg_tips_empty";
            $(displayId).innerHTML="&nbsp;用户名长度格式不正确，请确保在"+RegNameMin+"和"+RegNameMax+"字符之间。";
        }  
        else{
           $("reg_UserName").className="reg_tips_empty";
           $(displayId).innerHTML="&nbsp;该用户名已经被其他人注册";
           
        }   
     }else{
         $("reg_UserName").className="reg_tips_empty";
         $(displayId).innerHTML="&nbsp;连接不成功，无法判断用户名是否已存在！";
     }
   }
   ajaxobj.send(null);
   
}
function CheckSQLInj(str) 
{ 
    re= /select|update|delete|exec|count|'|"|=|;|>|<|%/i;
    if (re.test(str) ) 
      return false; 
    
}

var OldCheckCode;
function CheckIsRight(){
  
   var CheckCodeflag=false;
   var _code=$("CheckCode").value;  
   if(_code!=""&&_code!=null)
   {
      if(OldCheckCode==_code)
      {
        return;
      }
      else
      {
         OldCheckCode=_code;
      }
      var ajaxobj=GetAjaxObject();
      var submitURL=ServiceUrl+"?Action=CheckCode&ValidateCode="+_code+"&Rnd="+Math.random();     
      ajaxobj.open('GET',submitURL,true); 
      ajaxobj.send(null);
      ajaxobj.onreadystatechange=function()
      {
         //switch(ajaxobj.readystate){
//            case 1:
//            case 2:
//            case 3:
//                CheckCodeflag=false;
//                break;
//            case 4:
              if(ajaxobj.status==200){
                 if(ajaxobj.responseText=="true"){
                    var _codeObj=$("Y_CheckCode");
                    if(_codeObj!=null)
                    {
                       _codeObj.innerHTML="<div class='reg_tips_onchange'>输入正确</div>";
                       $("W_CheckCode").innerHTML="";
                    }   
                    CheckCodeflag=true;
                    return true;
                 }  
                 else {
                    $("W_CheckCode").innerHTML="<span style='color:red;'>验证码输入错误</span>";
                    CheckCodeflag=false;              
                 }
         
             }
             //break; 
         //}
     } 
   }
   else{
       $("W_CheckCode").innerHTML="<span style='color:red;'>请输入验证码</span>";
       CheckCodeflag=false;
   }
   //alert(CheckCodeflag);
   return CheckCodeflag;
}
function ShowLogin()
{
      // document.getElementById("showlogin").style.display="";
       $("showloginsuccess").style.display="none"; 
       ClearRssList();
}   
//获取Rss
function GetRss(url,id,rssNum,refresh){
   	var __RssTable__ = new Dragable();
	__RssTable__.controlId = id+"_Content";;
	__RssTable__.handlerId = id+"_Title";
	__RssTable__.bind(); 

    $(id).innerHTML="<img src=\"../IMS_Img/Loading.gif\"/>正在连接网站，请稍候..."
    var xmlhttp=GetAjaxObject();
    
    var submitURL=ServiceUrl+"?Action=GetRssContent&rssurl="+url;
    if(parseInt(refresh)==1)
    {
       submitURL=ServiceUrl+"?Action=GetRssContent&rssurl="+url+"&rnd="+Math.random();      
    }
   
    rssNum=parseInt(rssNum);      
//    if(!IsNaN(rssNum))
   //  rssNum=10;   
       xmlhttp.onreadystatechange=function()
       {  
           if(xmlhttp.readystate==4)
           {  
              if(xmlhttp.status==200)
              {
                 
                 title.length=0;
                 link.length=0;
                 pubDate.length=0;
                 description.length=0;                
                 var rssobj=xmlhttp.responseXML;              
                 var news=rssobj.getElementsByTagName("item");
                 if(news.length<rssNum)
                   rssNum=news.length;             
                 for(var i=0;i<rssNum;i++)
                 {           
                    title[i]=news[i].getElementsByTagName("title")[0].firstChild.nodeValue; 
                    link[i]=news[i].getElementsByTagName("link")[0].firstChild.nodeValue;                   
                    pubDate[i]=news[i].getElementsByTagName("pubDate")[0].firstChild.nodeValue;                    
                     if(news[i].getElementsByTagName("description")[0].hasChildNodes())
                     {
                        description[i]=news[i].getElementsByTagName("description")[0].firstChild.nodeValue;  
                     }       
                     else
                     {
                        description[i]="略";
                     }  
                 }
                 var result="<ul>";
                 for(var i=0;i<title.length;i++)
                 {
                    result+="<li><a href='"+link[i]+"' target='_blank' onmouseover='showdescription("+i+");'onmouseout='hidedescription();'>"+title[i]+"</a></li>";
                 }
                 result+="</ul>"
                 $(id).innerHTML=result;
              }
              else
                 $(id).innerHTML="网站暂时未能连接请稍候重试";    
           }
      }
     xmlhttp.open("GET",submitURL,true); 
     xmlhttp.send(null);    
}

//显示Rss介绍
function showdescription(i){
  divobj.style.left=event.x;
  divobj.style.top=event.offsetY+130;
  divobj.style.display="";
  divobj.innerHTML=description[i]; 
 
}
//添加RSS地址
function AddRss()
{
   var url=$("RssUrl").value;
   if(url=="")
   {
      alert("请填入Rss链接地址");
      return;
   }
   var name=$("RssName").value;
   if(name=="")
   {
     alert("请填入标签名称");
     return;
   }
   xmlhttp=GetAjaxObject();
   xmlhttp.onreadystatechange=function()
   {   
     if(xmlhttp.readystate==4&&xmlhttp.status==200)
     {
        parasefeed();
     }    
   }
   var submitURL=ServiceUrl+"?Action=AddRss&RssUrl="+url+"&RssName="+escape(name)+"&Rnd="+Math.random();
   xmlhttp.open('GET',submitURL,true);
   xmlhttp.send(null);
}
function ClearRssList()
{
  $("MyRssList").innerHTML="";
}
function GetRsslist()
{  
   xmlhttp="";
   xmlhttp=GetAjaxObject();
   xmlhttp.onreadystatechange=function()
  {
     if(xmlhttp.readystate==4 && xmlhttp.status==200)
     {      
        ParseRsslist();
     }
  }
   var submitURL=ServiceUrl+"?Action=GetMyRssList&RND="+Math.random();  
   xmlhttp.open('GET',submitURL,true);
   xmlhttp.send(null);
}

function hidedescription(){
  divobj.style.display="none";  
}
//显示隐藏Rss
function showhide(objID)
{
   var obj=$(objID);
   obj.style.display=obj.style.display=="none"?"":"none";
}

function ParseRsslist()
{
  var xmlDoc=xmlhttp.responseXML;
      var news=xmlDoc.getElementsByTagName("item");
      var url=[];
      var name=[];
      var id=$("MyRssList");
     
      for(var i=0;i<news.length;i++)
      {
         url[i]=news[i].getElementsByTagName("url")[0].firstChild.nodeValue;//获取RSS链接
         name[i]=news[i].getElementsByTagName("name")[0].firstChild.nodeValue;//获取RSS名称 
         var liTag=document.createElement("Li")   
         var aTag=document.createElement("A");
         var txt=document.createTextNode(name[i]);
         aTag.appendChild(txt);  
         aTag.href="javascript:GetRss('"+url[i]+"','content')";  
       
         liTag.appendChild(aTag)
         insertAt(id,liTag,0);
       }    
 
}
function parasefeed()
{
   var xmlDoc=xmlhttp.responseXML;
   var url=xmlDoc.getElementsByTagName("url")[0].firstChild.nodeValue;//获取RSS链接
   var name=xmlDoc.getElementsByTagName("name")[0].firstChild.nodeValue;//获取RSS名称  
   var id=document.getElementById("MyRssList");
   var liTag=document.createElement("Li")   
   var aTag=document.createElement("A");
   var txt=document.createTextNode(name);
   aTag.appendChild(txt);
   aTag.href="javascript:GetRss('"+url+"','content')"; 
   liTag.appendChild(aTag)
   insertAt(id, liTag,0)
  
}
//插入结点
function insertAt(parentNode,newNode,index)
{
  if(!parentNode.hasChildNodes())
  {
     parentNode.appendChild(newNode);
     return newNode;
   }
   try
   {
      parentNode.insertBefore(newNode,parentNode.childNodes[index]);      
   }
   catch(e)
   {
      return null;
      
   }
   return newNode;
}

function deleteAt(parentNode,index)
{
  if(parentNode.hasChildNodes())
  {
    try
    {
       parentNode.removeChild(parentNode.childNodes[index]);
    }
    catch(e)
    {
       return null;
    }
  }
}
function showDel()
{ 
  var menuobj=$("menu");
  menuobj.style.display="";
 // menuobj.style.left=event.x;
 // menuobj.style.top=event.y;
  //menuobj.innerHTML="<a href=javascript:void(0) onclick=deleteAt("+obj+","+index+")"
} 
function getCookie(cookieName)
{
    var cookieString = document.cookie
    var first ,last
    first=cookieString.indexOf(cookieName)
    if(first!=-1)
    {
       first+=(cookieName.length+1)
       last=cookieString.indexOf(';',first)
       return unescape(cookieString.substring(first,last));
    }
    return null;
    
}


function GoSearch()
{
   var searchKey=$("WebSearch").value
   if(searchKey==""||searchKey==null)
   { 
      window.alert("搜索内容不能为空");
      return false;
   }   
   if($("Google").checked)
      var arr=showModalDialog('http://www.google.com/search?hl=zh-CN&inlang=zh-CN&ie=GB2312&oe=GB2312&q='+searchKey,'','dialogWidth:800px; dialogHeight:600px; help: yes; scroll: yes; status: no;');
   if(document.getElementById("Baidu").checked)
      var arr=showModalDialog('http://www1.baidu.com/baidu?tn=verywind&word='+searchKey,'','dialogWidth:800px; dialogHeight:600px; help: yes; scroll: yes; status: no;');
   if(document.getElementById("Yahoo").checked)
      var arr=showModalDialog('http://www.yahoo.com.cn/search?p='+searchKey,'','dialogWidth:800px; dialogHeight:600px; help: yes; scroll: yes; status: no;');
   return false;
}
function ShowTopLoginSuccess()
{
  $("toolbar_login").style.display="none";
  $("toolbar_welcome").style.display="";
  var text= $("toolbar_welcome").innerHTML;
  if(text=="")
  {
     $("toolbar_welcome").innerHTML="欢迎：<b style='color:red;font-size:13px'>"+getCookie("Howave_UcodeUserName")+"</b>&nbsp;&nbsp;<a href='Logout.aspx'>注销</a>&nbsp;&nbsp;<a href='javascript:void(0)' onclick='return initContent(\"ManageSiteMessage.aspx\")'>短消息</a>";
  }
}
function ShowLoginSuccess()
{
	   $("showlogin").style.display="none";
       $("showloginsuccess").style.display="";
       var text= $("welocme").innerHTML;
       if(text=="")
       {
          text="欢迎：";
          $("welocme").innerHTML=text+getCookie("Howave_UcodeUserName")+"</br><div style='text-align:center'>&nbsp;&nbsp;异步登录后需在导航栏点击相应系统进入才可激活该系统<a href='Logout.aspx'>注销</a></div>";
       }else{
		  text = "登陆失败，用户名或密码错！";
	   }
      
       GetRsslist();
}
function ShowDefaultHide()
{   
if($('leftmenu') != null)
{
	if($('leftmenu').style.display == 'none')
	{
		$('leftmenu').style.display='inline';
		$('sideBar2').style.display='inline';
	}
	if($('leftarea') != null)
	{
	   $('leftmenu').innerHTML = $('leftarea').innerHTML;	 
	   $('leftarea').innerHTML = '';
	}
}
}
function GetAnnounce()
{
  
   var Announcehttp=GetAjaxObject(); 
   var submitURL=ServiceUrl+"?Action=GetAnnounce";  
   Announcehttp.open('GET',submitURL,true); 
 
   Announcehttp.onreadystatechange=function()
   {
     if(Announcehttp.readystate==4)
     {   
        if(Announcehttp.status==200)
        {
      
        var xmlDoc=Announcehttp.responseXML;
        var news=xmlDoc.getElementsByTagName("item");
        var url=[];
        var name=[];
        var id=$("MyAnnounce");
     
          for(var i=0;i<news.length;i++)
          {
             url[i]=news[i].getElementsByTagName("url")[0].firstChild.nodeValue;//获取RSS链接
             name[i]=news[i].getElementsByTagName("name")[0].firstChild.nodeValue;//获取RSS名称 
             var liTag=document.createElement("Li")   
             var aTag=document.createElement("A");
             var txt=document.createTextNode(name[i]);
             aTag.appendChild(txt);
             aTag.href=url[i]+"&TypeSelect=Announce"; 
             aTag.target="_blank";
             liTag.appendChild(aTag)
             insertAt(id,liTag,0);
          }  
        }    
     }
   }
  
   Announcehttp.send(null);

}
function chkemail(a)
{	var i=a.length;
	var temp = a.indexOf('@');
	var all = a.substring(temp);
	var tempd = all.indexOf('.')+temp;
	if (temp > 1) {
	  return 1;
	}
	return 0;
}
function ChangeStyle(color)
{
  switch(color)
  {
    case "red":
      $("toolbar").style.backgroundImage="url(Images/dialog/toolbar_red_bg.gif)";     
      break;
    case "blue":
      $("toolbar").style.backgroundImage="url(Images/dialog/toolbar_blue_bg.gif)";     
      break;
    case "green":
      $("toolbar").style.backgroundImage="url(Images/dialog/toolbar_green_bg.gif)";     
      break;
    case "orange":
      $("toolbar").style.backgroundImage="url(Images/dialog/toolbar_orange_bg.gif)";     
      break;
    default:
      $("toolbar").style.backgroundImage="url(Images/dialog/toolbar_bg.gif)";     
      break;     
  }
  SetCookie("toolbarBg",color,30);
}
function SetCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
	
}
function showToolbar()
{
  var toolbarObj=$("toolbar");
  toolbarObj.style.display="";
}
var fix=0;
function fixToolbar(fixed)
{
  if(fixed==1)
  {
    fix=1;
    $("toolbar").style.position="relative";
  }
  else if(fixed==0)
  {
   fix=0;
   $("toolbar").style.position="absolute";
  }
  if(fix==0)
  {
    $("toolbar").style.display="none";   
  }
  else
  {
    $("toolbar").style.display="";   
  }
}
function ShowTime()
{
  var d = new Date();
  var vHour = d.getHours();
  var vMin = d.getMinutes();
  var vSec = d.getSeconds();  
  $("time").innerHTML=vHour+":"+vMin+":"+vSec;
  setTimeout("test()",1000);
}
function Active()
{
  parent.ActivePage.location="Index.aspx";
  setTimeout("Active()",300000);//1000表示1秒，表示5分钟从后台激活一次asp.net,这里时间随你系统的配置而更改。主要是保持页面不用重新加载
}    
function RssSet(id,Settype)
{
  var rssobj= $(id)
  switch (Settype)
  {
     case "min":
       rssobj.style.display="none";
       break;
     case "max":
       rssobj.style.display="block";
       break;
     case "close":
       rssobj.style.display="none";
       break;
  }

}
