//发出一个异步请求
function sendAsynchronRequest(url,parameter,callback){
	createXMLHttpRequest();
	if(parameter == null){
		
		//配置一个事件触发器(xmlHttp每次状态发生变化都会调用这个触发器,然后由它去调用callback指向的函数)

		xmlHttp.onreadystatechange = callback;
		//建立起对服务器端的调用

		xmlHttp.open("GET",url,true);
		//
		xmlHttp.send(null);
	}else{
		xmlHttp.onreadystatechange = callback;
		xmlHttp.open("POST",url,true);
		xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
		//真正发出异步请求的方法。
		xmlHttp.send(parameter);
	}
}


var xmlHttp;

//创建XMLHttpRequest对象的实例
function createXMLHttpRequest(){
	//作为本地浏览器对象来创建
    if(window.XMLHttpRequest){ //Mozilla 浏览器
        xmlHttp = new XMLHttpRequest();
    }else if(window.ActiveXObject) { //IE浏览器
    //作为Active对象来创建
    	try{
        	xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }catch(e){
        	try {
            	xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        	}catch(e){}
        }
    }
    if(xmlHttp == null){
    	alert("不能创建XMLHttpRequest对象");
    	return false;
    }
}

//################ ajax 库 #######################################


//检查昵称是否存在
function check(url){
	//获得表单中提交的字段值
	var loginName=document.getElementById("mbrId").value;
	var parameter="loginName="+loginName;
	//alert(parameter);
	var info=document.getElementById("info");
	if(loginName.length<1)
	{
		info.innerHTML="<font size='2' color='green'><b>"+
	"用户名不能为空！"+"</b></font>";
	return;
	}
	
	info.innerHTML="<font size='2' color='green'><b>"+
	"正在查询中..."+"</b></font>";
	sendAsynchronRequest(url,parameter,checkCallBack);
}

function checkCallBack()
{
	//alert("readyState= "+xmlHttp.readyState);
	if(xmlHttp.readyState==4){
	//alert("status= "+xmlHttp.status);
		if(xmlHttp.status==200){
			var xmlDoc=xmlHttp.responseXML;
			//alert(xmlHttp.responseText);
			var message=xmlDoc.getElementsByTagName("message")[0];
			var error=xmlDoc.getElementsByTagName("error")[0];
			var member=xmlDoc.getElementsByTagName("member")[0];
			//alert("memberChild="+member.childNodes.length);
			var info=document.getElementById("info");
			
			if(message!=null){
				//alert("message is not null");
				info.innerHTML="<font size='2' color='green'>"+
				message.firstChild.nodeValue+"</b></font>";
			}
			
			if(error!=null){
				//alert("error is not null");
				info.innerHTML="<font size='2' color='green'>"+
				error.firstChild.nodeValue+"</b></font>";
			}
			
		}
	}
}


