// 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="";        
            ShowLoginSuccess();
            ShowTopLoginSuccess();
            $("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();
    }
   
}

function CheckRegName(ctl,displayId)
{
  
  var regname=$(ctl).value;

  if(regname==""||regname==null)
  {
     $(displayId).innerHTML="用户名不能为空";
     return;
   }
   var ajaxobj=GetAjaxObject();
   var submitURL=ServiceUrl+"?Action=CheckReg&UserName="+regname+"&Rnd="+Math.random();
   ajaxobj.open('GET',submitURL,true); 
   ajaxobj.onreadystatechange=function()
   {
     if(ajaxobj.readystate==4&&ajaxobj.status==200)
     { 
       $(displayId).innerHTML=ajaxobj.responseText;
     }
   }
   ajaxobj.send(null);
   
}
function ShowLogin()
{
      // document.getElementById("showlogin").style.display="";
       $("showloginsuccess").style.display="none"; 
       ClearRssList();
}   
//获取Rss
function GetRss(url,id){
       $(id).innerHTML="正在连接网站，请稍候..."
       xmlhttp=GetAjaxObject();
       var submitURL=ServiceUrl+"?Action=GetRssContent&rssurl="+url;   
       xmlhttp.open("GET",submitURL,true);  
       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");                 
                 for(var i=0;i<news.length;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></br><span class='rssdate'>"+pubDate[i]+"</span></li>";
                 }
                 result+="</ul>"
                 $(id).innerHTML=result;
              }
              else
                 alert("资源未找到");      
           }
      }
     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;
    
}


    // <!CDATA[
function getWeather_onclick() {
  // new atlas 'Select' control
  var place = new Sys.UI.Select($('place'));
    
  // invoke the bridge method
  HoWave.YahooWeatherService.GetWeather({'p': place.get_selectedValue()}, onGetComplete);
}
function onGetComplete(result) {
    $('result').style.display = "block";   
    $('description').innerHTML = result[0].Description;
}

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>";
       }
      
       GetRsslist();
}
function ShowDefaultHide()
{   
if($('leftmenu') != null)
{
	if($('leftmenu').style.display == 'none')
	{
		$('leftmenu').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=/";
	alert(value);
	alert(name);
}
