function verify(campos){
	arr = campos.split('|');
	total = arr.length;
	i = 0;
	validado = 1;

	while( i<total ){
		campo = $("#"+arr[i]);
		valor = campo.val();
		titulo = campo.attr('title');
		alt = campo.attr('alt');
		
		if( titulo=='' || typeof(titulo)=='undefined' ){
			msg = 'Algum campo precisa ser preenchido.';
		} else {
			msg = 'O campo "'+titulo+'" precisa ser preenchido.';
		}
		
		if( valor=='' ){
			alert(msg);
			
			try{
				campo.attr("class", "vermelha");
				//alert( campo.attr('id') );
				campo.focus();
				begin();
				
			} catch(e){ alert('Erro na função javascript.'); }
			
			validado = 0;
			return false;

		} else {
			if( alt=='email' ){
				if (valor.indexOf("@") == -1 || valor.indexOf(".") == -1) {
					alert("Preencha com um e-mail válido.");
					
					try{
						campo.attr("class", "vermelha");
						campo.focus();
						begin();
						
					} catch(e){ alert('Erro na função javascript.'); }
					
					validado = 0;
					return false;
				} else {
					validado=1;
				}
				
			} else {
				validado = 1;
			}
			
		}
		
		i++;
	}
}

function acessa(ID){
	var id = ID;

	$.ajax({
			type: "POST",
			url: "resposta.php",
			dataType: "html",
			data: "id="+id,
			success: function(msg){												
				$("#ajax_load").fadeOut("slow", function(){
					$("#ajax_load").html(msg).fadeIn("slow");
				});
			},
			beforeSend: function(){
				$("#ajax_load").html('<table  align="left" width="609" border="0" cellspacing="0" cellpadding="0"><tr><td valign="middle" height="23" colspan="3" style="padding-left:15px" class="at_top"><span class="txt_w">Carregando</span></td></tr><tr><td><table width="609" border="0" cellspacing="0" cellpadding="0" class="a_base"><tr><td rowspan="2" align="center" valign="middle" height="120"><img src="images/lightbox-ico-loading.gif" />&nbsp;</td></tr></table></tr></td><tr><td  class="a_bottom" align="left" colspan="3">&nbsp;</td></tr></table>').fadeIn("slow");
			}
	})
}

