function AJAX(file)
{
    this.requestFile = file;
    this.MensagemErro = "";
    this.Url="";
    this.ContentType = "application/x-www-form-urlencoded";
    this.Variaveis="";
    this.method="POST";
    this.falha=false;
    this.xmlhttp=null;
    this.textoRetorno="";
    this.xmlRetorno="";
    this.onLoading = function(){};
    this.onLoaded = function(){};
    this.onInteractive = function(){};
    this.onCompletion = function(){};
    this.criarAJAX = function(){
       try {
			this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e1) {
			try {
				this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) {
				this.xmlhttp = null;
			}
		}

		if (! this.xmlhttp) {
			if (typeof XMLHttpRequest != "undefined") {
				this.xmlhttp = new XMLHttpRequest();
			} else {
				this.falha = true;
			}
		}

    };
    
    this.setVariavel = function(nome,valor)
    {
        if(this.Variaveis.length < 3)
            this.Variaveis = nome + "=" + valor;
        else
            this.Variaveis += "&" + nome + "=" + valor;
    };
    
    this.codVar = function(nome,valor)
    {
        var varString = encodeURIComponent(nome) + "=" + encodeURIComponent(valor);
	    return varString;
    }
    
    this.CodificarURL = function(string)
    {
        varArray = string.split('&');
        for (i = 0; i < varArray.length; i++){
			urlVars = varArray[i].split('=');
			if (urlVars[0].indexOf('amp;') != -1){
				urlVars[0] = urlVars[0].substring(4);
			}
			varArray[i] = this.codVar(urlVars[0],urlVars[1]);
		}
		return varArray.join('&');
    };
    
    this.ExecutarRetorno = function()
    {
       alert(textoRetorno);
    }
    
    this.ExecutarAjax = function(Url)
    {
        this.responseStatus = new Array(2);
        if(this.falha){
            alert(this.MensagemErro);
        }else
        {
            
                if(this.xmlhttp)
                {
                    
                    if(this.method == "GET")
                    {
                        var totalurlstring = this.requestFile + "?" + this.Variaveis + ('&'+ Math.ceil(Math.random() * 100000));
					    this.xmlhttp.open(this.method, totalurlstring, true);
                    }else
                    {
                        this.xmlhttp.open(this.method, this.requestFile  + ('&'+Math.ceil(Math.random() * 100000)), true);
                    }
                    
                    if(this.method == "POST")
                    {
                        try {
						    this.xmlhttp.setRequestHeader('Content-Type',ContentType);
					    } catch (e) {}
                    }
                    
                    var self = this;
                    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:
							self.textoRetorno = self.xmlhttp.responseText;
							self.xmlRetorno = self.xmlhttp.responseXML;
							self.responseStatus[0] = self.xmlhttp.status;
							self.responseStatus[1] = self.xmlhttp.statusText;
							self.onCompletion();
							if(self.execute){ self.ExecutarRetorno(); }
							self.Variaveis = "";
						break;
					    }
				    };
				    
				    this.xmlhttp.send(Url);
            }
        }
    };
    
    this.criarAJAX();
};
