function cadastrar_ajax(tipo) {
	var cadastrar = false;
	if (window.XMLHttpRequest) { //mozilla.
		cadastrar = new XMLHttpRequest();
	} else if (window.ActiveXObject) { //IE
		cadastrar = ObjMSXMLHttp();
	}
	switch (tipo) {
		case "atendimento":
			var formulario = document.getElementById("form_cadastrar");
			var campo_cidade = trim(formulario.campo_cidade.value);
			var campo_estado_selecionado = formulario.campo_estado.selectedIndex;
			var campo_estado = trim(formulario.campo_estado.options[campo_estado_selecionado].value);
			var campo_unidade_selecionado = formulario.campo_unidade.selectedIndex;
			var campo_unidade = trim(formulario.campo_unidade.options[campo_unidade_selecionado].value);

			if(campo_cidade.length == 0) { alert("Campo Cidade Em Branco."); loading("hide"); return false; }
			if(campo_estado.length == 0) { alert("Campo Estado Em Branco."); loading("hide"); return false; }
			if(campo_unidade.length == 0) { alert("Campo Unidade Em Branco."); loading("hide"); return false; }
			
			var dados = "acao=cadastrar";
			dados += "&tipo=atendimento";
			dados += "&campo_cidade="+campo_cidade;
			dados += "&campo_estado="+campo_estado;
			dados += "&campo_unidade="+campo_unidade;
		break;
		
		case "cliente":
			var formulario = document.getElementById("form_cadastrar");
			var campo_nome = trim(formulario.campo_nome.value);
			if(campo_nome.length == 0) { alert("Campo Nome Em Branco."); loading("hide"); return false; }
			var dados = "acao=cadastrar";
			dados += "&tipo=cliente";
			dados += "&campo_nome="+campo_nome;
		break;

		case "case":
			var formulario = document.getElementById("form_cadastrar");
			var campo_empresa = trim(formulario.campo_empresa.value);
			var campo_responsavel = trim(formulario.campo_responsavel.value);
			var campo_cargo = trim(formulario.campo_cargo.value);
			var campo_cliente_desde = trim(formulario.campo_cliente_desde.value);
			var campo_mensagem = trim(formulario.campo_mensagem.value);
			if(campo_empresa.length == 0) { alert("Campo Empresa Em Branco."); loading("hide"); return false; }
			if(campo_responsavel.length == 0) { alert("Campo Responsável Em Branco."); loading("hide"); return false; }
			if(campo_cargo.length == 0) { alert("Campo Cargo Em Branco."); loading("hide"); return false; }
			if(campo_cliente_desde.length == 0) { alert("Campo Cliente Desde Em Branco."); loading("hide"); return false; }
			if(campo_mensagem.length == 0) { alert("Campo Mensagem Em Branco."); loading("hide"); return false; }
			var dados = "acao=cadastrar";
			dados += "&tipo=case";
			dados += "&campo_empresa="+campo_empresa;
			dados += "&campo_responsavel="+campo_responsavel;
			dados += "&campo_cargo="+campo_cargo;
			dados += "&campo_cliente_desde="+campo_cliente_desde;
			dados += "&campo_mensagem="+campo_mensagem;
		break;
		
		case "contato":
			var formulario = document.getElementById("form_cadastrar");
			var campo_assunto = trim(formulario.campo_assunto.value);
			var campo_email = trim(formulario.campo_email.value);

			if(campo_assunto.length == 0) { alert("Campo Assunto Em Branco."); loading("hide"); return false; }
			if(campo_email.length == 0) { alert("Campo Email Em Branco."); loading("hide"); return false; }
			var email = campo_email.split(",");
			var regex = /^([A-Za-z0-9]+)([A-Za-z0-9\.-_]+)@([A-Za-z0-9\.-_]+)\.([A-Za-z0-9]+)([A-Za-z0-9]?)/;
			for (var i = 0; i <= (email.length -1); i++) {
				if(!regex.test(trim(email[i]))) {
					loading("hide");
					return alert("Campo Email Inválido.");;
				}			
			}
			var dados = "acao=cadastrar";
			dados += "&tipo=contato";
			dados += "&campo_assunto="+campo_assunto;
			dados += "&campo_email="+campo_email;
		break;
		
		case "orcamento":
			var formulario = document.getElementById("form_cadastrar");
			var campo_email = trim(formulario.campo_email.value);

			if(campo_email.length == 0) { alert("Campo Email Em Branco."); loading("hide"); return false; }
			var email = campo_email.split(",");
			var regex = /^([A-Za-z0-9]+)([A-Za-z0-9\.-_]+)@([A-Za-z0-9\.-_]+)\.([A-Za-z0-9]+)([A-Za-z0-9]?)/;
			for (var i = 0; i <= (email.length -1); i++) {
				if(!regex.test(trim(email[i]))) {
					loading("hide");
					return alert("Campo Email Inválido.");;
				}			
			}
			var dados = "acao=cadastrar";
			dados += "&tipo=orcamento";
			dados += "&campo_email="+campo_email;
		break;

		case "unidade":
			var formulario = document.getElementById("form_cadastrar");
			var campo_tipo_selecionado = formulario.campo_tipo.selectedIndex;
			var campo_tipo = trim(formulario.campo_tipo.options[campo_tipo_selecionado].value);
			var campo_nome = trim(formulario.campo_nome.value);
			var campo_razao_social = trim(formulario.campo_razao_social.value);
			var campo_endereco = trim(formulario.campo_endereco.value);
			var campo_bairro = trim(formulario.campo_bairro.value);
			var campo_cep = trim(formulario.campo_cep.value);
			var campo_cidade = trim(formulario.campo_cidade.value);
			var campo_estado_selecionado = formulario.campo_estado.selectedIndex;
			var campo_estado = trim(formulario.campo_estado.options[campo_estado_selecionado].value);
			var campo_telefone = trim(formulario.campo_telefone.value);
			var campo_fax = trim(formulario.campo_fax.value);
			var campo_contato = trim(formulario.campo_contato.value);
			var campo_email = trim(formulario.campo_email.value);
			var campo_marcador_selecionado = formulario.campo_marcador.selectedIndex;
			var campo_marcador = trim(formulario.campo_marcador.options[campo_marcador_selecionado].value);
			var campo_posx = trim(formulario.campo_posx.value);
			var campo_posy = trim(formulario.campo_posy.value);

			if(campo_tipo.length == 0) { alert("Campo Tipo Em Branco."); loading("hide"); return false; }
			if(campo_nome.length == 0) { alert("Campo Nome Em Branco."); loading("hide"); return false; }
			if(campo_razao_social.length == 0) { alert("Campo Razão Social Em Branco."); loading("hide"); return false; }
			if(campo_endereco.length == 0) { alert("Campo Endereço Em Branco."); loading("hide"); return false; }
			if(campo_bairro.length == 0) { alert("Campo Bairro Em Branco."); loading("hide"); return false; }
			if(campo_cep.length == 0) { alert("Campo Cep Em Branco."); loading("hide"); return false; }
			if(campo_cidade.length == 0) { alert("Campo Cidade Em Branco."); loading("hide"); return false; }
			if(campo_estado.length == 0) { alert("Campo Estado Em Branco."); loading("hide"); return false; }
			if(campo_telefone.length == 0) { alert("Campo Telefone Em Branco."); loading("hide"); return false; }
			if(campo_fax.length == 0) { alert("Campo Fax Em Branco."); loading("hide"); return false; }
			if(campo_contato.length == 0) { alert("Campo Contato Em Branco."); loading("hide"); return false; }
			if(campo_email.length == 0) { alert("Campo Email Em Branco."); loading("hide"); return false; }
			var regex = /^([0-9a-zA-Z]+([_.-]?[0-9a-zA-Z]+)*@[0-9a-zA-Z]+[0-9,a-z,A-Z,.,-]*(.){1}[a-zA-Z]{2,4})+$/;
			if(!regex.test(campo_email)) { alert("Campo Email Inválido."); loading("hide"); return false; }
			if(campo_marcador.length == 0) { alert("Campo Marcador Em Branco."); loading("hide"); return false; }
			if(campo_posx.length == 0) { alert("Campo Pos X Marcador Em Branco."); loading("hide"); return false; }
			if(campo_posy.length == 0) { alert("Campo Pos Y Marcador Em Branco."); loading("hide"); return false; }
			
			var dados = "acao=cadastrar";
			dados += "&tipo=unidade";
			dados += "&campo_tipo="+campo_tipo;
			dados += "&campo_nome="+campo_nome;
			dados += "&campo_razao_social="+campo_razao_social;
			dados += "&campo_endereco="+campo_endereco;
			dados += "&campo_bairro="+campo_bairro;
			dados += "&campo_cep="+campo_cep;
			dados += "&campo_cidade="+campo_cidade;
			dados += "&campo_estado="+campo_estado;
			dados += "&campo_telefone="+campo_telefone;
			dados += "&campo_fax="+campo_fax;
			dados += "&campo_contato="+campo_contato;
			dados += "&campo_email="+campo_email;
			dados += "&campo_marcador="+campo_marcador;
			dados += "&campo_posx="+campo_posx;
			dados += "&campo_posy="+campo_posy;
		break;
		
		case "usuario":
			var formulario = document.getElementById("form_cadastrar");
			var campo_nome = trim(formulario.campo_nome.value);
			var campo_login = trim(formulario.campo_login.value);
			var campo_senha = trim(formulario.campo_senha.value);
			if(campo_nome.length == 0) { alert("Campo Nome Em Branco."); loading("hide"); return false; }
			if(campo_login.length == 0) { alert("Campo Login Em Branco."); loading("hide"); return false; }
			if(campo_senha.length == 0) { alert("Campo Senha Em Branco."); loading("hide"); return false; }
			if(campo_login.length > 8) { alert("Máximo 8 caracteres para campo Login."); loading("hide"); return false; }
			if(campo_senha.length > 8) { alert("Máximo 8 caracteres para campo Senha."); loading("hide"); return false; }
			var dados = "acao=cadastrar";
			dados += "&tipo=usuario";
			dados += "&campo_nome="+campo_nome;
			dados += "&campo_login="+campo_login;
			dados += "&campo_senha="+campo_senha;
		break;
	}
	
	cadastrar.open("POST", "intranet.php", true);
	cadastrar.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	cadastrar.onreadystatechange = function() {
		if(cadastrar.readyState == 1) {
			loading("show");
		}
		if(cadastrar.readyState == 4) {
			if(cadastrar.status == 200) {
				var response = trim(cadastrar.responseText);
				if(response > 0) {
					var id = trim(response);
					return incluir_cadastrar(id, tipo, '0');
				} else {
					loading("hide");
					return alert(response);
				}
			} else {
				loading("hide");
				return alert("Erro ao carregar os dados");
			}
		}
	}
	cadastrar.send(dados);
}
function incluir_cadastrar(id, tipo, erro) {
	if(erro != "0") {
		var elemento_post = document.getElementById("post");
		if(elemento_post) {
			elemento_post.parentNode.removeChild(elemento_post);
		}
		loading("hide");
		return alert(trim(erro));
	} else {
		var elemento_post = document.getElementById("post");
		if(elemento_post) {
			elemento_post.parentNode.removeChild(elemento_post);
		}
		var elemento = document.getElementById("lista");
		if(elemento) {
			if(document.getElementById("sem_registro")) {
				var sem_registro = document.getElementById("sem_registro");
				var remover = sem_registro.parentNode;
				remover.removeChild(sem_registro);
			}
			var formulario = document.getElementById("form_cadastrar");
			switch (tipo) { // ELEMENTO A SER INCLUIDO
				case "atendimento": /*################################################## UNIDADE*/
					var campo_cidade = trim(formulario.campo_cidade.value);
					var campo_estado_selecionado = formulario.campo_estado.selectedIndex;
					var campo_estado = trim(formulario.campo_estado.options[campo_estado_selecionado].value);
					var campo_unidade_selecionado = formulario.campo_unidade.selectedIndex;
					var campo_unidade = trim(formulario.campo_unidade.options[campo_unidade_selecionado].value);

					var div = document.createElement("div");
						div.setAttribute("id", "atendimento"+id);
						div.setAttribute("class", "lista")
						div.setAttribute("className", "lista")
							var ul = document.createElement("ul");
								var li = document.createElement("li")
									var img = document.createElement("img");
										img.setAttribute("src", "estrutura/erro.gif");
										img.setAttribute("style", "cursor: pointer;");
										img.onclick = function () { excluir(id, 'atendimento'); };
								li.appendChild(img);
							ul.appendChild(li);
						div.appendChild(ul);
							var ul = document.createElement("ul");
								var li = document.createElement("li");
									var strong = document.createElement("strong");
										strong.appendChild(document.createTextNode("Cidade: "));
								li.appendChild(strong);
							ul.appendChild(li);
								var li2 = document.createElement("li");
									var span = document.createElement("span");
										span.setAttribute("id", "cidade"+id);
										span.appendChild(document.createTextNode(campo_cidade));
										span.onclick = function () { editar(id, this, 'input', 'atendimento', '1'); };
								li2.appendChild(span);
							ul.appendChild(li2);
						div.appendChild(ul);
							var ul = document.createElement("ul");
								var li = document.createElement("li");
									var strong = document.createElement("strong");
										strong.appendChild(document.createTextNode("Estado: "));
								li.appendChild(strong);
							ul.appendChild(li);
								var li2 = document.createElement("li");
									var span = document.createElement("span");
										span.setAttribute("id", "estado"+id);
										span.appendChild(document.createTextNode(campo_estado));
										span.onclick = function () { editar(id, this, 'select_estado', 'atendimento', '2'); };
								li2.appendChild(span);
							ul.appendChild(li2);
						div.appendChild(ul);
							var ul = document.createElement("ul");
								var li = document.createElement("li");
									var strong = document.createElement("strong");
										strong.appendChild(document.createTextNode("Unidade Responsável: "));
								li.appendChild(strong);
							ul.appendChild(li);
								var li2 = document.createElement("li");
									var span = document.createElement("span");
										span.setAttribute("id", "unidade"+id);
										span.appendChild(document.createTextNode(campo_unidade));
										span.onclick = function () { editar(id, this, 'select_unidade', 'atendimento', '3'); };
								li2.appendChild(span);
							ul.appendChild(li2);
						div.appendChild(ul);
				break;
				
				case "case": /*################################################ CASE*/
					var empresa = trim(formulario.campo_empresa.value);
					var responsavel = trim(formulario.campo_responsavel.value);
					var cargo = trim(formulario.campo_cargo.value);
					var cliente_desde = trim(formulario.campo_cliente_desde.value);
					var mensagem_original = trim(formulario.campo_mensagem.value);
					var mensagem = mensagem_original.replace(/\n/gi, "<br>");

					var div = document.createElement("div");
						div.setAttribute("id", "case"+id);
						div.setAttribute("class", "lista");
						div.setAttribute("className", "lista");
					var ul = document.createElement("ul");
						var li = document.createElement("li");
							var img = document.createElement("img");
								img.setAttribute("src", "estrutura/erro.gif");
								img.setAttribute("style", "cursor: pointer;");
								img.onclick = function () { excluir(id, 'case'); };
						li.appendChild(img);
					ul.appendChild(li);
				div.appendChild(ul);
					var ul = document.createElement("ul");
						var li = document.createElement("li");
							var strong = document.createElement("strong");
								strong.appendChild(document.createTextNode("Empresa: "));
						li.appendChild(strong);
					ul.appendChild(li);
						var li2 = document.createElement("li");
							var span = document.createElement("span");
								span.setAttribute("id", "empresa"+id);
								span.appendChild(document.createTextNode(empresa));
								span.onclick = function () { editar(id, this, 'input', 'case', '1'); };
						li2.appendChild(span);
					ul.appendChild(li2);
				div.appendChild(ul);
					var ul = document.createElement("ul");
						var li = document.createElement("li");
							var strong = document.createElement("strong");
								strong.appendChild(document.createTextNode("Responsável: "));
						li.appendChild(strong);
					ul.appendChild(li);
						var li2 = document.createElement("li");
							var span = document.createElement("span");
								span.setAttribute("id", "responsavel"+id);
								span.appendChild(document.createTextNode(responsavel));
								span.onclick = function () { editar(id, this, 'input', 'case', '2'); };
						li2.appendChild(span);
					ul.appendChild(li2);
				div.appendChild(ul);
					var ul = document.createElement("ul");
						var li = document.createElement("li");
							var strong = document.createElement("strong");
								strong.appendChild(document.createTextNode("Cargo: "));
						li.appendChild(strong);
					ul.appendChild(li);
						var li2 = document.createElement("li");
							var span = document.createElement("span");
								span.setAttribute("id", "cargo"+id);
								span.appendChild(document.createTextNode(cargo));
								span.onclick = function () { editar(id, this, 'input', 'case', '3'); };
						li2.appendChild(span);
					ul.appendChild(li2);
				div.appendChild(ul);
					var ul = document.createElement("ul");
						var li = document.createElement("li");
							var strong = document.createElement("strong");
								strong.appendChild(document.createTextNode("Cliente Desde: "));
						li.appendChild(strong);
					ul.appendChild(li);
						var li2 = document.createElement("li");
							var span = document.createElement("span");
								span.setAttribute("id", "cliente_desde"+id);
								span.appendChild(document.createTextNode(cliente_desde));
								span.onclick = function () { editar(id, this, 'input', 'case', '4'); };
						li2.appendChild(span);
					ul.appendChild(li2);
				div.appendChild(ul);
					var ul = document.createElement("ul");
						var li = document.createElement("li");
							var strong = document.createElement("strong");
								strong.appendChild(document.createTextNode("Mensagem: "));
						li.appendChild(strong);
					ul.appendChild(li);
						var li2 = document.createElement("li");
							var span = document.createElement("span");
								span.setAttribute("id", "mensagem"+id);
								span.innerHTML = mensagem;
								span.onclick = function () { editar(id, this, 'textarea', 'case', '5'); };
						li2.appendChild(span);
					ul.appendChild(li2);
				div.appendChild(ul);
				break;
				
				
				case "contato": /*################################################ CONTATO*/
					var assunto = trim(formulario.campo_assunto.value);
					var email = trim(formulario.campo_email.value);

					var div = document.createElement("div");
						div.setAttribute("id", "contato"+id);
						div.setAttribute("class", "lista");
						div.setAttribute("className", "lista");
					var ul = document.createElement("ul");
						var li = document.createElement("li");
							var img = document.createElement("img");
								img.setAttribute("src", "estrutura/erro.gif");
								img.setAttribute("style", "cursor: pointer;");
								img.onclick = function () { excluir(id, 'contato'); };
						li.appendChild(img);
					ul.appendChild(li);
				div.appendChild(ul);
					var ul = document.createElement("ul");
						var li = document.createElement("li");
							var strong = document.createElement("strong");
								strong.appendChild(document.createTextNode("Assunto: "));
						li.appendChild(strong);
					ul.appendChild(li);
						var li2 = document.createElement("li");
							var span = document.createElement("span");
								span.setAttribute("id", "assunto"+id);
								span.appendChild(document.createTextNode(assunto));
								span.onclick = function () { editar(id, this, 'input', 'contato', '1'); };
						li2.appendChild(span);
					ul.appendChild(li2);
				div.appendChild(ul);
					var ul = document.createElement("ul");
						var li = document.createElement("li");
							var strong = document.createElement("strong");
								strong.appendChild(document.createTextNode("Email: "));
						li.appendChild(strong);
					ul.appendChild(li);
						var li2 = document.createElement("li");
							var span = document.createElement("span");
								span.setAttribute("id", "email"+id);
								span.appendChild(document.createTextNode(email));
								span.onclick = function () { editar(id, this, 'input_email', 'contato', '2'); };
						li2.appendChild(span);
					ul.appendChild(li2);
				div.appendChild(ul);
				break;
				
				case "orcamento": /*################################################ ORCAMENTO*/
					var email = trim(formulario.campo_email.value);

					var div = document.createElement("div");
						div.setAttribute("id", "orcamento"+id);
						div.setAttribute("class", "lista");
						div.setAttribute("className", "lista");
					var ul = document.createElement("ul");
						var li = document.createElement("li");
							var img = document.createElement("img");
								img.setAttribute("src", "estrutura/erro.gif");
								img.setAttribute("style", "cursor: pointer;");
								img.onclick = function () { excluir(id, 'orcamento'); };
						li.appendChild(img);
					ul.appendChild(li);
				div.appendChild(ul);
					var ul = document.createElement("ul");
						var li = document.createElement("li");
							var strong = document.createElement("strong");
								strong.appendChild(document.createTextNode("Email: "));
						li.appendChild(strong);
					ul.appendChild(li);
						var li2 = document.createElement("li");
							var span = document.createElement("span");
								span.setAttribute("id", "email"+id);
								span.appendChild(document.createTextNode(email));
								span.onclick = function () { editar(id, this, 'input_email', 'orcamento', '2'); };
						li2.appendChild(span);
					ul.appendChild(li2);
				div.appendChild(ul);
				break;


				case "unidade": /*################################################## UNIDADE*/
					var campo_tipo_selecionado = formulario.campo_tipo.selectedIndex;
					var campo_tipo = trim(formulario.campo_tipo.options[campo_tipo_selecionado].value);
					var campo_nome = trim(formulario.campo_nome.value);
					var campo_razao_social = trim(formulario.campo_razao_social.value);
					var campo_endereco = trim(formulario.campo_endereco.value);
					var campo_bairro = trim(formulario.campo_bairro.value);
					var campo_cep = trim(formulario.campo_cep.value);
					var campo_cidade = trim(formulario.campo_cidade.value);
					var campo_estado_selecionado = formulario.campo_estado.selectedIndex;
					var campo_estado = trim(formulario.campo_estado.options[campo_estado_selecionado].value);
					var campo_telefone = trim(formulario.campo_telefone.value);
					var campo_fax = trim(formulario.campo_fax.value);
					var campo_contato = trim(formulario.campo_contato.value);
					var campo_email = trim(formulario.campo_email.value);
					var campo_marcador_selecionado = formulario.campo_marcador.selectedIndex;
					var campo_marcador = trim(formulario.campo_marcador.options[campo_marcador_selecionado].value);
					var campo_posx = trim(formulario.campo_posx.value);
					var campo_posy = trim(formulario.campo_posy.value);

					var div = document.createElement("div");
						div.setAttribute("id", "unidade"+id);
						div.setAttribute("class", "lista")
						div.setAttribute("className", "lista")
							var ul = document.createElement("ul");
								var li = document.createElement("li")
									var img = document.createElement("img");
										img.setAttribute("src", "estrutura/erro.gif");
										img.setAttribute("style", "cursor: pointer;");
										img.onclick = function () { excluir(id, 'unidade'); };
								li.appendChild(img);
							ul.appendChild(li);
						div.appendChild(ul);
							var ul = document.createElement("ul");
								var li = document.createElement("li");
									var strong = document.createElement("strong");
										strong.appendChild(document.createTextNode("Tipo: "));
								li.appendChild(strong);
							ul.appendChild(li);
								var li2 = document.createElement("li");
									var span = document.createElement("span");
										span.setAttribute("id", "tipo"+id);
										span.appendChild(document.createTextNode(campo_tipo));
										span.onclick = function () { editar(id, this, 'select_tipo', 'unidade', '1'); };
								li2.appendChild(span);
							ul.appendChild(li2);
						div.appendChild(ul);
							var ul = document.createElement("ul");
								var li = document.createElement("li");
									var strong = document.createElement("strong");
										strong.appendChild(document.createTextNode("Nome: "));
								li.appendChild(strong);
							ul.appendChild(li);
								var li2 = document.createElement("li");
									var span = document.createElement("span");
										span.setAttribute("id", "nome"+id);
										span.appendChild(document.createTextNode(campo_nome));
										span.onclick = function () { editar(id, this, 'input', 'unidade', '2'); };
								li2.appendChild(span);
							ul.appendChild(li2);
						div.appendChild(ul);
							var ul = document.createElement("ul");
								var li = document.createElement("li");
									var strong = document.createElement("strong");
										strong.appendChild(document.createTextNode("Razão Social: "));
								li.appendChild(strong);
							ul.appendChild(li);
								var li2 = document.createElement("li");
									var span = document.createElement("span");
										span.setAttribute("id", "razao_social"+id);
										span.appendChild(document.createTextNode(campo_razao_social));
										span.onclick = function () { editar(id, this, 'input', 'unidade', '3'); };
								li2.appendChild(span);
							ul.appendChild(li2);
						div.appendChild(ul);
							var ul = document.createElement("ul");
								var li = document.createElement("li");
									var strong = document.createElement("strong");
										strong.appendChild(document.createTextNode("Endereço: "));
								li.appendChild(strong);
							ul.appendChild(li);
								var li2 = document.createElement("li");
									var span = document.createElement("span");
										span.setAttribute("id", "endereco"+id);
										span.appendChild(document.createTextNode(campo_endereco));
										span.onclick = function () { editar(id, this, 'input', 'unidade', '4'); };
								li2.appendChild(span);
							ul.appendChild(li2);
						div.appendChild(ul);
							var ul = document.createElement("ul");
								var li = document.createElement("li");
									var strong = document.createElement("strong");
										strong.appendChild(document.createTextNode("Bairro: "));
								li.appendChild(strong);
							ul.appendChild(li);
								var li2 = document.createElement("li");
									var span = document.createElement("span");
										span.setAttribute("id", "bairro"+id);
										span.appendChild(document.createTextNode(campo_bairro));
										span.onclick = function () { editar(id, this, 'input', 'unidade', '5'); };
								li2.appendChild(span);
							ul.appendChild(li2);
						div.appendChild(ul);
							var ul = document.createElement("ul");
								var li = document.createElement("li");
									var strong = document.createElement("strong");
										strong.appendChild(document.createTextNode("Cep: "));
								li.appendChild(strong);
							ul.appendChild(li);
								var li2 = document.createElement("li");
									var span = document.createElement("span");
										span.setAttribute("id", "cep"+id);
										span.appendChild(document.createTextNode(campo_cep));
										span.onclick = function () { editar(id, this, 'input', 'unidade', '6'); };
								li2.appendChild(span);
							ul.appendChild(li2);
						div.appendChild(ul);
							var ul = document.createElement("ul");
								var li = document.createElement("li");
									var strong = document.createElement("strong");
										strong.appendChild(document.createTextNode("Cidade: "));
								li.appendChild(strong);
							ul.appendChild(li);
								var li2 = document.createElement("li");
									var span = document.createElement("span");
										span.setAttribute("id", "cidade"+id);
										span.appendChild(document.createTextNode(campo_cidade));
										span.onclick = function () { editar(id, this, 'input', 'unidade', '7'); };
								li2.appendChild(span);
							ul.appendChild(li2);
						div.appendChild(ul);
							var ul = document.createElement("ul");
								var li = document.createElement("li");
									var strong = document.createElement("strong");
										strong.appendChild(document.createTextNode("Estado: "));
								li.appendChild(strong);
							ul.appendChild(li);
								var li2 = document.createElement("li");
									var span = document.createElement("span");
										span.setAttribute("id", "estado"+id);
										span.appendChild(document.createTextNode(campo_estado));
										span.onclick = function () { editar(id, this, 'select_estado', 'unidade', '8'); };
								li2.appendChild(span);
							ul.appendChild(li2);
						div.appendChild(ul);
							var ul = document.createElement("ul");
								var li = document.createElement("li");
									var strong = document.createElement("strong");
										strong.appendChild(document.createTextNode("Telefone: "));
								li.appendChild(strong);
							ul.appendChild(li);
								var li2 = document.createElement("li");
									var span = document.createElement("span");
										span.setAttribute("id", "telefone"+id);
										span.appendChild(document.createTextNode(campo_telefone));
										span.onclick = function () { editar(id, this, 'input', 'unidade', '9'); };
								li2.appendChild(span);
							ul.appendChild(li2);
						div.appendChild(ul);
							var ul = document.createElement("ul");
								var li = document.createElement("li");
									var strong = document.createElement("strong");
										strong.appendChild(document.createTextNode("Fax: "));
								li.appendChild(strong);
							ul.appendChild(li);
								var li2 = document.createElement("li");
									var span = document.createElement("span");
										span.setAttribute("id", "fax"+id);
										span.appendChild(document.createTextNode(campo_fax));
										span.onclick = function () { editar(id, this, 'input', 'unidade', '10'); };
								li2.appendChild(span);
							ul.appendChild(li2);
						div.appendChild(ul);
							var ul = document.createElement("ul");
								var li = document.createElement("li");
									var strong = document.createElement("strong");
										strong.appendChild(document.createTextNode("Contato: "));
								li.appendChild(strong);
							ul.appendChild(li);
								var li2 = document.createElement("li");
									var span = document.createElement("span");
										span.setAttribute("id", "contato"+id);
										span.appendChild(document.createTextNode(campo_contato));
										span.onclick = function () { editar(id, this, 'input', 'unidade', '11'); };
								li2.appendChild(span);
							ul.appendChild(li2);
						div.appendChild(ul);
							var ul = document.createElement("ul");
								var li = document.createElement("li");
									var strong = document.createElement("strong");
										strong.appendChild(document.createTextNode("E-mail: "));
								li.appendChild(strong);
							ul.appendChild(li);
								var li2 = document.createElement("li");
									var span = document.createElement("span");
										span.setAttribute("id", "email"+id);
										span.appendChild(document.createTextNode(campo_email));
										span.onclick = function () { editar(id, this, 'input_email', 'unidade', '12'); };
								li2.appendChild(span);
							ul.appendChild(li2);
						div.appendChild(ul);
							var ul = document.createElement("ul");
								var li = document.createElement("li");
									var strong = document.createElement("strong");
										strong.appendChild(document.createTextNode("Marcador: "));
								li.appendChild(strong);
							ul.appendChild(li);
								var li2 = document.createElement("li");
									var span = document.createElement("span");
										span.setAttribute("id", "marcador"+id);
										span.appendChild(document.createTextNode(campo_marcador));
										span.onclick = function () { editar(id, this, 'select_marcadores', 'unidade', '13'); };
								li2.appendChild(span);
							ul.appendChild(li2);
						div.appendChild(ul);
							var ul = document.createElement("ul");
								var li = document.createElement("li");
									var strong = document.createElement("strong");
										strong.appendChild(document.createTextNode("Pos X Marcador: "));
								li.appendChild(strong);
							ul.appendChild(li);
								var li2 = document.createElement("li");
									var span = document.createElement("span");
										span.setAttribute("id", "posx"+id);
										span.appendChild(document.createTextNode(campo_posx));
										span.onclick = function () { editar(id, this, 'input', 'unidade', '14'); };
								li2.appendChild(span);
							ul.appendChild(li2);
						div.appendChild(ul);
							var ul = document.createElement("ul");
								var li = document.createElement("li");
									var strong = document.createElement("strong");
										strong.appendChild(document.createTextNode("Pos Y Marcador: "));
								li.appendChild(strong);
							ul.appendChild(li);
								var li2 = document.createElement("li");
									var span = document.createElement("span");
										span.setAttribute("id", "posy"+id);
										span.appendChild(document.createTextNode(campo_posy));
										span.onclick = function () { editar(id, this, 'input', 'unidade', '15'); };
								li2.appendChild(span);
							ul.appendChild(li2);
						div.appendChild(ul);
				break;

				case "usuario": /*################################################ USUARIO*/
					var nome = trim(formulario.campo_nome.value);
					var login = trim(formulario.campo_login.value);
					var senha = trim(formulario.campo_senha.value);
					var div = document.createElement("div");
						div.setAttribute("id", "usuario"+id);
						div.setAttribute("class", "lista")
						div.setAttribute("className", "lista")
							var ul = document.createElement("ul");
								var li = document.createElement("li")
									var img = document.createElement("img");
										img.setAttribute("src", "estrutura/erro.gif");
										img.setAttribute("style", "cursor: pointer;");
										img.onclick = function () { excluir(id, 'usuario'); };
								li.appendChild(img);
							ul.appendChild(li);
						div.appendChild(ul);
							var ul = document.createElement("ul");
								var li = document.createElement("li");
									var strong = document.createElement("strong");
										strong.appendChild(document.createTextNode("Nome: "));
								li.appendChild(strong);
							ul.appendChild(li);
								var li2 = document.createElement("li");
									var span = document.createElement("span");
										span.setAttribute("id", "nome"+id);
										span.appendChild(document.createTextNode(nome));
										span.onclick = function () { editar(id, this, 'input', 'usuario', '1'); };
								li2.appendChild(span);
							ul.appendChild(li2);
						div.appendChild(ul);
							var ul = document.createElement("ul");
								var li = document.createElement("li");
									var strong = document.createElement("strong");
										strong.appendChild(document.createTextNode("Login: "));
								li.appendChild(strong);
							ul.appendChild(li);
								var li2 = document.createElement("li");
									var span = document.createElement("span");
										span.setAttribute("id", "login"+id);
										span.appendChild(document.createTextNode(login));
										span.onclick = function () { editar(id, this, 'input', 'usuario', '2'); };
								li2.appendChild(span);
							ul.appendChild(li2);
						div.appendChild(ul);
							var ul = document.createElement("ul");
								var li = document.createElement("li");
									var strong = document.createElement("strong");
										strong.appendChild(document.createTextNode("Senha: "));
								li.appendChild(strong);
							ul.appendChild(li);
								var li2 = document.createElement("li");
									var span = document.createElement("span");
										span.setAttribute("id", "senha"+id);
										span.appendChild(document.createTextNode(senha));
										span.onclick = function () { editar(id, this, 'input', 'usuario', '3'); };
								li2.appendChild(span);
							ul.appendChild(li2);
						div.appendChild(ul);
				break;
			}
			formulario.reset();
			loading("hide");
			if(elemento.appendChild(div)) {
				loading("hide");
				return alert("Cadastro efetuado com sucesso!");			
			} else {
				loading("hide");
				return alert("Cadastro efetuado com sucesso, mas o registro não pode ser inserido na lista.");			
			}
		} else {
			return alert("Elemento não encontrado")
		}
	}
}

