/* --------------------------------------------
 * Ajax.js
 * 封装了有关Ajax对象的操作类
 * --------------------------------------------
 * CopyRight @ 2007-05-09  Powered by Alan Yu
 */


/***** 函数Ajax() 由该类来进行数据访问以及封装返回结果 *****/
function Ajax(){
	this.url = null;                 // 访问服务器的URL
	this.method = "post";            // 访问服务器的方式
	this.async = false;              // 是否以异步方式访问数据库
	this.rs = null;                  // Ajax RecordSet 数据存取对象
	this.rsCount = 0;                // 当前Ajax RecordSet 对象中的记录行数
	this.fieldCount = 0;             // 每行的字段数
	this.rsCurRec = -1;              // 指针游标
	this.XMLHttp = null;             // XMLHttpRequest 对象
	this.XMLDoc = null;              // XMLDOC对象
	this.isOpen = false;             // 当前的Ajax RecordSet 对象是否已经打开

    /* 方法定义 */
	this.callBack = null;
	this.setCallBack = setArsCallBack;
	this.open = openArs;
	this.close = closeArs;
	this.next = nextArs;
	this.prev = prevArs;
	this.first = firstArs;
	this.last = lastArs;
	this.getCount = getCountArs;
	this.getFieldCount = getFieldCountArs;
	this.getFieldAt = getFieldAtArs;
	this.setUrl = setArsUrl;
	this.setMethod = setArsMethod;
	this.getUrl = getArsUrl;
	this.getMethod = getArsMethod;
	this.getElementsByTagName = getElementsByTagName;
}

/***** 函数getElementsByTagName(tagName) 根据标签名称返回结果集 *****/
function getElementsByTagName(tagName){
	return this.XMLHttp.responseXML.getElementsByTagName(tagName);
}

/***** 函数openArs() 打开一个Ajax RecordSet 结果集 *****/
function openArs(){
	
	if(this.XMLDoc==null){
		this.XMLDoc = new ActiveXObject("Microsoft.XMLDOM");
		this.XMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if(this.callBack!=null){
		this.async = true;
		var handle = this;
		this.XMLHttp.onreadystatechange = function(){
			if(handle.XMLHttp.readyState==4){  // 当XMLHttpRequest 的 readyState状态为4时，表示已经从服务器得到响应
				if(handle.XMLHttp.status==0 || handle.XMLHttp.status==200){  // 判断从服务器得到的响应状态，200表示正确响应
					// 得到响应的文本内容，防止没有加载完就读取DOM，从而报对象找不到错误
					handle.XMLDoc.async = false;
					handle.XMLDoc.loadXML(handle.XMLHttp.responseText);
					handle.rs = handle.XMLDoc.documentElement;
					handle.rsCount = handle.rs.childNodes.length;
					handle.fieldCount = handle.rs.childNodes.item(0).childNodes.length;
					handle.rsCurRec = -1;
					handle.isOpen = true;
					// 数据处理完毕，回调用户设置的回调函数
					handle.callBack();
				}
			}
		}
	}else this.async = false;
	this.XMLHttp.Open(this.method, this.url, this.anync);
	this.XMLHttp.send(null);
	if(!this.async){  // 非异步情况下，直接可以处理结果，防止没有加载完就读取DOM，从而报对象找不到错误
		this.XMLDoc.async = false;
		this.XMLDoc.loadXML(this.XMLHttp.responseText);
		this.rs = this.XMLDoc.documentElement;
		this.rsCount = this.rs.childNodes.length;
		this.fieldCount = this.rs.childNodes.item(0).childNodes.length;
		this.rsCurRec = -1;
		this.isOpen = true;
	}
}

/***** 函数closeArs() 关闭一个Ajax RecordSet 结果集 *****/
function closeArs(){
	if(!this.isOpen){
		return null;
	}
	this.rsCurRec = -1;
	this.rsCount = 0;
	this.rs = null;
	this.callBack = null;
	this.XMLDoc = null;
	this.XMLHttp = null;
	this.isOpen = false;
}

/***** 函数setArsCallBack(handle) 设置异步情况下的回调函数 *****/
/***** 参数 handle：回调的函数名 *****/
function setArsCallBack(handle){
	this.callBack = handle;
}

/***** 函数getCountArs() 获取打开的 Ajax RecordSet 结果集中的记录数 *****/
function getCountArs(){
	if(!this.isOpen){
		return null;
	}
	return this.rsCount;
}

/***** 函数getFieldCountArs() 获取打开的 Ajax RecordSet 结果集中的字段数 *****/
function getFieldCountArs(){
	if(!this.isOpen){
		return null;
	}
	return this.fieldCount;
}

/***** 函数getFieldAtArs(index) 获取打开的 Ajax RecordSet 结果集中当前记录指定索引列的值 *****/
/***** 参数 index：索引值 *****/
function getFieldAtArs(index){
	if(!this.isOpen || this.rsCurRec==-1){
		return null;
	}
	var rec = this.rs.childNodes.item(this.rsCurRec);
	if(rec.childNodes.length < index+1) return rec.childNodes.item(0).text;
	else  return rec.childNodes.item(index).text;
}

/***** 函数nextArs() 使 Ajax RecordSet 结果集中指针游标移向下一条记录，如果到了尾部，则返回false *****/
function nextArs(){
	if(!this.isOpen){
		return false;
	}
	if(this.rsCurRec < (this.rsCount-1)){
		this.rsCurRec++;
		return true;
	}else return false;
}

/***** 函数prevArs() 使 Ajax RecordSet 结果集中指针游标移向前一条记录，如果到了首部，则返回false *****/
function prevArs(){
	if(!this.isOpen){
		return false;
	}
	if(this.rsCurRec>0){
		this.rsCurRec--;
		return true;
	}else return false;
}

/***** 函数firstArs() 使 Ajax RecordSet 结果集中指针游标移动到第一条记录 *****/
function firstArs(){
	if(!this.Open)	return;
	this.rsCurRec=0;
}

/***** 函数lastArs() 使 Ajax RecordSet 结果集中指针游标移动到最后一条记录 *****/
function lastArs(){
	if(!this.Open)  return;
	this.rsCurRec = this.rsCount-1;
}

/***** 函数setArsUrl(url) 设置url *****/
function setArsUrl(url){
	this.url = url;
}

/***** 函数getArsUrl() 获取url *****/
function getArsUrl(){
	return this.url;
}

/***** 函数setArsMethod(method) 设置method *****/
function setArsMethod(method){
	this.method = method;
}

/***** 函数getArsMethod() 获取method *****/
function getArsMethod(){
	return this.method;
}