$(document).ready(function(){
	function verifica()
	{
		var RegExp = /\b[\w]+@[\w]+\.[\w]+/;
		if ($("#infoNome")[0] && $("#infoNome").val() == "")
		{
			alert("Por favor preencha o seu nome.");		
			return false;
		}
		else if ($("#infoEmail")[0] && $("#infoEmail").val() == "")
		{
			alert("Por favor preencha o seu email.");
			return false;
		}
		else if ($("#infoEmail")[0] && $("#infoEmail").val().search(RegExp) == -1)
		{
			alert("O endereço de e-mail informado não é válido.");	
			return false;		
		}				
		else if ($("#name")[0] && $("#name").val() == "")
		{
			alert("O campo Nome é obrigatório");
			$("#name").focus();
			return false;
		}
		else if ($("#empresa")[0] && $("#empresa").val() == "")
		{
			alert("O campo Empresa é obrigatório");
			$("#empresa").focus();
			return false;
		}
		else if ($("#cargo")[0] && $("#cargo").val() == "")
		{
			alert("O campo Cargo é obrigatório");
			$("#cargo").focus();
			return false;
		}
		else if ($("#email")[0] && $("#email").val().search(RegExp) == -1)
		{
			alert("Email digitado não é válido, favor verificar");
			$("#email").focus();
			return false;		
		}
		else if ($("#email")[0] && $("#email").val() == "")
		{
			alert("O campo Email é obrigatório");
			$("#email").focus();
			return false;
		}
		else if ($("#telefone")[0] && $("#telefone").val() == "")
		{
			alert("O campo Telefone é obrigatório");
			$("#telefone").focus();
			return false;
		}
		else if ($("#cidade")[0] && $("#cidade").val() == "")
		{
			alert("O campo Cidade é obrigatório");
			$("#cidade").focus();
			return false;
		}
		else if ($("#estado")[0] && $("#estado").val() == "")
		{
			alert("O campo Estado é obrigatório");
			$("#estado").focus();
			return false;
		}
		else if ($("#pais")[0] && $("#pais").val() == 0)
		{
			alert("O campo País é obrigatório");
			$("#pais").focus();
			return false;
		}
		else if ($("#assunto")[0] && $("#assunto").val() == "")
		{
			alert("O campo Assunto é obrigatório");
			$("#assunto").focus();
			return false;
		}		
		else if ($("#mensagem")[0] && $("#mensagem").val() == "")
		{
			alert("O campo Mensagem é obrigatório");
			$("#mensagem").focus();
			return false;
		}
		else if ($("#git_razao_social")[0] && $("#git_razao_social").val() == "")
		{
			alert("O campo Razão social é obrigatório");
			$("#git_razao_social").focus();
			return false;
		}
		else if ($("#git_endereco")[0] && $("#git_endereco").val() == "")
		{
			alert("O campo Endereço é obrigatório");
			$("#git_endereco").focus();
			return false;
		}
		else if ($("#git_cnpj")[0] && $("#git_cnpj").val() == "")
		{
			alert("O campo CNPJ é obrigatório");
			$("#git_cnpj").focus();
			return false;
		}
		else if ($("#git_ins_estadual")[0] && $("#git_ins_estadual").val() == "")
		{
			alert("O campo Inscrição Estadual é obrigatório");
			$("#git_ins_estadual").focus();
			return false;
		}
		else if ($("#git_ins_municipal")[0] && $("#git_ins_municipal").val() == "")
		{
			alert("O campo Inscrição Municipal é obrigatório");
			$("#git_ins_municipal").focus();
			return false;
		}
		else if ($("#git_contato")[0] && $("#git_contato").val() == "")
		{
			alert("O campo Contato é obrigatório");
			$("#git_contato").focus();
			return false;
		}
		else if ($("#git_email")[0] && $("#git_email").val().search(RegExp) == -1)
		{
			alert("Email digitado não é válido, favor verificar");
			$("#git_email").focus();
			return false;		
		}
		else if ($("#git_email")[0] && $("#git_email").val() == "")
		{
			alert("O campo Email é obrigatório");
			$("#git_email").focus();
			return false;
		}
		else if ($("#git_telefone")[0] && $("#git_telefone").val() == "")
		{
			alert("O campo Telefone é obrigatório");
			$("#git_telefone").focus();
			return false;
		}
		else if ($(".git_cont")[0] && $(".git_cont").is(':checked') == false)
		{
			alert("O campo Contribuinte de ICMS é obrigatório");
			$(".git_cont").focus();
			return false;
		}
		else
		{
			return true;
		}
	}
	
    var $form = $('.formulario_dinamico');
    $form.submit(function(e)
    {
        e.preventDefault();
        if (verifica())
        {   
	     	if( $("#mensagem_retorno").size() > 0 ){
        		$form.find('input[type=submit]').val('Enviando...');
        	}
			
			$.ajax({
				async: false,
				url: $(this).attr('action'),
				data: $(this).serialize(),
				dataType: 'json',
				type: 'POST',
				complete: function(result)
	            {
	                window.location.href = 'http://' + window.location.host + '/' + window.location.pathname.split('/')[1] + '/notificacao';
	            }
			});
        }
    });
	
    function valida() {
    	var RegExp = /\b[\w]+@[\w]+\.[\w]+/;
		if ($("#ideNome")[0] && $("#ideNome").val() == "")
		{
			alert("O campo nome é de preenchimento obrigatório, por favor, verifique!");		
			return false;
		}
		else if ($("#ideEmail")[0] && $("#ideEmail").val() == "")
		{
			alert("O campo email é de preenchimento obrigatório, por favor, verifique!");
			return false;
		}
		else if ($("#ideEmail")[0] && $("#ideEmail").val().search(RegExp) == -1)
		{
			alert("O endereço de e-mail informado não é válido.");	
			return false;		
		}
		if ($("#textIdeia")[0] && $("#textIdeia").val() == "")
		{
			alert("O campo ideia é de preenchimento obrigatório, por favor, verifique!");		
			return false;
		}
		if ($("#categoriaIdeia")[0] && $("#categoriaIdeia").val() == "")
		{
			alert("O campo categoria é de preenchimento obrigatório, por favor, verifique!");		
			return false;
		}else
		{
			return true;
		}
    }
    
	$("#formTiveUmaIdeia").submit(function(e)
	{
		e.preventDefault();
		
		if (valida())
        {  
			$container = $(this);
	
			if( ! _recaptcha($container) ){
				return false;
			}
			
			$.post($(this).attr('action'), $(this).serialize(), function(result){
			        if(result.status == "true")
			        {		        					        		        
		                //alert("Sua ideia foi enviada com sucesso, aguardando moderação.");
	                
						$("#ideNome").val("");
		                $("#ideoEmail").val("");
		                $("#textIdeia").html("");
		                $("#textIdeia").val("");
		                $("#categoriaIdeia").val("");		
		                
		                window.location = 'http://' + window.location.host + '/' + window.location.pathname.split('/')[1] + '/notificacao';
			        }
			        else if (result.status == "erro")
			        {
			            //alert("Todos os campos são obrigatórios.");
			            alert(result.mensagem);
			        }
			        else
			        {
			            alert("Não foi possível enviar sua ideia, por favor, tente novamente.");
			        }
					
					$container.find('#recaptcha_challenge_field,#recaptcha_response_field').remove();
			}, "JSON");
        }
	});
	
	/*$("#form-jornalistas").submit(function()
	{
		var erro = false;
		var msg = "";
		if ($("#name").val() == "")
		{
			erro = true;
			msg = "O campo nome Ã?Â© obrigatÃ?Â³rio";
		}
		if ($("#veiculo").val() == "")
		{
			erro = true;
			msg = "O campo veÃ?Â­culo Ã?Â© obrigatÃ?Â³rio";
		}
		if ($("#funcao").val() == "")
		{
			erro = true;
			msg = "O campo funÃ?Â§Ã?Â£o Ã?Â© obrigatÃ?Â³rio";
		}
		if ($("#email").val() == "")
		{
			erro = true;
			msg = "O campo email Ã?Â© obrigatÃ?Â³rio";
		}
		if ($("#telefone").val() == "")
		{
			erro = true;
			msg = "O campo telefone Ã?Â© obrigatÃ?Â³rio";
		}
		
		if (erro == true)
		{
			$("#msg_retorno").html(msg);
			$("#msg_retorno").show();
			return false;
		}
	});*/
	
//	$("#telefone").mask("(99) 9999-9999");
    
});

function resetForm(id) {
	$('#'+id).each(function(){
	        this.reset();
	});
}
