var mac = (navigator.appVersion.indexOf("Mac") != -1) ? true : false;
var ie = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var fire = (navigator.userAgent.toLowerCase().indexOf("firefox") != -1) ? true : false;

var oldo = '';

function getOffsetTop(element, deep) {
	return getOffsetProperty(element, 'Top', deep);
}

function getOffsetLeft(element, deep) {
	return getOffsetProperty(element, 'Left', deep);
}

function getOffsetProperty(element, property, deep) {
   var offsetValue = 0;
   offsetProperty = 'offset' + property;
   
   do {
      offsetValue += element[offsetProperty];
      element = element.offsetParent;
   } while (deep == true && element != document.body && element != null);
   return offsetValue;
}

function showC(o) {
	if(!fire && !mac) {
		if(o) document.getElementById("chamadoIndv").style.marginTop = getOffsetTop(o,1) + 24;
		else document.getElementById("chamadoIndv").style.marginTop = getOffsetTop(oldo,1) + 24;
		if(o) document.getElementById('chamadoIndv').style.marginLeft = getOffsetLeft(o,1) - 2;
		else document.getElementById("chamadoIndv").style.marginLeft = getOffsetLeft(oldo,1) - 2;
		document.getElementById("chamadoIndv").style.display='block';
		oldo = o;
	}
}

function hideC() {
	if(!fire && !mac) {
		document.getElementById('chamadoIndv').style.display='none';
	}
}

function showC2(o) {
	if(!fire && !mac) {
		if(o) document.getElementById("altCategoria").style.marginTop = getOffsetTop(o,1) + 18;
		else document.getElementById("altCategoria").style.marginTop = getOffsetTop(oldo,1) + 18;
		if(o) document.getElementById('altCategoria').style.marginLeft = getOffsetLeft(o,1) + 2;
		else document.getElementById("altCategoria").style.marginLeft = getOffsetLeft(oldo,1) + 2;
		document.getElementById("altCategoria").style.display='block';
		oldo = o;
	}
}

function hideC2() {
	if(!fire && !mac) {
		document.getElementById('altCategoria').style.display='none';
	}
	
}

function showC3(o) {
	if(!fire && !mac) {
		if(o) document.getElementById("altRealizado").style.marginTop = getOffsetTop(o,1) + 18;
		else document.getElementById("altRealizado").style.marginTop = getOffsetTop(oldo,1) + 18;
		if(o) document.getElementById('altRealizado').style.marginLeft = getOffsetLeft(o,1) + 2;
		else document.getElementById("altRealizado").style.marginLeft = getOffsetLeft(oldo,1) + 2;
		document.getElementById("altRealizado").style.display='block';
		oldo = o;
	}
}

function hideC3() {
	if(!fire && !mac) {
		document.getElementById('altRealizado').style.display='none';
	}
}

function showLoad(o) {
	if(!fire && !mac) {
		if(o) document.getElementById("carregando").style.marginTop = getOffsetTop(o,1) + 24;
		else document.getElementById("carregando").style.marginTop = getOffsetTop(oldo,1) + 24;
		if(o) document.getElementById('carregando').style.marginLeft = getOffsetLeft(o,1) - 2;
		else document.getElementById("carregando").style.marginLeft = getOffsetLeft(oldo,1) - 2;
		document.getElementById("carregando").style.display='block';
		oldo = o;
	}
}

function hideLoad() {
	if(!fire && !mac) {
		document.getElementById('carregando').style.display='none';
	}
}


