/*类名：AJAX
创建方法：var ajaxobj=new AJAX;，如果创建失败则返回false
属性：method  -  请求方法，字符串，POST或者GET，默认为POST
　　　url       - 请求URL，字符串，默认为空
　　　async     - 是否异步，true为异步，false为同步，默认为true
　　　content   - 请求的内容，如果请求方法为POST需要设定此属性，默认为空
      backtext  -　默认true当backtext=true时返回XMLHttp.responseText为false时返回XMLHttp.responseXML　
	  gettext 　-　返回值
　　　callback  - 回调函数，即返回响应内容时调用的函数，默认为直接返回，回调函数有一个参数为XMLHttpRequest对象，即定义回调函数时要这样：function mycallback(xmlobj)
    如要获取响应消息 就重写 responseMsg方法 并调用responseMsgBack获取响应消息
    notifyMsg为处理各种广播消息
方法：send()     -  发送请求，无参数
*/

function AJAX() {
	this.resData = null;
	var XMLHttp = false;
	var ObjSelf;
	ObjSelf=this;
	try { XMLHttp=new XMLHttpRequest; }
	catch(e) {
		try { XMLHttp=new ActiveXObject("MSXML2.XMLHttp"); }
		catch(e2) {
			try { XMLHttp=new ActiveXObject("Microsoft.XMLHttp"); }
			catch(e3) { XMLHttp=false; }
		}
	}
	if (!XMLHttp) return false;
	this.method="POST";
	this.url=""
	this.url += (this.url.indexOf("?") >= 0) ? "&nowtime=" + new Date().getTime():"?nowtime=" + new Date().getTime();
	this.async=true;
	this.data="";
	ObjSelf.loadid=""
	this.backtext=true
    
	this.callback=function() {
		this.resData = JSON.parse(this.gettext());
		var tempMsg = this.resData;
		var size = tempMsg.size;
		for(var i=0;i < size;i ++){
			if(tempMsg.msg[i].msgHead.cmdCode == 204){
				if(tempMsg.msg[i].msgHead.errorCode == 0){
					alert("注册成功！");
				}
				else{
					alert("注册失败！");
				}
			}
			if(tempMsg.msg[i].msgHead.cmdCode == 200){
				if(tempMsg.msg[i].msgHead.errorCode == 0){
					alert("登陆成功！");
				}
				else{
					alert("登陆失败！");
				}
			}
		}
	}
	this.responseMsg=function(){
	}

	this.send=function() {
		if(!this.method||!this.url||!this.async) return false;
		XMLHttp.open (this.method, this.url, this.async);
		if(this.method=="POST"){
			XMLHttp.setRequestHeader("Content-Length",(this.data).length); 
			XMLHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		}
   
		XMLHttp.onreadystatechange=function() {
			if(XMLHttp.readyState==4) {
				if (ObjSelf.loadid!="") $CS(ObjSelf.loadid,"none");
				if(XMLHttp.status==200) {
   			        ObjSelf.callback();
   			        ObjSelf.responseMsg();
				}
			}
            else {
				if (ObjSelf.loadid!="") $CS(ObjSelf.loadid,"block");
			}
		}

		if(this.method=="POST") {XMLHttp.send(this.data);}
		else {XMLHttp.send(null);}
	}

	this.gettext=function(){
		if(XMLHttp.readyState==4) {
			if(XMLHttp.status==200) {
				if (this.backtext==true){
					//alert(XMLHttp.responseText);
					return XMLHttp.responseText;
				}else{
					//alert(XMLHttp.responseXML);
					return XMLHttp.responseXML;
				}	 
			}
		}
	}
}

