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();
};

function mascaraMoeda(objTextBox, SeparadorMilesimo, SeparadorDecimal, e) {
    var sep = 0;
    var key = '';
    var i = j = 0;
    var len = len2 = 0;
    var strCheck = '0123456789';
    var aux = aux2 = '';

    var code;
    if (!e) var e = window.event;
    if (e.keyCode) code = e.keyCode;
    else if (e.which) code = e.which;
    if (!(code == 8 || code == 9 || code > 47 && code < 58)) { return false; }
    else if (code == 8 || code == 9) { return true }

    key = String.fromCharCode(code);

    len = objTextBox.value.length;
    for (i = 0; i < len; i++)
        if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal)) break;
    aux = '';
    for (; i < len; i++)
        if (strCheck.indexOf(objTextBox.value.charAt(i)) != -1) aux += objTextBox.value.charAt(i);
    aux += key;
    len = aux.length;
    if (len == 0) objTextBox.value = '';
    if (len == 1) objTextBox.value = '0' + SeparadorDecimal + '0' + aux;
    if (len == 2) objTextBox.value = '0' + SeparadorDecimal + aux;
    if (len > 2) {
        aux2 = '';
        for (j = 0, i = len - 3; i >= 0; i--) {
            if (j == 3) {
                aux2 += SeparadorMilesimo;
                j = 0;
            }
            aux2 += aux.charAt(i);
            j++;
        }
        objTextBox.value = '';
        len2 = aux2.length;
        for (i = len2 - 1; i >= 0; i--)
            objTextBox.value += aux2.charAt(i);
        objTextBox.value += SeparadorDecimal + aux.substr(len - 2, len);
    }
    return false;
} 

