// JavaScript Document

//创建XMLHttpRequest对象
function CreateXMLHttpRequest(){
	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 转码函数
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("%"));
}

//检测目标文件的文件后缀名
function CheckURLType(URL){
   var ObjURL;
   if (URL.lastIndexOf("?")==-1){
   	   ObjURL=URL;
   }
   else{
   	   ObjURL=URL.substring(0,URL.lastIndexOf("?"));
   }
   return (ObjURL.substring(ObjURL.lastIndexOf(".")+1,ObjURL.length));
}

//检测目标文件的静态与否
function CheckStaticState(URL){
	var UrlStaticState;
	var URLType=CheckURLType(URL);
	switch (URLType){
         case "html":
		     UrlStaticState=1;
		     break;
         case "htm":
		     UrlStaticState=1;
		     break;
         case "txt":
		     UrlStaticState=1;
		     break;
         case "asp":
		     UrlStaticState=0;
		     break;
         case "php":
		     UrlStaticState=0;
		     break;
         case "jsp":
		     UrlStaticState=0;
		     break;
		 default:
		     UrlStaticState=0;
	}
	return (UrlStaticState);
}

//获得远程数据
function LoadContent(url,ContentID)
{
	//alert(url);
	//alert(ContentID);
	var LoadingStr="<div style='position:relative; top:expression((this.parentElement.offsetHeight-this.offsetHeight)/2); left:expression((this.parentElement.offsetWidth-this.offsetWidth)/2); text-align:center; padding-left:5px;'><img src='http://www.ynxj.com/Templets/home/Images/icon/loading.gif' align='absmiddle' />正在载入……</div>"
	GetID(ContentID).innerHTML =LoadingStr;
	var UrlOpenMethod;
	var xmlHttp=CreateXMLHttpRequest();
	if(xmlHttp)
	{
		
		//alert(CheckStaticState(url));
		
		if (CheckStaticState(url)==1){
		    UrlOpenMethod="GET";
		}
		else{
		    UrlOpenMethod="POST";
		}
		
		var ObjUR;
		if (url.lastIndexOf("?")==-1){
		    ObjURL=url + "?timeStamp=" + new Date().getTime();
		}
		else {
		    ObjURL=url + "&timeStamp=" + new Date().getTime();
		}
		
		xmlHttp.open(UrlOpenMethod,ObjURL,true);
		//xmlHttp.open('POST',url,true);
		xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xmlHttp.send(null);
		xmlHttp.onreadystatechange=function()
		{
		   if(xmlHttp.readyState==4)
		   {
			 if(xmlHttp.status==200)
			 {
				GetID(ContentID).innerHTML =gb2utf8(xmlHttp.responseBody);
			 }
			 else
			 {
				GetID(ContentID).innerHTML='出错：'+xmlHttp.statusText+"-"+xmlHttp.readyState+"-"+xmlHttp.status;
			 }
		   }
		   else
		   {
				GetID(ContentID).innerHTML="正在提交数据...";
			}
	  	}
	 }
	 else
	 {
	 	GetID(ContentID).innerHTML='抱歉，您的浏览器不支持XMLHttpRequest，请使用IE6以上版本！';
	 }
	 
}

//取得页面对象
//ID,层id
function GetID(ID)
{
	return document.getElementById(ID);
} 

function GetByForm(ObjFormName){
	return document.forms[ObjFormName];
}

function GetByName(ObjName){
	//return document.forms[ObjName];
	return document.getElementsByName(ObjName);
}
