var esIE=(window.navigator.appName.indexOf('Explorer')>0);
var XHRFactory;
if (!(esIE && navigator.appVersion.indexOf("MSIE 5.0")>0)){
XHRFactory = (function(){
 var stack = new Array();
 var poolSize = 5;
 
 var nullFunction = function() {};
 
 function createXHR() {
    var xmlHttpReq;

	try {
		xmlHttpReq = new XMLHttpRequest();
	}catch (e){
		var XMLHTTP_IDS = new Array('MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP','Microsoft.XMLHTTP' );
		var success = false;
		for (var i=0;i < XMLHTTP_IDS.length && !success; i++) {
			try {
				xmlHttpReq = new ActiveXObject(XMLHTTP_IDS[i]);
				success = true;
			} catch (e) {}
		}
		if (!success) xmlHttpReq = null;
	}
	if (!xmlHttpReq) return null;
        return xmlHttpReq;
}

  for (var i = 0; i < poolSize; i++) {
  stack.push(createXHR());
 }
 
 return ({
  release:function(xhr){
   xhr.onreadystatechange = nullFunction;
   stack.push(xhr);
  },
  getInstance:function(){
   if (stack.length < 1) {
    return createXHR();
   } else {
    return stack.pop();
   }
  },
  toString:function(){
   return "stack size = " + stack.length;
  }
 });
})();
}

