function estatisticas(news1_cod){
	w = 600;
	h = 500;
	largura = screen.width;
	altura = screen.height;
	XX =(largura-w)/2;
	YY = (altura-h)/2;
	janela = window.open('estatisticas.php?news1_cod='+news1_cod,'ESTATISTICAS','width='+w+',height='+h+',left='+XX+',top='+YY+',scrollbars=yes,status=no,resizable=no, toolbar=no,directories=no,menubar=no');
	janela.focus();
}

function change_menu(num, state){
	ne = "submenu_"+num;
		
	// MOSTRA OU ESCONDE O SUBMENU
	if (state == "on"){
		vis = "visible";
	}
	else {
		vis = "hidden";
	}
		
	ne = "submenu_"+num;
		
	document.getElementById(ne).style.visibility = vis;
}

function confirma(hp){
	if (confirm('Deseja realmente excluir este registro?')){
		parent.location = hp;
	}
}

function mudaTeste(obj){

	var indice = obj.selectedIndex;
	var teste = obj.options[indice].value;
	
	var url = "envioTeste.php";
	url = url + "?teste="+teste;
	url = url + "&cache=" + new Date().getTime();

	xmlhttp.open("GET", url, true);
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) {
				var retorno = url_decode(xmlhttp.responseText);
				
				var divCampo = document.getElementById("teste");
				divCampo.innerHTML = "";
				
				if (retorno.length > 0){
					divCampo.innerHTML = retorno;
				}
			}
		}
	};
	xmlhttp.send(null);
}

function mudaTipo(obj,news1_cod){
	var indice = obj.selectedIndex;
	var tipo = obj.options[indice].value;
	
	var url = "tipoEnvio.php";
	url = url + "?tipo="+tipo;
	url = url + "&news1_cod="+news1_cod;
	url = url + "&cache=" + new Date().getTime();
	xmlhttp.open("GET", url, true);
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) {
				var retorno = url_decode(xmlhttp.responseText);
				
				var divCampo = document.getElementById("tipo");
				divCampo.innerHTML = "";
				
				if (retorno.length > 0){
					divCampo.innerHTML = retorno;
				}
			}
		}
	};
	xmlhttp.send(null);
}

function getDados(obj) {
	var indice = obj.selectedIndex;
	var area = obj.options[indice].value

	if (area != '0'){
		var url = "../clientes/registros.php";
		url = url + "?area="+area;
		url = url + "&cache=" + new Date().getTime();
		xmlhttp.open("GET", url, true);
		xmlhttp.onreadystatechange = preencheCampo;
		xmlhttp.send(null);
	}
	else {
		var divRegistros = document.getElementById("scategorias");
		divRegistros.innerHTML = "";
			
		var elemSel = document.createElement("SELECT");
		elemSel.name = 'scat1_cod';
		elemSel.id = 'scat1_cod';
		var elemOpt = document.createElement("OPTION");
		elemOpt.setAttribute("value",0);
		elemOpt.appendChild(document.createTextNode('Selecione...'));
		elemSel.appendChild(elemOpt);
		
		divRegistros.appendChild(elemSel);
	}
}

function preencheCampo(){
	if (xmlhttp.readyState == 4) {
		if (xmlhttp.status == 200) {
			var retorno = url_decode(xmlhttp.responseText);
			
			var divRegistros = document.getElementById("scategorias");
			divRegistros.innerHTML = "";
			
			var elemSel = document.createElement("SELECT");
			elemSel.name = 'scat1_cod';
			elemSel.id = 'scat1_cod';
			
			if (retorno.length > 0){
				var resultados = retorno.split(';;;;');
				
				var elemOpt = document.createElement("OPTION");
				elemOpt.setAttribute("value",0);
				elemOpt.appendChild(document.createTextNode('Selecione...'));
				elemSel.appendChild(elemOpt);
				
				for (i=0; i<resultados.length; i++){
					var valores = resultados[i].split('////');
					
					var elemOpt = document.createElement("OPTION");
					elemOpt.setAttribute("value",valores[0]);
					elemOpt.appendChild(document.createTextNode(valores[1]));
					elemSel.appendChild(elemOpt);
				}
			}
			else {
				var elemOpt = document.createElement("OPTION");
				elemOpt.setAttribute("value",0);
				elemOpt.appendChild(document.createTextNode('Nenhuma sub-categoria cadastrada'));
				elemSel.appendChild(elemOpt);
			}
			
			divRegistros.appendChild(elemSel);
			elemSel.style.width = '250px;';
		}
	}
}

