// JavaScript Document

function getId(id)
{
	return document.getElementById(id);
}

function mostraId(id)
{
	$("#"+id).show("fast");
}

function escondeId(id)
{
	$("#"+id).hide("fast");
}

function mostraEscondeId(id)
{
	if(getId(id).style.display=='none')
	{
		$("#"+id).show("fast");
	}
	else
	{
		$("#"+id).hide("fast");
	}
}

function checkMail(mail){
	var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
	if(typeof(mail) == "string"){
			if(er.test(mail)){ return true; }
	}else if(typeof(mail) == "object"){
			if(er.test(mail.value)){
									return true;
							}
	}else{
			return false;
			}
}

//===========================================================================

function ampliaImagem(id_imagem)
{
	xajax_ampliaImagem(id_imagem);
}

function enviaContato()
{
	var nome = document.getElementById('nome');
	var email = document.getElementById('email');
	var telefone = document.getElementById('telefone');
	var cidade = document.getElementById('cidade');
	var assunto = document.getElementById('assunto');	
	
	var mensagem = document.getElementById('mensagem');
	
	if(nome.value=='')
	{
		alert("Verifique o campo 'Nome'.");
		nome.focus();
		return false;
	}
	if(!checkMail(email.value))
	{
		alert("Verifique seu 'Email'.");
		email.focus();
		return false;
	}
	if(cidade.value=='')
	{
		alert("Preencha sua 'Cidade'.");
		cidade.focus();
		return false;
	}
	if(mensagem.value=='')
	{
		alert("Preencha o campo 'Mensagem'.");
		mensagem.focus();
		return false;
	}
	
	mostraId('enviando');
	xajax_enviaContato(nome.value, email.value, telefone.value, cidade.value, assunto.value, mensagem.value);
}

//===========================================================================

function buscaCeps()
{
	var cep = getId('txtCep');
	var id_cidade = getId('id_cidade');
	if(cep.value.length < 3)
	{
		alert('Preencha o campo de busca corretamente!');
		cep.focus();
		return false;
	}
	getId('ocorrencias').innerHTML = 'Carregando...';
	xajax_buscaCeps(id_cidade.value, cep.value);
}

//===========================================================================

function atualizaCloudSearch(id_cidade)
{
	document.getElementById('titleCloud').innerHTML = "Atualizando...";
	xajax_atualizaCloudSearch(id_cidade);
}

function efetuaBusca(id_cidade, palavra)
{
	xajax_efetuaBusca(id_cidade, palavra);
}

function mostraFotos(id_assinante)
{
	document.getElementById('fotos'+id_assinante).style.display = 'block';
	//document.getElementById('carregando'+id_assinante).style.display = 'block';
	//xajax_mostraFotos(id_assinante);
}

function reportaErro(id_assinante)
{
	var elementos = $('.erroAssinante');
	var i = 0;
	while(i < elementos.length)
	{
		elementos[i].style.display = 'none';
		++i;
	}
	mostraId('carregando'+id_assinante);
	xajax_reportaErro(id_assinante);
}

function insertErroAssinante(id_assinante)
{
	var texto = getId('txtErro'+id_assinante);
	var email = getId('txtEmail'+id_assinante);
	
	if(texto.value.length < 5)
	{
		alert('Descreva o erro.');
		texto.focus();
		return false;
	}
	if(!checkMail(email.value))
	{
		alert('Preencha seu email corretamente!');
		email.focus();
		return false;
	}
	xajax_insertErroAssinante(id_assinante, texto.value, email.value);
}