function ajax(file,mtype) 
{
	this.xmlhttp = null;

	//função para reset
	this.reset = function()
	{
		this.onLoading = function() { };
  		this.onLoaded = function() { };
  		this.onInteractive = function() { };
  		this.onCompleted = function() { };
  		this.onError = function() { };
		this.onFail = function() { };
				
		this.metodo = "POST"; //metodo
  		this.URLString = ""; //url string
		this.elemento = null; //elemento onde vai ser colocado o texto retornado
		this.elementoObj = null; // objecto do elemento
		this.ficheiro = file; //ficheiro a ser chamado
		this.vars = new Object(); //variaveis
		this.statusResposta = new Array(2); //status da resposta
		this.mimeType=mtype; //mimeType
	};
		
	//funcao para criar xmlhttp object
	this.criarAJAX = function() 
	{
		if(window.XMLHttpRequest)
	  	{
	  		this.xmlhttp = new XMLHttpRequest();  // (FF / Safari / Konqueror / Opera / etc)
	  		
	  		if(typeof(this.mimeType) != "undefined")
			{
				this.xmlhttp.overrideMimeType(this.mimeType); //especifica qual o mimeType
			}
	  	}
	  	else
	  	{
	  		try
	  		{
	  			this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); //activeX (IE5.5+/MSXML2+)
	  		}
	  		catch(e1)
	  		{
	  			try
	  			{
	  				this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); //activeX (IE5+/MSXML1)
	  			}
	  			catch(e2)
	  			{
	  				this.failed = true; //não suporta
	  			}
	  		}
	  	}
	  	
	};
	
	//define o metodo POST ou GET
	this.setMetodo = function(met)
	{
		this.metodo = met;
	};
	
	//define o elemento ou se vai colocar a resposta
	this.setElemento = function(elem)
	{
		this.elemento = elem;
	}

	//cria variavel para enviar para o servidor por GET ou POST
	this.criaVar = function(nome, valor) 
	{
		this.vars[encodeURIComponent(nome)] = encodeURIComponent(valor);
	};
	
	//devolve a resposta
	this.getResposta = function()
	{
		return this.resposta;
	}
	
	//devolve a respostaXML
	this.getRespostaXML = function()
	{
		return this.respostaXML;
	}

	this.criarURLString = function(urlstring) //cria a URLString
	{
		if (urlstring) //se receber uma string por parametro
		{
			if (this.URLString.length) // verifica se ja existe URLString
			{
				this.URLString += '&' + urlstring; // adiciona urlstring à URLString, separadas pelo separador predefinido
			}
			else
			{
				this.URLString = urlstring; //senao iguala urlstring a URLString
			}
			this.URLString += '&';//adiciona o separador por causa das variaveis que vêm a seguir
		}

		// prevenir caching da URLString
		this.criaVar("rndval", new Date().getTime());

		urlstringtemp = new Array(); //cria urlstring temporaria
		
		for (key in this.vars) //percorre o array vars
		{					
			urlstringtemp[urlstringtemp.length] = key + "=" + this.vars[key];
		}
		for (key in this.vars) //percorre o array vars
		{					
			urlstringtemp[urlstringtemp.length] = key + "=" + this.vars[key];
		}
		
		this.URLString += urlstringtemp.join('&'); // junta tudo
		
	};


	this.ajaxRun = function(urlstring,sync)
	{
		if (sync==null)
		{
			sync=true;
		}
		
		if (this.failed) //se falhou a criação
		{
			this.onFail(); // funcao que mostra mensagem quando falha a conexão
		} 
		else
		{
			
			this.criarURLString(urlstring); // cria URLstring
			
			if (this.elemento) // se foi especificado objecto onde colocar resultado
			{
				this.elementoObj = document.getElementById(this.elemento); //vai buscar objecto
			}
			
			if (this.xmlhttp) // se existe objecto xmlhttp
			{
				//verifica metodo
				if (this.metodo == "GET") //GET
				{
					urlstringcompleta = this.ficheiro + "?" + this.URLString; // junta nome de ficheiro e URLString com o separador (defeito: '?' )
					
					this.xmlhttp.open(this.metodo, urlstringcompleta, sync); //abre conexão 
				} 
				else //POST
				{
					this.xmlhttp.open(this.metodo, this.ficheiro, sync); //abre conexão
					
					try 
					{
						this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); //envia header necessário para POST
						this.xmlhttp.setRequestHeader("encoding","ISO-8859-1");
					} 
					catch (e) {	}
					
				}
				
				
				var aux = this; //variavel auxiliar para funcao do onreadystatechange

				this.xmlhttp.onreadystatechange = function() 
				{
					switch (aux.xmlhttp.readyState) 
					{
						//on loading
						case 1: 
								aux.onLoading(); //executa funcao para executar codigo quando estiver no estado 1
								break;
							
						//on loaded	
						case 2: 
								aux.onLoaded(); //executa funcao para executar codigo quando estiver no estado 2
								break;
						
						//on interactive		
						case 3: 
								aux.onInteractive(); //executa funcao para executar codigo quando estiver no estado 3
								break;
						
						// on completed			
						case 4: 
								
								aux.resposta = aux.xmlhttp.responseText; //texto retornado em string normal
								aux.respostaXML = aux.xmlhttp.responseXML; //texto para ser tratado em XML
														
								aux.statusResposta[0] = aux.xmlhttp.status; //código de HTTP
								aux.statusResposta[1] = aux.xmlhttp.statusText; //texto de status
	
								if (aux.elementoObj) //se existe objecto para colocar texto de retorno
								{
									elemNodeName = aux.elementoObj.nodeName; //vai buscar o nodename
									elemNodeName.toLowerCase(); //coloca em minusculas para comparar
									
									//verifica se o elemento é input ou select ou option ou textarea, para usar a propriedade value
									if (elemNodeName == "input" || elemNodeName == "select"	|| elemNodeName == "option"	|| elemNodeName == "textarea")
									{
										aux.elementoObj.value = aux.resposta; // coloca o texto
									}
									else //caso contrario usar a propriedade innerHTML
									{
										aux.elementoObj.innerHTML = aux.resposta; // coloca o texto
									}
								}
								
								if (aux.statusResposta[0] == "200") // se foi bem sucedido 
								{
									aux.onCompleted(); //chama funcao que executa codigo caso bem sucedido
								}
								else //se não foi bem sucedido
								{
									aux.onError(); //chama função de erro
								}
	
								aux.URLString = ""; //reset da URLString
								break;
							
					}
				};
				
				this.xmlhttp.send(this.URLString); //envia 
			}
		}
	};

	this.reset(); // faz reset
	this.criarAJAX(); //cria Ajax
	
}