//Interface genérica para acesso ao conector remoto.
function createXMLHttpRequest() {
	
   try{ return new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){}
   try{ return new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){}
   try{ return new XMLHttpRequest(); }catch(e){}
   alert("XMLHttpRequest not supported");
   return null;
}
// CARREGA XML
function loadXMLDoc(url,funcao) {
	if (window.XMLHttpxmlhttpuest) {
	    xmlhttp = new XMLHttpxmlhttpuest();
	} else if (window.ActiveXObject) {
	    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange = funcao;
	xmlhttp.open("GET", url, true);
	xmlhttp.send(null);		
}

function trataNumeros(numero) {
	numero = String(numero) ;
	qtdChar = numero.indexOf(".") ;
	if (qtdChar == -1) {
		numero += ".00";
	} else if (qtdChar+2 == numero.length) {
		numero += "0";
	}
	if(qtdChar != -1) {
		depoisVirgula = numero.substring(qtdChar+1,18);
		if(depoisVirgula.length > 2) {
			depoisVirgula = String(depoisVirgula.substring(0,1)) + ".9" ;
			numero = String(numero.substring(0,qtdChar)) + "." + String(Math.ceil(depoisVirgula) + "0");
		}
	}
	return numero ;
}

function zeraFrete() {
	if(document.getElementById("entrega").value == "0") {
		document.getElementById("frete").value = "0.00" ;
	}
	calculaTotal() ;
}

function exibeProdutos(NumInput) {
	
	ShowText = "<table>" ;
		
	document.getElementById("listaProdutos").style.visibility = "hidden";
	
	if (document.getElementById("produto"+NumInput).value.length > 0) {
		
		NovaString = document.getElementById("produto"+NumInput).value.toUpperCase() ;

		for (i=0;i<Produto.length;i++) {
			if (Produto[i].indexOf(NovaString) == 0) {
				ShowText = ShowText + "<tr><td bgColor='#E8AA33'><a href='javascript:guardaProduto(" + IdProdutoTipo[i] + "," + NumInput + ")'>" + Produto[i] + " (" + IdProdutoTipo[i] + ")</a></td></tr>"
			}
		}		
		
		
	} else {
	
		document.getElementById("id_produto"+NumInput).value = 0 ;
		document.getElementById("precoUnitario"+NumInput).value = "0.00" ;
		document.getElementById("precoTotal"+NumInput).value = "0.00" ;
		document.getElementById("qtd"+NumInput).value = "" ;
		calculaTotalProds() ;
		
	}
	
	ShowText = ShowText + "</table>" ;
	
	showC(document.getElementById("produto"+NumInput),ShowText) ;
	
	document.getElementById("listaProdutos").style.visibility = "visible";
	
}

function calculaFrete(campo) {
	frete = 0 ;
	if(campo.value != "") {
		pos = campo.value.indexOf("#") ;
		frete = campo.value.slice(pos+1,campo.value.length) ;
	}
	document.getElementById("frete").value = trataNumeros(frete) ;
	calculaTotal() ;
	zeraFrete() ;
}

function exibeBairros(tipo) {
	
	ShowText = "<table cellspacing='0' cellpadding='0'>" ;	
	
	if(tipo == "select") {		
		
		ShowText +=		"<tr>" ;
		ShowText +=			"<td align='right' style='padding-right:3px;'><p class='padrao'>Bairro:</p></td>" ;
		ShowText +=			"<td>" ;
		ShowText += 			"<select id='id_regiao' name='id_regiao' class='formnormal' style='width:250px;' onChange='calculaFrete(this); zeraFrete();'>" ;
		ShowText +=					"<option value='' selected>-- Escolha aqui --</option>" ;
										for (k=0;k<Regiao.length;k++) {
											ShowText += "<option value='"+IdRegiao[k]+"#"+RegiaoPreco[k].replace(/,/, ".")+"'>"+Regiao[k]+" - "+RegiaoPreco[k]+"</option>" ;
										}
		ShowText +=				"</select>" ;
		ShowText +=			"</td>" ;
		ShowText +=		"</tr>" ;
		ShowText +=		"<tr>" ;
		ShowText +=			"<td height='20' colspan='2' align='right' valign='top'><p class=padrao>Se o bairro não existir na lista acima <a href='javascript:exibeBairros(\"text\") ;;' target='_self' style='color:#CC4444'><b>clique aqui</b>.</a></p></td>" ;
		ShowText +=		"</tr>" ;	
		
		document.getElementById("escolheBairro").value = "yes"
		document.getElementById("frete").readOnly = true ;
		document.getElementById("frete").style.border = "0px solid #ECA400" ;
		document.getElementById("frete").style.color = "#ECA400" ;
		document.getElementById("frete").style.fontWeight = "bold" ;
		document.getElementById("cifrao").style.color = "#ECA400" ;
		document.getElementById("cifrao").style.fontWeight = "bold" ;
	}else if(tipo == "text"){
		
		ShowText +=		"<tr>" ;
		ShowText +=			"<td align='right' style='padding-right:3px;'><p class='padrao'>Bairro:</p></td>" ;
		ShowText +=			"<td><input onFocus='muda_form(\"ativa\",this)' onBlur='muda_form(\"desativa\",this)' type='text' name='bairro' maxlength='30' class='formnormal' style='border-color:#ECA400; height=17px; width=250px;'></td>" ;
		ShowText +=		"</tr>" ;
		ShowText +=		"<tr>" ;
		ShowText +=			"<td height='20' colspan='2' align='right' valign='top'><p class=padrao>Se quer escolher o bairro na lista <a href='javascript:exibeBairros(\"select\") ;;' target='_self' style='color:#CC4444'><b>clique aqui</b>.</a></p></td>" ;
		ShowText +=		"</tr>" ;	
		
		document.getElementById("escolheBairro").value = "no" ;
		document.getElementById("frete").readOnly = false ;
		document.getElementById("frete").style.border = "1px solid #ECA400" ;
		document.getElementById("frete").style.color = "#000000" ;
		document.getElementById("frete").style.fontWeight = "normal" ;
		document.getElementById("cifrao").style.color = "#000000" ;
		document.getElementById("cifrao").style.fontWeight = "normal" ;
		
	}
	
	ShowText += "</table>" ;
	
	document.getElementById("carregaBairros").innerHTML = ShowText ;
	
}


function guardaProduto(IdProd,NumInput) {
	if (Number(NumInput) > (linhas)) {linhas = NumInput ;}
	document.getElementById("id_produto"+NumInput).value = IdProd ;
	for (i=0;i<Produto.length;i++) {
		if (IdProdutoTipo[i] == IdProd) {
			document.getElementById("codigoViva"+NumInput).value = codigoViva[i] ;
			document.getElementById("produto"+NumInput).value = Produto[i] ;
			document.getElementById("precoUnitario"+NumInput).value = Preco[i].replace(/,/, ".") ;
			document.getElementById("qtd"+NumInput).value = 1 ;
		}
	}
	document.getElementById("listaProdutos").style.visibility = "hidden";
	calculaTotalProd(NumInput) ;
	calculaTotalProds() ;
}

function calculaTotalProd(NumInput) {
	Total = document.getElementById("qtd"+NumInput).value * document.getElementById("precoUnitario"+NumInput).value.replace(/,/, ".") ;
	document.getElementById("precoTotal"+NumInput).value = trataNumeros(Total)  ;
	calculaTotalProds() ;
}

function calculaTotalProds() {
	Total = 0	
	for (i=0;i<linhas;i++) {
		if (document.getElementById("id_produto"+(i+1)).value > 0) {
			NovPreco = document.getElementById("precoUnitario"+(i+1)).value.replace(/,/, ".") ;
			Total += document.getElementById("qtd"+(i+1)).value * NovPreco ;
		}
	}
	document.getElementById("totalProdutos").value = trataNumeros(Total)  ;
	calculaTotal() ;
}

function calculaTotal() {
	total = 0 ;
	total += ((Number(document.getElementById("totalProdutos").value.replace(/,/, ".")) + Number(document.getElementById("frete").value.replace(/,/, "."))) - Number(document.getElementById("desconto").value.replace(/,/, "."))) ;
	document.getElementById("total").value = trataNumeros(total)  ;	
}



function SomenteNumeros(Campo) {

	NovCampoText = "" ;
	SomaPontos = 0 ;
	SomaPosPontos = 0 ;
	
	for(i=0;i<Campo.value.length;i++) {
		VarConfirma = false ;
		VarTeste = Campo.value.charAt(i);
		for(n=0;n<=9;n++) {
			if (VarTeste == n) {VarConfirma = true ;}
		}
		if (VarTeste == "." && SomaPontos == 0) {
			VarConfirma = true ;
			SomaPontos++ ;
		}
		if (SomaPontos > 0 && VarConfirma == true) {
			SomaPosPontos ++ ;
			if (SomaPosPontos > 3) {VarConfirma = false ;}
		}
		if (VarConfirma == false) {alert("Apenas permitidos números e '.' .")} else {NovCampoText += VarTeste};
	}
	
	Campo.value = NovCampoText ;
	
}

function guardaCliente(ID) {
	
	for (i=0;i<IdCliente.length;i++) {
		if (IdCliente[i] == ID) {
			document.getElementById("id_cliente").value = IdCliente[i] ;
			document.getElementById("nome").value = Nome[i] ;
			document.getElementById("email").value = Email[i] ;
			document.getElementById("senha").value = Senha[i] ;
			document.getElementById("senha2").value = Senha[i] ;
			document.getElementById("logradouro").value = Logradouro[i] ;
			document.getElementById("numero").value = Numero[i] ;
			document.getElementById("complemento").value = Complemento[i] ;
			if (IdRegiaoCli[i] == 0) {
				exibeBairros("text") ;
				document.getElementById("bairro").value = Bairro[i] ;
			} else {
				exibeBairros("select") ;
				for (j=1;j<document.getElementById("id_regiao").length;j++) {
					pos = document.getElementById("id_regiao")[j].value.indexOf("#") ;
					id_regiao = document.getElementById("id_regiao")[j].value.slice(0,pos) ;
					if (IdRegiaoCli[i] == id_regiao)  {
						document.getElementById("id_regiao")[j].selected = true ;
						calculaFrete(document.getElementById("id_regiao")[j]) ;
					}
				}
			}			
			document.getElementById("cep1").value = Cep1[i] ;
			document.getElementById("cep2").value = Cep2[i] ;
			document.getElementById("ref").value = Referencia[i] ;
			document.getElementById("ref2").value = Referencia2[i] ;
			document.getElementById("dia").value = DiaNas[i] ;
			document.getElementById("mes").value = MesNas[i] ;
			document.getElementById("dddtel").value = DddTel[i] ;
			document.getElementById("tel").value = Tel[i] ;
			document.getElementById("dddcel").value = DddCel[i] ;
			document.getElementById("cel").value = Cel[i] ;
			document.getElementById("rg").value = Rg[i] ;
			document.getElementById("cpf").value = Cpf[i] ;
			if(News[i] == "True") {document.getElementById("news").checked = true;} else {document.getElementById("news").checked = false;}
			
			document.getElementById("botao").value = "ALTERAR"
			if (DiasAniver[i] < 31) {
				alert("Esse cliente vai fazer aniversário em " + DiaNas[i] + "/" + MesNas[i] + " falta " + DiasAniver[i] + " dia(s)") ;
			}
		}
	}
	document.getElementById("listaProdutos").style.visibility = "hidden";	
}

function carregaCliente() {
	if (xmlhttp.readyState==4)  {
	  // if "OK"
	  if (xmlhttp.status==200)  {
		var txt = xmlhttp.responseText;
		var xml = xmlhttp.responseXML;
		var node = xml.getElementsByTagName("clientes");
				
		for (i=0;i<node.length;i++) {
			for (a=0;a<node[i].childNodes.length;a++) {
				document.getElementById("infCliente").innerHTML = "<b>" + node[i].childNodes[a].getAttribute("informacoes") + "</b>";
			}
		}		
		
	  } else  {
		 alert("Problemas no carregamento da página\nMensagem: ' " + xmlhttp.statusText + " '\nInforme a mensagem aos Administradores do Site.")
	  }
	}
}

function carregaRespostaPossivel() {
	if (xmlhttp.readyState==4)  {
	  // if "OK"
	  if (xmlhttp.status==200)  {
		var txt = xmlhttp.responseText;
		var xml = xmlhttp.responseXML;
		
		if (txt != "#0#")
		{
			if (document.getElementById("texto").value != "")
			{
				document.getElementById("texto").value = document.getElementById("texto").value += "\n" + txt ;
			}
			else
			{
				document.getElementById("texto").value = txt ;
			}
			
		}
		else
		{
			alert("Não foi encontrado nenhuma resposta possível.");
		}
		
		
		
		
		
		/*		
		var node = xml.getElementsByTagName("clientes");
				
		for (i=0;i<node.length;i++) {
			for (a=0;a<node[i].childNodes.length;a++) {
				document.getElementById("infCliente").innerHTML = "<b>" + node[i].childNodes[a].getAttribute("informacoes") + "</b>";
			}
		}		
		*/
		
	  } else  {
		 alert("Problemas no carregamento da página\nMensagem: ' " + xmlhttp.statusText + " '\nInforme a mensagem aos Administradores do Site.")
	  }
	}
}

function carregaClientes() {
	if (xmlhttp.readyState==4)  {
	  // if "OK"
	  if (xmlhttp.status==200)  {
		var txt = xmlhttp.responseText;
		var xml = xmlhttp.responseXML;
		var node = xml.getElementsByTagName("clientes");
		
		ShowText = "<table>" ;
		
		document.getElementById("listaProdutos").style.visibility = "hidden";
		
		if (document.getElementById('nome').value.length > 0) {			
						
			for (i=0;i<node.length;i++) {
				for (a=0;a<node[i].childNodes.length;a++) {
					
					DiasAniver[a] = node[i].childNodes[a].getAttribute("diasAniver") ;
					IdCliente[a] = node[i].childNodes[a].getAttribute("id_cliente") ;
					Nome[a] = node[i].childNodes[a].getAttribute("nome") ;
					Email[a] = node[i].childNodes[a].getAttribute("email") ;
					Senha[a] = node[i].childNodes[a].getAttribute("senha") ;
					Logradouro[a] = node[i].childNodes[a].getAttribute("logradouro") ;
					Numero[a] = node[i].childNodes[a].getAttribute("numero") ;
					Complemento[a] = node[i].childNodes[a].getAttribute("complemento") ;
					Bairro[a] = node[i].childNodes[a].getAttribute("bairro") ;
					IdRegiaoCli[a] = node[i].childNodes[a].getAttribute("id_regiao") ;
					Cep1[a] = node[i].childNodes[a].getAttribute("cep1") ;
					Cep2[a] = node[i].childNodes[a].getAttribute("cep2") ;
					Referencia[a] = node[i].childNodes[a].getAttribute("referencia") ;
					Referencia2[a] = node[i].childNodes[a].getAttribute("referencia2") ;
					DiaNas[a] = node[i].childNodes[a].getAttribute("diaNas") ;
					MesNas[a] = node[i].childNodes[a].getAttribute("mesNas") ;
					DddTel[a] = node[i].childNodes[a].getAttribute("dddtel") ;
					Tel[a] = node[i].childNodes[a].getAttribute("tel") ;
					DddCel[a] = node[i].childNodes[a].getAttribute("dddcel") ;
					Cel[a] = node[i].childNodes[a].getAttribute("cel") ;
					Rg[a] = node[i].childNodes[a].getAttribute("rg") ;
					Cpf[a] = node[i].childNodes[a].getAttribute("cpf") ;
					News[a] = node[i].childNodes[a].getAttribute("news") ;
					ShowText = ShowText + "<tr><td bgColor='#E8AA33'><a href='javascript:guardaCliente(" + node[i].childNodes[a].getAttribute("id_cliente") + ")'>" + node[i].childNodes[a].getAttribute("nome") + " (" + node[i].childNodes[a].getAttribute("id_cliente") + ")</a></td></tr>" ;

				}
			}
		
		} else {
		
			document.getElementById("id_cliente").value = 0 ;
			document.getElementById("nome").value = "" ;
			document.getElementById("email").value = "" ;
			document.getElementById("senha").value = "" ;
			document.getElementById("senha2").value = "" ;
			document.getElementById("logradouro").value = "" ;
			document.getElementById("numero").value = "" ;
			document.getElementById("complemento").value = "" ;
			if(document.getElementById("bairro")) {
				document.getElementById("bairro").value = "" ;
			} else if(document.getElementById("id_regiao")) {
				document.getElementById("id_regiao")[0].selected = true ;
			}
			document.getElementById("cep1").value = "" ;
			document.getElementById("cep2").value = "" ;
			document.getElementById("ref").value = "" ;
			document.getElementById("ref2").value = "" ;
			document.getElementById("dia").value = "" ;
			document.getElementById("mes").value = "" ;
			document.getElementById("dddtel").value = "" ;
			document.getElementById("tel").value = "" ;
			document.getElementById("dddcel").value = "" ;
			document.getElementById("cel").value = "" ;
			document.getElementById("rg").value = "" ;
			document.getElementById("cpf").value = "" ;
			document.getElementById("news").checked = false ;
			document.getElementById("botao").value = "CADASTRAR" ;
		}
		
		ShowText = ShowText + "</table>" ;
		
		showC(document.getElementById("nome"),ShowText) ;
		
		document.getElementById("listaProdutos").style.visibility = "visible";		
		
	  } else  {
		 alert("Problemas no carregamento da página\nMensagem: ' " + xmlhttp.statusText + " '\nInforme a mensagem aos Administradores do Site.")
	  }
	}
}






//SISTEMA CHAMADOS

function EnviaEmailChamado() {
	if (xmlhttp.readyState==4) {
		if (xmlhttp.status==200) {
			var txt = xmlhttp.responseText;
			if(txt == "-1") {
				alert("Email enviado com sucesso!");
				loadXMLDoc("chamados.asp?id_chamado="+document.formulario.id_chamado.value,carregaChamadoIndv);
			} else {
				alert(txt) ;
			}
		} else {
			alert("Problemas no carregamento da página\nMensagem: ' " + xmlhttp.statusText + " '\nInforme a mensagem aos Administradores do Site.")
		}
	}
}

function cadastraChamado() {
	if (xmlhttp.readyState==4) {
		if (xmlhttp.status==200) {
			var txt = xmlhttp.responseText;
			if(txt == "-1") {
				loadXMLDoc("chamados.asp?id_chamado="+document.formulario.id_chamado.value,carregaChamadoIndv);
			} else {
				alert(txt) ;
			}
		} else {
			alert("Problemas no carregamento da página\nMensagem: ' " + xmlhttp.statusText + " '\nInforme a mensagem aos Administradores do Site.")
		}
	}
}

function alteraCategoria() {
	if (xmlhttp.readyState==4) {
		if (xmlhttp.status==200) {
			var txt = xmlhttp.responseText;
			if(txt == "-1") {
				hideC2();
				loadXMLDoc("chamados.asp?id_cliente="+document.formulario.id_cliente.value+"&realizado="+document.formulario.realizado.checked+"&naoRealizado="+document.formulario.naoRealizado.checked,carregaChamados);
			} else {
				alert(txt) ;
			}
		} else {
			alert("Problemas no carregamento da página\nMensagem: ' " + xmlhttp.statusText + " '\nInforme a mensagem aos Administradores do Site.")
		}
	}
}

function alteraTipo() {
	if (xmlhttp.readyState==4) {
		if (xmlhttp.status==200) {
			var txt = xmlhttp.responseText;
			if(txt == "-1") {
				hideC2();
				loadXMLDoc("chamados.asp?id_cliente="+document.formulario.id_cliente.value+"&realizado="+document.formulario.realizado.checked+"&naoRealizado="+document.formulario.naoRealizado.checked,carregaChamados);
			} else {
				alert(txt) ;
			}
		} else {
			alert("Problemas no carregamento da página\nMensagem: ' " + xmlhttp.statusText + " '\nInforme a mensagem aos Administradores do Site.")
		}
	}
}

function alteraRealizado() {
	if (xmlhttp.readyState==4) {
		if (xmlhttp.status==200) {
			var txt = xmlhttp.responseText;
			if(txt == "-1") {
				hideC3();
				loadXMLDoc("chamados.asp?id_cliente="+document.formulario.id_cliente.value+"&realizado="+document.formulario.realizado.checked+"&naoRealizado="+document.formulario.naoRealizado.checked,carregaChamados);
			} else {
				alert(txt) ;
			}
		} else {
			alert("Problemas no carregamento da página\nMensagem: ' " + xmlhttp.statusText + " '\nInforme a mensagem aos Administradores do Site.")
		}
	}
}

function carregaChamadoIndv() {
	if (xmlhttp.readyState==4)  {
	  if (xmlhttp.status==200)  {
		var txt = xmlhttp.responseText;
		var xml = xmlhttp.responseXML;
		var node = xml.getElementsByTagName("interacaos");
		showC(document.getElementById("linha"+document.formulario.id_chamado.value));
		var HTML = "<table align='right' class='lista'><tr><td><b><a href='javascript:hideC();'>X - FECHAR</a></b></td></td></tr></table><br />";
				
		HTML += "<ul class='lista'>";					
		
		HTML += "<form action='sel_chamado.asp' method='post' name='ativa' id='ativa'>";
		HTML += "<input type='hidden' name='assunto_basico' value='" + node[0].childNodes[0].getAttribute("assunto_basico") + "'>";
		
		HTML += "<li style='background-color:#8D8D8D; padding-left:2px; color:#FFFFFF;'><b>ASSUNTO BÁSICO:</b></li>";
		HTML += "<li>" + node[0].childNodes[0].getAttribute("assunto_basico") + "<br />" + "<br /><br /></li>";
		
		HTML += "<li style='background-color:#8D8D8D; padding-left:2px; color:#FFFFFF;'><b>CONTATO NO CLIENTE:</b></li>";
		HTML += "<li>" + node[0].childNodes[0].getAttribute("chamado") + "<br /><br /></li>";
		
		HTML += "<li style='background-color:#8D8D8D; padding-left:2px; color:#FFFFFF;'><b>ABERTURA:</b></li>";
		HTML += "<li>" + node[0].childNodes[0].getAttribute("remetente") + " - " + node[0].childNodes[0].getAttribute("dataChamado") + "<br /><br /></li>";
		
		HTML += "<li style='background-color:#8D8D8D; padding-left:2px; color:#FFFFFF;'><b>MENSAGEM:</b></li>";
		HTML += "<li>" + node[0].childNodes[0].firstChild.nodeValue + "<br /><br /></li>";
		
		if (node[0].childNodes.length > 2) {
			HTML += "<li style='background-color:#8D8D8D; padding-left:2px; color:#FFFFFF;'><b>RESPOSTAS:</b></li>";
			for (a=2;a<node[0].childNodes.length;a++) {
				HTML += "<li style='background-color:"+node[0].childNodes[a].getAttribute("cor")+";'><b>"+ node[0].childNodes[a].getAttribute("remetente") + " - " + node[0].childNodes[a].getAttribute("entrada") + " - " + node[0].childNodes[a].getAttribute("entradaHora") + "</b><br /><i>" + node[0].childNodes[a].firstChild.nodeValue + "</i><br /><br />" + "</li>";
			}			
		}
		
		HTML += "<li style='background-color:#8D8D8D; padding-left:2px; color:#FFFFFF;'><b>RESPONDER:</b></li>";
		
		HTML += "<li style='padding-left:2px;'>Texto:</li>";
		HTML += "<li style='padding-left:2px;'><textarea name='texto' class='formnormal' style='height=100px; width=700px;'></textarea></li>";
		HTML += "<li style='padding-left:2px; text-align:center;'><input type='button' value='ENTRAR' onClick='checa(document.ativa)' style='height=17px; width=80px; color: BLACK; background-color: SILVER; font-family: Verdana; font-size: 10px; border: 1px double gray;'></li>";
		HTML += "</form>";
		
		HTML += "<form action='sel_chamado.asp' method='post' name='enviaEmail' id='enviaEmail'>";
		HTML += "<li style='padding-left:2px;'></li>";
		HTML += "<li style='padding-left:2px;'></li>";
		HTML += "<li style='padding-left:2px;'></li>";
		HTML += "<li style='background-color:#8D8D8D; padding-left:2px; color:#FFFFFF;'><b>ENVIAR EMAIL:</b></li>";
		HTML += "<li style='padding-left:2px;'></li>";
		HTML += "<li style='padding-left:2px;'>E-Mail(s):</li>";
		HTML += "<li style='padding-left:2px;'></li>";
		HTML += "<li style='padding-left:2px;'><input type='checkbox' name='ativaEmails' id='ativaEmails' value='yes' onClick='DesativaEmail(this, document.enviaEmail.emails);' checked> - <input type='text' name='emails' id='emails' value='"+node[0].childNodes[1].getAttribute("email")+"' class='formnormal' style='height=20px; width=250px; padding-top:2px; padding-left:3px;'></li>";
		HTML += "<li style='padding-left:2px;'><input type='checkbox' name='ativaEmails2' id='ativaEmails2' value='yes' onClick='DesativaEmail(this, document.enviaEmail.emails2);'> - <input type='text' name='emails2' id='emails2' value='' class='formnormal' style='height=20px; width=250px; padding-top:2px; padding-left:3px;' disabled></li>";
		HTML += "<li style='padding-left:205px;'><input type='button' value='ENVIAR' onClick='EnviaEmail(document.enviaEmail)' style='height=17px; width=80px; color: BLACK; background-color: SILVER; font-family: Verdana; font-size: 10px; border: 1px double gray;'></li>";
		HTML += "</form>";
		
		HTML += "</ul>";

		
		document.getElementById("chamadoIndv").innerHTML = HTML;
	  } else  {
		 alert("Problemas no carregamento da página\nMensagem: ' " + xmlhttp.statusText + " '\nInforme a mensagem aos Administradores do Site.")
	  }
	}
}


function carregaClientes() {
	if (xmlhttp.readyState==4)  {
	  if (xmlhttp.status==200)  {
		hideC();
		var txt = xmlhttp.responseText;
		var xml = xmlhttp.responseXML;
		var node = xml.getElementsByTagName("clientes");
		var HTML = "<ul class='lista'>";
		for (i=0;i<node.length;i++) {
			for (a=0;a<node[i].childNodes.length;a++) {
				HTML += "<li> - <a href='javascript:loadXMLDoc("+'"chamados.asp?id_cliente='+node[i].childNodes[a].getAttribute("id_cliente")+'&realizado='+document.formulario.realizado.checked+'&naoRealizado='+document.formulario.naoRealizado.checked+'"'+",carregaChamados); ativaCliente("+node[i].childNodes[a].getAttribute("id_cliente")+");' target='_self'>" + node[i].childNodes[a].getAttribute("nome") + "</a></li>";
			}
		}
		HTML += "</ul>";
		document.getElementById("Clientes").innerHTML = HTML;
		if(document.formulario.load.value == "") {
			loadXMLDoc("chamados.asp?filtroIdChamado="+document.formulario.filtroIdChamado.value+"&filtroCodigoCliente="+document.formulario.filtroCodigoCliente.value+"&realizado="+document.formulario.realizado.checked+"&naoRealizado="+document.formulario.naoRealizado.checked,carregaChamados) ;
			document.formulario.filtroIdChamado.value = "";
			document.formulario.filtroCodigoCliente.value = "";
			document.formulario.load.value = "yes" ;
		} else {
			loadXMLDoc("chamados.asp?filtroIdChamado="+document.formulario.filtroIdChamado.value+"&filtroCodigoCliente="+document.formulario.filtroCodigoCliente.value+"&id_cliente="+document.formulario.id_cliente.value+"&realizado="+document.formulario.realizado.checked+"&naoRealizado="+document.formulario.naoRealizado.checked,carregaChamados);
			document.formulario.filtroIdChamado.value = "";
			document.formulario.filtroCodigoCliente.value = "";
		}
	  } else  {
		 alert("Problemas no carregamento da página\nMensagem: ' " + xmlhttp.statusText + " '\nInforme a mensagem aos Administradores do Site.")
	  }
	}
}

function carregaCategorias() {
	if (xmlhttp.readyState==4)  {
	  if (xmlhttp.status==200)  {
		showC2(document.getElementById("colunaCategoria"+document.formulario.id_chamado.value));
		var writeBG;
		var txt = xmlhttp.responseText;
		var xml = xmlhttp.responseXML;
		var node = xml.getElementsByTagName("chamado_categorias");
		var HTML = "<ul class='lista'>";
		for (i=0;i<node.length;i++) {
			for (a=0;a<node[i].childNodes.length;a++) {
				if (node[i].childNodes[a].getAttribute("ativa") == "yes"){writeBG = "style='background-color:#DEDEDE;'";} else {writeBG = "";}
				HTML += "<li " + writeBG + "> - <a href='javascript:loadXMLDoc("+'"transitaChamado.asp?id_chamado='+node[i].childNodes[a].getAttribute("id_chamado")+'&id_chamado_categoria='+node[i].childNodes[a].getAttribute("id_chamado_categoria")+'"'+",alteraCategoria);' target='_self'>" + node[i].childNodes[a].getAttribute("nome") + "</a></li>";
			}
		}
		HTML += "</ul>";
		document.getElementById("altCategoria").innerHTML = HTML;
	  } else  {
		 alert("Problemas no carregamento da página\nMensagem: ' " + xmlhttp.statusText + " '\nInforme a mensagem aos Administradores do Site.")
	  }
	}
}

function carregaTipos() {
	if (xmlhttp.readyState==4)  {
	  if (xmlhttp.status==200)  {
		showC2(document.getElementById("colunaTipo"+document.formulario.id_chamado.value));
		var writeBG;
		var txt = xmlhttp.responseText;
		var xml = xmlhttp.responseXML;
		var node = xml.getElementsByTagName("chamado_tipos");
		var HTML = "<ul class='lista'>";
		for (i=0;i<node.length;i++) {
			for (a=0;a<node[i].childNodes.length;a++) {
				if (node[i].childNodes[a].getAttribute("ativa") == "yes"){writeBG = "style='background-color:#DEDEDE;'";} else {writeBG = "";}
				HTML += "<li " + writeBG + "> - <a href='javascript:loadXMLDoc("+'"transitaChamado.asp?id_chamado='+node[i].childNodes[a].getAttribute("id_chamado")+'&id_chamado_tipo='+node[i].childNodes[a].getAttribute("id_chamado_tipo")+'"'+",alteraTipo);' target='_self'>" + node[i].childNodes[a].getAttribute("nome") + "</a></li>";
			}
		}
		HTML += "</ul>";
		document.getElementById("altCategoria").innerHTML = HTML;
	  } else  {
		 alert("Problemas no carregamento da página\nMensagem: ' " + xmlhttp.statusText + " '\nInforme a mensagem aos Administradores do Site.")
	  }
	}
}

function carregaCategoriasSelect() {
	if (xmlhttp.readyState==4)  {
	  if (xmlhttp.status==200)  {
		var txt = xmlhttp.responseText;
		var xml = xmlhttp.responseXML;
		var nodeCategoria = xml.getElementsByTagName("chamado_categorias");
		var HTML = "";
		if (nodeCategoria[0].childNodes.length != 0) {
			HTML += 		"<select name='id_chamado_categoria'>" ;
			HTML +=				"<option value=''>-- Escolha aqui --</option>" ;
				for (j=0;j<nodeCategoria[0].childNodes.length;j++) {
					HTML += "<option value='"+nodeCategoria[0].childNodes[j].getAttribute("id_chamado_categoria")+"'>"+nodeCategoria[0].childNodes[j].getAttribute("nome")+"</option>" ;
				}
			HTML +=			"</select>" ;
	    } else {
			HTML += "<select name='id_chamado_categoria' disabled>";
			HTML += 	"<option value=''>-- Escolha uma categoria --</option>";
			HTML += "</select>";
		}
	  	document.getElementById("subCategoria").innerHTML = HTML;
	  } else  {
		 alert("Problemas no carregamento da página\nMensagem: ' " + xmlhttp.statusText + " '\nInforme a mensagem aos Administradores do Site.")
	  }
	}
}

function carregaRealizados() {
	if (xmlhttp.readyState==4)  {
	  if (xmlhttp.status==200)  {
		showC3(document.getElementById("colunaRealizado"+document.formulario.id_chamado.value));
		var writeBG;
		var txt = xmlhttp.responseText;
		var xml = xmlhttp.responseXML;
		var node = xml.getElementsByTagName("realizados");
		var HTML = "<ul class='lista'>";
		for (i=0;i<node.length;i++) {
			for (a=0;a<node[i].childNodes.length;a++) {
				if (node[i].childNodes[a].getAttribute("ativo") == "Sim"){writeBGSim = "style='background-color:#DEDEDE;'";} else {writeBGSim = "";}
				if (node[i].childNodes[a].getAttribute("ativo") == "Não"){writeBGNao = "style='background-color:#DEDEDE;'";} else {writeBGNao = "";}
				HTML += "<li " + writeBGSim + "> - <a href='javascript:loadXMLDoc("+'"transitaChamado.asp?id_chamado='+node[i].childNodes[a].getAttribute("id_chamado")+'&tipo=Sim"'+",alteraRealizado);' target='_self'>Sim</a></li>";
				HTML += "<li " + writeBGNao + "> - <a href='javascript:loadXMLDoc("+'"transitaChamado.asp?id_chamado='+node[i].childNodes[a].getAttribute("id_chamado")+'&tipo=Nao"'+",alteraRealizado);' target='_self'>Não</a></li>";
			}
		}
		HTML += "</ul>";
		document.getElementById("altRealizado").innerHTML = HTML;
	  } else  {
		 alert("Problemas no carregamento da página\nMensagem: ' " + xmlhttp.statusText + " '\nInforme a mensagem aos Administradores do Site.")
	  }
	}
}

function carregaChamados() {
	if (xmlhttp.readyState==4)  {
	  if (xmlhttp.status==200)  {
		hideC();
		var txt = xmlhttp.responseText;
		var xml = xmlhttp.responseXML;
		var node = xml.getElementsByTagName("chamados");
		var nodeCategoria = xml.getElementsByTagName("chamado_categorias");
		var writeMensagem = "Operação realizada com sucesso!";
		var writeSelected;
		var HTML = "<table width='100%' cellspacing='5' class='lista'>";
		if(node[0].childNodes.length > 0) {
			HTML += "<tr style='background-color:#000000; color:#FFFFFF;'>"
			HTML += 	"<td style='padding:2px;' width='45'><b>ORDEM</b></td>";
			HTML += 	"<td style='padding:2px;' width='40'><b>Nº</b></td>";
			HTML += 	"<td style='padding:2px;'><b>ASSUNTO BÁSICO</b></td>";
			HTML += 	"<td style='padding:2px;' colspan='2'><b>CATEGORIA</b></td>";
			HTML += 	"<td style='padding:2px;' colspan='2' width='80'><b>TIPO</b></td>";
			HTML += 	"<td style='padding:2px;'><b>DATA</b></td>";
			HTML += 	"<td style='padding:2px;'><b>HORA</b></td>";
			HTML += 	"<td style='padding:2px;' colspan='2'><b>CONCLUÍDO</b></td>";
			HTML += 	"<td style='padding:2px;'><b>CLIENTE</b></td>";
			HTML += "</tr>"
			for (i=0;i<node.length;i++) {
				for (a=0;a<node[i].childNodes.length;a++) {
					HTML += "<tr class='" + node[i].childNodes[a].getAttribute("cor") + "' id='linha" + node[i].childNodes[a].getAttribute("id_chamado") + "'>"
					HTML += 	"<td><a href='javascript:loadXMLDoc("+'"chamados.asp?id_chamado='+node[i].childNodes[a].getAttribute("id_chamado")+'"'+",carregaChamadoIndv); ativaChamado("+node[i].childNodes[a].getAttribute("id_chamado")+");' target='_self'>" + node[i].childNodes[a].getAttribute("ordem") + "</a></td>";
					HTML += 	"<td><a href='javascript:loadXMLDoc("+'"chamados.asp?id_chamado='+node[i].childNodes[a].getAttribute("id_chamado")+'"'+",carregaChamadoIndv); ativaChamado("+node[i].childNodes[a].getAttribute("id_chamado")+");' target='_self'><b>" + node[i].childNodes[a].getAttribute("id_chamado") + "</b></a></td>";
					HTML += 	"<td><a href='javascript:loadXMLDoc("+'"chamados.asp?id_chamado='+node[i].childNodes[a].getAttribute("id_chamado")+'"'+",carregaChamadoIndv); ativaChamado("+node[i].childNodes[a].getAttribute("id_chamado")+");' target='_self'>" + node[i].childNodes[a].getAttribute("assunto_basico") + "</a></td>";
					HTML += 	"<td id='colunaCategoria" + node[i].childNodes[a].getAttribute("id_chamado") + "'><a href='javascript:loadXMLDoc("+'"chamados.asp?id_chamado='+node[i].childNodes[a].getAttribute("id_chamado")+'"'+",carregaChamadoIndv); ativaChamado("+node[i].childNodes[a].getAttribute("id_chamado")+");' target='_self'>" + node[i].childNodes[a].getAttribute("chamado_categoria") + "</a></td>";
					HTML += 	"<td><a href='javascript:loadXMLDoc("+'"chamados.asp?id_chamado='+node[i].childNodes[a].getAttribute("id_chamado")+'&altCategoria=yes"'+",carregaCategorias); ativaChamado("+node[i].childNodes[a].getAttribute("id_chamado")+");' target='_self'><img src='../i/altCategoria.gif' alt='Alterar Categoria' border='0'/></a></td>";
					HTML += 	"<td id='colunaTipo" + node[i].childNodes[a].getAttribute("id_chamado") + "'><a href='javascript:loadXMLDoc("+'"chamados.asp?id_chamado='+node[i].childNodes[a].getAttribute("id_chamado")+'"'+",carregaChamadoIndv); ativaChamado("+node[i].childNodes[a].getAttribute("id_chamado")+");' target='_self'>" + node[i].childNodes[a].getAttribute("chamado_tipo") + "</a></td>";
					HTML += 	"<td><a href='javascript:loadXMLDoc("+'"chamados.asp?id_chamado='+node[i].childNodes[a].getAttribute("id_chamado")+'&altTipo=yes"'+",carregaTipos); ativaChamado("+node[i].childNodes[a].getAttribute("id_chamado")+");' target='_self'><img src='../i/altCategoria.gif' alt='Alterar Tipo' border='0'/></a></td>";
					HTML += 	"<td><a href='javascript:loadXMLDoc("+'"chamados.asp?id_chamado='+node[i].childNodes[a].getAttribute("id_chamado")+'"'+",carregaChamadoIndv); ativaChamado("+node[i].childNodes[a].getAttribute("id_chamado")+");' target='_self'>" + node[i].childNodes[a].getAttribute("entrada") + "</a></td>";
					HTML += 	"<td><a href='javascript:loadXMLDoc("+'"chamados.asp?id_chamado='+node[i].childNodes[a].getAttribute("id_chamado")+'"'+",carregaChamadoIndv); ativaChamado("+node[i].childNodes[a].getAttribute("id_chamado")+");' target='_self'>" + node[i].childNodes[a].getAttribute("entradaHora") + "</a></td>";
					HTML += 	"<td id='colunaRealizado" + node[i].childNodes[a].getAttribute("id_chamado") + "'><a href='javascript:loadXMLDoc("+'"chamados.asp?id_chamado='+node[i].childNodes[a].getAttribute("id_chamado")+'"'+",carregaChamadoIndv); ativaChamado("+node[i].childNodes[a].getAttribute("id_chamado")+");' target='_self'>" + node[i].childNodes[a].getAttribute("realizado") + "</a></td>";
					HTML += 	"<td><a href='javascript:loadXMLDoc("+'"chamados.asp?id_chamado='+node[i].childNodes[a].getAttribute("id_chamado")+'&altRealizado=yes"'+",carregaRealizados); ativaChamado("+node[i].childNodes[a].getAttribute("id_chamado")+");' target='_self'><img src='../i/altCategoria.gif' alt='Realizado' border='0'/></a></td>";
					HTML += 	"<td><a href='javascript:loadXMLDoc("+'"chamados.asp?id_chamado='+node[i].childNodes[a].getAttribute("id_chamado")+'"'+",carregaChamadoIndv); ativaChamado("+node[i].childNodes[a].getAttribute("id_chamado")+");' target='_self'>" + node[i].childNodes[a].getAttribute("cliente") + "</a></td>";
					HTML += "</tr>"
				}
			}
			
		} else {
			HTML += "<tr><td><span style='color:#000000;'>Não existem registros até o momento.</span></td></tr>";
		}
		HTML += "</table>";
		document.getElementById("Chamados").innerHTML = HTML;
	  } else  {
		 alert("Problemas no carregamento da página\nMensagem: ' " + xmlhttp.statusText + " '\nInforme a mensagem aos Administradores do Site.")
	  }
	}
}

function endScript() {
	document.getElementById("linhaAcao").style.display = 'none';
	for (i=0;i<document.ativa.elements.length;i++) {
		document.ativa.elements[i].disabled = false ;
	}
}

function checkeds() {
	if (xmlhttp.readyState==4){
		if (xmlhttp.status==200){
			if (xmlhttp.responseText == "-1"){
				for (i=0;i<document.ativa.elements.length;i++) {
					document.ativa.elements[i].disabled = true ;
				}
				document.getElementById("linhaAcao").style.display = 'block';
				document.getElementById("acao").innerHTML = "Operação efetuada com sucesso!";
				setTimeout('endScript()', 2000) ;
			}
		} else{	  
			alert("Problemas no carregamento da página\nMensagem: ' " + xmlhttp.statusText + " '\nInforme a mensagem aos Administradores do Site.")
		}
	}
}


//FIM SISTEMA CHAMADOS


function modCliente() {
	if (xmlhttp.readyState==4)  {
	  // if "OK"
	  if (xmlhttp.status==200)  {
		var txt = xmlhttp.responseText;
		alert(txt) ;
		if(txt == "Operacao efetuada com sucesso") {
			exibeBairros("select") ;
			document.getElementById("id_cliente").value = 0 ;
			document.getElementById("nome").value = "" ;
			document.getElementById("email").value = "" ;
			document.getElementById("senha").value = "" ;
			document.getElementById("senha2").value = "" ;
			document.getElementById("logradouro").value = "" ;
			document.getElementById("numero").value = "" ;
			document.getElementById("complemento").value = "" ;
			if(document.getElementById("bairro")) {
				document.getElementById("bairro").value = "" ;
			} else if(document.getElementById("id_regiao")) {
				document.getElementById("id_regiao")[0].selected = true ;
			}
			document.getElementById("cep1").value = "" ;
			document.getElementById("cep2").value = "" ;
			document.getElementById("ref").value = "" ;
			document.getElementById("ref2").value = "" ;
			document.getElementById("dia").value = "" ;
			document.getElementById("mes").value = "" ;
			document.getElementById("dddtel").value = "" ;
			document.getElementById("tel").value = "" ;
			document.getElementById("dddcel").value = "" ;
			document.getElementById("cel").value = "" ;
			document.getElementById("rg").value = "" ;
			document.getElementById("cpf").value = "" ;
			document.getElementById("news").checked = false ;
			document.getElementById("botao").value = "CADASTRAR" ;
		}		
	  } else  {
		 alert("Problemas no carregamento da página\nMensagem: ' " + xmlhttp.statusText + " '\nInforme a mensagem aos Administradores do Site.")
	  }
	}
}

function muda_form(acao,quem) {
	if (acao == "ativa") {
		quem.style.backgroundColor = "#EDEDED" ;
	} else {
		quem.style.backgroundColor = "#FFFFFF" ;
	}
}
