$(document).ready(function() {

	$("#formPadrao").validate({
		submitHandler: 
			function(form) {
			$(form).ajaxSubmit({
				target: "#voluntario-resposta",
				url: "index.php?option=com_voluntario&task=enviar&Itemid=10000",
				beforeSubmit: function () {
					if ($("input[name=disponibilidade_viagem]:checked").val()==undefined) {
						alert("Preencha sua disponibilidade para viagens.");
						return false;
					}
                    if ($("input[name=disponibilidade_viagem]:checked").val()==undefined) {
						alert("Preencha sua disponibilidade para viagens.");
						return false;
					}
					return true;
				},
				success: function (responseText, statusText) {
					responseText = $.trim(responseText);
					var resultado = responseText.substr(0, 1);
					if(resultado == "1"){
						$("#voluntario-resposta").html( responseText.substr(2) );
						$(".quadroDestaque").css("display", "none");
						document.location.href += "#";
					} else{
						alert("Os seguintes erros foram encontrados: \n"+responseText);
					}
				}
			});
		return false;
		}
	});

	$(".ddd").mask("99",{placeholder:" "});
	$(".telefone").mask("9999-9999",{placeholder:" "});
	$("input[name=cpf]").mask("999.999.999-99",{placeholder:" "});
	$("input[name=dt_nascimento]").mask("99/99/9999",{placeholder:" "});
	$("input[name=cep]").mask("99999-999",{placeholder:" "});

	$("#id_estado").change(function(){
		mostraCidades();
	});
	
	$("#empresa_id_estado").change(function(){
		mostraCidades("empresa_");
	});

	mostraCidades();
	mostraCidades("empresa_");


	$(".show-campo-texto").click(function () {
		if ($(this).attr("checked")!=undefined) {
			$(this).parent().next().removeClass("d-none");
		} else {
			$(this).parent().next().attr("class", "d-none");
		}
	});
	
	function voluntarioTrExcluir(){
		var tabela = $(this).parent().parent().parent();
		if( tabela.find("tr").is(":only-child") ){
			tabela.find("tr").find("input").val("");
		}else{
			$(this).parent().parent().remove();
		}
		return false;
	}
	
	$(".bt-clone").click(function () {
		var tabela = $(this).parent().prev();
		var trNovo = tabela.find(".tr-clone:first").clone();
		trNovo.find("input").val("");
		trNovo.appendTo(tabela);
		$(".voluntarioTrExcluir").click(voluntarioTrExcluir);
	});
	
	$(".voluntarioTrExcluir").click(voluntarioTrExcluir);
	
	//Cadastro
	$("#formVoluntarioLogin").validate({
		submitHandler: 
			function(form) {
				$(form).ajaxSubmit({
				success: function (responseText, statusText) {
					responseText = $.trim(responseText);
					var resultado = responseText.substr(0, 1);
					if(resultado == "1"){
						window.location = responseText.substr(2);
					} else{
						$("#formVoluntarioLogin").find("input").val("");
						alert(responseText);
					}
				}
				});
				return false;
			}
	});
	$("#formVoluntarioEstado").validate({
		submitHandler: 
			function() {
				window.location = $("#selectPortal").val();
				return false;
			}
	});
	$("#formVoluntarioSenha").validate({
		submitHandler: 
			function(form) {
				$(form).ajaxSubmit({
					beforeSubmit: function () {
					$("#formVoluntarioSenhaErro").hide();
					return true;
				},
				success: function (responseText, statusText) {
					if(responseText == 1){
						$("#formVoluntarioSenha").html('');
						$("#formVoluntarioSenhaOk").show();
					} else{
						$("#formVoluntarioSenha").find("input").val("");
						$("#formVoluntarioSenhaErro").html(responseText);
						$("#formVoluntarioSenhaErro").show();
					}
				}
				});
				return false;
			}
	});
});

function mostraCidades(prefix) {
	if (prefix==undefined) {
		prefix = "";
	}
	var elementIdName = "#"+prefix+"id_cidade"; 
	$(elementIdName).html('<option value="">Carregando...</option>');

	$.get("index.php", { option: "com_cidade", id_estado: $("#"+prefix+"id_estado").val(), Itemid: '10000', id_cidade: $(elementIdName+"_hidden").val()},
		function(data){
			$(elementIdName).html(data);
			var value = $(elementIdName+"_hidden").val();
			$(elementIdName + " option[value=" + value + "]").attr("selected", "selected");
			$(elementIdName+"_hidden").val(0);
		}
	);
}
