﻿function ajax(){}

ajax.getXmlHttpObj = function()
{
	if(typeof XMLHttpRequest != "undefined")
	{
		return new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{
		var aVersion =["MSXML2.XMLHttp.5.0",
						"MSXML2.XMLHttp.4.0",
						"MSXML2.XMLHttp.3.0",
						"MSXML2.XMLHttp",
						"Microsoft.XMLHttp"];
		for(var i=0;i<aVersion.length;i++)
		{
			try
			{
				var oXmlHttp = new ActiveXObject(aVersion[i]);
				return oXmlHttp;
			}
			catch(e)
			{
				
			}
		}
	}
	throw new Error("XMLHttp object can't be create!");
};

//
//  使用XMLHTTP和远程服务器通信。
//
//  参数名称        必须    类型        取值范围            描述
//-------------------------------------------------------------------------------------------
//	async			是      boolean     true/false          是否使用异步方式
//	httpMethod		是      string      "post"/"get"        http方法
//	responseType	否      string      "text"/"xml"        返回数据的类型
//	url				是      string                          接收请求的URL地址
//	callback		否      function                        异步方式操作完成时执行的回调函数
//	postData		否      string                          post方式时发送的数据
//
//  注：非必须的参数，如不被传递时使用null代替。
//
ajax.transmit = function(async, httpMethod, responseType, url, callback, postData)
{
    httpMethod = httpMethod.toLowerCase();
    if(responseType != null) responseType = responseType.toLowerCase();
    
    var xmlhttp = this.getXmlHttpObj();
	xmlhttp.open(httpMethod, url, async);
	
	if(!async && httpMethod == "post")
	    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

	if(async)
	{
		xmlhttp.onreadystatechange = function()
		{
			if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
			{
			    try
			    {
			        if(typeof(callback) == "function")
			        {
			            switch(responseType)
			            {
			                case "text":
			                    callback(xmlhttp.responseText);
			                    break;
			                case "xml":
					            callback(xmlhttp.responseXML);
					            break;
					        default:
					            callback(null);
			            }
			        }
				}
				finally
				{
				    xmlhttp = null;
				}
			}
		}
		xmlhttp.send(postData);
	}
	else
	{
		xmlhttp.send(postData);
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
		{
    		switch(responseType)
			{
			    case "text":
			        return xmlhttp.responseText;
			    case "xml":
			        return xmlhttp.responseXML;
			    default:
			        return null;
			}
		}
		else
		{
		    return null;
		}
	}
};

//通过同步GET方式获得指定服务端的数据
//url 指定的服务端处理文件路径
ajax.getText = function(url)
{
    var xmlhttp = this.getXmlHttpObj();
    xmlhttp.open("GET", url, false);
    xmlhttp.send(null);
	if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
	{
		return xmlhttp.responseText;
	}
	else
	{
	    return null;
	}
};

//通过同步POST方式获得指定服务端的数据
//url 指定的服务端处理文件路径
//post 发送的数据
ajax.postText = function(url,postData)
{
    var xmlhttp = this.getXmlHttpObj();
    xmlhttp.open("POST", url, false);
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlhttp.send(postData);
	if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
	{
		return xmlhttp.responseText;
	}
	else
	{
	    return null;
	}
};

//以异步GET方式获得指定服务端的数据
//url 指定的服务端处理文件
//callback 异步处理方法
ajax.asyncGetText = function(url,callback)
{
    var xmlhttp = this.getXmlHttpObj();
    xmlhttp.open("GET", url, true);
    xmlhttp.onreadystatechange = function()
	{
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
		{
		    try
		    {
		        if(typeof(callback) == "function")
		        {
		            callback(xmlhttp.responseText);
		        }
			}
			finally
			{
			    xmlhttp = null;
			}
		}
	}
    xmlhttp.send(null);
};

//以异步Post方式获得指定服务端的数据
//url 指定的服务端处理文件
//callback 异步处理方法
//post 发送的数据
ajax.asyncPostText = function(url,callback,postData)
{
    var xmlhttp = this.getXmlHttpObj();
    xmlhttp.open("POST", url, true);
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlhttp.onreadystatechange = function()
	{
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
		{
		    try
		    {
		        if(typeof(callback) == "function")
		        {
		            callback(xmlhttp.responseText);
		        }
			}
			finally
			{
			    xmlhttp = null;
			}
		}
	}
    xmlhttp.send(postData);
};

//通过同步GET方式获得指定服务端的数据
//url 指定的服务端处理文件路径
ajax.getXml = function(url)
{
    var xmlhttp = this.getXmlHttpObj();
    xmlhttp.open("GET", url, false);
    xmlhttp.send(null);
	if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
	{
		return xmlhttp.responseXML;
	}
	else
	{
	    return null;
	}
};

//通过同步Post方式获得指定服务端的数据
//url 指定的服务端处理文件路径
//post 发送的数据
ajax.postXml = function(url,postData)
{
    var xmlhttp = this.getXmlHttpObj();
    xmlhttp.open("POST", url, false);
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlhttp.send(postData);
	if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
	{
		return xmlhttp.responseXML;
	}
	else
	{
	    return null;
	}
};

//以异步GET方式获得指定服务端的数据
//url 指定的服务端处理文件
//callback 异步处理方法
ajax.asyncGetXml = function(url,callback)
{
    var xmlhttp = this.getXmlHttpObj();
    xmlhttp.open("GET", url, true);
    xmlhttp.onreadystatechange = function()
	{
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
		{
		    try
		    {
		        if(typeof(callback) == "function")
		        {
		            callback(xmlhttp.responseXML);
		        }
			}
			finally
			{
			    xmlhttp = null;
			}
		}
	}
    xmlhttp.send(null);
};

//以异步Post方式获得指定服务端的数据
//url 指定的服务端处理文件
//callback 异步处理方法
ajax.asyncPostXml = function(url,callback,postData)
{
    var xmlhttp = this.getXmlHttpObj();
    xmlhttp.open("POST", url, true);
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlhttp.onreadystatechange = function()
	{
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
		{
		    try
		    {
		        if(typeof(callback) == "function")
		        {
		            callback(xmlhttp.responseXML);
		        }
			}
			finally
			{
			    xmlhttp = null;
			}
		}
	}
    xmlhttp.send(postData);
};
