// JavaScript Document
////////////////////////////////////////////////////////////////////////////////
function Alterar_Noticia(valor) {
	var mensagem = document.getElementById("id_mensagem_noticias");
	mensagem.innerHTML = "";
	if (ajax) {
 		pagina = document.getElementById("id_noticias");
		ajax.open("POST", "../admin/xml_pesquisa_noticia.php", true);
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		ajax.onreadystatechange = function() {
			if (ajax.readyState == 1) { pagina.innerHTML = "Carregando...<img src=\"../img/loading.gif\">"; }
           	if (ajax.readyState == 4) { if(ajax.responseXML) { xml_Altera_Noticia(ajax.responseXML); } }
       	}
	   	var params = "codigo="+valor;
       	ajax.send(params);
    }
}
function xml_Altera_Noticia(obj) {
    var dataArray = obj.getElementsByTagName("pesq_noticias");
	var codigo = ""; var titulo = ""; var descricao = ""; var dados = "";
    if (dataArray.length > 0) {
		dados = "<form name=\"formNoticia\">";
		dados+= "<table width=\"105%\" bgcolor=\"#eaeaea\" cellspacing=\"1\" cellpadding=\"2\" border=\"0\">";
		dados+= "<th class=\"titulo\" bgcolor=\"#FFF0D2\">Preencher campos para Alterar Notícia</th>";
        for (var i = 0; i < dataArray.length; i++) {
        	var item = dataArray[i];
			codigo = item.getElementsByTagName("codigo")[0].firstChild.nodeValue;
			titulo = item.getElementsByTagName("titulo")[0].firstChild.nodeValue;
			descricao = item.getElementsByTagName("descricao")[0].firstChild.nodeValue;
			dados+= "<tr><td>Titulo:</td></tr>";
			dados+= "<tr><td><textarea name=\"txtTitulo\" class=\"txtarea\" cols=\"80\" rows=\"1\" onKeyDown=\"TABEnter()\">"+titulo+"</textarea></td></tr>";
			dados+= "<tr><td>Descrição:</td></tr>";
			dados+= "<tr><td><textarea name=\"txtDescricao\" class=\"txtarea\" cols=\"80\" rows=\"4\" onKeyDown=\"TABEnter()\">"+descricao+"</textarea>&nbsp;&nbsp;";
			dados+= "<input name=\"Gravar\" type=\"button\" class=\"conteudo_input\" onClick=\"Gravar_Noticia("+codigo+")\" value=\"   Gravar   \" />&nbsp;";
			dados+= "<input name=\"Limpar\" type=\"reset\" class=\"conteudo_input\" value=\"Limpar Campos\" />";
			dados+= "</td></tr>";
      	}
		dados+= "</table></form>";
		pagina.innerHTML = dados;
   	}
}

////////////////////////////////////////////////////////////////////////////////
function New_Noticia() {
	var mensagem = document.getElementById("id_mensagem_noticias");
	mensagem.innerHTML = "";
	if (ajax) {
 		var dados = "";
		pagina = document.getElementById("id_noticias");
		dados = "<form name=\"formNoticia\">";
		dados+= "<table width=\"105%\" bgcolor=\"#eaeaea\" cellspacing=\"1\" cellpadding=\"2\" border=\"0\">";
		dados+= "<th class=\"titulo\" bgcolor=\"#FFF0D2\">Incluir nova Notícia</th>";
		dados+= "<tr><td>Titulo:</td></tr>";
		dados+= "<tr><td><textarea name=\"txtTitulo\" class=\"txtarea\" cols=\"80\" rows=\"1\" onKeyDown=\"TABEnter()\"></textarea></td></tr>";
		dados+= "<tr><td>Descrição:</td></tr>";
		dados+= "<tr><td><textarea name=\"txtDescricao\" class=\"txtarea\" cols=\"80\" rows=\"4\" onKeyDown=\"TABEnter()\"></textarea>&nbsp;&nbsp;";
		dados+= "<input name=\"Incluir\" type=\"button\" class=\"conteudo_input\" onClick=\"Incluir_Noticia()\" value=\"   Incluir   \" />&nbsp;";
		dados+= "<input name=\"Limpar\" type=\"reset\" class=\"conteudo_input\" value=\"Limpar Campos\" />";
		dados+= "</td></tr>";
		dados+= "</table></form>";
		pagina.innerHTML = dados;	
    }
}