function editar(id, local, campo, tipo, titulo) {
	/*
	 * id = id do elemento
	 * local = local aonde esta o elemento
	 * campo = tipo de campo (input, textarea)
	 * tipo = tipo de edicao (projeto, etc)
	 * titulo = titulo do campo a ser editado (titulo, descricao, pdf);
	 */
	var elemento = document.getElementById(local.getAttribute("id"));
	if (elemento) {
		var valor_original = trim(elemento.innerHTML);
		switch (campo) {
		case "input":
			var incluir = document.createElement("input");
				incluir.setAttribute("name","editor");
				incluir.setAttribute("type","text");
				incluir.setAttribute("class","editar_input");
				incluir.setAttribute("className","editar_input");
				incluir.setAttribute("id","editor");
				incluir.setAttribute("value",valor_original);
				incluir.setAttribute("size","70");
		break;

		case "textarea":
			var valor_br = valor_original.replace(/<br>|<\/br>|<br\/>/gi, "\r");
			var incluir = document.createElement("textarea");
				incluir.setAttribute("name","editor");
				incluir.setAttribute("class","editar_textarea");
				incluir.setAttribute("className","editar_textarea");
				incluir.setAttribute("id","editor");
				incluir.appendChild(document.createTextNode(valor_br));
		break;

		case "input_email":
			var incluir = document.createElement("input");
				incluir.setAttribute("name","editor");
				incluir.setAttribute("type","text");
				incluir.setAttribute("class","editar_input");
				incluir.setAttribute("className","editar_input");
				incluir.setAttribute("id","editor");
				incluir.setAttribute("value",valor_original);
				incluir.setAttribute("size","70");
		break;
		
		case "select_marcadores":
			var marcadores = new Array();
		marcadores[0] = "Azul Claro";
		marcadores[1] = "Azul Escuro";
		marcadores[2] = "Banana";
		marcadores[3] = "Laranja";
		marcadores[4] = "Marrom";
		marcadores[5] = "Preto";
		marcadores[6] = "Roxo";
		marcadores[7] = "Verde Claro";
		marcadores[8] = "Verde Escuro";
		marcadores[9] = "Vermelho";
		marcadores[10] = "Violeta";
			var incluir = document.createElement("select");
				incluir.setAttribute("name","editor");
				incluir.setAttribute("class","editar_input");
				incluir.setAttribute("className","editar_input");
				incluir.setAttribute("id","editor");
				for (var i = 0; i <= (marcadores.length -1); i++) {
					var option = document.createElement("option");
						option.setAttribute("value",marcadores[i]);
						option.appendChild(document.createTextNode(marcadores[i]));
					if(valor_original == marcadores[i]) {
						option.setAttribute("selected", true);							
					}
					incluir.appendChild(option);					
				}
		break;

		case "select_unidade":
			var unidades = new Array();
		unidades[0] = "Belo Horizonte - MG";
		unidades[1] = "Fortaleza - CE";
		unidades[2] = "JoÃ£o Pessoa - PB";
		unidades[3] = "Juiz de Fora - MG";
		unidades[4] = "JundiaÃ­ - SP";
		unidades[5] = "Porto Alegre - RS";
		unidades[6] = "Recife - PE";
		unidades[7] = "RibeirÃ£o Preto - SP";
		unidades[8] = "Rio de Janeiro - RJ";
			var incluir = document.createElement("select");
				incluir.setAttribute("name","editor");
				incluir.setAttribute("class","editar_input");
				incluir.setAttribute("className","editar_input");
				incluir.setAttribute("id","editor");
				for (var i = 0; i <= (unidades.length -1); i++) {
					var option = document.createElement("option");
						option.setAttribute("value",unidades[i]);
						option.appendChild(document.createTextNode(unidades[i]));
					if(valor_original == unidades[i]) {
						option.setAttribute("selected", true);							
					}
					incluir.appendChild(option);					
				}
		break;

		case "select_estado":
		var estados = new Array();
		estados[0] = "AC";
		estados[1] = "AL";
		estados[2] = "AP";
		estados[3] = "AM";
		estados[4] = "BA";
		estados[5] = "CE";
		estados[6] = "DF";
		estados[7] = "ES";
		estados[8] = "GO";
		estados[9] = "MA";
		estados[10] = "MT";
		estados[11] = "MS";
		estados[12] = "MG";
		estados[13] = "PA";
		estados[14] = "PB";
		estados[15] = "PR";
		estados[16] = "PE";
		estados[17] = "PI";
		estados[18] = "RJ";
		estados[19] = "RN";
		estados[20] = "RS";
		estados[21] = "RO";
		estados[22] = "RR";
		estados[23] = "SP";
		estados[24] = "SC";
		estados[25] = "SE";
		estados[26] = "TO";
			var incluir = document.createElement("select");
				incluir.setAttribute("name","editor");
				incluir.setAttribute("class","editar_input");
				incluir.setAttribute("className","editar_input");
				incluir.setAttribute("id","editor");
				for (var i = 0; i <= (estados.length -1); i++) {
					var option = document.createElement("option");
						option.setAttribute("value",estados[i]);
						option.appendChild(document.createTextNode(estados[i]));
					if(valor_original == estados[i]) {
						option.setAttribute("selected", true);							
					}
					incluir.appendChild(option);					
				}
		break;

		case "select_tipo":
			var tipos = new Array();
				tipos[0] = "Unidade";tipos[1] = "Parceiro";
			var incluir = document.createElement("select");
				incluir.setAttribute("name","editor");
				incluir.setAttribute("class","editar_input");
				incluir.setAttribute("className","editar_input");
				incluir.setAttribute("id","editor");
				for (var i = 0; i <= (tipos.length -1); i++) {
					var option = document.createElement("option");
						option.setAttribute("value",tipos[i]);
						option.appendChild(document.createTextNode(tipos[i]));
						if(valor_original == tipos[i]) {
							option.setAttribute("selected", true);							
						}
						incluir.appendChild(option);					
				}
		break;
		}
		
		elemento.parentNode.replaceChild(incluir, elemento);
		incluir.focus();
		incluir.onblur = function () {
			var editor = document.getElementById("editor");
			if (editor) {
				if(campo == "textarea") {
					var valor_novo = trim(editor.value);
					valor_novo = valor_novo.replace(/\n/gi, "<br>");
				} else {
					var valor_novo = trim(editor.value);
				}
				if(valor_original != valor_novo && valor_novo.length > 0) {

					if(campo == "input_email") {
						var email = valor_novo.split(",");
						var regex = /^([A-Za-z0-9]+)([A-Za-z0-9\.-_]+)@([A-Za-z0-9\.-_]+)\.([A-Za-z0-9]+)([A-Za-z0-9]?)/;
						for (var i = 0; i <= (email.length -1); i++) {
							if(!regex.test(trim(email[i]))) {
								var span = document.createElement("span");
									span.setAttribute("id",local.getAttribute("id"));
									span.onclick = function () {editar(id, local, campo, tipo, titulo); };
								editor.parentNode.replaceChild(span, editor);
								span.innerHTML = valor_original;
								return alert("Email invalido");
							}			
						}
					}

					var dados = "acao=editar"; //acao
					dados += "&tipo="+tipo; //tipo (projeto, etc)
					dados += "&campo="+titulo; //campo a ser editado
					dados += "&id="+id;
					dados += "&valor="+valor_novo.replace(/<br>|<\/br>|<br\/>/gi, "\n");
					var enviar = false;
					if (window.XMLHttpRequest) { //mozilla.
						enviar = new XMLHttpRequest();
					} else if (window.ActiveXObject) { //IE
						enviar = ObjMSXMLHttp();
					}
					enviar.open("POST", "intranet.php", true);
					enviar.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
					enviar.onreadystatechange = function() {
						if(enviar.readyState == 1) {
							loading("show");
						}
						if(enviar.readyState == 4) {
							if(enviar.status == 200) {
								loading("hide");
								var response = enviar.responseText;
								if(trim(response) == "ok") {
									var span = document.createElement("span");
										span.setAttribute("id",local.getAttribute("id"));
										span.onclick = function () {editar(id, local, campo, tipo, titulo); };
									editor.parentNode.replaceChild(span, editor);
									span.innerHTML = valor_novo;
									return alert("Alteração efetuada com sucesso.");
								} else {
									var span = document.createElement("span");
										span.setAttribute("id",local.getAttribute("id"));
										span.onclick = function () {editar(id, local, campo, tipo, titulo); };
									editor.parentNode.replaceChild(span, editor);
									span.innerHTML = valor_original;
									return alert(trim(response));
								}				
							} else {
								return alert("Erro ao carregar os dados");
							}
						}
					}
					enviar.send(dados);	
				} else {
					var span = document.createElement("span");
						span.setAttribute("id",local.getAttribute("id"));
						span.onclick = function () {editar(id, local, campo, tipo, titulo); };
					editor.parentNode.replaceChild(span, editor);
					span.innerHTML = valor_original;
				}
			} else {
				return alert("Elemento nao encontrado.");
			}
		}
	} else {
		return alert("Elemento nao encontrado.");
	}
}