function sack(file) {
	this.xmlhttp = null;
        this.resetData = function() {
		this.method = "POST";
  		this.queryStringSeparator = "?";
		this.argumentSeparator = "&";
		this.URLString = "";
		this.encodeURIString = true;
  		this.execute = false;
  		this.element = null;
		this.elementObj = null;
		this.requestFile = file;
                this.sincronismo = true;
		this.vars = new Object();
		this.responseStatus = new Array(2);
  	};
	this.resetFunctions = function() {
  		this.onLoading = function() {};
  		this.onLoaded = function() {};
  		this.onInteractive = function() {};
  		this.onCompletion = function() {};
  		this.onError = function() {};
		this.onFail = function() {};
	};
	this.reset = function() {
		this.resetFunctions();
		this.resetData();
	};
         
	this.createAJAX = function() {
            if (esIE && navigator.appVersion.indexOf("MSIE 5.0")>0){
		try {
			this.xmlhttp = new XMLHttpRequest();
		} catch (e1) {
                        var XMLHTTP_IDS = new Array('MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP','Microsoft.XMLHTTP' );
                        var success = false;
                        for (var i=0;i < XMLHTTP_IDS.length && !success; i++) {
                            try {
				this.xmlhttp = new ActiveXObject(XMLHTTP_IDS[i]);
				success = true;
                            } catch (e) {}
                        }
                        if (!success) this.xmlhttp = null;
		}

		if (! this.xmlhttp) {
			if (typeof XMLHttpRequest != "undefined") {
				this.xmlhttp = new XMLHttpRequest();
			} else {
				this.failed = true;
			}
		}
            }else {
                this.xmlhttp = XHRFactory.getInstance();
            }
	};
	this.setVar = function(name, value){
		this.vars[name] = Array(value, false);
	};
	this.encVar = function(name, value, returnvars) {
		if (true == returnvars) {
			return Array(encodeURIComponent(name), encodeURIComponent(value));
		} else {
			this.vars[encodeURIComponent(name)] = Array(encodeURIComponent(value), true);
		}
	}
	this.processURLString = function(string, encode) {
		encoded = encodeURIComponent(this.argumentSeparator);
		regexp = new RegExp(this.argumentSeparator + "|" + encoded);
		varArray = string.split(regexp);
		for (i = 0; i < varArray.length; i++){
			urlVars = varArray[i].split("=");
			if (true == encode){
				this.encVar(urlVars[0], urlVars[1]);
			} else {
				this.setVar(urlVars[0], urlVars[1]);
			}
		}
	}
	this.createURLString = function(urlstring) {
		if (this.encodeURIString && this.URLString.length) {
			this.processURLString(this.URLString, true);
		}
		if (urlstring) {
			if (this.URLString.length) {
				this.URLString += this.argumentSeparator + urlstring;
			} else {
				this.URLString = urlstring;
			}
		}
		urlstringtemp = new Array();
		for (key in this.vars) {
			if (false == this.vars[key][1] && true == this.encodeURIString) {
				encoded = this.encVar(key, this.vars[key][0], true);
				delete this.vars[key];
				this.vars[encoded[0]] = Array(encoded[1], true);
				key = encoded[0];
			}

			urlstringtemp[urlstringtemp.length] = key + "=" + this.vars[key][0];
		}
		if (urlstring){
			this.URLString += this.argumentSeparator + urlstringtemp.join(this.argumentSeparator);
		} else {
			this.URLString += urlstringtemp.join(this.argumentSeparator);
		}
	}
	this.runResponse = function() {
		eval(this.response);
	}
	this.runAJAX = function(urlstring) {
		if (this.failed) {
			this.onFail();
		} else {
			this.createURLString(urlstring);
			if (this.element) {
				this.elementObj = document.getElementById(this.element);
			}
			if (this.xmlhttp) {
				var self = this;
				if (this.method == "GET") {
					totalurlstring = this.requestFile + this.queryStringSeparator + this.URLString;
					this.xmlhttp.open(this.method, totalurlstring, this.sincronismo);
				} else {
					this.xmlhttp.open(this.method, this.requestFile, this.sincronismo);
					try {
						this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
					} catch (e) { }
				}
                                
                                 
				this.xmlhttp.onreadystatechange = function() {
					switch (self.xmlhttp.readyState) {
						case 1:
							self.onLoading();
							break;
						case 2:
							self.onLoaded();
							break;
						case 3:
							self.onInteractive();
							break;
						case 4:
                                                        clearTimeout(timer);
							self.response = self.xmlhttp.responseText;
                                                        if (self.response.indexOf('Bad req')>-1){
                                                            self.response='';
                                                        }
							self.responseXML = self.xmlhttp.responseXML;
                                                        try {
							self.responseStatus[0] = self.xmlhttp.status;
							self.responseStatus[1] = self.xmlhttp.statusText;
                                                        }catch(e4){
                                                        self.responseStatus[0] = "0";
							self.responseStatus[1] = ""; 
                                                        }
							if (self.execute) {
								self.runResponse();
							}
							if (self.elementObj) {
								elemNodeName = self.elementObj.nodeName;
								elemNodeName.toLowerCase();
								if (elemNodeName == "input" || elemNodeName == "select" || elemNodeName == "option" || elemNodeName == "textarea") {
									self.elementObj.value = self.response;
								} else {
									self.elementObj.innerHTML = self.response;
								}
							}
							if (self.responseStatus[0] == "200") {
								self.onCompletion();
							} else {
								self.onError();
							}
							self.URLString = "";
                                                        if (!(esIE && navigator.appVersion.indexOf("MSIE 5.0")>0)){
                                                            XHRFactory.release(self.xmlhttp);
                                                        }
 							break;
					}
				};
                                var timer = setTimeout(
                                     function() {
                                         self.xmlhttp.onreadystatechange = function(){};
                                         self.onError();
                                     }
                                     ,7000);
				this.xmlhttp.send(this.URLString);
                                 //Codigo para cuando la llamada es sincrona
                                if(this.sincronismo==false){
                                    clearTimeout(timer);
                                    self.response = self.xmlhttp.responseText;
                                    if (self.response.indexOf('Bad req')>-1){
                                        self.response='';

                                    }
                                    self.responseXML = self.xmlhttp.responseXML;
                                    try {
                                        self.responseStatus[0] = self.xmlhttp.status;
					self.responseStatus[1] = self.xmlhttp.statusText;
                                    }catch(e4){
                                        self.responseStatus[0] = "0";
                                        self.responseStatus[1] = ""; 
                                    }
                                    if (self.execute) {
                                        self.runResponse();
                                    }
                                    if (self.elementObj) {
                                        elemNodeName = self.elementObj.nodeName;
					elemNodeName.toLowerCase();
                                        if (elemNodeName == "input" || elemNodeName == "select" || elemNodeName == "option" || elemNodeName == "textarea") {
                                            self.elementObj.value = self.response;
					} else {
                                            self.elementObj.innerHTML = self.response;
					}
                                    }
                                    if (self.responseStatus[0] == "200") {
                                        self.onCompletion();
                                    } else {
                                        self.onError();
                                    }
                                    self.URLString = "";
                                    if (!(esIE && navigator.appVersion.indexOf("MSIE 5.0")>0)){
                                        XHRFactory.release(self.xmlhttp);
                                    }
                                }
                                 //Fin codigo para llamada sincrona                                 
			}
		}
	};
	this.reset();
	this.createAJAX();
}

