$(document).ready(function() {

	// ESCONDE AS MENSAGENS DE ERRO ====================================
	    
	    $("#sucesso").hide();
	    $("#erro_nome").hide();
		$("#erro_email").hide();
		$("#erro_formato_email").hide();
		$("#loader").hide();
		//$("#nome_news").val("Nome");
		//$("#email_news").val("E-mail");
		//$("#tel").val("Fone");
		//==================================================================
		/* 
		
		$("#cponome").focus(function(){
		$(this).val("");							 
		});
		
		$("#cpoemail").focus(function(){
		$(this).val("");							 
		});
		
		$("#tel").focus(function(){
		$(this).val("");							 
		});
		
		*/
		// SELECIONA O EVENTO ==============================================
		$("#envia").click(function() {
			// RECEBE OS VALORES DOS CAMPOS ====================================
				var nome = $("#nome_news").val();
				var email = $("#email_news").val();
				//==================================================================
				// TESTA/TRATA OS CAMPOS ESTÃO VAZIOS ==============================
				if (nome == "") {
					$("#erro_nome").slideDown('slow');
					$("#nome_news").css('backgroundColor', 'red');
					$("#nome_news").keypress(function() {
						$("#erro_nome").slideUp('slow');
						$("#nome_news").css('backgroundColor', 'gray');
					});

					var invalido = 1;
				}
				if (email == "") {
					$("#erro_email").slideDown('slow');
					$("#email_news").css('backgroundColor', 'red');
					$("#email_news").keypress(function() {
						$("#erro_email").slideUp('slow');
						$("#email_news").css('backgroundColor', 'gray');
					});
					invalido = 1;
				}
				if (nome == "" && email == "") {
					invalido = 1;
					$("#nome_news").css('backgroundColor', 'red');
					$("#email_news").css('backgroundColor', 'red');
					$("#erro_nome").slideDown('slow');
					$("#erro_email").slideDown('slow');

					//alert("Os Campos NOME e EMAIL Devem Ser Preenchidos!");
					$("#nome_news").keypress(function() {
						$("#erro_nome").slideUp('slow');
						$("#nome_news").css('backgroundColor', 'gray');
					});
					$("#email_news").keypress(function() {
						$("#erro_email").slideUp('slow');
						$("#email_news").css('backgroundColor', 'gray');
					});

				}
				//==================================================================
				// SE OS CAMPOS ESTIVEREM PREENCHIDOS =================================
				if (invalido != 1) {

					// TESTA SE O EMAIL TEM FORMATO VÁLIDO =============================
					var RegExp = /^[\w]+@[\w]+\.[\w|\.]+$/;

					if (RegExp.test(email) == true) {
						var emailcerto = 1;
					} else {
						//alert("erro"+email);
						$("#erro_formato_email").slideDown('slow');
						$("#email_news").css('backgroundColor', 'red');
						$("#email_news").keypress(function() {
							$("#erro_formato_email").slideUp('slow');
							$("#email_news").css('backgroundColor', 'gray');
						});
					}
					//================================================================
					// AJAX ==========================================================
					if (emailcerto == 1) {

						// MANIPULA O DOM ==================================================
						$("#envia").slideUp('slow');
						$("#email").slideUp('slow');
						$("#nome").slideUp('slow');
						$("#loader").slideDown('slow');
						//==================================================================

						/**/$.get('insere_contato.php', {
							nome : nome,
							email : email
						}, function() {
							alert("Bem vindo " + nome
									+ ", Você Receberá As Novidades Do PORTAL DO EQUINO por email!");
							$("#loader").slideUp('slow');
							$("#nome").slideDown('slow');
							$("#email").slideDown('slow');
							$("#envia").slideDown('slow');

						})
						// APAGA OS VALORES DOS CAMPOS ==============================
						$("#nome_news").val("");
						$("#email_news").val("");
						//===========================================================

					}//FIM DO IF DE AJAX ==========================================
					//================================================================

				}
				//==================================================================

			});//FIM DO EVENTO ONCLICK =============================================

		//==================================================================

	});
