/*
* Programador: Tiago Camargo Finger
* E-mail: tiagocfinger@gmail.com
*/

//functions javascript
function substr_count(string,substring,start,length)
{
 var c = 0;
 if(start) { string = string.substr(start); }
 if(length) { string = string.substr(0,length); }
 for (var i=0;i<string.length;i++)
 {
  if(substring == string.substr(i,substring.length))
  c++;
 }
 return c;
}

function enviarForm( idNoticia, inNumero ){
			
	var stNome = document.getElementById('stNome'+inNumero).value;
	var stEmail = document.getElementById('stEmail'+inNumero).value;
	var stComentario = document.getElementById('stComentario'+inNumero).value;
	var emailer = document.getElementById('emailer'+inNumero).value;
    var bool = true;
	
	if (substr_count(stComentario,'href') > 1 )
	   {
		   alert('span');
		bool = false;
	   }
	
	if (emailer != "" ||  idNoticia == '' || idNoticia == 0)
	   {
		alert('span');
		bool = false;
	   }
	
	if( stNome == '' )
	{
		stMsgErro = 'Nome inválido.';
		document.getElementById('stNome'+inNumero).focus();
		document.getElementById('stNome'+inNumero).style.border=' 1px solid #ff0000';
		document.getElementById("nome-obrigatorio").style.display = "none";
		document.getElementById("nome-obrigatorio2").style.display = "";
		bool =false;
		
	
	}else {document.getElementById('stNome'+inNumero).style.border=' 1px solid #CCCCCC';document.getElementById("nome-obrigatorio").style.display = "";
		document.getElementById("nome-obrigatorio2").style.display = "none";}
	
	
	if( stEmail == '' || stEmail.indexOf('@') == -1 || stEmail.indexOf('.') == -1 )
	{
		stMsgErro = 'E-mail inválido.';
		if (bool==true){document.getElementById('stEmail'+inNumero).focus();}
		document.getElementById('stEmail'+inNumero).style.border=' 1px solid #ff0000';
		document.getElementById("email-obrigatorio").style.display = "none";
		document.getElementById("email-obrigatorio2").style.display = "";
		bool =false;
		//alert(stMsgErro);
	}else {document.getElementById('stEmail'+inNumero).style.border=' 1px solid #CCCCCC';document.getElementById("email-obrigatorio").style.display = "";
		document.getElementById("email-obrigatorio2").style.display = "none";}
	if( stComentario == '' )
	{
		stMsgErro = 'Comentário inválido.';
		if (bool==true){document.getElementById('stComentario'+inNumero).focus();}
		document.getElementById('stComentario'+inNumero).style.border=' 1px solid #ff0000';
		document.getElementById("comentario-obrigatorio").style.display = "none";
		document.getElementById("comentario-obrigatorio2").style.display = "";
		bool =false;
		//alert(stMsgErro);
	}else{document.getElementById('stComentario'+inNumero).style.border=' 1px solid #CCCCCC';document.getElementById("comentario-obrigatorio").style.display = "";
		document.getElementById("comentario-obrigatorio2").style.display = "none";}
	
	if (bool == true)	
		{
		var stUrl = "/sitenovob2br/recebeFormAjaxa.php";
		var stParametros = "boAjax=true&idNoticia="+idNoticia+"&stNome="+stNome+"&stEmail="+stEmail+"&stComentario="+stComentario;
		var obAjax = jQuery.ajax({
						data: "",
						url: stUrl,												
						data: stParametros,
						type: "POST",
						success: mostraResposta(inNumero, idNoticia)
					  });
		
	}
}
function enviarForm2( idNoticia, inNumero ){
			
	var stNome = document.getElementById('stNome'+inNumero).value;
	var stEmail = document.getElementById('stEmail'+inNumero).value;
	var stComentario = document.getElementById('stComentario'+inNumero).value;

	if( stNome == '' )
	{
		stMsgErro = 'Nome inválido.';
		document.getElementById('stNome'+inNumero).focus();
		alert(stMsgErro);
	}else if( stEmail == '' || stEmail.indexOf('@') == -1 || stEmail.indexOf('.') == -1 )
	{
		stMsgErro = 'E-mail inválido.';
		document.getElementById('stEmail'+inNumero).focus();
		alert(stMsgErro);
	}else if( stComentario == '' )
	{
		stMsgErro = 'Comentário inválido.';
		document.getElementById('stComentario'+inNumero).focus();
		alert(stMsgErro);
	}else{
		var stUrl = "recebeFormAjax.php";
		var stParametros = "boAjax=true&idNoticia="+idNoticia+"&stNome="+stNome+"&stEmail="+stEmail+"&stComentario="+stComentario;
		var obAjax = jQuery.ajax({
						url: stUrl,												
						data: stParametros,
						//type: "GET",
						success: mostraResposta(inNumero, idNoticia)
					  });
		
	}
}

//limpa campos após requisição ajax
function mostraResposta( inNumero, idNoticia ) 
{
	//mostra sucesso
	//alert("Dados enviados com sucesso!");
	//document.getElementById("envcoma").style.display = "none";
	document.getElementById("envcomb").style.display = "";
	document.getElementById('stNome'+inNumero).value = '';
	document.getElementById('stEmail'+inNumero).value = '';
	document.getElementById('stComentario'+inNumero).value = '';
	//document.getElementById('stNome'+inNumero).focus();
	document.location.href="#ANCORA";
	
	//atualiza total de posts
	//var stUrl = "trocaNumeroComentarios.php";
	//var stParametros = "boAjax=true&idNoticia="+idNoticia;
	//var obAjax = new Ajax.Updater( 'spn'+inNumero, stUrl, { method: 'get', parameters: stParametros });
}

