
function cnLength(Str) 
		{
			var escStr = escape(Str);
			var numI = 0;
			var escStrlen = escStr.length;
			for (i = 0;  i < escStrlen;  i++) 
			if(escStr.charAt(i) == '%') 
			if(escStr.charAt(++i) == 'u')  numI ++;
			return Str.length+numI;
		}
		
var hexunAPI = {};
hexunAPI.callMethod = function(APIMethod, params, listener, testingURL, attempts) {
	if (typeof params != 'object') params = {}; // because we are going to stick a few things in even if no params are passed
	params.method = APIMethod; // see? And this also makes sure a method parameter is not passed
	
	var RESTURLROOT = 'http://blog.hexun.com/Group/ResponseClient.aspx';
	var RESTURL='';
	for (var p in params) {
		if(RESTURL=='')
			RESTURL=p + '=' + escape_utf8(params[p]);
		else
			RESTURL+= '&' + p + '=' + escape_utf8(params[p]);
	}
	
	params.RESTURL = RESTURL; // again. we stick this in here because we pass params to the callback, and it might want to see the URL
	
	var attempts = (attempts == undefined) ? 1 : attempts;
	var req = new XMLHttpRequest();
	if (req) {
		req.onreadystatechange = function() {
			if (req.readyState == 4) {
				if (req.responseText == '' && attempts<2) {
					attempts++;
					req.abort();
					hexunAPI.callMethod(APIMethod, params, listener, testingURL, attempts);
				} else {
					hexunAPI.handleResponseForTitle(req.responseXML, APIMethod, params, req.responseText, listener);
				}
			}
		}
		
		if (testingURL) RESTURLROOT = testingURL;
		req.open('POST', RESTURLROOT);
		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		req.send(RESTURL);
	}
}


hexunAPI.getCallBackName = function (dotted) {
	return dotted.split('.').join('_')+'_onLoad';
}

hexunAPI.handleResponseForTitle = function(responseXML, APIMethod, params, responseText, listener) {
	if (!responseXML) { //OPERA!
		var success = (responseText.indexOf('stat="ok"') > -1) ? true : false;
	} else {
		var success = (responseXML.documentElement && responseXML.documentElement.getAttribute('stat') == 'ok') ? true : false;
	}
	listener = (listener) ? listener : this;
	listener[this.getCallBackName(APIMethod)](success, responseXML, responseText);
}

VoteArticle = function(articleid,articleUserId,blogId,cacheName,method)
{
	var listener = document.getElementById("Recommend_" + articleid);
	listener.hexun_group_VoteArcticle_onLoad = function(success, responseXML, responseText) 
	{	
		if (success) 
		{
			//var hidden = document.getElementById("__hdd_AlertMessage");
			var alertMsg = responseXML.documentElement.getElementsByTagName('message')[0].firstChild.nodeValue
			var sourceControl = document.getElementById("__LinkVoteCount_" + articleid);
			var ret = responseXML.documentElement.getElementsByTagName('friend')[0].firstChild.nodeValue;
			if( ret == "-2")
			{
				GotoHead();
			}
			else
			if( ret != "-1" && ret != "-2" && ret != "-100" && ret != 0 && sourceControl)
			{
				sourceControl.innerHTML = "<span>" + ret + "</span>";
			}
			if(ret != "-2")
				method(alertMsg);
		} 
		else 
			alert("错误！");
	}
	hexunAPI.callMethod('hexun.group.VoteArcticle', {cateid:articleid,VoteArticle:1,CacheIndex:cacheName,ArticleUserId:articleUserId,BlogId:blogId}, listener);	
	return;
}

BuryArticle = function(articleid,burytype,method)
{
	var listener = document.getElementById("Recommend_" + articleid);
	listener.hexun_group_BuryArticle_onLoad = function(success, responseXML, responseText) 
	{
		if (success) 
		{
			var msg = responseXML.documentElement.getElementsByTagName('message')[0].firstChild.nodeValue
			var ret = responseXML.documentElement.getElementsByTagName('status')[0].firstChild.nodeValue;
			if( ret == "-2")
				GotoHead();
			else
				method(articleid,ret,msg);
		} 
		else 
			alert("错误！");
	}
	hexunAPI.callMethod('hexun.group.BuryArticle', {cateid:articleid,BuryArticle:1,buryType:burytype}, listener);	
	return;
}
 
ControlCount = function(articleid)
{
	//Ajax开始
	var listener = document.getElementById("Recommend_" + articleid);
	listener.hexun_group_controlCount_onLoad = function(success, responseXML, responseText) 
	{	
		if (success) 
		{
			var ret = responseXML.documentElement.getElementsByTagName('friend')[0].firstChild.nodeValue;
			if( ret == "100")
				GotoHead();
			if(  ret =="-1" )
				alert("推荐失败！");
			else if(ret=="0")
			{
				var cur =  document.getElementById("Vote_"+articleid);
				if(cur!=null)
				{
					var curcount=parseInt(cur.innerText) + 1
					cur.innerHTML = "<a href=votebox.aspx?articleid=" + articleid + " target=_blank>" +  curcount  + "</a>";
				}
				alert("推荐成功！");
				
			}
			else if(ret=="1")
				alert("您已经推荐过此文章！");
			else if(ret=="2")
				alert("此文章不存在！");
		} 
		else 
			alert("错误！");
	}

	hexunAPI.callMethod('hexun.group.controlCount', {cateid:articleid,ControlCount:1}, listener);	

	return ;
}

 


