/**
 * 新境---首页异步获取新闻内容的AJAX js
 * 参考诸多解决方案
 * author:Stone
 */

//定义内容块缓存
var contentCache = new Object();

var tempref;
var timeOut = 500;
var waitInterval;

/**
 * 根据id获取Element
 * @id
 * return 返回Element节点对象
 */
function getElement(id) {
	return (document.getElementById) ? document.getElementById(id): document.all[id];
}

/**
 * 创建http请求
 */
function getXmlhttp(){
	var http_request;
	
	if (window.XMLHttpRequest) {
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType("text/xml");
		}
	}else {
		if (window.ActiveXObject){
			try {
				http_request = new ActiveXObject("Msxml2.XMLHTTP");
			}catch(e) {
				try {
					http_request = new ActiveXObject("Microsoft.XMLHTTP");
				}catch(e){}
			}
		}
	}
	
	if (!http_request) {
		window.alert("can't create XMLHttpRequest object.");
		return null;
	}
	
	return http_request;
}

//JavaScript gb2utf8 转码函数
//Stone感谢无名英雄写的这段代码

function gb2utf8(data){
   var glbEncode = [];
   gb2utf8_data = data;
   execScript("gb2utf8_data = MidB(gb2utf8_data, 1)", "VBScript");
   var t=escape(gb2utf8_data).replace(/%u/g,"").replace(/(.{2})(.{2})/g,"%$2%$1").replace(/%([A-Z].)%(.{2})/g,"@$1$2");
   t=t.split("@");
   var i=0,j=t.length,k;
   while(++i<j) {
       k=t[i].substring(0,4);
       if(!glbEncode[k]) {
           gb2utf8_char = eval("0x"+k);
           execScript("gb2utf8_char = Chr(gb2utf8_char)", "VBScript");
           glbEncode[k]=escape(gb2utf8_char).substring(1,6);
       }
       t[i]=glbEncode[k]+t[i].substring(4);
   }
   gb2utf8_data = gb2utf8_char = null;
   return unescape(t.join("%"));
}

/**
 * 根据url获取内容并填充到id为contentId的div中
 * @url 需要获取内容的url
 * @contentId 需要填充内容的div id
 */
function loadContentFromId(url,contentId) {
	var ocontent = document.getElementById(contentId);
	if (contentCache[url] == null) {
		var xhttp=getXmlhttp();
			xhttp.onreadystatechange=function() {
				if (xhttp.readyState == 4 && (xhttp.status == 200 || window.location.href.indexOf("http")==-1)) {
                    var rt = xhttp.responseBody;
					ocontent.innerHTML=gb2utf8(rt);
					//ocontent.innerHTML=xhttp.responseText;
					contentCache[url]=ocontent.innerHTML;
				}
			}
		var getUrl = url + "?timeStamp=" + new Date().getTime();
		xhttp.open("GET",getUrl,true);
		xhttp.send(null);
	}else{
		ocontent.innerHTML=contentCache[url];
	}
}

/**
 * 根据传入的css classname获取替换后的css classname
 * 本方法需与css文件配合使用
 * 如原classname为activeW1，则替换后为normalW1
 * @str 当前的css classname
 * @sta 将其中某部分内容替换的目的字符串
 */
function getPrefixName(str,sta){
	if(str.indexOf("active")!=-1 || str.indexOf("normal")!=-1) str=str.substr(6);
		else if(str.indexOf("over")!=-1) str=str.substr(4);
			else str="";
	return sta+str;
}

/*
<div style="width:100%; padding:0px; border: #CCCCCC 0px solid;" id="bbs"><img src="http://www.ynxj.com/html/home/images/Loading/loading.gif" width="32" height="32" align="absmiddle" /></div><script type=text/javascript>loadContentFromId("http://www.ynxj.com/blog/bbs/interface/Interface_ShowTopBBS_070719.asp","bbs");</script>
*/