function listaSubCategorias(obj) {
	var indice = obj.selectedIndex;
	var area = obj.options[indice].value

	if (area != '0'){
		var url = "../clientes/registros.php";
		url = url + "?area="+area;
		url = url + "&cache=" + new Date().getTime();
		xmlhttp.open("GET", url, true);
		xmlhttp.onreadystatechange = preencheSubCategorias;
		xmlhttp.send(null);
	}
	else {
		var divRegistros = document.getElementById("subcategorias");
		divRegistros.innerHTML = "";
			
		var elemSel = document.createElement("SELECT");
		elemSel.name = 'scat1_cod';
		
		var elemOpt = document.createElement("OPTION");
		elemOpt.setAttribute("value",0);
		elemOpt.appendChild(document.createTextNode('Todas as Sub-Categorias'));
		elemSel.appendChild(elemOpt);
		
		divRegistros.appendChild(elemSel);
	}
}

function preencheSubCategorias(){
	if (xmlhttp.readyState == 4) {
		if (xmlhttp.status == 200) {
			var retorno = url_decode(xmlhttp.responseText);
			
			var divRegistros = document.getElementById("subcategorias");
			divRegistros.innerHTML = "";
			
			var elemSel = document.createElement("SELECT");
			elemSel.name = 'scat1_cod';
			elemSel.style.width = '200px';
			
			var elemOpt = document.createElement("OPTION");
			elemOpt.setAttribute("value",0);
			elemOpt.appendChild(document.createTextNode('Todas'));
			elemSel.appendChild(elemOpt);
			
			if (retorno.length > 0){
				var resultados = retorno.split(';;;;');
				
				for (i=0; i<resultados.length; i++){
					var valores = resultados[i].split('////');
					
					var elemOpt = document.createElement("OPTION");
					elemOpt.setAttribute("value",valores[0]);
					elemOpt.appendChild(document.createTextNode(valores[1]));
					elemSel.appendChild(elemOpt);
				}
			}
			else {
				var elemOpt = document.createElement("OPTION");
				elemOpt.setAttribute("value",0);
				elemOpt.appendChild(document.createTextNode('Nenhuma sub-categoria cadastrada'));
				elemSel.appendChild(elemOpt);
			}
			
			divRegistros.appendChild(elemSel);
		}
	}
}

function abreFotos(gale1_cod){
	w = 600;
	h = 500;
	largura = screen.width;
	altura = screen.height;
	XX =(largura-w)/2;
	YY = (altura-h)/2;
	janela = window.open('fotos.php?gale1_cod='+gale1_cod,'FOTOS','width='+w+',height='+h+',left='+XX+',top='+YY+',scrollbars=yes,status=no,resizable=no, toolbar=no,directories=no,menubar=no');
	janela.focus();
}

// -------------------------------  *****   Ajax ******* -----------------------------------------------
/*
EXEMPLO DE UTILIZAÇÃO
 
	ResultadoListData = null;
	
	SendData('ajax.asp','RetornaListData','XML',undefined,false);
			
	alert(ResultadoListData);


FORMATO DO RETORNO XML 

	<RETORNO> ... conteúdo... </RETORNO>
 
 */

