////////////////////Ajax////////////Class///////////////////
//Power By Gloot CopyRight @2006
//Edit Section for ...
//Blog http://blog.sina.com.cn/tecz
//QQ 345268267
////////////////////////////////////////////////////////////

var Try = {
	these: function() {
		var returnValue;
		for (var i = 0; i < arguments.length; i++) {
			var lambda = arguments[i];
			try {
				returnValue = lambda();
				break;
			} catch (e) {}
		}
	//alert(123);
	return returnValue;
	}
}

function grr(rp) {
	if(RegExp.$1)/(.*)/.exec("");
	var re=new RegExp("<result>(.*)<\/result>");
	re.exec(rp);
	if(RegExp.$1) return RegExp.$1;
	return "";
}

function crr(rp) {
	if(RegExp.$1)/(.*)/.exec("");
	var re=new RegExp("<content>(.*)<\/content>");
	re.exec(rp);
	if(RegExp.$1) return RegExp.$1;
	return "";
}

var Ajax = function() {}
var Objxhr = null;//这样定义不行
Ajax.prototype.Init = function(){
	return Try.these(
		function() {return new ActiveXObject("MSXML2.XMLHTTP")},
		function() {return new ActiveXObject("MSXML2.XMLHTTP.5.0")},
		function() {return new ActiveXObject("MSXML2.XMLHTTP.4.0")},
		function() {return new ActiveXObject("MSXML2.XMLHTTP.3.0")},
		function() {return new ActiveXObject("Microsoft.XMLHTTP")},
		function() {return new XMLHttpRequest()}
	) || false;
}

Ajax.prototype.Config = {
	Result:"",
	SucInfo:"",
	FaildInfo:"",
	Url:"",
	returnType:"Compare",  //输入Compare是比较返回的字符是否一致,要指定Result值,//其他返回内容
	ExecFunc:function(ty,w,reVal){ //w表示window,reVal：返回成功信息值
		if (typeof ExecResult == 'function')
			ExecResult(ty,w,reVal);
	},
	sendData:""
}

Ajax.prototype.Action = function(url) {  //同步
	url = url + '&e='+Math.random();
	var xhr = this.Init();    
	xhr.onreadystatechange = function()
	{
		if (xhr.readyState == 4) 
		{
			if (xhr.status == 200)
			{
				new Ajax().FuncResult(this.Config.Spn,xhr);     
			} 
		}
	}
	xhr.open("POST",url,false);
	xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

	xhr.send(null); 

}

Ajax.prototype.Actionfor = function(url,spn) {  //异步

	url = url + '&e='+Math.random();
	Objxhr = null;
	Objxhr = this.Init();  //这样写是为了多异步执行 

	Objxhr.onreadystatechange = function()
	{
		if (Objxhr.readyState == 4) 
		{  
			if (Objxhr.status == 200)
			{
				new Ajax().FuncResult(spn,Objxhr);    
			} 
		}
	}
	Objxhr.open("GET",url,true);

	Objxhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	Objxhr.send(null);  
}

Ajax.prototype.ActionAlert = function(url) //执行alert提示框的同步
{
	url = url + '&e='+Math.random();
	var xhr = this.Init();  //这样写是为了多异步执行 

	xhr.onreadystatechange = function()
	{
		if (xhr.readyState == 4) 
		{
			if (xhr.status == 200)
			{
				new Ajax().AlertResult(xhr);     
			} 
		}
	}
	xhr.open("GET",url,false);

	xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	xhr.send(null);
}

Ajax.prototype.ActionPost = function(url,spn)
{
    url = url + '?e='+Math.random();
	var xhr = this.Init();  //这样写是为了多异步执行 

	xhr.onreadystatechange = function()
	{
		if (xhr.readyState == 4) 
		{
			if (xhr.status == 200)
			{
				new Ajax().postResult(spn,xhr);
				xhr = null;
			} 
		}
	}
	
	try {
	    if (netscape.security.PrivilegeManager.enablePrivilege) 
	    {
            netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserRead');
            netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
        }
	}catch(e) {};
	
	xhr.open("POST",url,true);
    
	xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	//xhr.setRequestHeader("Content-Length",pars.length); 
	xhr.setRequestHeader("Connection", "open");
	
	xhr.send(this.Config.sendData);
}

Ajax.prototype.GetJSONData = function(url,postStr,JSON)
{
    url = url + '?e='+Math.random();
    Objxhr = null;
	Objxhr = this.Init();  //这样写是为了多异步执行 

	Objxhr.onreadystatechange = function()
	{
		if (Objxhr.readyState == 4) 
		{
			if (Objxhr.status == 200)
			{
				new Ajax().jsonResult(Objxhr);
				JSON.onSuccess();
				//xhr = null;
			}else {
				JSON.onFailed();
				Objxhr = null;
			}
		}
	}
	
	Objxhr.open("POST", url, true);
    
	Objxhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	//xhr.setRequestHeader("Content-Length",pars.length);
	Objxhr.setRequestHeader("Connection", "open");
	
	Objxhr.send(postStr);
}

/////////////////////////////////////////////////////////////////////////////////////////////////

Ajax.prototype.onResult = function(v) {
	return v==this.Config.Result;
}

Ajax.prototype.FuncResult = function(spn,xhr)
{   
    
	if (this.Config.returnType == 'Compare')
	{
		if (this.onResult(grr(xhr.responseText)))
		{

			spn.innerHTML = this.Config.SucInfo;
			this.Config.ExecFunc(this.Config.Result);
			if (this.Config.Url!='') 
			{
				window.location.href = this.Config.Url;
			}
		}
		else
		{

			spn.innerHTML =  this.Config.FaildInfo;

		}
	}
	else
	{
	    var cstr = crr(xhr.responseText);
	    
	    if (cstr != '') {
		    spn.innerHTML = crr(xhr.responseText);
		}

		this.Config.ExecFunc(this.Config.Result);
		
	}

}


Ajax.prototype.AlertResult = function(xhr)
{
	if (this.Config.returnType=='Compare')
	{
		if (this.onResult(grr(xhr.responseText)))
		{
			alert(this.Config.SucInfo);
			this.Config.ExecFunc(this.Config.Result);
		}
		else
		{
			alert(this.Config.FaildInfo); 
		}
	}
	else
	{
		alert(crr(xhr.responseText));
		this.Config.ExecFunc(this.Config.Result);
	}
}

Ajax.prototype.postResult = function(spn,xhr)
{
    if (this.Config.returnType == 'Compare')
    {
        if (this.onResult(grr(xhr.responseText)))
		{
			spn.innerHTML = this.Config.SucInfo;
			this.Config.ExecFunc(aj.Config.Result);
		}
		else
		{
			spn.innerHTML = this.Config.FaildInfo; 
		}
    }
    else
	{
		spn.innerHTML = crr(xhr.responseText);
		var result = grr(xhr.responseText);
		
		this.Config.ExecFunc(this.Config.Result,window,result);
	}
}

Ajax.prototype.jsonResult = function(xhr)
{
    var jsonstr = xhr.responseText;
    
    eval("json = " + jsonstr);
    
    //get data json.something json:{username:"123",content:""};
    
    this.Config.ExecFunc(this.Config.Result,window,json);
    xhr = null;
}
///////////////////////////////////////////////////////////////

var WebServices = function() {}
WebServices.Config = {

}
