function Ajax() {
	this.init();
}

Ajax.prototype = {
 	ajax:false,
 	conteudo:'',
 	divisor:'%^%',
 	
	init:function()
	{
		this.ajax = false;
		this.conteudo = '';
		this.GetObj()
	},
	
	GetObj:function()
 	{
 		try{
 			xmlhttp = new XMLHttpRequest();
 		} catch(ee) {
 			try{
 				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 			} catch(e) {
 				try{
 					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
 				} catch(E) {
 					xmlhttp = false;
 				}
 			}
 		}
 		
 		this.ajax = xmlhttp;
 	},
 	
 	send:function(url,destino,execFuncaoID)
 	{
 		this.init();
 		var objParent = this;
 		
 		if (this.ajax) {
			var ajax = this.ajax;
			ajax.open("GET", this.formataURL(url), true);			
			ajax.onreadystatechange = function() {
				if (ajax.readyState == 4) {
					if (ajax.status == 200) {
						var texto = ajax.responseText;
						texto = objParent.formataTexto(texto);
						objParent.conteudo = texto;
						
						switch (execFuncaoID) {
							case 'select':
								objParent.carregaOpcoesSelect(destino);
								break;
							case 'conteudo':
								objParent.escreveConteudo(destino);
								break;
							case 'executa':
								objParent.executaCodigo();
								break;
							default:
								alert('Não fez nada');
								break;
						}
					} else {
						alert("Erro: " + ajax.status + " " + ajax.statusText);
					}
				}
			}		
			ajax.send(null);
			
 		} else {
 			alert("Falha na comunicação com o servidor");
 		}
 		
 		return false;
 	},
 	
 	executaCodigo:function()
 	{
 		eval(this.conteudo);
 	},
 	
 	escreveConteudo:function(destino)
 	{
 		var retorno = this.conteudo.split(this.divisor);
	
		document.getElementById(destino).innerHTML = retorno[0];
		
		if (retorno.length > 1) {
			this.executaCodigo(retorno[1]);
		}
 	},
 	
 	adicionaElementoSelect:function(objeto, texto, valor, selecionado)
 	{
 		var selectsObj = document.getElementById(objeto);
 		
 		if (selectsObj) {
 			tam = selectsObj.length;
 			
 			if (selecionado == "true") {
 				selectsObj.options[tam] = new Option(texto, valor, true, true);
 			} else {
 				selectsObj.options[tam] = new Option(texto, valor, false, false);
 			}
 		}
 	},
 	
 	limpaElementoSelect:function(objeto)
 	{
 		var selectsObj = document.getElementById(objeto);
 		
 		if (selectsObj) {
	 		tam = selectsObj.length;
	 		
	 		while(tam > 0) {
	 			selectsObj.options[tam-1] = null;
	 			tam--;
	 		}
 		}
 	},

	carregaOpcoesSelect:function(objDestino)
	{
		lista = this.conteudo.split("\n");
		this.limpaElementoSelect(objDestino);
		
		for (i = 0; i < lista.length; i++) {
			dados = lista[i].split('||');
			if (dados[1]) {
				if (dados[0].length > 0 && dados[1].length > 0) {
					dados[0] = (dados[0] == '_') ? '' : dados[0];
					this.adicionaElementoSelect(objDestino, dados[1], dados[0], dados[2]);
				}
			}
		}
	},
 	
 	formataTexto:function(str)
 	{
 		str = str.replace(/\+/g," ");
 		str = unescape(str);
 		
 		return str;
	},
	
	formataURL:function(url)
	{
		if (url.match(/\?/)) {
			return url + '&ajax=1';
		} else {
			return url + '?ajax=1';
		}
	}
}

var AjaxObj = new Ajax();