// 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 = function () {
		return "{XMLHttpRequest Object}";
	}
	
    //->Inicia o Objeto XMLHttpRequest
    function createXMLHttpRequest() {
        if (window.ActiveXObject) {
            objThis.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        } 
        else if (window.XMLHttpRequest) {
            objThis.xmlHttp = new XMLHttpRequest();
        }
    }
    
    //->Funções Publicas
    function intLoad(url,dados,method,bAsync){
        var URLSend;
        var DadosSend;

               
        URLSend = url + "?" + dados;
        
		if ((bAsync != true) && (bAsync != false)){
			if (navigator.appName.indexOf('Microsoft') != -1){
				bAsync = true;
			}else{
				bAsync = false;
			}
		}
        createXMLHttpRequest();
        t_Status = 1;
        objThis.xmlHttp.onreadystatechange = handleStateChange;
		//Em alguns casos na utilização da abertura do methodo "open" de forma assincrona pode dar erro no firefox e opera
		objThis.xmlHttp.open(method, URLSend, bAsync);
        if (method == "POST"){
            //objThis.xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=iso-8859-1");//"application/x-www-form-urlencoded; charset=iso-8859-1
            //objThis.xmlHttp.setRequestHeader("Content-length", DadosSend.length);
            //objThis.xmlHttp.setRequestHeader("Connection", "close");
			
			objThis.xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; iso-8859-1");
			objThis.xmlHttp.setRequestHeader("CharSet", "iso-8859-1")
			objThis.xmlHttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
			objThis.xmlHttp.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
			objThis.xmlHttp.setRequestHeader("Pragma", "no-cache");
			
        }else {
			objThis.xmlHttp.setRequestHeader("Content-type", "text/html; charset=iso-8859-1");
		}
		//Colocar store junto	
		objThis.xmlHttp.send(URLSend);
        
    }
    
    //->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) {
            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 ); 
	}
} 