//********************************************************************************
//** 插件制作:		软件兔 (http://blog.wyshen.com/ SoftBunny+Soft@Gmail.com)
//** 插件修改:		http://www.jlr.cn
//** 文章页面调用此JS，实现投票及显示
//********************************************************************************
var xmlhttp
//var ID=getID();
//var Path=getPath();
var Path="../Rating/";
//var Path="";
var Score=0;
var UserScore=0;
var Rnumber=0;

sndReqShowRating();

function sndReqShowRating()
{
	xmlhttp=myXMLHttpRequest();
//	if (xmlhttp!=null)
//	{
    xmlhttp.open('get', Path + 'Score/' + xID + '.txt?rnd=' + Math.random());
    xmlhttp.onreadystatechange = handleShowRating;
    xmlhttp.send(null);	
//	}
}

function handleShowRating() {
	if(xmlhttp.readyState == 4)
	{
		if (xmlhttp.status == 200){
			Rnumber = new Number(xmlhttp.responseText);
		}
		else if (xmlhttp.status == 404)
		{
			Rnumber = 0;
		}
		else
		{
			Rnumber = 0;
		}
		G("vote_num").innerHTML = Rnumber;

		if (IfCanVote(xID)==null)
		{
			G("vote_txt").innerHTML = "给博主投一票";
			var A=G("vote_btn");
			A.onmouseover=function(){this.className="text_vote2"};
			A.onmouseout=function(){this.className="text_vote1"};
			//G("vote_txt").innerHTML="对我有帮助";
			A.onclick=function(){Rating(xID)};
			//document.body.appendChild(A)
		}
		else
		{
			G("vote_txt").innerHTML = "支持博主有你一票";
			var A=G("vote_btn");
			A.onclick=function(){this.className="text_vote2";G("vote_txt").innerHTML="已投过,不用重复";};
			A.onmouseover=function(){this.className="text_vote2";G("vote_txt").innerHTML="支持博主明天再来";};
			A.onmouseout=function(){this.className="text_vote1";G("vote_txt").innerHTML = "支持博主有你一票";};
		}
	}
}


function Rating(xID)
{
	G("vote_btn").onclick=function(){return false;};
	G("vote_txt").innerHTML = "投票中,请稍候";
	xmlhttp=myXMLHttpRequest();
    xmlhttp.open('get', Path + 'Rating.asp?x=' + xID + '&rnd=' + Math.random());
    xmlhttp.onreadystatechange = handleRating;
    xmlhttp.send(null);	
}

function handleRating() {
	if(xmlhttp.readyState == 4)
		if (xmlhttp.status == 200){
			var Rdata = xmlhttp.responseText.toLowerCase();
			if (Rdata == "ok")
			{
				setCookie("vote"+xID,"1");
				G("vote_btn").onmouseover=function(){this.className="text_vote1";};
				G("vote_txt").innerHTML = "成功,感谢参与";
				Rnumber = parseInt(Rnumber)+1;
				G("vote_num").innerHTML = Rnumber;
			}
			else
			{
				G("vote_txt").innerHTML = "遇到未知错误";
			}
		}
}


function getID(){
	if (PostID)
		return PostID;
	else
		var str = document.location.pathname;
		return str.substring(str.lastIndexOf("/")+1,str.lastIndexOf("."));

}

function getPath(){
	var strtmp;
	var scripts=document.getElementsByTagName("script");
	for (i=scripts.length-1; i>0; i--)
		if (scripts[i].src != "")
			if(scripts[i].src.toLowerCase().indexOf("rating.js")>0)
				strtmp = scripts[i].src.toLowerCase().replace("rating.js","");
	if (strtmp!="")
		if (strtmp.substr(0,4).toLowerCase() != "http")
			strtmp=document.location.protocol + "//" + document.location.host + strtmp;
	return strtmp;
}

function myXMLHttpRequest() {
	var xmlhttplocal;
   
	var msxmlhttp = new Array(
	'Msxml2.XMLHTTP.5.0',
	'Msxml2.XMLHTTP.4.0',
	'Msxml2.XMLHTTP.3.0',
	'Msxml2.XMLHTTP',
	'Microsoft.XMLHTTP');
	for (var i = 0; i < msxmlhttp.length; i++) {
		try {
			xmlhttplocal = new ActiveXObject(msxmlhttp[i]);
		} catch (e) {
			xmlhttplocal = null;
		}
	}
	
	if(!xmlhttplocal && typeof XMLHttpRequest != "undefined")
		xmlhttplocal = new XMLHttpRequest();
	if (!xmlhttplocal)
		alert("Could not create connection object.");
	return xmlhttplocal;
}

//用cookie实现投票时间控制

function IfCanVote(xid)
{
    return getCookie("vote"+xid);
}
function IfCanVote2(xid)
{
   var CookieValue =getCookie("vote"+xid);
    if(CookieValue == null)
   {
      return true;
//      setCookie("vote"+xid,"1");
   }
   else
   {
     return null;
//      alert("对不起，24小时内不能对同一商品重复投票。");
   }
    
}
function setCookie(name,value)
{
   var exp = new Date();
   exp.setTime(exp.getTime() + 24*60*60*1000);//毫秒
   document.cookie = name + "="+escape(value)+";expires="+exp.toGMTString();
}
function setCookie2(sName,sValue,expireHours) {
    var cookieString = sName + "=" + escape(sValue);
    if (expireHours>0) {//设置过期时间
         var date = new Date();
         date.setTime(date.getTime + expireHours *60*60*1000);//时间单位=毫秒
         cookieString = cookieString + "; expire=" + date.toGMTString();// date.toGMTString()转为GMT时间
    }
    document.cookie = cookieString;//写cookie
}
function getCookie(name)
{
   var arr,reg=new RegExp("(^|)"+name+"=([^;]*)(;|$)");
   if(arr=document.cookie.match(reg)) 
     return unescape(arr[2]);
   else
     return null;
}
function getCookie2(sName) {
  var aCookie = document.cookie.split("; ");
  for (var j=0; j < aCookie.length; j++){
    var aCrumb = aCookie[j].split("=");
    if (escape(sName) == aCrumb[0]&& aCrumb[1]=="true")
      return true;
  }
  return null;
}
function delCookie(name)
{
   var exp = new Date();
   exp.setTime(exp.getTime()-24);
   var cval=getCookie(name);
   if(cval!=null)
     document.cookie=name+"="+cval+";expires="+exp.toGMTString();
}
function G(id){return document.getElementById(id);}
