// 轻量级ajax应用框架 By phpwms at 2007-10-24

/* ------------- 准备阶段 ---------->>> */
//过滤两端空格
function Trim() {
	return this.replace(/\s+$|^\s+/g,"");
}
String.prototype.Trim=Trim;

//获取指定id的object
function getObject(elementId) {
	if (document.getElementById) { 
		return document.getElementById(elementId); 
	} else if (document.all) { 
		return document.all[elementId]; 
	} else if (document.layers) { 
		return document.layers[elementId]; 
	} 
}

//获取指定id的form组件的值
function getObjValue(elementId){	
	if(getObject(elementId).value!=undefined) {
		return getObject(elementId).value.Trim();
	} else {
		return "";
	}
}
/* <<<---------- 准备阶段 ------------- */


/* ------------- AJAX相关 ---------->>> */
//创建XMLHttpRequest对象
function XHR() {
	var XHR = false;
	try {
		XHR = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			XHR = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e2) {
			XHR = false;
		}
	}
	if (!XHR && typeof XMLHttpRequest != 'undefined') {
		XHR = new XMLHttpRequest();
	}
	return XHR;
}

/* ------------- 应用阶段 ---------->>> */
// 用户名检测
function get(url,target,info,msg) {
	var	xmlHttp = XHR();
	var va = getObjValue(target).Trim();
	var re = getObject(info);
	if ((va == null) || (va == "")) {
		re.innerHTML="<font color=\"red\">" + msg + "</font>";
		return;
	}
 	xmlHttp.onreadystatechange = function() { updatePage(xmlHttp,re); };
 	xmlHttp.open("GET", url, true);
 	xmlHttp.send(null);
}

function updatePage(xmlHttp,re) {
	if (xmlHttp.readyState < 4) {
		re.innerHTML="检测中ing...";
	} else {
		re.innerHTML = (xmlHttp.status == 200) ?xmlHttp.responseText:"网络异常，错误代码为：" + xmlHttp.status;
	}
}

function getHits(url,info) {
	var	xmlHttp = XHR();
	var obj = getObject(info);
 	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState < 4) {
			obj.innerHTML="0";
		} else {
			obj.innerHTML = (xmlHttp.status == 200) ?xmlHttp.responseText:"网络异常，错误代码为：" + xmlHttp.status;
		}
	};
 	xmlHttp.open("GET", url, true);
 	xmlHttp.send(null);
}