/**
* Dj97 Ajax Class
* @ Author: iFiky , 780537@gmail.com
* @ Create date: 2008.07.13
* @ Update date: 2008.09.18
* @package Dj97.Com
*/

var Dj97_ajax = function(_obj){
	if (_obj){
		this.method = _obj.method;
		this.url = _obj.url;
		this.async = _obj.async;
		this.responseType = _obj.responseType;
	};
	this.create_xmlHttp = function(){
		var xmlHttp = false;
		if(window.XMLHttpRequest) {
			xmlHttp = new XMLHttpRequest();
			if (xmlHttp.overrideMimeType) {
				xmlHttp.overrideMimeType('text/xml');
			}
		}else if (window.ActiveXObject){
			try{
				xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
			} catch (e) {
				try {
					xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
				} catch (e) {}
			}
		}
		return xmlHttp;
	};
	this.send = function(_data){
		if ( undefined == this.method ) this.method = 'GET';
		if ( undefined == this.async ) this.async = true;
		if ( undefined == this.responseType ) this.responseType = 'text';
		if ( undefined == this.onReady ) this.onReady = function(_dj97_com){};
		_data = ( undefined == _data ) ? null : _data;

		if ( 'GET' == this.method.toUpperCase() ){
			this.url = this.url + '?' + _data;
			_data = null;
		};
		var obj = this.create_xmlHttp();
		obj.open (this.method, this.url, this.async);
		if ( 'POST' == this.method.toUpperCase() ){
			if (_data!=null) obj.setRequestHeader('Content-Length', _data.length);
			obj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8');
		}else{
			obj.setRequestHeader('Content-Type', 'text/html;charset=UTF-8');
		};
		var Me = this;
		obj.onreadystatechange = function(){
			if (obj.readyState==4){
				var result = '';
				if (obj.status==200){
					if ( 'TEXT' == Me.responseType.toUpperCase() )
						result = obj.responseText;
					else if( 'XML' == Me.responseType.toUpperCase() )
						result = obj.responseXml;
					obj = null;
				}else{
                    alert("网络出现故障, 请刷新后重试!!");
                }
				Me.onReady(result);
			}
		};
		obj.send(_data);
	};
};