//mostra e Escondo combo de datas anteriores
function mostraEsconde()
{
	if( document.getElementById('aAnteriores').style.visibility == "visible" )
	{
		document.getElementById('aAnteriores').style.visibility = "hidden";
		document.getElementById('cmbDataNoticia').style.visibility = "visible";
	}else
	{
		document.getElementById('aAnteriores').style.visibility = "visible";
		document.getElementById('cmbDataNoticia').style.visibility = "hidden";
	}
}

//cadastro básico da index
function cadastra(){
	
	stNome = document.getElementById('stNome').value;
	stEmail = document.getElementById('stEmail').value;
	
	if( stNome == '' || stNome == 'Nome' || stNome == 'Seu nome')
	{
		document.getElementById("nomeob").style.display = "";
		document.getElementById("emailob").style.display = "none";
		document.getElementById("checkob").style.display = "none";
		document.getElementById("succes").style.display = "none";
		stMsgErro = 'Nome inválido.';
		document.getElementById('stNome').focus();
		//alert(stMsgErro);
	}else if( stEmail == '' || stEmail.indexOf('@') == -1 || stEmail.indexOf('.') == -1 || stEmail == 'E-mail' || stEmail == 'Seu email' )
	{
		document.getElementById("nomeob").style.display = "none";
		document.getElementById("emailob").style.display = "";
		document.getElementById("checkob").style.display = "none";
		document.getElementById("succes").style.display = "none";
		stMsgErro = 'E-mail inválido.';
		document.getElementById('stEmail').focus();
		//alert(stMsgErro);
	}
	//else if (document.getElementById('aceito').checked == false){
		//	stMsgErro = 'Você deve marcar o checkbox abaixo dos campos nome e email antes de se cadastrar, para autorizar o recebimento de emails da B2BR.';
		  //  document.getElementById("nomeob").style.display = "none";
			//document.getElementById("emailob").style.display = "none";
			//document.getElementById("checkob").style.display = "";
			//document.getElementById("succes").style.display = "none";
			//document.getElementById('stEmail').focus();
		    //alert(stMsgErro);
			
	//}
	   else{			
	
		var stUrl = "/sitenovob2br/recebeFormSimplesAjax.php";
		
		var stParametros = "boAjax=true&stNome="+stNome+"&stEmail="+stEmail;
		var obAjax = jQuery.ajax({
						data: "",								 
						url: stUrl,												
						data: stParametros,
						type: "POST",
						success: mostraRespostaFormSimples(),
					  });
	}
}

//limpa campos após requisição ajax
function mostraRespostaFormSimples() 
{
	//alert("Dados enviados com sucesso!");
	document.getElementById("nomeob").style.display = "none";
	document.getElementById("emailob").style.display = "none";
	document.getElementById("checkob").style.display = "none";
	document.getElementById("succes").style.display = "";
	document.getElementById('newsletter_form').stNome.value = '';
	document.getElementById('newsletter_form').stEmail.value = '';		
}

//valida form busca
function enviaBusca(){
	if ($('buscar').value == '' || $('buscar').value == 'Buscar no site' ){
		alert('Digite uma palavra para a busca.');
		$('buscar').focus();
		return false;
	}else{
		return true;
	}
}

//verifica cambo embranco
function verificaEmBranco( inId, stMsg ){
	if( document.getElementById(inId).value == '' ){
		alert(stMsg);
		return false;
	}else{
		return true;
	}
}


//verifica cambo em branco e manda para galeria
function selecionaMandaGaleria( inValor ){
	if( inValor != '' ){
		document.getElementById('mandaGaleria').href = 'galeria.php?idGaleria='+inValor;
		eval(document.getElementById('mandaGaleria').click);
	}
}

//verifica cambo em branco e manda para galeria
function selecionaManda( inId, inValor ){
	if( inValor != '' ){
		document.getElementById(inId).href = 'galeria.php?idGaleria='+inValor;
		eval(document.getElementById(inId).click);
	}
}

function validaFrmFaleConosco(){
	if( $('stNomeFC').value == '' ){
		alert('Nome inválido.');
		$('stNomeFC').focus();
		return false;
	}
	else if( $('stEmailFC').value == '' || $('stEmailFC').value.indexOf('@') == -1 || $('stEmailFC').value.indexOf('.') == -1 ){
		alert('E-mail inválido.');
		$('stEmailFC').focus();
		return false;
	}
	else if( $('stAssuntoFC').value == '' ){
		alert('Assunto inválido.');
		$('stAssuntoFC').focus();
		return false;
	}
	else if( $('stMensagemFC').value == '' ){
		alert('Mensagem inválida.');
		$('stMensagemFC').focus();
		return false;
	}
	else{
		return true;
	}
}

function validaFrmCadastro(){
	if( $('stNome').value == '' ){
		alert('Nome inválido.');
		$('stNome').focus();
		return false;
	}
	if( $('stEmail').value == '' || $('stEmail').value.indexOf('@') == -1 || $('stEmail').value.indexOf('.') == -1 ){
		alert('E-mail inválido.');
		$('stEmail').focus();
		return false;
	}
	return true;
}