////////////////////////////////////////////////////////////////////////////////
function Incluir_Noticia() {
	var mensagem = document.getElementById("id_mensagem_noticias");
	mensagem.innerHTML = "";
	if (Valida_Noticia() == false) { exit; }
	if (ajax) {
		ajax.open("POST", "../admin/xml_incluir_noticia.php", true);
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		ajax.onreadystatechange = function() {
		   	if (ajax.readyState == 1) { mensagem.innerHTML = "Cadastrando...<img src=\"../img/loading.gif\">"; }
           	if (ajax.readyState == 4) { 
				if (ajax.status == 200) { if (ajax.responseText) { mensagem.innerHTML = "Notícia inclusa com sucesso."; } }
			}
       }
	   var titulo = "titulo="+document.formNoticia.txtTitulo.value;
	   var descricao = "descricao="+document.formNoticia.txtDescricao.value;
	   var params = titulo+"&"+descricao;
	   ajax.send(params);
    }
}

////////////////////////////////////////////////////////////////////////////////
function Valida_Noticia() {
	var titulo = document.formNoticia.txtTitulo.value;
	var descricao = document.formNoticia.txtDescricao.value;
	var mensagem = document.getElementById("id_mensagem_noticias");
	if (titulo.length < 3) {
		mensagem.innerHTML = "Preenche o Titulo Corretamente.";
		document.formNoticia.txtTitulo.focus();
		return false;
	}
	if (descricao.length < 3) {
		mensagem.innerHTML = "Preenche a Descrição Corretamente.";
		document.formNoticia.txtDescricao.focus();
		return false;
	}
}

////////////////////////////////////////////////////////////////////////////////
function Excluir_Noticia(valor) {
	var mensagem = document.getElementById("id_mensagem_noticias");
	mensagem.innerHTML = "";
	if (ajax) {
		ajax.open("POST", "../admin/xml_excluir_noticia.php", true);
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		ajax.onreadystatechange = function() {
		   	if (ajax.readyState == 1) { mensagem.innerHTML = "Excluindo...<img src=\"../img/loading.gif\">"; }
           	if (ajax.readyState == 4) { 
				if (ajax.status == 200) { if (ajax.responseText) { mensagem.innerHTML = "Notícia excluída com sucesso."; } }
		   	}
       	}
	   	var params = "codigo="+valor;
       	ajax.send(params);
    }
}

////////////////////////////////////////////////////////////////////////////////
function Gravar_Noticia(valor) {
	var mensagem = document.getElementById("id_mensagem_noticias");
	mensagem.innerHTML = "";	
	if (Valida_Noticia() == false) { exit; }
	if (ajax) {
		ajax.open("POST", "../admin/xml_gravar_noticia.php", true);
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		ajax.onreadystatechange = function() {
		   	if (ajax.readyState == 1) { mensagem.innerHTML = "Alterando...<img src=\"../img/loading.gif\">"; }
           	if (ajax.readyState == 4) { 
				if (ajax.status == 200) { if (ajax.responseText) { mensagem.innerHTML = "Notícia alterada com sucesso."; } }
			}
       	}
	   	var codigo = "codigo="+valor;
	   	var titulo = "titulo="+document.formNoticia.txtTitulo.value;
	   	var descricao = "descricao="+document.formNoticia.txtDescricao.value;
	   	var params = codigo+"&"+titulo+"&"+descricao;
	   	ajax.send(params);
    }
}