jQuery(function($){
	//Navegação inicial.php
	$(".acesso_ajax").click(function(){
		acessa($(this).attr('id'));
	});
	
	//Associe-se
	$("#form1").submit(function(){
		nome = $("#nome").val();
		empresa = $("#empresa").val();
		area = $("#area").val();
		cidade = $("#cidade").val();
		email = $("#email").val();
		telefone = $("#telefone").val();
		obs = $("#obs").val();
		cpf = $("#cpf").val();
		cnpj =  $("#cnpj").val();

		verify('nome|empresa|area|cidade|email|telefone|cpf|cnpj');		
		
		if( validado ){
			$.ajax({
				type: "POST",
				
				url: "associese_enviar.php",
				
				dataType: "html",
				
				data: "controle=1&nome=" +nome+ "&empresa=" +empresa+ "&area=" +area+ "&cidade=" +cidade+ "&email=" +email+ "&telefone=" +telefone+ "&obs=" +obs+"&cpf=" +cpf+ "&cnpj="+cnpj,
				
				success: function(msg){												
					$("#ajax_load").fadeOut("slow", function(){
						msg = msg.split("|");
						sucesso = msg[0];
						mensagem = msg[1];
						
						if(sucesso==1){
							$("#formulario_html").fadeOut("slow", function(){
								$("#ajax_load").html(mensagem).fadeIn("slow");
							});
						} else {
							$("#ajax_load").html(mensagem).fadeIn("slow");
						}
					});
				},
				
				beforeSend: function(){
					$("#ajax_load").html('<img src="images/lightbox-ico-loading.gif" /><br />Carregando').fadeIn("slow");
				}
			});
		}
		
		return false;

	});
	
	//Cadastre-se
	$("#form2").submit(function(){
		nome = $("#nome").val();
		email = $("#email").val();
		
		verify('nome|email');
		
		if( validado ){
			$.ajax({
				type: "POST",
				
				url: "cadastre_enviar.php",
				
				dataType: "html",
				
				data: "controle=1&nome=" +nome+ "&email=" +email,
				
				success: function(msg){												
					$("#ajax_load").fadeOut("slow", function(){
						msg = msg.split("|");
						sucesso = msg[0];
						mensagem = msg[1];
						
						if(sucesso==1){
							$("#formulario_html").fadeOut("slow", function(){
								$("#ajax_load").html(mensagem).fadeIn("slow");
							});
						} else {
							$("#ajax_load").html(mensagem).fadeIn("slow");
						}
					});
				},
				
				beforeSend: function(){
					$("#ajax_load").html('<img src="images/lightbox-ico-loading.gif" /><br />Carregando').fadeIn("slow");
				}
			});
		}
		
		return false;
	});
	
	
	//Contato
	$("#form3").submit(function(){
		assunto = $("#assunto").val();
		nome = $("#nome").val();
		cidade = $("#cidade").val();
		email = $("#email").val();
		telefone = $("#telefone").val();
		mensagem = $("#mensagem").val();
		
		verify('nome|cidade|email|telefone');
		
		if( validado ){
			$.ajax({
				type: "POST",
				
				url: "contato_enviar.php",
				
				dataType: "html",
				
				data: "controle=1&nome=" +nome+ "&assunto=" +assunto+ "&cidade=" +cidade+ "&email=" +email+ "&telefone=" +telefone+ "&mensagem=" +mensagem,
				
				success: function(msg){												
					$("#ajax_loade").fadeOut("slow", function(){
						msg = msg.split("|");
						sucesso = msg[0];
						retorno = msg[1];
						
						if(sucesso==1){
							$("#formulario_html").fadeOut("slow", function(){
								$("#ajax_loade").html(retorno).fadeIn("slow");
							});
						} else {
							$("#ajax_loade").html(retorno).fadeIn("slow");
						}
					});
				},
				
				beforeSend: function(){
					$("#ajax_loade").html('<img src="images/lightbox-ico-loading.gif" /><br />Carregando').fadeIn("slow");
				}
			});
		}
		
		return false;
	});
	
	//Orçamento
	$("#form4").submit(function(){
		checks = $("input:checked");
		i = 0;
		check = new Array();
		while( i< checks.length ){
			check[i] = checks.eq(i).val();
			i++;
		}

		nome = $("#nome").val();
		cidade = $("#cidade").val();
		email = $("#email").val();
		telefone = $("#telefone").val();
		mensagem = $("#mensagem").val();
		foner = $("#foner").val();
		fonec = $("#fonec").val();
		celular = $("#celular").val();
		dataf = $("#dataf").val();
		tipof = $("#tipof").val();
		localf = $("#localf").val();
		convidados = $("#convidados").val();
		pesquisa = $("#pesquisa").val();


		verify('titulo|nome|foner|fonec|celular|dataf|tipof|localf|convidados|cidade|email|pesquisa');
		
		if( validado ){
			$.ajax({
				type: "POST",
				
				url: "orcamentos_enviar.php",
				
				dataType: "html",
				
				data: "controle=1&nome=" +nome+ "&check=" +check+ "&cidade=" +cidade+ "&email=" +email+ "&mensagem=" +mensagem+ "&foner=" + foner + "&fonec=" + fonec + "&celular=" + celular + "&dataf=" + dataf + "&tipof=" + tipof + "&localf=" + localf + "&convidados=" + convidados + "&pesquisa=" + pesquisa,
				
				success: function(msg){												
					$("#ajax_loade").fadeOut("slow", function(){
						msg = msg.split("|");
						sucesso = msg[0];
						retorno = msg[1];
						
						if(sucesso==1){
							$("#formulario_html").fadeOut("slow", function(){
								$("#ajax_loade").html(retorno).fadeIn("slow");
							});
						} else {
							$("#ajax_loade").html(retorno).fadeIn("slow");
						}
					});
				},
				
				beforeSend: function(){
					$("#ajax_loade").html('<img src="images/lightbox-ico-loading.gif" /><br />Carregando').fadeIn("slow");
				}
			});
		}
		
		return false;
	});
});