//-=-=-=-=- AJAX BY CL/CGB -=-=-=-=-
	var xmlhttp=false;
	var isFirefox=false;
	var agt = navigator.userAgent.toLowerCase(); 
	var is_opera = (agt.indexOf("opera") != -1); 			
	var sReturnTypeDefault = "XML";
	var sLoadFunctionDefault = "";
	var sASyncDefault = true;
	var iASync = 0;
		
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {
			xmlhttp = false;
		}
	}

	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest();
		isFirefox = true;
	}

	//END - XMLHTTP
	
	function SetReturnType(value){
		sReturnTypeDefault = value;
	}

	function SetLoadFunction(value){
		sLoadFunctionDefault = value;
	}

	function SetASync(value){
		sASyncDefault = value;
	}
	
	function OnlySendDataASync(sURL){
		eval('var xmlhttp'+iASync+' = false;');
		if (!(isFirefox)){
			try {
				eval('xmlhttp'+iASync+' = new ActiveXObject("Msxml2.XMLHTTP");');
				
			} catch (e) {
				try {
					eval('xmlhttp'+iASync+' = new ActiveXObject("Microsoft.XMLHTTP");');
				} catch (e) {
				}
			}
		}else{
			if (typeof XMLHttpRequest!='undefined') {
				eval('xmlhttp'+iASync+' = new XMLHttpRequest();');
			}
		}
		try {
			eval('xmlhttp'+iASync+'.open("GET", sURL, true);');
			eval('xmlhttp'+iASync+'.send(null);');
			} catch (e) {

		}
		iASync += 1;
		return true;
	}

	function SendData(sURL,sWriteFunction,sReturnType,sLoadFunction,sASync){
		
		if (sURL == undefined)
		{
			alert("É necessário informar alguma URL para consulta");
			return false;
		}


		if( sASync && sWriteFunction != undefined && sReturnType!='XML' )
		{			
			if( typeof sWriteFunction == 'string' )
				eval('sWriteFunction='+sWriteFunction);
			
			if( typeof sLoadFunction == 'string' )
				eval('sLoadFunction='+sLoadFunction);
				
			ajax.addRequest( sURL, sWriteFunction,undefined,undefined,undefined, sLoadFunction,undefined,"GET");
			return;
		}
		
		if (sWriteFunction == undefined)
		{				
			OnlySendDataASync(sURL);
			return false;
		}
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");			
			
		} catch (e) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				xmlhttp = new XMLHttpRequest();
			}
		}

		if (sReturnType == undefined)
		{
			sReturnType = sReturnTypeDefault
		}

		if (sLoadFunction == undefined)
		{
			sLoadFunction = sLoadFunctionDefault;
		}

		if (sASync == undefined)
		{
			sASync = sASyncDefault;
		}
				
		if (sLoadFunction != "")
		{
			ShowLoading(sLoadFunction,true);
		}

		xmlhttp.open("GET", sURL, sASync);
		xmlhttp.onreadystatechange= function() {
			
			/* estado carregado */
			if (xmlhttp.readyState==4) 
			{
				eval(sWriteFunction + "(RequisitionResponse(sReturnType),sReturnType);");
				if (sLoadFunction != "")
				{
					ShowLoading(sLoadFunction,false);
				}
			}
		}		
		
		try{ 
			xmlhttp.send(null);
		}catch(e){ 

			ResultadoListData = null;
			return false;
		}

		if (isFirefox && !(sASync))
		{
			if (xmlhttp.readyState==4) 
				{
					eval(sWriteFunction + "(RequisitionResponse(sReturnType),sReturnType);");
					if (sLoadFunction != "")
					{
						ShowLoading(sLoadFunction,false);
					}
				}
		}
	}
	
	function RequisitionResponse(sReturnType){
		if (sReturnType.toUpperCase() == 'TXT')
			return xmlhttp.responseText;
		if (sReturnType.toUpperCase() == 'XML')
			if (isFirefox){
				xmlhttp.responseXML.normalize();
			}
			return xmlhttp.responseXML;
	}

	function ShowLoading(tDiv,b){
		if (b){
			eval("document.getElementById('" + tDiv + "').style.visibility = ''");
		}else{
			eval("document.getElementById('" + tDiv + "').style.visibility = 'hidden'");
		}
	}

	function GetValueXML(doc){
		if (doc.getElementsByTagName('RETORNO').length != 0){
			var element = doc.getElementsByTagName('RETORNO').item(0);
			if (!(element.firstChild == null)){
				return element.firstChild.data.trim();//replace para spaco no ffx
			}else{
				return '';/*entra aqui qndo o retorno é vazio*/
			}
		}else{
			return null;/*entra aqui qndo der pau ou nao tiver*/
		}
	}

	function RetornaListData(resposta,type)
	{
		if (resposta != null){
			if (type == 'XML'){
				
				ResultadoListData = GetValueXML(resposta);
				if (!(isFirefox)){
					AlimentaResultadoListData(ResultadoListData);
				}
			}else{
				ResultadoListData = resposta; // qndo for txt(JSON) deve se testar 
				if (!(isFirefox)){
					AlimentaResultadoListData(ResultadoListData);
				}
			}
		}else{
			ResultadoListData = null;
			AlimentaResultadoListData(null);
		}
		return true;
	}	
	
	function GetValueRetornoXML(doc){//Aki varre o resultado para buscar..
		if (doc.length != 0){
			var element = doc.item(0);
			if (!(element.firstChild == null)){
				return element.firstChild.data.trim();//replace para spaco no ffx
			}else{
				return '';/*entra aqui qndo o retorno é vazio*/
			}
		}else{
			return null;/*entra aqui qndo der pau ou nao tiver*/
		}
	}	
	
	String.prototype.trim = function()
	{
		/*
		^  - busca do inicio da string
		\s - equivale a espaço em branco
		+  - equivale a buscar 1 ou mais ocorrencias
		|  - equivale a OR
		$  - busca do fim da string
		g  - busca por todas as ocorrencias
		*/
		
		return this.replace(/^\s+|\s+$/g , "" );
	}
	
	//-=-=-=-=- FIM AJAX -=-=-=-=-=-=-

	
	ResultadoListData = null
	
	function  AlimentaResultadoListData(Valor)
	{
		ResultadoListData = Valor;
	}
	