function excluir(id, tipo) {
	var i = "Atenção!\n";
	i += "Deseja realmente excluir este registro?\n";
	var pergunta = window.confirm(i);
	if (pergunta) {
		loading("show");
		var elemento = document.getElementById(tipo+id);
		if(elemento) {
			var enviar = false;
			if (window.XMLHttpRequest) { //mozilla.
				enviar = new XMLHttpRequest();
			} else if (window.ActiveXObject) { //IE
				enviar = ObjMSXMLHttp();
			}
			var dados = "acao=excluir";
			dados += "&tipo="+tipo;
			dados += "&id="+id;
			enviar.open("POST", "intranet.php", true);
			enviar.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			enviar.onreadystatechange = function() {
				if(enviar.readyState == 1) {
					loading("show");
				}
				if(enviar.readyState == 4) {
					if(enviar.status == 200) {
						loading("hide");
						var response = enviar.responseText;
						if(trim(response) == "ok") {
							var pai = elemento.parentNode;
							pai.removeChild(elemento);
							var j = document.getElementById("lista");
							if (j) {
								var divs = j.getElementsByTagName("div");
								if(divs.length < 1) {
									var div = document.createElement("div");
									div.setAttribute("id","sem_registro");
									div.appendChild(document.createTextNode("Nenhum registro encontrado no banco de dados."));
									j.appendChild(div);
								}								
							} else {
								alert("Removido com sucesso. Elemento nao encontrado.");
							}
							return alert("Removido com sucesso.");
						} else {
							return alert(trim(response));
						}				
					} else {
						return alert("Erro ao carregar os dados");
					}
				}
			}
			enviar.send(dados);		
		} else {
			loading("hide")
			return alert("Elemento nao encontrado");
		}
	} else {
		return false;
	}
}
function posicionaralfinete() {
	var formulario = document.getElementById("form_cadastrar");
	var campo_marcador_selecionado = formulario.campo_marcador.selectedIndex;
	var campo_marcador = trim(formulario.campo_marcador.options[campo_marcador_selecionado].value);
	switch (campo_marcador) {
		case "Azul Claro":
			var img =  "alfinete_06.png";
		break;		case "Azul Escuro":
			var img =  "alfinete_01.png";
		break;		case "Banana":
			var img =  "alfinete_08.png";
		break;		case "Laranja":
			var img =  "alfinete_04.png";
		break;		case "Marrom":
			var img =  "alfinete_10.png";
		break;		case "Preto":
			var img =  "alfinete_11.png";
		break;		case "Roxo":
			var img =  "alfinete_03.png";
		break;		case "Verde Claro":
			var img =  "alfinete_02.png";
		break;		case "Verde Escuro":
			var img =  "alfinete_07.png";
		break;		case "Vermelho":
			var img =  "alfinete_05.png";
		break;		case "Violeta":
			var img =  "alfinete_09.png";
		break;		default:
			var img = "alfinete_01.png";
		break;
	}
	var posx = trim(formulario.campo_posx.value);
	var posy = trim(formulario.campo_posy.value);
	var alfinete = document.getElementById('alfinete');
	alfinete.src = "estrutura/"+img;
	alfinete.style.marginTop = posy;
	alfinete.style.marginLeft = posx;
}
