// JScript File
//###############################################
// Classe Ajax
// Desenvolvida por: Hélvio Júnior
// Data de Criação: 2006-08-25
// Data da Última alteração: 2007-01-03
//###############################################
//


function Ajax(){
    //-> Funções Gerais
    var xmlHttp;
    var funcRetorno;
    var t_Status;
    var objThis = this;
    t_Status = 0;
    this.id = Math.random().toString().replace(".", "");
    eval('window.Ajax_' + this.id + ' = this');
    
    this.xmlHttp = null;
    
    //->Definição das funções publicas
    this.load = intLoad;
    this.toString = "Ajax";
    
    //->Inicia o Objeto XMLHttpRequest
    function createXMLHttpRequest(){
        if (window.XMLHttpRequest) {
            objThis.xmlHttp = new XMLHttpRequest();
        }
        else 
            if (window.ActiveXObject) {
                try {
                    objThis.xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
                } 
                catch (e) {
                    try {
                        objThis.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                    } 
                    catch (E) {
                        objThis.xmlHttp = false;
                    }
                }
            }
    }
    
    //->Funções Publicas
    function intLoad(url, dados, method, bAsync){
        var URLSend;
        var DadosSend;
        URLSend = url + '?time=' + new Date().getTime();
        DadosSend = (dados) + '&';
        if ((dados != null || dados != "") && method == "GET") {
            URLSend = URLSend + "&" + dados;
            DadosSend = null;
        }
        if ((bAsync != true) && (bAsync != false)) {
            if (navigator.appName.indexOf('Microsoft') != -1) {
                bAsync = true;
            }
            else {
                bAsync = false;
            }
        }
        createXMLHttpRequest();
        
        objThis.xmlHttp.onreadystatechange = function(){
            handleStateChange()
        };
        objThis.xmlHttp.open("POST", url + "?" + dados, true)
        objThis.xmlHttp.send('')
        return true;
    }
    
    //->Eventos
    function onStateChange(classState, httpState, retornoTxt, retornoXml){
        //Função para evento somente
        if (objThis.onStateChange != 'undefined') {
            objThis.onStateChange(classState, httpState, retornoTxt, retornoXml);
        }
    }
    
    function handleStateChange(){
        if (objThis.xmlHttp.readyState == 4) {
            if (objThis.xmlHttp.status == 200 || objThis.xmlHttp.readyState == "complete") {
                t_Status = 0;
                try {
                    r_status = objThis.xmlHttp.status;
                } 
                catch (e) {
                    r_status = 15000;
                }
                onStateChange(t_Status, r_status, objThis.xmlHttp.responseText, objThis.xmlHttp.responseXML);
            }
        }
    }
}

function escapeAll(string){
    var decHex = function(dec){
        var chars = '0123456789ABCDEF';
        
        return chars.charAt(Math.floor(dec / 16)) + chars.charAt(dec % 16);
    };
    
    var out = '';
    
    for (var i = 0; i < string.length; i++) {
        var code = string.charCodeAt(i);
        
        if (code > 255) 
            out += '%3F'; //coloca uma interrogacao caso o caractere seja desconhecido
        else 
            out += '%' + decHex(code);
    }
    
    return out;
}


function addEvent(obj, evType, fn){
    if (obj.addEventListener) {
        obj.addEventListener(evType, fn, false);
        return true;
    }
    else 
        if (obj.attachEvent) {
            var r = obj.attachEvent("on" + evType, fn);
            return r;
        }
        else {
            return false;
        }
}

function removeEvent(obj, type, fn){
    if (obj.detachEvent) {
        obj.detachEvent('on' + type, fn);
    }
    else {
        obj.removeEventListener(type, fn, false);
    }
}