// -------------------------------  //////////  Ajax \\\\\\\\\\\\ -----------------------------------------------

// No XML do AJAX como ele nao aceita acentuacao, é necessario converter para HTML
// mas o XML nao aceita o carcter &, entao colocasse o HTML do acento mas ao inves de &
// usa-se # exemplo #aacute = á em html seria &aacute, essa funcao tem como finalidade
// converter o # para & e converter o html para a letra correspondente
function htmlToText(strTexto)
{
	strTexto = strTexto.replace(/#/g, "&" );
	strTexto = strTexto.replace(/&agrave;/g, "a" ); 
	strTexto = strTexto.replace(/&acirc;/g , "â" );
	strTexto = strTexto.replace(/&auml;/g 	, "ä" );
	strTexto = strTexto.replace(/&otilde;/g, "o" );
	strTexto = strTexto.replace(/&ograve;/g, "o" );
	strTexto = strTexto.replace(/&ocirc;/g, "ô" );
	strTexto = strTexto.replace(/&egrave;/g, "e" );
	strTexto = strTexto.replace(/&eacute;/g, "é" );
	strTexto = strTexto.replace(/&ecirc;/g , "e" );
	strTexto = strTexto.replace(/&euml;/g 	, "ë" );
	strTexto = strTexto.replace(/&igrave;/g, "i" );
	strTexto = strTexto.replace(/&iacute;/g, "í" );	
	strTexto = strTexto.replace(/&iuml;/g 	, "i" );
	strTexto = strTexto.replace(/&ccedil;/g, "ç" );
	strTexto = strTexto.replace(/&Aacute;/g, "Á" );
	strTexto = strTexto.replace(/&Agrave;/g, "A" );
	strTexto = strTexto.replace(/&Acirc;/g , "Â" );
	strTexto = strTexto.replace(/&Auml;/g 	, "Ä" );
	strTexto = strTexto.replace(/&Otilde;/g, "O" );
	strTexto = strTexto.replace(/&Ograve;/g, "O" );
	strTexto = strTexto.replace(/&Ocirc;/g, "Ô" );
	strTexto = strTexto.replace(/&Egrave;/g, "E" );
	strTexto = strTexto.replace(/&Eacute;/g, "É" );
	strTexto = strTexto.replace(/&Ecirc;/g , "E" );
	strTexto = strTexto.replace(/&Euml;/g 	, "Ë" );
	strTexto = strTexto.replace(/&Igrave;/g, "I" );
	strTexto = strTexto.replace(/&Iacute;/g, "Í" );	
	strTexto = strTexto.replace(/&Iuml;/g 	, "I" );
	strTexto = strTexto.replace(/&Ccedil;/g, "Ç" );
	strTexto = strTexto.replace(/&ugrave;/g, "ú" );
	strTexto = strTexto.replace(/&Ugrave;/g, "Ú" );
	strTexto = strTexto.replace(/&ecirc;/g, "e" );
	strTexto = strTexto.replace(/&Ecirc;/g, "E" );
	strTexto = strTexto.replace(/&atilde;/g, "a" );
	strTexto = strTexto.replace(/&Atilde;/g, "A" );
	strTexto = strTexto.replace(/&aacute;/g, "á" );
	strTexto = strTexto.replace(/&Aacute;/g, "Á" );
	strTexto = strTexto.replace(/&ocirc;/g, "ô" );
	strTexto = strTexto.replace(/&Ocirc;/g, "Ô" );
	return strTexto;
}		

/**
 * Gerencia a troca de destinatario quando cadastra um novo boletim
 * @param select elemento Select
 */
function alteraRemetente(select){
	//select com as opcoes de remetente
	//var selectRemetente = document.getElementById("remetente_select");

	//div que guarda o input text 
	var divOutro = document.getElementById("div_outro");
	
	//o input type="text" que escreverao o outro remetente
	var txtOutro = document.getElementById("outro_txt");
	
	/**
	 * Se o valor do select for 0 (outro)
	 */
	if(select.value == "0"){
		//mostra a div do elemento
		divOutro.style.display = "inline";
		
		//tira o nome do select (nao posso dar post em dois elementos com o mesmo nome)
		select.name = "";
		
		//atribuo o nome no elemento texto
		txtOutro.name="usua1_nom";
		
		txtOutro.focus(true);
		
	}
	/**
	 * Outros valores selecionados no select
	 */
	else{
		if(divOutro.style.display != "none"){
			//esconde a div do elemento
			divOutro.style.display = "none";
			
			//atribuo o nome no elemento select
			select.name = "usua1_nom";
			

			//tira o nome do input (nao posso dar post em dois elementos com o mesmo nome)
			txtOutro.name="";
			
		}
	}
	
}


