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

//functions javascript
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;

	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 = new Ajax.Updater( 'post'+inNumero, stUrl, { method: 'get', parameters: stParametros, onSuccess:mostraResposta( inNumero, idNoticia ) });
	}
}

//limpa campos após requisição ajax
function mostraResposta( inNumero, idNoticia ) 
{
	//mostra sucesso
	alert("Dados enviados com sucesso!");
	
	document.getElementById('stNome'+inNumero).value = '';
	document.getElementById('stEmail'+inNumero).value = '';
	document.getElementById('stComentario'+inNumero).value = '';
	document.getElementById('stNome'+inNumero).focus();
	
	//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' )
	{
		stMsgErro = 'Nome inválido.';
		document.getElementById('stNome').focus();
		alert(stMsgErro);
	}else if( stEmail == '' || stEmail.indexOf('@') == -1 || stEmail.indexOf('.') == -1 || stEmail == 'E-mail' )
	{
		stMsgErro = 'E-mail inválido.';
		document.getElementById('stEmail').focus();
		alert(stMsgErro);
	}else{	
		var stUrl = "recebeFormSimplesAjax.php";
		var stParametros = "boAjax=true&stNome="+stNome+"&stEmail="+stEmail;
		var obAjax = new Ajax.Updater( 'cadastre-se', stUrl, { method: 'post', parameters: stParametros, onSuccess:mostraRespostaFormSimples() });
	}
}

//limpa campos após requisição ajax
function mostraRespostaFormSimples() 
{
	alert("Dados enviados com sucesso!");
	document.frm.stNome.value = '';
	document.frm.stEmail.value = '';
	document.getElementById('lkCadastrese').focus();		
}

//trocar as galerias da index
function trocaGalerias( inId )
{
	var stUrl = "miniGaleriaAjax.php";
	var stParametros = "inPagina="+inId;
	var obAjax = new Ajax.Updater( 'lateral-fotos', stUrl, { method: 'post', parameters: stParametros, onLoading: loading() });
}

//mostra o gif de carregando
function loading(){
	$('lateral-fotos').innerHTML = "<div align='center'><div style='width:80px; height:60.33px;'>&nbsp</div><div style='width:80px; height:60.33px;'><img src='img/loading.gif' /></div><div style='width:80px; height:60.33px;'>&nbsp;</div></div>";
}

//trocar as fotos de destaque da galeria
function trocaFoto( inId )
{
	var stUrl = "trocaFotoAjax.php";
	var stParametros = "idFoto="+inId;
	var obAjax = new Ajax.Updater( 'coluna-direita-galeria', stUrl, { method: 'post', parameters: stParametros, onLoading: loadingTrocaFoto() });
}

//mostra o gif de carregando
function loadingTrocaFoto(){
	$('coluna-direita-galeria').innerHTML = "<div align='center'><div style='width:166px; height:150px;'>&nbsp;</div><div style='width:166px; height:150px;'><img src='img/loading.gif' /></div><div style='width:166px; height:150px;'>&nbsp;</div></div>";
}

//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;
	}
}

//trocar as fotos de destaque da galeria
function trocaFotoProximaAnterior( inIdGaleria, inIdFoto, boAcao )
{
	var stUrl = "trocaFotoAjax.php";
	var stParametros = "idGaleria="+inIdGaleria+"&idFoto="+inIdFoto+"&boAcao="+boAcao;
	var obAjax = new Ajax.Updater( 'coluna-direita-galeria', stUrl, { method: 'post', parameters: stParametros, onLoading: loadingTrocaFoto() });
}

//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;
	}
	if( $('dtNascimento').value == '' || $('dtNascimento').value.length < 10 ){
		alert('Data de nascimento inválida.');
		$('dtNascimento').focus();
		return false;
	}
	if( $('stPais').value == '' ){
		alert('País inválido.');
		$('stPais').focus();
		return false;
	}
	if( $('stTelefone').value == '' ){
		alert('Telefone inválido.');
		$('stTelefone').focus();
		return false;
	}
	if( $('stCelular').value == '' ){
		alert('Celular inválido.');
		$('stCelular').focus();
		return false;
	}
	
	//verifica se foi marcado como conheceu a b2br
	boConheceu = false;
	for( i = 0; i < 5; i++ ){
		if ( document.cadastro.stConheceu[i].checked == true ){
			boConheceu = true;
		}
	}
	if( !boConheceu ){
		alert('Selecione como conheceu a B2BR.');
		return false;
	}
	
	//verifica se foi marcado a relação com o esporte
	boRelacao = false;
	for( i = 0; i < 5; i++ ){
		if ( document.cadastro.stRelacao[i].checked == true ){
			boRelacao = true;
		}
	}
	if( !boRelacao ){
		alert('Selecione sua relação com o esporte.');
		return false;
	}
	
	//verifica se foi marcado oque quer ver no portal
	boQuerver = false;
	for( i = 0; i < 5; i++ ){
		if ( document.cadastro.stQuerver[i].checked == true ){
			boQuerver = true;
		}
	}
	if( !boQuerver ){
		alert('Selecione oque você quer ver em nosso portal.');
		return false;
	}
}