
var Ajax = function () {
    
    this.xmlHttpObject = null;
    this.onlyHeaders   = false;  // Indica se deve ler somente os cabeçãrios (true), ou os cabeçarios mais o conteúdo do arquivo de requisição (false)
    
    this.initXmlHttpObject = function () {
        this.xmlHttpObject = this.getXmlHttpObject();
    };
    
    this.getXmlHttpObject = function () {
        var xmlHttp = null;
        try {           // Firefox, Opera 8.0+, Safari
            xmlHttp = new XMLHttpRequest();
        } catch(e) {    // Internet Explorer
            try {
                xmlHttp = new ActiveXObject( "Msxml2.XMLHTTP" );
            } catch(e) {
                xmlHttp = new ActiveXObject( "Microsoft.XMLHTTP" );
            }
        }
        return xmlHttp;
    };
    
    this.abort = function () {
        try {
            this.xmlHttpObject.abort();
        } catch (e) { 
            this.xmlHttpObject.stop();
        }
    };
    
    this.requestState = function () {
        //readyState:
        //0 - Não iniciado (Uninitialised) 
        //1 - Carregando (Loading) 
        //2 - Carregado (Loaded) 
        //3 - Interativo (Interactive) 
        //4 - Completado (Completed) 
        try {
            return this.xmlHttpObject.readyState;
        } catch (e) {
            return 0;
        }
    };
    
    this.requestStatus = function () {
        return this.xmlHttpObject.status;
    };
    
    this.requestDataText = function () {
        return this.requestData( 'texto' );
    };
    
    this.requestDataXml = function () {
        return this.requestData( 'xml' );
    };
    
    this.requestData = function ( modo ) {
        if ( (this.requestState() == 2) && this.onlyHeaders ) {
            this.abort();
        }
        if ( this.requestState() == 4 ) {
            if ( this.requestStatus() == 200) {
                if ( modo == 'xml' ) {
                    return this.xmlHttpObject.responseXML;
                } else {
                    return this.xmlHttpObject.responseText;
                }
            } else {
                if ( this.requestStatus() > 0 ) {
                    alert("Ajax - Erro "+this.requestStatus()+" na requisição!");
                }
            }
        }
        if ( modo == 'xml' ) {
            return null;
        } else {
            return '';
        }
    };
    
    this.sendDataGet = function ( url, functionName ) {
        if ( url.length == 0 || functionName.length == 0 ) {
            return;
        }
        if ( url.indexOf("?",0) >= 0 && url.indexOf("sid",0) == -1 ) {
            url = url + "&sid=" + Math.random();
        }
        this.initXmlHttpObject();
        if ( this.xmlHttpObject == null ) {
            alert( "Seu navegador não suporta AJAX!" );
            return;
        }
        this.xmlHttpObject.onreadystatechange = eval(functionName);
        this.xmlHttpObject.open( "GET", url, true );
        //this.xmlHttpObject.setRequestHeader('Content-Type','text/xml');
        //this.xmlHttpObject.setRequestHeader('encoding','ISO-8859-1');
        this.xmlHttpObject.send( null );
    };
    
    this.sendDataPost = function ( url, parameters, functionName ) {
        if ( url.length == 0 || parameters.length == 0 || functionName.length == 0 ) {
            return;
        }
        this.initXmlHttpObject();
        if ( this.xmlHttpObject == null ) {
            alert( "Seu navegador não suporta AJAX!" );
            return;
        }
        this.xmlHttpObject.onreadystatechange = eval(functionName);
        this.xmlHttpObject.open( "POST", url, true );
        this.xmlHttpObject.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
        this.xmlHttpObject.setRequestHeader('Content-length', parameters.length);
        this.xmlHttpObject.setRequestHeader('Connection', 'close');
        this.xmlHttpObject.send( parameters );
    };
    
};




function funcaoVazia() {
}

function getFunctionName( functionName ) {
    return (functionName==null || functionName=='') ? 'funcaoVazia' : functionName;
}

//
// Instância da classe Ajax(), para agilisar a escrita de código que utilizam chamdas em ajax
//
var AjaxDefault = new Ajax();

//
// Retorna o valor de uma requisição de um arquivo em formato TEXTO
//
function requestDataText() {
    return AjaxDefault.requestDataText();
}

//
// Retorna o valor de uma requisição de um arquivo em formato XML
//
function requestDataXml() {
    return AjaxDefault.requestDataXml();
}

//
// Faz a requisição completa pelo metodo GET
// Ex: <a href="javascript:void(0)" onclick="sendDataGet('index.php?ar1=valor1&ar2=valor2')">
//
function sendDataGet( url, functionName ) {
    AjaxDefault.onlyHeaders = false;
    AjaxDefault.sendDataGet( url, getFunctionName(functionName) );
}

//
// Faz a requisição completa pelo metodo POST
// Ex: <a href="javascript:void(0)" onclick="sendDataPostFast('index.php', 'ar1=valor1&ar2=valor2')">
//
function sendDataPost( url, parameters, functionName ) {
    AjaxDefault.onlyHeaders = false;
    AjaxDefault.sendDataPost( url, parameters, getFunctionName(functionName) );
}

//
// Faz uma requisição pelo metodo GET, carregando somente dos cabeçarios
// Ex: <a href="javascript:void(0)" onclick="sendDataGetFast('index.php?ar1=valor1&ar2=valor2')">
//
function sendDataGetFast( url, functionName ) {
    AjaxDefault.onlyHeaders = true;
    AjaxDefault.sendDataGet( url, getFunctionName(functionName) );
}

//
// Faz uma requisição pelo metodo POST, carregando somente dos cabeçarios
// Ex: <a href="javascript:void(0)" onclick="sendDataPostFast('index.php', 'ar1=valor1&ar2=valor2')">
//
function sendDataPostFast( url, parameters, functionName ) {
    AjaxDefault.onlyHeaders = true;
    AjaxDefault.sendDataPost( url, parameters, getFunctionName(functionName) );
}
