// JavaScript Document
$(document).ready(function() {
    // Aquí dentro você faz a chamada para suas funções.
    $("#loading").hide("slow");
    $("#loading_biblia").hide("slow");

    /* envio do form */
	$("#form1").submit(function() {
		var enviar_ok = true;
		var form_name = $(this).attr('name');

		$("#loading").show();

		/* checar campos */
		$('#'+form_name+' :input[@title=requerido] ').each(function(){
			if($.trim($("#"+this.id).val()) == ''){
				$("#"+this.id).css({ background: "red" });
				enviar_ok = false;
			} else {
				$("#"+this.id).css({ background: "#B8F5B1",color: "#000" });
			}
		});

		if(enviar_ok) {
			var options = {
				success: function(msg) {
					$("#loading").hide("slow");
					// sucesso no envio
					if(msg == "-1") {
						alert('E-mail enviado com sucesso. Em breve retornaremos. Obrigado!');
						$('#'+form_name).resetForm();
					} else {
						$("#resultado").html(msg);
					}
				}
			};

			$(this).ajaxSubmit(options);

			return false; // faz o submit normal
		} else {
			$("#loading").hide("slow");
			alert('Preencha os campos obrigatórios.');
			return false; //cancela submit normal
		}
	});
     // biblia
   // selecionou o livro
   $("#biblia_livro").change(function() {
      $("#loading_biblia").show();
	  if($("#biblia_livro").val() != '') {
	  	  $("#biblia_cap").attr({ disabled: "true"});
		  $.ajax({
			 type: "POST",
			 url: "ajax_add_conteudo.php",
			 data: "act=cap&livro="+this.value,
			 success: function(msg){
				$("#biblia_cap").html(msg);
				$("#biblia_cap").removeAttr("disabled");
				$("#loading_biblia").hide("slow");
				$("#biblia_cap").focus();
			 },
			 error: function(obj, msg, exception){
				$("#biblia_cap").html("<option value=''>Erro!</option>");
				$("#biblia_ver").html("<option value=''>--</option>");
				$("#biblia_ver").attr({ disabled: "true"});
				$("#loading_biblia").hide("slow");
			 }
		  });
	   } else {
	   	  $("#loading_biblia").hide();
		  $("#biblia_cap").html("<option value='' selected='selected'>--</option>");
		  $("#biblia_cap").attr({ disabled: "true"});
		  $("#biblia_ver").html("<option value='' selected='selected'>--</option>");
		  $("#biblia_ver").attr({ disabled: "true"});
		  $("#biblia_livro").focus();
	   }
   });

   // selecionou o cap
   $("#biblia_cap").change(function() {
      $("#loading_biblia").show();
	  if($("#biblia_cap").val() != '') {
	  	  $("#biblia_ver").attr({ disabled: "true"});
		  $.ajax({
			 type: "POST",
			 url: "ajax_add_conteudo.php",
			 data: "act=ver&cap="+this.value+"&livro="+$("#biblia_livro").val(),
			 success: function(msg){
				$("#biblia_ver").html(msg);
				$("#biblia_ver").removeAttr("disabled");
				$("#loading_biblia").hide("slow");
				$("#biblia_ver").focus();
			 },
			 error: function(obj, msg, exception){
				$("#biblia_ver").html("<option value=''>Erro!</option>");
				$("#loading_biblia").hide("slow");
			 }
		  });
	   } else {
	   	  $("#loading_biblia").hide();
		  $("#biblia_ver").html("<option value=''>--</option>");
		  $("#biblia_ver").attr({ disabled: "true"});
		  $("#biblia_cap").focus();
	   }
   });

   // faz a busca na biblia
   $("#busca_biblia").click(function() {
		if($("#biblia_cap").val() != '' && $("#biblia_livro").val() != "" && $("#biblia_ver").val() != "" ) {
			$("#busca_da_biblia").submit();
		} else {
			alert("Digite algo para Buscar!");
		}
   });
});
