$(document).ready(function(){
	$('.btn-envia').click(function(){
		contato.envia();
	});
});

function goTo(url){
	
	window.open(url, '_blank');
	
}

/* [CONTATO][FORM] Submit */
var contato = {
	
	erro : 0,
	
	envia : function(){
		
		contato.enviando_msg();
		
		contato.erro = 0;
		
		// Nome
		var n = '';
		n = $('#contato-nome').val();
		
		// E-mail
		var e = '';
		e = $('#contato-email').val();
		
		// Assunto
		var a = '';
		a = $('#contato-assunto').val();
        
        // Telefone
		var t = '';
		t = $('#contato-telefone').val();
		
		// Mensagem
		var m = '';
		m = $('#contato-mensagem').val();

		
		if(contato.erro == 0 && (n == 'Nome' || n == '')){
			contato.erro = 1;
			$('#contato-nome').focus();
			contato.erro_msg('Preencha o campo nome.');
			return false;
		}else{
			contato.erro = 0;
		}
		
		if(contato.erro == 0 && (e == 'E-mail' || e == '')){
			contato.erro = 1;
			$('#contato-email').focus();
			contato.erro_msg('Preencha o campo e-mail.');
			return false;
		}else if(e !== '' && e !== 'E-mail'){
			contato.erro = 0;
		}
		
        if(contato.erro == 0 && (t == 'Telefone' || t == '')){
			contato.erro = 1;
			$('#contato-telefone').focus();
			contato.erro_msg('Preencha o campo telefone.');
			return false;
		}else if(t !== '' && t !== 'Telefone'){
			contato.erro = 0;
		}
        
		if(contato.erro == 0 && (a == 'Assunto' || a == '')){
			contato.erro = 1;
			$('#contato-assunto').focus();
			contato.erro_msg('Preencha o campo assunto.');
			return false;
		}else if(a !== '' && a !== 'Assunto'){
			contato.erro = 0;
		}
		
		if(contato.erro == 0 && (m == 'Mensagem' || m == '')){
			contato.erro = 1;
			$('#contato-mensagem').focus();
			contato.erro_msg('Preencha o campo mensagem.');
			return false;
		}else if(m !== '' && m !== 'Mensagem'){
			contato.erro = 0;
		}
		
		if(contato.erro == 0){
			
			contato.enviando_msg();
			
			$.post('http://www.caffedigital.com.br/home/envia_contato',{n:n,e:e,a:a,m:m,t:t},function(data){

				if(data == 0){
					contato.erro_msg('Desculpe, n&atilde;o foi poss&iacute;vel enviar os dados.');
				} else if (data == 2){
					contato.erro_msg('E-mail inv&aacute;lido.');
				}else{
					contato.sucesso_msg('Mensagem enviada com sucesso!');
				}
			
			});
			
		}
	},
	
	sucesso_msg : function(msg){
		
		$('.btn-envia').hide();
		$('.contato-enviando').hide();
		$('.contato-erro').hide();
		$('.contato-sucesso').hide();
		
		$('.contato-sucesso p').html(msg);
		$('.contato-sucesso').show();
		
		setTimeout('$(\'.contato-sucesso\').hide();$(\'.btn-envia\').show();',3000);
		
		$('#contato-nome').val('');
		$('#contato-email').val('');
        $('#contato-telefone').val('');
		$('#contato-assunto').val('');
		$('#contato-mensagem').val('');
	
	},
	
	erro_msg : function(msg){
		
		$('.btn-envia').hide();
		$('.contato-enviando').hide();
		$('.contato-erro').hide();
		$('.contato-sucesso').hide();
		
		$('.contato-erro p').html(msg);
		$('.contato-erro').show();
		
		setTimeout('$(\'.contato-erro\').hide();$(\'.btn-envia\').show();',3000)
	
	},
	
	enviando_msg : function(){
		
		$('.btn-envia').hide();
		$('.contato-erro').hide();
		$('.contato-sucesso').hide();
		$('.contato-enviando').show();
		
	}
	
}

