/***********************************************/
/*                                             */
/* Ajax 基本类库，提供常用的XmlHttp对象的引用、*/
/* 请求等功能。                                */
/* 2007.01.16                                  */
/*                                             */
/***********************************************/


/**
 *
 *	Function: GetRequestObject()
 *		取得一个XmlHttpRequest对象。
 *	
 *	Returns:
 *		根据不同的浏览器取得特定的浏览器支持的XmlHttpRequest对象。
 *			
 *	Example:
 *		(start code)
 *		var myAjax = GetRequestObject();
 *		(end)
 */
function GetRequestObject()
{	
    return TryFunctions(
      function() {return new ActiveXObject('Msxml2.XMLHTTP')},
      function() {return new ActiveXObject('Microsoft.XMLHTTP')},
      function() {return new XMLHttpRequest()}
    ) || false;
}

/**
 *
 *	Function: AsyncRequest()
 *		异步提交数据到服务器的函数。
 *
 *  Arguments:
 *		options - 调用选项，一个Json对象，形式为 {
 *			url - 请求的Ajax地址，可省略。
 *			method - 发送到服务器的请求方法(POST/GET)，省略时采用默认值 POST。
 *			postBody - 发送到服务器的数据。
 *			onComplete - 当服务器处理完毕后的回调函数。当省略此参数时意味着没有回调函数
 *			[* 已不使用] evalScripts - 是否对返回的数据进行Json转换，默认为false。]
 *			resultType - 返回的数据类型，三态枚举：1 - Json类型、2 - XmlDocument文档、3 - 字符串。(默认为3)
 *			isAsync - 使用同步异步，布尔值：true(异步)、false(同步)。默认值异步
 *		}
 *	Example:
 *		(start code)
 *			AsyncRequest({postBody:'name=abc&age=23',onComplete: func, resultType:'Object');
 *			function func(result)
 *			{
 *				alert(result.Name);
 *			}
 *		(end)
 */
function AsyncRequest(options)
{	
	this.options = {
		url: 'Ajax.aspx',
		method: 'post',
		postBody: null,
		onComplete:function(){},
		resultType: 3,
		isAsync: true
	}
	
	for(var property in options)
	{
		this.options[property] = options[property];
	}
	
	this.Ajax = GetRequestObject();
	this.Ajax.open(this.options.method, this.options.url, this.options.isAsync);
	
	if (this.options.method == 'post')
	{
		this.Ajax.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
		if (this.Ajax.overrideMimeType) this.Ajax.setRequestHeader('Connection', 'close');
	}
	else if(this.options.method == 'get')
	{
		this.options.url = this.options.url + "?" + this.options.postBody;
		this.options.postBody = null;
	}
	if (this.options.resultType == 1)
	{
		this.Ajax.setRequestHeader('RequestType', 'X-JSON');
	}
	if (this.options.resultType == 2)
	{
		this.Ajax.setRequestHeader('RequestType', 'XmlDocument');
	}
	if (this.options.resultType == 3)
	{
		this.Ajax.setRequestHeader('RequestType', 'String');
	}
	if(this.options.HttpHeaders)
	{
		for(var i=0;i<this.options.HttpHeaders.length; i = i+2)
		{
			this.Ajax.setRequestHeader(this.options.HttpHeaders[i], this.options.HttpHeaders[i+1]);
		}
	}
	
	if(typeof(this.options.postBody) == 'object')
	{
		this.options.postBody = this.options.postBody.toJSONString();
	}

	if(this.options.isAsync)
	{
		this.Ajax.onreadystatechange = function(){
			var result;
			if (this.Ajax.readyState == 4 && this.Ajax.status == 200){
				//alert(this.Ajax.responseText);	//debug
				switch(this.options.resultType)
				{
					case 1:
					case 'Object':
						var ret = eval('(' + this.Ajax.responseText + ')');
						result = ret.ServerObject
						break;
					case 2:
					case 'Xml':
						result = this.Ajax.responseXML;
						break;
					case 3:
					case 'String':
					default:
						result = this.Ajax.responseText;
						break;
				}
				
				this.options.onComplete(result);
				this.Ajax.onreadystatechange = function(){};
			}
		};
	}

	if(typeof(this.options.postBody) == 'string')
		this.Ajax.send(this.options.postBody);
	else
		this.Ajax.send(null);

	if(!this.options.isAsync)
	{
		var result;
		switch(this.options.resultType)
		{
			case 1:
			case 'Object':
				var ret = eval('(' + this.Ajax.responseText + ')');
				result = ret.ServerObject
				break;
			case 2:
			case 'Xml':
				result = this.Ajax.responseXML;
				break;
			case 3:
			case 'String':
			default:
				result = this.Ajax.responseText;
				break;
		}
		return result;
	}
}