

/* ********************************************************************************* *
* Classe de Ajax															 		 *
* ********************************************************************************** */
function Ajax(){
	this.setXmlHttp();
}


Ajax.prototype = {
	
	
	
	
	/* variaveis */
	xmlhttp:null,
	
	
	
	
	/* instancia a váriavel xmlhttp */
	setXmlHttp:function(){
		try{ this.xmlhttp = new XMLHttpRequest(); }
		catch(e){
			try{ this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } 
			catch(ee){
				try{ this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");	}
				catch(eee){
					this.xmlhttp = false;
				}
			}
		}
	},
	
	
	
	
	/* retorna a variável xmlhttp */
	getXmlHttp:function()
	{
		return this.xmlhttp;
	},
	
	
	
	
	/* requisição ao ajax */
	load:function( metodo, url, parametros, div, img, responseFunction ){
		
		var param = "";
		
		/* verifica se tem algum parametro */
		if( (parametros!=null) && (parametros!=undefined) )
			param = parametros;
			
		/* verifica se tem alguma img de loader */
		if( (div!=null && div!=undefined) && (img!=null && img!=undefined) )
			this.loading( div, img );
			
		if( (metodo=="GET") || (metodo=="get") )
			this.loadGet(url, param, div, responseFunction);
		else
			if( (metodo=="POST") || (metodo=="post") )
				this.loadPost(url, param, div, responseFunction);
				
	},
	
	
	
	
	/* requisição por GET */
	loadGet:function( url, params, div, responseFunction ){
		
		var xmlhttp = this.getXmlHttp();
		
		xmlhttp.open("GET", url+"?"+params, true);
		xmlhttp.onreadystatechange = function(){
			if(xmlhttp.readyState == 4){
				var retorno = xmlhttp.responseText;
				
				/* verifica se é pra "jogar" o retorno em algum lugar */
				if( (div!=null) && (div!=undefined) ) {
					var conteudo= document.getElementById(div);
					if( conteudo!=null && conteudo!="undefined" )
						conteudo.innerHTML = retorno;
				}
				
			
				/* verifica se tem alguma função para responseFunction */
				if( (responseFunction!=null) && (responseFunction!=undefined) )
					eval(responseFunction);
					
			}
		}
		xmlhttp.send(null);
		
	},
	
	
	
	
	
	/* requisição por POST */
	loadPost:function( url, params, div, responseFunction ){
		
		var xmlhttp = this.getXmlHttp();
		
		xmlhttp.open("POST", url, true);
		
		/* seta os parametros */
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.setRequestHeader("Content-length", params.length);
		xmlhttp.setRequestHeader("Connection", "close");
		
		xmlhttp.onreadystatechange = function(){
			if( (xmlhttp.readyState==4) && (xmlhttp.status==200) ){
				var retorno = xmlhttp.responseText;
				
				/* verifica se é pra "jogar" o retorno em algum lugar */
				if( (div!=null) && (div!=undefined) ) {
					var conteudo= document.getElementById(div);
					if( conteudo!=null && conteudo!="undefined" )
						conteudo.innerHTML = retorno;
				}
				
				/* verifica se tem alguma função para responseFunction */
				if( (responseFunction!=null) && (responseFunction!=undefined) )
					eval(responseFunction);
					
			}
		}
		xmlhttp.send(params);
	},
	
	
	
	
	/* loading */
	loading:function( div, img ){
		var div = document.getElementById(div);
		div.innerHTML = "<center><img src=\""+img+"\" border=0></center>";
	}
	
}
