/*
var url = '';
var data = '';
var callback = function back(obj){alert(obj.responseText);}
Request.reSend(url,data,callback);
var channel = 'apple';
var timer = setInterval( 'Request.reSend(url,data,callback,channel)', 1000);
*/

var Request = new function(){
	this.pool = new Array();
	this.getXMLHttp = function (channel){	
		if(channel != null){
			for (var a = 0; a < this.pool.length; a++){
				if(this.pool[a]["channel"] == channel){
					if(this.pool[a]["obj"].readyState == 0 || this.pool[a]["obj"].readyState == 4){
						return this.pool[a]["obj"];
					}
					else{
						return "busy";
					}
				}
			} 
			this.pool[this.pool.length] = new Array();
			this.pool[this.pool.length - 1]["obj"] = this.createXMLHttp();
			this.pool[this.pool.length - 1]["channel"] = channel;
			return this.pool[this.pool.length - 1]["obj"];
		}
		
		for (var i = 0; i < this.pool.length; i++){
			if (this.pool[i]["obj"].readyState == 0 || this.pool[i]["obj"].readyState == 4){
				return this.pool[i]["obj"];
			}
		} 
		this.pool[this.pool.length] = new Array();
		this.pool[this.pool.length - 1]["obj"] = this.createXMLHttp();
		this.pool[this.pool.length - 1]["channel"] = "";
		return this.pool[this.pool.length - 1]["obj"];
	}
	
	this.createXMLHttp = function (){
		if(window.XMLHttpRequest){
			var xmlObj = new XMLHttpRequest();
		} 
		else{
			var MSXML = ['Microsoft.XMLHTTP', 'MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP'];
			for(var n = 0; n < MSXML.length; n++){
				try{
					var xmlObj = new ActiveXObject(MSXML[n]);        
					break;
				}
				catch(e){
				}
			}
		} 
		return xmlObj;
	}
	this.getTagValue=function(doc, tag){
     if(!doc.getElementsByTagName(tag)[0] || doc.getElementsByTagName(tag)[0].firstChild==null){return '';}
     return doc.getElementsByTagName(tag)[0].firstChild.nodeValue;
    }
	this.reSend = function (url,data,callback,channel,strBack){
		var objXMLHttp = this.getXMLHttp(channel)
		if(typeof(objXMLHttp) != "object"){
			return ; //上次的请求如果没有完成，那么这次reSend将停止退出
		}
		url += (url.indexOf("?") >= 0) ? "&nowtime=" + new Date().getTime() : "?nowtime=" + new Date().getTime();
		url +="&__SCRIPTCALLBACKID="+strBack;
		
		if(data == ""){
			objXMLHttp.open('GET' , url, true);
			objXMLHttp.send('');
		}
		else {
			objXMLHttp.open('POST' , url, true);
			objXMLHttp.setRequestHeader("Content-Length",data.length); 
			objXMLHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			objXMLHttp.setRequestHeader("Cache-Control","no-cache");  
			objXMLHttp.send(data);	
		}
		if(typeof(callback) == "function" ){
			objXMLHttp.onreadystatechange = function (){
				if (objXMLHttp.readyState == 4){
					if(objXMLHttp.status == 200 || objXMLHttp.status == 304){
					   if(getTagValue(objXMLHttp.responseXML,"Success")=="-10000"){
                    alert("您还没有登陆呢！");}
                else if(getTagValue(objXMLHttp.responseXML,"Success")=="-10001"){
                    alert("对不起，您没有操作权限！");}
                    else{
           callback (objXMLHttp );
           }
						
						return;
					}
					else{
						//window.status="Error loading page\n"+ objXMLHttp.status +":"+ objXMLHttp.statusText;
						return;
					}
			  }
		  }
	  }
  }
}

function getTagValue(doc, tag){
     if(!doc.getElementsByTagName(tag)[0] || doc.getElementsByTagName(tag)[0].firstChild==null){return '';}
     return doc.getElementsByTagName(tag)[0].firstChild.nodeValue;
}
